• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
45 
46     /* Of course, you can also use prefsPtr to
47      * set the parameters of the compressed file
48      * NULL is use default
49      */
50     ret = LZ4F_writeOpen(&lz4fWrite, f_out, NULL);
51     if (LZ4F_isError(ret)) {
52         printf("LZ4F_writeOpen error: %s\n", LZ4F_getErrorName(ret));
53         free(buf);
54         return 1;
55     }
56 
57     while (1) {
58         len = fread(buf, 1, CHUNK_SIZE, f_in);
59 
60         if (ferror(f_in)) {
61             printf("fread error\n");
62             goto out;
63         }
64 
65         /* nothing to read */
66         if (len == 0) {
67             break;
68         }
69 
70         ret = LZ4F_write(lz4fWrite, buf, len);
71         if (LZ4F_isError(ret)) {
72             printf("LZ4F_write: %s\n", LZ4F_getErrorName(ret));
73             goto out;
74         }
75     }
76 
77 out:
78     free(buf);
79     if (LZ4F_isError(LZ4F_writeClose(lz4fWrite))) {
80         printf("LZ4F_writeClose: %s\n", LZ4F_getErrorName(ret));
81         return 1;
82     }
83 
84     return 0;
85 }
86 
decompress_file(FILE * f_in,FILE * f_out)87 static int decompress_file(FILE* f_in, FILE* f_out)
88 {
89     assert(f_in != NULL); assert(f_out != NULL);
90 
91     LZ4F_errorCode_t ret = LZ4F_OK_NoError;
92     LZ4_readFile_t* lz4fRead;
93     void* const buf= malloc(CHUNK_SIZE);
94     if (!buf) {
95         printf("error: memory allocation failed \n");
96     }
97 
98     ret = LZ4F_readOpen(&lz4fRead, f_in);
99     if (LZ4F_isError(ret)) {
100         printf("LZ4F_readOpen error: %s\n", LZ4F_getErrorName(ret));
101         free(buf);
102         return 1;
103     }
104 
105     while (1) {
106         ret = LZ4F_read(lz4fRead, buf, CHUNK_SIZE);
107         if (LZ4F_isError(ret)) {
108             printf("LZ4F_read error: %s\n", LZ4F_getErrorName(ret));
109             goto out;
110         }
111 
112         /* nothing to read */
113         if (ret == 0) {
114             break;
115         }
116 
117         if(fwrite(buf, 1, ret, f_out) != ret) {
118             printf("write error!\n");
119             goto out;
120         }
121     }
122 
123 out:
124     free(buf);
125     if (LZ4F_isError(LZ4F_readClose(lz4fRead))) {
126         printf("LZ4F_readClose: %s\n", LZ4F_getErrorName(ret));
127         return 1;
128     }
129 
130     if (ret) {
131         return 1;
132     }
133 
134     return 0;
135 }
136 
compareFiles(FILE * fp0,FILE * fp1)137 int compareFiles(FILE* fp0, FILE* fp1)
138 {
139     int result = 0;
140 
141     while (result==0) {
142         char b0[1024];
143         char b1[1024];
144         size_t const r0 = fread(b0, 1, sizeof(b0), fp0);
145         size_t const r1 = fread(b1, 1, sizeof(b1), fp1);
146 
147         result = (r0 != r1);
148         if (!r0 || !r1) break;
149         if (!result) result = memcmp(b0, b1, r0);
150     }
151 
152     return result;
153 }
154 
main(int argc,const char ** argv)155 int main(int argc, const char **argv) {
156     char inpFilename[256] = { 0 };
157     char lz4Filename[256] = { 0 };
158     char decFilename[256] = { 0 };
159 
160     if (argc < 2) {
161         printf("Please specify input filename\n");
162         return 0;
163     }
164 
165     snprintf(inpFilename, 256, "%s", argv[1]);
166     snprintf(lz4Filename, 256, "%s.lz4", argv[1]);
167     snprintf(decFilename, 256, "%s.lz4.dec", argv[1]);
168 
169     printf("inp = [%s]\n", inpFilename);
170     printf("lz4 = [%s]\n", lz4Filename);
171     printf("dec = [%s]\n", decFilename);
172 
173     /* compress */
174     {   FILE* const inpFp = fopen(inpFilename, "rb");
175         FILE* const outFp = fopen(lz4Filename, "wb");
176         printf("compress : %s -> %s\n", inpFilename, lz4Filename);
177         LZ4F_errorCode_t ret = compress_file(inpFp, outFp);
178         fclose(inpFp);
179         fclose(outFp);
180 
181         if (ret) {
182             printf("compression error: %s\n", LZ4F_getErrorName(ret));
183             return 1;
184         }
185 
186         printf("%s: %zu → %zu bytes, %.1f%%\n",
187             inpFilename,
188             get_file_size(inpFilename),
189             get_file_size(lz4Filename), /* might overflow is size_t is 32 bits and size_{in,out} > 4 GB */
190             (double)get_file_size(lz4Filename) / get_file_size(inpFilename) * 100);
191 
192         printf("compress : done\n");
193     }
194 
195     /* decompress */
196     {
197         FILE* const inpFp = fopen(lz4Filename, "rb");
198         FILE* const outFp = fopen(decFilename, "wb");
199 
200         printf("decompress : %s -> %s\n", lz4Filename, decFilename);
201         LZ4F_errorCode_t ret = decompress_file(inpFp, outFp);
202 
203         fclose(outFp);
204         fclose(inpFp);
205 
206         if (ret) {
207             printf("compression error: %s\n", LZ4F_getErrorName(ret));
208             return 1;
209         }
210 
211         printf("decompress : done\n");
212     }
213 
214     /* verify */
215     {   FILE* const inpFp = fopen(inpFilename, "rb");
216         FILE* const decFp = fopen(decFilename, "rb");
217 
218         printf("verify : %s <-> %s\n", inpFilename, decFilename);
219         int const cmp = compareFiles(inpFp, decFp);
220 
221         fclose(decFp);
222         fclose(inpFp);
223 
224         if (cmp) {
225             printf("corruption detected : decompressed file differs from original\n");
226             return cmp;
227         }
228 
229         printf("verify : OK\n");
230     }
231 
232 }
233