1 /* Copyright (C) 2001-2019 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
13 CA 94945, U.S.A., +1(415)492-9861, for further information.
14 */
15
16
17 /* Interpreter implementations of parameter dictionaries */
18 #include "memory_.h"
19 #include "string_.h"
20 #include "ghost.h"
21 #include "ierrors.h"
22 #include "oper.h" /* for check_type */
23 #include "opcheck.h"
24 #include "ialloc.h"
25 #include "idict.h"
26 #include "imemory.h" /* for iutil.h */
27 #include "iname.h"
28 #include "istack.h"
29 #include "iparam.h"
30 #include "iutil.h" /* for num_params */
31 #include "ivmspace.h"
32 #include "store.h"
33 #include "gsstruct.h" /* for st_bytes */
34
35 /* ================ Utilities ================ */
36
37 /* Convert a key to a ref. */
38 static int
ref_param_key(const iparam_list * plist,gs_param_name pkey,ref * pkref)39 ref_param_key(const iparam_list * plist, gs_param_name pkey, ref * pkref)
40 {
41 if (plist->int_keys) {
42 long key;
43
44 if (sscanf(pkey, "%ld", &key) != 1)
45 return_error(gs_error_rangecheck);
46 make_int(pkref, key);
47 return 0;
48 } else
49 return name_ref(plist->memory, (const byte *)pkey, strlen(pkey), pkref, 0);
50 }
51
52 /* Fill in a gs_param_key_t from a name or int ref. */
53 static int
ref_to_key(const ref * pref,gs_param_key_t * key,iparam_list * plist)54 ref_to_key(const ref * pref, gs_param_key_t * key, iparam_list *plist)
55 {
56 if (r_has_type(pref, t_name)) {
57 ref nref;
58
59 name_string_ref(plist->memory, pref, &nref);
60 key->data = nref.value.const_bytes;
61 key->size = r_size(&nref);
62 key->persistent = false; /* names may be freed */
63 } else if (r_has_type(pref, t_integer)) {
64 char istr[sizeof(long) * 8 / 3 + 2];
65 int len;
66 byte *buf;
67
68 gs_sprintf(istr, "%"PRIpsint, pref->value.intval);
69 len = strlen(istr);
70 /* GC will take care of freeing this: */
71 buf = gs_alloc_string(plist->memory, len, "ref_to_key");
72 if (!buf)
73 return_error(gs_error_VMerror);
74 key->data = buf;
75 key->size = len;
76 key->persistent = true;
77 } else
78 return_error(gs_error_typecheck);
79 return 0;
80 }
81
82 /* ================ Writing parameters to refs ================ */
83
84 /* Forward references */
85 static int array_new_indexed_plist_write(dict_param_list *plist,
86 ref *parray, const ref *pwanted,
87 gs_ref_memory_t *imem);
88
89 /* ---------------- Generic writing procedures ---------------- */
90
91 static param_proc_begin_xmit_collection(ref_param_begin_write_collection);
92 static param_proc_end_xmit_collection(ref_param_end_write_collection);
93 static param_proc_xmit_typed(ref_param_write_typed);
94 static param_proc_next_key(ref_param_get_next_key);
95 static param_proc_requested(ref_param_requested);
96 static const gs_param_list_procs ref_write_procs =
97 {
98 ref_param_write_typed,
99 ref_param_begin_write_collection,
100 ref_param_end_write_collection,
101 ref_param_get_next_key,
102 NULL, /* request */
103 ref_param_requested
104 };
105 static int ref_array_param_requested(const iparam_list *, gs_param_name,
106 ref *, uint, client_name_t);
107 static int ref_param_write(iparam_list *, gs_param_name, const ref *);
108 static int ref_param_write_string_value(ref *, const gs_param_string *,
109 gs_ref_memory_t *);
110 static int ref_param_write_name_value(const gs_memory_t *mem, ref *, const gs_param_string *);
111 static int
ref_param_make_int(ref * pe,const void * pvalue,uint i,gs_ref_memory_t * imem)112 ref_param_make_int(ref *pe, const void *pvalue, uint i, gs_ref_memory_t *imem)
113 {
114 make_tav(pe, t_integer, imemory_new_mask(imem), intval,
115 ((const gs_param_int_array *)pvalue)->data[i]);
116 return 0;
117 }
118 static int
ref_param_make_float(ref * pe,const void * pvalue,uint i,gs_ref_memory_t * imem)119 ref_param_make_float(ref *pe, const void *pvalue, uint i, gs_ref_memory_t *imem)
120 {
121 make_tav(pe, t_real, imemory_new_mask(imem), realval,
122 ((const gs_param_float_array *)pvalue)->data[i]);
123 return 0;
124 }
125 static int
ref_param_make_string(ref * pe,const void * pvalue,uint i,gs_ref_memory_t * imem)126 ref_param_make_string(ref *pe, const void *pvalue, uint i, gs_ref_memory_t *imem)
127 {
128 return ref_param_write_string_value(pe,
129 &((const gs_param_string_array *)pvalue)->data[i],
130 imem);
131 }
132 static int
ref_param_make_name(ref * pe,const void * pvalue,uint i,gs_ref_memory_t * imem)133 ref_param_make_name(ref * pe, const void *pvalue, uint i, gs_ref_memory_t *imem)
134 {
135 return ref_param_write_name_value((const gs_memory_t *)imem, pe,
136 &((const gs_param_string_array *)pvalue)->data[i]);
137 }
138 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 *))139 ref_param_write_typed_array(gs_param_list * plist, gs_param_name pkey,
140 void *pvalue, uint count,
141 int (*make)(ref *, const void *, uint,
142 gs_ref_memory_t *))
143 {
144 iparam_list *const iplist = (iparam_list *) plist;
145 ref value;
146 uint i;
147 ref *pe;
148 int code;
149
150 if ((code = ref_array_param_requested(iplist, pkey, &value, count,
151 "ref_param_write_typed_array")) <= 0)
152 return code;
153 for (i = 0, pe = value.value.refs; i < count; ++i, ++pe)
154 if ((code = (*make) (pe, pvalue, i, iplist->ref_memory)) < 0)
155 return code;
156 return ref_param_write(iplist, pkey, &value);
157 }
158 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)159 ref_param_begin_write_collection(gs_param_list * plist, gs_param_name pkey,
160 gs_param_dict * pvalue,
161 gs_param_collection_type_t coll_type)
162 {
163 iparam_list *const iplist = (iparam_list *) plist;
164 gs_ref_memory_t *imem = iplist->ref_memory;
165 dict_param_list *dlist = (dict_param_list *)
166 gs_alloc_bytes(plist->memory, size_of(dict_param_list),
167 "ref_param_begin_write_collection");
168 int code;
169
170 if (dlist == 0)
171 return_error(gs_error_VMerror);
172 if (coll_type != gs_param_collection_array) {
173 ref dref;
174
175 code = dict_alloc(imem, pvalue->size, &dref);
176 if (code >= 0) {
177 code = dict_param_list_write(dlist, &dref, NULL, imem);
178 dlist->int_keys = coll_type == gs_param_collection_dict_int_keys;
179 }
180 } else {
181 ref aref;
182
183 code = gs_alloc_ref_array(imem, &aref, a_all, pvalue->size,
184 "ref_param_begin_write_collection");
185 if (code >= 0)
186 code = array_new_indexed_plist_write(dlist, &aref, NULL, imem);
187 }
188 if (code < 0)
189 gs_free_object(plist->memory, dlist, "ref_param_begin_write_collection");
190 else
191 pvalue->list = (gs_param_list *) dlist;
192 return code;
193 }
194 static int
ref_param_end_write_collection(gs_param_list * plist,gs_param_name pkey,gs_param_dict * pvalue)195 ref_param_end_write_collection(gs_param_list * plist, gs_param_name pkey,
196 gs_param_dict * pvalue)
197 {
198 iparam_list *const iplist = (iparam_list *) plist;
199 int code = ref_param_write(iplist, pkey,
200 &((dict_param_list *) pvalue->list)->dict);
201
202 gs_free_object(plist->memory, pvalue->list, "ref_param_end_write_collection");
203 pvalue->list = 0;
204 return code;
205 }
206 static int
ref_param_write_typed(gs_param_list * plist,gs_param_name pkey,gs_param_typed_value * pvalue)207 ref_param_write_typed(gs_param_list * plist, gs_param_name pkey,
208 gs_param_typed_value * pvalue)
209 {
210 iparam_list *const iplist = (iparam_list *) plist;
211 ref value;
212 int code = 0;
213
214 switch (pvalue->type) {
215 case gs_param_type_null:
216 make_null(&value);
217 break;
218 case gs_param_type_bool:
219 make_bool(&value, pvalue->value.b);
220 break;
221 case gs_param_type_int:
222 make_int(&value, pvalue->value.i);
223 break;
224 case gs_param_type_long:
225 make_int(&value, pvalue->value.l);
226 break;
227 case gs_param_type_float:
228 make_real(&value, pvalue->value.f);
229 break;
230 case gs_param_type_string:
231 if (!ref_param_requested(plist, pkey))
232 return 0;
233 code = ref_param_write_string_value(&value, &pvalue->value.s,
234 iplist->ref_memory);
235 break;
236 case gs_param_type_name:
237 if (!ref_param_requested(plist, pkey))
238 return 0;
239 code = ref_param_write_name_value(iplist->memory, &value, &pvalue->value.n);
240 break;
241 case gs_param_type_int_array:
242 return ref_param_write_typed_array(plist, pkey, &pvalue->value.ia,
243 pvalue->value.ia.size,
244 ref_param_make_int);
245 case gs_param_type_float_array:
246 return ref_param_write_typed_array(plist, pkey, &pvalue->value.fa,
247 pvalue->value.fa.size,
248 ref_param_make_float);
249 case gs_param_type_string_array:
250 return ref_param_write_typed_array(plist, pkey, &pvalue->value.sa,
251 pvalue->value.sa.size,
252 ref_param_make_string);
253 case gs_param_type_name_array:
254 return ref_param_write_typed_array(plist, pkey, &pvalue->value.na,
255 pvalue->value.na.size,
256 ref_param_make_name);
257 case gs_param_type_dict:
258 case gs_param_type_dict_int_keys:
259 case gs_param_type_array:
260 return ref_param_begin_write_collection(plist, pkey,
261 &pvalue->value.d,
262 (gs_param_collection_type_t)(pvalue->type - gs_param_type_dict));
263 default:
264 return_error(gs_error_typecheck);
265 }
266 if (code < 0)
267 return code;
268 return ref_param_write(iplist, pkey, &value);
269 }
270
271 /* Check whether a given parameter was requested. */
272 static int
ref_param_requested(const gs_param_list * plist,gs_param_name pkey)273 ref_param_requested(const gs_param_list * plist, gs_param_name pkey)
274 {
275 const iparam_list *const ciplist = (const iparam_list *)plist;
276 ref kref;
277 ref *ignore_value;
278
279 if (!r_has_type(&ciplist->u.w.wanted, t_dictionary))
280 return -1;
281 if (ref_param_key(ciplist, pkey, &kref) < 0)
282 return -1; /* catch it later */
283 return (dict_find(&ciplist->u.w.wanted, &kref, &ignore_value) > 0);
284 }
285
286 /* Check whether an array parameter is wanted, and allocate it if so. */
287 /* Return <0 on error, 0 if not wanted, 1 if wanted. */
288 static int
ref_array_param_requested(const iparam_list * iplist,gs_param_name pkey,ref * pvalue,uint size,client_name_t cname)289 ref_array_param_requested(const iparam_list *iplist, gs_param_name pkey,
290 ref *pvalue, uint size, client_name_t cname)
291 {
292 int code;
293
294 if (!ref_param_requested((const gs_param_list *)iplist, pkey))
295 return 0;
296 code = gs_alloc_ref_array(iplist->ref_memory, pvalue, a_all, size, cname);
297 return (code < 0 ? code : 1);
298 }
299
300 /* ---------------- Internal routines ---------------- */
301
302 /* Prepare to write a string value. */
303 static int
ref_param_write_string_value(ref * pref,const gs_param_string * pvalue,gs_ref_memory_t * imem)304 ref_param_write_string_value(ref * pref, const gs_param_string * pvalue,
305 gs_ref_memory_t *imem)
306 {
307 const byte *pdata = pvalue->data;
308 uint n = pvalue->size;
309
310 if (pvalue->persistent)
311 make_const_string(pref, a_readonly | avm_foreign, n, pdata);
312 else {
313 byte *pstr = gs_alloc_string((gs_memory_t *)imem, n,
314 "ref_param_write_string");
315
316 if (pstr == 0)
317 return_error(gs_error_VMerror);
318 memcpy(pstr, pdata, n);
319 make_string(pref, a_readonly | imemory_space(imem), n, pstr);
320 }
321 return 0;
322 }
323
324 /* Prepare to write a name value. */
325 static int
ref_param_write_name_value(const gs_memory_t * mem,ref * pref,const gs_param_string * pvalue)326 ref_param_write_name_value(const gs_memory_t *mem, ref * pref, const gs_param_string * pvalue)
327 {
328 return name_ref(mem, pvalue->data, pvalue->size, pref,
329 (pvalue->persistent ? 0 : 1));
330 }
331
332 /* Generic routine for writing a ref parameter. */
333 static int
ref_param_write(iparam_list * plist,gs_param_name pkey,const ref * pvalue)334 ref_param_write(iparam_list * plist, gs_param_name pkey, const ref * pvalue)
335 {
336 ref kref;
337 int code;
338
339 if (!ref_param_requested((gs_param_list *) plist, pkey))
340 return 0;
341 code = ref_param_key(plist, pkey, &kref);
342 if (code < 0)
343 return code;
344 return (*plist->u.w.write) (plist, &kref, pvalue);
345 }
346
347 /* ---------------- Implementations ---------------- */
348
349 /* Initialize for writing parameters. */
350 static void
ref_param_write_init(iparam_list * plist,const ref * pwanted,gs_ref_memory_t * imem)351 ref_param_write_init(iparam_list * plist, const ref * pwanted,
352 gs_ref_memory_t *imem)
353 {
354 gs_param_list_init((gs_param_list *)plist, &ref_write_procs,
355 (gs_memory_t *)imem);
356 plist->ref_memory = imem;
357 if (pwanted == 0)
358 make_null(&plist->u.w.wanted);
359 else
360 plist->u.w.wanted = *pwanted;
361 plist->results = 0;
362 plist->int_keys = false;
363 }
364
365 /* Implementation for getting parameters to a stack. */
366 static int
stack_param_write(iparam_list * plist,const ref * pkey,const ref * pvalue)367 stack_param_write(iparam_list * plist, const ref * pkey, const ref * pvalue)
368 {
369 stack_param_list *const splist = (stack_param_list *) plist;
370 ref_stack_t *pstack = splist->pstack;
371 s_ptr p = pstack->p;
372
373 if (pstack->top - p < 2) {
374 int code = ref_stack_push(pstack, 2);
375
376 if (code < 0)
377 return code;
378 *ref_stack_index(pstack, 1) = *pkey;
379 p = pstack->p;
380 } else {
381 pstack->p = p += 2;
382 p[-1] = *pkey;
383 }
384 *p = *pvalue;
385 splist->count++;
386 return 0;
387 }
388
389 /* Implementation for enumerating parameters on a stack */
390 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)391 stack_param_enumerate(iparam_list * plist, gs_param_enumerator_t * penum,
392 gs_param_key_t * key, ref_type * type)
393 {
394 int code;
395 stack_param_list *const splist = (stack_param_list *) plist;
396 int index = penum->intval;
397 ref *stack_element;
398
399 do {
400 stack_element =
401 ref_stack_index(splist->pstack, index + 1 + splist->skip);
402 if (!stack_element)
403 return 1;
404 } while (index += 2, !r_has_type(stack_element, t_name));
405 *type = r_type(stack_element);
406 code = ref_to_key(stack_element, key, plist);
407 penum->intval = index;
408 return code;
409 }
410
411 int
stack_param_list_write(stack_param_list * plist,ref_stack_t * pstack,const ref * pwanted,gs_ref_memory_t * imem)412 stack_param_list_write(stack_param_list * plist, ref_stack_t * pstack,
413 const ref * pwanted, gs_ref_memory_t *imem)
414 {
415 plist->u.w.write = stack_param_write;
416 ref_param_write_init((iparam_list *) plist, pwanted, imem);
417 plist->enumerate = stack_param_enumerate;
418 plist->pstack = pstack;
419 plist->skip = 0;
420 plist->count = 0;
421 return 0;
422 }
423
424 /* Implementation for getting parameters to a dictionary. */
425 static int
dict_param_write(iparam_list * plist,const ref * pkey,const ref * pvalue)426 dict_param_write(iparam_list * plist, const ref * pkey, const ref * pvalue)
427 {
428 int code =
429 dict_put(&((dict_param_list *) plist)->dict, pkey, pvalue, NULL);
430
431 return min(code, 0);
432 }
433
434 /* Implementation for enumerating parameters in a dictionary */
435 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)436 dict_param_enumerate(iparam_list * plist, gs_param_enumerator_t * penum,
437 gs_param_key_t * key, ref_type * type)
438 {
439 ref elt[2];
440 int code;
441 dict_param_list *const pdlist = (dict_param_list *) plist;
442 int index =
443 (penum->intval != 0 ? penum->intval : dict_first(&pdlist->dict));
444
445 index = dict_next(&pdlist->dict, index, elt);
446 if (index < 0)
447 return 1;
448 *type = r_type(&elt[1]);
449 code = ref_to_key(&elt[0], key, plist);
450 penum->intval = index;
451 return code;
452 }
453
454 int
dict_param_list_write(dict_param_list * plist,ref * pdict,const ref * pwanted,gs_ref_memory_t * imem)455 dict_param_list_write(dict_param_list *plist, ref *pdict, const ref *pwanted,
456 gs_ref_memory_t *imem)
457 {
458 check_dict_write(*pdict);
459 plist->u.w.write = dict_param_write;
460 plist->enumerate = dict_param_enumerate;
461 ref_param_write_init((iparam_list *) plist, pwanted, imem);
462 plist->dict = *pdict;
463 return 0;
464 }
465
466 /* Implementation for getting parameters to an indexed array. */
467 /* Note that this is now internal, since it only handles "new" arrays. */
468 static int
array_new_indexed_param_write(iparam_list * iplist,const ref * pkey,const ref * pvalue)469 array_new_indexed_param_write(iparam_list * iplist, const ref * pkey,
470 const ref * pvalue)
471 {
472 const ref *const arr = &((dict_param_list *)iplist)->dict;
473 ref *eltp;
474
475 if (!r_has_type(pkey, t_integer))
476 return_error(gs_error_typecheck);
477 check_int_ltu(*pkey, r_size(arr));
478 store_check_dest(arr, pvalue);
479 eltp = arr->value.refs + pkey->value.intval;
480 /* ref_assign_new(eltp, pvalue); */
481 ref_assign(eltp, pvalue);
482 r_set_attrs(eltp, imemory_new_mask(iplist->ref_memory));
483 return 0;
484 }
485 static int
array_new_indexed_plist_write(dict_param_list * plist,ref * parray,const ref * pwanted,gs_ref_memory_t * imem)486 array_new_indexed_plist_write(dict_param_list * plist, ref * parray,
487 const ref * pwanted, gs_ref_memory_t *imem)
488 {
489 check_array(*parray);
490 check_write(*parray);
491 plist->u.w.write = array_new_indexed_param_write;
492 ref_param_write_init((iparam_list *) plist, pwanted, imem);
493 plist->dict = *parray;
494 plist->int_keys = true;
495 return 0;
496 }
497
498 /* ================ Reading refs to parameters ================ */
499
500 /* ---------------- Generic reading procedures ---------------- */
501
502 static param_proc_begin_xmit_collection(ref_param_begin_read_collection);
503 static param_proc_end_xmit_collection(ref_param_end_read_collection);
504 static param_proc_xmit_typed(ref_param_read_typed);
505
506 /*static param_proc_next_key(ref_param_get_next_key); already dec'ld above */
507 static param_proc_get_policy(ref_param_read_get_policy);
508 static param_proc_signal_error(ref_param_read_signal_error);
509 static param_proc_commit(ref_param_read_commit);
510 static const gs_param_list_procs ref_read_procs =
511 {
512 ref_param_read_typed,
513 ref_param_begin_read_collection,
514 ref_param_end_read_collection,
515 ref_param_get_next_key,
516 NULL, /* request */
517 NULL, /* requested */
518 ref_param_read_get_policy,
519 ref_param_read_signal_error,
520 ref_param_read_commit
521 };
522 static int ref_param_read(iparam_list *, gs_param_name,
523 iparam_loc *, int);
524 static int ref_param_read_string_value(gs_memory_t *mem,
525 const iparam_loc *,
526 gs_param_string *);
527 static int ref_param_read_array(iparam_list *, gs_param_name,
528 iparam_loc *);
529
530 #define iparam_note_error(loc, code)\
531 gs_note_error(*(loc).presult = code)
532 #define iparam_check_type(loc, typ)\
533 if ( !r_has_type((loc).pvalue, typ) )\
534 return iparam_note_error(loc, gs_error_typecheck)
535 #define iparam_check_read(loc)\
536 if ( !r_has_attr((loc).pvalue, a_read) )\
537 return iparam_note_error(loc, gs_error_invalidaccess)
538
539 static int
ref_param_read_int_array(gs_param_list * plist,gs_param_name pkey,gs_param_int_array * pvalue)540 ref_param_read_int_array(gs_param_list * plist, gs_param_name pkey,
541 gs_param_int_array * pvalue)
542 {
543 iparam_list *const iplist = (iparam_list *) plist;
544 iparam_loc loc;
545 int code = ref_param_read_array(iplist, pkey, &loc);
546 int *piv;
547 uint size;
548 long i;
549
550 if (code != 0)
551 return code;
552 size = r_size(loc.pvalue);
553 piv = (int *)gs_alloc_byte_array(plist->memory, size, sizeof(int),
554 "ref_param_read_int_array");
555
556 if (piv == 0)
557 return_error(gs_error_VMerror);
558 for (i = 0; i < size; i++) {
559 ref elt;
560
561 array_get(plist->memory, loc.pvalue, i, &elt);
562 if (!r_has_type(&elt, t_integer)) {
563 code = gs_note_error(gs_error_typecheck);
564 break;
565 }
566 piv[i] = (int)elt.value.intval;
567 }
568 if (code < 0) {
569 gs_free_object(plist->memory, piv, "ref_param_read_int_array");
570 return (*loc.presult = code);
571 }
572 pvalue->data = piv;
573 pvalue->size = size;
574 pvalue->persistent = true;
575 return 0;
576 }
577 static int
ref_param_read_float_array(gs_param_list * plist,gs_param_name pkey,gs_param_float_array * pvalue)578 ref_param_read_float_array(gs_param_list * plist, gs_param_name pkey,
579 gs_param_float_array * pvalue)
580 {
581 iparam_list *const iplist = (iparam_list *) plist;
582 iparam_loc loc;
583 ref aref, elt;
584 int code = ref_param_read_array(iplist, pkey, &loc);
585 float *pfv;
586 uint size;
587 long i;
588
589 if (code != 0)
590 return code;
591 size = r_size(loc.pvalue);
592 pfv = (float *)gs_alloc_byte_array(plist->memory, size, sizeof(float),
593 "ref_param_read_float_array");
594
595 if (pfv == 0)
596 return_error(gs_error_VMerror);
597 aref = *loc.pvalue;
598 loc.pvalue = &elt;
599 for (i = 0; code >= 0 && i < size; i++) {
600 array_get(plist->memory, &aref, i, &elt);
601 code = float_param(&elt, pfv + i);
602 }
603 if (code < 0) {
604 gs_free_object(plist->memory, pfv, "ref_read_float_array_param");
605 return (*loc.presult = code);
606 }
607 pvalue->data = pfv;
608 pvalue->size = size;
609 pvalue->persistent = true;
610 return 0;
611 }
612 static int
ref_param_read_string_array(gs_param_list * plist,gs_param_name pkey,gs_param_string_array * pvalue)613 ref_param_read_string_array(gs_param_list * plist, gs_param_name pkey,
614 gs_param_string_array * pvalue)
615 {
616 iparam_list *const iplist = (iparam_list *) plist;
617 iparam_loc loc;
618 ref aref;
619 int code = ref_param_read_array(iplist, pkey, &loc);
620 gs_param_string *psv;
621 uint size;
622 long i;
623
624 if (code != 0)
625 return code;
626 size = r_size(loc.pvalue);
627 psv = (gs_param_string *)
628 gs_alloc_byte_array(plist->memory, size, sizeof(gs_param_string),
629 "ref_param_read_string_array");
630 if (psv == 0)
631 return_error(gs_error_VMerror);
632 aref = *loc.pvalue;
633 if (r_has_type(&aref, t_array)) {
634 for (i = 0; code >= 0 && i < size; i++) {
635 loc.pvalue = aref.value.refs + i;
636 code = ref_param_read_string_value(plist->memory, &loc, psv + i);
637 }
638 } else {
639 ref elt;
640
641 loc.pvalue = &elt;
642 for (i = 0; code >= 0 && i < size; i++) {
643 array_get(plist->memory, &aref, i, &elt);
644 code = ref_param_read_string_value(plist->memory, &loc, psv + i);
645 }
646 }
647 if (code < 0) {
648 gs_free_object(plist->memory, psv, "ref_param_read_string_array");
649 return (*loc.presult = code);
650 }
651 pvalue->data = psv;
652 pvalue->size = size;
653 pvalue->persistent = true;
654 return 0;
655 }
656 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)657 ref_param_begin_read_collection(gs_param_list * plist, gs_param_name pkey,
658 gs_param_dict * pvalue,
659 gs_param_collection_type_t coll_type)
660 {
661 iparam_list *const iplist = (iparam_list *) plist;
662 iparam_loc loc;
663 bool int_keys = coll_type != 0;
664 int code = ref_param_read(iplist, pkey, &loc, -1);
665 dict_param_list *dlist;
666
667 if (code != 0)
668 return code;
669 dlist = (dict_param_list *)
670 gs_alloc_bytes(plist->memory, size_of(dict_param_list),
671 "ref_param_begin_read_collection");
672 if (dlist == 0)
673 return_error(gs_error_VMerror);
674 if (r_has_type(loc.pvalue, t_dictionary)) {
675 code = dict_param_list_read(dlist, loc.pvalue, NULL, false,
676 iplist->ref_memory);
677 dlist->int_keys = int_keys;
678 if (code >= 0)
679 pvalue->size = dict_length(loc.pvalue);
680 } else if (int_keys && r_is_array(loc.pvalue)) {
681 code = array_indexed_param_list_read(dlist, loc.pvalue, NULL, false,
682 iplist->ref_memory);
683 if (code >= 0)
684 pvalue->size = r_size(loc.pvalue);
685 } else
686 code = gs_note_error(gs_error_typecheck);
687 if (code < 0) {
688 gs_free_object(plist->memory, dlist, "ref_param_begin_write_collection");
689 return iparam_note_error(loc, code);
690 }
691 pvalue->list = (gs_param_list *) dlist;
692 return 0;
693 }
694 static int
ref_param_end_read_collection(gs_param_list * plist,gs_param_name pkey,gs_param_dict * pvalue)695 ref_param_end_read_collection(gs_param_list * plist, gs_param_name pkey,
696 gs_param_dict * pvalue)
697 {
698 iparam_list_release((dict_param_list *) pvalue->list);
699 gs_free_object(plist->memory, pvalue->list,
700 "ref_param_end_read_collection");
701 return 0;
702 }
703 static int
ref_param_read_typed(gs_param_list * plist,gs_param_name pkey,gs_param_typed_value * pvalue)704 ref_param_read_typed(gs_param_list * plist, gs_param_name pkey,
705 gs_param_typed_value * pvalue)
706 {
707 iparam_list *const iplist = (iparam_list *) plist;
708 iparam_loc loc;
709 ref elt;
710 int code = ref_param_read(iplist, pkey, &loc, -1);
711
712 if (code != 0)
713 return code;
714 switch (r_type(loc.pvalue)) {
715 case t_array:
716 case t_mixedarray:
717 case t_shortarray:
718 iparam_check_read(loc);
719 if (r_size(loc.pvalue) <= 0) {
720 /* 0-length array; can't get type info */
721 pvalue->type = gs_param_type_array;
722 pvalue->value.d.list = 0;
723 pvalue->value.d.size = 0;
724 return 0;
725 }
726 /*
727 * We have to guess at the array type. First we guess based
728 * on the type of the first element of the array. If that
729 * fails, we try again with more general types.
730 */
731 array_get(plist->memory, loc.pvalue, 0, &elt);
732 switch (r_type(&elt)) {
733 case t_integer:
734 pvalue->type = gs_param_type_int_array;
735 code = ref_param_read_int_array(plist, pkey,
736 &pvalue->value.ia);
737 if (code != gs_error_typecheck)
738 return code;
739 /* This might be a float array. Fall through. */
740 *loc.presult = 0; /* reset error */
741 case t_real:
742 pvalue->type = gs_param_type_float_array;
743 return ref_param_read_float_array(plist, pkey,
744 &pvalue->value.fa);
745 case t_string:
746 pvalue->type = gs_param_type_string_array;
747 return ref_param_read_string_array(plist, pkey,
748 &pvalue->value.sa);
749 case t_name:
750 pvalue->type = gs_param_type_name_array;
751 return ref_param_read_string_array(plist, pkey,
752 &pvalue->value.na);
753 default:
754 break;
755 }
756 return gs_note_error(gs_error_typecheck);
757 case t_boolean:
758 pvalue->type = gs_param_type_bool;
759 pvalue->value.b = loc.pvalue->value.boolval;
760 return 0;
761 case t_dictionary:
762 code = ref_param_begin_read_collection(plist, pkey,
763 &pvalue->value.d, gs_param_collection_dict_any);
764 if (code < 0)
765 return code;
766 pvalue->type = gs_param_type_dict;
767
768 /* fixup new dict's type & int_keys field if contents have int keys */
769 {
770 gs_param_enumerator_t enumr;
771 gs_param_key_t key;
772 ref_type keytype;
773 dict_param_list *dlist = (dict_param_list *) pvalue->value.d.list;
774
775 param_init_enumerator(&enumr);
776 if (!(*(dlist->enumerate))
777 ((iparam_list *) dlist, &enumr, &key, &keytype)
778 && keytype == t_integer) {
779 dlist->int_keys = 1;
780 pvalue->type = gs_param_type_dict_int_keys;
781 }
782 }
783 return 0;
784 case t_integer:
785 pvalue->type = gs_param_type_long;
786 pvalue->value.l = loc.pvalue->value.intval;
787 return 0;
788 case t_name:
789 pvalue->type = gs_param_type_name;
790 return ref_param_read_string_value(plist->memory, &loc, &pvalue->value.n);
791 case t_null:
792 pvalue->type = gs_param_type_null;
793 return 0;
794 case t_real:
795 pvalue->value.f = loc.pvalue->value.realval;
796 pvalue->type = gs_param_type_float;
797 return 0;
798 case t_string:
799 case t_astruct:
800 pvalue->type = gs_param_type_string;
801 return ref_param_read_string_value(plist->memory, &loc, &pvalue->value.s);
802 default:
803 break;
804 }
805 return gs_note_error(gs_error_typecheck);
806 }
807
808 static int
ref_param_read_get_policy(gs_param_list * plist,gs_param_name pkey)809 ref_param_read_get_policy(gs_param_list * plist, gs_param_name pkey)
810 {
811 iparam_list *const iplist = (iparam_list *) plist;
812 ref *pvalue;
813
814 if (!(r_has_type(&iplist->u.r.policies, t_dictionary) &&
815 dict_find_string(&iplist->u.r.policies, pkey, &pvalue) > 0 &&
816 r_has_type(pvalue, t_integer))
817 )
818 return gs_param_policy_ignore;
819 return (int)pvalue->value.intval;
820 }
821 static int
ref_param_read_signal_error(gs_param_list * plist,gs_param_name pkey,int code)822 ref_param_read_signal_error(gs_param_list * plist, gs_param_name pkey, int code)
823 {
824 iparam_list *const iplist = (iparam_list *) plist;
825 iparam_loc loc = {0};
826
827 ref_param_read(iplist, pkey, &loc, -1);
828 if (loc.presult)
829 *loc.presult = code;
830 switch (ref_param_read_get_policy(plist, pkey)) {
831 case gs_param_policy_ignore:
832 return 0;
833 case gs_param_policy_consult_user:
834 return_error(gs_error_configurationerror);
835 default:
836 return code;
837 }
838 }
839 static int
ref_param_read_commit(gs_param_list * plist)840 ref_param_read_commit(gs_param_list * plist)
841 {
842 iparam_list *const iplist = (iparam_list *) plist;
843 int i;
844 int ecode = 0;
845
846 if (!iplist->u.r.require_all)
847 return 0;
848 /* Check to make sure that all parameters were actually read. */
849 for (i = 0; i < iplist->count; ++i)
850 if (iplist->results[i] == 0)
851 iplist->results[i] = ecode = gs_note_error(gs_error_undefined);
852 return ecode;
853 }
854 static int
ref_param_get_next_key(gs_param_list * plist,gs_param_enumerator_t * penum,gs_param_key_t * key)855 ref_param_get_next_key(gs_param_list * plist, gs_param_enumerator_t * penum,
856 gs_param_key_t * key)
857 {
858 ref_type keytype; /* result not needed here */
859 iparam_list *const pilist = (iparam_list *) plist;
860
861 return (*pilist->enumerate) (pilist, penum, key, &keytype);
862 }
863
864 /* ---------------- Internal routines ---------------- */
865
866 /* Read a string value. */
867 static int
ref_param_read_string_value(gs_memory_t * mem,const iparam_loc * ploc,gs_param_string * pvalue)868 ref_param_read_string_value(gs_memory_t *mem, const iparam_loc * ploc, gs_param_string * pvalue)
869 {
870 const ref *pref = ploc->pvalue;
871
872 switch (r_type(pref)) {
873 case t_name: {
874 ref nref;
875
876 name_string_ref(mem, pref, &nref);
877 pvalue->data = nref.value.const_bytes;
878 pvalue->size = r_size(&nref);
879 pvalue->persistent = true;
880 }
881 break;
882 case t_string:
883 iparam_check_read(*ploc);
884 pvalue->data = pref->value.const_bytes;
885 pvalue->size = r_size(pref);
886 pvalue->persistent = false;
887 break;
888 case t_astruct:
889 /* Note: technically, instead of the "mem" argument, we
890 should be using the plists's ref_memory. However, in a
891 simple call to .putdeviceparams, they are identical. */
892 iparam_check_read(*ploc);
893 if (gs_object_type(mem, pref->value.pstruct) != &st_bytes)
894 return iparam_note_error(*ploc, gs_error_typecheck);
895 pvalue->data = r_ptr(pref, byte);
896 pvalue->size = gs_object_size(mem, pref->value.pstruct);
897 pvalue->persistent = false;
898 break;
899 default:
900 return iparam_note_error(*ploc, gs_error_typecheck);
901 }
902 return 0;
903 }
904
905 /* Read an array (or packed array) parameter. */
906 static int
ref_param_read_array(iparam_list * plist,gs_param_name pkey,iparam_loc * ploc)907 ref_param_read_array(iparam_list * plist, gs_param_name pkey, iparam_loc * ploc)
908 {
909 int code = ref_param_read(plist, pkey, ploc, -1);
910
911 if (code != 0)
912 return code;
913 if (!r_is_array(ploc->pvalue))
914 return iparam_note_error(*ploc, gs_error_typecheck);
915 iparam_check_read(*ploc);
916 return 0;
917 }
918
919 /* Generic routine for reading a ref parameter. */
920 static int
ref_param_read(iparam_list * plist,gs_param_name pkey,iparam_loc * ploc,int type)921 ref_param_read(iparam_list * plist, gs_param_name pkey, iparam_loc * ploc,
922 int type)
923 {
924 iparam_list *const iplist = (iparam_list *) plist;
925 ref kref;
926 int code = ref_param_key(plist, pkey, &kref);
927
928 if (code < 0)
929 return code;
930 code = (*plist->u.r.read) (iplist, &kref, ploc);
931 if (code != 0)
932 return code;
933 if (type >= 0)
934 iparam_check_type(*ploc, type);
935 return 0;
936 }
937
938 /* ---------------- Implementations ---------------- */
939
940 /* Implementation for putting parameters from an empty collection. */
941 static int
empty_param_read(iparam_list * plist,const ref * pkey,iparam_loc * ploc)942 empty_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
943 {
944 return 1;
945 }
946
947 /* Initialize for reading parameters. */
948 static int
ref_param_read_init(iparam_list * plist,uint count,const ref * ppolicies,bool require_all,gs_ref_memory_t * imem)949 ref_param_read_init(iparam_list * plist, uint count, const ref * ppolicies,
950 bool require_all, gs_ref_memory_t *imem)
951 {
952 gs_param_list_init((gs_param_list *)plist, &ref_read_procs,
953 (gs_memory_t *)imem);
954 plist->ref_memory = imem;
955 if (ppolicies == 0)
956 make_null(&plist->u.r.policies);
957 else
958 plist->u.r.policies = *ppolicies;
959 plist->u.r.require_all = require_all;
960 plist->count = count;
961 plist->results = (int *)
962 gs_alloc_byte_array(plist->memory, count, sizeof(int),
963 "ref_param_read_init");
964
965 if (plist->results == 0)
966 return_error(gs_error_VMerror);
967 memset(plist->results, 0, count * sizeof(int));
968
969 plist->int_keys = false;
970 return 0;
971 }
972
973 /* Implementation for putting parameters from an indexed array. */
974 static int
array_indexed_param_read(iparam_list * plist,const ref * pkey,iparam_loc * ploc)975 array_indexed_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
976 {
977 ref *const arr = &((dict_param_list *) plist)->dict;
978
979 check_type(*pkey, t_integer);
980 if (pkey->value.intval < 0 || pkey->value.intval >= r_size(arr))
981 return 1;
982 ploc->pvalue = arr->value.refs + pkey->value.intval;
983 ploc->presult = &plist->results[pkey->value.intval];
984 *ploc->presult = 1;
985 return 0;
986 }
987 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)988 array_indexed_param_list_read(dict_param_list * plist, const ref * parray,
989 const ref * ppolicies, bool require_all,
990 gs_ref_memory_t *ref_memory)
991 {
992 iparam_list *const iplist = (iparam_list *) plist;
993 int code;
994
995 check_read_type(*parray, t_array);
996 plist->u.r.read = array_indexed_param_read;
997 plist->dict = *parray;
998 code = ref_param_read_init(iplist, r_size(parray), ppolicies,
999 require_all, ref_memory);
1000 plist->int_keys = true;
1001 return code;
1002 }
1003
1004 /* Implementation for putting parameters from an array. */
1005 static int
array_param_read(iparam_list * plist,const ref * pkey,iparam_loc * ploc)1006 array_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
1007 {
1008 ref *bot = ((array_param_list *) plist)->bot;
1009 ref *ptr = bot;
1010 ref *top = ((array_param_list *) plist)->top;
1011
1012 for (; ptr < top; ptr += 2) {
1013 if (r_has_type(ptr, t_name) && name_eq(ptr, pkey)) {
1014 ploc->pvalue = ptr + 1;
1015 ploc->presult = &plist->results[ptr - bot];
1016 *ploc->presult = 1;
1017 return 0;
1018 }
1019 }
1020 return 1;
1021 }
1022
1023 /* Implementation for enumerating parameters in an array */
1024 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)1025 array_param_enumerate(iparam_list * plist, gs_param_enumerator_t * penum,
1026 gs_param_key_t * key, ref_type * type)
1027 {
1028 int index = penum->intval;
1029 ref *bot = ((array_param_list *) plist)->bot;
1030 ref *ptr = bot + index;
1031 ref *top = ((array_param_list *) plist)->top;
1032
1033 for (; ptr < top; ptr += 2) {
1034 index += 2;
1035
1036 if (r_has_type(ptr, t_name)) {
1037 int code = ref_to_key(ptr, key, plist);
1038
1039 *type = r_type(ptr);
1040 penum->intval = index;
1041 return code;
1042 }
1043 }
1044 return 1;
1045 }
1046
1047 int
array_param_list_read(array_param_list * plist,ref * bot,uint count,const ref * ppolicies,bool require_all,gs_ref_memory_t * imem)1048 array_param_list_read(array_param_list * plist, ref * bot, uint count,
1049 const ref * ppolicies, bool require_all,
1050 gs_ref_memory_t *imem)
1051 {
1052 iparam_list *const iplist = (iparam_list *) plist;
1053
1054 if (count & 1)
1055 return_error(gs_error_rangecheck);
1056 plist->u.r.read = array_param_read;
1057 plist->enumerate = array_param_enumerate;
1058 plist->bot = bot;
1059 plist->top = bot + count;
1060 return ref_param_read_init(iplist, count, ppolicies, require_all, imem);
1061 }
1062
1063 /* Implementation for putting parameters from a stack. */
1064 static int
stack_param_read(iparam_list * plist,const ref * pkey,iparam_loc * ploc)1065 stack_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
1066 {
1067 stack_param_list *const splist = (stack_param_list *) plist;
1068 ref_stack_t *pstack = splist->pstack;
1069
1070 /* This implementation is slow, but it probably doesn't matter. */
1071 uint index = splist->skip + 1;
1072 uint count = splist->count;
1073
1074 for (; count; count--, index += 2) {
1075 const ref *p = ref_stack_index(pstack, index);
1076
1077 if (r_has_type(p, t_name) && name_eq(p, pkey)) {
1078 ploc->pvalue = ref_stack_index(pstack, index - 1);
1079 ploc->presult = &plist->results[count - 1];
1080 *ploc->presult = 1;
1081 return 0;
1082 }
1083 }
1084 return 1;
1085 }
1086 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)1087 stack_param_list_read(stack_param_list * plist, ref_stack_t * pstack,
1088 uint skip, const ref * ppolicies, bool require_all,
1089 gs_ref_memory_t *imem)
1090 {
1091 iparam_list *const iplist = (iparam_list *) plist;
1092 uint count = ref_stack_counttomark(pstack);
1093
1094 if (count == 0)
1095 return_error(gs_error_unmatchedmark);
1096 count -= skip + 1;
1097 if (count & 1)
1098 return_error(gs_error_rangecheck);
1099 plist->u.r.read = stack_param_read;
1100 plist->enumerate = stack_param_enumerate;
1101 plist->pstack = pstack;
1102 plist->skip = skip;
1103 return ref_param_read_init(iplist, count >> 1, ppolicies, require_all, imem);
1104 }
1105
1106 /* Implementation for putting parameters from a dictionary. */
1107 static int
dict_param_read(iparam_list * plist,const ref * pkey,iparam_loc * ploc)1108 dict_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
1109 {
1110 ref const *spdict = &((dict_param_list *) plist)->dict;
1111 int code = dict_find(spdict, pkey, &ploc->pvalue);
1112
1113 if (code != 1)
1114 return 1;
1115 ploc->presult =
1116 &plist->results[dict_value_index(spdict, ploc->pvalue)];
1117 *ploc->presult = 1;
1118 return 0;
1119 }
1120 int
dict_param_list_read(dict_param_list * plist,const ref * pdict,const ref * ppolicies,bool require_all,gs_ref_memory_t * imem)1121 dict_param_list_read(dict_param_list * plist, const ref * pdict,
1122 const ref * ppolicies, bool require_all,
1123 gs_ref_memory_t *imem)
1124 {
1125 iparam_list *const iplist = (iparam_list *) plist;
1126 uint count;
1127
1128 if (pdict == 0) {
1129 plist->u.r.read = empty_param_read;
1130 count = 0;
1131 } else {
1132 check_dict_read(*pdict);
1133 plist->u.r.read = dict_param_read;
1134 plist->dict = *pdict;
1135 count = dict_max_index(pdict) + 1;
1136 }
1137 plist->enumerate = dict_param_enumerate;
1138 return ref_param_read_init(iplist, count, ppolicies, require_all, imem);
1139 }
1140