blob: 4b7ea59e50ccf5154b9ba2a01f259f82305b6b3e [file] [log] [blame]
Yann Collet4ded9e52016-08-30 10:04:33 -07001/**
2 * Copyright 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the license found in the
6 * LICENSE-examples file in the root directory of this source tree.
7 */
Yann Collet0809a882016-07-09 18:25:10 +02008
Yann Collet0809a882016-07-09 18:25:10 +02009#include <stdlib.h> // malloc, exit
10#include <stdio.h> // printf
11#include <string.h> // strerror
12#include <errno.h> // errno
13#include <sys/stat.h> // stat
Yann Collet45960372017-02-15 12:00:03 -080014#define ZSTD_STATIC_LINKING_ONLY // ZSTD_findDecompressedSize
Yann Collet0809a882016-07-09 18:25:10 +020015#include <zstd.h> // presumes zstd library is installed
16
17
Yann Collet45960372017-02-15 12:00:03 -080018static off_t fsize_orDie(const char *filename)
Yann Collet0809a882016-07-09 18:25:10 +020019{
20 struct stat st;
21 if (stat(filename, &st) == 0) return st.st_size;
22 /* error */
Soojin Nam971c1612017-02-22 16:04:48 +090023 fprintf(stderr, "stat: %s : %s \n", filename, strerror(errno));
Yann Collet0809a882016-07-09 18:25:10 +020024 exit(1);
25}
26
Yann Collet45960372017-02-15 12:00:03 -080027static FILE* fopen_orDie(const char *filename, const char *instruction)
Yann Collet0809a882016-07-09 18:25:10 +020028{
29 FILE* const inFile = fopen(filename, instruction);
30 if (inFile) return inFile;
31 /* error */
Soojin Nam971c1612017-02-22 16:04:48 +090032 fprintf(stderr, "fopen: %s : %s \n", filename, strerror(errno));
Yann Collet0809a882016-07-09 18:25:10 +020033 exit(2);
34}
35
Yann Collet45960372017-02-15 12:00:03 -080036static void* malloc_orDie(size_t size)
Yann Collet0809a882016-07-09 18:25:10 +020037{
Yann Collet1f3d54d2017-02-22 11:08:00 -080038 void* const buff = malloc(size + !size); /* avoid allocating size of 0 : may return NULL (implementation dependent) */
Yann Collet0809a882016-07-09 18:25:10 +020039 if (buff) return buff;
40 /* error */
Soojin Nam971c1612017-02-22 16:04:48 +090041 fprintf(stderr, "malloc: %s \n", strerror(errno));
Yann Collet0809a882016-07-09 18:25:10 +020042 exit(3);
43}
44
Yann Collet45960372017-02-15 12:00:03 -080045static void* loadFile_orDie(const char* fileName, size_t* size)
Yann Collet0809a882016-07-09 18:25:10 +020046{
Yann Collet45960372017-02-15 12:00:03 -080047 off_t const buffSize = fsize_orDie(fileName);
48 FILE* const inFile = fopen_orDie(fileName, "rb");
49 void* const buffer = malloc_orDie(buffSize);
Yann Collet0809a882016-07-09 18:25:10 +020050 size_t const readSize = fread(buffer, 1, buffSize, inFile);
51 if (readSize != (size_t)buffSize) {
Soojin Nam971c1612017-02-22 16:04:48 +090052 fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
Yann Collet0809a882016-07-09 18:25:10 +020053 exit(4);
54 }
Yann Colleta2664642016-09-09 19:33:56 +020055 fclose(inFile); /* can't fail (read only) */
Yann Collet0809a882016-07-09 18:25:10 +020056 *size = buffSize;
57 return buffer;
58}
59
60
Yann Collet0809a882016-07-09 18:25:10 +020061static void decompress(const char* fname)
62{
63 size_t cSize;
Yann Collet45960372017-02-15 12:00:03 -080064 void* const cBuff = loadFile_orDie(fname, &cSize);
Sean Purcell4e709712017-02-07 13:50:09 -080065 unsigned long long const rSize = ZSTD_findDecompressedSize(cBuff, cSize);
Soojin Nam971c1612017-02-22 16:04:48 +090066 if (rSize==ZSTD_CONTENTSIZE_ERROR) {
67 fprintf(stderr, "%s : it was not compressed by zstd.\n", fname);
Yann Collet0809a882016-07-09 18:25:10 +020068 exit(5);
Soojin Nam971c1612017-02-22 16:04:48 +090069 } else if (rSize==ZSTD_CONTENTSIZE_UNKNOWN) {
70 fprintf(stderr,
71 "%s : original size unknown. Use streaming decompression instead.\n", fname);
72 exit(6);
Yann Collet0809a882016-07-09 18:25:10 +020073 }
Soojin Nam971c1612017-02-22 16:04:48 +090074
Yann Collet45960372017-02-15 12:00:03 -080075 void* const rBuff = malloc_orDie((size_t)rSize);
Yann Collet0809a882016-07-09 18:25:10 +020076
77 size_t const dSize = ZSTD_decompress(rBuff, rSize, cBuff, cSize);
78
79 if (dSize != rSize) {
Soojin Nam971c1612017-02-22 16:04:48 +090080 fprintf(stderr, "error decoding %s : %s \n", fname, ZSTD_getErrorName(dSize));
Yann Collet0809a882016-07-09 18:25:10 +020081 exit(7);
82 }
83
84 /* success */
85 printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize);
86
87 free(rBuff);
88 free(cBuff);
89}
90
91
92int main(int argc, const char** argv)
93{
94 const char* const exeName = argv[0];
95
96 if (argc!=2) {
97 printf("wrong arguments\n");
98 printf("usage:\n");
99 printf("%s FILE\n", exeName);
100 return 1;
101 }
102
103 decompress(argv[1]);
104
Yann Collet677ed262016-07-10 14:23:30 +0200105 printf("%s correctly decoded (in memory). \n", argv[1]);
106
107 return 0;
Yann Collet0809a882016-07-09 18:25:10 +0200108}