xref: /aosp_15_r20/external/lz4/examples/fileCompress.c (revision 27162e4e17433d5aa7cb38e7b6a433a09405fc7f)
1 /* LZ4file API example : compress a file
2  * Modified from an example code by anjiahao
3  *
4  * This example will demonstrate how
5  * to manipulate lz4 compressed files like
6  * normal files */
7 
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 
14 #include <lz4file.h>
15 
16 
17 #define CHUNK_SIZE (16*1024)
18 
get_file_size(char * filename)19 static size_t get_file_size(char *filename)
20 {
21     struct stat statbuf;
22 
23     if (filename == NULL) {
24         return 0;
25     }
26 
27     if(stat(filename,&statbuf)) {
28         return 0;
29     }
30 
31     return statbuf.st_size;
32 }
33 
compress_file(FILE * f_in,FILE * f_out)34 static int compress_file(FILE* f_in, FILE* f_out)
35 {
36     assert(f_in != NULL); assert(f_out != NULL);
37 
38     LZ4F_errorCode_t ret = LZ4F_OK_NoError;
39     size_t len;
40     LZ4_writeFile_t* lz4fWrite;
41     void* const buf = malloc(CHUNK_SIZE);
42     if (!buf) {
43         printf("error: memory allocation failed \n");
44         return 1;
45     }
46 
47     /* Of course, you can also use prefsPtr to
48      * set the parameters of the compressed file
49      * NULL is use default
50      */
51     ret = LZ4F_writeOpen(&lz4fWrite, f_out, NULL);
52     if (LZ4F_isError(ret)) {
53         printf("LZ4F_writeOpen error: %s\n", LZ4F_getErrorName(ret));
54         free(buf);
55         return 1;
56     }
57 
58     while (1) {
59         len = fread(buf, 1, CHUNK_SIZE, f_in);
60 
61         if (ferror(f_in)) {
62             printf("fread error\n");
63             goto out;
64         }
65 
66         /* nothing to read */
67         if (len == 0) {
68             break;
69         }
70 
71         ret = LZ4F_write(lz4fWrite, buf, len);
72         if (LZ4F_isError(ret)) {
73             printf("LZ4F_write: %s\n", LZ4F_getErrorName(ret));
74             goto out;
75         }
76     }
77 
78 out:
79     free(buf);
80     if (LZ4F_isError(LZ4F_writeClose(lz4fWrite))) {
81         printf("LZ4F_writeClose: %s\n", LZ4F_getErrorName(ret));
82         return 1;
83     }
84 
85     return 0;
86 }
87 
decompress_file(FILE * f_in,FILE * f_out)88 static int decompress_file(FILE* f_in, FILE* f_out)
89 {
90     assert(f_in != NULL); assert(f_out != NULL);
91 
92     LZ4F_errorCode_t ret = LZ4F_OK_NoError;
93     LZ4_readFile_t* lz4fRead;
94     void* const buf= malloc(CHUNK_SIZE);
95     if (!buf) {
96         printf("error: memory allocation failed \n");
97     }
98 
99     ret = LZ4F_readOpen(&lz4fRead, f_in);
100     if (LZ4F_isError(ret)) {
101         printf("LZ4F_readOpen error: %s\n", LZ4F_getErrorName(ret));
102         free(buf);
103         return 1;
104     }
105 
106     while (1) {
107         ret = LZ4F_read(lz4fRead, buf, CHUNK_SIZE);
108         if (LZ4F_isError(ret)) {
109             printf("LZ4F_read error: %s\n", LZ4F_getErrorName(ret));
110             goto out;
111         }
112 
113         /* nothing to read */
114         if (ret == 0) {
115             break;
116         }
117 
118         if(fwrite(buf, 1, ret, f_out) != ret) {
119             printf("write error!\n");
120             goto out;
121         }
122     }
123 
124 out:
125     free(buf);
126     if (LZ4F_isError(LZ4F_readClose(lz4fRead))) {
127         printf("LZ4F_readClose: %s\n", LZ4F_getErrorName(ret));
128         return 1;
129     }
130 
131     if (ret) {
132         return 1;
133     }
134 
135     return 0;
136 }
137 
compareFiles(FILE * fp0,FILE * fp1)138 int compareFiles(FILE* fp0, FILE* fp1)
139 {
140     int result = 0;
141 
142     while (result==0) {
143         char b0[1024];
144         char b1[1024];
145         size_t const r0 = fread(b0, 1, sizeof(b0), fp0);
146         size_t const r1 = fread(b1, 1, sizeof(b1), fp1);
147 
148         result = (r0 != r1);
149         if (!r0 || !r1) break;
150         if (!result) result = memcmp(b0, b1, r0);
151     }
152 
153     return result;
154 }
155 
main(int argc,const char ** argv)156 int main(int argc, const char **argv) {
157     char inpFilename[256] = { 0 };
158     char lz4Filename[256] = { 0 };
159     char decFilename[256] = { 0 };
160 
161     if (argc < 2) {
162         printf("Please specify input filename\n");
163         return 0;
164     }
165 
166     snprintf(inpFilename, 256, "%s", argv[1]);
167     snprintf(lz4Filename, 256, "%s.lz4", argv[1]);
168     snprintf(decFilename, 256, "%s.lz4.dec", argv[1]);
169 
170     printf("inp = [%s]\n", inpFilename);
171     printf("lz4 = [%s]\n", lz4Filename);
172     printf("dec = [%s]\n", decFilename);
173 
174     /* compress */
175     {   FILE* const inpFp = fopen(inpFilename, "rb");
176         FILE* const outFp = fopen(lz4Filename, "wb");
177         printf("compress : %s -> %s\n", inpFilename, lz4Filename);
178         LZ4F_errorCode_t ret = compress_file(inpFp, outFp);
179         fclose(inpFp);
180         fclose(outFp);
181 
182         if (ret) {
183             printf("compression error: %s\n", LZ4F_getErrorName(ret));
184             return 1;
185         }
186 
187         printf("%s: %zu → %zu bytes, %.1f%%\n",
188             inpFilename,
189             get_file_size(inpFilename),
190             get_file_size(lz4Filename), /* might overflow is size_t is 32 bits and size_{in,out} > 4 GB */
191             (double)get_file_size(lz4Filename) / get_file_size(inpFilename) * 100);
192 
193         printf("compress : done\n");
194     }
195 
196     /* decompress */
197     {
198         FILE* const inpFp = fopen(lz4Filename, "rb");
199         FILE* const outFp = fopen(decFilename, "wb");
200 
201         printf("decompress : %s -> %s\n", lz4Filename, decFilename);
202         LZ4F_errorCode_t ret = decompress_file(inpFp, outFp);
203 
204         fclose(outFp);
205         fclose(inpFp);
206 
207         if (ret) {
208             printf("compression error: %s\n", LZ4F_getErrorName(ret));
209             return 1;
210         }
211 
212         printf("decompress : done\n");
213     }
214 
215     /* verify */
216     {   FILE* const inpFp = fopen(inpFilename, "rb");
217         FILE* const decFp = fopen(decFilename, "rb");
218 
219         printf("verify : %s <-> %s\n", inpFilename, decFilename);
220         int const cmp = compareFiles(inpFp, decFp);
221 
222         fclose(decFp);
223         fclose(inpFp);
224 
225         if (cmp) {
226             printf("corruption detected : decompressed file differs from original\n");
227             return cmp;
228         }
229 
230         printf("verify : OK\n");
231     }
232 
233 }
234