forked from GreySyntax/partial-zip
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpartial.c
384 lines (323 loc) · 10.4 KB
/
partial.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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <curl/curl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <zlib.h>
#include <libgen.h>
#include "common.h"
#include "partial/partial.h"
static size_t dummyReceive(void* data, size_t size, size_t nmemb, void* info) {
return size * nmemb;
}
static size_t receiveCentralDirectoryEnd(void* data, size_t size, size_t nmemb, ZipInfo* info) {
memcpy(info->centralDirectoryEnd + info->centralDirectoryEndRecvd, data, size * nmemb);
info->centralDirectoryEndRecvd += size * nmemb;
return size * nmemb;
}
static size_t receiveCentralDirectory(void* data, size_t size, size_t nmemb, ZipInfo* info) {
memcpy(info->centralDirectory + info->centralDirectoryRecvd, data, size * nmemb);
info->centralDirectoryRecvd += size * nmemb;
return size * nmemb;
}
static size_t receiveData(void* data, size_t size, size_t nmemb, void** pFileData) {
memcpy(pFileData[0], data, size * nmemb);
pFileData[0] = ((char*)pFileData[0]) + (size * nmemb);
ZipInfo* info = ((ZipInfo*)pFileData[1]);
CDFile* file = ((CDFile*)pFileData[2]);
size_t* progress = ((size_t*)pFileData[3]);
if(progress) {
*progress += size * nmemb;
}
if(info && info->progressCallback && file) {
info->progressCallback(info, file, *progress);
}
return size * nmemb;
}
static void flipFiles(ZipInfo* info)
{
char* cur = info->centralDirectory;
unsigned int i;
for(i = 0; i < info->centralDirectoryDesc->CDEntries; i++)
{
CDFile* candidate = (CDFile*) cur;
FLIPENDIANLE(candidate->signature);
FLIPENDIANLE(candidate->version);
FLIPENDIANLE(candidate->versionExtract);
// FLIPENDIANLE(candidate->flags);
FLIPENDIANLE(candidate->method);
FLIPENDIANLE(candidate->modTime);
FLIPENDIANLE(candidate->modDate);
// FLIPENDIANLE(candidate->crc32);
FLIPENDIANLE(candidate->compressedSize);
FLIPENDIANLE(candidate->size);
FLIPENDIANLE(candidate->lenFileName);
FLIPENDIANLE(candidate->lenExtra);
FLIPENDIANLE(candidate->lenComment);
FLIPENDIANLE(candidate->diskStart);
// FLIPENDIANLE(candidate->internalAttr);
// FLIPENDIANLE(candidate->externalAttr);
FLIPENDIANLE(candidate->offset);
cur += sizeof(CDFile) + candidate->lenFileName + candidate->lenExtra + candidate->lenComment;
}
}
static void extractZipDateAndTime(CDFile* file, uint16_t *year, uint16_t *month, uint16_t *day, uint16_t *hour, uint16_t *minute, uint16_t *second)
{
struct dateFormat {
uint16_t day:5;
uint16_t month:4;
uint16_t year:7; // add 1980
};
union dateFormatUnion {
uint16_t value;
struct dateFormat alt;
};
union dateFormatUnion du = { .value = file->modDate };
struct dateFormat df = du.alt;
if(year) {
*year = df.year + 1980;
}
if(month) {
*month = df.month;
}
if(day) {
*day = df.day;
}
struct timeFormat {
uint16_t second:5; // multiply by 2
uint16_t minute:6;
uint16_t hour:5;
};
union timeFormatUnion {
uint16_t value;
struct timeFormat alt;
};
union timeFormatUnion tu = { .value = file->modTime };
struct timeFormat tf = tu.alt;
if(hour) {
*hour = tf.hour;
}
if(minute) {
*minute = tf.minute;
}
if(second) {
*second = tf.second * 2;
}
}
ZipInfo* PartialZipInit(const char* url)
{
ZipInfo* info = (ZipInfo*) malloc(sizeof(ZipInfo));
info->url = strdup(url);
info->centralDirectoryRecvd = 0;
info->centralDirectoryEndRecvd = 0;
info->centralDirectoryDesc = NULL;
info->progressCallback = NULL;
info->hCurl = curl_easy_init();
curl_easy_setopt(info->hCurl, CURLOPT_URL, info->url);
curl_easy_setopt(info->hCurl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(info->hCurl, CURLOPT_NOBODY, 1);
curl_easy_setopt(info->hCurl, CURLOPT_WRITEFUNCTION, dummyReceive);
if(strncmp(info->url, "file://", 7) == 0)
{
char* filePath = (char*) curl_easy_unescape(info->hCurl, info->url + 7, 0, NULL);
FILE* f = fopen(filePath, "rb");
if(!f)
{
curl_free(filePath);
curl_easy_cleanup(info->hCurl);
free(info->url);
free(info);
return NULL;
}
fseek(f, 0, SEEK_END);
info->length = ftell(f);
fclose(f);
curl_free(filePath);
}
else
{
curl_easy_perform(info->hCurl);
double dFileLength;
curl_easy_getinfo(info->hCurl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &dFileLength);
info->length = dFileLength;
}
char sRange[100];
uint64_t start;
if(info->length > (0xffff + sizeof(EndOfCD)))
start = info->length - 0xffff - sizeof(EndOfCD);
else
start = 0;
uint64_t end = info->length - 1;
sprintf(sRange, "%" PRIu64 "-%" PRIu64, start, end);
curl_easy_setopt(info->hCurl, CURLOPT_WRITEFUNCTION, receiveCentralDirectoryEnd);
curl_easy_setopt(info->hCurl, CURLOPT_WRITEDATA, info);
curl_easy_setopt(info->hCurl, CURLOPT_RANGE, sRange);
curl_easy_setopt(info->hCurl, CURLOPT_HTTPGET, 1);
curl_easy_perform(info->hCurl);
char* cur;
for(cur = info->centralDirectoryEnd; cur < (info->centralDirectoryEnd + (end - start - 1)); cur++)
{
EndOfCD* candidate = (EndOfCD*) cur;
uint32_t signature = candidate->signature;
FLIPENDIANLE(signature);
if(signature == 0x06054b50)
{
uint16_t lenComment = candidate->lenComment;
FLIPENDIANLE(lenComment);
if((cur + lenComment + sizeof(EndOfCD)) == (info->centralDirectoryEnd + info->centralDirectoryEndRecvd))
{
FLIPENDIANLE(candidate->diskNo);
FLIPENDIANLE(candidate->CDDiskNo);
FLIPENDIANLE(candidate->CDDiskEntries);
FLIPENDIANLE(candidate->CDEntries);
FLIPENDIANLE(candidate->CDSize);
FLIPENDIANLE(candidate->CDOffset);
FLIPENDIANLE(candidate->lenComment);
info->centralDirectoryDesc = candidate;
break;
}
}
}
if(info->centralDirectoryDesc)
{
info->centralDirectory = malloc(info->centralDirectoryDesc->CDSize);
start = info->centralDirectoryDesc->CDOffset;
end = start + info->centralDirectoryDesc->CDSize - 1;
sprintf(sRange, "%" PRIu64 "-%" PRIu64, start, end);
curl_easy_setopt(info->hCurl, CURLOPT_WRITEFUNCTION, receiveCentralDirectory);
curl_easy_setopt(info->hCurl, CURLOPT_WRITEDATA, info);
curl_easy_setopt(info->hCurl, CURLOPT_RANGE, sRange);
curl_easy_setopt(info->hCurl, CURLOPT_HTTPGET, 1);
curl_easy_perform(info->hCurl);
flipFiles(info);
return info;
}
else
{
curl_easy_cleanup(info->hCurl);
free(info->url);
free(info);
return NULL;
}
}
ZipInfo* PartialZipInitWithCallback(const char* url, PartialZipProgressCallback progressCallback)
{
ZipInfo* info = PartialZipInit(url);
if(info)
{
PartialZipSetProgressCallback(info, progressCallback);
}
return info;
}
CDFile* PartialZipFindFile(ZipInfo* info, const char* fileName)
{
char* cur = info->centralDirectory;
unsigned int i;
for(i = 0; i < info->centralDirectoryDesc->CDEntries; i++)
{
CDFile* candidate = (CDFile*) cur;
const char* curFileName = cur + sizeof(CDFile);
if(strlen(fileName) == candidate->lenFileName && strncmp(fileName, curFileName, candidate->lenFileName) == 0)
return candidate;
cur += sizeof(CDFile) + candidate->lenFileName + candidate->lenExtra + candidate->lenComment;
}
return NULL;
}
CDFile* PartialZipListFiles(ZipInfo* info)
{
printf("Archive: %s\n"
" Length Date Time Name\n"
"--------- ---------- ----- ----\n", info->url);
uint64_t total = 0;
char* cur = info->centralDirectory;
unsigned int i;
for(i = 0; i < info->centralDirectoryDesc->CDEntries; i++)
{
CDFile* candidate = (CDFile*) cur;
const char* curFileName = cur + sizeof(CDFile);
char* myFileName = (char*) malloc(candidate->lenFileName + 1);
memcpy(myFileName, curFileName, candidate->lenFileName);
myFileName[candidate->lenFileName] = '\0';
uint16_t year, month, day, hour, minute;
extractZipDateAndTime(candidate, &year, &month, &day, &hour, &minute, NULL);
printf(" %8u %02d-%02d-%04d %02d:%02d %s\n", candidate->size,
month, day, year, hour, minute, myFileName);
free(myFileName);
cur += sizeof(CDFile) + candidate->lenFileName + candidate->lenExtra + candidate->lenComment;
total += candidate->size;
}
printf("--------- -------\n"
"%llu %d files\n", total, i);
return NULL;
}
unsigned char* PartialZipGetFile(ZipInfo* info, CDFile* file)
{
LocalFile localHeader;
LocalFile* pLocalHeader = &localHeader;
uint64_t start = file->offset;
uint64_t end = file->offset + sizeof(LocalFile) - 1;
char sRange[100];
sprintf(sRange, "%" PRIu64 "-%" PRIu64, start, end);
void* pFileHeader[] = {pLocalHeader, NULL, NULL, NULL};
curl_easy_setopt(info->hCurl, CURLOPT_URL, info->url);
curl_easy_setopt(info->hCurl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(info->hCurl, CURLOPT_WRITEFUNCTION, receiveData);
curl_easy_setopt(info->hCurl, CURLOPT_WRITEDATA, &pFileHeader);
curl_easy_setopt(info->hCurl, CURLOPT_RANGE, sRange);
curl_easy_setopt(info->hCurl, CURLOPT_HTTPGET, 1);
curl_easy_perform(info->hCurl);
FLIPENDIANLE(localHeader.signature);
FLIPENDIANLE(localHeader.versionExtract);
// FLIPENDIANLE(localHeader.flags);
FLIPENDIANLE(localHeader.method);
FLIPENDIANLE(localHeader.modTime);
FLIPENDIANLE(localHeader.modDate);
// FLIPENDIANLE(localHeader.crc32);
FLIPENDIANLE(localHeader.compressedSize);
FLIPENDIANLE(localHeader.size);
FLIPENDIANLE(localHeader.lenFileName);
FLIPENDIANLE(localHeader.lenExtra);
unsigned char* fileData = (unsigned char*) malloc(file->compressedSize);
size_t progress = 0;
void* pFileData[] = {fileData, info, file, &progress};
start = file->offset + sizeof(LocalFile) + localHeader.lenFileName + localHeader.lenExtra;
end = start + file->compressedSize - 1;
sprintf(sRange, "%" PRIu64 "-%" PRIu64, start, end);
curl_easy_setopt(info->hCurl, CURLOPT_WRITEFUNCTION, receiveData);
curl_easy_setopt(info->hCurl, CURLOPT_WRITEDATA, pFileData);
curl_easy_setopt(info->hCurl, CURLOPT_RANGE, sRange);
curl_easy_setopt(info->hCurl, CURLOPT_HTTPGET, 1);
curl_easy_perform(info->hCurl);
if(file->method == 8)
{
unsigned char* uncData = (unsigned char*) malloc(file->size);
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = NULL;
inflateInit2(&strm, -MAX_WBITS);
strm.avail_in = file->compressedSize;
strm.next_in = fileData;
strm.avail_out = file->size;
strm.next_out = uncData;
inflate(&strm, Z_FINISH);
inflateEnd(&strm);
free(fileData);
fileData = uncData;
}
return fileData;
}
void PartialZipSetProgressCallback(ZipInfo* info, PartialZipProgressCallback progressCallback)
{
info->progressCallback = progressCallback;
}
void PartialZipRelease(ZipInfo* info)
{
curl_easy_cleanup(info->hCurl);
free(info->centralDirectory);
free(info->url);
free(info);
curl_global_cleanup();
}