10c16b537SWarner Losh /* 2*37f1f268SConrad Meyer * Copyright (c) 2017-2020, Facebook, Inc. 30c16b537SWarner Losh * All rights reserved. 40c16b537SWarner Losh * 50c16b537SWarner Losh * This source code is licensed under both the BSD-style license (found in the 60c16b537SWarner Losh * LICENSE file in the root directory of this source tree) and the GPLv2 (found 70c16b537SWarner Losh * in the COPYING file in the root directory of this source tree). 8*37f1f268SConrad Meyer * You may select, at your option, one of the above-listed licenses. 90c16b537SWarner Losh */ 100c16b537SWarner Losh 110c16b537SWarner Losh #include <stdio.h> 120c16b537SWarner Losh #include <stdlib.h> 130c16b537SWarner Losh 140c16b537SWarner Losh #include "zstd_decompress.h" 150c16b537SWarner Losh 160c16b537SWarner Losh typedef unsigned char u8; 170c16b537SWarner Losh 180c16b537SWarner Losh // If the data doesn't have decompressed size with it, fallback on assuming the 190c16b537SWarner Losh // compression ratio is at most 16 200c16b537SWarner Losh #define MAX_COMPRESSION_RATIO (16) 210c16b537SWarner Losh 220c16b537SWarner Losh // Protect against allocating too much memory for output 230c16b537SWarner Losh #define MAX_OUTPUT_SIZE ((size_t)1024 * 1024 * 1024) 240c16b537SWarner Losh 25*37f1f268SConrad Meyer // Error message then exit 26*37f1f268SConrad Meyer #define ERR_OUT(...) { fprintf(stderr, __VA_ARGS__); exit(1); } 27*37f1f268SConrad Meyer 28*37f1f268SConrad Meyer 29*37f1f268SConrad Meyer typedef struct { 30*37f1f268SConrad Meyer u8* address; 31*37f1f268SConrad Meyer size_t size; 32*37f1f268SConrad Meyer } buffer_s; 33*37f1f268SConrad Meyer 34*37f1f268SConrad Meyer static void freeBuffer(buffer_s b) { free(b.address); } 35*37f1f268SConrad Meyer 36*37f1f268SConrad Meyer static buffer_s read_file(const char *path) 379cbefe25SConrad Meyer { 389cbefe25SConrad Meyer FILE* const f = fopen(path, "rb"); 39*37f1f268SConrad Meyer if (!f) ERR_OUT("failed to open file %s \n", path); 400c16b537SWarner Losh 410c16b537SWarner Losh fseek(f, 0L, SEEK_END); 429cbefe25SConrad Meyer size_t const size = (size_t)ftell(f); 430c16b537SWarner Losh rewind(f); 440c16b537SWarner Losh 45*37f1f268SConrad Meyer void* const ptr = malloc(size); 46*37f1f268SConrad Meyer if (!ptr) ERR_OUT("failed to allocate memory to hold %s \n", path); 470c16b537SWarner Losh 48*37f1f268SConrad Meyer size_t const read = fread(ptr, 1, size, f); 49*37f1f268SConrad Meyer if (read != size) ERR_OUT("error while reading file %s \n", path); 500c16b537SWarner Losh 510c16b537SWarner Losh fclose(f); 52*37f1f268SConrad Meyer buffer_s const b = { ptr, size }; 53*37f1f268SConrad Meyer return b; 540c16b537SWarner Losh } 550c16b537SWarner Losh 569cbefe25SConrad Meyer static void write_file(const char* path, const u8* ptr, size_t size) 579cbefe25SConrad Meyer { 589cbefe25SConrad Meyer FILE* const f = fopen(path, "wb"); 59*37f1f268SConrad Meyer if (!f) ERR_OUT("failed to open file %s \n", path); 600c16b537SWarner Losh 610c16b537SWarner Losh size_t written = 0; 620c16b537SWarner Losh while (written < size) { 639cbefe25SConrad Meyer written += fwrite(ptr+written, 1, size, f); 64*37f1f268SConrad Meyer if (ferror(f)) ERR_OUT("error while writing file %s\n", path); 65*37f1f268SConrad Meyer } 660c16b537SWarner Losh 670c16b537SWarner Losh fclose(f); 680c16b537SWarner Losh } 690c16b537SWarner Losh 709cbefe25SConrad Meyer int main(int argc, char **argv) 719cbefe25SConrad Meyer { 72*37f1f268SConrad Meyer if (argc < 3) 73*37f1f268SConrad Meyer ERR_OUT("usage: %s <file.zst> <out_path> [dictionary] \n", argv[0]); 740c16b537SWarner Losh 75*37f1f268SConrad Meyer buffer_s const input = read_file(argv[1]); 760c16b537SWarner Losh 77*37f1f268SConrad Meyer buffer_s dict = { NULL, 0 }; 780c16b537SWarner Losh if (argc >= 4) { 79*37f1f268SConrad Meyer dict = read_file(argv[3]); 800c16b537SWarner Losh } 810c16b537SWarner Losh 82*37f1f268SConrad Meyer size_t out_capacity = ZSTD_get_decompressed_size(input.address, input.size); 839cbefe25SConrad Meyer if (out_capacity == (size_t)-1) { 84*37f1f268SConrad Meyer out_capacity = MAX_COMPRESSION_RATIO * input.size; 850c16b537SWarner Losh fprintf(stderr, "WARNING: Compressed data does not contain " 860c16b537SWarner Losh "decompressed size, going to assume the compression " 870c16b537SWarner Losh "ratio is at most %d (decompressed size of at most " 889cbefe25SConrad Meyer "%u) \n", 899cbefe25SConrad Meyer MAX_COMPRESSION_RATIO, (unsigned)out_capacity); 900c16b537SWarner Losh } 91*37f1f268SConrad Meyer if (out_capacity > MAX_OUTPUT_SIZE) 92*37f1f268SConrad Meyer ERR_OUT("Required output size too large for this implementation \n"); 939cbefe25SConrad Meyer 949cbefe25SConrad Meyer u8* const output = malloc(out_capacity); 95*37f1f268SConrad Meyer if (!output) ERR_OUT("failed to allocate memory \n"); 960c16b537SWarner Losh 970c16b537SWarner Losh dictionary_t* const parsed_dict = create_dictionary(); 98*37f1f268SConrad Meyer if (dict.size) { 99*37f1f268SConrad Meyer #if defined (ZDEC_NO_DICTIONARY) 100*37f1f268SConrad Meyer printf("dict.size = %zu \n", dict.size); 101*37f1f268SConrad Meyer ERR_OUT("no dictionary support \n"); 102*37f1f268SConrad Meyer #else 103*37f1f268SConrad Meyer parse_dictionary(parsed_dict, dict.address, dict.size); 104*37f1f268SConrad Meyer #endif 1050c16b537SWarner Losh } 1069cbefe25SConrad Meyer size_t const decompressed_size = 1079cbefe25SConrad Meyer ZSTD_decompress_with_dict(output, out_capacity, 108*37f1f268SConrad Meyer input.address, input.size, 1099cbefe25SConrad Meyer parsed_dict); 1100c16b537SWarner Losh 1110c16b537SWarner Losh free_dictionary(parsed_dict); 1120c16b537SWarner Losh 1139cbefe25SConrad Meyer write_file(argv[2], output, decompressed_size); 1140c16b537SWarner Losh 115*37f1f268SConrad Meyer freeBuffer(input); 116*37f1f268SConrad Meyer freeBuffer(dict); 1170c16b537SWarner Losh free(output); 1189cbefe25SConrad Meyer return 0; 1190c16b537SWarner Losh } 120