]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/zstd/examples/simple_decompression.c
Import zstandard 1.1.4 in base
[FreeBSD/FreeBSD.git] / contrib / zstd / examples / simple_decompression.c
1 /**
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  */
8
9 #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
14 #define ZSTD_STATIC_LINKING_ONLY   // ZSTD_findDecompressedSize
15 #include <zstd.h>      // presumes zstd library is installed
16
17
18 static off_t fsize_orDie(const char *filename)
19 {
20     struct stat st;
21     if (stat(filename, &st) == 0) return st.st_size;
22     /* error */
23     fprintf(stderr, "stat: %s : %s \n", filename, strerror(errno));
24     exit(1);
25 }
26
27 static FILE* fopen_orDie(const char *filename, const char *instruction)
28 {
29     FILE* const inFile = fopen(filename, instruction);
30     if (inFile) return inFile;
31     /* error */
32     fprintf(stderr, "fopen: %s : %s \n", filename, strerror(errno));
33     exit(2);
34 }
35
36 static void* malloc_orDie(size_t size)
37 {
38     void* const buff = malloc(size + !size);   /* avoid allocating size of 0 : may return NULL (implementation dependent) */
39     if (buff) return buff;
40     /* error */
41     fprintf(stderr, "malloc: %s \n", strerror(errno));
42     exit(3);
43 }
44
45 static void* loadFile_orDie(const char* fileName, size_t* size)
46 {
47     off_t const buffSize = fsize_orDie(fileName);
48     FILE* const inFile = fopen_orDie(fileName, "rb");
49     void* const buffer = malloc_orDie(buffSize);
50     size_t const readSize = fread(buffer, 1, buffSize, inFile);
51     if (readSize != (size_t)buffSize) {
52         fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
53         exit(4);
54     }
55     fclose(inFile);   /* can't fail (read only) */
56     *size = buffSize;
57     return buffer;
58 }
59
60
61 static void decompress(const char* fname)
62 {
63     size_t cSize;
64     void* const cBuff = loadFile_orDie(fname, &cSize);
65     unsigned long long const rSize = ZSTD_findDecompressedSize(cBuff, cSize);
66     if (rSize==ZSTD_CONTENTSIZE_ERROR) {
67         fprintf(stderr, "%s : it was not compressed by zstd.\n", fname);
68         exit(5);
69     } else if (rSize==ZSTD_CONTENTSIZE_UNKNOWN) {
70         fprintf(stderr,
71                 "%s : original size unknown. Use streaming decompression instead.\n", fname);
72         exit(6);
73     }
74
75     void* const rBuff = malloc_orDie((size_t)rSize);
76
77     size_t const dSize = ZSTD_decompress(rBuff, rSize, cBuff, cSize);
78
79     if (dSize != rSize) {
80         fprintf(stderr, "error decoding %s : %s \n", fname, ZSTD_getErrorName(dSize));
81         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
92 int 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
105     printf("%s correctly decoded (in memory). \n", argv[1]);
106
107     return 0;
108 }