1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
/* Copyright (C) 2001-2020 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
CA 94945, U.S.A., +1(415)492-9861, for further information.
*/
/* Support for parameter lists */
#include "memory_.h"
#include "string_.h"
#include "gx.h"
#include "gserrors.h"
#include "gsparam.h"
#include "gsstruct.h"
/* GC procedures */
ENUM_PTRS_WITH(gs_param_typed_value_enum_ptrs, gs_param_typed_value *pvalue) return 0;
case 0:
switch (pvalue->type) {
case gs_param_type_string:
return ENUM_STRING(&pvalue->value.s);
case gs_param_type_name:
return ENUM_STRING(&pvalue->value.n);
case gs_param_type_int_array:
return ENUM_OBJ(pvalue->value.ia.data);
case gs_param_type_float_array:
return ENUM_OBJ(pvalue->value.fa.data);
case gs_param_type_string_array:
return ENUM_OBJ(pvalue->value.sa.data);
case gs_param_type_name_array:
return ENUM_OBJ(pvalue->value.na.data);
default:
return ENUM_OBJ(0); /* don't stop early */
}
ENUM_PTRS_END
RELOC_PTRS_WITH(gs_param_typed_value_reloc_ptrs, gs_param_typed_value *pvalue) {
switch (pvalue->type) {
case gs_param_type_string:
case gs_param_type_name: {
gs_const_string str;
str.data = pvalue->value.s.data; /* n == s */
str.size = pvalue->value.s.size;
RELOC_CONST_STRING_VAR(str);
pvalue->value.s.data = str.data;
break;
}
case gs_param_type_int_array:
RELOC_VAR(pvalue->value.ia.data);
break;
case gs_param_type_float_array:
RELOC_VAR(pvalue->value.fa.data);
break;
case gs_param_type_string_array:
RELOC_VAR(pvalue->value.sa.data);
break;
case gs_param_type_name_array:
RELOC_VAR(pvalue->value.na.data);
break;
default:
break;
}
}
RELOC_PTRS_END
/* Internal procedure to initialize the common part of a parameter list. */
void
gs_param_list_init(gs_param_list *plist, const gs_param_list_procs *procs,
gs_memory_t *mem)
{
plist->procs = procs;
plist->memory = mem;
plist->persistent_keys = true;
}
/* Set whether the keys for param_write_XXX are persistent. */
void
gs_param_list_set_persistent_keys(gs_param_list *plist, bool persistent)
{
plist->persistent_keys = persistent;
}
/* Reset a gs_param_key_t enumerator to its initial state */
void
param_init_enumerator(gs_param_enumerator_t * enumerator)
{
memset(enumerator, 0, sizeof(*enumerator));
}
/* Transfer a collection of parameters. */
static const byte xfer_item_sizes[] = {
GS_PARAM_TYPE_SIZES(0)
};
int
gs_param_read_items(gs_param_list * plist, void *obj,
const gs_param_item_t * items)
{
const gs_param_item_t *pi;
int ecode = 0;
for (pi = items; pi->key != 0; ++pi) {
const char *key = pi->key;
void *pvalue = (void *)((char *)obj + pi->offset);
gs_param_typed_value typed;
int code;
typed.type = pi->type;
code = param_read_requested_typed(plist, key, &typed);
switch (code) {
default: /* < 0 */
ecode = code;
case 1:
break;
case 0:
if (typed.type != pi->type) /* shouldn't happen! */
ecode = gs_note_error(gs_error_typecheck);
else
memcpy(pvalue, &typed.value, xfer_item_sizes[pi->type]);
}
}
return ecode;
}
int
gs_param_write_items(gs_param_list * plist, const void *obj,
const void *default_obj, const gs_param_item_t * items)
{
const gs_param_item_t *pi;
int ecode = 0;
for (pi = items; pi->key != 0; ++pi) {
const char *key = pi->key;
const void *pvalue = (const void *)((const char *)obj + pi->offset);
int size = xfer_item_sizes[pi->type];
gs_param_typed_value typed;
int code;
if (default_obj != 0 &&
!memcmp((const void *)((const char *)default_obj + pi->offset),
pvalue, size)
)
continue;
memcpy(&typed.value, pvalue, size);
typed.type = pi->type;
code = (*plist->procs->xmit_typed) (plist, key, &typed);
if (code < 0)
ecode = code;
}
return ecode;
}
/* Read a value, with coercion if requested, needed, and possible. */
/* If mem != 0, we can coerce int arrays to float arrays. */
int
param_coerce_typed(gs_param_typed_value * pvalue, gs_param_type req_type,
gs_memory_t * mem)
{
if (req_type == gs_param_type_any || pvalue->type == req_type)
return 0;
/*
* Look for coercion opportunities. It would be wonderful if we
* could convert int/float arrays and name/string arrays, but
* right now we can't. However, a 0-length heterogenous array
* will satisfy a request for any specific type.
*/
/* Strictly speaking assigning one element of union
* to another, overlapping element of a different size is
* undefined behavior, hence assign to intermediate variables
*/
switch (pvalue->type /* actual type */ ) {
case gs_param_type_int:
switch (req_type) {
case gs_param_type_i64:
{
int64_t i64 = (int64_t)pvalue->value.i;
pvalue->value.i64 = i64;
goto ok;
}
case gs_param_type_size_t:
{
size_t z = (size_t)pvalue->value.i;
if (pvalue->value.i < 0)
return gs_error_rangecheck;
pvalue->value.z = z;
goto ok;
}
case gs_param_type_long:
{
long l = (long)pvalue->value.i;
pvalue->value.l = l;
goto ok;
}
case gs_param_type_float:
{
float fl = (float)pvalue->value.l;
pvalue->value.f = fl;
goto ok;
}
default:
break;
}
break;
case gs_param_type_long:
switch (req_type) {
case gs_param_type_i64:
{
int64_t i64 = (int64_t)pvalue->value.l;
pvalue->value.i64 = i64;
goto ok;
}
case gs_param_type_size_t:
{
size_t z = (size_t)pvalue->value.l;
if (pvalue->value.l < 0
#if ARCH_SIZEOF_SIZE_T < ARCH_SIZEOF_LONG
|| pvalue->value.l != (long)z
#endif
)
return_error(gs_error_rangecheck);
pvalue->value.z = z;
goto ok;
}
case gs_param_type_int:
{
int int1 = (int)pvalue->value.l;
#if ARCH_SIZEOF_INT < ARCH_SIZEOF_LONG
if (pvalue->value.l != (long)int1)
return_error(gs_error_rangecheck);
#endif
pvalue->value.i = int1;
goto ok;
}
case gs_param_type_float:
{
float fl = (float)pvalue->value.l;
pvalue->value.f = fl;
goto ok;
}
default:
break;
}
break;
case gs_param_type_i64:
switch (req_type) {
case gs_param_type_size_t:
{
size_t z = (size_t)pvalue->value.i64;
if (pvalue->value.i64 < 0
#if ARCH_SIZEOF_SIZE_T < 8 /* sizeof(int64_t) */
|| pvalue->value.i64 != (int64_t)z
#endif
)
return_error(gs_error_rangecheck);
pvalue->value.z = z;
goto ok;
}
case gs_param_type_long:
{
long l = (long)pvalue->value.i64;
#if ARCH_SIZEOF_LONG < 8 /* sizeof(int64_t) */
if (pvalue->value.i64 != (int64_t)l)
return_error(gs_error_rangecheck);
#endif
pvalue->value.l = l;
goto ok;
}
case gs_param_type_int:
{
int int1 = (int)pvalue->value.l;
#if ARCH_SIZEOF_INT < 8 /* sizeof(int64_t) */
if (pvalue->value.i64 != (int)int1)
return_error(gs_error_rangecheck);
#endif
pvalue->value.i = int1;
goto ok;
}
case gs_param_type_float:
{
float fl = (float)pvalue->value.i64;
pvalue->value.f = fl;
goto ok;
}
default:
break;
}
break;
case gs_param_type_size_t:
switch (req_type) {
case gs_param_type_i64:
{
int64_t i64 = (int64_t)pvalue->value.z;
if (i64 < 0
#if 8 /* sizeof(int64_t) */ < ARCH_SIZEOF_SIZE_T
/* Unlikely, but let's plan for the day when we need 128bit addressing :) */
|| pvalue->value.z != (size_t)i64
#endif
)
return_error(gs_error_rangecheck);
pvalue->value.i64 = i64;
goto ok;
}
case gs_param_type_long:
{
long l = (long)pvalue->value.i64;
#if ARCH_SIZEOF_LONG < 8 /* sizeof(int64_t) */
if (pvalue->value.i64 != (int64_t)l)
return_error(gs_error_rangecheck);
#endif
pvalue->value.l = l;
goto ok;
}
case gs_param_type_int:
{
int int1 = (int)pvalue->value.l;
#if ARCH_SIZEOF_INT < 8 /* sizeof(int64_t) */
if (pvalue->value.i64 != (int)int1)
return_error(gs_error_rangecheck);
#endif
pvalue->value.i = int1;
goto ok;
}
case gs_param_type_float:
{
float fl = (float)pvalue->value.i64;
pvalue->value.f = fl;
goto ok;
}
default:
break;
}
break;
case gs_param_type_string:
if (req_type == gs_param_type_name)
goto ok;
break;
case gs_param_type_name:
if (req_type == gs_param_type_string)
goto ok;
break;
case gs_param_type_int_array:
switch (req_type) {
case gs_param_type_float_array:{
uint size = pvalue->value.ia.size;
float *fv;
uint i;
if (mem == 0)
break;
fv = (float *)gs_alloc_byte_array(mem, size, sizeof(float),
"int array => float array");
if (fv == 0)
return_error(gs_error_VMerror);
for (i = 0; i < size; ++i)
fv[i] = (float)pvalue->value.ia.data[i];
pvalue->value.fa.data = fv;
pvalue->value.fa.persistent = false;
goto ok;
}
default:
break;
}
break;
case gs_param_type_string_array:
if (req_type == gs_param_type_name_array)
goto ok;
break;
case gs_param_type_name_array:
if (req_type == gs_param_type_string_array)
goto ok;
break;
case gs_param_type_array:
if (pvalue->value.d.size == 0 &&
(req_type == gs_param_type_int_array ||
req_type == gs_param_type_float_array ||
req_type == gs_param_type_string_array ||
req_type == gs_param_type_name_array)
)
goto ok;
break;
default:
break;
}
return_error(gs_error_typecheck);
ok:pvalue->type = req_type;
return 0;
}
int
param_read_requested_typed(gs_param_list * plist, gs_param_name pkey,
gs_param_typed_value * pvalue)
{
gs_param_type req_type = pvalue->type;
int code = (*plist->procs->xmit_typed) (plist, pkey, pvalue);
if (code != 0)
return code;
return param_coerce_typed(pvalue, req_type, plist->memory);
}
/* ---------------- Fixed-type reading procedures ---------------- */
#define RETURN_READ_TYPED(alt, ptype)\
gs_param_typed_value typed;\
int code;\
\
typed.type = ptype;\
code = param_read_requested_typed(plist, pkey, &typed);\
if ( code == 0 )\
*pvalue = typed.value.alt;\
return code
int
param_read_null(gs_param_list * plist, gs_param_name pkey)
{
gs_param_typed_value typed;
typed.type = gs_param_type_null;
return param_read_requested_typed(plist, pkey, &typed);
}
int
param_read_bool(gs_param_list * plist, gs_param_name pkey, bool * pvalue)
{
RETURN_READ_TYPED(b, gs_param_type_bool);
}
int
param_read_int(gs_param_list * plist, gs_param_name pkey, int *pvalue)
{
RETURN_READ_TYPED(i, gs_param_type_int);
}
int
param_read_long(gs_param_list * plist, gs_param_name pkey, long *pvalue)
{
RETURN_READ_TYPED(l, gs_param_type_long);
}
int
param_read_i64(gs_param_list * plist, gs_param_name pkey, int64_t *pvalue)
{
RETURN_READ_TYPED(i64, gs_param_type_i64);
}
int
param_read_size_t(gs_param_list * plist, gs_param_name pkey, size_t *pvalue)
{
RETURN_READ_TYPED(z, gs_param_type_size_t);
}
int
param_read_float(gs_param_list * plist, gs_param_name pkey, float *pvalue)
{
RETURN_READ_TYPED(f, gs_param_type_float);
}
int
param_read_string(gs_param_list * plist, gs_param_name pkey,
gs_param_string * pvalue)
{
RETURN_READ_TYPED(s, gs_param_type_string);
}
int
param_read_name(gs_param_list * plist, gs_param_name pkey,
gs_param_string * pvalue)
{
RETURN_READ_TYPED(n, gs_param_type_string);
}
int
param_read_int_array(gs_param_list * plist, gs_param_name pkey,
gs_param_int_array * pvalue)
{
RETURN_READ_TYPED(ia, gs_param_type_int_array);
}
int
param_read_float_array(gs_param_list * plist, gs_param_name pkey,
gs_param_float_array * pvalue)
{
RETURN_READ_TYPED(fa, gs_param_type_float_array);
}
int
param_read_string_array(gs_param_list * plist, gs_param_name pkey,
gs_param_string_array * pvalue)
{
RETURN_READ_TYPED(sa, gs_param_type_string_array);
}
int
param_read_name_array(gs_param_list * plist, gs_param_name pkey,
gs_param_string_array * pvalue)
{
RETURN_READ_TYPED(na, gs_param_type_name_array);
}
#undef RETURN_READ_TYPED
/* ---------------- Default writing procedures ---------------- */
#define RETURN_WRITE_TYPED(alt, ptype)\
gs_param_typed_value typed;\
\
typed.value.alt = *pvalue;\
typed.type = ptype;\
return param_write_typed(plist, pkey, &typed)
int
param_write_null(gs_param_list * plist, gs_param_name pkey)
{
gs_param_typed_value typed;
typed.type = gs_param_type_null;
return param_write_typed(plist, pkey, &typed);
}
int
param_write_bool(gs_param_list * plist, gs_param_name pkey, const bool * pvalue)
{
RETURN_WRITE_TYPED(b, gs_param_type_bool);
}
int
param_write_int(gs_param_list * plist, gs_param_name pkey, const int *pvalue)
{
RETURN_WRITE_TYPED(i, gs_param_type_int);
}
int
param_write_long(gs_param_list * plist, gs_param_name pkey, const long *pvalue)
{
RETURN_WRITE_TYPED(l, gs_param_type_long);
}
int
param_write_i64(gs_param_list * plist, gs_param_name pkey, const int64_t *pvalue)
{
RETURN_WRITE_TYPED(i64, gs_param_type_i64);
}
int
param_write_size_t(gs_param_list * plist, gs_param_name pkey, const size_t *pvalue)
{
RETURN_WRITE_TYPED(z, gs_param_type_size_t);
}
int
param_write_float(gs_param_list * plist, gs_param_name pkey,
const float *pvalue)
{
RETURN_WRITE_TYPED(f, gs_param_type_float);
}
int
param_write_string(gs_param_list * plist, gs_param_name pkey,
const gs_param_string * pvalue)
{
RETURN_WRITE_TYPED(s, gs_param_type_string);
}
int
param_write_name(gs_param_list * plist, gs_param_name pkey,
const gs_param_string * pvalue)
{
RETURN_WRITE_TYPED(n, gs_param_type_name);
}
int
param_write_int_array(gs_param_list * plist, gs_param_name pkey,
const gs_param_int_array * pvalue)
{
RETURN_WRITE_TYPED(ia, gs_param_type_int_array);
}
int
param_write_int_values(gs_param_list * plist, gs_param_name pkey,
const int *values, uint size, bool persistent)
{
gs_param_int_array ia;
ia.data = values, ia.size = size, ia.persistent = persistent;
return param_write_int_array(plist, pkey, &ia);
}
int
param_write_float_array(gs_param_list * plist, gs_param_name pkey,
const gs_param_float_array * pvalue)
{
RETURN_WRITE_TYPED(fa, gs_param_type_float_array);
}
int
param_write_float_values(gs_param_list * plist, gs_param_name pkey,
const float *values, uint size, bool persistent)
{
gs_param_float_array fa;
fa.data = values, fa.size = size, fa.persistent = persistent;
return param_write_float_array(plist, pkey, &fa);
}
int
param_write_string_array(gs_param_list * plist, gs_param_name pkey,
const gs_param_string_array * pvalue)
{
RETURN_WRITE_TYPED(sa, gs_param_type_string_array);
}
int
param_write_name_array(gs_param_list * plist, gs_param_name pkey,
const gs_param_string_array * pvalue)
{
RETURN_WRITE_TYPED(na, gs_param_type_name_array);
}
#undef RETURN_WRITE_TYPED
/* ---------------- Default request implementation ---------------- */
int
gs_param_request_default(gs_param_list * plist, gs_param_name pkey)
{
return 0;
}
int
gs_param_requested_default(const gs_param_list * plist, gs_param_name pkey)
{
return -1; /* requested by default */
}
|