static SquashStatus squash_libdeflate_decompress_buffer (SquashCodec* codec, size_t* decompressed_size, uint8_t decompressed[HEDLEY_ARRAY_PARAM(*decompressed_size)], size_t compressed_size, const uint8_t compressed[HEDLEY_ARRAY_PARAM(compressed_size)], SquashOptions* options) { struct deflate_decompressor *decompressor = deflate_alloc_decompressor(); size_t actual_out_nbytes; enum decompress_result ret = deflate_decompress(decompressor, compressed, compressed_size, decompressed, *decompressed_size, &actual_out_nbytes); deflate_free_decompressor(decompressor); *decompressed_size = actual_out_nbytes; switch (ret) { case DECOMPRESS_SUCCESS: return SQUASH_OK; case DECOMPRESS_BAD_DATA: return squash_error (SQUASH_FAILED); case DECOMPRESS_SHORT_OUTPUT: return squash_error (SQUASH_BUFFER_FULL); case DECOMPRESS_INSUFFICIENT_SPACE: return squash_error (SQUASH_BUFFER_FULL); } HEDLEY_UNREACHABLE (); }
int main(int argc, char **argv) { struct deflate_decompressor *d; int ret; int fd = open(argv[1], O_RDONLY); struct stat stbuf; assert(fd >= 0); ret = fstat(fd, &stbuf); assert(!ret); char in[stbuf.st_size]; ret = read(fd, in, sizeof in); assert(ret == sizeof in); char out[sizeof(in) * 3]; d = deflate_alloc_decompressor(); zlib_decompress(d, in, sizeof in, out, sizeof out, NULL); deflate_free_decompressor(d); return 0; }
int tmain(int argc, tchar *argv[]) { struct options options; int opt_char; int i; int ret; program_invocation_name = get_filename(argv[0]); options.to_stdout = false; options.decompress = is_gunzip(); options.force = false; options.keep = false; options.compression_level = 6; options.suffix = T("gz"); while ((opt_char = tgetopt(argc, argv, optstring)) != -1) { switch (opt_char) { case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': options.compression_level = parse_compression_level(opt_char, toptarg); if (options.compression_level == 0) return -1; break; case 'c': options.to_stdout = true; break; case 'd': options.decompress = true; break; case 'f': options.force = true; break; case 'h': show_usage(stdout); return 0; case 'k': options.keep = true; break; case 'S': options.suffix = toptarg; break; case 'V': show_version(); return 0; default: show_usage(stderr); return 1; } } argv += toptind; argc -= toptind; if (argc == 0) { show_usage(stderr); return 1; } for (i = 0; i < argc; i++) { if (argv[i][0] == '-' && argv[i][1] == '\0') { msg("This implementation of gzip does not yet " "support reading from standard input."); return 1; } } ret = 0; if (options.decompress) { struct deflate_decompressor *d; d = alloc_decompressor(); if (d == NULL) return 1; for (i = 0; i < argc; i++) ret |= -decompress_file(d, argv[i], &options); deflate_free_decompressor(d); } else { struct deflate_compressor *c; c = alloc_compressor(options.compression_level); if (c == NULL) return 1; for (i = 0; i < argc; i++) ret |= -compress_file(c, argv[i], &options); deflate_free_compressor(c); } /* * If ret=0, there were no warnings or errors. Exit with status 0. * If ret=2, there was at least one warning. Exit with status 2. * Else, there was at least one error. Exit with status 1. */ if (ret != 0 && ret != 2) ret = 1; return ret; }