Yann Collet | 394bdd7 | 2017-08-29 09:24:11 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
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 | |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 11 | #include <stdio.h> // printf |
| 12 | #include <stdlib.h> // free |
| 13 | #include <string.h> // strlen, strcat, memset |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 14 | #include <zstd.h> // presumes zstd library is installed |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 15 | #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD() |
Yann Collet | 677ed26 | 2016-07-10 14:23:30 +0200 | [diff] [blame] | 16 | |
Yann Collet | 4b9ca0a | 2016-07-27 19:53:19 +0200 | [diff] [blame] | 17 | static void compress_orDie(const char* fname, const char* oname) |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 18 | { |
| 19 | size_t fSize; |
Yi Jin | bc4dc60 | 2018-12-17 16:54:55 -0800 | [diff] [blame] | 20 | void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize); |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 21 | size_t const cBuffSize = ZSTD_compressBound(fSize); |
Yann Collet | 4b9ca0a | 2016-07-27 19:53:19 +0200 | [diff] [blame] | 22 | void* const cBuff = malloc_orDie(cBuffSize); |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 23 | |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 24 | /* Compress. |
| 25 | * If you are doing many compressions, you may want to reuse the context. |
| 26 | * See the multiple_simple_compression.c example. |
| 27 | */ |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 28 | size_t const cSize = ZSTD_compress(cBuff, cBuffSize, fBuff, fSize, 1); |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 29 | CHECK_ZSTD(cSize); |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 30 | |
Yann Collet | 4b9ca0a | 2016-07-27 19:53:19 +0200 | [diff] [blame] | 31 | saveFile_orDie(oname, cBuff, cSize); |
Yann Collet | 677ed26 | 2016-07-10 14:23:30 +0200 | [diff] [blame] | 32 | |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 33 | /* success */ |
Yann Collet | 677ed26 | 2016-07-10 14:23:30 +0200 | [diff] [blame] | 34 | printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname); |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 35 | |
| 36 | free(fBuff); |
| 37 | free(cBuff); |
| 38 | } |
| 39 | |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 40 | static char* createOutFilename_orDie(const char* filename) |
Yann Collet | 677ed26 | 2016-07-10 14:23:30 +0200 | [diff] [blame] | 41 | { |
| 42 | size_t const inL = strlen(filename); |
| 43 | size_t const outL = inL + 5; |
Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 44 | void* const outSpace = malloc_orDie(outL); |
Yann Collet | 677ed26 | 2016-07-10 14:23:30 +0200 | [diff] [blame] | 45 | memset(outSpace, 0, outL); |
| 46 | strcat(outSpace, filename); |
| 47 | strcat(outSpace, ".zst"); |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 48 | return (char*)outSpace; |
Yann Collet | 677ed26 | 2016-07-10 14:23:30 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 51 | int main(int argc, const char** argv) |
| 52 | { |
| 53 | const char* const exeName = argv[0]; |
| 54 | |
| 55 | if (argc!=2) { |
| 56 | printf("wrong arguments\n"); |
| 57 | printf("usage:\n"); |
| 58 | printf("%s FILE\n", exeName); |
| 59 | return 1; |
| 60 | } |
| 61 | |
niXman | 65e2cda | 2017-04-26 13:04:04 +0300 | [diff] [blame] | 62 | const char* const inFilename = argv[1]; |
| 63 | |
Yann Collet | 4596037 | 2017-02-15 12:00:03 -0800 | [diff] [blame] | 64 | char* const outFilename = createOutFilename_orDie(inFilename); |
Yann Collet | 4b9ca0a | 2016-07-27 19:53:19 +0200 | [diff] [blame] | 65 | compress_orDie(inFilename, outFilename); |
zefanxu2 | 2bb6fc2 | 2017-02-13 21:12:59 -0600 | [diff] [blame] | 66 | free(outFilename); |
Yann Collet | 677ed26 | 2016-07-10 14:23:30 +0200 | [diff] [blame] | 67 | return 0; |
Yann Collet | 0809a88 | 2016-07-09 18:25:10 +0200 | [diff] [blame] | 68 | } |