-
Notifications
You must be signed in to change notification settings - Fork 0
/
izlib.h
366 lines (343 loc) · 9.89 KB
/
izlib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#ifndef iZLIB_H
#define iZLIB_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <sys/stat.h>
#include "igzip_lib.h"
#ifndef UNIX
#define UNIX 3
#endif
#ifndef BUF_SIZE
#define BUF_SIZE (1<<22)
#endif
#ifndef HDR_SIZE
#define HDR_SIZE (1<<16)
#endif
#ifndef MIN_COM_LVL
#define MIN_COM_LVL 0
#endif
#ifndef MAX_COM_LVL
#define MAX_COM_LVL 3
#endif
#ifndef COM_LVL_DEFAULT
#define COM_LVL_DEFAULT 3 // was 2
#endif
const int com_lvls[4] = {
ISAL_DEF_LVL0_DEFAULT,
ISAL_DEF_LVL1_DEFAULT,
ISAL_DEF_LVL2_DEFAULT,
ISAL_DEF_LVL3_DEFAULT
};
typedef struct
{
FILE *fp;
char *mode;
int is_plain;
struct isal_gzip_header *gzip_header;
struct inflate_state *state;
struct isal_zstream *zstream;
uint8_t *buf_in;
size_t buf_in_size;
uint8_t *buf_out;
size_t buf_out_size;
} gzFile_t;
typedef gzFile_t* gzFile;
#ifdef __cplusplus
extern "C" {
#endif
int is_gz(FILE* fp);
int is_plain(FILE* fp);
uint32_t get_posix_filetime(FILE* fp);
int ingest_gzip_header(gzFile fp);
gzFile gzopen(const char *in, const char *mode);
gzFile gzdopen(int fd, const char *mode);
int gzread(gzFile fp, void *buf, size_t len);
int gzwrite(gzFile fp, void *buf, size_t len);
int gzeof(gzFile fp);
int set_compress_level(gzFile fp, int level);
void gzclose(gzFile fp);
#ifdef __cplusplus
}
#endif
int is_gz(FILE* fp)
{
if(!fp) return 0;
char buf[2];
int gzip = 0;
if(fread(buf, 1, 2, fp) == 2)
if(((int)buf[0] == 0x1f) && ((int)(buf[1]&0xFF) == 0x8b))
gzip = 1; // normal gzip
fseek(fp, 12, SEEK_SET);
if(fread(buf, 1, 2, fp) == 2)
if(gzip == 1 && (int)buf[0] == 0x42 && (int)(buf[1]&0xFF) == 0x43)
gzip = 2; // bgzf format, need to require the normal gzip header
fseek(fp, 0, SEEK_SET);
return gzip;
}
int is_plain(FILE* fp)
{
int r = 0;
if(!feof(fp))
{
int c = getc(fp);
r = isprint(c);
ungetc(c, fp);
}
return r;
}
uint32_t get_posix_filetime(FILE* fp)
{
struct stat file_stats;
fstat(fileno(fp), &file_stats);
return file_stats.st_mtime;
}
int ingest_gzip_header(gzFile fp) {
// assume fp->state->avail_in > 0
int status = isal_read_gzip_header(fp->state, fp->gzip_header);
while (status == ISAL_END_INPUT && !feof(fp->fp)) {
fp->state->next_in = fp->buf_in;
fp->state->avail_in = fread(fp->state->next_in, 1, fp->buf_in_size, fp->fp);
status = isal_read_gzip_header(fp->state, fp->gzip_header);
}
return status;
}
gzFile gzopen(const char *in, const char *mode)
{
gzFile fp = (gzFile_t *)calloc(1, sizeof(gzFile_t));
fp->fp = fopen(in, mode);
if(!fp->fp)
{
gzclose(fp);
return NULL;
}
fp->mode = strdup(mode);
// plain file
if(*mode == 'r' && (fp->is_plain = !is_gz(fp->fp)))
return fp;
// gz file
fp->gzip_header = (struct isal_gzip_header *)calloc(1, sizeof(struct isal_gzip_header));
isal_gzip_header_init(fp->gzip_header);
if (*mode == 'r') // read
{
fp->state = (struct inflate_state *)calloc(1, sizeof(struct inflate_state));
fp->buf_in_size = BUF_SIZE;
fp->buf_in = (uint8_t *)malloc(fp->buf_in_size * sizeof(uint8_t));
isal_inflate_init(fp->state);
fp->state->crc_flag = ISAL_GZIP_NO_HDR_VER;
fp->state->next_in = fp->buf_in;
fp->state->avail_in = fread(fp->state->next_in, 1, fp->buf_in_size, fp->fp);
if (ingest_gzip_header(fp) != ISAL_DECOMP_OK)
{
gzclose(fp);
return NULL;
}
}
else if (*mode == 'w') // write
{
fp->gzip_header->os = UNIX; // FIXME auto parse OS
fp->gzip_header->time = get_posix_filetime(fp->fp);
fp->gzip_header->name = strdup(in);
fp->gzip_header->name_buf_len = strlen(fp->gzip_header->name) + 1;
fp->buf_out_size = BUF_SIZE;
fp->buf_out = (uint8_t *)calloc(fp->buf_out_size, sizeof(uint8_t));
fp->zstream = (struct isal_zstream *)calloc(1, sizeof(struct isal_zstream));
isal_deflate_init(fp->zstream);
fp->zstream->avail_in = 0;
fp->zstream->flush = NO_FLUSH;
fp->zstream->level = COM_LVL_DEFAULT;
fp->zstream->level_buf_size = com_lvls[fp->zstream->level];
fp->zstream->level_buf = (uint8_t *)calloc(fp->zstream->level_buf_size, sizeof(uint8_t));
fp->zstream->gzip_flag = IGZIP_GZIP_NO_HDR;
fp->zstream->avail_out = fp->buf_out_size;
fp->zstream->next_out = fp->buf_out;
if(isal_write_gzip_header(fp->zstream, fp->gzip_header) != ISAL_DECOMP_OK)
{
gzclose(fp);
return NULL;
}
}
return fp;
}
gzFile gzdopen(int fd, const char *mode)
{
char path[10]; /* identifier for error messages */
if (fd == -1)
return NULL;
sprintf(path, "<fd:%d>", fd); /* for debugging */
gzFile fp = (gzFile_t *)calloc(1, sizeof(gzFile_t));
if(!(fp->fp = fdopen(fd, mode)))
{
gzclose(fp);
return NULL;
}
fp->mode = strdup(mode);
// plain file
if(*mode == 'r' && (fp->is_plain = is_plain(fp->fp)))
return fp;
// gz file
fp->gzip_header = (struct isal_gzip_header *)calloc(1, sizeof(struct isal_gzip_header));
isal_gzip_header_init(fp->gzip_header);
if (*mode == 'r') // read
{
fp->state = (struct inflate_state *)calloc(1, sizeof(struct inflate_state));
fp->buf_in_size = BUF_SIZE;
fp->buf_in = (uint8_t *)malloc(fp->buf_in_size * sizeof(uint8_t));
isal_inflate_init(fp->state);
fp->state->crc_flag = ISAL_GZIP_NO_HDR_VER;
fp->state->next_in = fp->buf_in;
fp->state->avail_in = fread(fp->state->next_in, 1, fp->buf_in_size, fp->fp);
if (ingest_gzip_header(fp) != ISAL_DECOMP_OK)
{
gzclose(fp);
return NULL;
}
}
else if (*mode == 'w') // write
{
fp->gzip_header->os = UNIX; // FIXME auto parse OS
fp->gzip_header->time = get_posix_filetime(fp->fp);
fp->gzip_header->name = strdup(path);
fp->gzip_header->name_buf_len = strlen(fp->gzip_header->name) + 1;
fp->buf_out_size = BUF_SIZE;
fp->buf_out = (uint8_t *)calloc(fp->buf_out_size, sizeof(uint8_t));
fp->zstream = (struct isal_zstream *)calloc(1, sizeof(struct isal_zstream));
isal_deflate_init(fp->zstream);
fp->zstream->avail_in = 0;
fp->zstream->flush = NO_FLUSH;
fp->zstream->level = COM_LVL_DEFAULT;
fp->zstream->level_buf_size = com_lvls[fp->zstream->level];
fp->zstream->level_buf = (uint8_t *)calloc(fp->zstream->level_buf_size, sizeof(uint8_t));
fp->zstream->gzip_flag = IGZIP_GZIP_NO_HDR;
fp->zstream->avail_out = fp->buf_out_size;
fp->zstream->next_out = fp->buf_out;
if(isal_write_gzip_header(fp->zstream, fp->gzip_header) != ISAL_DECOMP_OK)
{
gzclose(fp);
return NULL;
}
}
return fp;
}
void gzclose(gzFile fp)
{
if(!fp) return;
if(fp->mode) free(fp->mode);
if(fp->zstream && fp->fp) gzwrite(fp, NULL, 0);
if(fp->gzip_header)
{
if(fp->gzip_header->extra) free(fp->gzip_header->extra);
if(fp->gzip_header->name) free(fp->gzip_header->name);
if(fp->gzip_header->comment) free(fp->gzip_header->comment);
free(fp->gzip_header);
}
if(fp->state) free(fp->state);
if(fp->buf_in) free(fp->buf_in);
if(fp->buf_out) free(fp->buf_out);
if(fp->zstream){
if(fp->zstream->level_buf) free(fp->zstream->level_buf);
free(fp->zstream);
}
if(fp->fp) fclose(fp->fp);
free(fp);
}
int gzread(gzFile fp, void *buf, size_t len)
{
int buf_data_len = 0;
if (fp->is_plain)
{
if(!feof(fp->fp))
buf_data_len = fread((uint8_t *)buf, 1, len, fp->fp);
return buf_data_len;
}
do // Start reading in compressed data and decompress
{
if (!feof(fp->fp) && !fp->state->avail_in)
{
fp->state->next_in = fp->buf_in;
fp->state->avail_in = fread(fp->state->next_in, 1, fp->buf_in_size, fp->fp);
}
fp->state->next_out = (uint8_t *)buf;
fp->state->avail_out = len;
if (isal_inflate(fp->state) != ISAL_DECOMP_OK)
return -3;
if ((buf_data_len = fp->state->next_out - (uint8_t *)buf))
return buf_data_len;
} while (fp->state->block_state != ISAL_BLOCK_FINISH // while not done
&& (!feof(fp->fp) || !fp->state->avail_out)); // and work to do
// Add the following to look for and decode additional concatenated files
if (!feof(fp->fp) && !fp->state->avail_in)
{
fp->state->next_in = fp->buf_in;
fp->state->avail_in = fread(fp->state->next_in, 1, fp->buf_in_size, fp->fp);
}
while (fp->state->avail_in && fp->state->next_in[0] == 31) // 0x1f
{
// Look for magic numbers for gzip header. Follows the gzread() decision
// whether to treat as trailing junk
if (fp->state->avail_in > 1 && fp->state->next_in[1] != 139) // 0x8b
break;
isal_inflate_reset(fp->state);
isal_gzip_header_init(fp->gzip_header);
fp->state->crc_flag = ISAL_GZIP_NO_HDR_VER;
if (ingest_gzip_header(fp) != ISAL_DECOMP_OK) return -3; // fail to parse header
do
{
if (!feof(fp->fp) && !fp->state->avail_in)
{
fp->state->next_in = fp->buf_in;
fp->state->avail_in = fread(fp->state->next_in, 1, fp->buf_in_size, fp->fp);
}
fp->state->next_out = (uint8_t *)buf;
fp->state->avail_out = len;
if (isal_inflate(fp->state) != ISAL_DECOMP_OK)
return -3;
if ((buf_data_len = fp->state->next_out - (uint8_t *)buf))
return buf_data_len;
} while (fp->state->block_state != ISAL_BLOCK_FINISH
&& (!feof(fp->fp) || !fp->state->avail_out));
}
return buf_data_len;
}
int set_compress_level(gzFile fp, int level)
{
if (!fp || !fp->mode || *fp->mode != 'w') return -1;
if (level < MIN_COM_LVL || level > MAX_COM_LVL) return -1;
if (fp->zstream->level != level)
{
fp->zstream->level = level;
fp->zstream->level_buf_size = com_lvls[fp->zstream->level];
fp->zstream->level_buf = (uint8_t *)realloc(fp->zstream->level_buf,
fp->zstream->level_buf_size * sizeof(uint8_t));
}
return 0;
}
int gzwrite(gzFile fp, void *buf, size_t _len)
{
fp->zstream->next_in = (uint8_t *)buf;
fp->zstream->avail_in = _len;
fp->zstream->end_of_stream = !buf;
size_t len = 0;
do
{
if(!fp->zstream->next_out)
{
fp->zstream->next_out = fp->buf_out;
fp->zstream->avail_out = fp->buf_out_size;
}
int ret = isal_deflate(fp->zstream);
if (ret != ISAL_DECOMP_OK) return -3;
len += fwrite(fp->buf_out, 1, fp->zstream->next_out - fp->buf_out, fp->fp);
fp->zstream->next_out = NULL;
} while (!fp->zstream->avail_out);
return len;
}
int gzeof(gzFile fp)
{
if(!fp) return 0;
if(fp->mode[0] != 'w' || fp->mode[0] != 'r') return 0;
return fp->mode[0] == 'r' ? feof(fp->fp) : 0;
}
#endif