📄 padding_encoding.c
字号:
/* $Author: peltotal $ $Date: 2006/02/17 08:07:17 $ $Revision: 1.2 $ *//* * MAD-FLUTELIB: Implementation of FLUTE protocol. * Copyright (c) 2003-2006 TUT - Tampere University of Technology * main authors/contacts: jani.peltotalo@tut.fi and sami.peltotalo@tut.fi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include "flute_inc.h"#ifdef WIN32int compute_padding_length(ULONGLONG f_size, int block_len, int es_len) {#elseint compute_padding_length(unsigned long long f_size, int block_len, int es_len) {#endif int p_length; if(f_size <= (block_len * es_len)) { return 0; } p_length = (int)((block_len * es_len) - f_size % (block_len * es_len)); return p_length;}int padding_decoder(char *file_name,#ifdef WIN32 ULONGLONG content_length#else unsigned long long content_length#endif ) { int fp_in;#ifdef WIN32 ULONGLONG f_size; struct __stat64 file_stats;#else unsigned long long f_size; struct stat64 file_stats;#endif char file_name_in[256] = ""; char file_name_out[256] = ""; char *ptr; int retval; strcpy(file_name_in, file_name); ptr = strstr(file_name_in, PAD_SUFFIX); memcpy(file_name_out, file_name_in, (ptr - file_name_in));#ifdef WIN32 fp_in = open((const char*)file_name_in, _O_RDWR | _O_CREAT | _O_BINARY, _S_IREAD | _S_IWRITE);#else fp_in = open64(file_name_in, O_RDWR | O_CREAT, S_IRWXU);#endif if(fp_in == -1) { printf("open error: %s\n", file_name_in); return -1; }#ifdef WIN32 _fstat64(fp_in, &file_stats);#else fstat64(fp_in, &file_stats);#endif f_size = file_stats.st_size; if(f_size > content_length) {#ifdef WIN32 retval = _chsize(fp_in, (long)content_length); /* TODO: 64 bits, how ??? */#else retval = ftruncate64(fp_in, content_length);#endif if(retval != 0) { printf("Problem in padding decoding.\n" ); close(fp_in); return -1; } } close(fp_in); if(rename(file_name_in, file_name_out) < 0) { if(errno == EEXIST) { retval = remove(file_name_out); if(retval == -1) { printf("errno: %i\n", errno); fflush(stdout); close(fp_in); return -1; } if(rename(file_name_in, file_name_out) < 0) { printf("rename() error1: %s\n", file_name_in); fflush(stdout); close(fp_in); return -1; } } else { printf("rename() error2: %s\n", file_name_in); fflush(stdout); close(fp_in); return -1; } } return 0;}/*int padding_encoder(char *file_name, int b_length, int s_length) { int fp_in, p_length;#ifdef WIN32 ULONGLONG f_size;#else unsigned long long;#endif struct stat64 file_stats; char file_name_out[256] = "";#ifdef WIN32 fp_in = open((const char*)file_name, _O_RDWR | _O_CREAT | _O_BINARY, _S_IREAD | _S_IWRITE);#else fp_in = open(file_name, O_RDWR | O_CREAT, S_IRWXU);#endif if(fp_in == -1) { perror("open"); return -1; } strcpy(file_name_out, file_name); strcat(file_name_out, PAD_SUFFIX); fstat(fp_in, &file_stats); f_size = file_stats.st_size; printf("File length before padding encoding: %llu\n", f_size); if ( f_size <= (b_length * s_length)) { close(fp_in); return 1; } p_length = (b_length * s_length) - f_size % (b_length * s_length);#ifdef WIN32 if(_chsize(fp_in, f_size+p_length) == 0) {#else if(ftruncate64(fp_in, f_size+p_length) == 0) {#endif fstat(fp_in, &file_stats); f_size = file_stats.st_size; printf("File length after padding encoding: %llu\n", f_size); close(fp_in); rename(file_name, file_name_out); } else { printf("Problem in padding encoding.\n" ); close(fp_in); return -1; } return 1;}*//*int padding_encoder(char *file_name, int b_length, int s_length) { int fp_in, fp_out, f_size, p_length; struct stat file_stats; char file_name_out[256] = ""; char line[10240]; int bytes;#ifdef WIN32 fp_in = open((const char*)file_name, _O_RDONLY | _O_BINARY);#else fp_in = open(file_name, O_RDONLY);#endif if(fp_in == -1) { printf("open error: %s\n", file_name); return -1; } strcpy(file_name_out, file_name); strcat(file_name_out, PAD_SUFFIX); if((fp_out = open(file_name_out, O_WRONLY | O_CREAT, 0666)) == -1) { perror("open"); exit(-1); } while((bytes = read(fp_in, line, sizeof(line))) > 0) { write(fp_out, line, bytes); } fstat(fp_in, &file_stats); close(fp_in); f_size = (int)file_stats.st_size; printf("File length before padding encoding: %d\n", f_size); if ( f_size <= (b_length * s_length)) { printf("%s was padding encoded with ratio: 1.0 (no padding)\n", file_name); return -1; } p_length = (b_length * s_length) - f_size % (b_length * s_length);#ifdef WIN32 if(_chsize(fp_out, f_size+p_length) == 0) {#else if(ftruncate(fp_out, f_size+p_length) == 0) {#endif fstat(fp_out, &file_stats); f_size = (int)file_stats.st_size; printf("File length after padding encoding: %d\n", f_size); close(fp_out); } else { printf("Problem in padding encoding.\n" ); close(fp_out); return -1; } close(fp_out); return 1;}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -