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