Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7640 views
1
#include "mupdf/fitz.h"
2
3
/* Pretend we have a filter that just copies data forever */
4
5
fz_stream *
6
fz_open_copy(fz_context *ctx, fz_stream *chain)
7
{
8
return fz_keep_stream(ctx, chain);
9
}
10
11
/* Null filter copies a specified amount of data */
12
13
struct null_filter
14
{
15
fz_stream *chain;
16
int remain;
17
int offset;
18
unsigned char buffer[4096];
19
};
20
21
static int
22
next_null(fz_context *ctx, fz_stream *stm, int max)
23
{
24
struct null_filter *state = stm->state;
25
int n;
26
27
if (state->remain == 0)
28
return EOF;
29
fz_seek(ctx, state->chain, state->offset, 0);
30
n = fz_available(ctx, state->chain, max);
31
if (n > state->remain)
32
n = state->remain;
33
if (n > sizeof(state->buffer))
34
n = sizeof(state->buffer);
35
memcpy(state->buffer, state->chain->rp, n);
36
stm->rp = state->buffer;
37
stm->wp = stm->rp + n;
38
if (n == 0)
39
return EOF;
40
state->chain->rp += n;
41
state->remain -= n;
42
state->offset += n;
43
stm->pos += n;
44
return *stm->rp++;
45
}
46
47
static void
48
close_null(fz_context *ctx, void *state_)
49
{
50
struct null_filter *state = (struct null_filter *)state_;
51
fz_stream *chain = state->chain;
52
fz_free(ctx, state);
53
fz_drop_stream(ctx, chain);
54
}
55
56
fz_stream *
57
fz_open_null(fz_context *ctx, fz_stream *chain, int len, int offset)
58
{
59
struct null_filter *state;
60
61
if (len < 0)
62
len = 0;
63
fz_try(ctx)
64
{
65
state = fz_malloc_struct(ctx, struct null_filter);
66
state->chain = chain;
67
state->remain = len;
68
state->offset = offset;
69
}
70
fz_catch(ctx)
71
{
72
fz_drop_stream(ctx, chain);
73
fz_rethrow(ctx);
74
}
75
76
return fz_new_stream(ctx, state, next_null, close_null);
77
}
78
79
/* Concat filter concatenates several streams into one */
80
81
struct concat_filter
82
{
83
int max;
84
int count;
85
int current;
86
int pad; /* 1 if we should add whitespace padding between streams */
87
unsigned char ws_buf;
88
fz_stream *chain[1];
89
};
90
91
static int
92
next_concat(fz_context *ctx, fz_stream *stm, int max)
93
{
94
struct concat_filter *state = (struct concat_filter *)stm->state;
95
int n;
96
97
while (state->current < state->count)
98
{
99
/* Read the next block of underlying data. */
100
if (stm->wp == state->chain[state->current]->wp)
101
state->chain[state->current]->rp = stm->wp;
102
n = fz_available(ctx, state->chain[state->current], max);
103
if (n)
104
{
105
stm->rp = state->chain[state->current]->rp;
106
stm->wp = state->chain[state->current]->wp;
107
stm->pos += n;
108
return *stm->rp++;
109
}
110
else
111
{
112
if (state->chain[state->current]->error)
113
{
114
stm->error = 1;
115
break;
116
}
117
state->current++;
118
fz_drop_stream(ctx, state->chain[state->current-1]);
119
if (state->pad)
120
{
121
stm->rp = (&state->ws_buf)+1;
122
stm->wp = stm->rp + 1;
123
stm->pos++;
124
return 32;
125
}
126
}
127
}
128
129
stm->rp = stm->wp;
130
131
return EOF;
132
}
133
134
static void
135
close_concat(fz_context *ctx, void *state_)
136
{
137
struct concat_filter *state = (struct concat_filter *)state_;
138
int i;
139
140
for (i = state->current; i < state->count; i++)
141
{
142
fz_drop_stream(ctx, state->chain[i]);
143
}
144
fz_free(ctx, state);
145
}
146
147
fz_stream *
148
fz_open_concat(fz_context *ctx, int len, int pad)
149
{
150
struct concat_filter *state;
151
152
state = fz_calloc(ctx, 1, sizeof(struct concat_filter) + (len-1)*sizeof(fz_stream *));
153
state->max = len;
154
state->count = 0;
155
state->current = 0;
156
state->pad = pad;
157
state->ws_buf = 32;
158
159
return fz_new_stream(ctx, state, next_concat, close_concat);
160
}
161
162
void
163
fz_concat_push(fz_context *ctx, fz_stream *concat, fz_stream *chain)
164
{
165
struct concat_filter *state = (struct concat_filter *)concat->state;
166
167
if (state->count == state->max)
168
fz_throw(ctx, FZ_ERROR_GENERIC, "Concat filter size exceeded");
169
170
state->chain[state->count++] = chain;
171
}
172
173
/* ASCII Hex Decode */
174
175
typedef struct fz_ahxd_s fz_ahxd;
176
177
struct fz_ahxd_s
178
{
179
fz_stream *chain;
180
int eod;
181
unsigned char buffer[256];
182
};
183
184
static inline int iswhite(int a)
185
{
186
switch (a) {
187
case '\n': case '\r': case '\t': case ' ':
188
case '\0': case '\f': case '\b': case 0177:
189
return 1;
190
}
191
return 0;
192
}
193
194
static inline int ishex(int a)
195
{
196
return (a >= 'A' && a <= 'F') ||
197
(a >= 'a' && a <= 'f') ||
198
(a >= '0' && a <= '9');
199
}
200
201
static inline int unhex(int a)
202
{
203
if (a >= 'A' && a <= 'F') return a - 'A' + 0xA;
204
if (a >= 'a' && a <= 'f') return a - 'a' + 0xA;
205
if (a >= '0' && a <= '9') return a - '0';
206
return 0;
207
}
208
209
static int
210
next_ahxd(fz_context *ctx, fz_stream *stm, int max)
211
{
212
fz_ahxd *state = stm->state;
213
unsigned char *p = state->buffer;
214
unsigned char *ep;
215
int a, b, c, odd;
216
217
if (max > sizeof(state->buffer))
218
max = sizeof(state->buffer);
219
ep = p + max;
220
221
odd = 0;
222
223
while (p < ep)
224
{
225
if (state->eod)
226
break;
227
228
c = fz_read_byte(ctx, state->chain);
229
if (c < 0)
230
break;
231
232
if (ishex(c))
233
{
234
if (!odd)
235
{
236
a = unhex(c);
237
odd = 1;
238
}
239
else
240
{
241
b = unhex(c);
242
*p++ = (a << 4) | b;
243
odd = 0;
244
}
245
}
246
else if (c == '>')
247
{
248
if (odd)
249
*p++ = (a << 4);
250
state->eod = 1;
251
break;
252
}
253
else if (!iswhite(c))
254
{
255
fz_throw(ctx, FZ_ERROR_GENERIC, "bad data in ahxd: '%c'", c);
256
}
257
}
258
stm->rp = state->buffer;
259
stm->wp = p;
260
stm->pos += p - state->buffer;
261
262
if (stm->rp != p)
263
return *stm->rp++;
264
return EOF;
265
}
266
267
static void
268
close_ahxd(fz_context *ctx, void *state_)
269
{
270
fz_ahxd *state = (fz_ahxd *)state_;
271
fz_stream *chain = state->chain;
272
fz_free(ctx, state);
273
fz_drop_stream(ctx, chain);
274
}
275
276
fz_stream *
277
fz_open_ahxd(fz_context *ctx, fz_stream *chain)
278
{
279
fz_ahxd *state;
280
281
fz_try(ctx)
282
{
283
state = fz_malloc_struct(ctx, fz_ahxd);
284
state->chain = chain;
285
state->eod = 0;
286
}
287
fz_catch(ctx)
288
{
289
fz_drop_stream(ctx, chain);
290
fz_rethrow(ctx);
291
}
292
293
return fz_new_stream(ctx, state, next_ahxd, close_ahxd);
294
}
295
296
/* ASCII 85 Decode */
297
298
typedef struct fz_a85d_s fz_a85d;
299
300
struct fz_a85d_s
301
{
302
fz_stream *chain;
303
unsigned char buffer[256];
304
int eod;
305
};
306
307
static int
308
next_a85d(fz_context *ctx, fz_stream *stm, int max)
309
{
310
fz_a85d *state = stm->state;
311
unsigned char *p = state->buffer;
312
unsigned char *ep;
313
int count = 0;
314
int word = 0;
315
int c;
316
317
if (state->eod)
318
return EOF;
319
320
if (max > sizeof(state->buffer))
321
max = sizeof(state->buffer);
322
323
ep = p + max;
324
while (p < ep)
325
{
326
c = fz_read_byte(ctx, state->chain);
327
if (c < 0)
328
break;
329
330
if (c >= '!' && c <= 'u')
331
{
332
if (count == 4)
333
{
334
word = word * 85 + (c - '!');
335
336
*p++ = (word >> 24) & 0xff;
337
*p++ = (word >> 16) & 0xff;
338
*p++ = (word >> 8) & 0xff;
339
*p++ = (word) & 0xff;
340
341
word = 0;
342
count = 0;
343
}
344
else
345
{
346
word = word * 85 + (c - '!');
347
count ++;
348
}
349
}
350
351
else if (c == 'z' && count == 0)
352
{
353
*p++ = 0;
354
*p++ = 0;
355
*p++ = 0;
356
*p++ = 0;
357
}
358
359
else if (c == '~')
360
{
361
c = fz_read_byte(ctx, state->chain);
362
if (c != '>')
363
fz_warn(ctx, "bad eod marker in a85d");
364
365
switch (count) {
366
case 0:
367
break;
368
case 1:
369
/* Specifically illegal in the spec, but adobe
370
* and gs both cope. See normal_87.pdf for a
371
* case where this matters. */
372
fz_warn(ctx, "partial final byte in a85d");
373
break;
374
case 2:
375
word = word * (85 * 85 * 85) + 0xffffff;
376
*p++ = word >> 24;
377
break;
378
case 3:
379
word = word * (85 * 85) + 0xffff;
380
*p++ = word >> 24;
381
*p++ = word >> 16;
382
break;
383
case 4:
384
word = word * 85 + 0xff;
385
*p++ = word >> 24;
386
*p++ = word >> 16;
387
*p++ = word >> 8;
388
break;
389
}
390
state->eod = 1;
391
break;
392
}
393
394
else if (!iswhite(c))
395
{
396
fz_throw(ctx, FZ_ERROR_GENERIC, "bad data in a85d: '%c'", c);
397
}
398
}
399
400
stm->rp = state->buffer;
401
stm->wp = p;
402
stm->pos += p - state->buffer;
403
404
if (p == stm->rp)
405
return EOF;
406
407
return *stm->rp++;
408
}
409
410
static void
411
close_a85d(fz_context *ctx, void *state_)
412
{
413
fz_a85d *state = (fz_a85d *)state_;
414
fz_stream *chain = state->chain;
415
416
fz_free(ctx, state);
417
fz_drop_stream(ctx, chain);
418
}
419
420
fz_stream *
421
fz_open_a85d(fz_context *ctx, fz_stream *chain)
422
{
423
fz_a85d *state;
424
425
fz_try(ctx)
426
{
427
state = fz_malloc_struct(ctx, fz_a85d);
428
state->chain = chain;
429
state->eod = 0;
430
}
431
fz_catch(ctx)
432
{
433
fz_drop_stream(ctx, chain);
434
fz_rethrow(ctx);
435
}
436
437
return fz_new_stream(ctx, state, next_a85d, close_a85d);
438
}
439
440
/* Run Length Decode */
441
442
typedef struct fz_rld_s fz_rld;
443
444
struct fz_rld_s
445
{
446
fz_stream *chain;
447
int run, n, c;
448
unsigned char buffer[256];
449
};
450
451
static int
452
next_rld(fz_context *ctx, fz_stream *stm, int max)
453
{
454
fz_rld *state = stm->state;
455
unsigned char *p = state->buffer;
456
unsigned char *ep;
457
458
if (state->run == 128)
459
return EOF;
460
461
if (max > sizeof(state->buffer))
462
max = sizeof(state->buffer);
463
ep = p + max;
464
465
while (p < ep)
466
{
467
if (state->run == 128)
468
break;
469
470
if (state->n == 0)
471
{
472
state->run = fz_read_byte(ctx, state->chain);
473
if (state->run < 0)
474
{
475
state->run = 128;
476
break;
477
}
478
if (state->run < 128)
479
state->n = state->run + 1;
480
if (state->run > 128)
481
{
482
state->n = 257 - state->run;
483
state->c = fz_read_byte(ctx, state->chain);
484
if (state->c < 0)
485
fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of data in run length decode");
486
}
487
}
488
489
if (state->run < 128)
490
{
491
while (p < ep && state->n)
492
{
493
int c = fz_read_byte(ctx, state->chain);
494
if (c < 0)
495
fz_throw(ctx, FZ_ERROR_GENERIC, "premature end of data in run length decode");
496
*p++ = c;
497
state->n--;
498
}
499
}
500
501
if (state->run > 128)
502
{
503
while (p < ep && state->n)
504
{
505
*p++ = state->c;
506
state->n--;
507
}
508
}
509
}
510
511
stm->rp = state->buffer;
512
stm->wp = p;
513
stm->pos += p - state->buffer;
514
515
if (p == stm->rp)
516
return EOF;
517
518
return *stm->rp++;
519
}
520
521
static void
522
close_rld(fz_context *ctx, void *state_)
523
{
524
fz_rld *state = (fz_rld *)state_;
525
fz_stream *chain = state->chain;
526
527
fz_free(ctx, state);
528
fz_drop_stream(ctx, chain);
529
}
530
531
fz_stream *
532
fz_open_rld(fz_context *ctx, fz_stream *chain)
533
{
534
fz_rld *state;
535
536
fz_try(ctx)
537
{
538
state = fz_malloc_struct(ctx, fz_rld);
539
state->chain = chain;
540
state->run = 0;
541
state->n = 0;
542
state->c = 0;
543
}
544
fz_catch(ctx)
545
{
546
fz_drop_stream(ctx, chain);
547
fz_rethrow(ctx);
548
}
549
550
return fz_new_stream(ctx, state, next_rld, close_rld);
551
}
552
553
/* RC4 Filter */
554
555
typedef struct fz_arc4c_s fz_arc4c;
556
557
struct fz_arc4c_s
558
{
559
fz_stream *chain;
560
fz_arc4 arc4;
561
unsigned char buffer[256];
562
};
563
564
static int
565
next_arc4(fz_context *ctx, fz_stream *stm, int max)
566
{
567
fz_arc4c *state = stm->state;
568
int n = fz_available(ctx, state->chain, max);
569
570
if (n == 0)
571
return EOF;
572
if (n > sizeof(state->buffer))
573
n = sizeof(state->buffer);
574
575
stm->rp = state->buffer;
576
stm->wp = state->buffer + n;
577
fz_arc4_encrypt(&state->arc4, stm->rp, state->chain->rp, n);
578
state->chain->rp += n;
579
stm->pos += n;
580
581
return *stm->rp++;
582
}
583
584
static void
585
close_arc4(fz_context *ctx, void *state_)
586
{
587
fz_arc4c *state = (fz_arc4c *)state_;
588
fz_stream *chain = state->chain;
589
590
fz_free(ctx, state);
591
fz_drop_stream(ctx, chain);
592
}
593
594
fz_stream *
595
fz_open_arc4(fz_context *ctx, fz_stream *chain, unsigned char *key, unsigned keylen)
596
{
597
fz_arc4c *state;
598
599
fz_try(ctx)
600
{
601
state = fz_malloc_struct(ctx, fz_arc4c);
602
state->chain = chain;
603
fz_arc4_init(&state->arc4, key, keylen);
604
}
605
fz_catch(ctx)
606
{
607
fz_drop_stream(ctx, chain);
608
fz_rethrow(ctx);
609
}
610
611
return fz_new_stream(ctx, state, next_arc4, close_arc4);
612
}
613
614
/* AES Filter */
615
616
typedef struct fz_aesd_s fz_aesd;
617
618
struct fz_aesd_s
619
{
620
fz_stream *chain;
621
fz_aes aes;
622
unsigned char iv[16];
623
int ivcount;
624
unsigned char bp[16];
625
unsigned char *rp, *wp;
626
unsigned char buffer[256];
627
};
628
629
static int
630
next_aesd(fz_context *ctx, fz_stream *stm, int max)
631
{
632
fz_aesd *state = stm->state;
633
unsigned char *p = state->buffer;
634
unsigned char *ep;
635
636
if (max > sizeof(state->buffer))
637
max = sizeof(state->buffer);
638
ep = p + max;
639
640
while (state->ivcount < 16)
641
{
642
int c = fz_read_byte(ctx, state->chain);
643
if (c < 0)
644
fz_throw(ctx, FZ_ERROR_GENERIC, "premature end in aes filter");
645
state->iv[state->ivcount++] = c;
646
}
647
648
while (state->rp < state->wp && p < ep)
649
*p++ = *state->rp++;
650
651
while (p < ep)
652
{
653
int n = fz_read(ctx, state->chain, state->bp, 16);
654
if (n == 0)
655
break;
656
else if (n < 16)
657
fz_throw(ctx, FZ_ERROR_GENERIC, "partial block in aes filter");
658
659
aes_crypt_cbc(&state->aes, AES_DECRYPT, 16, state->iv, state->bp, state->bp);
660
state->rp = state->bp;
661
state->wp = state->bp + 16;
662
663
/* strip padding at end of file */
664
if (fz_is_eof(ctx, state->chain))
665
{
666
int pad = state->bp[15];
667
if (pad < 1 || pad > 16)
668
fz_throw(ctx, FZ_ERROR_GENERIC, "aes padding out of range: %d", pad);
669
state->wp -= pad;
670
}
671
672
while (state->rp < state->wp && p < ep)
673
*p++ = *state->rp++;
674
}
675
676
stm->rp = state->buffer;
677
stm->wp = p;
678
stm->pos += p - state->buffer;
679
680
if (p == stm->rp)
681
return EOF;
682
683
return *stm->rp++;
684
}
685
686
static void
687
close_aesd(fz_context *ctx, void *state_)
688
{
689
fz_aesd *state = (fz_aesd *)state_;
690
fz_stream *chain = state->chain;
691
692
fz_free(ctx, state);
693
fz_drop_stream(ctx, chain);
694
}
695
696
fz_stream *
697
fz_open_aesd(fz_context *ctx, fz_stream *chain, unsigned char *key, unsigned keylen)
698
{
699
fz_aesd *state = NULL;
700
701
fz_var(state);
702
703
fz_try(ctx)
704
{
705
state = fz_malloc_struct(ctx, fz_aesd);
706
state->chain = chain;
707
if (aes_setkey_dec(&state->aes, key, keylen * 8))
708
fz_throw(ctx, FZ_ERROR_GENERIC, "AES key init failed (keylen=%d)", keylen * 8);
709
state->ivcount = 0;
710
state->rp = state->bp;
711
state->wp = state->bp;
712
}
713
fz_catch(ctx)
714
{
715
fz_free(ctx, state);
716
fz_drop_stream(ctx, chain);
717
fz_rethrow(ctx);
718
}
719
720
return fz_new_stream(ctx, state, next_aesd, close_aesd);
721
}
722
723