git subrepo pull (merge) --force deps/libchdr
[pcsx_rearmed.git] / deps / libchdr / deps / zstd-1.5.5 / contrib / seekable_format / examples / seekable_compression.c
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
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).
8  */
9
10 #include <stdlib.h>    // malloc, free, exit, atoi
11 #include <stdio.h>     // fprintf, perror, feof, fopen, etc.
12 #include <string.h>    // strlen, memset, strcat
13 #define ZSTD_STATIC_LINKING_ONLY
14 #include <zstd.h>      // presumes zstd library is installed
15
16 #include "../zstd_seekable.h"
17
18 static void* malloc_orDie(size_t size)
19 {
20     void* const buff = malloc(size);
21     if (buff) return buff;
22     /* error */
23     perror("malloc:");
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 static void compressFile_orDie(const char* fname, const char* outName, int cLevel, unsigned frameSize)
64 {
65     FILE* const fin  = fopen_orDie(fname, "rb");
66     FILE* const fout = fopen_orDie(outName, "wb");
67     size_t const buffInSize = ZSTD_CStreamInSize();    /* can always read one full block */
68     void*  const buffIn  = malloc_orDie(buffInSize);
69     size_t const buffOutSize = ZSTD_CStreamOutSize();  /* can always flush a full block */
70     void*  const buffOut = malloc_orDie(buffOutSize);
71
72     ZSTD_seekable_CStream* const cstream = ZSTD_seekable_createCStream();
73     if (cstream==NULL) { fprintf(stderr, "ZSTD_seekable_createCStream() error \n"); exit(10); }
74     size_t const initResult = ZSTD_seekable_initCStream(cstream, cLevel, 1, frameSize);
75     if (ZSTD_isError(initResult)) { fprintf(stderr, "ZSTD_seekable_initCStream() error : %s \n", ZSTD_getErrorName(initResult)); exit(11); }
76
77     size_t read, toRead = buffInSize;
78     while( (read = fread_orDie(buffIn, toRead, fin)) ) {
79         ZSTD_inBuffer input = { buffIn, read, 0 };
80         while (input.pos < input.size) {
81             ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
82             toRead = ZSTD_seekable_compressStream(cstream, &output , &input);   /* toRead is guaranteed to be <= ZSTD_CStreamInSize() */
83             if (ZSTD_isError(toRead)) { fprintf(stderr, "ZSTD_seekable_compressStream() error : %s \n", ZSTD_getErrorName(toRead)); exit(12); }
84             if (toRead > buffInSize) toRead = buffInSize;   /* Safely handle case when `buffInSize` is manually changed to a value < ZSTD_CStreamInSize()*/
85             fwrite_orDie(buffOut, output.pos, fout);
86         }
87     }
88
89     while (1) {
90         ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
91         size_t const remainingToFlush = ZSTD_seekable_endStream(cstream, &output);   /* close stream */
92         if (ZSTD_isError(remainingToFlush)) { fprintf(stderr, "ZSTD_seekable_endStream() error : %s \n", ZSTD_getErrorName(remainingToFlush)); exit(13); }
93         fwrite_orDie(buffOut, output.pos, fout);
94         if (!remainingToFlush) break;
95     }
96
97     ZSTD_seekable_freeCStream(cstream);
98     fclose_orDie(fout);
99     fclose_orDie(fin);
100     free(buffIn);
101     free(buffOut);
102 }
103
104 static char* createOutFilename_orDie(const char* filename)
105 {
106     size_t const inL = strlen(filename);
107     size_t const outL = inL + 5;
108     void* outSpace = malloc_orDie(outL);
109     memset(outSpace, 0, outL);
110     strcat(outSpace, filename);
111     strcat(outSpace, ".zst");
112     return (char*)outSpace;
113 }
114
115 #define CLEVEL_DEFAULT 5
116 int main(int argc, const char** argv)
117 {
118     const char* const exeName = argv[0];
119     if (argc<3 || argc>4) {
120         printf("wrong arguments \n");
121         printf("usage: \n");
122         printf("%s FILE FRAME_SIZE [LEVEL] \n", exeName);
123         return 1;
124     }
125
126     {   const char* const inFileName = argv[1];
127         unsigned const frameSize = (unsigned)atoi(argv[2]);
128         int const cLevel = (argc==4) ? atoi(argv[3]) : CLEVEL_DEFAULT;
129
130         char* const outFileName = createOutFilename_orDie(inFileName);
131         compressFile_orDie(inFileName, outFileName, cLevel, frameSize);
132         free(outFileName);
133     }
134
135     return 0;
136 }