Path: blob/main/sys/contrib/zstd/doc/educational_decoder/harness.c
48378 views
/*1* Copyright (c) Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910#include <stdio.h>11#include <stdlib.h>1213#include "zstd_decompress.h"1415typedef unsigned char u8;1617// If the data doesn't have decompressed size with it, fallback on assuming the18// compression ratio is at most 1619#define MAX_COMPRESSION_RATIO (16)2021// Protect against allocating too much memory for output22#define MAX_OUTPUT_SIZE ((size_t)1024 * 1024 * 1024)2324// Error message then exit25#define ERR_OUT(...) { fprintf(stderr, __VA_ARGS__); exit(1); }262728typedef struct {29u8* address;30size_t size;31} buffer_s;3233static void freeBuffer(buffer_s b) { free(b.address); }3435static buffer_s read_file(const char *path)36{37FILE* const f = fopen(path, "rb");38if (!f) ERR_OUT("failed to open file %s \n", path);3940fseek(f, 0L, SEEK_END);41size_t const size = (size_t)ftell(f);42rewind(f);4344void* const ptr = malloc(size);45if (!ptr) ERR_OUT("failed to allocate memory to hold %s \n", path);4647size_t const read = fread(ptr, 1, size, f);48if (read != size) ERR_OUT("error while reading file %s \n", path);4950fclose(f);51buffer_s const b = { ptr, size };52return b;53}5455static void write_file(const char* path, const u8* ptr, size_t size)56{57FILE* const f = fopen(path, "wb");58if (!f) ERR_OUT("failed to open file %s \n", path);5960size_t written = 0;61while (written < size) {62written += fwrite(ptr+written, 1, size, f);63if (ferror(f)) ERR_OUT("error while writing file %s\n", path);64}6566fclose(f);67}6869int main(int argc, char **argv)70{71if (argc < 3)72ERR_OUT("usage: %s <file.zst> <out_path> [dictionary] \n", argv[0]);7374buffer_s const input = read_file(argv[1]);7576buffer_s dict = { NULL, 0 };77if (argc >= 4) {78dict = read_file(argv[3]);79}8081size_t out_capacity = ZSTD_get_decompressed_size(input.address, input.size);82if (out_capacity == (size_t)-1) {83out_capacity = MAX_COMPRESSION_RATIO * input.size;84fprintf(stderr, "WARNING: Compressed data does not contain "85"decompressed size, going to assume the compression "86"ratio is at most %d (decompressed size of at most "87"%u) \n",88MAX_COMPRESSION_RATIO, (unsigned)out_capacity);89}90if (out_capacity > MAX_OUTPUT_SIZE)91ERR_OUT("Required output size too large for this implementation \n");9293u8* const output = malloc(out_capacity);94if (!output) ERR_OUT("failed to allocate memory \n");9596dictionary_t* const parsed_dict = create_dictionary();97if (dict.size) {98#if defined (ZDEC_NO_DICTIONARY)99printf("dict.size = %zu \n", dict.size);100ERR_OUT("no dictionary support \n");101#else102parse_dictionary(parsed_dict, dict.address, dict.size);103#endif104}105size_t const decompressed_size =106ZSTD_decompress_with_dict(output, out_capacity,107input.address, input.size,108parsed_dict);109110free_dictionary(parsed_dict);111112write_file(argv[2], output, decompressed_size);113114freeBuffer(input);115freeBuffer(dict);116free(output);117return 0;118}119120121