-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
pdfio-dict.c
1075 lines (806 loc) · 25.4 KB
/
pdfio-dict.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
//
// PDF dictionary 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 int compare_pairs(_pdfio_pair_t *a, _pdfio_pair_t *b);
//
// 'pdfioDictClear()' - Remove a key/value pair from a dictionary.
//
bool // O - `true` if cleared, `false` otherwise
pdfioDictClear(pdfio_dict_t *dict, // I - Dictionary
const char *key) // I - Key
{
size_t idx; // Index into pairs
_pdfio_pair_t *pair, // Current pair
pkey; // Search key
PDFIO_DEBUG("pdfioDictClear(dict=%p, key=\"%s\")\n", dict, key);
if (!dict || !key)
return (false);
// See if the key is already set...
if (dict->num_pairs > 0)
{
pkey.key = key;
if ((pair = (_pdfio_pair_t *)bsearch(&pkey, dict->pairs, dict->num_pairs, sizeof(_pdfio_pair_t), (int (*)(const void *, const void *))compare_pairs)) != NULL)
{
// Yes, remove it...
if (pair->value.type == PDFIO_VALTYPE_BINARY)
free(pair->value.value.binary.data);
idx = (size_t)(pair - dict->pairs);
dict->num_pairs --;
if (idx < dict->num_pairs)
memmove(pair, pair + 1, (dict->num_pairs - idx) * sizeof(_pdfio_pair_t));
return (true);
}
}
return (false);
}
//
// 'pdfioDictCopy()' - Copy a dictionary to a PDF file.
//
pdfio_dict_t * // O - New dictionary
pdfioDictCopy(pdfio_file_t *pdf, // I - PDF file
pdfio_dict_t *dict) // I - Original dictionary
{
pdfio_dict_t *ndict; // New dictionary
size_t i; // Looping var
_pdfio_pair_t *p; // Current source pair
const char *key; // Current destination key
_pdfio_value_t v; // Current destination value
PDFIO_DEBUG("pdfioDictCopy(pdf=%p, dict=%p(%p))\n", pdf, dict, dict ? dict->pdf : NULL);
// Create the new dictionary...
if ((ndict = pdfioDictCreate(pdf)) == NULL)
return (NULL);
// Pre-allocate the pairs array to make this a little faster...
if ((ndict->pairs = (_pdfio_pair_t *)malloc(dict->num_pairs * sizeof(_pdfio_pair_t))) == NULL)
return (NULL); // Let pdfioFileClose do the cleanup...
ndict->alloc_pairs = dict->num_pairs;
// Copy and add each of the source dictionary's key/value pairs...
for (i = dict->num_pairs, p = dict->pairs; i > 0; i --, p ++)
{
if (!strcmp(p->key, "Length") && p->value.type == PDFIO_VALTYPE_INDIRECT && dict->pdf != pdf)
{
// Don't use indirect stream lengths for copied objects...
pdfio_obj_t *lenobj = pdfioFileFindObj(dict->pdf, p->value.value.indirect.number);
// Length object
v.type = PDFIO_VALTYPE_NUMBER;
if (lenobj)
{
if (lenobj->value.type == PDFIO_VALTYPE_NONE)
_pdfioObjLoad(lenobj);
v.value.number = lenobj->value.value.number;
}
else
v.value.number = 0.0;
}
else if (!_pdfioValueCopy(pdf, &v, dict->pdf, &p->value))
return (NULL); // Let pdfioFileClose do the cleanup...
if (_pdfioStringIsAllocated(dict->pdf, p->key))
key = pdfioStringCreate(pdf, p->key);
else
key = p->key;
if (!key)
return (NULL); // Let pdfioFileClose do the cleanup...
// Cannot fail since we already allocated space for the pairs...
_pdfioDictSetValue(ndict, key, &v);
}
// Successfully copied the dictionary, so return it...
return (ndict);
}
//
// 'pdfioDictCreate()' - Create a dictionary to hold key/value pairs.
//
pdfio_dict_t * // O - New dictionary
pdfioDictCreate(pdfio_file_t *pdf) // I - PDF file
{
pdfio_dict_t *dict; // New dictionary
if (!pdf)
return (NULL);
if ((dict = (pdfio_dict_t *)calloc(1, sizeof(pdfio_dict_t))) == NULL)
return (NULL);
dict->pdf = pdf;
if (pdf->num_dicts >= pdf->alloc_dicts)
{
pdfio_dict_t **temp = (pdfio_dict_t **)realloc(pdf->dicts, (pdf->alloc_dicts + 16) * sizeof(pdfio_dict_t *));
if (!temp)
{
free(dict);
return (NULL);
}
pdf->dicts = temp;
pdf->alloc_dicts += 16;
}
pdf->dicts[pdf->num_dicts ++] = dict;
return (dict);
}
//
// '_pdfioDictDecrypt()' - Decrypt the values in a dictionary.
//
bool // O - `true` on success, `false` on error
_pdfioDictDecrypt(pdfio_file_t *pdf, // I - PDF file
pdfio_obj_t *obj, // I - Object
pdfio_dict_t *dict, // I - Dictionary
size_t depth) // I - Depth
{
size_t i; // Looping var
_pdfio_pair_t *pair; // Current pair
for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++)
{
if (strcmp(pair->key, "ID") && !_pdfioValueDecrypt(pdf, obj, &pair->value, depth + 1))
return (false);
}
return (true);
}
//
// '_pdfioDictDebug()' - Dump a dictionary to stderr.
//
void
_pdfioDictDebug(pdfio_dict_t *dict, // I - Dictionary
FILE *fp) // I - Output file
{
size_t i; // Looping var
_pdfio_pair_t *pair; // Current pair
if (!dict)
return;
for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++)
{
fprintf(fp, "/%s", pair->key);
_pdfioValueDebug(&pair->value, fp);
}
}
//
// '_pdfioDictDelete()' - Free the memory used by a dictionary.
//
void
_pdfioDictDelete(pdfio_dict_t *dict) // I - Dictionary
{
if (dict)
{
size_t i; // Looping var
_pdfio_pair_t *pair; // Current pair
for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++)
{
if (pair->value.type == PDFIO_VALTYPE_BINARY)
free(pair->value.value.binary.data);
}
free(dict->pairs);
}
free(dict);
}
//
// 'pdfioDictGetArray()' - Get a key array value from a dictionary.
//
pdfio_array_t * // O - Value
pdfioDictGetArray(pdfio_dict_t *dict, // I - Dictionary
const char *key) // I - Key
{
_pdfio_value_t *value = _pdfioDictGetValue(dict, key);
if (value && value->type == PDFIO_VALTYPE_ARRAY)
return (value->value.array);
else
return (NULL);
}
//
// 'pdfioDictGetBinary()' - Get a key binary string value from a dictionary.
//
unsigned char * // O - Value
pdfioDictGetBinary(pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
size_t *length)// O - Length of value
{
_pdfio_value_t *value = _pdfioDictGetValue(dict, key);
if (!length)
return (NULL);
if (value && value->type == PDFIO_VALTYPE_BINARY)
{
*length = value->value.binary.datalen;
return (value->value.binary.data);
}
else if (value && value->type == PDFIO_VALTYPE_STRING)
{
*length = strlen(value->value.string);
return ((unsigned char *)value->value.string);
}
else
{
*length = 0;
return (NULL);
}
}
//
// 'pdfioDictGetBoolean()' - Get a key boolean value from a dictionary.
//
bool // O - Value
pdfioDictGetBoolean(pdfio_dict_t *dict, // I - Dictionary
const char *key) // I - Key
{
_pdfio_value_t *value = _pdfioDictGetValue(dict, key);
if (value && value->type == PDFIO_VALTYPE_BOOLEAN)
return (value->value.boolean);
else
return (false);
}
//
// 'pdfioDictGetDate()' - Get a date value from a dictionary.
//
time_t // O - Value
pdfioDictGetDate(pdfio_dict_t *dict, // I - Dictionary
const char *key) // I - Key
{
_pdfio_value_t *value = _pdfioDictGetValue(dict, key);
if (value && value->type == PDFIO_VALTYPE_DATE)
return (value->value.date);
else
return (0);
}
//
// 'pdfioDictGetDict()' - Get a key dictionary value from a dictionary.
//
pdfio_dict_t * // O - Value
pdfioDictGetDict(pdfio_dict_t *dict, // I - Dictionary
const char *key) // I - Key
{
_pdfio_value_t *value = _pdfioDictGetValue(dict, key);
if (value && value->type == PDFIO_VALTYPE_DICT)
return (value->value.dict);
else
return (NULL);
}
//
// 'pdfioDictGetKey()' - Get the key for the specified pair.
//
const char * // O - Key for specified pair
pdfioDictGetKey(pdfio_dict_t *dict, // I - Dictionary
size_t n) // I - Pair index (`0`-based)
{
return ((dict && n < dict->num_pairs) ? dict->pairs[n].key : NULL);
}
//
// 'pdfioDictGetName()' - Get a key name value from a dictionary.
//
const char * // O - Value
pdfioDictGetName(pdfio_dict_t *dict, // I - Dictionary
const char *key) // I - Key
{
_pdfio_value_t *value = _pdfioDictGetValue(dict, key);
if (value && value->type == PDFIO_VALTYPE_NAME)
return (value->value.name);
else
return (NULL);
}
//
// 'pdfioDictGetNumPairs()' - Get the number of key/value pairs in a dictionary.
//
size_t // O - Number of pairs
pdfioDictGetNumPairs(pdfio_dict_t *dict)// I - Dictionary
{
return (dict ? dict->num_pairs : 0);
}
//
// 'pdfioDictGetNumber()' - Get a key number value from a dictionary.
//
double // O - Value
pdfioDictGetNumber(pdfio_dict_t *dict, // I - Dictionary
const char *key) // I - Key
{
_pdfio_value_t *value = _pdfioDictGetValue(dict, key);
if (value && value->type == PDFIO_VALTYPE_NUMBER)
return (value->value.number);
else
return (0.0);
}
//
// 'pdfioDictGetObj()' - Get a key indirect object value from a dictionary.
//
pdfio_obj_t * // O - Value
pdfioDictGetObj(pdfio_dict_t *dict, // I - Dictionary
const char *key) // I - Key
{
_pdfio_value_t *value = _pdfioDictGetValue(dict, key);
if (value && value->type == PDFIO_VALTYPE_INDIRECT)
return (pdfioFileFindObj(dict->pdf, value->value.indirect.number));
else
return (NULL);
}
//
// 'pdfioDictGetRect()' - Get a key rectangle value from a dictionary.
//
pdfio_rect_t * // O - Rectangle
pdfioDictGetRect(pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
pdfio_rect_t *rect) // I - Rectangle
{
_pdfio_value_t *value = _pdfioDictGetValue(dict, key);
if (value && value->type == PDFIO_VALTYPE_ARRAY && pdfioArrayGetSize(value->value.array) == 4)
{
rect->x1 = pdfioArrayGetNumber(value->value.array, 0);
rect->y1 = pdfioArrayGetNumber(value->value.array, 1);
rect->x2 = pdfioArrayGetNumber(value->value.array, 2);
rect->y2 = pdfioArrayGetNumber(value->value.array, 3);
return (rect);
}
else
{
memset(rect, 0, sizeof(pdfio_rect_t));
return (NULL);
}
}
//
// 'pdfioDictGetString()' - Get a key string value from a dictionary.
//
const char * // O - Value
pdfioDictGetString(pdfio_dict_t *dict, // I - Dictionary
const char *key) // I - Key
{
_pdfio_value_t *value = _pdfioDictGetValue(dict, key);
if (value && value->type == PDFIO_VALTYPE_STRING)
{
return (value->value.string);
}
else if (value && value->type == PDFIO_VALTYPE_BINARY && value->value.binary.datalen < 4096)
{
// Convert binary string to regular string...
char temp[4096]; // Temporary string
memcpy(temp, value->value.binary.data, value->value.binary.datalen);
temp[value->value.binary.datalen] = '\0';
free(value->value.binary.data);
value->type = PDFIO_VALTYPE_STRING;
value->value.string = pdfioStringCreate(dict->pdf, temp);
return (value->value.string);
}
else
{
return (NULL);
}
}
//
// 'pdfioDictGetType()' - Get a key value type from a dictionary.
//
pdfio_valtype_t // O - Value type
pdfioDictGetType(pdfio_dict_t *dict, // I - Dictionary
const char *key) // I - Key
{
_pdfio_value_t *value = _pdfioDictGetValue(dict, key);
return (value ? value->type : PDFIO_VALTYPE_NONE);
}
//
// '_pdfioDictGetValue()' - Get a key value from a dictionary.
//
_pdfio_value_t * // O - Value or `NULL` on error
_pdfioDictGetValue(pdfio_dict_t *dict, // I - Dictionary
const char *key) // I - Key
{
_pdfio_pair_t temp, // Search key
*match; // Matching key pair
PDFIO_DEBUG("_pdfioDictGetValue(dict=%p, key=\"%s\")\n", dict, key);
if (!dict || !dict->num_pairs || !key)
{
PDFIO_DEBUG("_pdfioDictGetValue: Returning NULL.\n");
return (NULL);
}
temp.key = key;
if ((match = bsearch(&temp, dict->pairs, dict->num_pairs, sizeof(_pdfio_pair_t), (int (*)(const void *, const void *))compare_pairs)) != NULL)
{
PDFIO_DEBUG("_pdfioDictGetValue: Match, returning ");
PDFIO_DEBUG_VALUE(&(match->value));
PDFIO_DEBUG(".\n");
return (&(match->value));
}
else
{
PDFIO_DEBUG("_pdfioDictGetValue: No match, returning NULL.\n");
return (NULL);
}
}
//
// 'pdfioDictIterateKeys()' - Iterate the keys in a dictionary.
//
// This function iterates the keys in a dictionary, calling the supplied
// function "cb":
//
// ```
// bool
// my_dict_cb(pdfio_dict_t *dict, const char *key, void *cb_data)
// {
// ... "key" contains the dictionary key ...
// ... return true to continue or false to stop ...
// }
// ```
//
// The iteration continues as long as the callback returns `true` or all keys
// have been iterated.
//
void
pdfioDictIterateKeys(
pdfio_dict_t *dict, // I - Dictionary
pdfio_dict_cb_t cb, // I - Callback function
void *cb_data) // I - Callback data
{
size_t i; // Looping var
_pdfio_pair_t *pair; // Current pair
// Range check input...
if (!dict || !cb)
return;
for (i = dict->num_pairs, pair = dict->pairs; i > 0; i --, pair ++)
{
if (!(cb)(dict, pair->key, cb_data))
break;
}
}
//
// '_pdfioDictRead()' - Read a dictionary from a PDF file.
//
// At this point we've seen the initial "<<"...
//
pdfio_dict_t * // O - New dictionary
_pdfioDictRead(pdfio_file_t *pdf, // I - PDF file
pdfio_obj_t *obj, // I - Object, if any
_pdfio_token_t *tb, // I - Token buffer/stack
size_t depth) // I - Depth of dictionary
{
pdfio_dict_t *dict; // New dictionary
char key[256]; // Dictionary key
_pdfio_value_t value; // Dictionary value
PDFIO_DEBUG("_pdfioDictRead(pdf=%p, obj=%p, tb=%p, depth=%lu)\n", pdf, obj, tb, (unsigned long)depth);
// Create a dictionary and start reading...
if ((dict = pdfioDictCreate(pdf)) == NULL)
return (NULL);
while (_pdfioTokenGet(tb, key, sizeof(key)))
{
// Get the next key or end-of-dictionary...
if (!strcmp(key, ">>"))
{
// End of dictionary...
PDFIO_DEBUG("_pdfioDictRead: Returning dictionary value...\n");
return (dict);
}
else if (key[0] != '/')
{
_pdfioFileError(pdf, "Invalid dictionary contents.");
break;
}
else if (_pdfioDictGetValue(dict, key + 1))
{
_pdfioFileError(pdf, "Duplicate dictionary key '%s'.", key + 1);
return (NULL);
}
// Then get the next value...
PDFIO_DEBUG("_pdfioDictRead: Reading value for '%s'.\n", key + 1);
if (!_pdfioValueRead(pdf, obj, tb, &value, depth))
{
_pdfioFileError(pdf, "Missing value for dictionary key '%s'.", key + 1);
break;
}
if (!_pdfioDictSetValue(dict, pdfioStringCreate(pdf, key + 1), &value))
break;
PDFIO_DEBUG("_pdfioDictRead: Set %s.\n", key);
}
// Dictionary is invalid - pdfioFileClose will free the memory, return NULL
// to indicate an error...
return (NULL);
}
//
// 'pdfioDictSetArray()' - Set a key array in a dictionary.
//
bool // O - `true` on success, `false` on failure
pdfioDictSetArray(pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
pdfio_array_t *value) // I - Value
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value)
return (false);
// Set the key/value pair...
temp.type = PDFIO_VALTYPE_ARRAY;
temp.value.array = value;
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetBinary()' - Set a key binary string in a dictionary.
//
bool // O - `true` on success, `false` on failure
pdfioDictSetBinary(
pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
const unsigned char *value, // I - Value
size_t valuelen) // I - Length of value
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value || !valuelen)
return (false);
// Set the key/value pair...
temp.type = PDFIO_VALTYPE_BINARY;
temp.value.binary.datalen = valuelen;
if ((temp.value.binary.data = (unsigned char *)malloc(valuelen)) == NULL)
return (false);
memcpy(temp.value.binary.data, value, valuelen);
if (!_pdfioDictSetValue(dict, key, &temp))
{
free(temp.value.binary.data);
return (false);
}
return (true);
}
//
// 'pdfioDictSetBoolean()' - Set a key boolean in a dictionary.
//
bool // O - `true` on success, `false` on failure
pdfioDictSetBoolean(pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
bool value) // I - Value
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key)
return (false);
// Set the key/value pair...
temp.type = PDFIO_VALTYPE_BOOLEAN;
temp.value.boolean = value;
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetDate()' - Set a date value in a dictionary.
//
bool // O - `true` on success, `false` on failure
pdfioDictSetDate(pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
time_t value) // I - Value
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key)
return (false);
// Set the key/value pair...
temp.type = PDFIO_VALTYPE_DATE;
temp.value.date = value;
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetDict()' - Set a key dictionary in a dictionary.
//
bool // O - `true` on success, `false` on failure
pdfioDictSetDict(pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
pdfio_dict_t *value) // I - Value
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value)
return (false);
// Set the key/value pair...
temp.type = PDFIO_VALTYPE_DICT;
temp.value.dict = value;
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetName()' - Set a key name in a dictionary.
//
bool // O - `true` on success, `false` on failure
pdfioDictSetName(pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
const char *value) // I - Value
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value)
return (false);
// Set the key/value pair...
temp.type = PDFIO_VALTYPE_NAME;
temp.value.name = value;
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetNull()' - Set a key null in a dictionary.
//
bool // O - `true` on success, `false` on failure
pdfioDictSetNull(pdfio_dict_t *dict, // I - Dictionary
const char *key) // I - Key
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key)
return (false);
// Set the key/value pair...
temp.type = PDFIO_VALTYPE_NULL;
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetNumber()' - Set a key number in a dictionary.
//
bool // O - `true` on success, `false` on failure
pdfioDictSetNumber(pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
double value) // I - Value
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key)
return (false);
// Set the key/value pair...
temp.type = PDFIO_VALTYPE_NUMBER;
temp.value.number = value;
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetObj()' - Set a key indirect object reference in a dictionary.
//
bool // O - `true` on success, `false` on failure
pdfioDictSetObj(pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
pdfio_obj_t *value) // I - Value
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value)
return (false);
// Set the key/value pair...
temp.type = PDFIO_VALTYPE_INDIRECT;
temp.value.indirect.number = value->number;
temp.value.indirect.generation = value->generation;
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetRect()' - Set a key rectangle in a dictionary.
//
bool // O - `true` on success, `false` on failure
pdfioDictSetRect(pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
pdfio_rect_t *value) // I - Value
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value)
return (false);
// Set the key/value pair...
temp.type = PDFIO_VALTYPE_ARRAY;
temp.value.array = pdfioArrayCreate(dict->pdf);
pdfioArrayAppendNumber(temp.value.array, value->x1);
pdfioArrayAppendNumber(temp.value.array, value->y1);
pdfioArrayAppendNumber(temp.value.array, value->x2);
pdfioArrayAppendNumber(temp.value.array, value->y2);
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetString()' - Set a key literal string in a dictionary.
//
bool // O - `true` on success, `false` on failure
pdfioDictSetString(pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
const char *value)// I - Value
{
_pdfio_value_t temp; // New value
// Range check input...
if (!dict || !key || !value)
return (false);
// Set the key/value pair...
temp.type = PDFIO_VALTYPE_STRING;
temp.value.string = value;
return (_pdfioDictSetValue(dict, key, &temp));
}
//
// 'pdfioDictSetStringf()' - Set a key formatted string in a dictionary.
//
bool // O - `true` on success, `false` on failure
pdfioDictSetStringf(
pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
const char *format, // I - `printf`-style format string
...) // I - Additional arguments as needed
{
char buffer[8192]; // String buffer
va_list ap; // Argument list
// Range check input...
if (!dict || !key || !format)
return (false);
// Set the key/value pair...
va_start(ap, format);
vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
return (pdfioDictSetString(dict, key, buffer));
}
//
// '_pdfioDictSetValue()' - Set a key value in a dictionary.
//
bool // O - `true` on success, `false` on failure
_pdfioDictSetValue(
pdfio_dict_t *dict, // I - Dictionary
const char *key, // I - Key
_pdfio_value_t *value) // I - Value
{
_pdfio_pair_t *pair; // Current pair
PDFIO_DEBUG("_pdfioDictSetValue(dict=%p, key=\"%s\", value=%p)\n", dict, key, (void *)value);
// See if the key is already set...
if (dict->num_pairs > 0)
{
_pdfio_pair_t pkey; // Search key
pkey.key = key;
if ((pair = (_pdfio_pair_t *)bsearch(&pkey, dict->pairs, dict->num_pairs, sizeof(_pdfio_pair_t), (int (*)(const void *, const void *))compare_pairs)) != NULL)
{
// Yes, replace the value...
PDFIO_DEBUG("_pdfioDictSetValue: Replacing existing value.\n");
if (pair->value.type == PDFIO_VALTYPE_BINARY)
free(pair->value.value.binary.data);
pair->value = *value;
return (true);
}
}
// Nope, add a pair...
if (dict->num_pairs >= dict->alloc_pairs)
{
// Expand the dictionary...
_pdfio_pair_t *temp = (_pdfio_pair_t *)realloc(dict->pairs, (dict->alloc_pairs + 8) * sizeof(_pdfio_pair_t));
if (!temp)
{
PDFIO_DEBUG("_pdfioDictSetValue: Out of memory.\n");
return (false);
}
dict->pairs = temp;
dict->alloc_pairs += 8;
}