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 | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 10 | |
Yann Collet | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 11 | |
Yann Collet | 16a5752 | 2016-12-08 17:28:26 -0800 | [diff] [blame] | 12 | #include <stdlib.h> // malloc, free, exit |
| 13 | #include <stdio.h> // fprintf, perror, feof, fopen, etc. |
| 14 | #include <string.h> // strlen, memset, strcat |
Yann Collet | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 15 | #include <zstd.h> // presumes zstd library is installed |
| 16 | |
| 17 | |
| 18 | static void* malloc_orDie(size_t size) |
| 19 | { |
| 20 | void* const buff = malloc(size); |
| 21 | if (buff) return buff; |
| 22 | /* error */ |
Yann Collet | 9f9f1fc | 2016-08-12 18:56:27 +0200 | [diff] [blame] | 23 | perror("malloc:"); |
Yann Collet | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 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 | perror(filename); |
| 33 | exit(3); |
| 34 | } |
| 35 | |
| 36 | static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file) |
| 37 | { |
| 38 | size_t const readSize = fread(buffer, 1, sizeToRead, file); |
| 39 | if (readSize == sizeToRead) return readSize; /* good */ |
| 40 | if (feof(file)) return readSize; /* good, reached end of file */ |
| 41 | /* error */ |
| 42 | perror("fread"); |
| 43 | exit(4); |
| 44 | } |
| 45 | |
| 46 | static size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file) |
| 47 | { |
| 48 | size_t const writtenSize = fwrite(buffer, 1, sizeToWrite, file); |
| 49 | if (writtenSize == sizeToWrite) return sizeToWrite; /* good */ |
| 50 | /* error */ |
| 51 | perror("fwrite"); |
| 52 | exit(5); |
| 53 | } |
| 54 | |
| 55 | static size_t fclose_orDie(FILE* file) |
| 56 | { |
| 57 | if (!fclose(file)) return 0; |
| 58 | /* error */ |
| 59 | perror("fclose"); |
| 60 | exit(6); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | static void compressFile_orDie(const char* fname, const char* outName, int cLevel) |
| 65 | { |
| 66 | FILE* const fin = fopen_orDie(fname, "rb"); |
| 67 | FILE* const fout = fopen_orDie(outName, "wb"); |
Yann Collet | 88aa179 | 2016-09-18 11:58:30 +0200 | [diff] [blame] | 68 | size_t const buffInSize = ZSTD_CStreamInSize(); /* can always read one full block */ |
Yann Collet | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 69 | void* const buffIn = malloc_orDie(buffInSize); |
Yann Collet | 88aa179 | 2016-09-18 11:58:30 +0200 | [diff] [blame] | 70 | size_t const buffOutSize = ZSTD_CStreamOutSize(); /* can always flush a full block */ |
Yann Collet | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 71 | void* const buffOut = malloc_orDie(buffOutSize); |
Yann Collet | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 72 | |
| 73 | ZSTD_CStream* const cstream = ZSTD_createCStream(); |
| 74 | if (cstream==NULL) { fprintf(stderr, "ZSTD_createCStream() error \n"); exit(10); } |
| 75 | size_t const initResult = ZSTD_initCStream(cstream, cLevel); |
Yann Collet | 86bdcd8 | 2016-09-20 11:54:29 +0200 | [diff] [blame] | 76 | if (ZSTD_isError(initResult)) { fprintf(stderr, "ZSTD_initCStream() error : %s \n", ZSTD_getErrorName(initResult)); exit(11); } |
Yann Collet | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 77 | |
Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 78 | size_t read, toRead = buffInSize; |
Yann Collet | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 79 | while( (read = fread_orDie(buffIn, toRead, fin)) ) { |
Yann Collet | 2065879 | 2016-08-17 01:48:43 +0200 | [diff] [blame] | 80 | ZSTD_inBuffer input = { buffIn, read, 0 }; |
| 81 | while (input.pos < input.size) { |
| 82 | ZSTD_outBuffer output = { buffOut, buffOutSize, 0 }; |
Yann Collet | 88aa179 | 2016-09-18 11:58:30 +0200 | [diff] [blame] | 83 | toRead = ZSTD_compressStream(cstream, &output , &input); /* toRead is guaranteed to be <= ZSTD_CStreamInSize() */ |
Yann Collet | 86bdcd8 | 2016-09-20 11:54:29 +0200 | [diff] [blame] | 84 | if (ZSTD_isError(toRead)) { fprintf(stderr, "ZSTD_compressStream() error : %s \n", ZSTD_getErrorName(toRead)); exit(12); } |
Yann Collet | 16a5752 | 2016-12-08 17:28:26 -0800 | [diff] [blame] | 85 | if (toRead > buffInSize) toRead = buffInSize; /* Safely handle case when `buffInSize` is manually changed to a value < ZSTD_CStreamInSize()*/ |
Yann Collet | 2065879 | 2016-08-17 01:48:43 +0200 | [diff] [blame] | 86 | fwrite_orDie(buffOut, output.pos, fout); |
Yann Collet | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Yann Collet | 2065879 | 2016-08-17 01:48:43 +0200 | [diff] [blame] | 90 | ZSTD_outBuffer output = { buffOut, buffOutSize, 0 }; |
Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 91 | size_t const remainingToFlush = ZSTD_endStream(cstream, &output); /* close frame */ |
Yann Collet | 86bdcd8 | 2016-09-20 11:54:29 +0200 | [diff] [blame] | 92 | if (remainingToFlush) { fprintf(stderr, "not fully flushed"); exit(13); } |
Yann Collet | 2065879 | 2016-08-17 01:48:43 +0200 | [diff] [blame] | 93 | fwrite_orDie(buffOut, output.pos, fout); |
Yann Collet | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 94 | |
Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 95 | ZSTD_freeCStream(cstream); |
Yann Collet | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 96 | fclose_orDie(fout); |
| 97 | fclose_orDie(fin); |
| 98 | free(buffIn); |
| 99 | free(buffOut); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | static const char* createOutFilename_orDie(const char* filename) |
| 104 | { |
| 105 | size_t const inL = strlen(filename); |
| 106 | size_t const outL = inL + 5; |
| 107 | void* outSpace = malloc_orDie(outL); |
| 108 | memset(outSpace, 0, outL); |
| 109 | strcat(outSpace, filename); |
| 110 | strcat(outSpace, ".zst"); |
| 111 | return (const char*)outSpace; |
| 112 | } |
| 113 | |
| 114 | int main(int argc, const char** argv) |
| 115 | { |
| 116 | const char* const exeName = argv[0]; |
Yann Collet | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 117 | |
| 118 | if (argc!=2) { |
| 119 | printf("wrong arguments\n"); |
| 120 | printf("usage:\n"); |
| 121 | printf("%s FILE\n", exeName); |
| 122 | return 1; |
| 123 | } |
| 124 | |
niXman | 65e2cda | 2017-04-26 13:04:04 +0300 | [diff] [blame] | 125 | const char* const inFilename = argv[1]; |
| 126 | |
Yann Collet | 553b213 | 2016-08-12 18:42:25 +0200 | [diff] [blame] | 127 | const char* const outFilename = createOutFilename_orDie(inFilename); |
| 128 | compressFile_orDie(inFilename, outFilename, 1); |
| 129 | |
| 130 | return 0; |
| 131 | } |