-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpdfobj.c
3455 lines (2891 loc) · 81.1 KB
/
pdfobj.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
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
Copyright (C) 2007-2014 by Jin-Hwan Cho and Shunsaku Hirata,
the dvipdfmx project team.
Copyright (C) 1998, 1999 by Mark A. Wicks <mwicks@kettering.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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-1307 USA.
*/
#include "libtexpdf.h"
#ifdef WIN32
#include <fcntl.h>
#endif
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif /* HAVE_ZLIB */
#define STREAM_ALLOC_SIZE 4096u
#define ARRAY_ALLOC_SIZE 256
#define IND_OBJECTS_ALLOC_SIZE 512
#define OBJ_NO_OBJSTM (1 << 0)
/* Objects with this flag will not be put into an object stream.
For instance, all stream objects have this flag set. */
#define OBJ_NO_ENCRYPT (1 << 1)
/* Objects with this flag will not be encrypted.
This implies OBJ_NO_OBJSTM if encryption is turned on. */
/* Any of these types can be represented as follows */
struct pdf_obj
{
int type;
unsigned long label; /* Only used for indirect objects
all other "label" to zero */
unsigned short generation; /* Only used if "label" is used */
unsigned refcount; /* Number of links to this object */
int flags;
void *data;
};
struct pdf_boolean
{
char value;
};
struct pdf_number
{
double value;
};
struct pdf_string
{
unsigned char *string;
unsigned short length;
};
struct pdf_name
{
char *name;
};
struct pdf_array
{
unsigned long max;
unsigned long size;
struct pdf_obj **values;
};
struct pdf_dict
{
struct pdf_obj *key;
struct pdf_obj *value;
struct pdf_dict *next;
};
struct pdf_stream
{
struct pdf_obj *dict;
unsigned char *stream;
long *objstm_data; /* used for object streams */
unsigned long stream_length;
unsigned long max_length;
unsigned char _flags;
};
struct pdf_indirect
{
pdf_file *pf;
pdf_obj *obj; /* used when PF == NULL */
unsigned long label;
unsigned short generation;
};
typedef void pdf_null;
typedef struct pdf_boolean pdf_boolean;
typedef struct pdf_number pdf_number;
typedef struct pdf_string pdf_string;
typedef struct pdf_name pdf_name;
typedef struct pdf_array pdf_array;
typedef struct pdf_dict pdf_dict;
typedef struct pdf_stream pdf_stream;
typedef struct pdf_indirect pdf_indirect;
static FILE *pdf_output_file = NULL;
static long pdf_output_file_position = 0;
static long pdf_output_line_position = 0;
static long compression_saved = 0;
#define FORMAT_BUF_SIZE 4096
static char format_buffer[FORMAT_BUF_SIZE];
typedef struct xref_entry
{
unsigned char type; /* object storage type */
unsigned long field2; /* offset in file or object stream */
unsigned short field3; /* generation or index */
pdf_obj *direct; /* used for imported objects */
pdf_obj *indirect; /* used for imported objects */
} xref_entry;
static xref_entry *output_xref;
static unsigned long pdf_max_ind_objects;
static unsigned long next_label;
static unsigned long startxref;
struct pdf_file
{
FILE *file;
pdf_obj *trailer;
xref_entry *xref_table;
pdf_obj *catalog;
long num_obj;
long file_size;
int version;
};
static pdf_obj *output_stream; /* XXX needs to be re-entrant */
#define OBJSTM_MAX_OBJS 200
/* the limit is only 100 for linearized PDF */
static int enc_mode;
static int doc_enc_mode;
static pdf_obj *trailer_dict; /* XXX needs to be re-entrant */
static pdf_obj *xref_stream; /* XXX needs to be re-entrant */
/* Internal static routines */
static int texpdf_check_for_pdf_version (FILE *file);
static void pdf_flush_obj (pdf_obj *object, FILE *file);
static void pdf_label_obj (pdf_obj *object);
static void pdf_write_obj (pdf_obj *object, FILE *file);
static void set_objstm_data (pdf_obj *objstm, long *data);
static long *get_objstm_data (pdf_obj *objstm);
static void release_objstm (pdf_obj *objstm);
static void pdf_out_char (FILE *file, char c);
static void pdf_out (FILE *file, const void *buffer, long length);
static pdf_obj *texpdf_new_ref (pdf_obj *object);
static void release_indirect (pdf_indirect *data);
static void write_indirect (pdf_indirect *indirect, FILE *file);
static void release_boolean (pdf_obj *data);
static void write_boolean (pdf_boolean *data, FILE *file);
static void write_null (FILE *file);
static void release_number (pdf_number *number);
static void write_number (pdf_number *number, FILE *file);
static void write_string (pdf_string *str, FILE *file);
static void release_string (pdf_string *str);
static void write_name (pdf_name *name, FILE *file);
static void release_name (pdf_name *name);
static void write_array (pdf_array *array, FILE *file);
static void release_array (pdf_array *array);
static void write_dict (pdf_dict *dict, FILE *file);
static void release_dict (pdf_dict *dict);
static void write_stream (pdf_stream *stream, FILE *file);
static void release_stream (pdf_stream *stream);
static int verbose = 0;
static char compression_level = 9;
void
texpdf_set_compression (int level)
{
#ifndef HAVE_ZLIB
ERROR("You don't have compression compiled in. Possibly libz wasn't found by configure.");
#else
#ifndef HAVE_ZLIB_COMPRESS2
if (level != 0)
WARN("Unable to set compression level -- your zlib doesn't have compress2().");
#endif
if (level >= 0 && level <= 9)
compression_level = level;
else {
ERROR("set_compression: invalid compression level: %d", level);
}
#endif /* !HAVE_ZLIB */
return;
}
static unsigned pdf_version = PDF_VERSION_DEFAULT;
void
texpdf_set_version (unsigned version)
{
/* Don't forget to update CIDFont_stdcc_def[] in cid.c too! */
if (version >= PDF_VERSION_MIN && version <= PDF_VERSION_MAX) {
pdf_version = version;
}
}
unsigned
texpdf_get_version (void)
{
return pdf_version;
}
int
pdf_obj_get_verbose(void)
{
return verbose;
}
void
texpdf_obj_set_verbose(void)
{
verbose++;
}
static pdf_obj *current_objstm = NULL;
static int do_objstm;
static void
add_xref_entry (unsigned long label, unsigned char type, unsigned long field2, unsigned short field3)
{
if (label >= pdf_max_ind_objects) {
pdf_max_ind_objects = (label/IND_OBJECTS_ALLOC_SIZE+1)*IND_OBJECTS_ALLOC_SIZE;
output_xref = RENEW(output_xref, pdf_max_ind_objects, xref_entry);
}
output_xref[label].type = type;
output_xref[label].field2 = field2;
output_xref[label].field3 = field3;
output_xref[label].direct = NULL;
output_xref[label].indirect = NULL;
}
#define BINARY_MARKER "%\344\360\355\370\n"
void
pdf_out_init (const char *filename, int do_encryption)
{
char v;
output_xref = NULL;
pdf_max_ind_objects = 0;
add_xref_entry(0, 0, 0, 0xffff);
next_label = 1;
if (pdf_version >= 5) {
xref_stream = texpdf_new_stream(STREAM_COMPRESS);
xref_stream->flags |= OBJ_NO_ENCRYPT;
trailer_dict = texpdf_stream_dict(xref_stream);
texpdf_add_dict(trailer_dict, texpdf_new_name("Type"), texpdf_new_name("XRef"));
do_objstm = 1;
} else {
xref_stream = NULL;
trailer_dict = texpdf_new_dict();
do_objstm = 0;
}
output_stream = NULL;
if (filename == NULL) { /* no filename: writing to stdout */
#if defined(WIN32) && !defined(__MINGW32__)
setmode(fileno(stdout), _O_BINARY);
#endif
pdf_output_file = stdout;
} else {
pdf_output_file = MFOPEN(filename, FOPEN_WBIN_MODE);
if (!pdf_output_file) {
if (strlen(filename) < 128)
ERROR("Unable to open \"%s\".", filename);
else
ERROR("Unable to open file.");
}
}
pdf_out(pdf_output_file, "%PDF-1.", strlen("%PDF-1."));
v = '0' + pdf_version;
pdf_out(pdf_output_file, &v, 1);
pdf_out(pdf_output_file, "\n", 1);
pdf_out(pdf_output_file, BINARY_MARKER, strlen(BINARY_MARKER));
enc_mode = 0;
doc_enc_mode = do_encryption;
}
static void
texpdf_dump_xref_table (void)
{
long length;
unsigned long i;
pdf_out(pdf_output_file, "xref\n", 5);
length = sprintf(format_buffer, "%d %lu\n", 0, next_label);
pdf_out(pdf_output_file, format_buffer, length);
/*
* Every space counts. The space after the 'f' and 'n' is * *essential*.
* The PDF spec says the lines must be 20 characters long including the
* end of line character.
*/
for (i = 0; i < next_label; i++) {
unsigned char type = output_xref[i].type;
if (type > 1)
ERROR("object type %hu not allowed in xref table", type);
length = sprintf(format_buffer, "%010lu %05hu %c \n",
output_xref[i].field2, output_xref[i].field3,
type ? 'n' : 'f');
pdf_out(pdf_output_file, format_buffer, length);
}
}
static void
texpdf_dump_trailer_dict (void)
{
pdf_out(pdf_output_file, "trailer\n", 8);
enc_mode = 0;
write_dict(trailer_dict->data, pdf_output_file);
texpdf_release_obj(trailer_dict);
pdf_out_char(pdf_output_file, '\n');
}
/*
* output a PDF 1.5 cross-reference stream;
* contributed by Matthias Franz (March 21, 2007)
*/
static void
texpdf_dump_xref_stream (void)
{
unsigned long pos, i;
unsigned poslen;
unsigned char buf[7] = {0, 0, 0, 0, 0};
pdf_obj *w;
/* determine the necessary size of the offset field */
pos = startxref; /* maximal offset value */
poslen = 1;
while (pos >>= 8)
poslen++;
w = texpdf_new_array();
texpdf_add_array(w, texpdf_new_number(1)); /* type */
texpdf_add_array(w, texpdf_new_number(poslen)); /* offset (big-endian) */
texpdf_add_array(w, texpdf_new_number(2)); /* generation */
texpdf_add_dict(trailer_dict, texpdf_new_name("W"), w);
/* We need the xref entry for the xref stream right now */
add_xref_entry(next_label-1, 1, startxref, 0);
for (i = 0; i < next_label; i++) {
unsigned j;
unsigned short f3;
buf[0] = output_xref[i].type;
pos = output_xref[i].field2;
for (j = poslen; j--; ) {
buf[1+j] = (unsigned char) pos;
pos >>= 8;
}
f3 = output_xref[i].field3;
buf[poslen+1] = (unsigned char) (f3 >> 8);
buf[poslen+2] = (unsigned char) (f3);
texpdf_add_stream(xref_stream, &buf, poslen+3);
}
texpdf_release_obj(xref_stream);
}
void
pdf_out_flush (void)
{
if (pdf_output_file) {
long length;
/* Flush current object stream */
if (current_objstm) {
release_objstm(current_objstm);
current_objstm =NULL;
}
/*
* Label xref stream - we need the number of correct objects
* for the xref stream dictionary (= trailer).
* Labelling it in pdf_out_init (with 1) does not work (why?).
*/
if (xref_stream)
pdf_label_obj(xref_stream);
/* Record where this xref is for trailer */
startxref = pdf_output_file_position;
texpdf_add_dict(trailer_dict, texpdf_new_name("Size"),
texpdf_new_number(next_label));
if (xref_stream)
texpdf_dump_xref_stream();
else {
texpdf_dump_xref_table();
texpdf_dump_trailer_dict();
}
/* Done with xref table */
RELEASE(output_xref);
pdf_out(pdf_output_file, "startxref\n", 10);
length = sprintf(format_buffer, "%lu\n", startxref);
pdf_out(pdf_output_file, format_buffer, length);
pdf_out(pdf_output_file, "%%EOF\n", 6);
MESG("\n");
if (verbose) {
if (compression_level > 0) {
MESG("Compression saved %ld bytes%s\n", compression_saved,
pdf_version < 5 ? ". Try \"-V 5\" for better compression" : "");
}
}
MESG("%ld bytes written", pdf_output_file_position);
MFCLOSE(pdf_output_file);
pdf_output_file_position = 0;
pdf_output_line_position = 0;
}
}
void
texpdf_error_cleanup (void)
{
/*
* This routine is the cleanup required for an abnormal exit.
* For now, simply close the file.
*/
if (pdf_output_file)
MFCLOSE(pdf_output_file);
}
void
texpdf_set_root (pdf_obj *object)
{
if (texpdf_add_dict(trailer_dict, texpdf_new_name("Root"), texpdf_ref_obj(object))) {
ERROR("Root object already set!");
}
/* Adobe Readers don't like a document catalog inside an encrypted
* object stream, although the PDF v1.5 spec seems to allow this.
* Note that we don't set OBJ_NO_ENCRYPT since the name dictionary in
* a document catalog may contain strings, which should be encrypted.
*/
if (doc_enc_mode)
object->flags |= OBJ_NO_OBJSTM;
}
void
texpdf_set_info (pdf_obj *object)
{
if (texpdf_add_dict(trailer_dict, texpdf_new_name("Info"), texpdf_ref_obj(object))) {
ERROR ("Info object already set!");
}
}
void
texpdf_set_id (pdf_obj *id)
{
if (texpdf_add_dict(trailer_dict, texpdf_new_name("ID"), id)) {
ERROR ("ID already set!");
}
}
void
texpdf_set_encrypt (pdf_obj *encrypt)
{
if (texpdf_add_dict(trailer_dict, texpdf_new_name("Encrypt"), texpdf_ref_obj(encrypt))) {
ERROR("Encrypt object already set!");
}
encrypt->flags |= OBJ_NO_ENCRYPT;
}
static
void pdf_out_char (FILE *file, char c)
{
if (output_stream && file == pdf_output_file)
texpdf_add_stream(output_stream, &c, 1);
else {
fputc(c, file);
/* Keep tallys for xref table *only* if writing a pdf file. */
if (file == pdf_output_file) {
pdf_output_file_position += 1;
if (c == '\n')
pdf_output_line_position = 0;
else
pdf_output_line_position += 1;
}
}
}
static char xchar[] = "0123456789abcdef";
#define pdf_out_xchar(f,c) do {\
pdf_out_char((f), xchar[((c) >> 4) & 0x0f]);\
pdf_out_char((f), xchar[(c) & 0x0f]);\
} while (0)
static
void pdf_out (FILE *file, const void *buffer, long length)
{
if (output_stream && file == pdf_output_file)
texpdf_add_stream(output_stream, buffer, length);
else {
fwrite(buffer, 1, length, file);
/* Keep tallys for xref table *only* if writing a pdf file */
if (file == pdf_output_file) {
pdf_output_file_position += length;
pdf_output_line_position += length;
/* "foo\nbar\n "... */
if (length > 0 &&
((const char *)buffer)[length-1] == '\n')
pdf_output_line_position = 0;
}
}
}
/* returns 1 if a white-space character is necessary to separate
an object of type1 followed by an object of type2 */
static
int pdf_need_white (int type1, int type2)
{
return !(type1 == PDF_STRING || type1 == PDF_ARRAY || type1 == PDF_DICT ||
type2 == PDF_STRING || type2 == PDF_NAME ||
type2 == PDF_ARRAY || type2 == PDF_DICT);
}
static
void pdf_out_white (FILE *file)
{
if (file == pdf_output_file && pdf_output_line_position >= 80) {
pdf_out_char(file, '\n');
} else {
pdf_out_char(file, ' ');
}
}
#define TYPECHECK(o,t) if (!(o) || (o)->type != (t)) {\
ERROR("typecheck: Invalid object type: %d %d (line %d)", (o) ? (o)->type : -1, (t), __LINE__);\
}
#define INVALIDOBJ(o) ((o) == NULL || (o)->type <= 0 || (o)->type > PDF_UNDEFINED)
static pdf_obj *
texpdf_new_obj(int type)
{
pdf_obj *result;
if (type > PDF_UNDEFINED || type < 0)
ERROR("Invalid object type: %d", type);
result = NEW(1, pdf_obj);
result->type = type;
result->data = NULL;
result->label = 0;
result->generation = 0;
result->refcount = 1;
result->flags = 0;
return result;
}
int
texpdf_obj_typeof (pdf_obj *object)
{
if (INVALIDOBJ(object))
return PDF_OBJ_INVALID;
return object->type;
}
static void
pdf_label_obj (pdf_obj *object)
{
if (INVALIDOBJ(object))
ERROR("pdf_label_obj(): passed invalid object.");
/*
* Don't change label on an already labeled object. Ignore such calls.
*/
if (object->label == 0) {
object->label = next_label++;
object->generation = 0;
}
}
/*
* Transfer the label assigned to the object src to the object dst.
* The object dst must not yet have been labeled.
*/
void
pdf_transfer_label (pdf_obj *dst, pdf_obj *src)
{
ASSERT(dst && !dst->label && src);
dst->label = src->label;
dst->generation = src->generation;
src->label = 0;
src->generation = 0;
}
/*
* This doesn't really copy the object, but allows it to be used without
* fear that somebody else will free it.
*/
pdf_obj *
texpdf_link_obj (pdf_obj *object)
{
if (INVALIDOBJ(object))
ERROR("texpdf_link_obj(): passed invalid object.");
object->refcount += 1;
return object;
}
pdf_obj *
texpdf_ref_obj (pdf_obj *object)
{
if (INVALIDOBJ(object))
ERROR("texpdf_ref_obj(): passed invalid object.");
if (object->refcount == 0) {
MESG("\nTrying to refer already released object!!!\n");
pdf_write_obj(object, stderr);
ERROR("Cannot continue...");
}
if (PDF_OBJ_INDIRECTTYPE(object)) {
return texpdf_link_obj(object);
} else {
return texpdf_new_ref(object);
}
}
static void
release_indirect (pdf_indirect *data)
{
RELEASE(data);
}
static void
write_indirect (pdf_indirect *indirect, FILE *file)
{
long length;
ASSERT(!indirect->pf);
length = sprintf(format_buffer, "%lu %hu R", indirect->label, indirect->generation);
pdf_out(file, format_buffer, length);
}
/* The undefined object is used as a placeholder in pdfnames.c
* for objects which are referenced before they are defined.
*/
pdf_obj *
texpdf_new_undefined (void)
{
pdf_obj *result;
result = texpdf_new_obj(PDF_UNDEFINED);
result->data = NULL;
return result;
}
pdf_obj *
texpdf_new_null (void)
{
pdf_obj *result;
result = texpdf_new_obj(PDF_NULL);
result->data = NULL;
return result;
}
static void
write_null (FILE *file)
{
pdf_out(file, "null", 4);
}
pdf_obj *
texpdf_new_boolean (char value)
{
pdf_obj *result;
pdf_boolean *data;
result = texpdf_new_obj(PDF_BOOLEAN);
data = NEW(1, pdf_boolean);
data->value = value;
result->data = data;
return result;
}
static void
release_boolean (pdf_obj *data)
{
RELEASE (data);
}
static void
write_boolean (pdf_boolean *data, FILE *file)
{
if (data->value) {
pdf_out(file, "true", 4);
} else {
pdf_out(file, "false", 5);
}
}
char
pdf_boolean_value (pdf_obj *object)
{
pdf_boolean *data;
TYPECHECK(object, PDF_BOOLEAN);
data = object->data;
return data->value;
}
pdf_obj *
texpdf_new_number (double value)
{
pdf_obj *result;
pdf_number *data;
result = texpdf_new_obj(PDF_NUMBER);
data = NEW(1, pdf_number);
data->value = value;
result->data = data;
return result;
}
static void
release_number (pdf_number *data)
{
RELEASE (data);
}
static void
write_number (pdf_number *number, FILE *file)
{
int count;
count = pdf_sprint_number(format_buffer, number->value);
pdf_out(file, format_buffer, count);
}
void
texpdf_set_number (pdf_obj *object, double value)
{
pdf_number *data;
TYPECHECK(object, PDF_NUMBER);
data = object->data;
data->value = value;
}
double
texpdf_number_value (pdf_obj *object)
{
pdf_number *data;
TYPECHECK(object, PDF_NUMBER);
data = object->data;
return data->value;
}
pdf_obj *
texpdf_new_string (const void *str, unsigned length)
{
pdf_obj *result;
pdf_string *data;
ASSERT(str);
result = texpdf_new_obj(PDF_STRING);
data = NEW(1, pdf_string);
result->data = data;
data->length = length;
if (length) {
data->string = NEW(length+1, unsigned char);
memcpy(data->string, str, length);
/* Shouldn't assume NULL terminated. */
data->string[length] = '\0';
} else
data->string = NULL;
return result;
}
void *
texpdf_string_value (pdf_obj *object)
{
pdf_string *data;
TYPECHECK(object, PDF_STRING);
data = object->data;
return data->string;
}
unsigned
texpdf_string_length (pdf_obj *object)
{
pdf_string *data;
TYPECHECK(object, PDF_STRING);
data = object->data;
return (unsigned) (data->length);
}
/*
* This routine escapes non printable characters and control
* characters in an output string.
*/
int
pdfobj_escape_str (char *buffer, int bufsize, const unsigned char *s, int len)
{
int result = 0;
int i;
for (i = 0; i < len; i++) {
unsigned char ch;
ch = s[i];
if (result > bufsize - 4)
ERROR("pdfobj_escape_str: Buffer overflow");
/*
* We always write three octal digits. Optimization only gives few Kb
* smaller size for most documents when zlib compressed.
*/
if (ch < 32 || ch > 126) {
buffer[result++] = '\\';
#if 0
if (i < len - 1 && !isdigit(s[i+1]))
result += sprintf(buffer+result, "%o", ch);
else
result += sprintf(buffer+result, "%03o", ch);
#endif
result += sprintf(buffer+result, "%03o", ch);
} else {
switch (ch) {
case '(':
buffer[result++] = '\\';
buffer[result++] = '(';
break;
case ')':
buffer[result++] = '\\';
buffer[result++] = ')';
break;
case '\\':
buffer[result++] = '\\';
buffer[result++] = '\\';
break;
default:
buffer[result++] = ch;
break;
}
}
}
return result;
}
static void
write_string (pdf_string *str, FILE *file)
{
unsigned char *s;
char wbuf[FORMAT_BUF_SIZE]; /* Shouldn't use format_buffer[]. */
int nescc = 0, i, count;
s = str->string;
if (enc_mode)
pdf_encrypt_data(s, str->length);
/*
* Count all ASCII non-printable characters.
*/
for (i = 0; i < str->length; i++) {
if (!isprint(s[i]))
nescc++;
}
/*
* If the string contains much escaped chars, then we write it as
* ASCII hex string.
*/
if (nescc > str->length / 3) {
pdf_out_char(file, '<');
for (i = 0; i < str->length; i++) {
pdf_out_xchar(file, s[i]);
}
pdf_out_char(file, '>');
} else {
pdf_out_char(file, '(');
/*
* This section of code probably isn't speed critical. Escaping the
* characters in the string one at a time may seem slow, but it's
* safe if the formatted string length exceeds FORMAT_BUF_SIZE.
* Occasionally you see some long strings in PDF. pdfobj_escape_str
* is also used for strings of text with no kerning. These must be
* handled as quickly as possible since there are so many of them.
*/
for (i = 0; i < str->length; i++) {
count = pdfobj_escape_str(wbuf, FORMAT_BUF_SIZE, &(s[i]), 1);
pdf_out(file, wbuf, count);
}
pdf_out_char(file, ')');
}
}
static void
release_string (pdf_string *data)
{
if (data->string != NULL) {
RELEASE(data->string);
data->string = NULL;
}
RELEASE(data);
}
void
texpdf_set_string (pdf_obj *object, unsigned char *str, unsigned length)
{
pdf_string *data;
TYPECHECK(object, PDF_STRING);
data = object->data;
if (data->string != 0) {
RELEASE(data->string);
}
if (length != 0) {
data->length = length;
data->string = NEW(length + 1, unsigned char);
memcpy(data->string, str, length);
data->string[length] = '\0';