padding_encoding.c

Go to the documentation of this file.
00001 
00033 #include <stdlib.h>
00034 #include <stdio.h>
00035 #include <errno.h>
00036 #include <string.h>
00037 #include <sys/types.h>
00038 #include <sys/stat.h>
00039 #include <fcntl.h>
00040 
00041 #ifdef _MSC_VER
00042 #include <io.h>
00043 #else
00044 #include <unistd.h>
00045 #endif
00046 
00047 #include "padding_encoding.h"
00048 
00049 int compute_padding_length(unsigned long long f_size, int block_len, int es_len) {
00050   int p_length;
00051 
00052   if(f_size <= (block_len * es_len)) {
00053           p_length = 0;
00054   }
00055   else if(!(f_size % (block_len * es_len))) {
00056           p_length = 0;
00057   }
00058   else {
00059           p_length = (int)((block_len * es_len) - f_size % (block_len * es_len));
00060   }
00061 
00062   return p_length;
00063 }
00064 
00065 int padding_decoder(char *file_name, unsigned long long content_length) {
00066 
00067   int fp_in;
00068   unsigned long long f_size;
00069 
00070 #ifdef _MSC_VER
00071   struct __stat64 file_stats;
00072 #else  
00073   struct stat64 file_stats;
00074 #endif
00075 
00076   char file_name_in[256] = "";
00077   char file_name_out[256] = "";
00078   char *ptr;
00079   int retval;
00080 
00081   strcpy(file_name_in, file_name);  
00082   ptr = strstr(file_name_in, PAD_SUFFIX);
00083 
00084   memcpy(file_name_out, file_name_in, (ptr - file_name_in));
00085 
00086 #ifdef _MSC_VER
00087   fp_in = open((const char*)file_name_in, _O_RDWR | _O_CREAT | _O_BINARY, _S_IREAD | _S_IWRITE);
00088 #else
00089   fp_in = open64(file_name_in, O_RDWR | O_CREAT, S_IRWXU);
00090 #endif
00091 
00092   if(fp_in == -1) {
00093     printf("open error: %s\n", file_name_in);
00094     return -1;
00095   }
00096 
00097 #ifdef _MSC_VER
00098   _fstat64(fp_in, &file_stats);
00099 #else
00100   fstat64(fp_in, &file_stats);
00101 #endif
00102 
00103   f_size = file_stats.st_size;
00104 
00105   if(f_size > content_length) {
00106 #ifdef _MSC_VER
00107     retval = _chsize(fp_in, (long)content_length); /* TODO: 64 bits, how ??? */
00108 #else
00109           retval = ftruncate64(fp_in, content_length);
00110 #endif
00111 
00112           if(retval != 0) {
00113                   printf("Problem in padding decoding.\n" );
00114                   close(fp_in);
00115                   return -1;
00116           }
00117   }
00118 
00119   close(fp_in);
00120          
00121         if(rename(file_name_in, file_name_out) < 0) {
00122 
00123                 if(errno == EEXIST) {
00124                         retval = remove(file_name_out);
00125                 
00126                         if(retval == -1) {    
00127                                 printf("errno: %i\n", errno);
00128                                 fflush(stdout);
00129                                 close(fp_in);
00130                                 return -1;
00131                         }
00132                 
00133                         if(rename(file_name_in, file_name_out) < 0) {
00134                                 printf("rename() error1: %s\n", file_name_in);
00135                                 fflush(stdout);
00136                                 close(fp_in);
00137                                 return -1;
00138                         }
00139                 }
00140                 else {
00141                         printf("rename() error2: %s\n", file_name_in);
00142                         fflush(stdout);
00143                         close(fp_in);
00144                         return -1;
00145                 }
00146         }
00147 
00148   return 0;
00149 }
00150 

Generated on Fri Mar 9 20:08:52 2007 for MAD-FCL by  doxygen 1.5.0