static void lmz_initcomp(lua_State *L, int start, lmz_Comp *c) { static const mz_uint probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 }; int level = (int)luaL_optinteger(L, start, MZ_DEFAULT_LEVEL); mz_uint flags = probes[(level >= 0) ? MZ_MIN(10, level) : MZ_DEFAULT_LEVEL]; tdefl_status status; if (lua_tointeger(L, start+1) >= 0) flags |= TDEFL_WRITE_ZLIB_HEADER; if (level <= 3) flags |= TDEFL_GREEDY_PARSING_FLAG; if ((status = tdefl_init(c, NULL, NULL, flags)) != TDEFL_STATUS_OKAY) luaL_error(L, "compress failure (%d)", status); }
int miniz_deflate( unsigned char *dest, size_t *destLen, const unsigned char *source, size_t sourceLen, int level ){ tdefl_compressor comp; mz_uint comp_flags = tdefl_create_comp_flags_from_zip_params(level, -MAX_WBITS, MZ_DEFAULT_STRATEGY); int status=tdefl_init(&comp,NULL,NULL,comp_flags); size_t _sourceLen=sourceLen; if(status)return status; status=tdefl_compress(&comp,source,&_sourceLen,dest,destLen,MZ_FINISH); return status==MZ_STREAM_END ? MZ_OK : status; }
int compress_buffer_to_file(const char* filename, const char* inbuf, const size_t buffer_size) { // modified version of miniz.c's example5.c static const int COMPRESSION_FAIL = 1; int level = Z_BEST_COMPRESSION; void* next_out = outbuf; const void* next_in = (const void*)inbuf; size_t avail_in = buffer_size; size_t avail_out = OUT_BUF_SIZE; int error = 0; size_t total_in = 0, total_out = 0; size_t comp_flags = tdefl_create_comp_flags_from_zip_params( level, MZ_DEFAULT_WINDOW_BITS, MZ_DEFAULT_STRATEGY) | TDEFL_WRITE_ZLIB_HEADER; // Initialize the low-level compressor. tdefl_compressor deflator; tdefl_status status = tdefl_init(&deflator, NULL, NULL, int(comp_flags)); if (status != TDEFL_STATUS_OKAY) { printf("tdefl_init() failed!\n"); return COMPRESSION_FAIL; } FILE* outfile = fopen(filename, "wb"); if (outfile == NULL) { printf("Failed opening output file!\n"); return COMPRESSION_FAIL; } // Compression. for (;;) { size_t in_bytes = avail_in; size_t out_bytes = avail_out; // Compress as much of the input as possible (or all of it) to the output inbuf. status = tdefl_compress(&deflator, next_in, &in_bytes, next_out, &out_bytes, in_bytes ? TDEFL_NO_FLUSH : TDEFL_FINISH); next_in = (const char*)next_in + in_bytes; avail_in -= in_bytes; total_in += in_bytes; next_out = (char*)next_out + out_bytes; avail_out -= out_bytes; total_out += out_bytes; if ((status != TDEFL_STATUS_OKAY) || (!avail_out)) { // Output inbuf is full, or compression is done or failed, so write inbuf to output file. size_t n = OUT_BUF_SIZE - (size_t)avail_out; if (fwrite(outbuf, 1, n, outfile) != n) { printf("Failed writing to output file!\n"); error = COMPRESSION_FAIL; break; } next_out = outbuf; avail_out = OUT_BUF_SIZE; } if (status == TDEFL_STATUS_DONE) { // Compression completed successfully. break; } else if (status != TDEFL_STATUS_OKAY) { // Compression somehow failed. printf("tdefl_compress() failed with status %d!\n", status); error = COMPRESSION_FAIL; break; } } if (outfile != NULL) { int closed = fclose(outfile); if (closed) printf("failed to close file \"%s\"\n", filename); } return error; }
u8_t* get_file_data(const char* filename, int* file_size, int can_be_compressed, int* is_compressed) { FILE *inFile; size_t fsize = 0; u8_t* buf; size_t r; int rs; inFile = fopen(filename, "rb"); if (inFile == NULL) { printf("Failed to open file \"%s\"\n", filename); exit(-1); } fseek(inFile, 0, SEEK_END); rs = ftell(inFile); if (rs < 0) { printf("ftell failed with %d\n", errno); exit(-1); } fsize = (size_t)rs; fseek(inFile, 0, SEEK_SET); buf = (u8_t*)malloc(fsize); LWIP_ASSERT("buf != NULL", buf != NULL); r = fread(buf, 1, fsize, inFile); *file_size = fsize; *is_compressed = 0; #if MAKEFS_SUPPORT_DEFLATE overallDataBytes += fsize; if (deflateNonSsiFiles) { if (can_be_compressed) { if (fsize < OUT_BUF_SIZE) { u8_t* ret_buf; tdefl_status status; size_t in_bytes = fsize; size_t out_bytes = OUT_BUF_SIZE; const void *next_in = buf; void *next_out = s_outbuf; /* create tdefl() compatible flags (we have to compose the low-level flags ourselves, or use tdefl_create_comp_flags_from_zip_params() but that means MINIZ_NO_ZLIB_APIS can't be defined). */ mz_uint comp_flags = s_tdefl_num_probes[MZ_MIN(10, deflate_level)] | ((deflate_level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0); if (!deflate_level) { comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS; } status = tdefl_init(&g_deflator, NULL, NULL, comp_flags); if (status != TDEFL_STATUS_OKAY) { printf("tdefl_init() failed!\n"); exit(-1); } memset(s_outbuf, 0, sizeof(s_outbuf)); status = tdefl_compress(&g_deflator, next_in, &in_bytes, next_out, &out_bytes, TDEFL_FINISH); if (status != TDEFL_STATUS_DONE) { printf("deflate failed: %d\n", status); exit(-1); } LWIP_ASSERT("out_bytes <= COPY_BUFSIZE", out_bytes <= OUT_BUF_SIZE); if (out_bytes < fsize) { ret_buf = (u8_t*)malloc(out_bytes); LWIP_ASSERT("ret_buf != NULL", ret_buf != NULL); memcpy(ret_buf, s_outbuf, out_bytes); { /* sanity-check compression be inflating and comparing to the original */ tinfl_status dec_status; tinfl_decompressor inflator; size_t dec_in_bytes = out_bytes; size_t dec_out_bytes = OUT_BUF_SIZE; next_out = s_checkbuf; tinfl_init(&inflator); memset(s_checkbuf, 0, sizeof(s_checkbuf)); dec_status = tinfl_decompress(&inflator, (const mz_uint8 *)ret_buf, &dec_in_bytes, s_checkbuf, (mz_uint8 *)next_out, &dec_out_bytes, 0); LWIP_ASSERT("tinfl_decompress failed", dec_status == TINFL_STATUS_DONE); LWIP_ASSERT("tinfl_decompress size mismatch", fsize == dec_out_bytes); LWIP_ASSERT("decompressed memcmp failed", !memcmp(s_checkbuf, buf, fsize)); } /* free original buffer, use compressed data + size */ free(buf); buf = ret_buf; *file_size = out_bytes; printf(" - deflate: %d bytes -> %d bytes (%.02f%%)" NEWLINE, (int)fsize, (int)out_bytes, (float)((out_bytes*100.0)/fsize)); deflatedBytesReduced += (size_t)(fsize - out_bytes); *is_compressed = 1; } else { printf(" - uncompressed: (would be %d bytes larger using deflate)" NEWLINE, (int)(out_bytes - fsize)); } } else { printf(" - uncompressed: (file is larger than deflate bufer)" NEWLINE); } } else { printf(" - SSI file, cannot be compressed" NEWLINE); } } #else LWIP_UNUSED_ARG(can_be_compressed); #endif fclose(inFile); return buf; }
int main(int argc, char *argv[]) { const char *pMode; FILE *pInfile, *pOutfile; uint infile_size; int level = 9; int p = 1; const char *pSrc_filename; const char *pDst_filename; const void *next_in = s_inbuf; size_t avail_in = 0; void *next_out = s_outbuf; size_t avail_out = OUT_BUF_SIZE; size_t total_in = 0, total_out = 0; long file_loc; assert(COMP_OUT_BUF_SIZE <= OUT_BUF_SIZE); printf("miniz.c example5 (demonstrates tinfl/tdefl)\n"); if (argc < 4) { printf("File to file compression/decompression using the low-level tinfl/tdefl API's.\n"); printf("Usage: example5 [options] [mode:c or d] infile outfile\n"); printf("\nModes:\n"); printf("c - Compresses file infile to a zlib stream in file outfile\n"); printf("d - Decompress zlib stream in file infile to file outfile\n"); printf("\nOptions:\n"); printf("-l[0-10] - Compression level, higher values are slower, 0 is none.\n"); return EXIT_FAILURE; } while ((p < argc) && (argv[p][0] == '-')) { switch (argv[p][1]) { case 'l': { level = atoi(&argv[1][2]); if ((level < 0) || (level > 10)) { printf("Invalid level!\n"); return EXIT_FAILURE; } break; } default: { printf("Invalid option: %s\n", argv[p]); return EXIT_FAILURE; } } p++; } if ((argc - p) < 3) { printf("Must specify mode, input filename, and output filename after options!\n"); return EXIT_FAILURE; } else if ((argc - p) > 3) { printf("Too many filenames!\n"); return EXIT_FAILURE; } pMode = argv[p++]; if (!strchr("cCdD", pMode[0])) { printf("Invalid mode!\n"); return EXIT_FAILURE; } pSrc_filename = argv[p++]; pDst_filename = argv[p++]; printf("Mode: %c, Level: %u\nInput File: \"%s\"\nOutput File: \"%s\"\n", pMode[0], level, pSrc_filename, pDst_filename); // Open input file. pInfile = fopen(pSrc_filename, "rb"); if (!pInfile) { printf("Failed opening input file!\n"); return EXIT_FAILURE; } // Determine input file's size. fseek(pInfile, 0, SEEK_END); file_loc = ftell(pInfile); fseek(pInfile, 0, SEEK_SET); if ((file_loc < 0) || (file_loc > INT_MAX)) { // This is not a limitation of miniz or tinfl, but this example. printf("File is too large to be processed by this example.\n"); return EXIT_FAILURE; } infile_size = (uint)file_loc; // Open output file. pOutfile = fopen(pDst_filename, "wb"); if (!pOutfile) { printf("Failed opening output file!\n"); return EXIT_FAILURE; } printf("Input file size: %u\n", infile_size); if ((pMode[0] == 'c') || (pMode[0] == 'C')) { // The number of dictionary probes to use at each compression level (0-10). 0=implies fastest/minimal possible probing. static const mz_uint s_tdefl_num_probes[11] = { 0, 1, 6, 32, 16, 32, 128, 256, 512, 768, 1500 }; tdefl_status status; uint infile_remaining = infile_size; // create tdefl() compatible flags (we have to compose the low-level flags ourselves, or use tdefl_create_comp_flags_from_zip_params() but that means MINIZ_NO_ZLIB_APIS can't be defined). mz_uint comp_flags = TDEFL_WRITE_ZLIB_HEADER | s_tdefl_num_probes[MZ_MIN(10, level)] | ((level <= 3) ? TDEFL_GREEDY_PARSING_FLAG : 0); if (!level) comp_flags |= TDEFL_FORCE_ALL_RAW_BLOCKS; // Initialize the low-level compressor. status = tdefl_init(&g_deflator, NULL, NULL, comp_flags); if (status != TDEFL_STATUS_OKAY) { printf("tdefl_init() failed!\n"); return EXIT_FAILURE; } avail_out = COMP_OUT_BUF_SIZE; // Compression. for ( ; ; ) { size_t in_bytes, out_bytes; if (!avail_in) { // Input buffer is empty, so read more bytes from input file. uint n = my_min(IN_BUF_SIZE, infile_remaining); if (fread(s_inbuf, 1, n, pInfile) != n) { printf("Failed reading from input file!\n"); return EXIT_FAILURE; } next_in = s_inbuf; avail_in = n; infile_remaining -= n; //printf("Input bytes remaining: %u\n", infile_remaining); } in_bytes = avail_in; out_bytes = avail_out; // Compress as much of the input as possible (or all of it) to the output buffer. status = tdefl_compress(&g_deflator, next_in, &in_bytes, next_out, &out_bytes, infile_remaining ? TDEFL_NO_FLUSH : TDEFL_FINISH); next_in = (const char *)next_in + in_bytes; avail_in -= in_bytes; total_in += in_bytes; next_out = (char *)next_out + out_bytes; avail_out -= out_bytes; total_out += out_bytes; if ((status != TDEFL_STATUS_OKAY) || (!avail_out)) { // Output buffer is full, or compression is done or failed, so write buffer to output file. uint n = COMP_OUT_BUF_SIZE - (uint)avail_out; if (fwrite(s_outbuf, 1, n, pOutfile) != n) { printf("Failed writing to output file!\n"); return EXIT_FAILURE; } next_out = s_outbuf; avail_out = COMP_OUT_BUF_SIZE; } if (status == TDEFL_STATUS_DONE) { // Compression completed successfully. break; } else if (status != TDEFL_STATUS_OKAY) { // Compression somehow failed. printf("tdefl_compress() failed with status %i!\n", status); return EXIT_FAILURE; } } } else if ((pMode[0] == 'd') || (pMode[0] == 'D')) { // Decompression. uint infile_remaining = infile_size; tinfl_decompressor inflator; tinfl_init(&inflator); for ( ; ; ) { size_t in_bytes, out_bytes; tinfl_status status; if (!avail_in) { // Input buffer is empty, so read more bytes from input file. uint n = my_min(IN_BUF_SIZE, infile_remaining); if (fread(s_inbuf, 1, n, pInfile) != n) { printf("Failed reading from input file!\n"); return EXIT_FAILURE; } next_in = s_inbuf; avail_in = n; infile_remaining -= n; } in_bytes = avail_in; out_bytes = avail_out; status = tinfl_decompress(&inflator, (const mz_uint8 *)next_in, &in_bytes, s_outbuf, (mz_uint8 *)next_out, &out_bytes, (infile_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0) | TINFL_FLAG_PARSE_ZLIB_HEADER); avail_in -= in_bytes; next_in = (const mz_uint8 *)next_in + in_bytes; total_in += in_bytes; avail_out -= out_bytes; next_out = (mz_uint8 *)next_out + out_bytes; total_out += out_bytes; if ((status <= TINFL_STATUS_DONE) || (!avail_out)) { // Output buffer is full, or decompression is done, so write buffer to output file. uint n = OUT_BUF_SIZE - (uint)avail_out; if (fwrite(s_outbuf, 1, n, pOutfile) != n) { printf("Failed writing to output file!\n"); return EXIT_FAILURE; } next_out = s_outbuf; avail_out = OUT_BUF_SIZE; } // If status is <= TINFL_STATUS_DONE then either decompression is done or something went wrong. if (status <= TINFL_STATUS_DONE) { if (status == TINFL_STATUS_DONE) { // Decompression completed successfully. break; } else { // Decompression failed. printf("tinfl_decompress() failed with status %i!\n", status); return EXIT_FAILURE; } } } } else { printf("Invalid mode!\n"); return EXIT_FAILURE; } fclose(pInfile); if (EOF == fclose(pOutfile)) { printf("Failed writing to output file!\n"); return EXIT_FAILURE; } printf("Total input bytes: %u\n", (mz_uint32)total_in); printf("Total output bytes: %u\n", (mz_uint32)total_out); printf("Success.\n"); return EXIT_SUCCESS; }