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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
|
/* Copyright (C) 2001-2020 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
CA 94945, U.S.A., +1(415)492-9861, for further information.
*/
/* Interpreter implementations of parameter dictionaries */
#include "memory_.h"
#include "string_.h"
#include "ghost.h"
#include "ierrors.h"
#include "oper.h" /* for check_type */
#include "opcheck.h"
#include "ialloc.h"
#include "idict.h"
#include "imemory.h" /* for iutil.h */
#include "iname.h"
#include "istack.h"
#include "iparam.h"
#include "iutil.h" /* for num_params */
#include "ivmspace.h"
#include "store.h"
#include "gsstruct.h" /* for st_bytes */
/* ================ Utilities ================ */
/* Convert a key to a ref. */
static int
ref_param_key(const iparam_list * plist, gs_param_name pkey, ref * pkref)
{
if (plist->int_keys) {
long key;
if (sscanf(pkey, "%ld", &key) != 1)
return_error(gs_error_rangecheck);
make_int(pkref, key);
return 0;
} else
return name_ref(plist->memory, (const byte *)pkey, strlen(pkey), pkref, 0);
}
/* Fill in a gs_param_key_t from a name or int ref. */
static int
ref_to_key(const ref * pref, gs_param_key_t * key, iparam_list *plist)
{
if (r_has_type(pref, t_name)) {
ref nref;
name_string_ref(plist->memory, pref, &nref);
key->data = nref.value.const_bytes;
key->size = r_size(&nref);
key->persistent = false; /* names may be freed */
} else if (r_has_type(pref, t_integer)) {
char istr[sizeof(long) * 8 / 3 + 2];
int len;
byte *buf;
gs_sprintf(istr, "%"PRIpsint, pref->value.intval);
len = strlen(istr);
/* GC will take care of freeing this: */
buf = gs_alloc_string(plist->memory, len, "ref_to_key");
if (!buf)
return_error(gs_error_VMerror);
key->data = buf;
key->size = len;
key->persistent = true;
} else
return_error(gs_error_typecheck);
return 0;
}
/* ================ Writing parameters to refs ================ */
/* Forward references */
static int array_new_indexed_plist_write(dict_param_list *plist,
ref *parray, const ref *pwanted,
gs_ref_memory_t *imem);
/* ---------------- Generic writing procedures ---------------- */
static param_proc_begin_xmit_collection(ref_param_begin_write_collection);
static param_proc_end_xmit_collection(ref_param_end_write_collection);
static param_proc_xmit_typed(ref_param_write_typed);
static param_proc_next_key(ref_param_get_next_key);
static param_proc_requested(ref_param_requested);
static const gs_param_list_procs ref_write_procs =
{
ref_param_write_typed,
ref_param_begin_write_collection,
ref_param_end_write_collection,
ref_param_get_next_key,
NULL, /* request */
ref_param_requested
};
static int ref_array_param_requested(const iparam_list *, gs_param_name,
ref *, uint, client_name_t);
static int ref_param_write(iparam_list *, gs_param_name, const ref *);
static int ref_param_write_string_value(ref *, const gs_param_string *,
gs_ref_memory_t *);
static int ref_param_write_name_value(const gs_memory_t *mem, ref *, const gs_param_string *);
static int
ref_param_make_int(ref *pe, const void *pvalue, uint i, gs_ref_memory_t *imem)
{
make_tav(pe, t_integer, imemory_new_mask(imem), intval,
((const gs_param_int_array *)pvalue)->data[i]);
return 0;
}
static int
ref_param_make_float(ref *pe, const void *pvalue, uint i, gs_ref_memory_t *imem)
{
make_tav(pe, t_real, imemory_new_mask(imem), realval,
((const gs_param_float_array *)pvalue)->data[i]);
return 0;
}
static int
ref_param_make_string(ref *pe, const void *pvalue, uint i, gs_ref_memory_t *imem)
{
return ref_param_write_string_value(pe,
&((const gs_param_string_array *)pvalue)->data[i],
imem);
}
static int
ref_param_make_name(ref * pe, const void *pvalue, uint i, gs_ref_memory_t *imem)
{
return ref_param_write_name_value((const gs_memory_t *)imem, pe,
&((const gs_param_string_array *)pvalue)->data[i]);
}
static int
ref_param_write_typed_array(gs_param_list * plist, gs_param_name pkey,
void *pvalue, uint count,
int (*make)(ref *, const void *, uint,
gs_ref_memory_t *))
{
iparam_list *const iplist = (iparam_list *) plist;
ref value;
uint i;
ref *pe;
int code;
if ((code = ref_array_param_requested(iplist, pkey, &value, count,
"ref_param_write_typed_array")) <= 0)
return code;
for (i = 0, pe = value.value.refs; i < count; ++i, ++pe)
if ((code = (*make) (pe, pvalue, i, iplist->ref_memory)) < 0)
return code;
return ref_param_write(iplist, pkey, &value);
}
static int
ref_param_begin_write_collection(gs_param_list * plist, gs_param_name pkey,
gs_param_dict * pvalue,
gs_param_collection_type_t coll_type)
{
iparam_list *const iplist = (iparam_list *) plist;
gs_ref_memory_t *imem = iplist->ref_memory;
dict_param_list *dlist = (dict_param_list *)
gs_alloc_bytes(plist->memory, size_of(dict_param_list),
"ref_param_begin_write_collection");
int code;
if (dlist == 0)
return_error(gs_error_VMerror);
if (coll_type != gs_param_collection_array) {
ref dref;
code = dict_alloc(imem, pvalue->size, &dref);
if (code >= 0) {
code = dict_param_list_write(dlist, &dref, NULL, imem);
dlist->int_keys = coll_type == gs_param_collection_dict_int_keys;
}
} else {
ref aref;
code = gs_alloc_ref_array(imem, &aref, a_all, pvalue->size,
"ref_param_begin_write_collection");
if (code >= 0)
code = array_new_indexed_plist_write(dlist, &aref, NULL, imem);
}
if (code < 0)
gs_free_object(plist->memory, dlist, "ref_param_begin_write_collection");
else
pvalue->list = (gs_param_list *) dlist;
return code;
}
static int
ref_param_end_write_collection(gs_param_list * plist, gs_param_name pkey,
gs_param_dict * pvalue)
{
iparam_list *const iplist = (iparam_list *) plist;
int code = ref_param_write(iplist, pkey,
&((dict_param_list *) pvalue->list)->dict);
gs_free_object(plist->memory, pvalue->list, "ref_param_end_write_collection");
pvalue->list = 0;
return code;
}
static int
ref_param_write_typed(gs_param_list * plist, gs_param_name pkey,
gs_param_typed_value * pvalue)
{
iparam_list *const iplist = (iparam_list *) plist;
ref value;
int code = 0;
switch (pvalue->type) {
case gs_param_type_null:
make_null(&value);
break;
case gs_param_type_bool:
make_bool(&value, pvalue->value.b);
break;
case gs_param_type_int:
make_int(&value, pvalue->value.i);
break;
case gs_param_type_long:
/* FIXME: Rangecheck? */
make_int(&value, pvalue->value.l);
break;
case gs_param_type_size_t:
/* FIXME: Rangecheck? */
make_int(&value, pvalue->value.z);
break;
case gs_param_type_i64:
/* FIXME: Rangecheck? */
make_int(&value, pvalue->value.i64);
break;
case gs_param_type_float:
make_real(&value, pvalue->value.f);
break;
case gs_param_type_string:
if (!ref_param_requested(plist, pkey))
return 0;
code = ref_param_write_string_value(&value, &pvalue->value.s,
iplist->ref_memory);
break;
case gs_param_type_name:
if (!ref_param_requested(plist, pkey))
return 0;
code = ref_param_write_name_value(iplist->memory, &value, &pvalue->value.n);
break;
case gs_param_type_int_array:
return ref_param_write_typed_array(plist, pkey, &pvalue->value.ia,
pvalue->value.ia.size,
ref_param_make_int);
case gs_param_type_float_array:
return ref_param_write_typed_array(plist, pkey, &pvalue->value.fa,
pvalue->value.fa.size,
ref_param_make_float);
case gs_param_type_string_array:
return ref_param_write_typed_array(plist, pkey, &pvalue->value.sa,
pvalue->value.sa.size,
ref_param_make_string);
case gs_param_type_name_array:
return ref_param_write_typed_array(plist, pkey, &pvalue->value.na,
pvalue->value.na.size,
ref_param_make_name);
case gs_param_type_dict:
case gs_param_type_dict_int_keys:
case gs_param_type_array:
return ref_param_begin_write_collection(plist, pkey,
&pvalue->value.d,
(gs_param_collection_type_t)(pvalue->type - gs_param_type_dict));
default:
return_error(gs_error_typecheck);
}
if (code < 0)
return code;
return ref_param_write(iplist, pkey, &value);
}
/* Check whether a given parameter was requested. */
static int
ref_param_requested(const gs_param_list * plist, gs_param_name pkey)
{
const iparam_list *const ciplist = (const iparam_list *)plist;
ref kref;
ref *ignore_value;
if (!r_has_type(&ciplist->u.w.wanted, t_dictionary))
return -1;
if (ref_param_key(ciplist, pkey, &kref) < 0)
return -1; /* catch it later */
return (dict_find(&ciplist->u.w.wanted, &kref, &ignore_value) > 0);
}
/* Check whether an array parameter is wanted, and allocate it if so. */
/* Return <0 on error, 0 if not wanted, 1 if wanted. */
static int
ref_array_param_requested(const iparam_list *iplist, gs_param_name pkey,
ref *pvalue, uint size, client_name_t cname)
{
int code;
if (!ref_param_requested((const gs_param_list *)iplist, pkey))
return 0;
code = gs_alloc_ref_array(iplist->ref_memory, pvalue, a_all, size, cname);
return (code < 0 ? code : 1);
}
/* ---------------- Internal routines ---------------- */
/* Prepare to write a string value. */
static int
ref_param_write_string_value(ref * pref, const gs_param_string * pvalue,
gs_ref_memory_t *imem)
{
const byte *pdata = pvalue->data;
uint n = pvalue->size;
if (pvalue->persistent)
make_const_string(pref, a_readonly | avm_foreign, n, pdata);
else {
byte *pstr = gs_alloc_string((gs_memory_t *)imem, n,
"ref_param_write_string");
if (pstr == 0)
return_error(gs_error_VMerror);
memcpy(pstr, pdata, n);
make_string(pref, a_readonly | imemory_space(imem), n, pstr);
}
return 0;
}
/* Prepare to write a name value. */
static int
ref_param_write_name_value(const gs_memory_t *mem, ref * pref, const gs_param_string * pvalue)
{
return name_ref(mem, pvalue->data, pvalue->size, pref,
(pvalue->persistent ? 0 : 1));
}
/* Generic routine for writing a ref parameter. */
static int
ref_param_write(iparam_list * plist, gs_param_name pkey, const ref * pvalue)
{
ref kref;
int code;
if (!ref_param_requested((gs_param_list *) plist, pkey))
return 0;
code = ref_param_key(plist, pkey, &kref);
if (code < 0)
return code;
return (*plist->u.w.write) (plist, &kref, pvalue);
}
/* ---------------- Implementations ---------------- */
/* Initialize for writing parameters. */
static void
ref_param_write_init(iparam_list * plist, const ref * pwanted,
gs_ref_memory_t *imem)
{
gs_param_list_init((gs_param_list *)plist, &ref_write_procs,
(gs_memory_t *)imem);
plist->ref_memory = imem;
if (pwanted == 0)
make_null(&plist->u.w.wanted);
else
plist->u.w.wanted = *pwanted;
plist->results = 0;
plist->int_keys = false;
}
/* Implementation for getting parameters to a stack. */
static int
stack_param_write(iparam_list * plist, const ref * pkey, const ref * pvalue)
{
stack_param_list *const splist = (stack_param_list *) plist;
ref_stack_t *pstack = splist->pstack;
s_ptr p = pstack->p;
if (pstack->top - p < 2) {
int code = ref_stack_push(pstack, 2);
if (code < 0)
return code;
*ref_stack_index(pstack, 1) = *pkey;
p = pstack->p;
} else {
pstack->p = p += 2;
p[-1] = *pkey;
}
*p = *pvalue;
splist->count++;
return 0;
}
/* Implementation for enumerating parameters on a stack */
static int /* ret 0 ok, 1 if EOF, or -ve err */
stack_param_enumerate(iparam_list * plist, gs_param_enumerator_t * penum,
gs_param_key_t * key, ref_type * type)
{
int code;
stack_param_list *const splist = (stack_param_list *) plist;
int index = penum->intval;
ref *stack_element;
do {
stack_element =
ref_stack_index(splist->pstack, index + 1 + splist->skip);
if (!stack_element)
return 1;
} while (index += 2, !r_has_type(stack_element, t_name));
*type = r_type(stack_element);
code = ref_to_key(stack_element, key, plist);
penum->intval = index;
return code;
}
int
stack_param_list_write(stack_param_list * plist, ref_stack_t * pstack,
const ref * pwanted, gs_ref_memory_t *imem)
{
plist->u.w.write = stack_param_write;
ref_param_write_init((iparam_list *) plist, pwanted, imem);
plist->enumerate = stack_param_enumerate;
plist->pstack = pstack;
plist->skip = 0;
plist->count = 0;
return 0;
}
/* Implementation for getting parameters to a dictionary. */
static int
dict_param_write(iparam_list * plist, const ref * pkey, const ref * pvalue)
{
int code =
dict_put(&((dict_param_list *) plist)->dict, pkey, pvalue, NULL);
return min(code, 0);
}
/* Implementation for enumerating parameters in a dictionary */
static int /* ret 0 ok, 1 if EOF, or -ve err */
dict_param_enumerate(iparam_list * plist, gs_param_enumerator_t * penum,
gs_param_key_t * key, ref_type * type)
{
ref elt[2];
int code;
dict_param_list *const pdlist = (dict_param_list *) plist;
int index =
(penum->intval != 0 ? penum->intval : dict_first(&pdlist->dict));
index = dict_next(&pdlist->dict, index, elt);
if (index < 0)
return 1;
*type = r_type(&elt[1]);
code = ref_to_key(&elt[0], key, plist);
penum->intval = index;
return code;
}
int
dict_param_list_write(dict_param_list *plist, ref *pdict, const ref *pwanted,
gs_ref_memory_t *imem)
{
check_dict_write(*pdict);
plist->u.w.write = dict_param_write;
plist->enumerate = dict_param_enumerate;
ref_param_write_init((iparam_list *) plist, pwanted, imem);
plist->dict = *pdict;
return 0;
}
/* Implementation for getting parameters to an indexed array. */
/* Note that this is now internal, since it only handles "new" arrays. */
static int
array_new_indexed_param_write(iparam_list * iplist, const ref * pkey,
const ref * pvalue)
{
const ref *const arr = &((dict_param_list *)iplist)->dict;
ref *eltp;
if (!r_has_type(pkey, t_integer))
return_error(gs_error_typecheck);
check_int_ltu(*pkey, r_size(arr));
store_check_dest(arr, pvalue);
eltp = arr->value.refs + pkey->value.intval;
/* ref_assign_new(eltp, pvalue); */
ref_assign(eltp, pvalue);
r_set_attrs(eltp, imemory_new_mask(iplist->ref_memory));
return 0;
}
static int
array_new_indexed_plist_write(dict_param_list * plist, ref * parray,
const ref * pwanted, gs_ref_memory_t *imem)
{
check_array(*parray);
check_write(*parray);
plist->u.w.write = array_new_indexed_param_write;
ref_param_write_init((iparam_list *) plist, pwanted, imem);
plist->dict = *parray;
plist->int_keys = true;
return 0;
}
/* ================ Reading refs to parameters ================ */
/* ---------------- Generic reading procedures ---------------- */
static param_proc_begin_xmit_collection(ref_param_begin_read_collection);
static param_proc_end_xmit_collection(ref_param_end_read_collection);
static param_proc_xmit_typed(ref_param_read_typed);
/*static param_proc_next_key(ref_param_get_next_key); already dec'ld above */
static param_proc_get_policy(ref_param_read_get_policy);
static param_proc_signal_error(ref_param_read_signal_error);
static param_proc_commit(ref_param_read_commit);
static const gs_param_list_procs ref_read_procs =
{
ref_param_read_typed,
ref_param_begin_read_collection,
ref_param_end_read_collection,
ref_param_get_next_key,
NULL, /* request */
NULL, /* requested */
ref_param_read_get_policy,
ref_param_read_signal_error,
ref_param_read_commit
};
static int ref_param_read(iparam_list *, gs_param_name,
iparam_loc *, int);
static int ref_param_read_string_value(gs_memory_t *mem,
const iparam_loc *,
gs_param_string *);
static int ref_param_read_array(iparam_list *, gs_param_name,
iparam_loc *);
#define iparam_note_error(loc, code)\
gs_note_error(*(loc).presult = code)
#define iparam_check_type(loc, typ)\
if ( !r_has_type((loc).pvalue, typ) )\
return iparam_note_error(loc, gs_error_typecheck)
#define iparam_check_read(loc)\
if ( !r_has_attr((loc).pvalue, a_read) )\
return iparam_note_error(loc, gs_error_invalidaccess)
static int
ref_param_read_int_array(gs_param_list * plist, gs_param_name pkey,
gs_param_int_array * pvalue)
{
iparam_list *const iplist = (iparam_list *) plist;
iparam_loc loc;
int code = ref_param_read_array(iplist, pkey, &loc);
int *piv;
uint size;
long i;
if (code != 0)
return code;
size = r_size(loc.pvalue);
piv = (int *)gs_alloc_byte_array(plist->memory, size, sizeof(int),
"ref_param_read_int_array");
if (piv == 0)
return_error(gs_error_VMerror);
for (i = 0; i < size; i++) {
ref elt;
array_get(plist->memory, loc.pvalue, i, &elt);
if (!r_has_type(&elt, t_integer)) {
code = gs_note_error(gs_error_typecheck);
break;
}
piv[i] = (int)elt.value.intval;
}
if (code < 0) {
gs_free_object(plist->memory, piv, "ref_param_read_int_array");
return (*loc.presult = code);
}
pvalue->data = piv;
pvalue->size = size;
pvalue->persistent = true;
return 0;
}
static int
ref_param_read_float_array(gs_param_list * plist, gs_param_name pkey,
gs_param_float_array * pvalue)
{
iparam_list *const iplist = (iparam_list *) plist;
iparam_loc loc;
ref aref, elt;
int code = ref_param_read_array(iplist, pkey, &loc);
float *pfv;
uint size;
long i;
if (code != 0)
return code;
size = r_size(loc.pvalue);
pfv = (float *)gs_alloc_byte_array(plist->memory, size, sizeof(float),
"ref_param_read_float_array");
if (pfv == 0)
return_error(gs_error_VMerror);
aref = *loc.pvalue;
loc.pvalue = &elt;
for (i = 0; code >= 0 && i < size; i++) {
array_get(plist->memory, &aref, i, &elt);
code = float_param(&elt, pfv + i);
}
if (code < 0) {
gs_free_object(plist->memory, pfv, "ref_read_float_array_param");
return (*loc.presult = code);
}
pvalue->data = pfv;
pvalue->size = size;
pvalue->persistent = true;
return 0;
}
static int
ref_param_read_string_array(gs_param_list * plist, gs_param_name pkey,
gs_param_string_array * pvalue)
{
iparam_list *const iplist = (iparam_list *) plist;
iparam_loc loc;
ref aref;
int code = ref_param_read_array(iplist, pkey, &loc);
gs_param_string *psv;
uint size;
long i;
if (code != 0)
return code;
size = r_size(loc.pvalue);
psv = (gs_param_string *)
gs_alloc_byte_array(plist->memory, size, sizeof(gs_param_string),
"ref_param_read_string_array");
if (psv == 0)
return_error(gs_error_VMerror);
aref = *loc.pvalue;
if (r_has_type(&aref, t_array)) {
for (i = 0; code >= 0 && i < size; i++) {
loc.pvalue = aref.value.refs + i;
code = ref_param_read_string_value(plist->memory, &loc, psv + i);
}
} else {
ref elt;
loc.pvalue = &elt;
for (i = 0; code >= 0 && i < size; i++) {
array_get(plist->memory, &aref, i, &elt);
code = ref_param_read_string_value(plist->memory, &loc, psv + i);
}
}
if (code < 0) {
gs_free_object(plist->memory, psv, "ref_param_read_string_array");
return (*loc.presult = code);
}
pvalue->data = psv;
pvalue->size = size;
pvalue->persistent = true;
return 0;
}
static int
ref_param_begin_read_collection(gs_param_list * plist, gs_param_name pkey,
gs_param_dict * pvalue,
gs_param_collection_type_t coll_type)
{
iparam_list *const iplist = (iparam_list *) plist;
iparam_loc loc;
bool int_keys = coll_type != 0;
int code = ref_param_read(iplist, pkey, &loc, -1);
dict_param_list *dlist;
if (code != 0)
return code;
dlist = (dict_param_list *)
gs_alloc_bytes(plist->memory, size_of(dict_param_list),
"ref_param_begin_read_collection");
if (dlist == 0)
return_error(gs_error_VMerror);
if (r_has_type(loc.pvalue, t_dictionary)) {
code = dict_param_list_read(dlist, loc.pvalue, NULL, false,
iplist->ref_memory);
dlist->int_keys = int_keys;
if (code >= 0)
pvalue->size = dict_length(loc.pvalue);
} else if (int_keys && r_is_array(loc.pvalue)) {
code = array_indexed_param_list_read(dlist, loc.pvalue, NULL, false,
iplist->ref_memory);
if (code >= 0)
pvalue->size = r_size(loc.pvalue);
} else
code = gs_note_error(gs_error_typecheck);
if (code < 0) {
gs_free_object(plist->memory, dlist, "ref_param_begin_write_collection");
return iparam_note_error(loc, code);
}
pvalue->list = (gs_param_list *) dlist;
return 0;
}
static int
ref_param_end_read_collection(gs_param_list * plist, gs_param_name pkey,
gs_param_dict * pvalue)
{
iparam_list_release((dict_param_list *) pvalue->list);
gs_free_object(plist->memory, pvalue->list,
"ref_param_end_read_collection");
return 0;
}
static int
ref_param_read_typed(gs_param_list * plist, gs_param_name pkey,
gs_param_typed_value * pvalue)
{
iparam_list *const iplist = (iparam_list *) plist;
iparam_loc loc;
ref elt;
int code = ref_param_read(iplist, pkey, &loc, -1);
if (code != 0)
return code;
switch (r_type(loc.pvalue)) {
case t_array:
case t_mixedarray:
case t_shortarray:
iparam_check_read(loc);
if (r_size(loc.pvalue) <= 0) {
/* 0-length array; can't get type info */
pvalue->type = gs_param_type_array;
pvalue->value.d.list = 0;
pvalue->value.d.size = 0;
return 0;
}
/*
* We have to guess at the array type. First we guess based
* on the type of the first element of the array. If that
* fails, we try again with more general types.
*/
array_get(plist->memory, loc.pvalue, 0, &elt);
switch (r_type(&elt)) {
case t_integer:
pvalue->type = gs_param_type_int_array;
code = ref_param_read_int_array(plist, pkey,
&pvalue->value.ia);
if (code != gs_error_typecheck)
return code;
/* This might be a float array. Fall through. */
*loc.presult = 0; /* reset error */
case t_real:
pvalue->type = gs_param_type_float_array;
return ref_param_read_float_array(plist, pkey,
&pvalue->value.fa);
case t_string:
pvalue->type = gs_param_type_string_array;
return ref_param_read_string_array(plist, pkey,
&pvalue->value.sa);
case t_name:
pvalue->type = gs_param_type_name_array;
return ref_param_read_string_array(plist, pkey,
&pvalue->value.na);
default:
break;
}
return gs_note_error(gs_error_typecheck);
case t_boolean:
pvalue->type = gs_param_type_bool;
pvalue->value.b = loc.pvalue->value.boolval;
return 0;
case t_dictionary:
code = ref_param_begin_read_collection(plist, pkey,
&pvalue->value.d, gs_param_collection_dict_any);
if (code < 0)
return code;
pvalue->type = gs_param_type_dict;
/* fixup new dict's type & int_keys field if contents have int keys */
{
gs_param_enumerator_t enumr;
gs_param_key_t key;
ref_type keytype;
dict_param_list *dlist = (dict_param_list *) pvalue->value.d.list;
param_init_enumerator(&enumr);
if (!(*(dlist->enumerate))
((iparam_list *) dlist, &enumr, &key, &keytype)
&& keytype == t_integer) {
dlist->int_keys = 1;
pvalue->type = gs_param_type_dict_int_keys;
}
}
return 0;
case t_integer:
pvalue->type = gs_param_type_i64;
pvalue->value.i64 = loc.pvalue->value.intval;
return 0;
case t_name:
pvalue->type = gs_param_type_name;
return ref_param_read_string_value(plist->memory, &loc, &pvalue->value.n);
case t_null:
pvalue->type = gs_param_type_null;
return 0;
case t_real:
pvalue->value.f = loc.pvalue->value.realval;
pvalue->type = gs_param_type_float;
return 0;
case t_string:
case t_astruct:
pvalue->type = gs_param_type_string;
return ref_param_read_string_value(plist->memory, &loc, &pvalue->value.s);
default:
break;
}
return gs_note_error(gs_error_typecheck);
}
static int
ref_param_read_get_policy(gs_param_list * plist, gs_param_name pkey)
{
iparam_list *const iplist = (iparam_list *) plist;
ref *pvalue;
if (!(r_has_type(&iplist->u.r.policies, t_dictionary) &&
dict_find_string(&iplist->u.r.policies, pkey, &pvalue) > 0 &&
r_has_type(pvalue, t_integer))
)
return gs_param_policy_ignore;
return (int)pvalue->value.intval;
}
static int
ref_param_read_signal_error(gs_param_list * plist, gs_param_name pkey, int code)
{
iparam_list *const iplist = (iparam_list *) plist;
iparam_loc loc = {0};
ref_param_read(iplist, pkey, &loc, -1);
if (loc.presult)
*loc.presult = code;
switch (ref_param_read_get_policy(plist, pkey)) {
case gs_param_policy_ignore:
return 0;
case gs_param_policy_consult_user:
return_error(gs_error_configurationerror);
default:
return code;
}
}
static int
ref_param_read_commit(gs_param_list * plist)
{
iparam_list *const iplist = (iparam_list *) plist;
int i;
int ecode = 0;
if (!iplist->u.r.require_all)
return 0;
/* Check to make sure that all parameters were actually read. */
for (i = 0; i < iplist->count; ++i)
if (iplist->results[i] == 0)
iplist->results[i] = ecode = gs_note_error(gs_error_undefined);
return ecode;
}
static int
ref_param_get_next_key(gs_param_list * plist, gs_param_enumerator_t * penum,
gs_param_key_t * key)
{
ref_type keytype; /* result not needed here */
iparam_list *const pilist = (iparam_list *) plist;
return (*pilist->enumerate) (pilist, penum, key, &keytype);
}
/* ---------------- Internal routines ---------------- */
/* Read a string value. */
static int
ref_param_read_string_value(gs_memory_t *mem, const iparam_loc * ploc, gs_param_string * pvalue)
{
const ref *pref = ploc->pvalue;
switch (r_type(pref)) {
case t_name: {
ref nref;
name_string_ref(mem, pref, &nref);
pvalue->data = nref.value.const_bytes;
pvalue->size = r_size(&nref);
pvalue->persistent = true;
}
break;
case t_string:
iparam_check_read(*ploc);
pvalue->data = pref->value.const_bytes;
pvalue->size = r_size(pref);
pvalue->persistent = false;
break;
case t_astruct:
/* Note: technically, instead of the "mem" argument, we
should be using the plists's ref_memory. However, in a
simple call to .putdeviceparams, they are identical. */
iparam_check_read(*ploc);
if (gs_object_type(mem, pref->value.pstruct) != &st_bytes)
return iparam_note_error(*ploc, gs_error_typecheck);
pvalue->data = r_ptr(pref, byte);
pvalue->size = gs_object_size(mem, pref->value.pstruct);
pvalue->persistent = false;
break;
default:
return iparam_note_error(*ploc, gs_error_typecheck);
}
return 0;
}
/* Read an array (or packed array) parameter. */
static int
ref_param_read_array(iparam_list * plist, gs_param_name pkey, iparam_loc * ploc)
{
int code = ref_param_read(plist, pkey, ploc, -1);
if (code != 0)
return code;
if (!r_is_array(ploc->pvalue))
return iparam_note_error(*ploc, gs_error_typecheck);
iparam_check_read(*ploc);
return 0;
}
/* Generic routine for reading a ref parameter. */
static int
ref_param_read(iparam_list * plist, gs_param_name pkey, iparam_loc * ploc,
int type)
{
iparam_list *const iplist = (iparam_list *) plist;
ref kref;
int code = ref_param_key(plist, pkey, &kref);
if (code < 0)
return code;
code = (*plist->u.r.read) (iplist, &kref, ploc);
if (code != 0)
return code;
if (type >= 0)
iparam_check_type(*ploc, type);
return 0;
}
/* ---------------- Implementations ---------------- */
/* Implementation for putting parameters from an empty collection. */
static int
empty_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
{
return 1;
}
/* Initialize for reading parameters. */
static int
ref_param_read_init(iparam_list * plist, uint count, const ref * ppolicies,
bool require_all, gs_ref_memory_t *imem)
{
gs_param_list_init((gs_param_list *)plist, &ref_read_procs,
(gs_memory_t *)imem);
plist->ref_memory = imem;
if (ppolicies == 0)
make_null(&plist->u.r.policies);
else
plist->u.r.policies = *ppolicies;
plist->u.r.require_all = require_all;
plist->count = count;
plist->results = (int *)
gs_alloc_byte_array(plist->memory, count, sizeof(int),
"ref_param_read_init");
if (plist->results == 0)
return_error(gs_error_VMerror);
memset(plist->results, 0, count * sizeof(int));
plist->int_keys = false;
return 0;
}
/* Implementation for putting parameters from an indexed array. */
static int
array_indexed_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
{
ref *const arr = &((dict_param_list *) plist)->dict;
check_type(*pkey, t_integer);
if (pkey->value.intval < 0 || pkey->value.intval >= r_size(arr))
return 1;
ploc->pvalue = arr->value.refs + pkey->value.intval;
ploc->presult = &plist->results[pkey->value.intval];
*ploc->presult = 1;
return 0;
}
int
array_indexed_param_list_read(dict_param_list * plist, const ref * parray,
const ref * ppolicies, bool require_all,
gs_ref_memory_t *ref_memory)
{
iparam_list *const iplist = (iparam_list *) plist;
int code;
check_read_type(*parray, t_array);
plist->u.r.read = array_indexed_param_read;
plist->dict = *parray;
code = ref_param_read_init(iplist, r_size(parray), ppolicies,
require_all, ref_memory);
plist->int_keys = true;
return code;
}
/* Implementation for putting parameters from an array. */
static int
array_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
{
ref *bot = ((array_param_list *) plist)->bot;
ref *ptr = bot;
ref *top = ((array_param_list *) plist)->top;
for (; ptr < top; ptr += 2) {
if (r_has_type(ptr, t_name) && name_eq(ptr, pkey)) {
ploc->pvalue = ptr + 1;
ploc->presult = &plist->results[ptr - bot];
*ploc->presult = 1;
return 0;
}
}
return 1;
}
/* Implementation for enumerating parameters in an array */
static int /* ret 0 ok, 1 if EOF, or -ve err */
array_param_enumerate(iparam_list * plist, gs_param_enumerator_t * penum,
gs_param_key_t * key, ref_type * type)
{
int index = penum->intval;
ref *bot = ((array_param_list *) plist)->bot;
ref *ptr = bot + index;
ref *top = ((array_param_list *) plist)->top;
for (; ptr < top; ptr += 2) {
index += 2;
if (r_has_type(ptr, t_name)) {
int code = ref_to_key(ptr, key, plist);
*type = r_type(ptr);
penum->intval = index;
return code;
}
}
return 1;
}
int
array_param_list_read(array_param_list * plist, ref * bot, uint count,
const ref * ppolicies, bool require_all,
gs_ref_memory_t *imem)
{
iparam_list *const iplist = (iparam_list *) plist;
if (count & 1)
return_error(gs_error_rangecheck);
plist->u.r.read = array_param_read;
plist->enumerate = array_param_enumerate;
plist->bot = bot;
plist->top = bot + count;
return ref_param_read_init(iplist, count, ppolicies, require_all, imem);
}
/* Implementation for putting parameters from a stack. */
static int
stack_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
{
stack_param_list *const splist = (stack_param_list *) plist;
ref_stack_t *pstack = splist->pstack;
/* This implementation is slow, but it probably doesn't matter. */
uint index = splist->skip + 1;
uint count = splist->count;
for (; count; count--, index += 2) {
const ref *p = ref_stack_index(pstack, index);
if (r_has_type(p, t_name) && name_eq(p, pkey)) {
ploc->pvalue = ref_stack_index(pstack, index - 1);
ploc->presult = &plist->results[count - 1];
*ploc->presult = 1;
return 0;
}
}
return 1;
}
int
stack_param_list_read(stack_param_list * plist, ref_stack_t * pstack,
uint skip, const ref * ppolicies, bool require_all,
gs_ref_memory_t *imem)
{
iparam_list *const iplist = (iparam_list *) plist;
uint count = ref_stack_counttomark(pstack);
if (count == 0)
return_error(gs_error_unmatchedmark);
count -= skip + 1;
if (count & 1)
return_error(gs_error_rangecheck);
plist->u.r.read = stack_param_read;
plist->enumerate = stack_param_enumerate;
plist->pstack = pstack;
plist->skip = skip;
return ref_param_read_init(iplist, count >> 1, ppolicies, require_all, imem);
}
/* Implementation for putting parameters from a dictionary. */
static int
dict_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
{
ref const *spdict = &((dict_param_list *) plist)->dict;
int code = dict_find(spdict, pkey, &ploc->pvalue);
if (code != 1)
return 1;
ploc->presult =
&plist->results[dict_value_index(spdict, ploc->pvalue)];
*ploc->presult = 1;
return 0;
}
int
dict_param_list_read(dict_param_list * plist, const ref * pdict,
const ref * ppolicies, bool require_all,
gs_ref_memory_t *imem)
{
iparam_list *const iplist = (iparam_list *) plist;
uint count;
if (pdict == 0) {
plist->u.r.read = empty_param_read;
count = 0;
} else {
check_dict_read(*pdict);
plist->u.r.read = dict_param_read;
plist->dict = *pdict;
count = dict_max_index(pdict) + 1;
}
plist->enumerate = dict_param_enumerate;
return ref_param_read_init(iplist, count, ppolicies, require_all, imem);
}
|