int ZEXPORT dread_file(FILE *input, zd_mem_buffer* buffer){ int nbread; assert(input!=NULL); assert(buffer!=NULL); zd_alloc(buffer, ZD_BUF_SIZE); for(;;){ /* read data */ nbread = fread((void *) buffer->pos, sizeof(Byte), (size_t) ZD_BUF_SIZE, input); buffer->pos=buffer->pos+nbread; /* check for end of file */ if(feof(input)){ return (buffer->pos - buffer->buffer); } /* check for io error */ if(ferror(input)){ zd_free(buffer); return ZD_ERRNO; } /* increase the buffer */ if(!zd_realloc(buffer, ZD_BUF_SIZE)){ zd_free(buffer); /* not enough memory */ return ZD_MEM_ERROR; } } }
size_t zd_memcpy(zd_mem_buffer *m, void *data, size_t n){ size_t avail=0; avail = (m->size - (m->pos - m->buffer)); if(avail < n) if(zd_realloc(m, (n-avail)+1)== 0) return 0; memcpy(m->pos, data, n); m->pos += n; return n; }
/* * same as above, but with dynamic memory allocation */ int ZEXPORT zd_compress1(const Bytef *ref, uLong rsize, const Bytef *tar, uLong tsize, Bytef **delta, uLongf *dsize) { int rval; zd_stream strm; zd_mem_buffer dbuf; /* the zstream output buffer should have size greater than zero try to * guess buffer size, such that no memory realocation will be needed */ if(!(*dsize)) *dsize = tsize/EXPECTED_RATIO + 64; /* *dsize should not be 0*/ /* init io buffers */ strm.base[0] = (Bytef*) ref; strm.base_avail[0] = rsize; strm.base_out[0] = 0; strm.refnum = 1; strm.next_in = (Bytef*) tar; strm.total_in = 0; strm.avail_in = tsize; /* allocate the output buffer */ zd_alloc(&dbuf, *dsize); strm.next_out = dbuf.pos; strm.total_out = 0; strm.avail_out = *dsize; strm.zalloc = (alloc_func)0; strm.zfree = (free_func)0; strm.opaque = (voidpf)0; /* init huffman coder */ rval = zd_deflateInit(&strm, 1 ); //ZD_DEFAULT_COMPRESSION); if (rval != ZD_OK) { fprintf(stderr, "%s error: %d\n", "deflateInit", rval); return rval; } /* compress the data */ while((rval = zd_deflate(&strm,ZD_FINISH)) == ZD_OK){ /* set correctly the mem_buffef internal pointer */ dbuf.pos = strm.next_out; /* allocate more memory */ zd_realloc(&dbuf,dbuf.size); /* restore zstream internal pointer */ strm.next_out = (uch*)dbuf.pos; strm.avail_out = dbuf.size - strm.total_out; } /* set correcty the mem_buffer pointers */ dbuf.pos = strm.next_out; if(rval != ZD_STREAM_END){ fprintf(stderr, "%s error: %d\n", "deflateInit", rval); zd_free(&dbuf); zd_deflateEnd(&strm); return rval; } *delta = dbuf.buffer; *dsize = (uLong) strm.total_out; /* release memory */ return zd_deflateEnd(&strm); }
/* * same as above with dynamic memory allocation */ int ZEXPORT zd_uncompressN1(const Bytef *ref[], uLong rsize[], int rnum, Bytef **tar, uLongf *tsize, const Bytef *delta, uLong dsize) { int rval; int f = ZD_SYNC_FLUSH; int i; zd_mem_buffer tbuf; zd_stream strm; /* zdelta: tsize must not be 0; try to guess a good output buffer size */ if(!(*tsize)) *tsize = rsize[0]*2+64;/* *tsize should not be 0*/ /* init io buffers */ strm.refnum = rnum; /* init reference input buffers */ for(i = 0; i < rnum; ++i){ strm.base[i] = (Bytef*) ref[i]; strm.base_avail[i] = rsize[i]; strm.base_out[i] = 0; } /* allocate initial size delta buffer */ zd_alloc(&tbuf,*tsize); strm.avail_out = *tsize; strm.next_out = tbuf.buffer; strm.total_out = 0; strm.avail_in = dsize; strm.next_in = (Bytef*) delta; strm.total_in = 0; strm.zalloc = (alloc_func)0; strm.zfree = (free_func)0; strm.opaque = (voidpf)0; zd_inflateInit(&strm); while((rval = zd_inflate(&strm,f)) == ZD_OK){ /* add more output memory */ if(strm.avail_out!=0){ rval = ZD_STREAM_END; break; } /* set correctly the mem_buffer internal pointer */ tbuf.pos = strm.next_out; /* allocate more memory */ zd_realloc(&tbuf,tbuf.size); /* restore zstream internal pointer */ strm.next_out = tbuf.pos; strm.avail_out = tbuf.size - strm.total_out; } /* set correctly the mem_buffer internal pointer */ tbuf.pos = strm.next_out; if(rval != ZD_STREAM_END){ zd_free(&tbuf); if(strm.msg!=NULL) fprintf(stderr,"%s\n",strm.msg); zd_inflateEnd(&strm); return rval; } *tar = tbuf.buffer; *tsize = strm.total_out; /* free memory */ return zd_inflateEnd(&strm); }