size_t ZlibRecompress(unsigned char *src, size_t src_len, size_t size_leanified /*= 0*/) { if (!is_fast) { size_t s = 0; unsigned char *buffer = (unsigned char *)tinfl_decompress_mem_to_heap(src, src_len, &s, TINFL_FLAG_PARSE_ZLIB_HEADER); if (!buffer) { std::cerr << "Decompress Zlib data failed." << std::endl; } else { ZopfliOptions zopfli_options; ZopfliInitOptions(&zopfli_options); zopfli_options.numiterations = iterations; size_t new_size = 0; unsigned char *out_buffer = NULL; ZopfliZlibCompress(&zopfli_options, buffer, s, &out_buffer, &new_size); mz_free(buffer); if (new_size < src_len) { memcpy(src - size_leanified, out_buffer, new_size); delete[] out_buffer; return new_size; } delete[] out_buffer; } } memmove(src - size_leanified, src, src_len); return src_len; }
/* You can use this function in Your own lib calls/applications. ZopfliFormat is required but can be passed as simple number. ZopfliOptions, ZopfliPredefinedSplits and ZopfliAdditionalData structures are optional and can be NULL. ZopfliPredefinedSplits structure holds 3 variables being an array of block split offsets, number of block split offsets and if to perform additional auto splitting between predefined split points (0 or 1). size_t array, size_t and int. ZopfliAdditonalData structure can hold 2 variables being file timestamp and name. If timestamp is omitted, lower possible value will be used for GZ and ZIP. If file name is omitted GZ will not store filename and ZIP will use input data CRC32 as file name. */ DLL_PUBLIC void ZopfliCompress(ZopfliOptions* options, const ZopfliFormat output_type, const unsigned char* in, size_t insize, unsigned char** out, size_t* outsize, ZopfliPredefinedSplits* sp, const ZopfliAdditionalData* moredata) { if(in == NULL || out == NULL || outsize == NULL) { fprintf(stderr,"Critical Error: one or more required pointers are NULL\n"); exit(EXIT_FAILURE); } else { ZopfliOptions* optionslib = Zmalloc(sizeof(*optionslib)); if(options == NULL) { ZopfliInitOptions(optionslib); optionslib->verbose = 0; } else { optionslib = options; mui = options->maxfailiterations; } if (output_type == ZOPFLI_FORMAT_GZIP || output_type == ZOPFLI_FORMAT_GZIP_NAME) { ZopfliGzipCompress(optionslib, in, insize, out, outsize, sp, moredata); } else if (output_type == ZOPFLI_FORMAT_ZLIB) { ZopfliZlibCompress(optionslib, in, insize, out, outsize, sp); } else if (output_type == ZOPFLI_FORMAT_ZIP) { ZopfliZipCompress(optionslib, in, insize, out, outsize, sp, moredata); } else if (output_type == ZOPFLI_FORMAT_DEFLATE) { unsigned char bp = 0; ZopfliDeflate(optionslib, 2 /* Dynamic block */, 1, in, insize, &bp, out, outsize, sp); } else { fprintf(stderr,"Error: No output format specified.\n"); exit (EXIT_FAILURE); } free(optionslib); } }