1 /* Copyright (C) 2001-2012 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., 7 Mt. Lassen Drive - Suite A-134, San Rafael,
13 CA 94903, 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(e_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 sprintf(istr, "%d", 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(e_VMerror);
74 key->data = buf;
75 key->size = len;
76 key->persistent = true;
77 } else
78 return_error(e_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(e_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(e_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(e_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(e_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, e_typecheck)
535 #define iparam_check_read(loc)\
536 if ( !r_has_attr((loc).pvalue, a_read) )\
537 return iparam_note_error(loc, e_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(e_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(e_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(e_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(e_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(e_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(e_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 != e_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(e_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
774 param_init_enumerator(&enumr);
775 if (!(*((iparam_list *) plist)->enumerate)
776 ((iparam_list *) pvalue->value.d.list, &enumr, &key, &keytype)
777 && keytype == t_integer) {
778 ((dict_param_list *) pvalue->value.d.list)->int_keys = 1;
779 pvalue->type = gs_param_type_dict_int_keys;
780 }
781 }
782 return 0;
783 case t_integer:
784 pvalue->type = gs_param_type_long;
785 pvalue->value.l = loc.pvalue->value.intval;
786 return 0;
787 case t_name:
788 pvalue->type = gs_param_type_name;
789 return ref_param_read_string_value(plist->memory, &loc, &pvalue->value.n);
790 case t_null:
791 pvalue->type = gs_param_type_null;
792 return 0;
793 case t_real:
794 pvalue->value.f = loc.pvalue->value.realval;
795 pvalue->type = gs_param_type_float;
796 return 0;
797 case t_string:
798 case t_astruct:
799 pvalue->type = gs_param_type_string;
800 return ref_param_read_string_value(plist->memory, &loc, &pvalue->value.s);
801 default:
802 break;
803 }
804 return gs_note_error(e_typecheck);
805 }
806
807 static int
ref_param_read_get_policy(gs_param_list * plist,gs_param_name pkey)808 ref_param_read_get_policy(gs_param_list * plist, gs_param_name pkey)
809 {
810 iparam_list *const iplist = (iparam_list *) plist;
811 ref *pvalue;
812
813 if (!(r_has_type(&iplist->u.r.policies, t_dictionary) &&
814 dict_find_string(&iplist->u.r.policies, pkey, &pvalue) > 0 &&
815 r_has_type(pvalue, t_integer))
816 )
817 return gs_param_policy_ignore;
818 return (int)pvalue->value.intval;
819 }
820 static int
ref_param_read_signal_error(gs_param_list * plist,gs_param_name pkey,int code)821 ref_param_read_signal_error(gs_param_list * plist, gs_param_name pkey, int code)
822 {
823 iparam_list *const iplist = (iparam_list *) plist;
824 iparam_loc loc;
825
826 ref_param_read(iplist, pkey, &loc, -1); /* can't fail */
827 *loc.presult = code;
828 switch (ref_param_read_get_policy(plist, pkey)) {
829 case gs_param_policy_ignore:
830 return 0;
831 case gs_param_policy_consult_user:
832 return_error(e_configurationerror);
833 default:
834 return code;
835 }
836 }
837 static int
ref_param_read_commit(gs_param_list * plist)838 ref_param_read_commit(gs_param_list * plist)
839 {
840 iparam_list *const iplist = (iparam_list *) plist;
841 int i;
842 int ecode = 0;
843
844 if (!iplist->u.r.require_all)
845 return 0;
846 /* Check to make sure that all parameters were actually read. */
847 for (i = 0; i < iplist->count; ++i)
848 if (iplist->results[i] == 0)
849 iplist->results[i] = ecode = gs_note_error(e_undefined);
850 return ecode;
851 }
852 static int
ref_param_get_next_key(gs_param_list * plist,gs_param_enumerator_t * penum,gs_param_key_t * key)853 ref_param_get_next_key(gs_param_list * plist, gs_param_enumerator_t * penum,
854 gs_param_key_t * key)
855 {
856 ref_type keytype; /* result not needed here */
857 iparam_list *const pilist = (iparam_list *) plist;
858
859 return (*pilist->enumerate) (pilist, penum, key, &keytype);
860 }
861
862 /* ---------------- Internal routines ---------------- */
863
864 /* Read a string value. */
865 static int
ref_param_read_string_value(gs_memory_t * mem,const iparam_loc * ploc,gs_param_string * pvalue)866 ref_param_read_string_value(gs_memory_t *mem, const iparam_loc * ploc, gs_param_string * pvalue)
867 {
868 const ref *pref = ploc->pvalue;
869
870 switch (r_type(pref)) {
871 case t_name: {
872 ref nref;
873
874 name_string_ref(mem, pref, &nref);
875 pvalue->data = nref.value.const_bytes;
876 pvalue->size = r_size(&nref);
877 pvalue->persistent = true;
878 }
879 break;
880 case t_string:
881 iparam_check_read(*ploc);
882 pvalue->data = pref->value.const_bytes;
883 pvalue->size = r_size(pref);
884 pvalue->persistent = false;
885 break;
886 case t_astruct:
887 /* Note: technically, instead of the "mem" argument, we
888 should be using the plists's ref_memory. However, in a
889 simple call to .putdeviceparams, they are identical. */
890 iparam_check_read(*ploc);
891 if (gs_object_type(mem, pref->value.pstruct) != &st_bytes)
892 return iparam_note_error(*ploc, e_typecheck);
893 pvalue->data = r_ptr(pref, byte);
894 pvalue->size = gs_object_size(mem, pref->value.pstruct);
895 pvalue->persistent = false;
896 break;
897 default:
898 return iparam_note_error(*ploc, e_typecheck);
899 }
900 return 0;
901 }
902
903 /* Read an array (or packed array) parameter. */
904 static int
ref_param_read_array(iparam_list * plist,gs_param_name pkey,iparam_loc * ploc)905 ref_param_read_array(iparam_list * plist, gs_param_name pkey, iparam_loc * ploc)
906 {
907 int code = ref_param_read(plist, pkey, ploc, -1);
908
909 if (code != 0)
910 return code;
911 if (!r_is_array(ploc->pvalue))
912 return iparam_note_error(*ploc, e_typecheck);
913 iparam_check_read(*ploc);
914 return 0;
915 }
916
917 /* Generic routine for reading a ref parameter. */
918 static int
ref_param_read(iparam_list * plist,gs_param_name pkey,iparam_loc * ploc,int type)919 ref_param_read(iparam_list * plist, gs_param_name pkey, iparam_loc * ploc,
920 int type)
921 {
922 iparam_list *const iplist = (iparam_list *) plist;
923 ref kref;
924 int code = ref_param_key(plist, pkey, &kref);
925
926 if (code < 0)
927 return code;
928 code = (*plist->u.r.read) (iplist, &kref, ploc);
929 if (code != 0)
930 return code;
931 if (type >= 0)
932 iparam_check_type(*ploc, type);
933 return 0;
934 }
935
936 /* ---------------- Implementations ---------------- */
937
938 /* Implementation for putting parameters from an empty collection. */
939 static int
empty_param_read(iparam_list * plist,const ref * pkey,iparam_loc * ploc)940 empty_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
941 {
942 return 1;
943 }
944
945 /* Initialize for reading parameters. */
946 static int
ref_param_read_init(iparam_list * plist,uint count,const ref * ppolicies,bool require_all,gs_ref_memory_t * imem)947 ref_param_read_init(iparam_list * plist, uint count, const ref * ppolicies,
948 bool require_all, gs_ref_memory_t *imem)
949 {
950 gs_param_list_init((gs_param_list *)plist, &ref_read_procs,
951 (gs_memory_t *)imem);
952 plist->ref_memory = imem;
953 if (ppolicies == 0)
954 make_null(&plist->u.r.policies);
955 else
956 plist->u.r.policies = *ppolicies;
957 plist->u.r.require_all = require_all;
958 plist->count = count;
959 plist->results = (int *)
960 gs_alloc_byte_array(plist->memory, count, sizeof(int),
961 "ref_param_read_init");
962
963 if (plist->results == 0)
964 return_error(e_VMerror);
965 memset(plist->results, 0, count * sizeof(int));
966
967 plist->int_keys = false;
968 return 0;
969 }
970
971 /* Implementation for putting parameters from an indexed array. */
972 static int
array_indexed_param_read(iparam_list * plist,const ref * pkey,iparam_loc * ploc)973 array_indexed_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
974 {
975 ref *const arr = &((dict_param_list *) plist)->dict;
976
977 check_type(*pkey, t_integer);
978 if (pkey->value.intval < 0 || pkey->value.intval >= r_size(arr))
979 return 1;
980 ploc->pvalue = arr->value.refs + pkey->value.intval;
981 ploc->presult = &plist->results[pkey->value.intval];
982 *ploc->presult = 1;
983 return 0;
984 }
985 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)986 array_indexed_param_list_read(dict_param_list * plist, const ref * parray,
987 const ref * ppolicies, bool require_all,
988 gs_ref_memory_t *ref_memory)
989 {
990 iparam_list *const iplist = (iparam_list *) plist;
991 int code;
992
993 check_read_type(*parray, t_array);
994 plist->u.r.read = array_indexed_param_read;
995 plist->dict = *parray;
996 code = ref_param_read_init(iplist, r_size(parray), ppolicies,
997 require_all, ref_memory);
998 plist->int_keys = true;
999 return code;
1000 }
1001
1002 /* Implementation for putting parameters from an array. */
1003 static int
array_param_read(iparam_list * plist,const ref * pkey,iparam_loc * ploc)1004 array_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
1005 {
1006 ref *bot = ((array_param_list *) plist)->bot;
1007 ref *ptr = bot;
1008 ref *top = ((array_param_list *) plist)->top;
1009
1010 for (; ptr < top; ptr += 2) {
1011 if (r_has_type(ptr, t_name) && name_eq(ptr, pkey)) {
1012 ploc->pvalue = ptr + 1;
1013 ploc->presult = &plist->results[ptr - bot];
1014 *ploc->presult = 1;
1015 return 0;
1016 }
1017 }
1018 return 1;
1019 }
1020
1021 /* Implementation for enumerating parameters in an array */
1022 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)1023 array_param_enumerate(iparam_list * plist, gs_param_enumerator_t * penum,
1024 gs_param_key_t * key, ref_type * type)
1025 {
1026 int index = penum->intval;
1027 ref *bot = ((array_param_list *) plist)->bot;
1028 ref *ptr = bot + index;
1029 ref *top = ((array_param_list *) plist)->top;
1030
1031 for (; ptr < top; ptr += 2) {
1032 index += 2;
1033
1034 if (r_has_type(ptr, t_name)) {
1035 int code = ref_to_key(ptr, key, plist);
1036
1037 *type = r_type(ptr);
1038 penum->intval = index;
1039 return code;
1040 }
1041 }
1042 return 1;
1043 }
1044
1045 int
array_param_list_read(array_param_list * plist,ref * bot,uint count,const ref * ppolicies,bool require_all,gs_ref_memory_t * imem)1046 array_param_list_read(array_param_list * plist, ref * bot, uint count,
1047 const ref * ppolicies, bool require_all,
1048 gs_ref_memory_t *imem)
1049 {
1050 iparam_list *const iplist = (iparam_list *) plist;
1051
1052 if (count & 1)
1053 return_error(e_rangecheck);
1054 plist->u.r.read = array_param_read;
1055 plist->enumerate = array_param_enumerate;
1056 plist->bot = bot;
1057 plist->top = bot + count;
1058 return ref_param_read_init(iplist, count, ppolicies, require_all, imem);
1059 }
1060
1061 /* Implementation for putting parameters from a stack. */
1062 static int
stack_param_read(iparam_list * plist,const ref * pkey,iparam_loc * ploc)1063 stack_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
1064 {
1065 stack_param_list *const splist = (stack_param_list *) plist;
1066 ref_stack_t *pstack = splist->pstack;
1067
1068 /* This implementation is slow, but it probably doesn't matter. */
1069 uint index = splist->skip + 1;
1070 uint count = splist->count;
1071
1072 for (; count; count--, index += 2) {
1073 const ref *p = ref_stack_index(pstack, index);
1074
1075 if (r_has_type(p, t_name) && name_eq(p, pkey)) {
1076 ploc->pvalue = ref_stack_index(pstack, index - 1);
1077 ploc->presult = &plist->results[count - 1];
1078 *ploc->presult = 1;
1079 return 0;
1080 }
1081 }
1082 return 1;
1083 }
1084 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)1085 stack_param_list_read(stack_param_list * plist, ref_stack_t * pstack,
1086 uint skip, const ref * ppolicies, bool require_all,
1087 gs_ref_memory_t *imem)
1088 {
1089 iparam_list *const iplist = (iparam_list *) plist;
1090 uint count = ref_stack_counttomark(pstack);
1091
1092 if (count == 0)
1093 return_error(e_unmatchedmark);
1094 count -= skip + 1;
1095 if (count & 1)
1096 return_error(e_rangecheck);
1097 plist->u.r.read = stack_param_read;
1098 plist->enumerate = stack_param_enumerate;
1099 plist->pstack = pstack;
1100 plist->skip = skip;
1101 return ref_param_read_init(iplist, count >> 1, ppolicies, require_all, imem);
1102 }
1103
1104 /* Implementation for putting parameters from a dictionary. */
1105 static int
dict_param_read(iparam_list * plist,const ref * pkey,iparam_loc * ploc)1106 dict_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
1107 {
1108 ref const *spdict = &((dict_param_list *) plist)->dict;
1109 int code = dict_find(spdict, pkey, &ploc->pvalue);
1110
1111 if (code != 1)
1112 return 1;
1113 ploc->presult =
1114 &plist->results[dict_value_index(spdict, ploc->pvalue)];
1115 *ploc->presult = 1;
1116 return 0;
1117 }
1118 int
dict_param_list_read(dict_param_list * plist,const ref * pdict,const ref * ppolicies,bool require_all,gs_ref_memory_t * imem)1119 dict_param_list_read(dict_param_list * plist, const ref * pdict,
1120 const ref * ppolicies, bool require_all,
1121 gs_ref_memory_t *imem)
1122 {
1123 iparam_list *const iplist = (iparam_list *) plist;
1124 uint count;
1125
1126 if (pdict == 0) {
1127 plist->u.r.read = empty_param_read;
1128 count = 0;
1129 } else {
1130 check_dict_read(*pdict);
1131 plist->u.r.read = dict_param_read;
1132 plist->dict = *pdict;
1133 count = dict_max_index(pdict) + 1;
1134 }
1135 plist->enumerate = dict_param_enumerate;
1136 return ref_param_read_init(iplist, count, ppolicies, require_all, imem);
1137 }
1138