summaryrefslogtreecommitdiff
path: root/src/cos_object.c
blob: 52731ca76b882502adbf2f6e2ffa49443a4b3114 (plain)
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
/*
 * Copyright 2018 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of libnspsl
 *
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 */

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <nspdf/errors.h>

#include "xref.h"
#include "cos_content.h"
#include "cos_object.h"
#include "cos_parse.h"
#include "pdf_doc.h"

static nspdferror cos_dump_object(const char *fmt, struct cos_object *cos_obj)
{
    printf("%s\n", fmt);
    switch (cos_obj->type) {
    case COS_TYPE_NAMETREE:
        printf("  type = COS_TYPE_NAMETREE\n");
        break;

    case COS_TYPE_REFERENCE:
        printf("  type = COS_TYPE_REFERENCE\n"
               "  u.reference->id = %lu\n"
               "  u.reference->generation = %lu\n",
               cos_obj->u.reference->id,
               cos_obj->u.reference->generation);
        break;

    case COS_TYPE_NULL:
        printf("  type = COS_TYPE_NULL\n");
        break;

    case COS_TYPE_CONTENT:
        printf("  type = COS_TYPE_CONTENT\n"
               "  u.content->length = %d\n"
               "  u.content->alloc = %d\n"
               "  u.content->operations = %p\n",
               cos_obj->u.content->length,
               cos_obj->u.content->alloc,
               cos_obj->u.content->operations);
        break;

    case COS_TYPE_BOOL:
        printf("  type = COS_TYPE_BOOL\n  u.b = %s\n",
               cos_obj->u.b ? "true" : "false");
        break;

    case COS_TYPE_INT:
        printf("  type = COS_TYPE_INT\n  u.i = %ld\n", cos_obj->u.i);
        break;

    case COS_TYPE_REAL:
        printf("  type = COS_TYPE_REAL\n  u.real = %f\n", cos_obj->u.real);
        break;

    case COS_TYPE_NAME:
        printf("  type = COS_TYPE_NAME\n  u.name = %s\n", cos_obj->u.name);
        break;

    case COS_TYPE_STRING:
        printf("  type = COS_TYPE_STRING\n");
        //free(cos_obj->u.s->data);
        //free(cos_obj->u.s);
        break;

    case COS_TYPE_DICTIONARY:
        printf("  type = COS_TYPE_DICTIONARY\n");
        /*
          dentry = cos_obj->u.dictionary;
          while (dentry != NULL) {
          struct cos_dictionary_entry *odentry;

          cos_free_object(dentry->key);
          cos_free_object(dentry->value);

          odentry = dentry;
          dentry = dentry->next;
          free(odentry);
          }
        */
        break;

    case COS_TYPE_ARRAY:
        printf("  type = COS_TYPE_ARRAY\n");
        /*
          if (cos_obj->u.array->alloc > 0) {
          for (aentry = 0; aentry < cos_obj->u.array->length; aentry++) {
          cos_free_object(*(cos_obj->u.array->values + aentry));
          }
          free(cos_obj->u.array->values);
          }
          free(cos_obj->u.array);
        */
        break;

    case COS_TYPE_STREAM:
        printf("  type = COS_TYPE_STREAM\n");
        /*
          free(cos_obj->u.stream);
        */
        break;

    }


    return NSPDFERROR_OK;

}

nspdferror cos_free_object(struct cos_object *cos_obj)
{
    struct cos_dictionary_entry *dentry;
    unsigned int aentry;

    switch (cos_obj->type) {
    case COS_TYPE_NAME:
        free(cos_obj->u.name);
        break;

    case COS_TYPE_STRING:
        free(cos_obj->u.s->data);
        free(cos_obj->u.s);
        break;

    case COS_TYPE_DICTIONARY:
        dentry = cos_obj->u.dictionary;
        while (dentry != NULL) {
            struct cos_dictionary_entry *odentry;

            cos_free_object(dentry->key);
            cos_free_object(dentry->value);

            odentry = dentry;
            dentry = dentry->next;
            free(odentry);
        }
        break;

    case COS_TYPE_ARRAY:
        if (cos_obj->u.array->alloc > 0) {
            for (aentry = 0; aentry < cos_obj->u.array->length; aentry++) {
                cos_free_object(*(cos_obj->u.array->values + aentry));
            }
            free(cos_obj->u.array->values);
        }
        free(cos_obj->u.array);
        break;

    case COS_TYPE_STREAM:
        free(cos_obj->u.stream);
        break;

    }
    free(cos_obj);

    return NSPDFERROR_OK;
}


/*
 * extracts a value for a key in a dictionary.
 *
 * this finds and returns a value for a given key removing it from a dictionary
 */
nspdferror
cos_extract_dictionary_value(struct cos_object *dict,
                             const char *key,
                             struct cos_object **value_out)
{
    struct cos_dictionary_entry *entry;
    struct cos_dictionary_entry **prev;

    if (dict->type != COS_TYPE_DICTIONARY) {
        return NSPDFERROR_TYPE;
    }

    prev = &dict->u.dictionary;
    entry = *prev;
    while (entry != NULL) {
        if (strcmp(entry->key->u.name, key) == 0) {
            *value_out = entry->value;
            *prev = entry->next;
            cos_free_object(entry->key);
            free(entry);
            return NSPDFERROR_OK;
        }
        prev = &entry->next;
        entry = *prev;
    }
    return NSPDFERROR_NOTFOUND;
}


/*
 * get a value for a key from a dictionary
 */
nspdferror
cos_get_dictionary_value(struct nspdf_doc *doc,
                         struct cos_object *dict,
                         const char *key,
                         struct cos_object **value_out)
{
    nspdferror res;
    struct cos_dictionary_entry *entry;

    res = nspdf__xref_get_referenced(doc, &dict);
    if (res == NSPDFERROR_OK) {
        if (dict->type != COS_TYPE_DICTIONARY) {
            res = NSPDFERROR_TYPE;
        } else {
            res = NSPDFERROR_NOTFOUND;

            entry = dict->u.dictionary;
            while (entry != NULL) {
                if (strcmp(entry->key->u.name, key) == 0) {
                    *value_out = entry->value;
                    res = NSPDFERROR_OK;
                    break;
                }
                entry = entry->next;
            }
        }
    }
    return res;
}

nspdferror
cos_get_dictionary_int(struct nspdf_doc *doc,
                       struct cos_object *dict,
                       const char *key,
                       int64_t *value_out)
{
    nspdferror res;
    struct cos_object *dict_value;

    res = cos_get_dictionary_value(doc, dict, key, &dict_value);
    if (res != NSPDFERROR_OK) {
        return res;
    }
    return cos_get_int(doc, dict_value, value_out);
}

nspdferror
cos_get_dictionary_name(struct nspdf_doc *doc,
                        struct cos_object *dict,
                        const char *key,
                        const char **value_out)
{
    nspdferror res;
    struct cos_object *dict_value;

    res = cos_get_dictionary_value(doc, dict, key, &dict_value);
    if (res != NSPDFERROR_OK) {
        return res;
    }
    return cos_get_name(doc, dict_value, value_out);
}

nspdferror
cos_get_dictionary_string(struct nspdf_doc *doc,
                          struct cos_object *dict,
                          const char *key,
                          struct cos_string **string_out)
{
    nspdferror res;
    struct cos_object *dict_value;

    res = cos_get_dictionary_value(doc, dict, key, &dict_value);
    if (res != NSPDFERROR_OK) {
        return res;
    }
    return cos_get_string(doc, dict_value, string_out);
}

nspdferror
cos_get_dictionary_dictionary(struct nspdf_doc *doc,
                              struct cos_object *dict,
                              const char *key,
                              struct cos_object **value_out)
{
    nspdferror res;
    struct cos_object *dict_value;

    res = cos_get_dictionary_value(doc, dict, key, &dict_value);
    if (res != NSPDFERROR_OK) {
        return res;
    }
    return cos_get_dictionary(doc, dict_value, value_out);
}

nspdferror
cos_heritable_dictionary_dictionary(struct nspdf_doc *doc,
                                    struct cos_object *dict,
                                    const char *key,
                                    struct cos_object **value_out)
{
    nspdferror res;
    struct cos_object *dict_value;
    res = cos_get_dictionary_value(doc, dict, key, &dict_value);
    if (res == NSPDFERROR_NOTFOUND) {
        /* \todo get parent entry and extract key from that dictionary instead */
    }
    if (res != NSPDFERROR_OK) {
        return res;
    }
    return cos_get_dictionary(doc, dict_value, value_out);
}

nspdferror
cos_get_dictionary_array(struct nspdf_doc *doc,
                         struct cos_object *dict,
                         const char *key,
                         struct cos_object **value_out)
{
    nspdferror res;
    struct cos_object *dict_value;

    res = cos_get_dictionary_value(doc, dict, key, &dict_value);
    if (res != NSPDFERROR_OK) {
        return res;
    }
    return cos_get_array(doc, dict_value, value_out);
}

nspdferror
cos_heritable_dictionary_array(struct nspdf_doc *doc,
                               struct cos_object *dict,
                               const char *key,
                               struct cos_object **value_out)
{
    nspdferror res;
    struct cos_object *dict_value;

    res = cos_get_dictionary_value(doc, dict, key, &dict_value);
    if (res == NSPDFERROR_NOTFOUND) {
        /* \todo get parent entry and extract key from that dictionary instead */
    }
    if (res != NSPDFERROR_OK) {
        return res;
    }
    return cos_get_array(doc, dict_value, value_out);
}

nspdferror
cos_get_int(struct nspdf_doc *doc,
            struct cos_object *cobj,
            int64_t *value_out)
{
    nspdferror res;

    res = nspdf__xref_get_referenced(doc, &cobj);
    if (res == NSPDFERROR_OK) {
        if (cobj->type != COS_TYPE_INT) {
            res = NSPDFERROR_TYPE;
        } else {
            *value_out = cobj->u.i;
        }
    }
    return res;
}

nspdferror
cos_get_number(struct nspdf_doc *doc,
               struct cos_object *cobj,
               float *value_out)
{
    nspdferror res;

    res = nspdf__xref_get_referenced(doc, &cobj);
    if (res == NSPDFERROR_OK) {
        if (cobj->type == COS_TYPE_INT) {
            *value_out = (float)cobj->u.i;
        } else if (cobj->type == COS_TYPE_REAL) {
            *value_out = cobj->u.real;
        } else {
            res = NSPDFERROR_TYPE;
        }
    }
    return res;
}

nspdferror
cos_get_name(struct nspdf_doc *doc,
             struct cos_object *cobj,
             const char **value_out)
{
    nspdferror res;

    res = nspdf__xref_get_referenced(doc, &cobj);
    if (res == NSPDFERROR_OK) {
        if (cobj->type != COS_TYPE_NAME) {
            res = NSPDFERROR_TYPE;
        } else {
            *value_out = cobj->u.name;
        }
    }
    return res;
}


nspdferror
cos_get_dictionary(struct nspdf_doc *doc,
                   struct cos_object *cobj,
                   struct cos_object **value_out)
{
    nspdferror res;

    res = nspdf__xref_get_referenced(doc, &cobj);
    if (res == NSPDFERROR_OK) {
        if (cobj->type != COS_TYPE_DICTIONARY) {
            res = NSPDFERROR_TYPE;
        } else {
            *value_out = cobj;
        }
    }
    return res;
}


nspdferror
cos_get_array(struct nspdf_doc *doc,
              struct cos_object *cobj,
              struct cos_object **value_out)
{
    nspdferror res;

    res = nspdf__xref_get_referenced(doc, &cobj);
    if (res == NSPDFERROR_OK) {
        if (cobj->type != COS_TYPE_ARRAY) {
            res = NSPDFERROR_TYPE;
        } else {
            *value_out = cobj;
        }
    }
    return res;
}


nspdferror
cos_get_string(struct nspdf_doc *doc,
               struct cos_object *cobj,
               struct cos_string **string_out)
{
    nspdferror res;

    res = nspdf__xref_get_referenced(doc, &cobj);
    if (res == NSPDFERROR_OK) {
        if (cobj->type != COS_TYPE_STRING) {
            res = NSPDFERROR_TYPE;
        } else {
            *string_out = cobj->u.s;
        }
    }
    return res;
}


nspdferror
cos_get_stream(struct nspdf_doc *doc,
               struct cos_object *cobj,
               struct cos_stream **stream_out)
{
    nspdferror res;
    //printf("%p %d\n", cobj, cobj->type);
    res = nspdf__xref_get_referenced(doc, &cobj);
    //printf("%p %d res:%d\n", cobj, cobj->type, res);
    if (res == NSPDFERROR_OK) {
        if (cobj->type != COS_TYPE_STREAM) {
            res = NSPDFERROR_TYPE;
        } else {
            *stream_out = cobj->u.stream;
        }
    }
    return res;
}


/*
 * get object from object reference
 */
nspdferror
cos_get_object(struct nspdf_doc *doc,
               struct cos_object *cobj,
               struct cos_object **value_out)
{
    nspdferror res;
    res = nspdf__xref_get_referenced(doc, &cobj);
    if (res == NSPDFERROR_OK) {
        *value_out = cobj;
    }
    return res;
}

/*
 * exported interface documented in cos_object.h
 *
 * slightly different behaviour to other getters:
 *
 * - This getter can be passed an object pointer to a synthetic parsed content
 *     stream object in which case it returns that objects content operation
 *     list.
 *
 * - Alternatively it can be passed a single indirect object reference to a
 *    content stream which will be processed into a filtered stream and then
 *    converted into a parsed content stream which replaces the passed
 *    object. The underlying filtered streams will then be freed.
 *
 * - An array of indirect object references to content streams all of which
 *    will be converted as if a single stream of tokens and the result handled
 *    as per the single reference case.
 */
nspdferror
cos_get_content(struct nspdf_doc *doc,
                struct cos_object *cobj,
                struct cos_content **content_out)
{
    nspdferror res;
    struct cos_object **references;
    unsigned int reference_count;
    struct cos_stream **streams;
    unsigned int index;
    struct cos_object *content_obj; /* parsed content object */
    struct cos_object tmpobj;

    //cos_dump_object("get content of", cobj);

    if (cobj->type == COS_TYPE_CONTENT) {
        /* already parsed the content stream */
        goto cos_get_content_done;
    }

    if (cobj->type == COS_TYPE_REFERENCE) {
        /* single reference */
        reference_count = 1;
        references = calloc(reference_count, sizeof(struct cos_object *));
        if (references == NULL) {
            return NSPDFERROR_NOMEM;
        }

        *references = cobj;
    } else if (cobj->type == COS_TYPE_ARRAY) {
        /* array of references */
        reference_count = cobj->u.array->length;
        references = malloc(reference_count * sizeof(struct cos_object *));
        if (references == NULL) {
            return NSPDFERROR_NOMEM;
        }
        memcpy(references, cobj->u.array->values, reference_count * sizeof(struct cos_object *));
        /* check all objects in array are references */
        for (index = 0; index < reference_count ; index++) {
            if ((*(references + index))->type != COS_TYPE_REFERENCE) {
                free(references);
                return NSPDFERROR_TYPE;
            }
        }
    } else {
        return NSPDFERROR_TYPE;
    }

    /* obtain array of streams */
    streams = malloc(reference_count * sizeof(struct cos_stream *));
    if (streams == NULL) {
        free(references);
        return NSPDFERROR_TYPE;
    }

    for (index = 0; index < reference_count ; index++) {
        struct cos_object *stream_obj;

        stream_obj = *(references + index);
        res = nspdf__xref_get_referenced(doc, &stream_obj);
        if (res != NSPDFERROR_OK) {
            free(references);
            free(streams);
            return res;
        }
        if (stream_obj->type != COS_TYPE_STREAM) {
            free(references);
            free(streams);
            return NSPDFERROR_TYPE;
        }
        *(streams + index) = stream_obj->u.stream;;
    }

    res = cos_parse_content_streams(doc, streams, reference_count, &content_obj);
    if (res != NSPDFERROR_OK) {
        free(references);
        free(streams);
        return res;
    }

    /* replace passed object with parsed content operations object */
    tmpobj = *cobj;
    *cobj = *content_obj;
    *content_obj = tmpobj;

    //cos_dump_object("content object", cobj);
    //cos_dump_object("free object", content_obj);

    cos_free_object(content_obj);

    /** \todo call nspdf__xref_free_referenced(doc, *(references + index)); to free up storage associated with already parsed streams */

cos_get_content_done:
    *content_out = cobj->u.content;

    return NSPDFERROR_OK;
}

/*
 * get a value for a key from a dictionary
 */
nspdferror
cos_get_array_value(struct nspdf_doc *doc,
                    struct cos_object *array,
                    unsigned int index,
                    struct cos_object **value_out)
{
    nspdferror res;

    res = nspdf__xref_get_referenced(doc, &array);
    if (res == NSPDFERROR_OK) {
        if (array->type != COS_TYPE_ARRAY) {
            res = NSPDFERROR_TYPE;
        } else {
            if (index >= array->u.array->length) {
                res = NSPDFERROR_RANGE;
            } else {
                *value_out = *(array->u.array->values + index);
            }
        }
    }
    return res;
}

nspdferror
cos_get_array_dictionary(struct nspdf_doc *doc,
                         struct cos_object *array,
                         unsigned int index,
                         struct cos_object **value_out)
{
    nspdferror res;
    struct cos_object *array_value;

    res = cos_get_array_value(doc, array, index, &array_value);
    if (res != NSPDFERROR_OK) {
        return res;
    }
    return cos_get_dictionary(doc, array_value, value_out);
}

nspdferror
cos_get_array_size(struct nspdf_doc *doc,
                   struct cos_object *cobj,
                   unsigned int *size_out)
{
    nspdferror res;

    res = nspdf__xref_get_referenced(doc, &cobj);
    if (res == NSPDFERROR_OK) {
        if (cobj->type != COS_TYPE_ARRAY) {
            res = NSPDFERROR_TYPE;
        } else {
            *size_out = cobj->u.array->length;
        }
    }
    return res;
}