-
Notifications
You must be signed in to change notification settings - Fork 7
/
pdf_prim.c
733 lines (569 loc) · 16.2 KB
/
pdf_prim.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
/*
* tumble: build a PDF file from image files
*
* PDF routines
* Copyright 2001, 2002, 2003, 2017 Eric Smith <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. Note that permission is
* not granted to redistribute this program under the terms of any
* other version of the General Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*
* 2007-05-07 [JDB] Allow embedded nulls in strings by storing as character
* arrays plus length words.
*/
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bitblt.h"
#include "pdf.h"
#include "pdf_util.h"
#include "pdf_prim.h"
#include "pdf_private.h"
struct pdf_array_elem
{
struct pdf_array_elem *next;
pdf_obj_handle val;
};
struct pdf_array
{
struct pdf_array_elem *first;
struct pdf_array_elem *last;
};
struct pdf_dict_entry
{
struct pdf_dict_entry *next;
char *key;
pdf_obj_handle val;
};
struct pdf_dict
{
struct pdf_dict_entry *first;
};
struct pdf_stream
{
pdf_obj_handle stream_dict;
pdf_obj_handle length;
pdf_stream_write_callback callback;
void *app_data; /* arg to pass to callback */
pdf_obj_handle filters; /* name or array of names */
pdf_obj_handle decode_parms;
};
struct pdf_obj
{
/* these fields only apply to indirectly referenced objects */
pdf_obj_handle prev;
pdf_obj_handle next;
unsigned long obj_num;
unsigned long obj_gen;
long int file_offset;
/* these fields apply to all objects */
unsigned long ref_count;
pdf_obj_type type;
union {
bool boolean;
char *name;
struct {
char *content;
int length;
} string;
long integer;
double real;
pdf_obj_handle ind_ref;
struct pdf_dict dict;
struct pdf_array array;
struct pdf_stream stream;
} val;
};
pdf_obj_handle ref (pdf_obj_handle obj)
{
obj->ref_count++;
return (obj);
}
void unref (pdf_obj_handle obj)
{
if ((--obj->ref_count) == 0)
{
/* $$$ free the object */
}
}
pdf_obj_handle pdf_deref_ind_obj (pdf_obj_handle ind_obj)
{
pdf_assert (ind_obj->type == PT_IND_REF);
return (ind_obj->val.ind_ref);
}
void pdf_set_dict_entry (pdf_obj_handle dict_obj, char *key, pdf_obj_handle val)
{
struct pdf_dict_entry *entry;
if (dict_obj->type == PT_IND_REF)
dict_obj = pdf_deref_ind_obj (dict_obj);
pdf_assert (dict_obj->type == PT_DICTIONARY);
/* replacing existing entry? */
for (entry = dict_obj->val.dict.first; entry; entry = entry->next)
if (strcmp (entry->key, key) == 0)
{
unref (entry->val);
entry->val = ref (val);
return;
}
/* new entry */
entry = pdf_calloc (1, sizeof (struct pdf_dict_entry));
entry->next = dict_obj->val.dict.first;
dict_obj->val.dict.first = entry;
entry->key = pdf_strdup (key);
entry->val = ref (val);
}
pdf_obj_handle pdf_get_dict_entry (pdf_obj_handle dict_obj, char *key)
{
struct pdf_dict_entry *entry;
if (dict_obj->type == PT_IND_REF)
dict_obj = pdf_deref_ind_obj (dict_obj);
pdf_assert (dict_obj->type == PT_DICTIONARY);
for (entry = dict_obj->val.dict.first; entry; entry = entry->next)
if (strcmp (entry->key, key) == 0)
return (entry->val);
return (NULL);
}
void pdf_add_array_elem (pdf_obj_handle array_obj, pdf_obj_handle val)
{
struct pdf_array_elem *elem = pdf_calloc (1, sizeof (struct pdf_array_elem));
if (array_obj->type == PT_IND_REF)
array_obj = pdf_deref_ind_obj (array_obj);
pdf_assert (array_obj->type == PT_ARRAY);
elem->val = ref (val);
if (! array_obj->val.array.first)
array_obj->val.array.first = elem;
else
array_obj->val.array.last->next = elem;
array_obj->val.array.last = elem;
}
void pdf_add_array_elem_unique (pdf_obj_handle array_obj, pdf_obj_handle val)
{
struct pdf_array_elem *elem;
if (array_obj->type == PT_IND_REF)
array_obj = pdf_deref_ind_obj (array_obj);
pdf_assert (array_obj->type == PT_ARRAY);
for (elem = array_obj->val.array.first; elem; elem = elem->next)
if (pdf_compare_obj (val, elem->val) == 0)
return;
elem = pdf_calloc (1, sizeof (struct pdf_array_elem));
elem->val = ref (val);
if (! array_obj->val.array.first)
array_obj->val.array.first = elem;
else
array_obj->val.array.last->next = elem;
array_obj->val.array.last = elem;
}
pdf_obj_handle pdf_new_obj (pdf_obj_type type)
{
pdf_obj_handle obj = pdf_calloc (1, sizeof (struct pdf_obj));
obj->type = type;
return (obj);
}
pdf_obj_handle pdf_new_bool (bool val)
{
pdf_obj_handle obj = pdf_new_obj (PT_BOOL);
obj->val.boolean = val;
return (obj);
}
pdf_obj_handle pdf_new_name (char *name)
{
pdf_obj_handle obj = pdf_new_obj (PT_NAME);
obj->val.name = pdf_strdup (name);
return (obj);
}
pdf_obj_handle pdf_new_string (char *str)
{
pdf_obj_handle obj = pdf_new_obj (PT_STRING);
obj->val.string.content = pdf_strdup (str);
obj->val.string.length = strlen(str);
return (obj);
}
pdf_obj_handle pdf_new_string_n (char *str, int n)
{
pdf_obj_handle obj = pdf_new_obj (PT_STRING);
obj->val.string.length = n;
obj->val.string.content = pdf_calloc (1,n);
memcpy(obj->val.string.content, str, n);
return (obj);
}
pdf_obj_handle pdf_new_integer (long val)
{
pdf_obj_handle obj = pdf_new_obj (PT_INTEGER);
obj->val.integer = val;
return (obj);
}
pdf_obj_handle pdf_new_real (double val)
{
pdf_obj_handle obj = pdf_new_obj (PT_REAL);
obj->val.real = val;
return (obj);
}
pdf_obj_handle pdf_new_stream (pdf_file_handle pdf_file,
pdf_obj_handle stream_dict,
pdf_stream_write_callback callback,
void *app_data)
{
pdf_obj_handle obj = pdf_new_obj (PT_STREAM);
obj->val.stream.stream_dict = stream_dict;
obj->val.stream.length = pdf_new_ind_ref (pdf_file, pdf_new_integer (0));
pdf_set_dict_entry (obj->val.stream.stream_dict, "Length", obj->val.stream.length);
obj->val.stream.callback = callback;
obj->val.stream.app_data = app_data;
return (obj);
}
/* $$$ currently limited to one filter per stream */
void pdf_stream_add_filter (pdf_obj_handle stream,
char *filter_name,
pdf_obj_handle decode_parms)
{
if (stream->type == PT_IND_REF)
stream = pdf_deref_ind_obj (stream);
pdf_assert (stream->type == PT_STREAM);
pdf_set_dict_entry (stream->val.stream.stream_dict, "Filter", pdf_new_name (filter_name));
if (decode_parms)
pdf_set_dict_entry (stream->val.stream.stream_dict, "DecodeParms", decode_parms);
}
pdf_obj_handle pdf_new_ind_ref (pdf_file_handle pdf_file, pdf_obj_handle obj)
{
pdf_obj_handle ind_obj;
pdf_assert (obj->type != PT_IND_REF);
ind_obj = pdf_new_obj (PT_IND_REF);
ind_obj->type = PT_IND_REF;
ind_obj->val.ind_ref = obj;
/* is there already an indirect reference to this object? */
if (! obj->obj_num)
{
/* no, assign object number/generation and add to linked list */
if (! pdf_file->first_ind_obj)
{
obj->obj_num = 1;
pdf_file->first_ind_obj = pdf_file->last_ind_obj = obj;
}
else
{
obj->obj_num = pdf_file->last_ind_obj->obj_num + 1;
pdf_file->last_ind_obj->next = obj;
obj->prev = pdf_file->last_ind_obj;
pdf_file->last_ind_obj = obj;
}
}
return (ind_obj);
}
long pdf_get_integer (pdf_obj_handle obj)
{
if (obj->type == PT_IND_REF)
obj = pdf_deref_ind_obj (obj);
pdf_assert (obj->type == PT_INTEGER);
return (obj->val.integer);
}
void pdf_set_integer (pdf_obj_handle obj, long val)
{
if (obj->type == PT_IND_REF)
obj = pdf_deref_ind_obj (obj);
pdf_assert (obj->type == PT_INTEGER);
obj->val.integer = val;
}
double pdf_get_real (pdf_obj_handle obj)
{
if (obj->type == PT_IND_REF)
obj = pdf_deref_ind_obj (obj);
pdf_assert (obj->type == PT_REAL);
return (obj->val.real);
}
void pdf_set_real (pdf_obj_handle obj, double val)
{
if (obj->type == PT_IND_REF)
obj = pdf_deref_ind_obj (obj);
pdf_assert (obj->type == PT_REAL);
obj->val.real = val;
}
int pdf_compare_obj (pdf_obj_handle o1, pdf_obj_handle o2)
{
if (o1->type == PT_IND_REF)
o1 = pdf_deref_ind_obj (o1);
if (o2->type == PT_IND_REF)
o2 = pdf_deref_ind_obj (o2);
pdf_assert (o1->type == o2->type);
switch (o1->type)
{
case PT_INTEGER:
if (o1->val.integer < o2->val.integer)
return (-1);
if (o1->val.integer > o2->val.integer)
return (1);
return (0);
case PT_REAL:
if (o1->val.real < o2->val.real)
return (-1);
if (o1->val.real > o2->val.real)
return (1);
return (0);
case PT_STRING:
{
int l;
l = o1->val.string.length;
if(l > o2->val.string.length)
l = o2->val.string.length;
l = memcmp (o1->val.string.content, o2->val.string.content, l);
if (l)
return l;
return o1->val.string.length - o2->val.string.length;
}
case PT_NAME:
return (strcmp (o1->val.name, o2->val.name));
default:
pdf_fatal ("invalid object type for comparison\n");
}
}
static int name_char_needs_quoting (char c)
{
return ((c < '!') || (c > '~') || (c == '/') || (c == '\\') ||
(c == '(') || (c == ')') || (c == '<') || (c == '>') ||
(c == '[') || (c == ']') || (c == '{') || (c == '}') ||
(c == '%'));
}
void pdf_write_name (pdf_file_handle pdf_file, char *s)
{
fprintf (pdf_file->f, "/");
while (*s)
if (name_char_needs_quoting (*s))
fprintf (pdf_file->f, "#%02x", 0xff & *(s++));
else
fprintf (pdf_file->f, "%c", *(s++));
fprintf (pdf_file->f, " ");
}
static int pdf_write_literal_string (pdf_file_handle pdf_file, char *s, int n)
{
int i, p;
if (pdf_file)
fprintf (pdf_file->f, "(");
for (i = p = 0; n; n--)
{
int j, k;
k = 0;
switch (*s)
{
case '\\':
k = 1;
break;
case '(':
for (j = k =1; k && j < n; j++)
k += (s[j] == '(') ? 1 : (s[j] == ')') ? -1 : 0;
p += !k;
break;
case ')':
if (p)
p--;
else
k = 1;
break;
}
if (k)
{
i++;
if (pdf_file)
fprintf (pdf_file->f, "\\");
}
i++;
if (pdf_file)
fprintf (pdf_file->f, "%c", *(s++));
}
if (pdf_file)
fprintf (pdf_file->f, ") ");
return i;
}
void pdf_write_string (pdf_file_handle pdf_file, char *s, int n)
{
if (pdf_write_literal_string (NULL, s, n) < 2 * n)
pdf_write_literal_string (pdf_file, s, n);
else
{
fprintf (pdf_file->f, "<");
for( ; n--; )
fprintf (pdf_file->f, "%.2X",*(s++));
fprintf (pdf_file->f, "> ");
}
}
void pdf_write_real (pdf_file_handle pdf_file, double num)
{
/* $$$ not actually good enough, precision needs to be variable,
and no exponent is allowed */
fprintf (pdf_file->f, "%0f ", num);
}
void pdf_write_ind_ref (pdf_file_handle pdf_file, pdf_obj_handle ind_obj)
{
pdf_obj_handle obj = pdf_deref_ind_obj (ind_obj);
fprintf (pdf_file->f, "%ld %ld R ", obj->obj_num, obj->obj_gen);
}
void pdf_write_array (pdf_file_handle pdf_file, pdf_obj_handle array_obj)
{
struct pdf_array_elem *elem;
pdf_assert (array_obj->type == PT_ARRAY);
fprintf (pdf_file->f, "[ ");
for (elem = array_obj->val.array.first; elem; elem = elem->next)
{
pdf_write_obj (pdf_file, elem->val);
fprintf (pdf_file->f, " ");
}
fprintf (pdf_file->f, "] ");
}
void pdf_write_dict (pdf_file_handle pdf_file, pdf_obj_handle dict_obj)
{
struct pdf_dict_entry *entry;
pdf_assert (dict_obj->type == PT_DICTIONARY);
fprintf (pdf_file->f, "<<\r\n");
for (entry = dict_obj->val.dict.first; entry; entry = entry->next)
{
pdf_write_name (pdf_file, entry->key);
fprintf (pdf_file->f, " ");
pdf_write_obj (pdf_file, entry->val);
fprintf (pdf_file->f, "\r\n");
}
fprintf (pdf_file->f, ">>\r\n");
}
void pdf_stream_write_data (pdf_file_handle pdf_file,
pdf_obj_handle stream,
char *data,
unsigned long len)
{
while (len)
{
unsigned long l2 = fwrite (data, 1, len, pdf_file->f);
data += l2;
len -= l2;
if (ferror (pdf_file->f))
pdf_fatal ("error writing stream data\n");
}
}
void pdf_stream_printf (pdf_file_handle pdf_file,
pdf_obj_handle stream,
char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
vfprintf (pdf_file->f, fmt, ap);
va_end (ap);
}
void pdf_write_stream (pdf_file_handle pdf_file, pdf_obj_handle stream)
{
unsigned long begin_pos, end_pos;
pdf_assert (stream->type == PT_STREAM);
pdf_write_dict (pdf_file, stream->val.stream.stream_dict);
fprintf (pdf_file->f, "stream\r\n");
begin_pos = ftell (pdf_file->f);
stream->val.stream.callback (pdf_file,
stream,
stream->val.stream.app_data);
end_pos = ftell (pdf_file->f);
fprintf (pdf_file->f, "\r\nendstream\r\n");
pdf_set_integer (stream->val.stream.length, end_pos - begin_pos);
}
void pdf_write_obj (pdf_file_handle pdf_file, pdf_obj_handle obj)
{
switch (obj->type)
{
case PT_NULL:
fprintf (pdf_file->f, "null ");
break;
case PT_BOOL:
if (obj->val.boolean)
fprintf (pdf_file->f, "true ");
else
fprintf (pdf_file->f, "false ");
break;
case PT_NAME:
pdf_write_name (pdf_file, obj->val.name);
break;
case PT_STRING:
pdf_write_string (pdf_file, obj->val.string.content, obj->val.string.length);
break;
case PT_INTEGER:
fprintf (pdf_file->f, "%ld ", obj->val.integer);
break;
case PT_REAL:
pdf_write_real (pdf_file, obj->val.real);
break;
case PT_IND_REF:
pdf_write_ind_ref (pdf_file, obj);
break;
case PT_DICTIONARY:
pdf_write_dict (pdf_file, obj);
break;
case PT_ARRAY:
pdf_write_array (pdf_file, obj);
break;
case PT_STREAM:
pdf_write_stream (pdf_file, obj);
break;
default:
pdf_fatal ("bad object type\n");
}
}
void pdf_write_ind_obj (pdf_file_handle pdf_file, pdf_obj_handle ind_obj)
{
pdf_obj_handle obj;
if (ind_obj->type == PT_IND_REF)
obj = pdf_deref_ind_obj (ind_obj);
else
obj = ind_obj;
obj->file_offset = ftell (pdf_file->f);
fprintf (pdf_file->f, "%ld %ld obj\r\n", obj->obj_num, obj->obj_gen);
pdf_write_obj (pdf_file, obj);
fprintf (pdf_file->f, "endobj\r\n");
}
void pdf_write_all_ind_obj (pdf_file_handle pdf_file)
{
pdf_obj_handle ind_obj;
for (ind_obj = pdf_file->first_ind_obj; ind_obj; ind_obj = ind_obj->next)
if (! ind_obj->file_offset)
pdf_write_ind_obj (pdf_file, ind_obj);
}
unsigned long pdf_write_xref (pdf_file_handle pdf_file)
{
pdf_obj_handle ind_obj;
pdf_file->xref_offset = ftell (pdf_file->f);
fprintf (pdf_file->f, "xref\r\n");
fprintf (pdf_file->f, "0 %ld\r\n", pdf_file->last_ind_obj->obj_num + 1);
fprintf (pdf_file->f, "0000000000 65535 f\r\n");
for (ind_obj = pdf_file->first_ind_obj; ind_obj; ind_obj = ind_obj->next)
fprintf (pdf_file->f, "%010ld 00000 n\r\n", ind_obj->file_offset);
return (pdf_file->last_ind_obj->obj_num + 1);
}
/* this isn't really a PDF primitive data type */
char pdf_new_XObject (pdf_page_handle pdf_page, pdf_obj_handle ind_ref)
{
char XObject_name [4] = "Im ";
XObject_name [2] = ++pdf_page->last_XObject_name;
if (! pdf_page->XObject_dict)
{
pdf_page->XObject_dict = pdf_new_obj (PT_DICTIONARY);
pdf_set_dict_entry (pdf_page->resources, "XObject", pdf_page->XObject_dict);
}
pdf_set_dict_entry (pdf_page->XObject_dict, & XObject_name [0], ind_ref);
return (pdf_page->last_XObject_name);
}
void pdf_page_add_content_stream(pdf_page_handle pdf_page,
pdf_obj_handle content_stream)
{
pdf_obj_handle contents;
contents = pdf_get_dict_entry (pdf_page->page_dict, "Contents");
if (! contents)
contents = pdf_new_obj (PT_ARRAY);
pdf_add_array_elem (contents, content_stream);
pdf_set_dict_entry (pdf_page->page_dict, "Contents", contents);
pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
}