summaryrefslogtreecommitdiff
path: root/src/cos_content.c
blob: cf5d2e05d8de32fb7291d2ee58adcce999ad68a8 (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
/*
 * Copyright 2018 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of libnspdf.
 *
 * 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 "cos_object.h"
#include "cos_content.h"
#include "pdf_doc.h"

static const char*operator_name(enum content_operator operator)
{
    switch(operator) {
    case CONTENT_OP_b: return "b";
    case CONTENT_OP_B: return "B";
    case CONTENT_OP_b_: return "b*";
    case CONTENT_OP_B_: return "B*";
    case CONTENT_OP_BDC: return "BDC";
    case CONTENT_OP_BI: return "BI";
    case CONTENT_OP_BMC: return "BMC";
    case CONTENT_OP_BT: return "BT";
    case CONTENT_OP_BX: return "BX";
    case CONTENT_OP_c: return "c";
    case CONTENT_OP_cm: return "cm";
    case CONTENT_OP_CS: return "CS";
    case CONTENT_OP_cs: return "cs";
    case CONTENT_OP_d: return "d";
    case CONTENT_OP_d0: return "d0";
    case CONTENT_OP_d1: return "d1";
    case CONTENT_OP_Do: return "Do";
    case CONTENT_OP_DP: return "DP";
    case CONTENT_OP_EI: return "EI";
    case CONTENT_OP_EMC: return "EMC";
    case CONTENT_OP_ET: return "ET";
    case CONTENT_OP_EX: return "EX";
    case CONTENT_OP_f: return "f";
    case CONTENT_OP_F: return "F";
    case CONTENT_OP_f_: return "f*";
    case CONTENT_OP_G: return "G";
    case CONTENT_OP_g: return "g";
    case CONTENT_OP_gs: return "gs";
    case CONTENT_OP_h: return "h";
    case CONTENT_OP_i: return "i";
    case CONTENT_OP_ID: return "ID";
    case CONTENT_OP_j: return "j";
    case CONTENT_OP_J: return "J";
    case CONTENT_OP_K: return "K";
    case CONTENT_OP_k: return "k";
    case CONTENT_OP_l: return "l";
    case CONTENT_OP_m: return "m";
    case CONTENT_OP_M: return "M";
    case CONTENT_OP_MP: return "MP";
    case CONTENT_OP_n: return "n";
    case CONTENT_OP_q: return "q";
    case CONTENT_OP_Q: return "Q";
    case CONTENT_OP_re: return "re";
    case CONTENT_OP_RG: return "RG";
    case CONTENT_OP_rg: return "rg";
    case CONTENT_OP_ri: return "ri";
    case CONTENT_OP_s: return "s";
    case CONTENT_OP_S: return "S";
    case CONTENT_OP_SC: return "SC";
    case CONTENT_OP_sc: return "sc";
    case CONTENT_OP_SCN: return "SCN";
    case CONTENT_OP_scn: return "scn";
    case CONTENT_OP_sh: return "sh";
    case CONTENT_OP_T_: return "T*";
    case CONTENT_OP_Tc: return "Tc";
    case CONTENT_OP_Td: return "Td";
    case CONTENT_OP_TD: return "TD";
    case CONTENT_OP_Tf: return "Tf";
    case CONTENT_OP_Tj: return "Tj";
    case CONTENT_OP_TJ: return "TJ";
    case CONTENT_OP_TL: return "TL";
    case CONTENT_OP_Tm: return "Tm";
    case CONTENT_OP_Tr: return "Tr";
    case CONTENT_OP_Ts: return "Ts";
    case CONTENT_OP_Tw: return "Tw";
    case CONTENT_OP_Tz: return "Tz";
    case CONTENT_OP_v: return "v";
    case CONTENT_OP_w: return "w";
    case CONTENT_OP_W: return "W";
    case CONTENT_OP_W_: return "W_";
    case CONTENT_OP_y: return "y";
    case CONTENT_OP__: return "\'";
    case CONTENT_OP___: return "\"";
    }
    return "????";
}


/**
 * move number operands from list into operation
 *
 * This ensures all operands are correctly handled not just the wanted ones
 *
 * \param wanted The number of wanted operands to place in the operation
 * \param operands The array of operands from the parse
 * \param operand_idx The number of operands from the parse
 * \param operation_out The operation to place numbers in
 */
static nspdferror
copy_numbers(unsigned int wanted,
             struct cos_object **operands,
             unsigned int *operand_idx,
             struct content_operation *operation_out)
{
    nspdferror res;
    unsigned int index = 0;

    while ((index < (*operand_idx)) &&
           (index < wanted)) {
        /* process wanted operands */
        res = cos_get_number(NULL,
                             *(operands + index),
                             &operation_out->u.number[index]);
        if (res != NSPDFERROR_OK) {
            printf("operand %d could not be set in operation (code %d)\n",
                   index, res);
        }
        cos_free_object(*(operands + index));
        index++;
    }
    if ((*operand_idx) > index) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), wanted, *operand_idx);
        while (index < (*operand_idx)) {
            cos_free_object(*(operands + index));
            index++;
        }
    } else if ((*operand_idx) < index) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), wanted, *operand_idx);
    }

    *operand_idx = 0; /* all operands freed */

    return NSPDFERROR_OK;
}

static nspdferror
copy_integers(unsigned int wanted,
             struct cos_object **operands,
             unsigned int *operand_idx,
             struct content_operation *operation_out)
{
    nspdferror res;
    unsigned int index = 0;

    while ((index < (*operand_idx)) &&
           (index < wanted)) {
        /* process wanted operands */
        res = cos_get_int(NULL,
                             *(operands + index),
                             &operation_out->u.i[index]);
        if (res != NSPDFERROR_OK) {
            printf("operand %d could not be set in operation (code %d)\n",
                   index, res);
        }
        cos_free_object(*(operands + index));
        index++;
    }
    if ((*operand_idx) > index) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), wanted, *operand_idx);
        while (index < (*operand_idx)) {
            cos_free_object(*(operands + index));
            index++;
        }
    } else if ((*operand_idx) < index) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), wanted, *operand_idx);
    }

    *operand_idx = 0; /* all operands freed */

    return NSPDFERROR_OK;
}

static nspdferror
copy_string(struct cos_object **operands,
             unsigned int *operand_idx,
             struct content_operation *operation_out)
{
    nspdferror res;
    unsigned int index = 0;
    struct cos_string *string;

    if ((*operand_idx) == 0) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), 1, *operand_idx);
        operation_out->u.string.length = 0;
        return NSPDFERROR_OK;
    }

    /* process wanted operands */
    res = cos_get_string(NULL, *operands, &string);
    if (res != NSPDFERROR_OK) {
        printf("string could not be set in operation (code %d)\n", res);
        operation_out->u.string.length = 0;
    } else {
        operation_out->u.string.length = string->length;
        if (string->length > content_string_intrnl_lngth) {
            /* steal the string from the object */
            operation_out->u.string.u.pdata = string->data;
            string->alloc = 0;
            string->length = 0;
            /*printf("external string \"%.*s\"\n",
              operation_out->u.string.length,
              operation_out->u.string.u.pdata);*/
        } else {
            memcpy(operation_out->u.string.u.cdata,
                   string->data,
                   string->length);
            /*printf("internal string \"%.*s\"\n",
              operation_out->u.string.length,
              operation_out->u.string.u.cdata);*/
        }
    }

    if ((*operand_idx) > 1) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), 1, *operand_idx);
    }

    /* free all operands */
    while (index < (*operand_idx)) {
        cos_free_object(*(operands + index));
        index++;
    }
    *operand_idx = 0;

    return NSPDFERROR_OK;
}

static nspdferror
copy_array(struct cos_object **operands,
             unsigned int *operand_idx,
             struct content_operation *operation_out)
{
    unsigned int index = 0;

    if ((*operand_idx) == 0) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), 1, *operand_idx);
        operation_out->u.array.length = 0;
        return NSPDFERROR_OK;
    }

    /* process wanted operands */
    if ((*operands)->type != COS_TYPE_ARRAY) {
        printf("operand was not an array\n");
        operation_out->u.array.length = 0;
    } else {
        operation_out->u.array.length = (*operands)->u.array->length;
        /* steal the values from the array object */
        operation_out->u.array.values = (*operands)->u.array->values;
        (*operands)->u.array->alloc = 0;
        (*operands)->u.array->length = 0;
    }

    if ((*operand_idx) > 1) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), 1, *operand_idx);
    }

    /* free all operands */
    while (index < (*operand_idx)) {
        cos_free_object(*(operands + index));
        index++;
    }
    *operand_idx = 0;

    return NSPDFERROR_OK;
}


static nspdferror
copy_name(struct cos_object **operands,
          unsigned int *operand_idx,
          struct content_operation *operation_out)
{
    unsigned int index = 0;

    if ((*operand_idx) == 0) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), 1, *operand_idx);
        operation_out->u.name = NULL;
        return NSPDFERROR_OK;
    }

    /* process wanted operands */
    if ((*operands)->type != COS_TYPE_NAME) {
        printf("operand was not a name\n");
        operation_out->u.name = NULL;
    } else {
        /* steal the name from the name object */
        operation_out->u.name = (*operands)->u.name;
        (*operands)->u.name = NULL;
    }

    if ((*operand_idx) > 1) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), 1, *operand_idx);
    }

    /* free all operands */
    while (index < (*operand_idx)) {
        cos_free_object(*(operands + index));
        index++;
    }
    *operand_idx = 0;

    return NSPDFERROR_OK;
}

static nspdferror
copy_name_number(struct cos_object **operands,
             unsigned int *operand_idx,
             struct content_operation *operation_out)
{
    unsigned int index = 0;

    if ((*operand_idx) == 0) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), 2, *operand_idx);
        operation_out->u.namenumber.name = NULL;
        return NSPDFERROR_OK;
    }

    /* process wanted operands */
    if ((*operands)->type != COS_TYPE_NAME) {
        printf("operand was not a name\n");
        operation_out->u.namenumber.name = NULL;
    } else {
        /* steal the name from the name object */
        operation_out->u.namenumber.name = (*operands)->u.name;
        (*operands)->u.name = NULL;

        operation_out->u.namenumber.number = 0;
        /* get the number */
        if ((*operand_idx) > 1) {
            nspdferror res;
            res = cos_get_number(NULL,
                                 *(operands + 1),
                                 &operation_out->u.namenumber.number);
            if (res != NSPDFERROR_OK) {
                printf("operand 1 could not be set in operation (code %d)\n", res);
            }
        } else {
            printf("operator %s that takes %d operands passed %d\n",
                   operator_name(operation_out->operator), 2, *operand_idx);
        }
    }

    if ((*operand_idx) > 2) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), 2, *operand_idx);
    }

    /* free all operands */
    while (index < (*operand_idx)) {
        cos_free_object(*(operands + index));
        index++;
    }
    *operand_idx = 0;

    return NSPDFERROR_OK;
}


static nspdferror
copy_array_int(struct cos_object **operands,
             unsigned int *operand_idx,
             struct content_operation *operation_out)
{
    unsigned int index = 0;

    if ((*operand_idx) == 0) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), 2, *operand_idx);
        operation_out->u.namenumber.name = NULL;
        return NSPDFERROR_OK;
    }

    /* process wanted operands */
    if ((*operands)->type != COS_TYPE_ARRAY) {
        printf("operand was not an array\n");
        operation_out->u.arrayint.length = 0;
        operation_out->u.arrayint.values = NULL;
        operation_out->u.arrayint.i = 0;
    } else {
        operation_out->u.arrayint.length = (*operands)->u.array->length;
        /* steal the values from the array object */
        operation_out->u.arrayint.values = (*operands)->u.array->values;
        (*operands)->u.array->alloc = 0;
        (*operands)->u.array->length = 0;

        operation_out->u.arrayint.i = 0;
        /* get the int */
        if ((*operand_idx) > 1) {
            nspdferror res;
            res = cos_get_int(NULL, *(operands + 1), &operation_out->u.arrayint.i);
            if (res != NSPDFERROR_OK) {
                printf("operand 1 could not be set in operation (code %d)\n", res);
            }
        } else {
            printf("operator %s that takes %d operands passed %d\n",
                   operator_name(operation_out->operator), 2, *operand_idx);
        }
    }

    if ((*operand_idx) > 2) {
        printf("operator %s that takes %d operands passed %d\n",
               operator_name(operation_out->operator), 2, *operand_idx);
    }

    /* free all operands */
    while (index < (*operand_idx)) {
        cos_free_object(*(operands + index));
        index++;
    }
    *operand_idx = 0;

    return NSPDFERROR_OK;
}

/* exported interface documented in cos_content.h */
nspdferror
nspdf__cos_content_convert(enum content_operator operator,
                           struct cos_object **operands,
                           unsigned int *operand_idx,
                           struct content_operation *operation_out)
{
    nspdferror res;

    operation_out->operator = operator;

    switch (operator) {
    case CONTENT_OP_b:
    case CONTENT_OP_B:
    case CONTENT_OP_b_:
    case CONTENT_OP_B_:
    case CONTENT_OP_BI:
    case CONTENT_OP_BT:
    case CONTENT_OP_BX:
    case CONTENT_OP_EI:
    case CONTENT_OP_EMC:
    case CONTENT_OP_ET:
    case CONTENT_OP_EX:
    case CONTENT_OP_f:
    case CONTENT_OP_F:
    case CONTENT_OP_f_:
    case CONTENT_OP_h:
    case CONTENT_OP_ID:
    case CONTENT_OP_n:
    case CONTENT_OP_q:
    case CONTENT_OP_Q:
    case CONTENT_OP_s:
    case CONTENT_OP_S:
    case CONTENT_OP_T_:
    case CONTENT_OP_W:
    case CONTENT_OP_W_:
        /* no operands */
        res = copy_numbers(0, operands, operand_idx, operation_out);
        break;

    case CONTENT_OP_G:
    case CONTENT_OP_g:
    case CONTENT_OP_i:
    case CONTENT_OP_M:
    case CONTENT_OP_Tc:
    case CONTENT_OP_TL:
    case CONTENT_OP_Ts:
    case CONTENT_OP_Tw:
    case CONTENT_OP_Tz:
    case CONTENT_OP_w:
        /* one number */
        res = copy_numbers(1, operands, operand_idx, operation_out);
        break;

    case CONTENT_OP_d0:
    case CONTENT_OP_l:
    case CONTENT_OP_m:
    case CONTENT_OP_Td:
    case CONTENT_OP_TD:
        /* two numbers */
        res = copy_numbers(2, operands, operand_idx, operation_out);
        break;

    case CONTENT_OP_RG:
    case CONTENT_OP_rg:
        /* three numbers */
        res = copy_numbers(3, operands, operand_idx, operation_out);
        break;

    case CONTENT_OP_K:
    case CONTENT_OP_k:
    case CONTENT_OP_re:
    case CONTENT_OP_v:
    case CONTENT_OP_y:
        /* four numbers */
        res = copy_numbers(4, operands, operand_idx, operation_out);
        break;

    case CONTENT_OP_c:
    case CONTENT_OP_cm:
    case CONTENT_OP_d1:
    case CONTENT_OP_Tm:
        /* six numbers */
        res = copy_numbers(6, operands, operand_idx, operation_out);
        break;

    case CONTENT_OP_Tj:
    case CONTENT_OP__:
        /* single string */
        res = copy_string(operands, operand_idx, operation_out);
        break;

    case CONTENT_OP_TJ:
        /* single array */
        res = copy_array(operands, operand_idx, operation_out);
        break;

    case CONTENT_OP_Tf:
        /* name and number */
        res = copy_name_number(operands, operand_idx, operation_out);
        break;

    case CONTENT_OP_gs:
    case CONTENT_OP_Do:
    case CONTENT_OP_ri:
    case CONTENT_OP_CS:
    case CONTENT_OP_cs:
    case CONTENT_OP_sh:
    case CONTENT_OP_MP:
    case CONTENT_OP_BMC:
        /* name */
        res = copy_name(operands, operand_idx, operation_out);
        break;

    case CONTENT_OP_j:
    case CONTENT_OP_J:
    case CONTENT_OP_Tr:
        /* one integer */
        res = copy_integers(1, operands, operand_idx, operation_out);
        break;

    case CONTENT_OP_d:
        /* array and int */
        res = copy_array_int(operands, operand_idx, operation_out);
        break;

    case CONTENT_OP_BDC:
    case CONTENT_OP_DP:
    case CONTENT_OP_SC:
    case CONTENT_OP_sc:
    case CONTENT_OP_SCN:
    case CONTENT_OP_scn:
    case CONTENT_OP___:
        res = copy_numbers(0, operands, operand_idx, operation_out);
        break;
    }

    return res;
}