-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
pdfio-object.c
600 lines (465 loc) · 14.1 KB
/
pdfio-object.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
//
// PDF object functions for PDFio.
//
// Copyright © 2021-2024 by Michael R Sweet.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
#include "pdfio-private.h"
//
// Local functions...
//
static bool write_obj_header(pdfio_obj_t *obj);
//
// 'pdfioObjClose()' - Close an object, writing any data as needed to the PDF
// file.
//
bool // O - `true` on success, `false` on failure
pdfioObjClose(pdfio_obj_t *obj) // I - Object
{
// Range check input
if (!obj)
return (false);
// Clear the current object pointer...
obj->pdf->current_obj = NULL;
if (obj->pdf->mode != _PDFIO_MODE_WRITE)
{
// Nothing to do when reading
return (true);
}
// Write what remains for the object...
if (!obj->offset)
{
// Write the object value
if (!write_obj_header(obj))
return (false);
// Write the "endobj" line...
return (_pdfioFilePuts(obj->pdf, "endobj\n"));
}
else if (obj->stream)
{
// Close the stream...
return (pdfioStreamClose(obj->stream));
}
else
{
// Already closed
return (true);
}
}
//
// 'pdfioObjCopy()' - Copy an object to another PDF file.
//
pdfio_obj_t * // O - New object or `NULL` on error
pdfioObjCopy(pdfio_file_t *pdf, // I - PDF file
pdfio_obj_t *srcobj) // I - Object to copy
{
pdfio_obj_t *dstobj; // Destination object
pdfio_stream_t *srcst, // Source stream
*dstst; // Destination stream
char buffer[32768]; // Copy buffer
ssize_t bytes; // Bytes read
PDFIO_DEBUG("pdfioObjCopy(pdf=%p, srcobj=%p(%p))\n", pdf, srcobj, srcobj ? srcobj->pdf : NULL);
// Range check input
if (!pdf || !srcobj)
return (NULL);
// Load the object value if needed...
if (srcobj->value.type == PDFIO_VALTYPE_NONE)
_pdfioObjLoad(srcobj);
// Create the new object...
if ((dstobj = _pdfioFileCreateObj(pdf, srcobj->pdf, NULL)) == NULL)
return (NULL);
// Add new object to the cache of copied objects...
if (!_pdfioFileAddMappedObj(pdf, dstobj, srcobj))
return (NULL);
// Copy the object's value...
if (!_pdfioValueCopy(pdf, &dstobj->value, srcobj->pdf, &srcobj->value))
return (NULL);
if (dstobj->value.type == PDFIO_VALTYPE_DICT)
pdfioDictClear(dstobj->value.value.dict, "Length");
if (srcobj->stream_offset)
{
// Copy stream data...
if ((srcst = pdfioObjOpenStream(srcobj, false)) == NULL)
{
pdfioObjClose(dstobj);
return (NULL);
}
if ((dstst = pdfioObjCreateStream(dstobj, PDFIO_FILTER_NONE)) == NULL)
{
pdfioStreamClose(srcst);
pdfioObjClose(dstobj);
return (NULL);
}
while ((bytes = pdfioStreamRead(srcst, buffer, sizeof(buffer))) > 0)
{
if (!pdfioStreamWrite(dstst, buffer, (size_t)bytes))
{
bytes = -1;
break;
}
}
pdfioStreamClose(srcst);
pdfioStreamClose(dstst);
if (bytes < 0)
return (NULL);
}
else
pdfioObjClose(dstobj);
return (dstobj);
}
//
// 'pdfioObjCreateStream()' - Create an object (data) stream for writing.
//
pdfio_stream_t * // O - Stream or `NULL` on error
pdfioObjCreateStream(
pdfio_obj_t *obj, // I - Object
pdfio_filter_t filter) // I - Type of compression to apply
{
pdfio_obj_t *length_obj = NULL; // Length object, if any
// Range check input
if (!obj || obj->pdf->mode != _PDFIO_MODE_WRITE || obj->value.type != PDFIO_VALTYPE_DICT)
return (NULL);
if (obj->offset)
{
_pdfioFileError(obj->pdf, "Object has already been written.");
return (NULL);
}
if (filter != PDFIO_FILTER_NONE && filter != PDFIO_FILTER_FLATE)
{
_pdfioFileError(obj->pdf, "Unsupported filter value for PDFioObjCreateStream.");
return (NULL);
}
if (obj->pdf->current_obj)
{
_pdfioFileError(obj->pdf, "Another object (%u) is already open.", (unsigned)obj->pdf->current_obj->number);
return (NULL);
}
// Write the header...
if (!_pdfioDictGetValue(obj->value.value.dict, "Length"))
{
if (obj->pdf->output_cb)
{
// Streaming via an output callback, so add a placeholder length object
_pdfio_value_t length_value; // Length value
length_value.type = PDFIO_VALTYPE_NUMBER;
length_value.value.number = 0.0f;
length_obj = _pdfioFileCreateObj(obj->pdf, obj->pdf, &length_value);
pdfioDictSetObj(obj->value.value.dict, "Length", length_obj);
}
else
{
// Need a Length key for the stream, add a placeholder that we can fill in
// later...
pdfioDictSetNumber(obj->value.value.dict, "Length", 0.0);
}
}
if (!write_obj_header(obj))
return (NULL);
if (!_pdfioFilePuts(obj->pdf, "stream\n"))
return (NULL);
obj->stream_offset = _pdfioFileTell(obj->pdf);
obj->pdf->current_obj = obj;
// Return the new stream...
return (_pdfioStreamCreate(obj, length_obj, filter));
}
//
// '_pdfioObjDelete()' - Free memory used by an object.
//
void
_pdfioObjDelete(pdfio_obj_t *obj) // I - Object
{
if (obj)
{
pdfioStreamClose(obj->stream);
if (obj->datafree)
(obj->datafree)(obj->data);
}
free(obj);
}
//
// 'pdfioObjGetArray()' - Get the array associated with an object.
//
pdfio_array_t * // O - Array or `NULL` on error
pdfioObjGetArray(pdfio_obj_t *obj) // I - Object
{
if (!obj)
return (NULL);
if (obj->value.type == PDFIO_VALTYPE_NONE)
_pdfioObjLoad(obj);
if (obj->value.type == PDFIO_VALTYPE_ARRAY)
return (obj->value.value.array);
else
return (NULL);
}
//
// 'pdfioObjGetDict()' - Get the dictionary associated with an object.
//
pdfio_dict_t * // O - Dictionary or `NULL` on error
pdfioObjGetDict(pdfio_obj_t *obj) // I - Object
{
if (!obj)
return (NULL);
if (obj->value.type == PDFIO_VALTYPE_NONE)
_pdfioObjLoad(obj);
if (obj->value.type == PDFIO_VALTYPE_DICT)
return (obj->value.value.dict);
else
return (NULL);
}
//
// '_pdfioObjGetExtension()' - Get the extension pointer for an object.
//
void * // O - Extension data
_pdfioObjGetExtension(pdfio_obj_t *obj) // I - Object
{
return (obj->data);
}
//
// 'pdfioObjGetGeneration()' - Get the object's generation number.
//
unsigned short // O - Generation number (0 to 65535)
pdfioObjGetGeneration(pdfio_obj_t *obj) // I - Object
{
return (obj ? obj->generation : 0);
}
//
// 'pdfioObjGetLength()' - Get the length of the object's (data) stream.
//
size_t // O - Length in bytes or `0` for none
pdfioObjGetLength(pdfio_obj_t *obj) // I - Object
{
size_t length; // Length of stream
pdfio_obj_t *lenobj; // Length object
// Range check input...
if (!obj || !obj->stream_offset || obj->value.type != PDFIO_VALTYPE_DICT)
return (0);
// Try getting the length, directly or indirectly
if ((length = (size_t)pdfioDictGetNumber(obj->value.value.dict, "Length")) > 0)
{
PDFIO_DEBUG("pdfioObjGetLength(obj=%p) returning %lu.\n", obj, (unsigned long)length);
return (length);
}
if ((lenobj = pdfioDictGetObj(obj->value.value.dict, "Length")) == NULL)
{
_pdfioFileError(obj->pdf, "Unable to get length of stream.");
return (0);
}
if (lenobj->value.type == PDFIO_VALTYPE_NONE)
_pdfioObjLoad(lenobj);
if (lenobj->value.type != PDFIO_VALTYPE_NUMBER || lenobj->value.value.number <= 0.0)
{
_pdfioFileError(obj->pdf, "Unable to get length of stream.");
return (0);
}
PDFIO_DEBUG("pdfioObjGetLength(obj=%p) returning %lu.\n", obj, (unsigned long)lenobj->value.value.number);
return ((size_t)lenobj->value.value.number);
}
//
// 'pdfioObjGetName()' - Get the name value associated with an object.
//
const char * // O - Dictionary or `NULL` on error
pdfioObjGetName(pdfio_obj_t *obj) // I - Object
{
if (!obj)
return (NULL);
if (obj->value.type == PDFIO_VALTYPE_NONE)
_pdfioObjLoad(obj);
if (obj->value.type == PDFIO_VALTYPE_NAME)
return (obj->value.value.name);
else
return (NULL);
}
//
// 'pdfioObjGetNumber()' - Get the object's number.
//
size_t // O - Object number (1 to 9999999999)
pdfioObjGetNumber(pdfio_obj_t *obj) // I - Object
{
return (obj ? obj->number : 0);
}
//
// 'pdfioObjGetSubtype()' - Get an object's subtype.
//
// This function returns an object's PDF subtype name, if any. Common subtype
// names include:
//
// - "CIDFontType0": A CID Type0 font
// - "CIDFontType2": A CID TrueType font
// - "Image": An image or image mask
// - "Form": A fillable form
// - "OpenType": An OpenType font
// - "Type0": A composite font
// - "Type1": A PostScript Type1 font
// - "Type3": A PDF Type3 font
// - "TrueType": A TrueType font
//
const char * // O - Object subtype name or `NULL` for none
pdfioObjGetSubtype(pdfio_obj_t *obj) // I - Object
{
pdfio_dict_t *dict; // Object dictionary
if ((dict = pdfioObjGetDict(obj)) == NULL)
return (NULL);
else
return (pdfioDictGetName(dict, "Subtype"));
}
//
// 'pdfioObjGetType()' - Get an object's type.
//
// This function returns an object's PDF type name, if any. Common type names
// include:
//
// - "CMap": A character map for composite fonts
// - "Font": An embedded font (@link pdfioObjGetSubtype@ will tell you the
// font format)
// - "FontDescriptor": A font descriptor
// - "Page": A (visible) page
// - "Pages": A page tree node
// - "Template": An invisible template page
// - "XObject": An image, image mask, or form (@link pdfioObjGetSubtype@ will
// tell you which)
//
const char * // O - Object type name or `NULL` for none
pdfioObjGetType(pdfio_obj_t *obj) // I - Object
{
pdfio_dict_t *dict; // Object dictionary
if ((dict = pdfioObjGetDict(obj)) == NULL)
return (NULL);
else
return (pdfioDictGetName(dict, "Type"));
}
//
// '_pdfioObjLoad()' - Load an object dictionary/value.
//
bool // O - `true` on success, `false` otherwise
_pdfioObjLoad(pdfio_obj_t *obj) // I - Object
{
char line[64], // Line from file
*ptr; // Pointer into line
ssize_t bytes; // Bytes read
_pdfio_token_t tb; // Token buffer/stack
PDFIO_DEBUG("_pdfioObjLoad(obj=%p(%lu)), offset=%lu\n", obj, (unsigned long)obj->number, (unsigned long)obj->offset);
// Seek to the start of the object and read its header...
if (_pdfioFileSeek(obj->pdf, obj->offset, SEEK_SET) != obj->offset)
{
_pdfioFileError(obj->pdf, "Unable to seek to object %lu.", (unsigned long)obj->number);
return (false);
}
if ((bytes = _pdfioFilePeek(obj->pdf, line, sizeof(line) - 1)) < 0)
{
_pdfioFileError(obj->pdf, "Unable to read header for object %lu.", (unsigned long)obj->number);
return (false);
}
line[bytes] = '\0';
PDFIO_DEBUG("_pdfioObjLoad: Header is '%s'.\n", line);
if (strtoimax(line, &ptr, 10) != (intmax_t)obj->number)
{
_pdfioFileError(obj->pdf, "Bad header for object %lu.", (unsigned long)obj->number);
return (false);
}
if (strtol(ptr, &ptr, 10) != (long)obj->generation)
{
_pdfioFileError(obj->pdf, "Bad header for object %lu.", (unsigned long)obj->number);
return (false);
}
while (isspace(*ptr & 255))
ptr ++;
if (strncmp(ptr, "obj", 3) || (ptr[3] && ptr[3] != '<' && ptr[3] != '[' && !isspace(ptr[3] & 255)))
{
_pdfioFileError(obj->pdf, "Bad header for object %lu.", (unsigned long)obj->number);
return (false);
}
ptr += 3;
while (*ptr && isspace(*ptr & 255))
ptr ++;
_pdfioFileConsume(obj->pdf, (size_t)(ptr - line));
// Then grab the object value...
_pdfioTokenInit(&tb, obj->pdf, (_pdfio_tconsume_cb_t)_pdfioFileConsume, (_pdfio_tpeek_cb_t)_pdfioFilePeek, obj->pdf);
if (!_pdfioValueRead(obj->pdf, obj, &tb, &obj->value, 0))
{
_pdfioFileError(obj->pdf, "Unable to read value for object %lu.", (unsigned long)obj->number);
return (false);
}
// Now see if there is an associated stream...
if (!_pdfioTokenGet(&tb, line, sizeof(line)))
{
_pdfioFileError(obj->pdf, "Early end-of-file for object %lu.", (unsigned long)obj->number);
return (false);
}
PDFIO_DEBUG("_pdfioObjLoad: tb.bufptr=%p, tb.bufend=%p, tb.bufptr[0]=0x%02x, tb.bufptr[1]=0x%02x\n", tb.bufptr, tb.bufend, tb.bufptr[0], tb.bufptr[1]);
_pdfioTokenFlush(&tb);
if (!strcmp(line, "stream"))
{
// Yes, this is an embedded stream so save its location...
obj->stream_offset = _pdfioFileTell(obj->pdf);
PDFIO_DEBUG("_pdfioObjLoad: stream_offset=%lu.\n", (unsigned long)obj->stream_offset);
}
// Decrypt as needed...
if (obj->pdf->encryption)
{
PDFIO_DEBUG("_pdfioObjLoad: Decrypting value...\n");
if (!_pdfioValueDecrypt(obj->pdf, obj, &obj->value, 0))
{
PDFIO_DEBUG("_pdfioObjLoad: Failed to decrypt.\n");
return (false);
}
}
PDFIO_DEBUG("_pdfioObjLoad: ");
PDFIO_DEBUG_VALUE(&obj->value);
PDFIO_DEBUG("\n");
return (true);
}
//
// 'pdfioObjOpenStream()' - Open an object's (data) stream for reading.
//
pdfio_stream_t * // O - Stream or `NULL` on error
pdfioObjOpenStream(pdfio_obj_t *obj, // I - Object
bool decode) // I - Decode/decompress data?
{
// Range check input...
if (!obj)
return (NULL);
if (obj->pdf->current_obj)
{
_pdfioFileError(obj->pdf, "Another object (%u) is already open.", (unsigned)obj->pdf->current_obj->number);
return (NULL);
}
// Make sure we've loaded the object dictionary...
if (!obj->value.type)
{
if (!_pdfioObjLoad(obj))
return (NULL);
}
// No stream if there is no dict or offset to a stream...
if (obj->value.type != PDFIO_VALTYPE_DICT || !obj->stream_offset)
return (NULL);
// Open the stream...
obj->pdf->current_obj = obj;
return (_pdfioStreamOpen(obj, decode));
}
//
// '_pdfioObjSetExtension()' - Set extension data for an object.
//
void
_pdfioObjSetExtension(
pdfio_obj_t *obj, // I - Object
void *data, // I - Data
_pdfio_extfree_t datafree) // I - Free function
{
obj->data = data;
obj->datafree = datafree;
}
//
// 'write_obj_header()' - Write the object header...
//
static bool // O - `true` on success, `false` on failure
write_obj_header(pdfio_obj_t *obj) // I - Object
{
obj->offset = _pdfioFileTell(obj->pdf);
if (!_pdfioFilePrintf(obj->pdf, "%lu %u obj\n", (unsigned long)obj->number, obj->generation))
return (false);
if (!_pdfioValueWrite(obj->pdf, obj, &obj->value, &obj->length_offset))
return (false);
return (_pdfioFilePuts(obj->pdf, "\n"));
}