2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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).
11 #include <stdlib.h> // malloc, exit
12 #include <stdio.h> // fprintf, perror, feof
13 #include <string.h> // strerror
14 #include <errno.h> // errno
15 #define ZSTD_STATIC_LINKING_ONLY
16 #include <zstd.h> // presumes zstd library is installed
17 #include <zstd_errors.h>
19 #include "../zstd_seekable.h"
21 #define MIN(a, b) ((a) < (b) ? (a) : (b))
23 static void* malloc_orDie(size_t size)
25 void* const buff = malloc(size);
26 if (buff) return buff;
32 static void* realloc_orDie(void* ptr, size_t size)
34 ptr = realloc(ptr, size);
41 static FILE* fopen_orDie(const char *filename, const char *instruction)
43 FILE* const inFile = fopen(filename, instruction);
44 if (inFile) return inFile;
50 static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
52 size_t const readSize = fread(buffer, 1, sizeToRead, file);
53 if (readSize == sizeToRead) return readSize; /* good */
54 if (feof(file)) return readSize; /* good, reached end of file */
60 static size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
62 size_t const writtenSize = fwrite(buffer, 1, sizeToWrite, file);
63 if (writtenSize == sizeToWrite) return sizeToWrite; /* good */
69 static size_t fclose_orDie(FILE* file)
71 if (!fclose(file)) return 0;
77 static void fseek_orDie(FILE* file, long int offset, int origin) {
78 if (!fseek(file, offset, origin)) {
79 if (!fflush(file)) return;
87 static void decompressFile_orDie(const char* fname, off_t startOffset, off_t endOffset)
89 FILE* const fin = fopen_orDie(fname, "rb");
90 FILE* const fout = stdout;
91 size_t const buffOutSize = ZSTD_DStreamOutSize(); /* Guarantee to successfully flush at least one complete compressed block in all circumstances. */
92 void* const buffOut = malloc_orDie(buffOutSize);
94 ZSTD_seekable* const seekable = ZSTD_seekable_create();
95 if (seekable==NULL) { fprintf(stderr, "ZSTD_seekable_create() error \n"); exit(10); }
97 size_t const initResult = ZSTD_seekable_initFile(seekable, fin);
98 if (ZSTD_isError(initResult)) { fprintf(stderr, "ZSTD_seekable_init() error : %s \n", ZSTD_getErrorName(initResult)); exit(11); }
100 while (startOffset < endOffset) {
101 size_t const result = ZSTD_seekable_decompress(seekable, buffOut, MIN(endOffset - startOffset, buffOutSize), startOffset);
106 if (ZSTD_isError(result)) {
107 fprintf(stderr, "ZSTD_seekable_decompress() error : %s \n",
108 ZSTD_getErrorName(result));
111 fwrite_orDie(buffOut, result, fout);
112 startOffset += result;
115 ZSTD_seekable_free(seekable);
122 int main(int argc, const char** argv)
124 const char* const exeName = argv[0];
127 fprintf(stderr, "wrong arguments\n");
128 fprintf(stderr, "usage:\n");
129 fprintf(stderr, "%s FILE START END\n", exeName);
134 const char* const inFilename = argv[1];
135 off_t const startOffset = atoll(argv[2]);
136 off_t const endOffset = atoll(argv[3]);
137 decompressFile_orDie(inFilename, startOffset, endOffset);