Skip to content

Latest commit

 

History

History
1550 lines (1276 loc) · 42.9 KB

fileio.c

File metadata and controls

1550 lines (1276 loc) · 42.9 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);
Feb 19, 2012
Feb 19, 2012
18
MojoArchive *MojoArchive_createPKG(MojoInput *io);
Mar 26, 2006
Mar 26, 2006
19
20
21
22
23
typedef struct
{
const char *ext;
MojoArchiveCreateEntryPoint create;
May 9, 2009
May 9, 2009
24
boolean hasMagic; // can determine file type from contents?
Mar 26, 2006
Mar 26, 2006
25
26
} MojoArchiveType;
Nov 15, 2010
Nov 15, 2010
27
28
// !!! FIXME: order by archiver, not extension, since most of this is
// !!! FIXME: duplicates for .tar
Mar 26, 2006
Mar 26, 2006
29
30
static const MojoArchiveType archives[] =
{
May 9, 2009
May 9, 2009
31
32
33
{ "zip", MojoArchive_createZIP, true },
{ "tar", MojoArchive_createTAR, true },
{ "tar.gz", MojoArchive_createTAR, true },
Nov 15, 2010
Nov 15, 2010
34
{ "tar.xz", MojoArchive_createTAR, true },
May 9, 2009
May 9, 2009
35
36
37
38
39
{ "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
40
{ "txz", MojoArchive_createTAR, true },
May 10, 2009
May 10, 2009
41
{ "uz2", MojoArchive_createUZ2, false },
Mar 22, 2010
Mar 22, 2010
42
{ "pck", MojoArchive_createPCK, true },
Feb 19, 2012
Feb 19, 2012
43
{ "pkg", MojoArchive_createPKG, true },
Mar 26, 2006
Mar 26, 2006
44
45
};
Feb 28, 2010
Feb 28, 2010
46
47
48
#if SUPPORT_GZIP
Jun 16, 2012
Jun 16, 2012
49
#include "miniz.h"
Feb 28, 2010
Feb 28, 2010
50
51
52
53
54
55
56
57
58
#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
59
uint8 buffer[GZIP_READBUFSIZE];
Feb 28, 2010
Feb 28, 2010
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
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
143
144
145
146
147
info->stream.next_out = buf;
info->stream.avail_out = bufsize;
while (retval < ((int64) bufsize))
Feb 28, 2010
Feb 28, 2010
148
{
Nov 15, 2010
Nov 15, 2010
149
150
const uint32 before = info->stream.total_out;
int rc;
Feb 28, 2010
Feb 28, 2010
151
Nov 15, 2010
Nov 15, 2010
152
if (info->stream.avail_in == 0)
Feb 28, 2010
Feb 28, 2010
153
{
Nov 15, 2010
Nov 15, 2010
154
155
int64 br = origio->length(origio) - origio->tell(origio);
if (br > 0)
Feb 28, 2010
Feb 28, 2010
156
{
Nov 15, 2010
Nov 15, 2010
157
158
if (br > GZIP_READBUFSIZE)
br = GZIP_READBUFSIZE;
Feb 28, 2010
Feb 28, 2010
159
Nov 15, 2010
Nov 15, 2010
160
161
162
br = origio->read(origio, info->buffer, (uint32) br);
if (br <= 0)
return -1;
Feb 28, 2010
Feb 28, 2010
163
Nov 15, 2010
Nov 15, 2010
164
165
info->stream.next_in = info->buffer;
info->stream.avail_in = (uint32) br;
Feb 28, 2010
Feb 28, 2010
166
} // if
Nov 15, 2010
Nov 15, 2010
167
} // if
Feb 28, 2010
Feb 28, 2010
168
Nov 15, 2010
Nov 15, 2010
169
170
rc = inflate(&info->stream, Z_SYNC_FLUSH);
retval += (info->stream.total_out - before);
Feb 28, 2010
Feb 28, 2010
171
Sep 4, 2011
Sep 4, 2011
172
173
174
if ((rc == Z_STREAM_END) && (retval == 0))
return 0;
else if ((rc != Z_OK) && (rc != Z_STREAM_END))
Nov 15, 2010
Nov 15, 2010
175
176
return -1;
} // while
Feb 28, 2010
Feb 28, 2010
177
Nov 15, 2010
Nov 15, 2010
178
179
assert(retval >= 0);
info->uncompressed_position += (uint32) retval;
Feb 28, 2010
Feb 28, 2010
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
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
239
#include "bzip2/bzlib.h"
Feb 28, 2010
Feb 28, 2010
240
241
242
243
244
245
246
247
248
#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
249
uint8 buffer[BZIP2_READBUFSIZE];
Feb 28, 2010
Feb 28, 2010
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
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
347
348
349
350
351
info->stream.next_out = buf;
info->stream.avail_out = bufsize;
while (retval < ((int64) bufsize))
Feb 28, 2010
Feb 28, 2010
352
{
Nov 15, 2010
Nov 15, 2010
353
354
const uint32 before = info->stream.total_out_lo32;
int rc;
Feb 28, 2010
Feb 28, 2010
355
Nov 15, 2010
Nov 15, 2010
356
if (info->stream.avail_in == 0)
Feb 28, 2010
Feb 28, 2010
357
{
Nov 15, 2010
Nov 15, 2010
358
359
int64 br = origio->length(origio) - origio->tell(origio);
if (br > 0)
Feb 28, 2010
Feb 28, 2010
360
{
Nov 15, 2010
Nov 15, 2010
361
362
if (br > BZIP2_READBUFSIZE)
br = BZIP2_READBUFSIZE;
Feb 28, 2010
Feb 28, 2010
363
Nov 15, 2010
Nov 15, 2010
364
365
366
br = origio->read(origio, info->buffer, (uint32) br);
if (br <= 0)
return -1;
Feb 28, 2010
Feb 28, 2010
367
Nov 15, 2010
Nov 15, 2010
368
369
info->stream.next_in = (char *) info->buffer;
info->stream.avail_in = (uint32) br;
Feb 28, 2010
Feb 28, 2010
370
} // if
Nov 15, 2010
Nov 15, 2010
371
} // if
Feb 28, 2010
Feb 28, 2010
372
Nov 15, 2010
Nov 15, 2010
373
374
375
376
377
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
378
Nov 15, 2010
Nov 15, 2010
379
380
assert(retval >= 0);
info->uncompressed_position += (uint32) retval;
Feb 28, 2010
Feb 28, 2010
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
434
435
436
437
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
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
632
633
#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;
if (info->stream.avail_in == 0)
{
int64 br = origio->length(origio) - origio->tell(origio);
if (br > 0)
{
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
634
635
MojoInput *MojoInput_newCompressedStream(MojoInput *origio)
{
Nov 15, 2010
Nov 15, 2010
636
#if SUPPORT_GZIP || SUPPORT_BZIP2 || SUPPORT_XZ
Feb 28, 2010
Feb 28, 2010
637
638
639
// 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
640
uint8 magic[6];
Feb 28, 2010
Feb 28, 2010
641
const int64 br = origio->read(origio, magic, sizeof (magic));
Mar 17, 2010
Mar 17, 2010
642
if ((origio->seek(origio, 0)) && (br == sizeof (magic)))
Feb 28, 2010
Feb 28, 2010
643
644
{
#if SUPPORT_GZIP
Nov 15, 2010
Nov 15, 2010
645
646
647
648
649
{
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
650
651
652
#endif
#if SUPPORT_BZIP2
Nov 15, 2010
Nov 15, 2010
653
654
655
656
657
658
659
660
661
662
663
664
665
{
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
666
667
668
669
670
671
672
673
674
#endif
} // if
#endif
return NULL;
} // MojoInput_newCompressedStream
MojoArchive *MojoArchive_newFromInput(MojoInput *_io, const char *origfname)
Mar 26, 2006
Mar 26, 2006
675
676
677
{
int i;
MojoArchive *retval = NULL;
May 5, 2007
May 5, 2007
678
const char *ext = NULL;
Feb 28, 2010
Feb 28, 2010
679
680
681
MojoInput *io = MojoInput_newCompressedStream(_io);
if (io == NULL)
io = _io;
May 5, 2007
May 5, 2007
682
683
684
if (origfname != NULL)
{
May 9, 2009
May 9, 2009
685
ext = strrchr(origfname, '/');
May 5, 2007
May 5, 2007
686
687
688
689
690
691
if (ext == NULL)
ext = strchr(origfname, '.');
else
ext = strchr(ext+1, '.');
} // if
May 9, 2009
May 9, 2009
692
while (ext != NULL)
Mar 26, 2006
Mar 26, 2006
693
{
May 9, 2009
May 9, 2009
694
// Try for an exact match by filename extension.
May 5, 2007
May 5, 2007
695
ext++; // skip that '.'
Mar 26, 2006
Mar 26, 2006
696
697
for (i = 0; i < STATICARRAYLEN(archives); i++)
{
May 9, 2009
May 9, 2009
698
699
700
const MojoArchiveType *arc = &archives[i];
if (strcasecmp(ext, arc->ext) == 0)
return arc->create(io);
Mar 26, 2006
Mar 26, 2006
701
} // for
May 9, 2009
May 9, 2009
702
703
ext = strchr(ext, '.');
} // while
Mar 26, 2006
Mar 26, 2006
704
May 9, 2009
May 9, 2009
705
// Try any that could be determined without the file extension...
Mar 26, 2006
Mar 26, 2006
706
707
for (i = 0; i < STATICARRAYLEN(archives); i++)
{
May 9, 2009
May 9, 2009
708
709
const MojoArchiveType *arc = &archives[i];
if ((arc->hasMagic) && ((retval = arc->create(io)) != NULL))
Mar 26, 2006
Mar 26, 2006
710
711
712
713
714
715
716
717
return retval;
} // for
io->close(io);
return NULL; // nothing can handle this data.
} // MojoArchive_newFromInput
May 4, 2007
May 4, 2007
718
void MojoArchive_resetEntry(MojoArchiveEntry *info)
Mar 26, 2006
Mar 26, 2006
719
720
{
free(info->filename);
May 1, 2007
May 1, 2007
721
free(info->linkdest);
Apr 29, 2007
Apr 29, 2007
722
723
memset(info, '\0', sizeof (MojoArchiveEntry));
} // MojoArchive_resetEntry
Mar 26, 2006
Mar 26, 2006
724
725
May 1, 2007
May 1, 2007
726
727
// !!! 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
728
boolean MojoInput_toPhysicalFile(MojoInput *in, const char *fname, uint16 perms,
Jan 16, 2008
Jan 16, 2008
729
MojoChecksums *checksums, int64 maxbytes,
May 1, 2007
May 1, 2007
730
MojoInput_FileCopyCallback cb, void *data)
May 7, 2007
May 7, 2007
732
733
boolean retval = false;
uint32 start = MojoPlatform_ticks();
Sep 20, 2007
Sep 20, 2007
734
void *out = NULL;
735
boolean iofailure = false;
May 1, 2007
May 1, 2007
736
737
int64 flen = 0;
int64 bw = 0;
May 27, 2007
May 27, 2007
738
MojoChecksumContext sumctx;
739
740
741
742
if (in == NULL)
return false;
May 27, 2007
May 27, 2007
743
744
745
746
747
748
if (checksums != NULL)
{
memset(checksums, '\0', sizeof (MojoChecksums));
MojoChecksum_init(&sumctx);
} // if
May 12, 2007
May 12, 2007
749
750
751
752
753
754
// 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
755
if (!cb(MojoPlatform_ticks() - start, 0, 0, -1, data))
May 12, 2007
May 12, 2007
756
757
758
759
iofailure = true;
} // if
} // while
May 1, 2007
May 1, 2007
760
flen = in->length(in);
Jan 16, 2008
Jan 16, 2008
761
762
if ((maxbytes >= 0) && (flen > maxbytes))
flen = maxbytes;
May 1, 2007
May 1, 2007
763
Dec 9, 2006
Dec 9, 2006
764
MojoPlatform_unlink(fname);
May 12, 2007
May 12, 2007
765
if (!iofailure)
Sep 20, 2007
Sep 20, 2007
766
767
768
769
770
{
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
771
May 7, 2007
May 7, 2007
772
if (out != NULL)
May 7, 2007
May 7, 2007
774
while (!iofailure)
May 8, 2007
May 8, 2007
776
int64 br = 0;
Jan 16, 2008
Jan 16, 2008
777
778
779
780
781
782
783
784
785
786
787
788
789
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
790
May 7, 2007
May 7, 2007
791
792
793
// 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
794
795
MojoPlatform_sleep(100);
else
May 1, 2007
May 1, 2007
796
{
Feb 2, 2008
Feb 2, 2008
797
br = in->read(in, scratchbuf_128k, (uint32) maxread);
May 7, 2007
May 7, 2007
798
799
800
801
802
if (br == 0) // we're done!
break;
else if (br < 0)
iofailure = true;
else
May 1, 2007
May 1, 2007
803
{
Sep 25, 2007
Sep 25, 2007
804
if (MojoPlatform_write(out, scratchbuf_128k, (uint32) br) != br)
May 1, 2007
May 1, 2007
805
iofailure = true;
May 7, 2007
May 7, 2007
806
else
May 27, 2007
May 27, 2007
807
808
{
if (checksums != NULL)
Sep 25, 2007
Sep 25, 2007
809
MojoChecksum_append(&sumctx, scratchbuf_128k, (uint32) br);
May 7, 2007
May 7, 2007
810
bw += br;
May 27, 2007
May 27, 2007
811
} // else
May 7, 2007
May 7, 2007
812
} // else
May 1, 2007
May 1, 2007
813
} // else
May 7, 2007
May 7, 2007
815
816
if (cb != NULL)
{
May 8, 2007
May 8, 2007
817
if (!cb(MojoPlatform_ticks() - start, br, bw, flen, data))
May 7, 2007
May 7, 2007
818
819
820
821
iofailure = true;
} // if
} // while
Sep 20, 2007
Sep 20, 2007
822
if (MojoPlatform_close(out) != 0)
May 7, 2007
May 7, 2007
823
iofailure = true;
Jan 16, 2008
Jan 16, 2008
824
825
else if (bw != flen)
iofailure = true;
May 1, 2007
May 1, 2007
826
May 7, 2007
May 7, 2007
827
828
829
830
831
if (iofailure)
MojoPlatform_unlink(fname);
else
{
MojoPlatform_chmod(fname, perms);
May 27, 2007
May 27, 2007
832
833
if (checksums != NULL)
MojoChecksum_finish(&sumctx, checksums);
May 7, 2007
May 7, 2007
834
835
retval = true;
} // else
May 7, 2007
May 7, 2007
838
839
in->close(in);
return retval;
Dec 9, 2006
Dec 9, 2006
840
} // MojoInput_toPhysicalFile
Mar 25, 2006
Mar 25, 2006
842
Dec 3, 2006
Dec 3, 2006
843
844
845
MojoInput *MojoInput_newFromArchivePath(MojoArchive *ar, const char *fname)
{
MojoInput *retval = NULL;
May 4, 2007
May 4, 2007
846
if (ar->enumerate(ar))
Dec 3, 2006
Dec 3, 2006
847
{
Apr 29, 2007
Apr 29, 2007
848
const MojoArchiveEntry *entinfo;
Dec 3, 2006
Dec 3, 2006
849
850
while ((entinfo = ar->enumNext(ar)) != NULL)
{
May 4, 2007
May 4, 2007
851
if (strcmp(entinfo->filename, fname) == 0)
Dec 3, 2006
Dec 3, 2006
852
853
854
855
856
857
858
859
860
861
862
863
{
if (entinfo->type == MOJOARCHIVE_ENTRY_FILE)
retval = ar->openCurrentEntry(ar);
break;
} // if
} // while
} // if
return retval;
} // MojoInput_newFromArchivePath
Mar 26, 2006
Mar 26, 2006
864
Mar 25, 2006
Mar 25, 2006
865
866
// MojoInputs from files on the OS filesystem.
Mar 26, 2006
Mar 26, 2006
867
868
typedef struct
{
Sep 20, 2007
Sep 20, 2007
869
void *handle;
Mar 26, 2006
Mar 26, 2006
870
871
872
char *path;
} MojoInputFileInstance;
May 7, 2007
May 7, 2007
873
874
875
876
877
878
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
879
static int64 MojoInput_file_read(MojoInput *io, void *buf, uint32 bufsize)
Mar 25, 2006
Mar 25, 2006
880
{
Mar 26, 2006
Mar 26, 2006
881
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
Sep 20, 2007
Sep 20, 2007
882
return MojoPlatform_read(inst->handle, buf, bufsize);
Mar 25, 2006
Mar 25, 2006
883
884
885
886
} // MojoInput_file_read
static boolean MojoInput_file_seek(MojoInput *io, uint64 pos)
{
Mar 26, 2006
Mar 26, 2006
887
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
Sep 20, 2007
Sep 20, 2007
888
return (MojoPlatform_seek(inst->handle, pos, MOJOSEEK_SET) == pos);
Mar 25, 2006
Mar 25, 2006
889
890
} // MojoInput_file_seek
Mar 26, 2006
Mar 26, 2006
891
static int64 MojoInput_file_tell(MojoInput *io)
Mar 25, 2006
Mar 25, 2006
892
{
Mar 26, 2006
Mar 26, 2006
893
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
Sep 20, 2007
Sep 20, 2007
894
return MojoPlatform_tell(inst->handle);
Mar 25, 2006
Mar 25, 2006
895
896
} // MojoInput_file_tell
Mar 26, 2006
Mar 26, 2006
897
898
899
static int64 MojoInput_file_length(MojoInput *io)
{
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
Sep 20, 2007
Sep 20, 2007
900
return MojoPlatform_flen(inst->handle);
Mar 26, 2006
Mar 26, 2006
901
902
903
904
905
906
907
908
} // 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
909
910
static void MojoInput_file_close(MojoInput *io)
{
Mar 26, 2006
Mar 26, 2006
911
MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque;
Sep 20, 2007
Sep 20, 2007
912
MojoPlatform_close(inst->handle);
Mar 26, 2006
Mar 26, 2006
913
914
free(inst->path);
free(inst);
Mar 25, 2006
Mar 25, 2006
915
916
917
free(io);
} // MojoInput_file_close
May 3, 2007
May 3, 2007
918
MojoInput *MojoInput_newFromFile(const char *path)
Mar 25, 2006
Mar 25, 2006
919
{
Dec 13, 2006
Dec 13, 2006
920
MojoInput *io = NULL;
Sep 20, 2007
Sep 20, 2007
921
void *f = NULL;
Mar 26, 2006
Mar 26, 2006
922
Sep 20, 2007
Sep 20, 2007
923
f = MojoPlatform_open(path, MOJOFILE_READ, 0);
May 3, 2007
May 3, 2007
924
if (f != NULL)
Dec 14, 2006
Dec 14, 2006
925
{
May 3, 2007
May 3, 2007
926
927
928
929
930
931
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
932
io->ready = MojoInput_file_ready;
May 3, 2007
May 3, 2007
933
934
935
936
937
938
939
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
940
} // if
Mar 25, 2006
Mar 25, 2006
941
942
943
944
945
return io;
} // MojoInput_newFromFile
Jan 13, 2008
Jan 13, 2008
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;