Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 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 | */ |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 8 | |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 9 | |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 10 | |
| 11 | #include <stdlib.h> // malloc, exit |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 12 | #include <stdio.h> // printf |
| 13 | #include <string.h> // strerror |
| 14 | #include <errno.h> // errno |
| 15 | #include <sys/stat.h> // stat |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 16 | #define ZSTD_STATIC_LINKING_ONLY // ZSTD_findDecompressedSize |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 17 | #include <zstd.h> // presumes zstd library is installed |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 18 | |
| 19 | |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 20 | static off_t fsize_orDie(const char *filename) |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 21 | { |
| 22 | struct stat st; |
| 23 | if (stat(filename, &st) == 0) return st.st_size; |
| 24 | /* error */ |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 25 | perror(filename); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 26 | exit(1); |
| 27 | } |
| 28 | |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 29 | static FILE* fopen_orDie(const char *filename, const char *instruction) |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 30 | { |
| 31 | FILE* const inFile = fopen(filename, instruction); |
| 32 | if (inFile) return inFile; |
| 33 | /* error */ |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 34 | perror(filename); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 35 | exit(2); |
| 36 | } |
| 37 | |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 38 | static void* malloc_orDie(size_t size) |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 39 | { |
| 40 | void* const buff = malloc(size); |
| 41 | if (buff) return buff; |
| 42 | /* error */ |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 43 | perror("malloc"); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 44 | exit(3); |
| 45 | } |
| 46 | |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 47 | static void* loadFile_orDie(const char* fileName, size_t* size) |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 48 | { |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 49 | off_t const buffSize = fsize_orDie(fileName); |
| 50 | FILE* const inFile = fopen_orDie(fileName, "rb"); |
| 51 | void* const buffer = malloc_orDie(buffSize); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 52 | size_t const readSize = fread(buffer, 1, buffSize, inFile); |
| 53 | if (readSize != (size_t)buffSize) { |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 54 | fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno)); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 55 | exit(4); |
| 56 | } |
| 57 | fclose(inFile); |
| 58 | *size = buffSize; |
| 59 | return buffer; |
| 60 | } |
| 61 | |
Yann Collet | e66708d | 2016-07-09 22:56:12 +0200 | [diff] [blame] | 62 | /* createDict() : |
| 63 | `dictFileName` is supposed to have been created using `zstd --train` */ |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 64 | static ZSTD_DDict* createDict_orDie(const char* dictFileName) |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 65 | { |
| 66 | size_t dictSize; |
Yann Collet | 5e80dd3 | 2016-07-13 17:38:39 +0200 | [diff] [blame] | 67 | printf("loading dictionary %s \n", dictFileName); |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 68 | void* const dictBuffer = loadFile_orDie(dictFileName, &dictSize); |
| 69 | ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictSize); |
| 70 | if (ddict==NULL) { fprintf(stderr, "ZSTD_createDDict error \n"); exit(5); } |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 71 | free(dictBuffer); |
| 72 | return ddict; |
| 73 | } |
| 74 | |
| 75 | |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 76 | static void decompress(const char* fname, const ZSTD_DDict* ddict) |
| 77 | { |
| 78 | size_t cSize; |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 79 | void* const cBuff = loadFile_orDie(fname, &cSize); |
Sean Purcell | 4e70971 | 2017-02-07 13:50:09 -0800 | [diff] [blame] | 80 | unsigned long long const rSize = ZSTD_findDecompressedSize(cBuff, cSize); |
Soojin Nam | 971c161 | 2017-02-22 16:04:48 +0900 | [diff] [blame] | 81 | if (rSize==ZSTD_CONTENTSIZE_ERROR) { |
| 82 | fprintf(stderr, "%s : it was not compressed by zstd.\n", fname); |
| 83 | exit(5); |
| 84 | } else if (rSize==ZSTD_CONTENTSIZE_UNKNOWN) { |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 85 | fprintf(stderr, "%s : original size unknown \n", fname); |
| 86 | exit(6); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 87 | } |
Soojin Nam | 971c161 | 2017-02-22 16:04:48 +0900 | [diff] [blame] | 88 | |
Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 89 | void* const rBuff = malloc_orDie((size_t)rSize); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 90 | |
| 91 | ZSTD_DCtx* const dctx = ZSTD_createDCtx(); |
Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 92 | if (dctx==NULL) { fprintf(stderr, "ZSTD_createDCtx() error \n"); exit(10); } |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 93 | size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 94 | if (dSize != rSize) { |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 95 | fprintf(stderr, "error decoding %s : %s \n", fname, ZSTD_getErrorName(dSize)); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 96 | exit(7); |
| 97 | } |
| 98 | |
| 99 | /* success */ |
| 100 | printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize); |
| 101 | |
| 102 | ZSTD_freeDCtx(dctx); |
| 103 | free(rBuff); |
| 104 | free(cBuff); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | int main(int argc, const char** argv) |
| 109 | { |
| 110 | const char* const exeName = argv[0]; |
| 111 | |
| 112 | if (argc<3) { |
| 113 | printf("wrong arguments\n"); |
| 114 | printf("usage:\n"); |
| 115 | printf("%s [FILES] dictionary\n", exeName); |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | /* load dictionary only once */ |
| 120 | const char* const dictName = argv[argc-1]; |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 121 | ZSTD_DDict* const dictPtr = createDict_orDie(dictName); |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 122 | |
| 123 | int u; |
| 124 | for (u=1; u<argc-1; u++) decompress(argv[u], dictPtr); |
| 125 | |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 126 | ZSTD_freeDDict(dictPtr); |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 127 | printf("All %u files correctly decoded (in memory) \n", argc-2); |
Yann Collet | 0763905 | 2016-08-03 01:57:57 +0200 | [diff] [blame] | 128 | return 0; |
Yann Collet | 974f52f | 2016-07-07 14:08:00 +0200 | [diff] [blame] | 129 | } |