-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpycpdfmodule.c
7122 lines (6728 loc) · 284 KB
/
pycpdfmodule.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
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
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
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
#define PY_SSIZE_T_CLEAN
#include <stddef.h>
#include "Python.h"
#include "structmember.h"
#define PYCPDF_VERSION "1.0.4"
#if PY_MAJOR_VERSION >= 3
#define STR_FROMFORMAT PyUnicode_FromFormat
#define FORMAT_BYTES "y#"
#define NUM_OBJ PyLongObject
#define NUM_TYPE PyLong_Type
#define NUM_CHECK PyLong_Check
#define NUM_ASLONG PyLong_AsLong
#define NUM_FROMLONG PyLong_FromLong
#else
#define STR_FROMFORMAT PyString_FromFormat
#define FORMAT_BYTES "s#"
#define NUM_OBJ PyIntObject
#define NUM_TYPE PyInt_Type
#define NUM_CHECK PyInt_Check
#define NUM_ASLONG PyInt_AS_LONG
#define NUM_FROMLONG PyInt_FromLong
#endif
static PyObject *baseerror = NULL;
static PyObject *pdferror = NULL;
static PyObject *notsupportederror = NULL;
static PyObject *badpassworderror = NULL;
static PyObject *encodings;
static PyObject *pdfdocencoding;
static PyObject *glyphlist;
static PyObject *unicode_translations;
static PyObject *md5;
static PyObject *decompressobj;
static PyObject *string_page;
static PyObject *string_prev;
static PyObject *string_emptystring;
static PyObject *string_newline;
static PyObject *string_unknown;
static PyObject *bytestr_emptystring;
static PyObject *name_inlineimage;
static PyTypeObject PDFType;
static PyTypeObject IndirectObjectType;
static PyTypeObject ArrayType;
typedef struct IndirectObject IndirectObject;
static PyObject *array_item(PyObject *self, Py_ssize_t i);
static PyObject *array_slice(PyObject *self, Py_ssize_t i1, Py_ssize_t i2);
static PyObject *array_subscript(PyObject *self, PyObject *key);
static PyObject *dictionary_subscript(PyObject *self, PyObject *key);
static PyObject *dictionary_get(PyObject *self, PyObject *args);
static PyObject *indirectobject_getObject(IndirectObject *self);
typedef struct {
PyUnicodeObject unicode;
} Name;
static PyTypeObject NameType = {
PyVarObject_HEAD_INIT(NULL, 0)
"pycpdf.Name", /*tp_name*/
sizeof(Name), /*tp_basicsize*/
0, /*tp_itemsize*/
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"PDF Name objects", /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
0, /*tp_methods*/
0, /*tp_members*/
0, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
0, /*tp_init*/
0, /*tp_alloc*/
0, /*tp_new*/
0, /*tp_free*/
0, /*tp_is_gc*/
};
typedef struct {
PyUnicodeObject unicode;
} Operator;
static PyTypeObject OperatorType = {
PyVarObject_HEAD_INIT(NULL, 0)
"pycpdf.Operator", /*tp_name*/
sizeof(Operator), /*tp_basicsize*/
0, /*tp_itemsize*/
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"PDF Operator objects", /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
0, /*tp_methods*/
0, /*tp_members*/
0, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
0, /*tp_init*/
0, /*tp_alloc*/
0, /*tp_new*/
0, /*tp_free*/
0, /*tp_is_gc*/
};
typedef struct {
PyUnicodeObject unicode;
PyObject *raw;
} PDFString;
static PyObject *pdfstring_new(PyTypeObject *type, PyObject *args,
PyObject *kwargs) {
PDFString *self;
self = (PDFString *)PyUnicode_Type.tp_new(type, args, kwargs);
if (self)
self->raw = NULL;
return (PyObject *)self;
}
static void pdfstring_dealloc(PDFString *self) {
Py_CLEAR(self->raw);
PyUnicode_Type.tp_dealloc((PyObject *)self);
}
static PyMemberDef pdfstring_members[] = {
{ "raw_bytes", T_OBJECT, offsetof(PDFString, raw), READONLY,
"The original bytestream from the PDF file" },
{ NULL }
};
static PyTypeObject PDFStringType = {
PyVarObject_HEAD_INIT(NULL, 0)
"pycpdf.PDFString", /*tp_name*/
sizeof(PDFString), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)pdfstring_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
"PDF String objects", /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
0, /*tp_methods*/
pdfstring_members, /*tp_members*/
0, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
0, /*tp_init*/
0, /*tp_alloc*/
pdfstring_new, /*tp_new*/
0, /*tp_free*/
0, /*tp_is_gc*/
};
static int md5update(PyObject *md5obj, const char *data, Py_ssize_t length) {
PyObject *obj;
if (!(obj = PyObject_CallMethod(md5obj, "update", FORMAT_BYTES, data,
length)))
return 0;
Py_DECREF(obj);
return 1;
}
static int md5update_str(PyObject *md5obj, PyObject *obj, int maxlen) {
PyObject *ret;
if (obj->ob_type == &PDFStringType) {
obj = ((PDFString *)obj)->raw;
} else if (!PyBytes_Check(obj)) {
PyErr_SetString(pdferror, "MD5 update value is not a string");
return 0;
}
if (maxlen) {
PyObject *slice;
if (!(slice = PySequence_GetSlice(obj, 0, maxlen)))
return 0;
ret = PyObject_CallMethod(md5obj, "update", "(O)", slice);
Py_DECREF(slice);
} else {
ret = PyObject_CallMethod(md5obj, "update", "(O)", obj);
}
if (!ret)
return 0;
Py_DECREF(ret);
return 1;
}
static void rc4(const char *key, long keylen, char *data, long datalen) {
int i, j, t;
unsigned char state[256];
unsigned char *cp;
for (i = 0; i < 256; ++i)
state[i] = i;
j = 0;
for (i = 0; i < 256; ++i) {
j = (j + state[i] + ((const unsigned char *)key)[i % keylen]) % 256;
t = state[i];
state[i] = state[j];
state[j] = t;
}
i = j = 0;
for (cp = (unsigned char *)data; cp < ((unsigned char *)data) + datalen;
cp++) {
i = (i + 1) % 256;
j = (j + state[i]) % 256;
t = state[i];
state[i] = state[j];
state[j] = t;
*cp ^= state[(state[i] + state[j]) % 256];
}
}
static int is_pdfwhitespace(char c) {
return c == '\x09' || c == '\x0a' || c == '\x0c' || c == '\x0d' || c == ' ';
}
static int is_pdfdelimiter(char c) {
return c == '(' || c == ')' || c == '<' || c == '>' || c == '[' ||
c == ']' || c == '{' || c == '}' || c == '/' || c == '%';
}
static const char *skipwhitespace(const char *cp, const char *end) {
while (cp < end) {
if (*cp == '%') {
cp++;
while (cp < end && *cp != '\r' && *cp != '\n')
cp++;
} else if (is_pdfwhitespace(*cp)) {
cp++;
} else {
return cp;
}
}
return cp;
}
static const char *previous_line(const char *pos, const char **eol,
const char *start) {
const char *cp = pos - 1;
const char *end;
while (cp > start && *cp != '\r' && *cp != '\n')
cp--;
while (cp > start && is_pdfwhitespace(*cp))
cp--;
if (cp <= start || cp >= pos - 1)
return NULL;
end = cp + 1;
while (cp > start && *cp != '\r' && *cp != '\n')
cp--;
cp++;
while (cp < end && is_pdfwhitespace(*cp))
cp++;
if (cp >= end)
return NULL;
if (eol)
*eol = end;
return cp;
}
static int read_integer(const char **start, const char *end, long *retval) {
const char *cp = *start;
char digits[32];
size_t len;
if (cp >= end)
return 0;
len = 0;
if (*cp == '+' || *cp == '-')
digits[len++] = *(cp++);
while (cp < end && *cp >= '0' && *cp <= '9') {
if (len >= sizeof(digits) - 1)
return 0;
digits[len++] = *(cp++);
}
if (cp < end && !is_pdfwhitespace(*cp) && !is_pdfdelimiter(*cp))
return 0;
digits[len] = 0;
errno = 0;
*retval = strtol(digits, (char **)&cp, 10);
if (errno || *cp)
return 0;
*start += cp - digits;
return 1;
}
static int read_twointegers(const char **start, const char *end, long *num1p,
long *num2p, const char *keyword) {
const char *cp;
int keylen = keyword ? strlen(keyword) : 0;
cp = skipwhitespace(*start, end);
if (cp >= end || *cp < '0' || *cp > '9')
return 0;
while (cp < end && *cp >= '0' && *cp <= '9')
cp++;
cp = skipwhitespace(cp, end);
if (cp >= end || *cp < '0' || *cp > '9')
return 0;
while (cp < end && *cp >= '0' && *cp <= '9')
cp++;
cp = skipwhitespace(cp, end);
if (keyword) {
if (cp + keylen > end || memcmp(cp, keyword, keylen))
return 0;
if (cp + keylen < end) {
if (!is_pdfwhitespace(cp[keylen]) && !is_pdfdelimiter(cp[keylen]))
return 0;
}
}
cp = skipwhitespace(*start, end);
if (!read_integer(&cp, end, num1p))
return 0;
cp = skipwhitespace(cp, end);
if (!read_integer(&cp, end, num2p))
return 0;
*start = skipwhitespace(cp, end) + keylen;
return 1;
}
static int read_float(const char **start, const char *end, double *retval) {
const char *cp = *start;
char digits[64];
size_t len;
for (len = 0; len < sizeof(digits) - 1 && cp < end &&
((*cp >= '0' && *cp <= '9') || *cp == '+' || *cp == '-' || *cp == '.');
len++)
digits[len] = *(cp++);
if (cp < end && !is_pdfwhitespace(*cp) && !is_pdfdelimiter(*cp))
return 0;
digits[len] = 0;
errno = 0;
*retval = strtod(digits, (char **)&cp);
if (errno || *cp)
return 0;
*start += cp - digits;
return 1;
}
static char decodexdigit(char c) {
if (c >= 'a' && c <= 'f')
return 10 + (c - 'a');
if (c >= 'A' && c <= 'F')
return 10 + (c - 'A');
if (c >= '0' && c <= '9')
return c - '0';
return 16;
}
typedef struct {
PyListObject list;
} Array;
static PyMappingMethods array_as_mapping = {
NULL, /*mp_length*/
array_subscript, /*mp_subscript*/
NULL, /*mp_ass_subscript*/
};
static PySequenceMethods array_as_sequence = {
NULL, /*sq_length*/
NULL, /*sq_concat*/
NULL, /*sq_repeat*/
(ssizeargfunc)array_item, /*sq_item*/
(ssizessizeargfunc)array_slice, /*sq_slice*/
NULL, /*sq_ass_slice*/
NULL, /*sq_ass_item*/
NULL, /*sq_contains*/
NULL, /*sq_inplace_concat*/
NULL, /*sq_inplace_repeat*/
};
static PyMethodDef array_methods[] = {
/*{ "get", (PyCFunction)dictionary_get, METH_VARARGS,
"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None." },*/
{ NULL }
};
static PyObject *array_iter(PyObject *self);
static PyTypeObject ArrayType = {
PyVarObject_HEAD_INIT(NULL, 0)
"pycpdf.Array", /*tp_name*/
sizeof(Array), /*tp_basicsize*/
0, /*tp_itemsize*/
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
&array_as_sequence, /*tp_as_sequence*/
&array_as_mapping, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
"PDF Array objects", /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
array_iter, /*tp_iter*/
0, /*tp_iternext*/
array_methods, /*tp_methods*/
0, /*tp_members*/
0, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
0, /*tp_init*/
0, /*tp_alloc*/
0, /*tp_new*/
0, /*tp_free*/
0, /*tp_is_gc*/
};
static PyObject *array_item(PyObject *self, Py_ssize_t i) {
PyObject *obj;
if (!(obj = PyList_GetItem(self, i)))
return NULL;
if (obj->ob_type == &IndirectObjectType) {
if (!(obj = indirectobject_getObject((IndirectObject *)obj)))
return NULL;
if (PyList_SetItem(self, i, obj) < 0) {
Py_DECREF(obj);
return NULL;
}
}
Py_INCREF(obj);
return obj;
}
static PyObject *array_slice(PyObject *self, Py_ssize_t i1, Py_ssize_t i2) {
PyObject *result;
Py_ssize_t i;
Py_ssize_t len;
len = PyList_Size(self);
if (i1 < 0)
i1 = 0;
else if (i1 > len)
i1 = len;
if (i2 < i1)
i2 = i1;
else if (i2 > len)
i2 = len;
if (!(result = PyList_New(i2 - i1)))
return NULL;
for (i = 0; i < i2 - i1; i++) {
PyObject *obj;
if (!(obj = array_item(self, i1 + i))) {
Py_DECREF(result);
return NULL;
}
PyList_SET_ITEM(result, i, obj);
}
return result;
}
static PyObject *array_subscript(PyObject *self, PyObject *key) {
Py_ssize_t i;
if (!PyIndex_Check(key))
return PyErr_Format(PyExc_TypeError, "array index is a %s not an integer",
key->ob_type->tp_name);
if ((i = PyNumber_AsSsize_t(key, PyExc_IndexError)) == -1 &&
PyErr_Occurred())
return NULL;
return array_item(self, i < 0 ? i + PyList_Size(self) : i);
}
typedef struct {
PyObject_HEAD
Py_ssize_t index;
PyObject *array;
} ArrayIterator;
static void arrayiterator_clear(ArrayIterator *self) {
Py_CLEAR(self->array);
}
static int arrayiterator_traverse(ArrayIterator *self, visitproc visit,
void *arg) {
Py_VISIT(self->array);
return 0;
}
static void arrayiterator_dealloc(ArrayIterator *self) {
arrayiterator_clear(self);
((PyObject *)self)->ob_type->tp_free((PyObject *)self);
}
static PyObject *arrayiterator_next(ArrayIterator *self) {
PyObject *obj;
Py_ssize_t len;
if (!self->array)
return NULL;
if ((len = PySequence_Length(self->array)) < 0)
return NULL;
if (self->index >= len) {
Py_CLEAR(self->array);
return NULL;
}
if (!(obj = PySequence_GetItem(self->array, self->index)))
return NULL;
self->index++;
return obj;
}
static PyTypeObject ArrayIteratorType = {
PyVarObject_HEAD_INIT(NULL, 0)
"pycpdf.ArrayIterator", /*tp_name*/
sizeof(ArrayIterator), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)arrayiterator_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
"Iterator over Array objects", /*tp_doc*/
(traverseproc)arrayiterator_traverse, /*tp_traverse*/
(inquiry)arrayiterator_clear, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
PyObject_SelfIter, /*tp_iter*/
(iternextfunc)arrayiterator_next, /*tp_iternext*/
0, /*tp_methods*/
0, /*tp_members*/
0, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
0, /*tp_init*/
0, /*tp_alloc*/
0, /*tp_new*/
0, /*tp_free*/
0, /*tp_is_gc*/
};
static PyObject *array_iter(PyObject *self) {
ArrayIterator *iter;
if (!(iter = (ArrayIterator *)ArrayIteratorType.tp_alloc(
&ArrayIteratorType, 0)))
return NULL;
iter->index = 0;
Py_INCREF(self);
iter->array = self;
return (PyObject *)iter;
}
typedef struct {
PyDictObject dict;
} Dictionary;
static PyMappingMethods dictionary_as_mapping = {
NULL, /*mp_length*/
dictionary_subscript, /*mp_subscript*/
NULL, /*mp_ass_subscript*/
};
static PyMethodDef dictionary_methods[] = {
{ "get", (PyCFunction)dictionary_get, METH_VARARGS,
"D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None." },
{ NULL }
};
static PyTypeObject DictionaryType = {
PyVarObject_HEAD_INIT(NULL, 0)
"pycpdf.Dictionary", /*tp_name*/
sizeof(Dictionary), /*tp_basicsize*/
0, /*tp_itemsize*/
0, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
&dictionary_as_mapping, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
"PDF Dictionary objects", /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
dictionary_methods, /*tp_methods*/
0, /*tp_members*/
0, /*tp_getset*/
0, /*tp_base*/
0, /*tp_dict*/
0, /*tp_descr_get*/
0, /*tp_descr_set*/
0, /*tp_dictoffset*/
0, /*tp_init*/
0, /*tp_alloc*/
0, /*tp_new*/
0, /*tp_free*/
0, /*tp_is_gc*/
};
static PyObject *dictionary_subscript(PyObject *self, PyObject *key) {
PyObject *obj;
if (!(obj = PyDict_GetItem(self, key))) {
PyObject *arg = PyTuple_Pack(1, key);
if (arg) {
PyErr_SetObject(PyExc_KeyError, arg);
Py_DECREF(arg);
}
return NULL;
}
if (obj->ob_type == &IndirectObjectType) {
if (!(obj = indirectobject_getObject((IndirectObject *)obj)))
return NULL;
if (PyDict_SetItem(self, key, obj) < 0)
return NULL;
} else {
Py_INCREF(obj);
}
return obj;
}
static PyObject *dictionary_get(PyObject *self, PyObject *args) {
PyObject *key;
PyObject *failobj = Py_None;
int check;
if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
return NULL;
if ((check = PyDict_Contains(self, key)) < 0)
return NULL;
if (!check) {
Py_INCREF(failobj);
return failobj;
}
return dictionary_subscript(self, key);
}
typedef struct {
PyObject_HEAD
PyObject *weakreflist;
PyObject *source;
PyObject *version;
PyObject *xref;
PyObject *trailer;
PyObject *key;
PyObject *catalog;
PyObject *info;
PyObject *linearized;
PyObject *pages;
Py_buffer data;
} PDF;
static PyObject *process_filter(PDF *self, PyObject *value, PyObject *name,
PyObject *parms, long *usedbytes);
static PyObject *decode_string(const char *string, Py_ssize_t len,
PyObject *decryption_key, char unicode);
static PyObject *parse_contents(PDF *self, PyObject *contents);
Py_GCC_ATTRIBUTE((format(printf, 3, 4)))
static PyObject *pdf_error(PDF *self, const char *cp, const char *format,
...) {
char msgbuf[1000];
va_list argptr;
va_start(argptr, format);
vsnprintf(msgbuf, sizeof(msgbuf) - 100, format, argptr);
msgbuf[sizeof(msgbuf) - 100] = '\0';
if (cp && self && self->data.buf && cp >= (const char *)self->data.buf &&
cp < (((const char *)self->data.buf) + self->data.len))
sprintf(msgbuf + strlen(msgbuf), " at file offset %ld",
(long)(cp - (const char *)self->data.buf));
PyErr_SetString(pdferror, msgbuf);
return NULL;
}
static int get_intval(PyObject *dict, const char *key, long *retval) {
PyObject *obj;
if (!(obj = PyMapping_GetItemString(dict, (char *)key)))
return 0;
if (!NUM_CHECK(obj)) {
pdf_error(NULL, NULL, "Dictionary %s value is a %s not an integer", key,
obj->ob_type->tp_name);
Py_DECREF(obj);
return 0;
}
*retval = NUM_ASLONG(obj);
Py_DECREF(obj);
return (*retval != -1 || !PyErr_Occurred());
}
typedef struct {
Dictionary dict;
PDF *pdf;
PyObject *data;
PyObject *contents;
int decoded;
} StreamObject;
static int streamobject_clear(StreamObject *self) {
Py_CLEAR(self->pdf);
Py_CLEAR(self->data);
Py_CLEAR(self->contents);
return PyDict_Type.tp_clear((PyObject *)self);
}
static int streamobject_traverse(StreamObject *self, visitproc visit,
void *arg) {
Py_VISIT(self->pdf);
Py_VISIT(self->data);
Py_VISIT(self->contents);
return PyDict_Type.tp_traverse((PyObject *)self, visit, arg);
}
static void streamobject_dealloc(StreamObject *self) {
streamobject_clear(self);
PyDict_Type.tp_dealloc((PyObject *)self);
}
static PyObject *streamobject_new(PyTypeObject *type, PyObject *args,
PyObject *kwargs) {
StreamObject *self;
PDF *pdf;
PyObject *dict;
PyObject *data;
PyObject *dictargs;
if (!PyArg_ParseTuple(args, "O!OO:StreamObject", &PDFType, &pdf, &dict,
&data))
return NULL;
if (!(dictargs = Py_BuildValue("(O)", dict)))
return NULL;
self = (StreamObject *)DictionaryType.tp_new(type, dictargs, kwargs);
Py_DECREF(dictargs);
if (!self)
return NULL;
self->contents = NULL;
Py_INCREF(pdf);
self->pdf = pdf;
Py_INCREF(data);
self->data = data;
self->decoded = !PyMapping_HasKeyString(dict, "Filter");
return (PyObject *)self;
}
static int streamobject_init(StreamObject *self, PyObject *args,
PyObject *kwargs) {
PDF *pdf;
PyObject *dict;
PyObject *data;
PyObject *dictargs;
int retval;
if (!PyArg_ParseTuple(args, "O!OO:StreamObject", &PDFType, &pdf, &dict,
&data))
return -1;
if (!(dictargs = Py_BuildValue("(O)", dict)))
return -1;
retval = DictionaryType.tp_init((PyObject *)self, dictargs, kwargs);
Py_DECREF(dictargs);
return retval;
}
static PyObject *read_filtered_data(PDF *self, PyObject *value,
PyObject *filter, PyObject *parms, long *length) {
PyObject *name;
PyObject *result;
PyObject *obj;
PyObject *iterator;
long len;
if (value->ob_type == &PDFStringType)
value = ((PDFString *)value)->raw;
if (!PyBytes_Check(value))
return pdf_error(self, NULL, "Filtered data is a %s not a string",
value->ob_type->tp_name);
if (!length)
length = &len;
if (filter->ob_type == &NameType)
return process_filter(self, value, filter, parms, length);
else if (filter->ob_type != &ArrayType)
return pdf_error(self, NULL, "Stream Filter value is a %s"
" not a pycpdf.Array or a pycpdf.Name", filter->ob_type->tp_name);
if (!(iterator = PyObject_GetIter(filter)))
return NULL;
Py_INCREF(value);
result = value;
while ((name = PyIter_Next(iterator))) {
if (name->ob_type != &NameType) {
pdf_error(self, NULL,
"Stream Filter array value is a %s not a pycpdf.Name",
name->ob_type->tp_name);
Py_DECREF(name);
Py_DECREF(iterator);
Py_DECREF(result);
return NULL;
}
if (!(obj = process_filter(self, result, name, parms,
result == value ? length : &len))) {
Py_DECREF(name);
Py_DECREF(result);
Py_DECREF(iterator);
return NULL;
}
Py_DECREF(result);
result = obj;
Py_DECREF(name);
}
Py_DECREF(iterator);
if (PyErr_Occurred()) {
Py_DECREF(result);
return NULL;
}
return result;
}
static PyObject *streamobject_get_data(StreamObject *self, void *closure) {
if (!self->decoded && PyMapping_HasKeyString((PyObject *)self, "Filter")) {
PyObject *name;
PyObject *parms;
PyObject *obj;
PyObject *value;
char *buffer;
Py_ssize_t buflen;
if (!(name = PyMapping_GetItemString((PyObject *)self, "Filter")))
return NULL;
if (PyMapping_HasKeyString((PyObject *)self, "DecodeParms")) {
if (!(parms = PyMapping_GetItemString(
(PyObject *)self, "DecodeParms"))) {
Py_DECREF(name);
return NULL;
}
} else {
parms = Py_None;
Py_INCREF(Py_None);
}
value = read_filtered_data(self->pdf, self->data, name, parms, NULL);
Py_DECREF(parms);
Py_DECREF(name);
if (!value)
return NULL;