Skip to content

Latest commit

 

History

History
1381 lines (1132 loc) · 37.7 KB

fileio.c

File metadata and controls

1381 lines (1132 loc) · 37.7 KB
 
May 12, 2007
May 12, 2007
1
2
3
4
5
6
7
8
/**
* MojoSetup; a portable, flexible installation application.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
Mar 25, 2006
Mar 25, 2006
10
#include "platform.h"
Mar 26, 2006
Mar 26, 2006
12
13
14
typedef MojoArchive* (*MojoArchiveCreateEntryPoint)(MojoInput *io);
MojoArchive *MojoArchive_createZIP(MojoInput *io);
May 4, 2007
May 4, 2007
15
MojoArchive *MojoArchive_createTAR(MojoInput *io);
May 10, 2009
May 10, 2009
16
MojoArchive *MojoArchive_createUZ2(MojoInput *io);
Mar 22, 2010
Mar 22, 2010
17
MojoArchive *MojoArchive_createPCK(MojoInput *io);
Mar 26, 2006
Mar 26, 2006
18
19
20
21
22
typedef struct
{
const char *ext;
MojoArchiveCreateEntryPoint create;
May 9, 2009
May 9, 2009
23
boolean hasMagic; // can determine file type from contents?
Mar 26, 2006
Mar 26, 2006
24
25
} MojoArchiveType;
Nov 15, 2010
Nov 15, 2010
26
27
// !!! FIXME: order by archiver, not extension, since most of this is
// !!! FIXME: duplicates for .tar
Mar 26, 2006
Mar 26, 2006
28
29
static const MojoArchiveType archives[] =
{
May 9, 2009
May 9, 2009
30
31
32
{ "zip", MojoArchive_createZIP, true },
{ "tar", MojoArchive_createTAR, true },
{ "tar.gz", MojoArchive_createTAR, true },
Nov 15, 2010
Nov 15, 2010
33
{ "tar.xz", MojoArchive_createTAR, true },
May 9, 2009
May 9, 2009
34
35
36
37
38
{ "tar.bz2", MojoArchive_createTAR, true },
{ "tgz", MojoArchive_createTAR, true },
{ "tbz2", MojoArchive_createTAR, true },
{ "tb2", MojoArchive_createTAR, true },
{ "tbz", MojoArchive_createTAR, true },
Nov 15, 2010
Nov 15, 2010
39
{ "txz", MojoArchive_createTAR, true },
May 10, 2009
May 10, 2009
40
{ "uz2", MojoArchive_createUZ2, false },
Mar 22, 2010
Mar 22, 2010
41
{ "pck", MojoArchive_createPCK, true },
Mar 26, 2006
Mar 26, 2006
42
43
};
Feb 28, 2010
Feb 28, 2010
44
45
46
#if SUPPORT_GZIP
Mar 28, 2010
Mar 28, 2010
47
#include "zlib/zlib.h"
Feb 28, 2010
Feb 28, 2010
48
49
50
51
52
53
54
55
56
#define GZIP_READBUFSIZE (128 * 1024)
static MojoInput *make_gzip_input(MojoInput *origio);
typedef struct GZIPinfo
{
MojoInput *origio;
uint64 uncompressed_position;
Nov 15, 2010
Nov 15, 2010
57
uint8 buffer[GZIP_READBUFSIZE];
Feb 28, 2010
Feb 28, 2010
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
z_stream stream;
} GZIPinfo;
static voidpf mojoZlibAlloc(voidpf opaque, uInt items, uInt size)
{
return xmalloc(items * size);
} // mojoZlibAlloc
static void mojoZlibFree(voidpf opaque, voidpf address)
{
free(address);
} // mojoZlibFree
static void initializeZStream(z_stream *pstr)
{
memset(pstr, '\0', sizeof (z_stream));
pstr->zalloc = mojoZlibAlloc;
pstr->zfree = mojoZlibFree;
} // initializeZStream
static boolean MojoInput_gzip_ready(MojoInput *io)
{
return true; // !!! FIXME: ready if there are bytes uncompressed.
} // MojoInput_gzip_ready
static boolean MojoInput_gzip_seek(MojoInput *io, uint64 offset)
{
// This is all really expensive.
GZIPinfo *info = (GZIPinfo *) io->opaque;
/*
* If seeking backwards, we need to redecode the file
* from the start and throw away the compressed bits until we hit
* the offset we need. If seeking forward, we still need to
* decode, but we don't rewind first.
*/
if (offset < info->uncompressed_position)
{
if (!info->origio->seek(info->origio, 0))
return false;
inflateEnd(&info->stream);
initializeZStream(&info->stream);
if (inflateInit2(&info->stream, 31) != Z_OK)
return false;
info->uncompressed_position = 0;
} // if
while (info->uncompressed_position != offset)
{
uint8 buf[512];
uint32 maxread;
int64 br;
maxread = (uint32) (offset - info->uncompressed_position);
if (maxread > sizeof (buf))
maxread = sizeof (buf);
br = io->read(io, buf, maxread);
if (br != maxread)
return false;
} /* while */
return true;
} // MojoInput_gzip_seek
static int64 MojoInput_gzip_tell(MojoInput *io)
{
return (((GZIPinfo *) io->opaque)->uncompressed_position);
} // MojoInput_gzip_tell
static int64 MojoInput_gzip_length(MojoInput *io)
{
return -1;
} // MojoInput_gzip_length
static int64 MojoInput_gzip_read(MojoInput *io, void *buf, uint32 bufsize)
{
GZIPinfo *info = (GZIPinfo *) io->opaque;
MojoInput *origio = info->origio;
int64 retval = 0;
if (bufsize == 0)
return 0; // quick rejection.
Nov 15, 2010
Nov 15, 2010
141
142
143
144
145
info->stream.next_out = buf;
info->stream.avail_out = bufsize;
while (retval < ((int64) bufsize))
Feb 28, 2010
Feb 28, 2010
146
{
Nov 15, 2010
Nov 15, 2010
147
148
const uint32 before = info->stream.total_out;
int rc;
Feb 28, 2010
Feb 28, 2010
149
Nov 15, 2010
Nov 15, 2010
150
if (info->stream.avail_in == 0)
Feb 28, 2010
Feb 28, 2010
151
{
Nov 15, 2010
Nov 15, 2010
152
153
int64 br = origio->length(origio) - origio->tell(origio);
if (br > 0)
Feb 28, 2010
Feb 28, 2010
154
{
Nov 15, 2010
Nov 15, 2010
155
156
if (br > GZIP_READBUFSIZE)
br = GZIP_READBUFSIZE;
Feb 28, 2010
Feb 28, 2010
157
Nov 15, 2010
Nov 15, 2010
158
159
160
br = origio->read(origio, info->buffer, (uint32) br);
if (br <= 0)
return -1;
Feb 28, 2010
Feb 28, 2010
161
Nov 15, 2010
Nov 15, 2010
162
163
info->stream.next_in = info->buffer;
info->stream.avail_in = (uint32) br;
Feb 28, 2010
Feb 28, 2010
164
} // if
Nov 15, 2010
Nov 15, 2010
165
} // if
Feb 28, 2010
Feb 28, 2010
166
Nov 15, 2010
Nov 15, 2010
167
168
rc = inflate(&info->stream, Z_SYNC_FLUSH);
retval += (info->stream.total_out - before);
Feb 28, 2010
Feb 28, 2010
169
Nov 15, 2010
Nov 15, 2010
170
171
172
if (rc != Z_OK) // !!! FIXME: Z_STREAM_END?
return -1;
} // while
Feb 28, 2010
Feb 28, 2010
173
Nov 15, 2010
Nov 15, 2010
174
175
assert(retval >= 0);
info->uncompressed_position += (uint32) retval;
Feb 28, 2010
Feb 28, 2010
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
return retval;
} // MojoInput_gzip_read
static MojoInput* MojoInput_gzip_duplicate(MojoInput *io)
{
GZIPinfo *info = (GZIPinfo *) io->opaque;
MojoInput *retval = NULL;
MojoInput *newio = info->origio->duplicate(info->origio);
if (newio != NULL)
{
retval = make_gzip_input(newio);
if (retval != NULL)
retval->seek(retval, io->tell(io)); // slow, slow, slow...
} // if
return retval;
} // MojoInput_gzip_duplicate
static void MojoInput_gzip_close(MojoInput *io)
{
GZIPinfo *info = (GZIPinfo *) io->opaque;
if (info->origio != NULL)
info->origio->close(info->origio);
inflateEnd(&info->stream);
free(info);
free(io);
} // MojoInput_gzip_close
static MojoInput *make_gzip_input(MojoInput *origio)
{
MojoInput *io = NULL;
GZIPinfo *info = (GZIPinfo *) xmalloc(sizeof (GZIPinfo));
initializeZStream(&info->stream);
if (inflateInit2(&info->stream, 31) != Z_OK)
{
free(info);
return NULL;
} // if
info->origio = origio;
io = (MojoInput *) xmalloc(sizeof (MojoInput));
io->ready = MojoInput_gzip_ready;
io->read = MojoInput_gzip_read;
io->seek = MojoInput_gzip_seek;
io->tell = MojoInput_gzip_tell;
io->length = MojoInput_gzip_length;
io->duplicate = MojoInput_gzip_duplicate;
io->close = MojoInput_gzip_close;
io->opaque = info;
return io;
} // make_gzip_input
#endif // SUPPORT_GZIP
#if SUPPORT_BZIP2
Mar 28, 2010
Mar 28, 2010
235
#include "bzip2/bzlib.h"
Feb 28, 2010
Feb 28, 2010
236
237
238
239
240
241
242
243
244
#define BZIP2_READBUFSIZE (128 * 1024)
static MojoInput *make_bzip2_input(MojoInput *origio);
typedef struct BZIP2info
{
MojoInput *origio;
uint64 uncompressed_position;
Nov 15, 2010
Nov 15, 2010
245
uint8 buffer[BZIP2_READBUFSIZE];
Feb 28, 2010
Feb 28, 2010
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
bz_stream stream;
} BZIP2info;
static void *mojoBzlib2Alloc(void *opaque, int items, int size)
{
return xmalloc(items * size);
} // mojoBzlib2Alloc
static void mojoBzlib2Free(void *opaque, void *address)
{
free(address);
} // mojoBzlib2Free
static void initializeBZ2Stream(bz_stream *pstr)
{
memset(pstr, '\0', sizeof (bz_stream));
pstr->bzalloc = mojoBzlib2Alloc;
pstr->bzfree = mojoBzlib2Free;
} // initializeBZ2Stream
static boolean MojoInput_bzip2_ready(MojoInput *io)
{
return true; // !!! FIXME: ready if there are bytes uncompressed.
} // MojoInput_bzip2_ready
static boolean MojoInput_bzip2_seek(MojoInput *io, uint64 offset)
{
// This is all really expensive.
BZIP2info *info = (BZIP2info *) io->opaque;
/*
* If seeking backwards, we need to redecode the file
* from the start and throw away the compressed bits until we hit
* the offset we need. If seeking forward, we still need to
* decode, but we don't rewind first.
*/
if (offset < info->uncompressed_position)
{
#if 0
/* we do a copy so state is sane if inflateInit2() fails. */
bz_stream str;
initializeBZ2Stream(&str);
if (BZ2_bzDecompressInit(&str, 0, 0) != BZ_OK)
return false;
if (!info->origio->seek(info->origio, 0))
return false; // !!! FIXME: leaking (str)?
BZ2_bzDecompressEnd(&info->stream);
memcpy(&info->stream, &str, sizeof (bz_stream));
#endif
if (!info->origio->seek(info->origio, 0))
return false;
BZ2_bzDecompressEnd(&info->stream);
initializeBZ2Stream(&info->stream);
if (BZ2_bzDecompressInit(&info->stream, 0, 0) != BZ_OK)
return false;
info->uncompressed_position = 0;
} // if
while (info->uncompressed_position != offset)
{
uint8 buf[512];
uint32 maxread;
int64 br;
maxread = (uint32) (offset - info->uncompressed_position);
if (maxread > sizeof (buf))
maxread = sizeof (buf);
br = io->read(io, buf, maxread);
if (br != maxread)
return false;
} /* while */
return true;
} // MojoInput_bzip2_seek
static int64 MojoInput_bzip2_tell(MojoInput *io)
{
return (((BZIP2info *) io->opaque)->uncompressed_position);
} // MojoInput_bzip2_tell
static int64 MojoInput_bzip2_length(MojoInput *io)
{
return -1;
} // MojoInput_bzip2_length
static int64 MojoInput_bzip2_read(MojoInput *io, void *buf, uint32 bufsize)
{
BZIP2info *info = (BZIP2info *) io->opaque;
MojoInput *origio = info->origio;
int64 retval = 0;
if (bufsize == 0)
return 0; // quick rejection.
Nov 15, 2010
Nov 15, 2010
343
344
345
346
347
info->stream.next_out = buf;
info->stream.avail_out = bufsize;
while (retval < ((int64) bufsize))
Feb 28, 2010
Feb 28, 2010
348
{
Nov 15, 2010
Nov 15, 2010
349
350
const uint32 before = info->stream.total_out_lo32;
int rc;
Feb 28, 2010
Feb 28, 2010
351
Nov 15, 2010
Nov 15, 2010
352
if (info->stream.avail_in == 0)
Feb 28, 2010
Feb 28, 2010
353
{
Nov 15, 2010
Nov 15, 2010
354
355
int64 br = origio->length(origio) - origio->tell(origio);
if (br > 0)
Feb 28, 2010
Feb 28, 2010
356
{
Nov 15, 2010
Nov 15, 2010
357
358
if (br > BZIP2_READBUFSIZE)
br = BZIP2_READBUFSIZE;
Feb 28, 2010
Feb 28, 2010
359
Nov 15, 2010
Nov 15, 2010
360
361
362
br = origio->read(origio, info->buffer, (uint32) br);
if (br <= 0)
return -1;
Feb 28, 2010
Feb 28, 2010
363
Nov 15, 2010
Nov 15, 2010
364
365
info->stream.next_in = (char *) info->buffer;
info->stream.avail_in = (uint32) br;
Feb 28, 2010
Feb 28, 2010
366
} // if
Nov 15, 2010
Nov 15, 2010
367
} // if
Feb 28, 2010
Feb 28, 2010
368
Nov 15, 2010
Nov 15, 2010
369
370
371
372
373
rc = BZ2_bzDecompress(&info->stream);
retval += (info->stream.total_out_lo32 - before);
if (rc != BZ_OK)
return -1;
} // while
Feb 28, 2010
Feb 28, 2010
374
Nov 15, 2010
Nov 15, 2010
375
376
assert(retval >= 0);
info->uncompressed_position += (uint32) retval;
Feb 28, 2010
Feb 28, 2010
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
return retval;
} // MojoInput_bzip2_read
static MojoInput* MojoInput_bzip2_duplicate(MojoInput *io)
{
BZIP2info *info = (BZIP2info *) io->opaque;
MojoInput *retval = NULL;
MojoInput *newio = info->origio->duplicate(info->origio);
if (newio != NULL)
{
retval = make_bzip2_input(newio);
if (retval != NULL)
retval->seek(retval, io->tell(io)); // slow, slow, slow...
} // if
return retval;
} // MojoInput_bzip2_duplicate
static void MojoInput_bzip2_close(MojoInput *io)
{
BZIP2info *info = (BZIP2info *) io->opaque;
if (info->origio != NULL)
info->origio->close(info->origio);
BZ2_bzDecompressEnd(&info->stream);
free(info);
free(io);
} // MojoInput_bzip2_close
static MojoInput *make_bzip2_input(MojoInput *origio)
{
MojoInput *io = NULL;
BZIP2info *info = (BZIP2info *) xmalloc(sizeof (BZIP2info));
initializeBZ2Stream(&info->stream);
if (BZ2_bzDecompressInit(&info->stream, 0, 0) != BZ_OK)
{
free(info);
return NULL;
} // if
info->origio = origio;
io = (MojoInput *) xmalloc(sizeof (MojoInput));
io->ready = MojoInput_bzip2_ready;
io->read = MojoInput_bzip2_read;
io->seek = MojoInput_bzip2_seek;
io->tell = MojoInput_bzip2_tell;
io->length = MojoInput_bzip2_length;
io->duplicate = MojoInput_bzip2_duplicate;
io->close = MojoInput_bzip2_close;
io->opaque = info;
return io;
} // make_bzip2_input
#endif // SUPPORT_BZIP2
Nov 15, 2010
Nov 15, 2010
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#if SUPPORT_XZ
#include "lzma.h"
#define XZ_READBUFSIZE (128 * 1024)
static MojoInput *make_xz_input(MojoInput *origio);
typedef struct XZinfo
{
MojoInput *origio;
uint64 uncompressed_position;
uint8 buffer[XZ_READBUFSIZE];
lzma_stream stream;
} XZinfo;
static void *mojoLzmaAlloc(void *opaque, size_t items, size_t size)
{
return xmalloc(items * size);
} // mojoLzmaAlloc
static void mojoLzmaFree(void *opaque, void *address)
{
free(address);
} // mojoZlibFree
static void initializeXZStream(lzma_stream *pstr)
{
static lzma_allocator lzmaAlloc = { mojoLzmaAlloc, mojoLzmaFree };
memset(pstr, '\0', sizeof (lzma_stream));
pstr->allocator = &lzmaAlloc;
} // initializeXZStream
static boolean MojoInput_xz_ready(MojoInput *io)
{
return true; // !!! FIXME: ready if there are bytes uncompressed.
} // MojoInput_xz_ready
static boolean MojoInput_xz_seek(MojoInput *io, uint64 offset)
{
// This is all really expensive.
XZinfo *info = (XZinfo *) io->opaque;
/*
* If seeking backwards, we need to redecode the file
* from the start and throw away the compressed bits until we hit
* the offset we need. If seeking forward, we still need to
* decode, but we don't rewind first.
*/
if (offset < info->uncompressed_position)
{
lzma_stream *strm = &info->stream;
if (!info->origio->seek(info->origio, 0))
return false;
lzma_end(strm);
initializeXZStream(strm);
if (lzma_stream_decoder(strm, UINT64_MAX, LZMA_CONCATENATED) != LZMA_OK)
return false;
info->uncompressed_position = 0;
} // if
while (info->uncompressed_position != offset)
{
uint8 buf[512];
uint32 maxread;
int64 br;
maxread = (uint32) (offset - info->uncompressed_position);
if (maxread > sizeof (buf))
maxread = sizeof (buf);
br = io->read(io, buf, maxread);
if (br != maxread)
return false;
} /* while */
return true;
} // MojoInput_xz_seek
static int64 MojoInput_xz_tell(MojoInput *io)
{
return (((XZinfo *) io->opaque)->uncompressed_position);
} // MojoInput_xz_tell
static int64 MojoInput_xz_length(MojoInput *io)
{
return -1;
} // MojoInput_xz_length
static int64 MojoInput_xz_read(MojoInput *io, void *buf, uint32 bufsize)
{
XZinfo *info = (XZinfo *) io->opaque;
MojoInput *origio = info->origio;
int64 retval = 0;
if (bufsize == 0)
return 0; // quick rejection.
info->stream.next_out = buf;
info->stream.avail_out = bufsize;
while (retval < ((int64) bufsize))
{
const uint32 before = info->stream.total_out;
lzma_ret rc;
lzma_action action = LZMA_FINISH;
if (info->stream.avail_in == 0)
{
int64 br = origio->length(origio) - origio->tell(origio);
if (br > 0)
{
action = LZMA_RUN;
if (br > XZ_READBUFSIZE)
br = XZ_READBUFSIZE;
br = origio->read(origio, info->buffer, (uint32) br);
if (br <= 0)
return -1;
info->stream.next_in = info->buffer;
info->stream.avail_in = (uint32) br;
} // if
} // if
rc = lzma_code(&info->stream, LZMA_RUN);
retval += (info->stream.total_out - before);
if (rc != LZMA_OK)
return -1;
} // while
assert(retval >= 0);
info->uncompressed_position += (uint32) retval;
return retval;
} // MojoInput_xz_read
static MojoInput* MojoInput_xz_duplicate(MojoInput *io)
{
XZinfo *info = (XZinfo *) io->opaque;
MojoInput *retval = NULL;
MojoInput *newio = info->origio->duplicate(info->origio);
if (newio != NULL)
{
retval = make_xz_input(newio);
if (retval != NULL)
retval->seek(retval, io->tell(io)); // slow, slow, slow...
} // if
return retval;
} // MojoInput_xz_duplicate
static void MojoInput_xz_close(MojoInput *io)
{
XZinfo *info = (XZinfo *) io->opaque;
if (info->origio != NULL)
info->origio->close(info->origio);
lzma_end(&info->stream);
free(info);
free(io);
} // MojoInput_xz_close
static MojoInput *make_xz_input(MojoInput *origio)
{
MojoInput *io = NULL;
XZinfo *info = (XZinfo *) xmalloc(sizeof (XZinfo));
lzma_stream *strm = &info->stream;
// UINT64_MAX is the memory usage limit (so basically, no artificial
// limit here). Internally, this holds its entire dictionary in
// RAM (8 megabytes by default), plus a few other structures, so we
// shouldn't eat tons of memory in the normal case. Note that the
// dictionary can max out at _four gigabytes_, but obviously no one does
// that.
initializeXZStream(strm);
if (lzma_stream_decoder(strm, UINT64_MAX, LZMA_CONCATENATED) != LZMA_OK)
{
free(info);
return NULL;
} // if
info->origio = origio;
io = (MojoInput *) xmalloc(sizeof (MojoInput));
io->ready = MojoInput_xz_ready;
io->read = MojoInput_xz_read;
io->seek = MojoInput_xz_seek;
io->tell = MojoInput_xz_tell;
io->length = MojoInput_xz_length;
io->duplicate = MojoInput_xz_duplicate;
io->close = MojoInput_xz_close;
io->opaque = info;
return io;
} // make_xz_input
#endif // SUPPORT_XZ
Feb 28, 2010
Feb 28, 2010
632
633
MojoInput *MojoInput_newCompressedStream(MojoInput *origio)
{
Nov 15, 2010
Nov 15, 2010
634
#if SUPPORT_GZIP || SUPPORT_BZIP2 || SUPPORT_XZ
Feb 28, 2010
Feb 28, 2010
635
636
637
// Look at the first piece of the file to decide if it is compressed
// by a general compression algorithm, and if so, wrap the MojoInput
// in a decompressor.
Nov 15, 2010
Nov 15, 2010
638
uint8 magic[6];
Feb 28, 2010
Feb 28, 2010
639
const int64 br = origio->read(origio, magic, sizeof (magic));
Mar 17, 2010
Mar 17, 2010
640
if ((origio->seek(origio, 0)) && (br == sizeof (magic)))
Feb 28, 2010
Feb 28, 2010
641
642
{
#if SUPPORT_GZIP
Nov 15, 2010
Nov 15, 2010
643
644
645
646
647
{
static const uint8 gzip_sig[] = { 0x1F, 0x8B, 0x08 };
if (memcmp(magic, gzip_sig, sizeof (gzip_sig)) == 0)
return make_gzip_input(origio);
}
Feb 28, 2010
Feb 28, 2010
648
649
650
#endif
#if SUPPORT_BZIP2
Nov 15, 2010
Nov 15, 2010
651
652
653
654
655
656
657
658
659
660
661
662
663
{
static const uint8 bzip2_sig[] = { 0x42, 0x5A };
if (memcmp(magic, bzip2_sig, sizeof (bzip2_sig)) == 0)
return make_bzip2_input(origio);
}
#endif
#if SUPPORT_XZ
{
static const uint8 xz_sig[] = { 0xFD, '7', 'z', 'X', 'Z', 0x00 };
if (memcmp(magic, xz_sig, sizeof (xz_sig)) == 0)
return make_xz_input(origio);
}
Feb 28, 2010
Feb 28, 2010
664
665
666
667
668
669
670
671
672
#endif
} // if
#endif
return NULL;
} // MojoInput_newCompressedStream
MojoArchive *MojoArchive_newFromInput(MojoInput *_io, const char *origfname)
Mar 26, 2006
Mar 26, 2006
673
674
675
{
int i;
MojoArchive *retval = NULL;
May 5, 2007
May 5, 2007
676
const char *ext = NULL;
Feb 28, 2010
Feb 28, 2010
677
678
679
MojoInput *io = MojoInput_newCompressedStream(_io);
if (io == NULL)
io = _io;
May 5, 2007
May 5, 2007
680
681
682
if (origfname != NULL)
{
May 9, 2009
May 9, 2009
683
ext = strrchr(origfname, '/');
May 5, 2007
May 5, 2007
684
685
686
687
688
689
if (ext == NULL)
ext = strchr(origfname, '.');
else
ext = strchr(ext+1, '.');
} // if
May 9, 2009
May 9, 2009
690
while (ext != NULL)
Mar 26, 2006
Mar 26, 2006
691
{
May 9, 2009
May 9, 2009
692
// Try for an exact match by filename extension.
May 5, 2007
May 5, 2007
693
ext++; // skip that '.'
Mar 26, 2006
Mar 26, 2006
694
695
for (i = 0; i < STATICARRAYLEN(archives); i++)
{
May 9, 2009
May 9, 2009
696
697
698
const MojoArchiveType *arc = &archives[i];
if (strcasecmp(ext, arc->ext) == 0)
return arc->create(io);
Mar 26, 2006
Mar 26, 2006
699
} // for
May 9, 2009
May 9, 2009
700
701
ext = strchr(ext, '.');
} // while
Mar 26, 2006
Mar 26, 2006
702
May 9, 2009
May 9, 2009
703
// Try any that could be determined without the file extension...
Mar 26, 2006
Mar 26, 2006
704
705
for (i = 0; i < STATICARRAYLEN(archives); i++)
{
May 9, 2009
May 9, 2009
706
707
const MojoArchiveType *arc = &archives[i];
if ((arc->hasMagic) && ((retval = arc->create(io)) != NULL))
Mar 26, 2006
Mar 26, 2006
708
709
710
711
712
713
714
715
return retval;
} // for
io->close(io);
return NULL; // nothing can handle this data.
} // MojoArchive_newFromInput
May 4, 2007
May 4, 2007
716
void MojoArchive_resetEntry(MojoArchiveEntry *info)
Mar 26, 2006
Mar 26, 2006
717
718
{
free(info->filename);
May 1, 2007
May 1, 2007
719
free(info->linkdest);
Apr 29, 2007
Apr 29, 2007
720
721
memset(info, '\0', sizeof (MojoArchiveEntry));
} // MojoArchive_resetEntry
Mar 26, 2006
Mar 26, 2006
722
723
May 1, 2007
May 1, 2007
724
725
// !!! FIXME: I'd rather not use a callback here, but I can't see a cleaner
// !!! FIXME: way right now...
May 3, 2007
May 3, 2007
726
boolean MojoInput_toPhysicalFile(MojoInput *in, const char *fname, uint16 perms,
Jan 16, 2008
Jan 16, 2008
727
MojoChecksums *checksums, int64 maxbytes,
May 1, 2007
May 1, 2007
728
MojoInput_FileCopyCallback cb, void *data)
May 7, 2007
May 7, 2007
730
731
boolean retval = false;
uint32 start = MojoPlatform_ticks();
Sep 20, 2007
Sep 20, 2007
732
void *out = NULL;
733
boolean iofailure = false;
May 1, 2007
May 1, 2007
734
735
int64 flen = 0;
int64 bw = 0;
May 27, 2007
May 27, 2007
736
MojoChecksumContext sumctx;
737
738
739
740
if (in == NULL)
return false;
May 27, 2007
May 27, 2007
741
742
743
744
745
746
if (checksums != NULL)
{
memset(checksums, '\0', sizeof (MojoChecksums));
MojoChecksum_init(&sumctx);
} // if
May 12, 2007
May 12, 2007
747
748
749
750
751
752
// Wait for a ready(), so length() can be meaningful on network streams.
while ((!in->ready(in)) && (!iofailure))
{
MojoPlatform_sleep(100);
if (cb != NULL)
{
May 17, 2007
May 17, 2007
753
if (!cb(MojoPlatform_ticks() - start, 0, 0, -1, data))
May 12, 2007
May 12, 2007
754
755
756
757
iofailure = true;
} // if
} // while
May 1, 2007
May 1, 2007
758
flen = in->length(in);
Jan 16, 2008
Jan 16, 2008
759
760
if ((maxbytes >= 0) && (flen > maxbytes))
flen = maxbytes;
May 1, 2007
May 1, 2007
761
Dec 9, 2006
Dec 9, 2006
762
MojoPlatform_unlink(fname);
May 12, 2007
May 12, 2007
763
if (!iofailure)
Sep 20, 2007
Sep 20, 2007
764
765
766
767
768
{
const uint32 flags = MOJOFILE_WRITE|MOJOFILE_CREATE|MOJOFILE_TRUNCATE;
const uint16 mode = MojoPlatform_defaultFilePerms();
out = MojoPlatform_open(fname, flags, mode);
} // if
May 12, 2007
May 12, 2007
769
May 7, 2007
May 7, 2007
770
if (out != NULL)
May 7, 2007
May 7, 2007
772
while (!iofailure)
May 8, 2007
May 8, 2007
774
int64 br = 0;
Jan 16, 2008
Jan 16, 2008
775
776
777
778
779
780
781
782
783
784
785
786
787
int64 maxread = sizeof (scratchbuf_128k);
// see if we need to clamp to eof or maxbytes...
if (flen >= 0)
{
const int64 avail = flen - bw;
if (avail < maxread)
{
maxread = avail;
if (maxread == 0)
break; // nothing left to do, break out.
} // if
} // if
May 8, 2007
May 8, 2007
788
May 7, 2007
May 7, 2007
789
790
791
// If there's a callback, then poll. Otherwise, just block on
// the reads from the MojoInput.
if ((cb != NULL) && (!in->ready(in)))
May 7, 2007
May 7, 2007
792
793
MojoPlatform_sleep(100);
else
May 1, 2007
May 1, 2007
794
{
Feb 2, 2008
Feb 2, 2008
795
br = in->read(in, scratchbuf_128k, (uint32) maxread);
May 7, 2007
May 7, 2007
796
797
798
799
800
if (br == 0) // we're done!
break;
else if (br < 0)
iofailure = true;
else
May 1, 2007
May 1, 2007
801
{
Sep 25, 2007
Sep 25, 2007
802
if (MojoPlatform_write(out, scratchbuf_128k, (uint32) br) != br)
May 1, 2007
May 1, 2007
803
iofailure = true;
May 7, 2007
May 7, 2007
804
else
May 27, 2007
May 27, 2007
805
806
{
if (checksums != NULL)
Sep 25, 2007
Sep 25, 2007
807
MojoChecksum_append(&sumctx, scratchbuf_128k, (uint32) br);
May 7, 2007
May 7, 2007
808
bw += br;
May 27, 2007
May 27, 2007
809
} // else
May 7, 2007
May 7, 2007
810
} // else
May 1, 2007
May 1, 2007
811
} // else
May 7, 2007
May 7, 2007
813
814
if (cb != NULL)
{
May 8, 2007
May 8, 2007
815
if (!cb(MojoPlatform_ticks() - start, br, bw, flen, data))
May 7, 2007
May 7, 2007
816
817
818
819
iofailure = true;
} // if
} // while
Sep 20, 2007
Sep 20, 2007
820
if (MojoPlatform_close(out) != 0)
May 7, 2007
May 7, 2007
821
iofailure = true;
Jan 16, 2008
Jan 16, 2008
822
823
else if (bw != flen)
iofailure = true;
May 1, 2007
May 1, 2007
824
May 7, 2007
May 7, 2007
825
826
827
828
829
if (iofailure)
MojoPlatform_unlink(fname);
else
{
MojoPlatform_chmod(fname, perms);
May 27, 2007
May 27, 2007
830
831
if (checksums != NULL)
MojoChecksum_finish(&sumctx, checksums);
May 7, 2007
May 7, 2007
832
833
retval = true;
} // else
May 7, 2007
May 7, 2007
836
837
in->close(in);
return retval;
Dec 9, 2006
Dec 9, 2006
838
} // MojoInput_toPhysicalFile
Mar 25, 2006
Mar 25, 2006
840
Dec 3, 2006
Dec 3, 2006
841
842
843
MojoInput *MojoInput_newFromArchivePath(MojoArchive *ar, const char *fname)
{
MojoInput *retval = NULL;
May 4, 2007
May 4, 2007
844
if (ar->enumerate(ar))
Dec 3, 2006
Dec 3, 2006
845
{
Apr 29, 2007
Apr 29, 2007
846
const MojoArchiveEntry *entinfo;
Dec 3, 2006
Dec 3, 2006
847
848
while ((entinfo = ar->enumNext(ar)) != NULL)
{
May 4, 2007
May 4, 2007
849
if (strcmp(entinfo->filename, fname) == 0)
Dec 3, 2006
Dec 3, 2006
850
851
852
853
854
855
856
857
858
859
860
861
{
if (entinfo->type == MOJOARCHIVE_ENTRY_FILE)
retval = ar->openCurrentEntry(ar);
break;
} // if
} // while
} // if
return retval;
} // MojoInput_newFromArchivePath
Mar 26, 2006
Mar 26, 2006
862
Mar 25, 2006
Mar 25, 2006
863
864
// MojoInputs from files on the OS filesystem.
Mar 26, 2006
Mar 26, 2006
865
866
typedef struct
{
Sep 20, 2007
Sep 20, 2007
867
void *handle;
Mar 26, 2006
Mar 26, 2006
868
869
870
char *path;
} MojoInputFileInstance;
May 7, 2007
May 7, 2007
871
872
873
874
875
876
static boolean MojoInput_file_ready(MojoInput *io)
{
// !!! FIXME: select()? Does that help with network filesystems?
return true;
} // MojoInput_file_ready
Mar 26, 2006
Mar 26, 2006
877
static int64 MojoInput_file_read(MojoInput *io, void *buf, uint32 bufsize)
Mar 25, 2006
Mar 25, 2006
878
{
Mar 26, 2006
Mar 26, 2006
879
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
Sep 20, 2007
Sep 20, 2007
880
return MojoPlatform_read(inst->handle, buf, bufsize);
Mar 25, 2006
Mar 25, 2006
881
882
883
884
} // MojoInput_file_read
static boolean MojoInput_file_seek(MojoInput *io, uint64 pos)
{
Mar 26, 2006
Mar 26, 2006
885
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
Sep 20, 2007
Sep 20, 2007
886
return (MojoPlatform_seek(inst->handle, pos, MOJOSEEK_SET) == pos);
Mar 25, 2006
Mar 25, 2006
887
888
} // MojoInput_file_seek
Mar 26, 2006
Mar 26, 2006
889
static int64 MojoInput_file_tell(MojoInput *io)
Mar 25, 2006
Mar 25, 2006
890
{
Mar 26, 2006
Mar 26, 2006
891
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
Sep 20, 2007
Sep 20, 2007
892
return MojoPlatform_tell(inst->handle);
Mar 25, 2006
Mar 25, 2006
893
894
} // MojoInput_file_tell
Mar 26, 2006
Mar 26, 2006
895
896
897
static int64 MojoInput_file_length(MojoInput *io)
{
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
Sep 20, 2007
Sep 20, 2007
898
return MojoPlatform_flen(inst->handle);
Mar 26, 2006
Mar 26, 2006
899
900
901
902
903
904
905
906
} // MojoInput_file_length
static MojoInput *MojoInput_file_duplicate(MojoInput *io)
{
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
return MojoInput_newFromFile(inst->path);
} // MojoInput_file_duplicate
Mar 25, 2006
Mar 25, 2006
907
908
static void MojoInput_file_close(MojoInput *io)
{
Mar 26, 2006
Mar 26, 2006
909
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
Sep 20, 2007
Sep 20, 2007
910
MojoPlatform_close(inst->handle);
Mar 26, 2006
Mar 26, 2006
911
912
free(inst->path);
free(inst);
Mar 25, 2006
Mar 25, 2006
913
914
915
free(io);
} // MojoInput_file_close
May 3, 2007
May 3, 2007
916
MojoInput *MojoInput_newFromFile(const char *path)
Mar 25, 2006
Mar 25, 2006
917
{
Dec 13, 2006
Dec 13, 2006
918
MojoInput *io = NULL;
Sep 20, 2007
Sep 20, 2007
919
void *f = NULL;
Mar 26, 2006
Mar 26, 2006
920
Sep 20, 2007
Sep 20, 2007
921
f = MojoPlatform_open(path, MOJOFILE_READ, 0);
May 3, 2007
May 3, 2007
922
if (f != NULL)
Dec 14, 2006
Dec 14, 2006
923
{
May 3, 2007
May 3, 2007
924
925
926
927
928
929
MojoInputFileInstance *inst;
inst = (MojoInputFileInstance *) xmalloc(sizeof (MojoInputFileInstance));
inst->path = xstrdup(path);
inst->handle = f;
io = (MojoInput *) xmalloc(sizeof (MojoInput));
May 7, 2007
May 7, 2007
930
io->ready = MojoInput_file_ready;
May 3, 2007
May 3, 2007
931
932
933
934
935
936
937
io->read = MojoInput_file_read;
io->seek = MojoInput_file_seek;
io->tell = MojoInput_file_tell;
io->length = MojoInput_file_length;
io->duplicate = MojoInput_file_duplicate;
io->close = MojoInput_file_close;
io->opaque = inst;
Dec 14, 2006
Dec 14, 2006
938
} // if
Mar 25, 2006
Mar 25, 2006
939
940
941
942
943
return io;
} // MojoInput_newFromFile
Jan 13, 2008
Jan 13, 2008
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// MojoInputs from blocks of memory.
typedef struct
{
void *ptr; // original pointer from xmalloc()
uint32 *refcount; // address in xmalloc()'d block for reference count.
const uint8 *data; // base of actual "file" data in xmalloc()'d block.
uint32 len; // size, in bytes, of "file" data.
uint32 pos; // current read position.
} MojoInputMemInstance;
static boolean MojoInput_memory_ready(MojoInput *io)
{
return true; // always ready!
} // MojoInput_memory_ready
static int64 MojoInput_memory_read(MojoInput *io, void *buf, uint32 bufsize)
{
MojoInputMemInstance *inst = (MojoInputMemInstance *) io->opaque;
const uint32 avail = inst->len - inst->pos;
if (bufsize > avail)
bufsize = avail;
memcpy(buf, inst->data + inst->pos, bufsize);
inst->pos += bufsize;
return bufsize;
} // MojoInput_memory_read
static boolean MojoInput_memory_seek(MojoInput *io, uint64 pos)
{
MojoInputMemInstance *inst = (MojoInputMemInstance *) io->opaque;
if (pos > (uint64) inst->len)
return false;
inst->pos = (uint32) pos;
return true;
} // MojoInput_memory_seek
static int64 MojoInput_memory_tell(MojoInput *io)
{
MojoInputMemInstance *inst = (MojoInputMemInstance *) io->opaque;
return (int64) inst->pos;
} // MojoInput_memory_tell
static int64 MojoInput_memory_length(MojoInput *io)
{
MojoInputMemInstance *inst = (MojoInputMemInstance *) io->opaque;
return (int64) inst->len;
} // MojoInput_memory_length
static MojoInput *MojoInput_memory_duplicate(MojoInput *io)
{
MojoInputMemInstance *srcinst = (MojoInputMemInstance *) io->opaque;
MojoInput *retval = NULL;
MojoInputMemInstance *inst = NULL;
if (srcinst->refcount != NULL)
{