summaryrefslogtreecommitdiff
blob: fbabc6c35b6dfb0e599581099b6fc8310fbdee74 (plain)
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
/* Copyright (C) 2001-2019 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.
*/


/* Utilities for getting parameters out of dictionaries. */
#include "memory_.h"
#include "string_.h"		/* for strlen */
#include "ghost.h"
#include "ierrors.h"
#include "gsmatrix.h"		/* for dict_matrix_param */
#include "gsuid.h"
#include "dstack.h"             /* for systemdict */
#include "idict.h"
#include "iddict.h"
#include "idparam.h"		/* interface definition */
#include "ilevel.h"
#include "imemory.h"		/* for iutil.h */
#include "iname.h"
#include "iutil.h"
#include "oper.h"		/* for check_proc */
#include "store.h"		/* for making empty proc */

/* Get a Boolean parameter from a dictionary. */
/* Return 0 if found, 1 if defaulted, <0 if wrong type. */
int
dict_bool_param(const ref * pdict, const char *kstr,
                bool defaultval, bool * pvalue)
{
    ref *pdval;

    if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
        *pvalue = defaultval;
        return 1;
    }
    if (!r_has_type(pdval, t_boolean))
        return_error(gs_error_typecheck);
    *pvalue = pdval->value.boolval;
    return 0;
}

/* Get an integer or null parameter from a dictionary. */
/* Return 0 if found, 1 if defaulted, <0 if invalid. */
/* If the parameter is null, return 2 without setting *pvalue. */
/* Note that the default value may be out of range, in which case */
/* a missing value will return gs_error_undefined rather than 1. */
int
dict_int_null_param(const ref * pdict, const char *kstr, int minval,
                    int maxval, int defaultval, int *pvalue)
{
    ref *pdval;
    int code, ival;

    if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
        ival = defaultval;
        code = 1;
    } else {
        switch (r_type(pdval)) {
            case t_integer:
                if (pdval->value.intval < minval || pdval->value.intval > maxval)
                    return_error(gs_error_rangecheck);
                ival = pdval->value.intval;
                break;
            case t_real:
                /* Allow an integral real, because Fontographer */
                /* (which violates the Adobe specs in other ways */
                /* as well) sometimes generates output that */
                /* needs this. */
                if (pdval->value.realval < minval || pdval->value.realval > maxval)
                    return_error(gs_error_rangecheck);
                ival = (long)pdval->value.realval;
                if (ival != pdval->value.realval)
                    return_error(gs_error_rangecheck);
                break;
            case t_null:
                return 2;
            default:
                return_error(gs_error_typecheck);
        }
        code = 0;
    }
    if (ival < minval || ival > maxval) {
        if (code == 1)
            return_error(gs_error_undefined);
        else
            return_error(gs_error_rangecheck);
    }
    *pvalue = (int)ival;
    return code;
}
/* Get an integer parameter from a dictionary. */
/* Return like dict_int_null_param, but return gs_error_typecheck for null. */
int
dict_int_param(const ref * pdict, const char *kstr, int minval, int maxval,
               int defaultval, int *pvalue)
{
    int code = dict_int_null_param(pdict, kstr, minval, maxval,
                                   defaultval, pvalue);

    return (code == 2 ? gs_note_error(gs_error_typecheck) : code);
}

/* Get an unsigned integer parameter from a dictionary. */
/* Return 0 if found, 1 if defaulted, <0 if invalid. */
/* Note that the default value may be out of range, in which case */
/* a missing value will return gs_error_undefined rather than 1. */
int
dict_uint_param(const ref * pdict, const char *kstr,
                uint minval, uint maxval, uint defaultval, uint * pvalue)
{
    ref *pdval;
    int code;
    uint ival;

    if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
        ival = defaultval;
        code = 1;
    } else {
        check_type_only(*pdval, t_integer);
        if (pdval->value.intval != (uint) pdval->value.intval)
            return_error(gs_error_rangecheck);
        ival = (uint) pdval->value.intval;
        code = 0;
    }
    if (ival < minval || ival > maxval) {
        if (code == 1)
            return_error(gs_error_undefined);
        else
            return_error(gs_error_rangecheck);
    }
    *pvalue = ival;
    return code;
}

/* Get a float parameter from a dictionary. */
/* Return 0 if found, 1 if defaulted, <0 if wrong type. */
int
dict_float_param(const ref * pdict, const char *kstr,
                 double defaultval, float *pvalue)
{
    ref *pdval;

    if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
        *pvalue = defaultval;
        return 1;
    }
    switch (r_type(pdval)) {
        case t_integer:
            *pvalue = (float)pdval->value.intval;
            return 0;
        case t_real:
            *pvalue = pdval->value.realval;
            return 0;
    }
    return_error(gs_error_typecheck);
}

/* Get an integer array from a dictionary. */
/* See idparam.h for specification. */
int
dict_int_array_check_param(const gs_memory_t *mem, const ref * pdict,
   const char *kstr, uint len, int *ivec, int under_error, int over_error)
{
    ref pa, *pdval;
    uint size;
    int i, code;

    if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0)
        return 0;
    if (!r_is_array(pdval))
        return_error(gs_error_typecheck);
    size = r_size(pdval);
    if (size > len)
        return_error(over_error);
    for (i = 0; i < size; i++) {
        code = array_get(mem, pdval, i, &pa);
        if (code < 0)
            return code;
        /* See dict_int_param above for why we allow reals here. */
        switch (r_type(&pa)) {
            case t_integer:
                if (pa.value.intval != (int)pa.value.intval)
                    return_error(gs_error_rangecheck);
                ivec[i] = (int)pa.value.intval;
                break;
            case t_real:
                if (pa.value.realval < min_int ||
                    pa.value.realval > max_int ||
                    pa.value.realval != (int)pa.value.realval
                    )
                    return_error(gs_error_rangecheck);
                ivec[i] = (int)pa.value.realval;
                break;
            default:
                return_error(gs_error_typecheck);
        }
    }
    return (size == len || under_error >= 0 ? size :
            gs_note_error(under_error));
}
int
dict_int_array_param(const gs_memory_t *mem, const ref * pdict,
   const char *kstr, uint maxlen, int *ivec)
{
    return dict_int_array_check_param(mem, pdict, kstr, maxlen, ivec,
                                      0, gs_error_limitcheck);
}
int
dict_ints_param(const gs_memory_t *mem, const ref * pdict,
   const char *kstr, uint len, int *ivec)
{
    return dict_int_array_check_param(mem, pdict, kstr, len, ivec,
                                      gs_error_rangecheck, gs_error_rangecheck);
}

/* Get a float array from a dictionary. */
/* Return the element count if OK, <0 if invalid. */
/* If the parameter is missing, then if defaultvec is NULL, return 0; */
/* if defaultvec is not NULL, copy it into fvec (maxlen elements) */
/* and return maxlen. */
int
dict_float_array_check_param(const gs_memory_t *mem,
                             const ref * pdict, const char *kstr,
                             uint len, float *fvec, const float *defaultvec,
                             int under_error, int over_error)
{
    ref *pdval;
    uint size;
    int code;

    if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
        if (defaultvec == NULL)
            return 0;
        memcpy(fvec, defaultvec, len * sizeof(float));
        return len;
    }

    if (!r_is_array(pdval))
        return_error(gs_error_typecheck);
    size = r_size(pdval);
    if (size > len)
        return_error(over_error);

    code = process_float_array(mem, pdval, size, fvec);
    return (code < 0 ? code :
            size == len || under_error >= 0 ? size :
            gs_note_error(under_error));
}
int
dict_float_array_param(const gs_memory_t *mem,
                       const ref * pdict, const char *kstr,
                       uint maxlen, float *fvec, const float *defaultvec)
{
    return dict_float_array_check_param(mem ,pdict, kstr, maxlen, fvec,
                                        defaultvec, 0, gs_error_limitcheck);
}
int
dict_floats_param(const gs_memory_t *mem,
                  const ref * pdict, const char *kstr,
                  uint maxlen, float *fvec, const float *defaultvec)
{
    return dict_float_array_check_param(mem, pdict, kstr, maxlen,
                                        fvec, defaultvec,
                                        gs_error_rangecheck, gs_error_rangecheck);
}

/* Do dict_floats_param() and store [/key any] array in $error.errorinfo
 * on failure. The key must be a permanently allocated C string.
 */
int
dict_floats_param_errorinfo(i_ctx_t *i_ctx_p,
                  const ref * pdict, const char *kstr,
                  uint maxlen, float *fvec, const float *defaultvec)
{
    ref *val;
    int code = dict_float_array_check_param(imemory, pdict, kstr, maxlen,
                              fvec, defaultvec, gs_error_rangecheck, gs_error_rangecheck);
    if (code < 0) {
       if (dict_find_string(pdict, kstr, &val) > 0)
          gs_errorinfo_put_pair(i_ctx_p, kstr, strlen(kstr), val);
    }
    return code;
}

/*
 * Get a procedure from a dictionary.  If the key is missing,
 *      defaultval = false means substitute t__invalid;
 *      defaultval = true means substitute an empty procedure.
 * In either case, return 1.
 */
int
dict_proc_param(const ref * pdict, const char *kstr, ref * pproc,
                bool defaultval)
{
    ref *pdval;

    if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
        if (defaultval)
            make_empty_const_array(pproc, a_readonly + a_executable);
        else
            make_t(pproc, t__invalid);
        return 1;
    }
    check_proc(*pdval);
    *pproc = *pdval;
    return 0;
}

/* Get a matrix from a dictionary. */
int
dict_matrix_param(const gs_memory_t *mem, const ref * pdict, const char *kstr, gs_matrix * pmat)
{
    ref *pdval;

    if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0)
        return_error(gs_error_typecheck);
    return read_matrix(mem, pdval, pmat);
}

/* Get a UniqueID or XUID from a dictionary. */
/* Return 0 if UniqueID, 1 if XUID, <0 if error. */
/* If there is no uid, return default. */
int
dict_uid_param(const ref * pdict, gs_uid * puid, int defaultval,
               gs_memory_t * mem, const i_ctx_t *i_ctx_p)
{
    ref *puniqueid;

    if (pdict == 0) {
        uid_set_invalid(puid);
        return defaultval;
    }
    /* In a Level 2 environment, check for XUID first. */
    if (level2_enabled &&
        dict_find_string(pdict, "XUID", &puniqueid) > 0
        ) {
        long *xvalues;
        uint size, i;

        if (!r_has_type(puniqueid, t_array))
            return_error(gs_error_typecheck);
        size = r_size(puniqueid);
        if (size == 0)
            return_error(gs_error_rangecheck);
        xvalues = (long *)gs_alloc_byte_array(mem, size, sizeof(long),
                                              "get XUID");

        if (xvalues == 0)
            return_error(gs_error_VMerror);
        /* Get the values from the XUID array. */
        for (i = 0; i < size; i++) {
            const ref *pvalue = puniqueid->value.const_refs + i;

            if (!r_has_type(pvalue, t_integer)) {
                gs_free_object(mem, xvalues, "get XUID");
                return_error(gs_error_typecheck);
            }
            xvalues[i] = pvalue->value.intval;
        }
        uid_set_XUID(puid, xvalues, size);
        return 1;
    }
    /* If no UniqueID entry, set the UID to invalid, */
    /* because UniqueID need not be present in all fonts, */
    /* and if it is, the legal range is 0 to 2^24-1. */
    if (dict_find_string(pdict, "UniqueID", &puniqueid) <= 0) {
        uid_set_invalid(puid);
        return defaultval;
    } else {
        if (!r_has_type(puniqueid, t_integer))
           return_error(gs_error_typecheck);
        if (puniqueid->value.intval < 0 || puniqueid->value.intval > 0x7fffffff)
           return_error(gs_error_rangecheck);
        /* Apparently fonts created by Fontographer often have */
        /* a UniqueID of 0, contrary to Adobe's specifications. */
        /* Treat 0 as equivalent to -1 (no UniqueID). */
        if (puniqueid->value.intval == 0) {
            uid_set_invalid(puid);
            return defaultval;
        } else
            uid_set_UniqueID(puid, puniqueid->value.intval);
    }
    return 0;
}

/* Check that a UID in a dictionary is equal to an existing, valid UID. */
bool
dict_check_uid_param(const ref * pdict, const gs_uid * puid)
{
    ref *puniqueid;

    if (uid_is_XUID(puid)) {
        uint size = uid_XUID_size(puid);
        uint i;

        if (dict_find_string(pdict, "XUID", &puniqueid) <= 0)
            return false;
        if (!r_has_type(puniqueid, t_array) ||
            r_size(puniqueid) != size
            )
            return false;
        for (i = 0; i < size; i++) {
            const ref *pvalue = puniqueid->value.const_refs + i;

            if (!r_has_type(pvalue, t_integer))
                return false;
            if (pvalue->value.intval != uid_XUID_values(puid)[i])
                return false;
        }
        return true;
    } else {
        if (dict_find_string(pdict, "UniqueID", &puniqueid) <= 0)
            return false;
        return (r_has_type(puniqueid, t_integer) &&
                puniqueid->value.intval == puid->id);
    }
}

/* Create and store [/key any] array in $error.errorinfo.
 * The key must be a permanently allocated C string.
 * This routine is here because it is often used with parameter dictionaries.
 */
int
gs_errorinfo_put_pair(i_ctx_t *i_ctx_p, const char *key, int len, const ref *any)
{
    int code;
    ref pair, *aptr, key_name, *pderror;

    code = name_ref(imemory_local, (const byte *)key, len, &key_name, 0);
    if (code < 0)
        return code;
    code = gs_alloc_ref_array(iimemory_local, &pair, a_readonly, 2, "gs_errorinfo_put_pair");
    if (code < 0)
        return code;
    aptr = pair.value.refs;
    ref_assign_new(aptr, &key_name);
    ref_assign_new(aptr+1, any);
    if (dict_find_string(systemdict, "$error", &pderror) <= 0 ||
        !r_has_type(pderror, t_dictionary) ||
        idict_put_string(pderror, "errorinfo", &pair) < 0
        )
        return_error(gs_error_Fatal);
    return 0;
}

/* Take a key's value from a given dictionary, create [/key any] array,
 * and store it in $error.errorinfo.
 * The key must be a permanently allocated C string.
 */
void
gs_errorinfo_put_pair_from_dict(i_ctx_t *i_ctx_p, const ref *op, const char *key)
{   ref *val, n;
    if (dict_find_string(op, key, &val) <= 0) {
        make_null(&n);
        val = &n;
    }
    gs_errorinfo_put_pair(i_ctx_p, key, strlen(key), val);
}