]>
git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/doc/educational_decoder/harness.c
2 * Copyright (c) 2017-present, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
13 #include "zstd_decompress.h"
15 typedef unsigned char u8
;
17 // If the data doesn't have decompressed size with it, fallback on assuming the
18 // compression ratio is at most 16
19 #define MAX_COMPRESSION_RATIO (16)
21 // Protect against allocating too much memory for output
22 #define MAX_OUTPUT_SIZE ((size_t)1024 * 1024 * 1024)
28 size_t read_file(const char *path
, u8
**ptr
) {
29 FILE *f
= fopen(path
, "rb");
31 fprintf(stderr
, "failed to open file %s\n", path
);
35 fseek(f
, 0L, SEEK_END
);
36 size_t size
= ftell(f
);
41 fprintf(stderr
, "failed to allocate memory to hold %s\n", path
);
47 size_t read
= fread(&(*ptr
)[pos
], 1, size
, f
);
49 fprintf(stderr
, "error while reading file %s\n", path
);
60 void write_file(const char *path
, const u8
*ptr
, size_t size
) {
61 FILE *f
= fopen(path
, "wb");
64 while (written
< size
) {
65 written
+= fwrite(&ptr
[written
], 1, size
, f
);
67 fprintf(stderr
, "error while writing file %s\n", path
);
75 int main(int argc
, char **argv
) {
77 fprintf(stderr
, "usage: %s <file.zst> <out_path> [dictionary]\n",
83 size_t input_size
= read_file(argv
[1], &input
);
86 dict_size
= read_file(argv
[3], &dict
);
89 size_t decompressed_size
= ZSTD_get_decompressed_size(input
, input_size
);
90 if (decompressed_size
== (size_t)-1) {
91 decompressed_size
= MAX_COMPRESSION_RATIO
* input_size
;
92 fprintf(stderr
, "WARNING: Compressed data does not contain "
93 "decompressed size, going to assume the compression "
94 "ratio is at most %d (decompressed size of at most "
96 MAX_COMPRESSION_RATIO
, decompressed_size
);
98 if (decompressed_size
> MAX_OUTPUT_SIZE
) {
100 "Required output size too large for this implementation\n");
103 output
= malloc(decompressed_size
);
105 fprintf(stderr
, "failed to allocate memory\n");
109 dictionary_t
* const parsed_dict
= create_dictionary();
111 parse_dictionary(parsed_dict
, dict
, dict_size
);
113 size_t decompressed
=
114 ZSTD_decompress_with_dict(output
, decompressed_size
,
115 input
, input_size
, parsed_dict
);
117 free_dictionary(parsed_dict
);
119 write_file(argv
[2], output
, decompressed
);
124 input
= output
= dict
= NULL
;