mad_zlib.c

Go to the documentation of this file.
00001 
00033 #include <stdlib.h>
00034 #include <stdio.h>
00035 #include <string.h>
00036 #include <math.h>
00037 
00038 #include <zlib.h>
00039 
00040 #include "mad_zlib.h"
00041 
00042 #ifdef USE_ZLIB
00043 
00054 int gz_compress(FILE *in, gzFile out) {
00055 
00056     char buf[ZLIB_BUFLEN];
00057     int len;
00058     int err;
00059 
00060     for(;;) {
00061                 memset(buf, 0, ZLIB_BUFLEN);
00062         len = (int)fread(buf, 1, sizeof(buf), in);
00063         
00064                 if(ferror(in)) {
00065                 printf("Error: fread failed in gz_compress\n");
00066                         fflush(stdout);
00067             return -1;
00068         }
00069 
00070         if(len == 0) {
00071                         break;
00072                 }
00073 
00074         if(gzwrite(out, buf, (unsigned int)len) != len) {
00075                         printf("Error: %s\n", gzerror(out, &err));
00076                         fflush(stdout);
00077                         return -1;
00078                 }
00079     }
00080 
00081     if(gzclose(out) != Z_OK) {
00082                 printf("Error: gzclose failed in gz_compress\n");
00083                 fflush(stdout);
00084                 return -1;
00085         } 
00086         
00087         if(fclose(in)) {
00088                 printf("Error: fclose failed in gz_compress\n");
00089                 fflush(stdout);
00090                 return -1;
00091         }
00092 
00093         return 0;
00094 }
00095 
00106 int gz_uncompress(gzFile in, FILE *out) {
00107 
00108     char buf[ZLIB_BUFLEN];
00109     int len;
00110     int err;
00111 
00112     for(;;) {
00113         len = gzread(in, buf, sizeof(buf));
00114 
00115         if(len < 0) {
00116                         printf("Error: %s\n", gzerror(out, &err));
00117                         fflush(stdout);
00118                         return -1;
00119                 }
00120         
00121                 if(len == 0) {
00122                         break;
00123                 }
00124 
00125         if((int)fwrite(buf, 1, (unsigned int)len, out) != len) {
00126             printf("Error: fwrite failed in gz_compress\n");
00127                         fflush(stdout);
00128             return -1;
00129         }
00130     }
00131 
00132     if(fclose(out)) {
00133                 printf("Error: fclose failed in gz_compress\n");
00134                 fflush(stdout);
00135                 return -1;
00136         }
00137 
00138     if(gzclose(in) != Z_OK) {
00139                 printf("Error: gzclose failed in gz_compress\n");
00140                 fflush(stdout);
00141                 return -1;
00142         }
00143 
00144         return 0;
00145 }
00146 
00147 int file_gzip_compress(char *file, char *mode) {
00148     
00149         char outfile[MAX_PATH_LENGTH];
00150     FILE  *in;
00151     gzFile out;
00152         int retval = 0;
00153 
00154         memset(outfile, 0, MAX_PATH_LENGTH);
00155 
00156     strcpy(outfile, file);
00157     strcat(outfile, GZ_SUFFIX);
00158 
00159     if((in = fopen(file, "rb")) == NULL) {
00160         printf("Error: unable to fopen inputfile: %s\n", file);
00161                 fflush(stdout);
00162                 return -1;
00163     }
00164 
00165     if((out = gzopen(outfile, mode)) == NULL) {
00166                 printf("Error: unable to gzopen outputfile: %s\n", outfile);
00167                 fflush(stdout);
00168                 return -1;
00169     }
00170 
00171     retval = gz_compress(in, out);
00172 
00173         return retval;
00174 }
00175 
00176 int file_gzip_uncompress(char* file) {
00177 
00178     char buf[MAX_PATH_LENGTH];
00179     char *infile;
00180     char *outfile;
00181 
00182     FILE  *out;
00183     gzFile in;
00184     uInt len;
00185 
00186     int retval = 0;
00187 
00188     len = (uInt)strlen(file);
00189 
00190     memset(buf, 0, MAX_PATH_LENGTH);
00191     strcpy(buf, file);
00192 
00193     infile = file;
00194     outfile = buf;
00195     outfile[len - (GZ_SUFFIX_LEN)] = '\0';
00196 
00197     if((in = gzopen(infile, "rb")) == NULL) {
00198         printf("Error: unable to gzopen inputfile: %s\n", infile);
00199                 fflush(stdout);
00200                 return -1;
00201     }
00202    
00203     if((out = fopen(outfile, "wb")) == NULL) {
00204         printf("Error: unable to fopen outputfile: %s\n", outfile);
00205                 fflush(stdout);
00206                 return -1;
00207     }
00208 
00209     retval = gz_uncompress(in, out);
00210 
00211         if(retval == -1) {
00212                 printf("infile: %s, outfile: %s\n", infile, outfile);
00213         }
00214 
00215     remove(infile);
00216 
00217         return retval;
00218 }
00219 
00220 char* buffer_zlib_compress(char *buf, 
00221                                                         unsigned long long buflen, 
00222                                                         unsigned long long *comprlen
00223                                                    ) {
00224     
00225         int err;
00226 
00227         Byte *compr;
00228         uLong comprLen;
00229 
00230     comprLen =  (uLong)ceil((double)1.001*(double)(unsigned int)buflen) + 12;
00231 
00232         if((compr = (Byte*)calloc((uInt)comprLen, 1)) == Z_NULL) {
00233         printf("out of memory\n");
00234                 fflush(stdout);
00235         return NULL;
00236     }
00237 
00238         if((err = compress(compr, &comprLen, (const Bytef*)buf, (unsigned int)buflen)) != Z_OK) {
00239         printf("Error: %d in buffer_compress\n", err);
00240                 return NULL;
00241     }
00242 
00243         *comprlen = comprLen;
00244 
00245         return (char*)compr;
00246 }
00247 
00248 char* buffer_zlib_uncompress(char *buf,
00249                                                                 unsigned long long buflen, 
00250                                                                 unsigned long long *uncomprlen
00251                                                          ) {
00252     
00253         int err;
00254 
00255         Byte *uncompr = NULL;
00256         uLong uncomprLen = 5 * (unsigned int)buflen; /* TODO: size from where, now first allocate
00257                                                                                                  5*compressed buffer and then reallocated double
00258                                                                                                  amount if first size is not enough. */
00259 
00260         if((uncompr = (Byte*)calloc((uInt)uncomprLen, 1)) == Z_NULL) {
00261         printf("out of memory\n");
00262                 fflush(stdout);
00263         return NULL;
00264     }
00265 
00266         err = uncompress(uncompr, &uncomprLen, (const Bytef*)buf, (unsigned long)buflen);
00267 
00268         if(err != Z_OK) {
00269                 
00270                 if(err == Z_BUF_ERROR) {
00271                         free(uncompr);
00272                         
00273                         uncomprLen = 2 * uncomprLen;
00274 
00275                         if((uncompr = (Byte*)calloc((uInt)uncomprLen, 1)) == Z_NULL) {
00276                                 printf("out of memory\n");
00277                                 fflush(stdout);
00278                                 return NULL;
00279                         }
00280                         
00281                         err = uncompress(uncompr, &uncomprLen, (const Bytef*)buf, (unsigned long)buflen);
00282                         
00283                         if(err != Z_OK) {
00284                                 printf("Error: %d in buffer_uncompress\n", err);
00285                                 return NULL;
00286                         }
00287                 }
00288                 else {
00289                         printf("Error: %d in buffer_uncompress\n", err);
00290                         return NULL;    
00291                 }
00292         }
00293 
00294         *uncomprlen = uncomprLen;
00295 
00296         return (char*)uncompr;
00297 }
00298 
00299 char* buffer_gzip_uncompress(char *buf,
00300                                                                 unsigned long long buflen, 
00301                                                                 unsigned long long uncomprlen) {
00302 
00303         Byte *uncompr;
00304         int err;
00305     z_stream d_stream; /* decompression stream */
00306 
00307     d_stream.zalloc = Z_NULL;
00308     d_stream.zfree = Z_NULL;
00309     d_stream.opaque = Z_NULL;
00310 
00311     d_stream.next_in  = (Bytef*)buf;
00312     d_stream.avail_in = (uInt)buflen;
00313 
00314         if((uncompr = (Byte*)calloc(((uInt)buflen + (uInt)1), 1)) == Z_NULL) {
00315         printf("out of memory\n");
00316                 fflush(stdout);
00317         return NULL;
00318     }
00319     
00320         if((err = inflateInit2(&d_stream, -MAX_WBITS)) != Z_OK) {
00321           printf("Error: %d in inflateInit2\n", err);
00322       return NULL;
00323         }
00324 
00325     /*for (;;) {
00326         d_stream.next_out = uncompr;
00327         d_stream.avail_out = (uInt)buflen;
00328         err = inflate(&d_stream, Z_NO_FLUSH);
00329 
00330         if(err == Z_STREAM_END) {
00331                         break;
00332                 }
00333 
00334         if(err == Z_STREAM_ERROR) {
00335                         printf("internal stream error!\n");
00336                         return NULL;
00337                 }
00338         if(err == Z_MEM_ERROR) {
00339                         printf("out of memory\n");
00340                         return NULL;
00341                 }
00342         if(err == Z_DATA_ERROR) {
00343             printf("invalid compressed data\n");
00344                         return NULL;
00345                 }
00346     }
00347 
00348     if((err = inflateEnd(&d_stream)) != Z_OK) {
00349           printf("Error: %d in inflateEnd\n", err);
00350       return NULL;
00351         }
00352 
00353     if(d_stream.total_out != (uLong)(2*uncomprlen + buflen/2)) {
00354                 printf("uncompress error!\n");
00355                 fflush(stdout);
00356         return NULL;;
00357     }*/
00358 
00359     while(d_stream.total_out < (uLong)uncomprlen && d_stream.total_in < (uLong)buflen) {
00360         d_stream.avail_in = d_stream.avail_out = 1; 
00361         err = inflate(&d_stream, Z_BLOCK);/*Z_NO_FLUSH);*/
00362         
00363                 if(err == Z_STREAM_END) {
00364                         break;
00365                 }
00366 
00367         if(err == Z_STREAM_ERROR) {
00368                         printf("internal stream error!\n");
00369                         return NULL;
00370                 }
00371         if(err == Z_MEM_ERROR) {
00372                         printf("out of memory\n");
00373                         return NULL;
00374                 }
00375         if(err == Z_DATA_ERROR) {
00376             printf("invalid compressed data\n");
00377                         return NULL;
00378                 }
00379     }
00380 
00381     if((err = inflateEnd(&d_stream)) != Z_OK) {
00382           printf("Error: %d in inflateEnd\n", err);
00383       return NULL;
00384         }
00385 
00386         return (char*)uncompr;
00387 }
00388 
00389 #endif
00390 

Generated on Fri Mar 9 19:59:53 2007 for MAD-FCL by  doxygen 1.5.0