blob: 7c880725fc7600aced8afc514515dab1062287bc [file] [log] [blame]
Yann Collet394bdd72017-08-29 09:24:11 -07001/*
Elliott Hughes44aba642023-09-12 20:18:59 +00002 * Copyright (c) Meta Platforms, Inc. and affiliates.
Yann Collet4ded9e52016-08-30 10:04:33 -07003 * All rights reserved.
4 *
Yann Collet394bdd72017-08-29 09:24:11 -07005 * 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 Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Collet0809a882016-07-09 18:25:10 +020010
Nick Terrell1d0c1702019-04-05 18:11:17 -070011#include <stdio.h> // printf
12#include <stdlib.h> // free
13#include <string.h> // strlen, strcat, memset
Yann Collet0809a882016-07-09 18:25:10 +020014#include <zstd.h> // presumes zstd library is installed
Nick Terrell1d0c1702019-04-05 18:11:17 -070015#include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
Yann Collet677ed262016-07-10 14:23:30 +020016
Yann Collet4b9ca0a2016-07-27 19:53:19 +020017static void compress_orDie(const char* fname, const char* oname)
Yann Collet0809a882016-07-09 18:25:10 +020018{
19 size_t fSize;
Yi Jinbc4dc602018-12-17 16:54:55 -080020 void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize);
Yann Collet0809a882016-07-09 18:25:10 +020021 size_t const cBuffSize = ZSTD_compressBound(fSize);
Yann Collet4b9ca0a2016-07-27 19:53:19 +020022 void* const cBuff = malloc_orDie(cBuffSize);
Yann Collet0809a882016-07-09 18:25:10 +020023
Nick Terrell1d0c1702019-04-05 18:11:17 -070024 /* 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 Collet0809a882016-07-09 18:25:10 +020028 size_t const cSize = ZSTD_compress(cBuff, cBuffSize, fBuff, fSize, 1);
Nick Terrell1d0c1702019-04-05 18:11:17 -070029 CHECK_ZSTD(cSize);
Yann Collet0809a882016-07-09 18:25:10 +020030
Yann Collet4b9ca0a2016-07-27 19:53:19 +020031 saveFile_orDie(oname, cBuff, cSize);
Yann Collet677ed262016-07-10 14:23:30 +020032
Yann Collet0809a882016-07-09 18:25:10 +020033 /* success */
Yann Collet677ed262016-07-10 14:23:30 +020034 printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
Yann Collet0809a882016-07-09 18:25:10 +020035
36 free(fBuff);
37 free(cBuff);
38}
39
Yann Collet45960372017-02-15 12:00:03 -080040static char* createOutFilename_orDie(const char* filename)
Yann Collet677ed262016-07-10 14:23:30 +020041{
42 size_t const inL = strlen(filename);
43 size_t const outL = inL + 5;
Yann Colleta2664642016-09-09 19:33:56 +020044 void* const outSpace = malloc_orDie(outL);
Yann Collet677ed262016-07-10 14:23:30 +020045 memset(outSpace, 0, outL);
46 strcat(outSpace, filename);
47 strcat(outSpace, ".zst");
Yann Collet45960372017-02-15 12:00:03 -080048 return (char*)outSpace;
Yann Collet677ed262016-07-10 14:23:30 +020049}
50
Yann Collet0809a882016-07-09 18:25:10 +020051int 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
niXman65e2cda2017-04-26 13:04:04 +030062 const char* const inFilename = argv[1];
63
Yann Collet45960372017-02-15 12:00:03 -080064 char* const outFilename = createOutFilename_orDie(inFilename);
Yann Collet4b9ca0a2016-07-27 19:53:19 +020065 compress_orDie(inFilename, outFilename);
zefanxu22bb6fc22017-02-13 21:12:59 -060066 free(outFilename);
Yann Collet677ed262016-07-10 14:23:30 +020067 return 0;
Yann Collet0809a882016-07-09 18:25:10 +020068}