| #include <stdio.h> |
| #include <errno.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include <limits.h> |
| #include "zlib.h" |
|
|
| #define GZBUFSIZE 115200 |
| #define BUFFINCR 28800 |
|
|
| |
| int uncompress2mem(char *filename, |
| FILE *diskfile, |
| char **buffptr, |
| size_t *buffsize, |
| void *(*mem_realloc)(void *p, size_t newsize), |
| size_t *filesize, |
| int *status); |
|
|
| int uncompress2mem_from_mem( |
| char *inmemptr, |
| size_t inmemsize, |
| char **buffptr, |
| size_t *buffsize, |
| void *(*mem_realloc)(void *p, size_t newsize), |
| size_t *filesize, |
| int *status); |
|
|
| int uncompress2file(char *filename, |
| FILE *indiskfile, |
| FILE *outdiskfile, |
| int *status); |
|
|
|
|
| int compress2mem_from_mem( |
| char *inmemptr, |
| size_t inmemsize, |
| char **buffptr, |
| size_t *buffsize, |
| void *(*mem_realloc)(void *p, size_t newsize), |
| size_t *filesize, |
| int *status); |
|
|
| int compress2file_from_mem( |
| char *inmemptr, |
| size_t inmemsize, |
| FILE *outdiskfile, |
| size_t *filesize, |
| int *status); |
|
|
|
|
| |
| int uncompress2mem(char *filename, |
| FILE *diskfile, |
| char **buffptr, |
| size_t *buffsize, |
| void *(*mem_realloc)(void *p, size_t newsize), |
| size_t *filesize, |
| int *status) |
|
|
| |
| |
| |
| |
| |
| { |
| int err, len; |
| char *filebuff; |
| z_stream d_stream; |
| |
| |
| |
| |
| |
| |
| const uLong nPages = (uLong)(*buffsize)/(uLong)UINT_MAX; |
| uLong iPage=0; |
| uInt outbuffsize = (nPages > 0) ? UINT_MAX : (uInt)(*buffsize); |
| |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| |
| filebuff = (char*)malloc(GZBUFSIZE); |
| if (!filebuff) return(*status = 113); |
|
|
| d_stream.zalloc = (alloc_func)0; |
| d_stream.zfree = (free_func)0; |
| d_stream.opaque = (voidpf)0; |
| d_stream.next_out = (unsigned char*) *buffptr; |
| d_stream.avail_out = outbuffsize; |
|
|
| |
| |
|
|
| err = inflateInit2(&d_stream, (15+16)); |
| if (err != Z_OK) return(*status = 414); |
|
|
| |
| for (;;) |
| { |
| len = fread(filebuff, 1, GZBUFSIZE, diskfile); |
| if (ferror(diskfile)) { |
| inflateEnd(&d_stream); |
| free(filebuff); |
| return(*status = 414); |
| } |
|
|
| if (len == 0) break; |
|
|
| d_stream.next_in = (unsigned char*)filebuff; |
| d_stream.avail_in = len; |
|
|
| for (;;) { |
| |
| err = inflate(&d_stream, Z_NO_FLUSH); |
|
|
| if (err == Z_STREAM_END ) { |
| break; |
| } else if (err == Z_OK ) { |
|
|
| if (!d_stream.avail_in) break; |
| |
| |
| |
| |
| if (iPage < nPages) |
| { |
| ++iPage; |
| d_stream.next_out = (unsigned char*)(*buffptr + iPage*(uLong)UINT_MAX); |
| if (iPage < nPages) |
| d_stream.avail_out = UINT_MAX; |
| else |
| d_stream.avail_out = (uInt)((uLong)(*buffsize) % (uLong)UINT_MAX); |
| } |
| else if (mem_realloc) { |
| *buffptr = mem_realloc(*buffptr,*buffsize + BUFFINCR); |
| if (*buffptr == NULL){ |
| inflateEnd(&d_stream); |
| free(filebuff); |
| return(*status = 414); |
| } |
|
|
| d_stream.avail_out = BUFFINCR; |
| d_stream.next_out = (unsigned char*) (*buffptr + *buffsize); |
| *buffsize = *buffsize + BUFFINCR; |
| } else { |
| inflateEnd(&d_stream); |
| free(filebuff); |
| return(*status = 414); |
| } |
| } else { |
| inflateEnd(&d_stream); |
| free(filebuff); |
| return(*status = 414); |
| } |
| } |
| |
| if (feof(diskfile)) break; |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
|
|
| |
| *filesize = d_stream.total_out; |
| |
| free(filebuff); |
| |
| err = inflateEnd(&d_stream); |
| if (err != Z_OK) return(*status = 414); |
| |
| return(*status); |
| } |
| |
| int uncompress2mem_from_mem( |
| char *inmemptr, |
| size_t inmemsize, |
| char **buffptr, |
| size_t *buffsize, |
| void *(*mem_realloc)(void *p, size_t newsize), |
| size_t *filesize, |
| int *status) |
|
|
| |
| |
| |
| |
| |
| { |
| int err; |
| z_stream d_stream; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| d_stream.zalloc = (alloc_func)0; |
| d_stream.zfree = (free_func)0; |
| d_stream.opaque = (voidpf)0; |
|
|
| |
| |
| err = inflateInit2(&d_stream, (15+16)); |
| if (err != Z_OK) return(*status = 414); |
|
|
| d_stream.next_in = (unsigned char*)inmemptr; |
| d_stream.avail_in = inmemsize; |
|
|
| d_stream.next_out = (unsigned char*) *buffptr; |
| d_stream.avail_out = *buffsize; |
|
|
| for (;;) { |
| |
| err = inflate(&d_stream, Z_NO_FLUSH); |
|
|
| if (err == Z_STREAM_END) { |
| break; |
| } else if (err == Z_OK ) { |
|
|
| if (mem_realloc) { |
| *buffptr = mem_realloc(*buffptr,*buffsize + BUFFINCR); |
| if (*buffptr == NULL){ |
| inflateEnd(&d_stream); |
| return(*status = 414); |
| } |
|
|
| d_stream.avail_out = BUFFINCR; |
| d_stream.next_out = (unsigned char*) (*buffptr + *buffsize); |
| *buffsize = *buffsize + BUFFINCR; |
|
|
| } else { |
| inflateEnd(&d_stream); |
| return(*status = 414); |
| } |
| } else { |
| inflateEnd(&d_stream); |
| return(*status = 414); |
| } |
| } |
|
|
| |
| if (filesize) *filesize = d_stream.total_out; |
|
|
| |
| err = inflateEnd(&d_stream); |
|
|
| if (err != Z_OK) return(*status = 414); |
| |
| return(*status); |
| } |
| |
| int uncompress2file(char *filename, |
| FILE *indiskfile, |
| FILE *outdiskfile, |
| int *status) |
| |
| |
| |
| { |
| int err, len; |
| unsigned long bytes_out = 0; |
| char *infilebuff, *outfilebuff; |
| z_stream d_stream; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| |
| infilebuff = (char*)malloc(GZBUFSIZE); |
| if (!infilebuff) return(*status = 113); |
|
|
| outfilebuff = (char*)malloc(GZBUFSIZE); |
| if (!outfilebuff) return(*status = 113); |
|
|
| d_stream.zalloc = (alloc_func)0; |
| d_stream.zfree = (free_func)0; |
| d_stream.opaque = (voidpf)0; |
|
|
| d_stream.next_out = (unsigned char*) outfilebuff; |
| d_stream.avail_out = GZBUFSIZE; |
|
|
| |
| |
|
|
| err = inflateInit2(&d_stream, (15+16)); |
| if (err != Z_OK) return(*status = 414); |
|
|
| |
| for (;;) |
| { |
| len = fread(infilebuff, 1, GZBUFSIZE, indiskfile); |
| if (ferror(indiskfile)) { |
| inflateEnd(&d_stream); |
| free(infilebuff); |
| free(outfilebuff); |
| return(*status = 414); |
| } |
|
|
| if (len == 0) break; |
|
|
| d_stream.next_in = (unsigned char*)infilebuff; |
| d_stream.avail_in = len; |
|
|
| for (;;) { |
| |
| err = inflate(&d_stream, Z_NO_FLUSH); |
|
|
| if (err == Z_STREAM_END ) { |
| break; |
| } else if (err == Z_OK ) { |
|
|
| if (!d_stream.avail_in) break; |
| |
| |
| if ((int)fwrite(outfilebuff, 1, GZBUFSIZE, outdiskfile) != GZBUFSIZE) { |
| inflateEnd(&d_stream); |
| free(infilebuff); |
| free(outfilebuff); |
| return(*status = 414); |
| } |
| bytes_out += GZBUFSIZE; |
| d_stream.next_out = (unsigned char*) outfilebuff; |
| d_stream.avail_out = GZBUFSIZE; |
|
|
| } else { |
| inflateEnd(&d_stream); |
| free(infilebuff); |
| free(outfilebuff); |
| return(*status = 414); |
| } |
| } |
| |
| if (feof(indiskfile)) break; |
| } |
|
|
| |
| if (d_stream.total_out > bytes_out) { |
| if ((int)fwrite(outfilebuff, 1, (d_stream.total_out - bytes_out), outdiskfile) |
| != (d_stream.total_out - bytes_out)) { |
| inflateEnd(&d_stream); |
| free(infilebuff); |
| free(outfilebuff); |
| return(*status = 414); |
| } |
| } |
|
|
| free(infilebuff); |
| free(outfilebuff); |
|
|
| err = inflateEnd(&d_stream); |
| if (err != Z_OK) return(*status = 414); |
| |
| return(*status); |
| } |
| |
| int compress2mem_from_mem( |
| char *inmemptr, |
| size_t inmemsize, |
| char **buffptr, |
| size_t *buffsize, |
| void *(*mem_realloc)(void *p, size_t newsize), |
| size_t *filesize, |
| int *status) |
|
|
| |
| |
| |
| |
| |
| { |
| int err; |
| z_stream c_stream; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| c_stream.zalloc = (alloc_func)0; |
| c_stream.zfree = (free_func)0; |
| c_stream.opaque = (voidpf)0; |
|
|
| |
| |
| |
| |
| err = deflateInit2(&c_stream, Z_BEST_SPEED, Z_DEFLATED, |
| (15+16), 8, Z_DEFAULT_STRATEGY); |
|
|
| if (err != Z_OK) return(*status = 413); |
|
|
| c_stream.next_in = (unsigned char*)inmemptr; |
| c_stream.avail_in = inmemsize; |
|
|
| c_stream.next_out = (unsigned char*) *buffptr; |
| c_stream.avail_out = *buffsize; |
|
|
| for (;;) { |
| |
| err = deflate(&c_stream, Z_FINISH); |
|
|
| if (err == Z_STREAM_END) { |
| break; |
| } else if (err == Z_OK ) { |
|
|
| if (mem_realloc) { |
| *buffptr = mem_realloc(*buffptr,*buffsize + BUFFINCR); |
| if (*buffptr == NULL){ |
| deflateEnd(&c_stream); |
| return(*status = 413); |
| } |
|
|
| c_stream.avail_out = BUFFINCR; |
| c_stream.next_out = (unsigned char*) (*buffptr + *buffsize); |
| *buffsize = *buffsize + BUFFINCR; |
|
|
| } else { |
| deflateEnd(&c_stream); |
| return(*status = 413); |
| } |
| } else { |
| deflateEnd(&c_stream); |
| return(*status = 413); |
| } |
| } |
|
|
| |
| if (filesize) *filesize = c_stream.total_out; |
|
|
| |
| err = deflateEnd(&c_stream); |
|
|
| if (err != Z_OK) return(*status = 413); |
| |
| return(*status); |
| } |
| |
| int compress2file_from_mem( |
| char *inmemptr, |
| size_t inmemsize, |
| FILE *outdiskfile, |
| size_t *filesize, |
| int *status) |
|
|
| |
| |
| |
| { |
| int err; |
| unsigned long bytes_out = 0; |
| char *outfilebuff; |
| z_stream c_stream; |
|
|
| if (*status > 0) |
| return(*status); |
|
|
| |
| outfilebuff = (char*)malloc(GZBUFSIZE); |
| if (!outfilebuff) return(*status = 113); |
|
|
| c_stream.zalloc = (alloc_func)0; |
| c_stream.zfree = (free_func)0; |
| c_stream.opaque = (voidpf)0; |
|
|
| |
| |
| |
| |
| err = deflateInit2(&c_stream, Z_BEST_SPEED, Z_DEFLATED, |
| (15+16), 8, Z_DEFAULT_STRATEGY); |
|
|
| if (err != Z_OK) return(*status = 413); |
|
|
| c_stream.next_in = (unsigned char*)inmemptr; |
| c_stream.avail_in = inmemsize; |
|
|
| c_stream.next_out = (unsigned char*) outfilebuff; |
| c_stream.avail_out = GZBUFSIZE; |
|
|
| for (;;) { |
| |
| err = deflate(&c_stream, Z_FINISH); |
|
|
| if (err == Z_STREAM_END) { |
| break; |
| } else if (err == Z_OK ) { |
|
|
| |
| if ((int)fwrite(outfilebuff, 1, GZBUFSIZE, outdiskfile) != GZBUFSIZE) { |
| deflateEnd(&c_stream); |
| free(outfilebuff); |
| return(*status = 413); |
| } |
| bytes_out += GZBUFSIZE; |
| c_stream.next_out = (unsigned char*) outfilebuff; |
| c_stream.avail_out = GZBUFSIZE; |
|
|
|
|
| } else { |
| deflateEnd(&c_stream); |
| free(outfilebuff); |
| return(*status = 413); |
| } |
| } |
|
|
| |
| if (c_stream.total_out > bytes_out) { |
| if ((int)fwrite(outfilebuff, 1, (c_stream.total_out - bytes_out), outdiskfile) |
| != (c_stream.total_out - bytes_out)) { |
| deflateEnd(&c_stream); |
| free(outfilebuff); |
| return(*status = 413); |
| } |
| } |
|
|
| free(outfilebuff); |
|
|
| |
| if (filesize) *filesize = c_stream.total_out; |
|
|
| |
| err = deflateEnd(&c_stream); |
|
|
| if (err != Z_OK) return(*status = 413); |
| |
| return(*status); |
| } |
|
|