| Yann Collet | 394bdd7 | 2017-08-29 09:24:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| Yann Collet | 394bdd7 | 2017-08-29 09:24:11 -0700 | [diff] [blame] | 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). |
| Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
| Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 9 | */ |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 10 | |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 11 | #include <stdlib.h> // malloc, exit |
| 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 |
| 18 | |
| 19 | |
| Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 20 | static off_t fsize_orDie(const char *filename) |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 21 | { |
| 22 | struct stat st; |
| 23 | if (stat(filename, &st) == 0) return st.st_size; |
| 24 | /* error */ |
| Soojin Nam | 971c161 | 2017-02-22 16:04:48 +0900 | [diff] [blame] | 25 | fprintf(stderr, "stat: %s : %s \n", filename, strerror(errno)); |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 26 | exit(1); |
| 27 | } |
| 28 | |
| Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 29 | static FILE* fopen_orDie(const char *filename, const char *instruction) |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 30 | { |
| 31 | FILE* const inFile = fopen(filename, instruction); |
| 32 | if (inFile) return inFile; |
| 33 | /* error */ |
| Soojin Nam | 971c161 | 2017-02-22 16:04:48 +0900 | [diff] [blame] | 34 | fprintf(stderr, "fopen: %s : %s \n", filename, strerror(errno)); |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 35 | exit(2); |
| 36 | } |
| 37 | |
| Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 38 | static void* malloc_orDie(size_t size) |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 39 | { |
| Yann Collet | 1f3d54d | 2017-02-22 11:08:00 -0800 | [diff] [blame] | 40 | void* const buff = malloc(size + !size); /* avoid allocating size of 0 : may return NULL (implementation dependent) */ |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 41 | if (buff) return buff; |
| 42 | /* error */ |
| Soojin Nam | 971c161 | 2017-02-22 16:04:48 +0900 | [diff] [blame] | 43 | fprintf(stderr, "malloc: %s \n", strerror(errno)); |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 44 | exit(3); |
| 45 | } |
| 46 | |
| Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 47 | static void* loadFile_orDie(const char* fileName, size_t* size) |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 48 | { |
| Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [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 | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 52 | size_t const readSize = fread(buffer, 1, buffSize, inFile); |
| 53 | if (readSize != (size_t)buffSize) { |
| Soojin Nam | 971c161 | 2017-02-22 16:04:48 +0900 | [diff] [blame] | 54 | fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno)); |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 55 | exit(4); |
| 56 | } |
| Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 57 | fclose(inFile); /* can't fail (read only) */ |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 58 | *size = buffSize; |
| 59 | return buffer; |
| 60 | } |
| 61 | |
| 62 | |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 63 | static void decompress(const char* fname) |
| 64 | { |
| 65 | size_t cSize; |
| Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 66 | void* const cBuff = loadFile_orDie(fname, &cSize); |
| Sean Purcell | 4e70971 | 2017-02-07 13:50:09 -0800 | [diff] [blame] | 67 | unsigned long long const rSize = ZSTD_findDecompressedSize(cBuff, cSize); |
| Soojin Nam | 971c161 | 2017-02-22 16:04:48 +0900 | [diff] [blame] | 68 | if (rSize==ZSTD_CONTENTSIZE_ERROR) { |
| 69 | fprintf(stderr, "%s : it was not compressed by zstd.\n", fname); |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 70 | exit(5); |
| Soojin Nam | 971c161 | 2017-02-22 16:04:48 +0900 | [diff] [blame] | 71 | } else if (rSize==ZSTD_CONTENTSIZE_UNKNOWN) { |
| 72 | fprintf(stderr, |
| 73 | "%s : original size unknown. Use streaming decompression instead.\n", fname); |
| 74 | exit(6); |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 75 | } |
| Soojin Nam | 971c161 | 2017-02-22 16:04:48 +0900 | [diff] [blame] | 76 | |
| Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 77 | void* const rBuff = malloc_orDie((size_t)rSize); |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 78 | |
| 79 | size_t const dSize = ZSTD_decompress(rBuff, rSize, cBuff, cSize); |
| 80 | |
| 81 | if (dSize != rSize) { |
| Soojin Nam | 971c161 | 2017-02-22 16:04:48 +0900 | [diff] [blame] | 82 | fprintf(stderr, "error decoding %s : %s \n", fname, ZSTD_getErrorName(dSize)); |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 83 | exit(7); |
| 84 | } |
| 85 | |
| 86 | /* success */ |
| 87 | printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize); |
| 88 | |
| 89 | free(rBuff); |
| 90 | free(cBuff); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | int main(int argc, const char** argv) |
| 95 | { |
| 96 | const char* const exeName = argv[0]; |
| 97 | |
| 98 | if (argc!=2) { |
| 99 | printf("wrong arguments\n"); |
| 100 | printf("usage:\n"); |
| 101 | printf("%s FILE\n", exeName); |
| 102 | return 1; |
| 103 | } |
| 104 | |
| 105 | decompress(argv[1]); |
| 106 | |
| Yann Collet | 677ed26 | 2016-07-10 14:23:30 +0200 | [diff] [blame] | 107 | printf("%s correctly decoded (in memory). \n", argv[1]); |
| 108 | |
| 109 | return 0; |
| Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 110 | } |