10c16b537SWarner Losh /*
2*5ff13fbcSAllan Jude * Copyright (c) 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).
837f1f268SConrad 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
2537f1f268SConrad Meyer // Error message then exit
2637f1f268SConrad Meyer #define ERR_OUT(...) { fprintf(stderr, __VA_ARGS__); exit(1); }
2737f1f268SConrad Meyer
2837f1f268SConrad Meyer
2937f1f268SConrad Meyer typedef struct {
3037f1f268SConrad Meyer u8* address;
3137f1f268SConrad Meyer size_t size;
3237f1f268SConrad Meyer } buffer_s;
3337f1f268SConrad Meyer
freeBuffer(buffer_s b)3437f1f268SConrad Meyer static void freeBuffer(buffer_s b) { free(b.address); }
3537f1f268SConrad Meyer
read_file(const char * path)3637f1f268SConrad Meyer static buffer_s read_file(const char *path)
379cbefe25SConrad Meyer {
389cbefe25SConrad Meyer FILE* const f = fopen(path, "rb");
3937f1f268SConrad 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
4537f1f268SConrad Meyer void* const ptr = malloc(size);
4637f1f268SConrad Meyer if (!ptr) ERR_OUT("failed to allocate memory to hold %s \n", path);
470c16b537SWarner Losh
4837f1f268SConrad Meyer size_t const read = fread(ptr, 1, size, f);
4937f1f268SConrad Meyer if (read != size) ERR_OUT("error while reading file %s \n", path);
500c16b537SWarner Losh
510c16b537SWarner Losh fclose(f);
5237f1f268SConrad Meyer buffer_s const b = { ptr, size };
5337f1f268SConrad Meyer return b;
540c16b537SWarner Losh }
550c16b537SWarner Losh
write_file(const char * path,const u8 * ptr,size_t size)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");
5937f1f268SConrad 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);
6437f1f268SConrad Meyer if (ferror(f)) ERR_OUT("error while writing file %s\n", path);
6537f1f268SConrad Meyer }
660c16b537SWarner Losh
670c16b537SWarner Losh fclose(f);
680c16b537SWarner Losh }
690c16b537SWarner Losh
main(int argc,char ** argv)709cbefe25SConrad Meyer int main(int argc, char **argv)
719cbefe25SConrad Meyer {
7237f1f268SConrad Meyer if (argc < 3)
7337f1f268SConrad Meyer ERR_OUT("usage: %s <file.zst> <out_path> [dictionary] \n", argv[0]);
740c16b537SWarner Losh
7537f1f268SConrad Meyer buffer_s const input = read_file(argv[1]);
760c16b537SWarner Losh
7737f1f268SConrad Meyer buffer_s dict = { NULL, 0 };
780c16b537SWarner Losh if (argc >= 4) {
7937f1f268SConrad Meyer dict = read_file(argv[3]);
800c16b537SWarner Losh }
810c16b537SWarner Losh
8237f1f268SConrad Meyer size_t out_capacity = ZSTD_get_decompressed_size(input.address, input.size);
839cbefe25SConrad Meyer if (out_capacity == (size_t)-1) {
8437f1f268SConrad 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 }
9137f1f268SConrad Meyer if (out_capacity > MAX_OUTPUT_SIZE)
9237f1f268SConrad Meyer ERR_OUT("Required output size too large for this implementation \n");
939cbefe25SConrad Meyer
949cbefe25SConrad Meyer u8* const output = malloc(out_capacity);
9537f1f268SConrad Meyer if (!output) ERR_OUT("failed to allocate memory \n");
960c16b537SWarner Losh
970c16b537SWarner Losh dictionary_t* const parsed_dict = create_dictionary();
9837f1f268SConrad Meyer if (dict.size) {
9937f1f268SConrad Meyer #if defined (ZDEC_NO_DICTIONARY)
10037f1f268SConrad Meyer printf("dict.size = %zu \n", dict.size);
10137f1f268SConrad Meyer ERR_OUT("no dictionary support \n");
10237f1f268SConrad Meyer #else
10337f1f268SConrad Meyer parse_dictionary(parsed_dict, dict.address, dict.size);
10437f1f268SConrad Meyer #endif
1050c16b537SWarner Losh }
1069cbefe25SConrad Meyer size_t const decompressed_size =
1079cbefe25SConrad Meyer ZSTD_decompress_with_dict(output, out_capacity,
10837f1f268SConrad 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
11537f1f268SConrad Meyer freeBuffer(input);
11637f1f268SConrad Meyer freeBuffer(dict);
1170c16b537SWarner Losh free(output);
1189cbefe25SConrad Meyer return 0;
1190c16b537SWarner Losh }
120