summaryrefslogtreecommitdiff
path: root/met/zipparse.c
blob: 7401fd58a2159c7e25fe8e0de1a1e3f36b6b6ced (plain)
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
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
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
#include "assert.h"
#include "gserrors.h"
#include "memory_.h"
#include "zipparse.h"

#include "pltop.h"  /* NB hack to enable disable zip/xml parsing */

private inline int 
stream_has(stream_cursor_read *pr, unsigned int cnt)
{
    return( pr->limit - pr->ptr >= cnt );
}


private inline byte 
read_byte(stream_cursor_read *pr)
{
    pr->ptr++;
    return *pr->ptr;
}

private inline unsigned int 
read2(stream_cursor_read *pr, unsigned short *a)
{
    if (stream_has(pr, 2)) {
	*a =  read_byte(pr) | (read_byte(pr) << 8) ;
	// dprintf2(gs_mem_ptr, "read2 %0hd:%0hx\n", *a, *a);
    }
    else
	return eNeedData;
    return 0;
}

private inline unsigned long 
read4(stream_cursor_read *pr, unsigned long *a)
{
    if (stream_has(pr, 4)) {
	*a = read_byte(pr) |  
	    (read_byte(pr) << 8) |  
	    (read_byte(pr) << 16) |  
	    (read_byte(pr) << 24);
	// dprintf2(gs_mem_ptr, "read4 %0ld:%0lx\n", *a, *a);
    }
    else
	return eNeedData;
    return 0;
}


private int 
zip_new_block(zip_state_t *pzip, zip_part_t *part)
{
    int code = 0;
    zip_block_t *blk;

    if (pzip->free_list) {
	blk = pzip->free_list;
	pzip->free_list = blk->next;
	blk->next = NULL;
	blk->writeoffset = 0;
    }
    else {
	blk = (zip_block_t *) gs_alloc_bytes(pzip->memory, 
					     sizeof(zip_block_t), 
					     "zip_new_block");	
	if (blk == NULL)
	    return gs_error_VMerror;
	blk->writeoffset = 0;
	blk->next = NULL;
    }
    assert(part->write);
    assert(part->write->writeoffset == ZIP_BLOCK_SIZE);

    memset(blk->data, 0xF0, ZIP_BLOCK_SIZE);

    if (part->tail)
	part->tail->next = blk;
    part->tail = blk;

    return code;
}

private int 
zip_init_part(zip_state_t *pzip)
{
    zip_part_t *part = (zip_part_t *)gs_alloc_bytes(pzip->memory, size_of(zip_part_t), 
						    "zip_init_part");

    part->parent = pzip;  /* back pointer */
    pzip->num_files++;
    pzip->parts[pzip->num_files - 1] = part;
    pzip->part_read_state = 0;
    part->tail = part->head = part->csaved = part->metasize = part->namesize = 0;
    zip_new_block(pzip, part);
    part->head = part->curr = part->tail;
    part->keep_it = false;  /* read once by default */
    part->buf = NULL;
    part->zs = NULL;
    part->newfile = true;
    
    return 0;
}


/* Can return eNeedData, eExitLanguage, error, 0  
 */
private int 
zip_read_part(zip_state_t *pzip, stream_cursor_read *pr)
{
    unsigned long id;
    unsigned short shortInt;
    unsigned long longInt;
    int code = 0;
    zip_part_t *part =0;
    int i;

    switch( pzip->part_read_state ) {
    case 0:
	if ((code = read4(pr, &id)) != 0)
	    return code;
        if (id == 0x04034b50) {
	    if ((code = zip_init_part(pzip)) != 0 )
		return code;
	    part = pzip->parts[pzip->num_files - 1];
	} else { // if (id == 0x02014b50) 
	    // end of archive 
	    pzip->part_read_state = 20;
	    return 0;
	}
	pzip->part_read_state++;

    case 1: /* version to extract*/
	if ((code = read2(pr, &shortInt)) != 0)
	    return code;
	pzip->part_read_state++;

    case 2: /* general*/
	part = pzip->parts[pzip->num_files - 1];
	if ((code = read2(pr, &part->gp_bitflag)) != 0)
	    return code;
	pzip->part_read_state++;
	 
    case 3: /* method */
	part = pzip->parts[pzip->num_files - 1];
	if ((code = read2(pr, &part->comp_method)) != 0)
	    return code;
	pzip->part_read_state++;
   
    case 4: /* last mod file time */
	if ((code = read2(pr, &shortInt)) != 0)
	    return code;
	pzip->part_read_state++;
	    
    case 5: /* last mod file date */
	if ((code = read2(pr, &shortInt)) != 0)
	    return code;
	pzip->part_read_state++;

    case 6: /* crc-32 */
	if ((code = read4(pr, &longInt)) != 0)
	    return code;
	pzip->part_read_state++;

    case 7: /* csize */
	part = pzip->parts[pzip->num_files - 1];
	if ((code = read4(pr, &part->csize)) != 0)
	    return code;
	pzip->part_read_state++;	
	
    case 8: /* usize */
	part = pzip->parts[pzip->num_files - 1];
	if ((code = read4(pr, &part->usize)) != 0)
	    return code;
	pzip->part_read_state++;

    case 9: /* namesize */
	part = pzip->parts[pzip->num_files - 1];
	if ((code = read2(pr, &part->namesize)) != 0)
	    return code;
	pzip->part_read_state++;

    case 10: /* metasize */
	part = pzip->parts[pzip->num_files - 1];
	if ((code = read2(pr, &part->metasize)) != 0)
	    return code;
	pzip->part_read_state++; 

    case 11:
	part = pzip->parts[pzip->num_files - 1];
	if (stream_has(pr, part->namesize)) {
	    part->name = gs_alloc_bytes(pzip->memory, part->namesize + 1, "pzip part name");
	    if (part->name == NULL) 
		return gs_throw(-1, "out of memory");
	    memcpy(part->name, pr->ptr +1, part->namesize);
	    part->name[part->namesize] = 0;
	    pr->ptr += part->namesize;
	    pzip->part_read_state++;
	} 
	else 
	    return eNeedData;
    case 12:
	part = pzip->parts[pzip->num_files - 1];
	if (stream_has(pr, part->metasize)) {
	    pr->ptr += part->metasize;
	    pzip->part_read_state++;
	}
	else if (part->metasize){
	    part->metasize -= pr->limit - pr->ptr;
	    pr->ptr = pr->limit;
	    return eNeedData;
	}
    case 13:    
	pzip->read_state = 1;  /* read file after this header */
	pzip->part_read_state = 0; /* next call looks for header */
	break;

    case 20:
	/* read extra foo after last file 
	   [archive decryption header] (EFS)
	   [archive extra data record] (EFS)
	   [central directory]
	   [zip64 end of central directory record]
	   [zip64 end of central directory locator] 
	   [end of central directory record]
	*/
	for ( i = pr->limit - pr->ptr; i > 4; --i, pr->ptr++ ) {
	    if ( pr->ptr[1] == 0x50 && 
		 pr->ptr[2] == 0x4b &&
		 pr->ptr[3] == 0x05 &&
		 pr->ptr[4] == 0x06 ) {
		pzip->part_read_state++;
		pr->ptr += 4;  
		break;
	    }
	}
	break;
    case 21:

	if (stream_has(pr, 18) == 0 )
	    return eNeedData;
	pr->ptr += 16;
	if (stream_has(pr, 3)) {
	    read2(pr, &part->skip_len);
	    pzip->part_read_state++;
	} else {
	    pr->ptr = pr->limit - 1;
	    pzip->part_read_state = 0;  /* reset for next zip file */
	    return -102; // e_ExitLanguage;
	}
    case 22:
	if (stream_has(pr, part->skip_len) == 0 )
	    pr->ptr += part->skip_len;


	pzip->part_read_state = 0;  /* reset for next zip file */

	return -102; // e_ExitLanguage;
    
    default:
	return gs_throw(-1, "coding error");
    }
    
    return code;
}


private int 
zip_init_write_stream(zip_state_t *pzip, zip_part_t *part)
{
    if (part->zs == NULL) {
	part->zs = gs_alloc_bytes(pzip->memory, size_of(z_stream), "zip z_stream");
	if (part->zs == NULL)
	    return gs_throw(-1, "out of memory");
	part->zs->zalloc = 0;
	part->zs->zfree = 0;
	part->zs->opaque = 0;
    }
    return 0;
}

private int 
zip_decompress_data(zip_state_t *pzip, stream_cursor_read *pin )
{
    int code = 0;
    zip_part_t *part = pzip->parts[pzip->read_part];
    z_stream *zs = 0;
    long rlen = pin->limit - pin->ptr - 1;
    long wlen = 0;
    byte *wptr = NULL;

    int rstart = pin->ptr;
    long len;
    int status;

    /* add to tail of part's block list */
    wptr = &part->tail->data[part->tail->writeoffset];

    if (ZIP_BLOCK_SIZE <= part->tail->writeoffset + 1) {
	if ((code = zip_new_block(pzip, part)))
	    return gs_throw(code, "zip_new_block");
	wptr = &part->tail->data[part->tail->writeoffset];
	wlen = ZIP_BLOCK_SIZE;
    }
    else 
	wlen = ((ZIP_BLOCK_SIZE - 1) - part->tail->writeoffset) + 1;

    zip_init_write_stream(pzip, part);
	
    if (part->comp_method == 0) {
	int left = part->csize - part->csaved;
	if (left == 0)
	    return eEndOfStream;
	rlen = min(left, min(rlen, wlen));
	memcpy(wptr, pin->ptr+1, rlen);
	part->tail->writeoffset += rlen;
	part->csaved += rlen;
	pin->ptr += rlen;    
	if (part->csize && part->csaved == part->csize) 
	    return eEndOfStream;
    }
    else {  /* 8 == flate */
	zs = part->zs;
	zs->next_in = pin->ptr + 1;
	zs->avail_in = rlen;
	zs->next_out = wptr;
	zs->avail_out = wlen;

	if (part->newfile) 	/* init for new file */
	    inflateInit2(part->zs, -15);  /* neg zip, 15bit window */

	status = inflate(zs, Z_NO_FLUSH);

	part->csaved += zs->next_in - pin->ptr - 1;  // update comressed read for zips with csize
	pin->ptr = zs->next_in - 1;
	part->tail->writeoffset += zs->next_out - wptr;

	switch (status) {
        case Z_OK:
	    if (pin->ptr + 1 < pin->limit)
		code = 0;
	    else
		code = pin->ptr > rstart ? 0 : 1;
	    part->newfile = false;

	    if (part->csize && part->csaved == part->csize) {
		/* recursive call
		 * NB: need to test the end of part/end of file/no csize case. */
		code = zip_decompress_data(pzip, pin);
	    }
	    break;

        case Z_STREAM_END:
	    part->newfile = true;
	    inflateEnd(zs);
	    code = eEndOfStream; // was: EOFC;
	    break;

        default:
	    code = ERRC;
	    gs_throw(code, "inflate error");
	}
    }
    return code;
}

/* returns zero on ok else error, swallows status returns 
 */
int 
zip_decomp_process(met_parser_state_t *st, met_state_t *mets, zip_state_t *pzip, 
		   stream_cursor_read *pr)
{
    int code = 0;
    zip_part_t *rpart = NULL;

    /* update reading pointer 
     * NB: client should be able to choose a different file to read
     * currently just serially sends file after file.
     */

    if (pzip->read_part < 0 && pzip->num_files > 0)
	pzip->read_part = 0;
    if (pzip->read_part >= pzip->num_files )
	return code; // Nothing to do til we have some data.

    rpart = pzip->parts[pzip->read_part];

    /* decompress and send data to parser */
    code = zip_decompress_data(pzip, pr);
    if (code == eEndOfStream) { // good end of input stream 
	code = 0;	

	/* determine if the xps should be parsed and call the xml parser
	 */
	if ( pzip->inline_mode )
	    zip_page(st, mets, pzip, rpart);

	/* setup to read next part */
	pzip->read_part++;
	pzip->read_state = 2; 
    }
    else if (code == eWriteStall) { /* write stall on output */
	long len = rpart->s.r.limit - rpart->s.r.ptr;
	if ( len && rpart->buf -1 != rpart->s.r.ptr )
	    memmove(rpart->buf, rpart->s.r.ptr, len);
	rpart->s.r.ptr = rpart->buf -1;
	rpart->s.r.limit = rpart->buf + ( len - 1 );
	
	code = 0;  /* let it be read */
    }
    if (code == eNeedData) 
	code = 0;
    return code;
}


/* 
 * code == eNeedData is returned as 0 here.
 */
int
zip_process(met_parser_state_t *st, met_state_t *mets, zip_state_t *pzip, stream_cursor_read *pr)
{
    int code = 0;
    zip_part_t *rpart = NULL;
    zip_part_t *wpart = NULL;
    int last_len = pr->limit - pr->ptr;

    while (pr->limit - pr->ptr > 4 && code == 0) {

	switch (pzip->read_state) {
	case 0: /* local file header and skip of end of zip file records */
	    if ((code = zip_read_part(pzip, pr)) != 0) {
		return code;
	    }
	    /* 0 : more header to read 
	     * 1 : file to read
	     * 3 : end of file 
	     */
	    break;
	case 1: /* file data */	
	    /* We don't save compressed data since the 'standard' XPS files don't have lengths
	     */

	case 2: /* optional Data descriptor */
	    wpart = pzip->parts[pzip->num_files - 1];
	    if (wpart && wpart->csaved && wpart->csaved == wpart->csize ) {
		/* have count and fall through */

		if (wpart->gp_bitflag & 1 << 3) { 
		    /* Skip Data descriptor */
		    if (stream_has(pr, 12) == 0) {
			pzip->read_state = 2;
			return 0;
		    }
		    pr->ptr += 12; 
		}
		pzip->read_state = 0; 
		break;
	    }
	    else if (pzip->read_state == 2  && wpart->gp_bitflag & 1 << 3) {
		 /* else end of stream goto after decompress */ 
		if ( pr->ptr[1] == 0x50 &&
		     pr->ptr[2] == 0x4b &&
		     pr->ptr[3] == 0x07 &&
		     pr->ptr[4] == 0x08 ) {
		    /* check for split archive marker in the middle of the file
		     * spec says it must be first ?
		     */
		    if (stream_has(pr, 16) == 0) 
			return 0;
		    pr->ptr += 16; 
		}
		else {
		    if (stream_has(pr, 12) == 0) 
			return 0;
		    pr->ptr += 12; 
		}
		pzip->read_state = 0; 
		break;
	    }

	case 3:
    
	    if ((code = zip_decomp_process(st, mets, pzip, pr)) != 0 )
		return code;
	    break;

	case 4:
	default :
	    break;
	    //pzip->read_state = 0; 
	}
    }
    if (code == eNeedData) 
	return 0;
    return code;
}

private int
zip_initialize(zip_state_t *pzip)
{
    int i = 0;

    if (pzip->memory == 0)
	return -1;
    
#ifdef PARSE_XML_HACK
    pzip->zip_mode = false;
#else
    pzip->zip_mode = true;
#endif

    pzip->inline_mode = false;  /* false == buffer entire job in ram! */

    pzip->need_dir = false;
    pzip->needfiledata = false;
    pzip->read_state = 0;
    pzip->part_read_state = 0;
    pzip->read_part = -1;
    pzip->num_files = 0;
    for (i=0; i < 5000;  i++)
	pzip->parts[i] = 0;

    return 0;
}

private bool
is_font(char *name)
{
    int pos = strlen(name) - 4;
    if (name && pos > 0 )
	return strncasecmp("TTF", &name[pos], 3);
    else
	return false;
}

int
zip_end_job(met_parser_state_t *st, met_state_t *mets, zip_state_t *pzip)
{
    int i = 0; //pzip->firstPage;
    int code = 0;

    
    if (!pzip->inline_mode) {
	for (; i < 5000;  i++) {
	    if (pzip->parts[i]) {
		code = zip_page(st, mets, pzip, pzip->parts[i]);
		if (code) {
		    gs_rethrow(code, "delayed zip_page");
		    break;
		}
	    }
	}
    }

    for (i=0; i < 5000;  i++) {
	if (pzip->parts[i]) {
	    if ( is_font(pzip->parts[i]->name) ) {
		char buf[256];
		buf[0] = '/';
		buf[1] = 0;
		strncpy(&buf[1], pzip->parts[i]->name, strlen(pzip->parts[i]->name));
		buf[strlen(pzip->parts[i]->name) + 1] = 0;
		pl_dict_undef(&mets->font_dict, buf, strlen(buf));
	    }
	    zip_part_free_all(pzip->parts[i]);

	    // need destructor here.
	    gs_free_object(pzip->memory, pzip->parts[i]->name, "zip_part_free struct");
	    gs_free_object(pzip->memory, pzip->parts[i]->buf, "zip_part_free struct");
	    gs_free_object(pzip->memory, pzip->parts[i]->zs, "zip_part_free struct");
	    gs_free_object(pzip->memory, pzip->parts[i], "zip_part_free struct");
	    pzip->parts[i] = 0;
	}
    }
    pzip->read_part = -1;

    return zip_initialize(pzip);
}

zip_state_t *
zip_init_instance(gs_memory_t *mem)
{
    zip_state_t *pzip;

    pzip = gs_alloc_bytes(mem, sizeof(zip_state_t), "zip_init_instance alloc zip_state_t");
    pzip->memory = mem;
    zip_initialize(pzip);
    pzip->free_blk_list_len = 0;
    pzip->free_list = NULL;


    return pzip;
}