DataRecord* decompress_record(DataRecord *r) { if (r->flag & COMPRESS_FLAG) { char scratch[QLZ_SCRATCH_DECOMPRESS]; int csize = qlz_size_compressed(r->value); if (csize != r->vsz) { fprintf(stderr, "broken compressed data: %d != %d, flag=%x\n", csize, r->vsz, r->flag); goto DECOMP_END; } int size = qlz_size_decompressed(r->value); char *v = malloc(size); if (v == NULL) { fprintf(stderr, "malloc(%d)\n", size); goto DECOMP_END; } int ret = qlz_decompress(r->value, v, scratch); if (ret != size) { fprintf(stderr, "decompress %s failed: %d != %d\n", r->key, ret, size); goto DECOMP_END; } if (r->free_value) { free(r->value); } r->value = v; r->free_value = true; r->vsz = size; r->flag &= ~COMPRESS_FLAG; } return r; DECOMP_END: free_record(r); return NULL; }
int stream_decompress(FILE *ifile, FILE *ofile) { char *file_data, *decompressed; size_t d, c, dc, fd_size, d_size; qlz_state_decompress *state_decompress = (qlz_state_decompress *)malloc(sizeof(qlz_state_decompress)); // a compressed packet can be at most MAX_BUF_SIZE + BUF_BUFFER bytes if it // was compressed with this program. fd_size = MAX_BUF_SIZE + BUF_BUFFER; file_data = (char*) malloc(fd_size); // allocate decompression buffer d_size = fd_size - BUF_BUFFER; decompressed = (char*) malloc(d_size); // allocate and initially zero out the scratch buffer. After this, make sure it is // preserved across calls and never modified manually memset(state_decompress, 0, sizeof(qlz_state_decompress)); // read 9-byte header to find the size of the entire compressed packet, and // then read remaining packet while((c = fread(file_data, 1, 9, ifile)) != 0) { // Do we need a bigger decompressed buffer? If the file was compressed // with segments larger than the default in this program. dc = qlz_size_decompressed(file_data); if (dc > (fd_size - BUF_BUFFER)) { free(file_data); fd_size = dc + BUF_BUFFER; file_data = (char*)malloc(fd_size); } // Do we need a bigger compressed buffer? c = qlz_size_compressed(file_data); if (c > d_size) { free (decompressed); d_size = c; decompressed = (char*)malloc(d_size); } fread(file_data + 9, 1, c - 9, ifile); d = qlz_decompress(file_data, decompressed, state_decompress); fwrite(decompressed, d, 1, ofile); } free(decompressed); free(state_decompress); free(file_data); return 0; }
int main(int argc, char* argv[]) { FILE *ifile, *ofile; char *file_data, *decompressed; size_t d, c; qlz_state_decompress *state_decompress = (qlz_state_decompress *)malloc(sizeof(qlz_state_decompress)); // read source file ifile = fopen(argv[1], "rb"); ofile = fopen(argv[2], "wb"); // a compressed packet can be at most "uncompressed size" + 400 bytes large where // "uncompressed size" = 10000 in worst case in this sample demo file_data = (char*) malloc(10000 + 400); // allocate decompression buffer decompressed = (char*) malloc(10000); // allocate and initially zero out the scratch buffer. After this, make sure it is // preserved across calls and never modified manually memset(state_decompress, 0, sizeof(qlz_state_decompress)); // read 9-byte header to find the size of the entire compressed packet, and // then read remaining packet while((c = fread(file_data, 1, 9, ifile)) != 0) { c = qlz_size_compressed(file_data); fread(file_data + 9, 1, c - 9, ifile); d = qlz_decompress(file_data, decompressed, state_decompress); printf("%u bytes decompressed into %u.\n", (unsigned int)c, (unsigned int)d); fwrite(decompressed, d, 1, ofile); } fclose(ifile); fclose(ofile); return 0; }
PyObject *qlz_size_decompressed_py(PyObject *self, PyObject *args) { PyObject *result = NULL; char* buffer; int buffer_length; #if PY_MAJOR_VERSION >= 3 if (PyArg_ParseTuple(args, "y#", &buffer, &buffer_length)) { #else if (PyArg_ParseTuple(args, "s#", &buffer, &buffer_length)) { #endif result = Py_BuildValue("i", qlz_size_decompressed(buffer)); } /* otherwise there is an error, * the exception already raised by PyArg_ParseTuple, and NULL is * returned. */ return result; } PyObject *qlz_size_compressed_py(PyObject *self, PyObject *args) { PyObject *result = NULL; char* buffer; int buffer_length; #if PY_MAJOR_VERSION >= 3 if (PyArg_ParseTuple(args, "y#", &buffer, &buffer_length)) { #else if (PyArg_ParseTuple(args, "s#", &buffer, &buffer_length)) { #endif result = Py_BuildValue("i", qlz_size_compressed(buffer)); } /* otherwise there is an error, * the exception already raised by PyArg_ParseTuple, and NULL is * returned. */ return result; } PyObject *qlz_compress_py(PyObject *self, PyObject *args) { PyObject *result = NULL; PyObject *state = NULL; char* buffer; char* compressed_buffer; int buffer_length; int size_compressed; #if PY_MAJOR_VERSION >= 3 if (PyArg_ParseTuple(args, "y#O!", &buffer, &buffer_length, &QLZStateCompressType, &state)) #else if (PyArg_ParseTuple(args, "s#O!", &buffer, &buffer_length, &QLZStateCompressType, &state)) #endif { compressed_buffer = (char*)malloc(buffer_length+400); size_compressed = qlz_compress(buffer, compressed_buffer, buffer_length, ((qlz_state_compress_Type*)state)->value); #if PY_MAJOR_VERSION >= 3 result = Py_BuildValue("y#", compressed_buffer, size_compressed); #else result = Py_BuildValue("s#", compressed_buffer, size_compressed); #endif free(compressed_buffer); } /* otherwise there is an error, * the exception already raised by PyArg_ParseTuple, and NULL is * returned. */ return result; }