-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip_file_stream.c
230 lines (204 loc) · 7.17 KB
/
zip_file_stream.c
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
#include <stddef.h>
#include <sys/types.h>
#include <stdarg.h>
#include <stdlib.h>
#ifdef HAVE_GZIP
#include <zlib.h>
#endif
#include "zip_file_stream.h"
#ifdef HAVE_LIBZIP
static ssize_t zip_file_stream_read(struct stream *stream, void *ptr, size_t size) {
struct zip_file_stream *zip_file_stream = (struct zip_file_stream *)stream;
size_t r = zip_fread(zip_file_stream->f, ptr, size);
stream->_errno = zip_error_code_system(zip_file_get_error(zip_file_stream->f));
return r;
}
static ssize_t zip_file_stream_write(struct stream *stream, const void *ptr, size_t size) {
(void)stream;
(void)ptr;
(void)size;
return 0;
}
static size_t zip_file_stream_seek(struct stream *stream, long offset, int whence) {
struct zip_file_stream *zip_file_stream = (struct zip_file_stream *)stream;
size_t r = zip_fseek(zip_file_stream->f, offset, whence);
stream->_errno = zip_error_code_system(zip_file_get_error(zip_file_stream->f));
return r;
}
static int zip_file_stream_eof(struct stream *stream) {
struct zip_file_stream *zip_file_stream = (struct zip_file_stream *)stream;
zip_int64_t t = zip_ftell(zip_file_stream->f);
if(t < 0) return t;
return t == (zip_int64_t)zip_file_stream->stat.size;
}
static long zip_file_stream_tell(struct stream *stream) {
struct zip_file_stream *zip_file_stream = (struct zip_file_stream *)stream;
return zip_ftell(zip_file_stream->f);
}
static int zip_file_stream_vprintf(struct stream *stream, const char *fmt, va_list ap) {
(void)stream;
(void)fmt;
(void)ap;
return -1;
}
static void *zip_file_stream_get_memory_access(struct stream *stream, size_t *length) {
struct zip_file_stream *zip_file_stream = (struct zip_file_stream *)stream;
stream->mem = malloc(zip_file_stream->stat.size);
if(!stream->mem) return 0;
int r = zip_fseek(zip_file_stream->f, 0, SEEK_SET);
if(r) {
free(stream->mem);
return 0;
}
r = zip_fread(zip_file_stream->f, stream->mem, zip_file_stream->stat.size);
if(r < 0) {
free(stream->mem);
return 0;
}
if(length) *length = zip_file_stream->stat.size;
return stream->mem;
}
static int zip_file_stream_revoke_memory_access(struct stream *stream) {
if(stream->mem) free(stream->mem);
return 0;
}
static int zip_file_stream_close(struct stream *stream) {
struct zip_file_stream *zip_file_stream = (struct zip_file_stream *)stream;
int r = zip_fclose(zip_file_stream->f);
stream->_errno = r;
return r;
}
#ifdef HAVE_GZIP
static ssize_t mem_stream_read_gz(struct stream *stream, void *ptr, size_t size) {
struct zip_file_stream *zip_file_stream = (struct zip_file_stream *)stream;
zip_file_stream->z_stream.avail_out = size;
zip_file_stream->z_stream.next_out = (Bytef *)ptr;
int ret = inflate(&zip_file_stream->z_stream, Z_SYNC_FLUSH);
int written = zip_file_stream->z_stream.total_out - zip_file_stream->z_position;
zip_file_stream->z_position = zip_file_stream->z_stream.total_out;
if(ret == Z_STREAM_END)
zip_file_stream->decompressed_data_len = zip_file_stream->z_position;
return written;
}
static ssize_t mem_stream_write_gz(struct stream *stream, const void *ptr, size_t size) {
(void)stream;
(void)ptr;
(void)size;
return 0;
}
static size_t mem_stream_seek_gz(struct stream *stream, long offset, int whence) {
(void)stream;
(void)offset;
(void)whence;
return 0;
}
static int mem_stream_eof_gz(struct stream *stream) {
(void)stream;
return 0;
}
static long mem_stream_tell_gz(struct stream *stream) {
(void)stream;
return -1;
}
static int mem_stream_vprintf_gz(struct stream *stream, const char *fmt, va_list ap) {
(void)stream;
(void)fmt;
(void)ap;
return 0;
}
static void *mem_stream_get_memory_access_gz(struct stream *stream, size_t *length) {
(void)stream;
(void)length;
return 0;
}
static int mem_stream_revoke_memory_access_gz(struct stream *stream) {
(void)stream;
return 1;
}
static int mem_stream_close_gz(struct stream *stream) {
struct zip_file_stream *zip_file_stream = (struct zip_file_stream *)stream;
inflateEnd(&zip_file_stream->z_stream);
free(zip_file_stream->z_data);
return 0;
}
static int check_gzip_data(uint8_t *data, size_t data_len, size_t *decompressed_data_len) {
if(data_len < 20) return 0;
if(data[0] != 0x1f) return 0;
if(data[1] != 0x8b) return 0;
if(decompressed_data_len) {
uint8_t *p = data + data_len - 4;
*decompressed_data_len = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
}
return 1;
}
#endif
int zip_file_stream_init_index(struct zip_file_stream *stream, zip_t *zip, int index, int stream_flags) {
stream_init(&stream->stream, stream_flags);
int r = zip_stat_index(zip, index, ZIP_STAT_SIZE, &stream->stat);
stream->stream._errno = zip_error_code_system(zip_get_error(zip));
if(r) return r;
stream->f = zip_fopen_index(zip, index, 0);
stream->stream._errno = zip_error_code_system(zip_get_error(zip));
if(!stream->f) return -1;
#ifdef HAVE_GZIP
if(stream_flags & STREAM_TRANSPARENT_GZIP) {
stream->z_data = malloc(stream->stat.size);
if(!stream->z_data) return -3;
zip_uint64_t bytes_read = zip_fread(stream->f, stream->z_data, stream->stat.size);
if(bytes_read != stream->stat.size) return -4;
if(check_gzip_data(stream->z_data, stream->stat.size, &stream->decompressed_data_len)) {
stream->z_stream.zalloc = 0;
stream->z_stream.zfree = 0;
stream->z_stream.opaque = 0;
stream->z_stream.avail_in = stream->stat.size;
stream->z_stream.next_in = (z_const Bytef *)stream->z_data;
stream->z_position = 0;
if(inflateInit2(&stream->z_stream, 0x20 | 15) != Z_OK)
return -5;
stream->stream.write = mem_stream_write_gz;
stream->stream.read = mem_stream_read_gz;
stream->stream.seek = mem_stream_seek_gz;
stream->stream.eof = mem_stream_eof_gz;
stream->stream.tell = mem_stream_tell_gz;
stream->stream.vprintf = mem_stream_vprintf_gz;
stream->stream.get_memory_access = mem_stream_get_memory_access_gz;
stream->stream.revoke_memory_access = mem_stream_revoke_memory_access_gz;
stream->stream.close = mem_stream_close_gz;
} else {
stream->stream.read = zip_file_stream_read;
stream->stream.write = zip_file_stream_write;
stream->stream.seek = zip_file_stream_seek;
stream->stream.eof = zip_file_stream_eof;
stream->stream.tell = zip_file_stream_tell;
stream->stream.vprintf = zip_file_stream_vprintf;
stream->stream.get_memory_access = zip_file_stream_get_memory_access;
stream->stream.revoke_memory_access = zip_file_stream_revoke_memory_access;
stream->stream.close = zip_file_stream_close;
}
} else {
#endif
stream->stream.read = zip_file_stream_read;
stream->stream.write = zip_file_stream_write;
stream->stream.seek = zip_file_stream_seek;
stream->stream.eof = zip_file_stream_eof;
stream->stream.tell = zip_file_stream_tell;
stream->stream.vprintf = zip_file_stream_vprintf;
stream->stream.get_memory_access = zip_file_stream_get_memory_access;
stream->stream.revoke_memory_access = zip_file_stream_revoke_memory_access;
stream->stream.close = zip_file_stream_close;
#ifdef HAVE_GZIP
}
#endif
return 0;
}
struct stream *zip_file_stream_create_index(zip_t *zip, int index, int stream_flags) {
struct zip_file_stream *s = malloc(sizeof(struct zip_file_stream));
if(!s) return 0;
int r = zip_file_stream_init_index(s, zip, index, stream_flags);
if(r) {
free(s);
return 0;
}
return (struct stream *)s;
}
#endif