Coverage Report

Created: 2022-10-31 07:00

/src/ghostpdl/psi/iutil.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2022 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
13
   CA 94945, U.S.A., +1(415)492-9861, for further information.
14
*/
15
16
17
/* Utilities for Ghostscript interpreter */
18
#include "math_.h"    /* for fabs */
19
#include "memory_.h"
20
#include "string_.h"
21
#include "ghost.h"
22
#include "ierrors.h"
23
#include "gsccode.h"    /* for gxfont.h */
24
#include "gsmatrix.h"
25
#include "gsutil.h"
26
#include "gxfont.h"
27
#include "strimpl.h"
28
#include "sstring.h"
29
#include "idict.h"
30
#include "ifont.h"    /* for FontID equality */
31
#include "imemory.h"
32
#include "iname.h"
33
#include "ipacked.h"    /* for array_get */
34
#include "iutil.h"    /* for checking prototypes */
35
#include "ivmspace.h"
36
#include "oper.h"
37
#include "store.h"
38
39
/*
40
 * By design choice, none of the procedures in this file take a context
41
 * pointer (i_ctx_p).  Since a number of them require a gs_dual_memory_t
42
 * for store checking or save bookkeeping, we need to #undef idmemory.
43
 */
44
#undef idmemory
45
46
/* ------ Object utilities ------ */
47
48
/* Define the table of ref type properties. */
49
const byte ref_type_properties[] = {
50
    REF_TYPE_PROPERTIES_DATA
51
};
52
53
/* Copy refs from one place to another. */
54
int
55
refcpy_to_old(ref * aref, uint index, const ref * from,
56
              uint size, gs_dual_memory_t *idmemory, client_name_t cname)
57
396M
{
58
396M
    ref *to = aref->value.refs + index;
59
396M
    int code = refs_check_space(from, size, r_space(aref));
60
61
396M
    if (code < 0)
62
0
        return code;
63
    /* We have to worry about aliasing.... */
64
396M
    if (to <= from || from + size <= to)
65
1.11G
        while (size--)
66
714M
            ref_assign_old(aref, to, from, cname), to++, from++;
67
0
    else
68
0
        for (from += size, to += size; size--;)
69
0
            from--, to--, ref_assign_old(aref, to, from, cname);
70
396M
    return 0;
71
396M
}
72
void
73
refcpy_to_new(ref * to, const ref * from, uint size,
74
              gs_dual_memory_t *idmemory)
75
0
{
76
0
    while (size--)
77
0
        ref_assign_new(to, from), to++, from++;
78
0
}
79
80
/* Fill a new object with nulls. */
81
void
82
refset_null_new(ref * to, uint size, uint new_mask)
83
605M
{
84
19.1G
    for (; size--; ++to)
85
18.5G
        make_ta(to, t_null, new_mask);
86
605M
}
87
88
/* Compare two objects for equality. */
89
static bool fid_eq(const gs_memory_t *mem, const gs_font *pfont1,
90
                    const gs_font *pfont2);
91
bool
92
obj_eq(const gs_memory_t *mem, const ref * pref1, const ref * pref2)
93
3.67G
{
94
3.67G
    ref nref;
95
96
3.67G
    if (r_type(pref1) != r_type(pref2)) {
97
        /*
98
         * Only a few cases need be considered here:
99
         * integer/real (and vice versa), name/string (and vice versa),
100
         * arrays, and extended operators.
101
         */
102
885M
        switch (r_type(pref1)) {
103
479k
            case t_integer:
104
479k
                return (r_has_type(pref2, t_real) &&
105
479k
                        pref2->value.realval == pref1->value.intval);
106
10.3M
            case t_real:
107
10.3M
                return (r_has_type(pref2, t_integer) &&
108
10.3M
                        pref2->value.intval == pref1->value.realval);
109
808M
            case t_name:
110
808M
                if (!r_has_type(pref2, t_string))
111
799M
                    return false;
112
8.83M
                name_string_ref(mem, pref1, &nref);
113
8.83M
                pref1 = &nref;
114
8.83M
                break;
115
10.5M
            case t_string:
116
10.5M
                if (!r_has_type(pref2, t_name))
117
1.24M
                    return false;
118
9.30M
                name_string_ref(mem, pref2, &nref);
119
9.30M
                pref2 = &nref;
120
9.30M
                break;
121
                /*
122
                 * Differing implementations of packedarray can be eq,
123
                 * if the length is zero, but an array is never eq to a
124
                 * packedarray.
125
                 */
126
523k
            case t_mixedarray:
127
523k
            case t_shortarray:
128
                /*
129
                 * Since r_type(pref1) is one of the above, this is a
130
                 * clever fast check for r_type(pref2) being the other.
131
                 */
132
523k
                return ((int)r_type(pref1) + (int)r_type(pref2) ==
133
523k
                        t_mixedarray + t_shortarray) &&
134
523k
                    r_size(pref1) == 0 && r_size(pref2) == 0;
135
55.8M
            default:
136
55.8M
                if (r_btype(pref1) != r_btype(pref2))
137
55.8M
                    return false;
138
885M
        }
139
885M
    }
140
    /*
141
     * Now do a type-dependent comparison.  This would be very simple if we
142
     * always filled in all the bytes of a ref, but we currently don't.
143
     */
144
2.80G
    switch (r_btype(pref1)) {
145
1.72M
        case t_array:
146
1.72M
            return ((pref1->value.refs == pref2->value.refs ||
147
1.72M
                     r_size(pref1) == 0) &&
148
1.72M
                    r_size(pref1) == r_size(pref2));
149
3
        case t_mixedarray:
150
89.2k
        case t_shortarray:
151
89.2k
            return ((pref1->value.packed == pref2->value.packed ||
152
89.2k
                     r_size(pref1) == 0) &&
153
89.2k
                    r_size(pref1) == r_size(pref2));
154
3.54M
        case t_boolean:
155
3.54M
            return (pref1->value.boolval == pref2->value.boolval);
156
31.8M
        case t_dictionary:
157
31.8M
            return (pref1->value.pdict == pref2->value.pdict);
158
92.2k
        case t_file:
159
92.2k
            return (pref1->value.pfile == pref2->value.pfile &&
160
92.2k
                    r_size(pref1) == r_size(pref2));
161
1.13G
        case t_integer:
162
1.13G
            return (pref1->value.intval == pref2->value.intval);
163
274k
        case t_mark:
164
68.3M
        case t_null:
165
68.3M
            return true;
166
1.49G
        case t_name:
167
1.49G
            return (pref1->value.pname == pref2->value.pname);
168
0
        case t_oparray:
169
51
        case t_operator:
170
51
            return (op_index(pref1) == op_index(pref2));
171
343k
        case t_real:
172
343k
            return (pref1->value.realval == pref2->value.realval);
173
112k
        case t_save:
174
112k
            return (pref2->value.saveid == pref1->value.saveid);
175
72.6M
        case t_string:
176
72.6M
            return (!bytes_compare(pref1->value.bytes, r_size(pref1),
177
72.6M
                                   pref2->value.bytes, r_size(pref2)));
178
0
        case t_device:
179
0
            return (pref1->value.pdevice->device == pref2->value.pdevice->device);
180
0
        case t_struct:
181
2
        case t_astruct:
182
2
        case t_pdfctx:
183
2
            return (pref1->value.pstruct == pref2->value.pstruct);
184
0
        case t_fontID:
185
            /* This is complicated enough to deserve a separate procedure. */
186
0
            return fid_eq(mem, r_ptr(pref1, gs_font), r_ptr(pref2, gs_font));
187
2.80G
    }
188
0
    return false;   /* shouldn't happen! */
189
2.80G
}
190
191
/*
192
 * Compare two FontIDs for equality.  In the Adobe implementations,
193
 * different scalings of a font have "equal" FIDs, so we do the same.
194
 * Furthermore, in more recent Adobe interpreters, different *copies* of a
195
 * font have equal FIDs -- at least for Type 1 and Type 3 fonts -- as long
196
 * as the "contents" of the font are the same.  We aren't sure that the
197
 * following matches the Adobe algorithm, but it's close enough to pass the
198
 * Genoa CET.
199
 */
200
/* (This is a single-use procedure, for clearer code.) */
201
static bool
202
fid_eq(const gs_memory_t *mem, const gs_font *pfont1, const gs_font *pfont2)
203
0
{
204
0
    while (pfont1->base != pfont1)
205
0
        pfont1 = pfont1->base;
206
0
    while (pfont2->base != pfont2)
207
0
        pfont2 = pfont2->base;
208
0
    if (pfont1 == pfont2)
209
0
        return true;
210
0
    switch (pfont1->FontType) {
211
0
    case 1: case 3:
212
0
        if (pfont1->FontType == pfont2->FontType)
213
0
            break;
214
0
    default:
215
0
        return false;
216
0
    }
217
    /* The following, while peculiar, appears to match CPSI. */
218
0
    {
219
0
        const gs_uid *puid1 = &((const gs_font_base *)pfont1)->UID;
220
0
        const gs_uid *puid2 = &((const gs_font_base *)pfont2)->UID;
221
0
    if (uid_is_UniqueID(puid1) || uid_is_UniqueID(puid2) ||
222
0
        ((uid_is_XUID(puid1) || uid_is_XUID(puid2)) &&
223
0
         !uid_equal(puid1, puid2)))
224
0
        return false;
225
0
    }
226
0
    {
227
0
        const font_data *pfd1 = (const font_data *)pfont1->client_data;
228
0
        const font_data *pfd2 = (const font_data *)pfont2->client_data;
229
230
0
        if (!(obj_eq(mem, &pfd1->BuildChar, &pfd2->BuildChar) &&
231
0
              obj_eq(mem, &pfd1->BuildGlyph, &pfd2->BuildGlyph) &&
232
0
              obj_eq(mem, &pfd1->Encoding, &pfd2->Encoding) &&
233
0
              obj_eq(mem, &pfd1->CharStrings, &pfd2->CharStrings)))
234
0
            return false;
235
0
        if (pfont1->FontType == 1) {
236
0
            ref *ppd1, *ppd2;
237
238
0
            if (dict_find_string(&pfd1->dict, "Private", &ppd1) > 0 &&
239
0
                dict_find_string(&pfd2->dict, "Private", &ppd2) > 0 &&
240
0
                !obj_eq(mem, ppd1, ppd2))
241
0
                return false;
242
0
        }
243
0
    }
244
0
    return true;
245
0
}
246
247
/* Compare two objects for identity. */
248
bool
249
obj_ident_eq(const gs_memory_t *mem, const ref * pref1, const ref * pref2)
250
0
{
251
0
    if (r_type(pref1) != r_type(pref2))
252
0
        return false;
253
0
    if (r_has_type(pref1, t_string))
254
0
        return (pref1->value.bytes == pref2->value.bytes &&
255
0
                r_size(pref1) == r_size(pref2));
256
0
    return obj_eq(mem, pref1, pref2);
257
0
}
258
259
/*
260
 * Set *pchars and *plen to point to the data of a name or string, and
261
 * return 0.  If the object isn't a name or string, return gs_error_typecheck.
262
 * If the object is a string without read access, return gs_error_invalidaccess.
263
 */
264
int
265
obj_string_data(const gs_memory_t *mem, const ref *op, const byte **pchars, uint *plen)
266
171M
{
267
171M
    switch (r_type(op)) {
268
123M
    case t_name: {
269
123M
        ref nref;
270
271
123M
        name_string_ref(mem, op, &nref);
272
123M
        *pchars = nref.value.bytes;
273
123M
        *plen = r_size(&nref);
274
123M
        return 0;
275
0
    }
276
47.8M
    case t_string:
277
47.8M
        check_read(*op);
278
47.8M
        *pchars = op->value.bytes;
279
47.8M
        *plen = r_size(op);
280
47.8M
        return 0;
281
0
    default:
282
0
        return_error(gs_error_typecheck);
283
171M
    }
284
171M
}
285
286
/*
287
 * Create a printable representation of an object, a la cvs and =
288
 * (full_print = 0), == (full_print = 1), or === (full_print = 2).  Return 0
289
 * if OK, 1 if the destination wasn't large enough, gs_error_invalidaccess if the
290
 * object's contents weren't readable.  If the return value is 0 or 1,
291
 * *prlen contains the amount of data returned.  start_pos is the starting
292
 * output position -- the first start_pos bytes of output are discarded.
293
 *
294
 * When (restart = false) return gs_error_rangecheck the when destination wasn't
295
 * large enough without modifying the destination. This is needed for
296
 * compatibility with Adobe implementation of cvs and cvrs, which don't
297
 * change the destination string on failure.
298
 *
299
 * The mem argument is only used for getting the type of structures,
300
 * not for allocating; if it is NULL and full_print != 0, structures will
301
 * print as --(struct)--.
302
 *
303
 * This rather complex API is needed so that a client can call obj_cvp
304
 * repeatedly to print on a stream, which may require suspending at any
305
 * point to handle stream callouts.
306
 */
307
static void ensure_dot(char *, size_t);
308
int
309
obj_cvp(const ref * op, byte * str, uint len, uint * prlen,
310
        int full_print, uint start_pos, const gs_memory_t *mem, bool restart)
311
196M
{
312
196M
    char buf[256];  /* big enough for any float, double, or struct name */
313
196M
    const byte *data = (const byte *)buf;
314
196M
    uint size;
315
196M
    int code;
316
196M
    ref nref;
317
318
196M
    if (full_print) {
319
1.58M
        static const char * const type_strings[] = { REF_TYPE_PRINT_STRINGS };
320
321
1.58M
        switch (r_btype(op)) {
322
2.83k
        case t_boolean:
323
270k
        case t_integer:
324
270k
            break;
325
1.47k
        case t_real: {
326
            /*
327
             * To get fully accurate output results for IEEE
328
             * single-precision floats (24 bits of mantissa), the ANSI %g
329
             * default of 6 digits is not enough; 9 are needed.
330
             * Unfortunately, using %.9g for floats (as opposed to doubles)
331
             * produces unfortunate artifacts such as 0.01 5 mul printing as
332
             * 0.049999997.  Therefore, we print using %g, and if the result
333
             * isn't accurate enough, print again using %.9g.
334
             * Unfortunately, a few PostScript programs 'know' that the
335
             * printed representation of floats fits into 6 digits (e.g.,
336
             * with cvs).  We resolve this by letting cvs, cvrs, and = do
337
             * what the Adobe interpreters appear to do (use %g), and only
338
             * produce accurate output for ==, for which there is no
339
             * analogue of cvs.  What a hack!
340
             */
341
1.47k
            float value = op->value.realval;
342
1.47k
            float scanned;
343
1.47k
            code = gs_snprintf(buf, sizeof(buf), "%g", value);
344
1.47k
            if (code <= 0)
345
0
                return_error(gs_error_undefinedresult);
346
1.47k
            code = sscanf(buf, "%f", &scanned);
347
1.47k
            if (code <= 0)
348
2
                return_error(gs_error_undefinedresult);
349
1.46k
            if (scanned != value)
350
38
                gs_snprintf(buf, sizeof(buf), "%.9g", value);
351
1.46k
            ensure_dot(buf, 256);
352
1.46k
            goto rs;
353
1.47k
        }
354
4.30k
        case t_operator:
355
5.46k
        case t_oparray:
356
5.46k
            code = obj_cvp(op, (byte *)buf + 2, sizeof(buf) - 4, &size, 0, 0, mem, restart);
357
5.46k
            if (code < 0)
358
0
                return code;
359
5.46k
            buf[0] = buf[1] = buf[size + 2] = buf[size + 3] = '-';
360
5.46k
            size += 4;
361
5.46k
            goto nl;
362
664k
        case t_name:
363
664k
            if (r_has_attr(op, a_executable)) {
364
13.3k
                code = obj_string_data(mem, op, &data, &size);
365
13.3k
                if (code < 0)
366
0
                    return code;
367
13.3k
                goto nl;
368
13.3k
            }
369
651k
            if (start_pos > 0)
370
0
                return obj_cvp(op, str, len, prlen, 0, start_pos - 1, mem, restart);
371
651k
            if (len < 1)
372
0
                return_error(gs_error_rangecheck);
373
651k
            code = obj_cvp(op, str + 1, len - 1, prlen, 0, 0, mem, restart);
374
651k
            if (code < 0)
375
0
                return code;
376
651k
            str[0] = '/';
377
651k
            ++*prlen;
378
651k
            return code;
379
0
        case t_null:
380
0
            data = (const byte *)"null";
381
0
            goto rs;
382
642k
        case t_string:
383
642k
            if (!r_has_attr(op, a_read))
384
2
                goto other;
385
642k
            size = r_size(op);
386
642k
            {
387
642k
                bool truncate = (full_print == 1 && size > CVP_MAX_STRING);
388
642k
                stream_cursor_read r;
389
642k
                stream_cursor_write w;
390
642k
                uint skip;
391
642k
                byte *wstr;
392
642k
                uint len1;
393
642k
                int status = 1;
394
395
642k
                if (start_pos == 0) {
396
289k
                    if (len < 1)
397
0
                        return_error(gs_error_rangecheck);
398
289k
                    str[0] = '(';
399
289k
                    skip = 0;
400
289k
                    wstr = str + 1;
401
352k
                } else {
402
352k
                    skip = start_pos - 1;
403
352k
                    wstr = str;
404
352k
                }
405
642k
                len1 = len + (str - wstr);
406
642k
                stream_cursor_read_init(&r, op->value.const_bytes, truncate ? CVP_MAX_STRING : size);
407
408
1.06M
                while (skip && status == 1) {
409
771k
                    uint written;
410
411
771k
                    stream_cursor_write_init(&w, (byte *)buf, min(skip + len1, sizeof(buf)));
412
771k
                    status = s_PSSE_template.process(NULL, &r, &w, false);
413
                    /* +1 accounts for crazy w.ptr initialisation - see stream_cursor_write_init() */
414
771k
                    written = (w.ptr - ((byte *)buf)) + 1;
415
771k
                    if (written > skip) {
416
352k
                        written -= skip;
417
352k
                        memcpy(wstr, buf + skip, written);
418
352k
                        wstr += written;
419
352k
                        skip = 0;
420
352k
                        break;
421
352k
                    }
422
418k
                    skip -= written;
423
418k
                }
424
                /*
425
                 * We can reach here with status == 0 (and skip != 0) if
426
                 * start_pos lies within the trailing ")" or  "...)".
427
                 */
428
642k
                if (status == 0) {
429
#ifdef DEBUG
430
                    if (skip > (truncate ? 4 : 1)) {
431
                        return_error(gs_error_Fatal);
432
                    }
433
#endif
434
49.5k
                }
435
642k
                stream_cursor_write_init(&w, (byte *)wstr, (size_t)((str + len) - wstr));
436
642k
                if (status == 1)
437
593k
                    status = s_PSSE_template.process(NULL, &r, &w, false);
438
642k
                *prlen = w.ptr - (str - 1);
439
642k
                if (status != 0)
440
352k
                    return 1;
441
289k
                if (truncate) {
442
41.5k
                    if (len - *prlen < 4 - skip)
443
43
                        return 1;
444
41.5k
                    memcpy(w.ptr + 1, &"...)"[skip], 4 - skip);
445
41.5k
                    *prlen += 4 - skip;
446
248k
                } else {
447
248k
                    if (len - *prlen < 1 - skip)
448
175
                        return 1;
449
248k
                    if (!skip) {
450
248k
                        w.ptr[1] = ')';
451
248k
                        *prlen += 1;
452
248k
                    }
453
248k
                }
454
289k
            }
455
289k
            return 0;
456
0
        case t_astruct:
457
0
        case t_struct:
458
0
            if (r_is_foreign(op)) {
459
                /* gs_object_type may not work. */
460
0
                data = (const byte *)"-foreign-struct-";
461
0
                goto rs;
462
0
            }
463
0
            if (!mem) {
464
0
                data = (const byte *)"-(struct)-";
465
0
                goto rs;
466
0
            }
467
0
            data = (const byte *)
468
0
                gs_struct_type_name_string(
469
0
                                gs_object_type(mem,
470
0
                                    (const obj_header_t *)op->value.pstruct));
471
0
            size = strlen((const char *)data);
472
0
            if (size > 4 && !memcmp(data + size - 4, "type", 4))
473
0
                size -= 4;
474
0
            if (size > sizeof(buf) - 3)
475
0
                return_error(gs_error_rangecheck);
476
0
            buf[0] = '-';
477
0
            memcpy(buf + 1, data, size);
478
0
            buf[size + 1] = '-';
479
0
            size += 2;
480
0
            data = (const byte *)buf;
481
0
            goto nl;
482
0
        case t_pdfctx:
483
0
            data = (const byte *)"-pdfcontext-";
484
0
            goto rs;
485
2.23k
        default:
486
2.23k
other:
487
2.23k
            {
488
2.23k
                int rtype = r_btype(op);
489
490
2.23k
                if (rtype >= countof(type_strings))
491
0
                    return_error(gs_error_rangecheck);
492
2.23k
                data = (const byte *)type_strings[rtype];
493
2.23k
                if (data == 0)
494
0
                    return_error(gs_error_rangecheck);
495
2.23k
            }
496
2.23k
            goto rs;
497
1.58M
        }
498
1.58M
    }
499
    /* full_print = 0 */
500
195M
    switch (r_btype(op)) {
501
42.7k
    case t_boolean:
502
42.7k
        data = (const byte *)(op->value.boolval ? "true" : "false");
503
42.7k
        break;
504
7.63M
    case t_integer:
505
7.63M
        gs_snprintf(buf, sizeof(buf), "%"PRIpsint, op->value.intval);
506
7.63M
        break;
507
1.39M
    case t_string:
508
1.39M
        check_read(*op);
509
        /* falls through */
510
124M
    case t_name:
511
124M
        code = obj_string_data(mem, op, &data, &size);
512
124M
        if (code < 0)
513
0
            return code;
514
124M
        goto nl;
515
124M
    case t_oparray: {
516
46.4M
        uint index = op_index(op);
517
46.4M
        const op_array_table *opt = get_op_array(mem, index);
518
519
46.4M
        name_index_ref(mem, opt->nx_table[index - opt->base_index], &nref);
520
46.4M
        name_string_ref(mem, &nref, &nref);
521
46.4M
        code = obj_string_data(mem, &nref, &data, &size);
522
46.4M
        if (code < 0)
523
0
            return code;
524
46.4M
        goto nl;
525
46.4M
    }
526
46.4M
    case t_operator: {
527
        /* Recover the name from the initialization table. */
528
1.68M
        uint index = op_index(op);
529
530
        /*
531
         * Check the validity of the index.  (An out-of-bounds index
532
         * is only possible when examining an invalid object using
533
         * the debugger.)
534
         */
535
1.68M
        if (index > 0 && index < op_def_count) {
536
1.68M
            data = (const byte *)(op_index_def(index)->oname + 1);
537
1.68M
            break;
538
1.68M
        }
539
        /* Internal operator, no name. */
540
62
        gs_snprintf(buf, sizeof(buf), "@"PRI_INTPTR, (intptr_t) op->value.opproc);
541
62
        break;
542
1.68M
    }
543
257k
    case t_real:
544
        /*
545
         * The value 0.0001 is a boundary case that the Adobe interpreters
546
         * print in f-format but at least some gs versions print in
547
         * e-format, presumably because of differences in the underlying C
548
         * library implementation.  Work around this here.
549
         */
550
257k
        if (op->value.realval == (float)0.0001) {
551
4
            strncpy(buf, "0.0001", 256);
552
257k
        } else {
553
257k
            gs_snprintf(buf, sizeof(buf), "%g", op->value.realval);
554
257k
        }
555
257k
        ensure_dot(buf, 256);
556
257k
        break;
557
14.8M
    default:
558
14.8M
        data = (const byte *)"--nostringval--";
559
195M
    }
560
24.4M
rs: size = strlen((const char *)data);
561
195M
nl: if (size < start_pos)
562
0
        return_error(gs_error_rangecheck);
563
195M
    if (!restart && size > len)
564
4
        return_error(gs_error_rangecheck);
565
195M
    size -= start_pos;
566
195M
    *prlen = min(size, len);
567
195M
    memmove(str, data + start_pos, *prlen);
568
195M
    return (size > len);
569
195M
}
570
/*
571
 * Make sure the converted form of a real number has at least one of an 'e'
572
 * or a decimal point, so it won't be mistaken for an integer.
573
 * Re-format the exponent to satisfy Genoa CET test.
574
 */
575
static void
576
ensure_dot(char *buf, size_t buflen)
577
258k
{
578
258k
    char *pe = strchr(buf, 'e');
579
258k
    if (pe) {
580
45.5k
        int i;
581
45.5k
        (void)sscanf(pe + 1, "%d", &i);
582
45.5k
        buflen -= (size_t)(pe - buf);
583
        /* MSVC .net 2005 express doesn't support "%+02d" */
584
45.5k
        if (i >= 0)
585
16.4k
            gs_snprintf(pe + 1, buflen, "+%02d", i);
586
29.0k
        else
587
29.0k
            gs_snprintf(pe + 1, buflen, "-%02d", -i);
588
213k
    } else if (strchr(buf, '.') == NULL) {
589
65.9k
        strcat(buf, ".0");
590
65.9k
    }
591
258k
}
592
593
/*
594
 * Create a printable representation of an object, a la cvs and =.  Return 0
595
 * if OK, gs_error_rangecheck if the destination wasn't large enough,
596
 * gs_error_invalidaccess if the object's contents weren't readable.  If pchars !=
597
 * NULL, then if the object was a string or name, store a pointer to its
598
 * characters in *pchars even if it was too large; otherwise, set *pchars =
599
 * str.  In any case, store the length in *prlen.
600
 */
601
int
602
obj_cvs(const gs_memory_t *mem, const ref * op, byte * str, uint len, uint * prlen,
603
        const byte ** pchars)
604
175M
{
605
175M
    int code = obj_cvp(op, str, len, prlen, 0, 0, mem, false);  /* NB: NULL memptr */
606
607
175M
    if (code == 1) {
608
0
        if (pchars)
609
0
            obj_string_data(mem, op, pchars, prlen);
610
0
        return gs_note_error(gs_error_rangecheck);
611
175M
    } else {
612
175M
        if (pchars)
613
175M
          *pchars = str;
614
175M
        return code;
615
175M
    }
616
175M
}
617
618
/* Find the index of an operator that doesn't have one stored in it. */
619
ushort
620
op_find_index(const ref * pref /* t_operator */ )
621
2.43M
{
622
2.43M
    op_proc_t proc = real_opproc(pref);
623
2.43M
    const op_def *const *opp = op_defs_all;
624
2.43M
    const op_def *const *opend = opp + (op_def_count / OP_DEFS_MAX_SIZE);
625
626
43.8M
    for (; opp < opend; ++opp) {
627
43.8M
        const op_def *def = *opp;
628
629
290M
        for (; def->oname != 0; ++def)
630
249M
            if (def->proc == proc)
631
2.43M
                return (opp - op_defs_all) * OP_DEFS_MAX_SIZE + (def - *opp);
632
43.8M
    }
633
    /* Lookup failed!  This isn't possible.... */
634
122
    return 0;
635
2.43M
}
636
637
/*
638
 * Convert an operator index to an operator or oparray ref.
639
 * This is only used for debugging and for 'get' from packed arrays,
640
 * so it doesn't have to be very fast.
641
 */
642
void
643
op_index_ref(const gs_memory_t *mem, uint index, ref * pref)
644
114M
{
645
114M
    const op_array_table *opt;
646
647
114M
    if (op_index_is_operator(index)) {
648
48.7M
        make_oper(pref, index, op_index_proc(index));
649
48.7M
        return;
650
48.7M
    }
651
65.8M
    opt = get_op_array(mem, index);
652
65.8M
    make_tasv(pref, t_oparray, opt->attrs, index,
653
65.8M
              const_refs, (opt->table.value.const_refs
654
65.8M
                           + index - opt->base_index));
655
65.8M
}
656
657
/* Get an element from an array of some kind. */
658
/* This is also used to index into Encoding vectors, */
659
/* the error name vector, etc. */
660
int
661
array_get(const gs_memory_t *mem, const ref * aref, long index_long, ref * pref)
662
13.6G
{
663
13.6G
    if ((ulong)index_long >= r_size(aref))
664
3.96k
        return_error(gs_error_rangecheck);
665
13.6G
    switch (r_type(aref)) {
666
11.8G
        case t_array:
667
11.8G
            {
668
11.8G
                const ref *pvalue = aref->value.refs + index_long;
669
670
11.8G
                ref_assign(pref, pvalue);
671
11.8G
            }
672
11.8G
            break;
673
1.22G
        case t_mixedarray:
674
1.22G
            {
675
1.22G
                const ref_packed *packed = aref->value.packed;
676
1.22G
                uint index = (uint)index_long;
677
678
18.5G
                for (; index--;)
679
17.3G
                    packed = packed_next(packed);
680
1.22G
                packed_get(mem, packed, pref);
681
1.22G
            }
682
1.22G
            break;
683
569M
        case t_shortarray:
684
569M
            {
685
569M
                const ref_packed *packed = aref->value.packed + index_long;
686
687
569M
                packed_get(mem, packed, pref);
688
569M
            }
689
569M
            break;
690
2
        default:
691
2
            return_error(gs_error_typecheck);
692
13.6G
    }
693
13.6G
    return 0;
694
13.6G
}
695
696
/* Get an element from a packed array. */
697
/* (This works for ordinary arrays too.) */
698
/* Source and destination are allowed to overlap if the source is packed, */
699
/* or if they are identical. */
700
void
701
packed_get(const gs_memory_t *mem, const ref_packed * packed, ref * pref)
702
3.01G
{
703
3.01G
    const ref_packed elt = *packed;
704
3.01G
    uint value = elt & packed_value_mask;
705
706
3.01G
    switch (elt >> r_packed_type_shift) {
707
0
        default:    /* (shouldn't happen) */
708
0
            make_null(pref);
709
0
            break;
710
49.1M
        case pt_executable_operator:
711
49.1M
            op_index_ref(mem, value, pref);
712
49.1M
            break;
713
486M
        case pt_integer:
714
486M
            make_int(pref, (ps_int)value + packed_min_intval);
715
486M
            break;
716
553M
        case pt_literal_name:
717
553M
            name_index_ref(mem, value, pref);
718
553M
            break;
719
1.81G
        case pt_executable_name:
720
1.81G
            name_index_ref(mem, value, pref);
721
1.81G
            r_set_attrs(pref, a_executable);
722
1.81G
            break;
723
107M
        case pt_full_ref:
724
107M
        case pt_full_ref + 1:
725
107M
            ref_assign(pref, (const ref *)packed);
726
3.01G
    }
727
3.01G
}
728
729
/* Check to make sure an interval contains no object references */
730
/* to a space younger than a given one. */
731
/* Return 0 or gs_error_invalidaccess. */
732
int
733
refs_check_space(const ref * bot, uint size, uint space)
734
396M
{
735
1.20G
    for (; size--; bot++)
736
805M
        store_check_space(space, bot);
737
396M
    return 0;
738
396M
}
739
740
/* ------ String utilities ------ */
741
742
/* Convert a C string to a Ghostscript string */
743
int
744
string_to_ref(const char *cstr, ref * pref, gs_ref_memory_t * mem,
745
              client_name_t cname)
746
34.8k
{
747
34.8k
    uint size = strlen(cstr);
748
34.8k
    int code = gs_alloc_string_ref(mem, pref, a_all, size, cname);
749
750
34.8k
    if (code < 0)
751
0
        return code;
752
34.8k
    memcpy(pref->value.bytes, cstr, size);
753
34.8k
    return 0;
754
34.8k
}
755
756
/* Convert a Ghostscript string to a C string. */
757
/* Return 0 iff the buffer can't be allocated. */
758
char *
759
ref_to_string(const ref * pref, gs_memory_t * mem, client_name_t cname)
760
713k
{
761
713k
    uint size = r_size(pref);
762
713k
    char *str = (char *)gs_alloc_string(mem, size + 1, cname);
763
764
713k
    if (str == 0)
765
0
        return 0;
766
713k
    memcpy(str, (const char *)pref->value.bytes, size);
767
713k
    str[size] = 0;
768
713k
    return str;
769
713k
}
770
771
/* ------ Operand utilities ------ */
772
773
/* Get N numeric operands from the stack or an array. */
774
/* Return a bit-mask indicating which ones are integers, */
775
/* or a (negative) error indication. */
776
/* The 1-bit in the bit-mask refers to the first operand. */
777
/* Store float versions of the operands at pval. */
778
/* The stack underflow check (check for t__invalid) is harmless */
779
/* if the operands come from somewhere other than the stack. */
780
int
781
num_params(const ref * op, int count, double *pval)
782
36.7M
{
783
36.7M
    int mask = 0;
784
785
36.7M
    pval += count;
786
212M
    while (--count >= 0) {
787
176M
        mask <<= 1;
788
176M
        switch (r_type(op)) {
789
32.6M
            case t_real:
790
32.6M
                *--pval = op->value.realval;
791
32.6M
                break;
792
143M
            case t_integer:
793
143M
                *--pval = (double)op->value.intval;
794
143M
                mask++;
795
143M
                break;
796
340
            case t__invalid:
797
340
                return_error(gs_error_stackunderflow);
798
1.28k
            default:
799
1.28k
                return_error(gs_error_typecheck);
800
176M
        }
801
176M
        op--;
802
176M
    }
803
    /* If count is very large, mask might overflow. */
804
    /* In this case we clearly don't care about the value of mask. */
805
36.7M
    return (mask < 0 ? 0 : mask);
806
36.7M
}
807
/* float_params doesn't bother to keep track of the mask. */
808
int
809
float_params(const ref * op, int count, float *pval)
810
20.4M
{
811
86.5M
    for (pval += count; --count >= 0; --op)
812
66.0M
        switch (r_type(op)) {
813
15.9M
            case t_real:
814
15.9M
                *--pval = op->value.realval;
815
15.9M
                break;
816
50.1M
            case t_integer:
817
50.1M
                *--pval = (float)op->value.intval;
818
50.1M
                break;
819
99
            case t__invalid:
820
99
                return_error(gs_error_stackunderflow);
821
75
            default:
822
75
                return_error(gs_error_typecheck);
823
66.0M
        }
824
20.4M
    return 0;
825
20.4M
}
826
827
/* Get N numeric parameters (as floating point numbers) from an array */
828
int
829
process_float_array(const gs_memory_t *mem, const ref * parray, int count, float * pval)
830
668k
{
831
668k
    int         code = 0, indx0 = 0;
832
833
    /* we assume parray is an array of some type, of adequate length */
834
668k
    if (r_has_type(parray, t_array))
835
666k
        return float_params(parray->value.refs + count - 1, count, pval);
836
837
    /* short/mixed array; convert the entries to refs */
838
2.86k
    while (count > 0 && code >= 0) {
839
1.43k
        int     i, subcount;
840
1.43k
        ref     ref_buff[20];   /* 20 is arbitrary */
841
842
1.43k
        subcount = (count > countof(ref_buff) ? countof(ref_buff) : count);
843
4.29k
        for (i = 0; i < subcount && code >= 0; i++)
844
2.86k
            code = array_get(mem, parray, (long)(i + indx0), &ref_buff[i]);
845
1.43k
        if (code >= 0)
846
1.43k
            code = float_params(ref_buff + subcount - 1, subcount, pval);
847
1.43k
        count -= subcount;
848
1.43k
        pval += subcount;
849
1.43k
        indx0 += subcount;
850
1.43k
    }
851
852
1.43k
    return code;
853
668k
}
854
855
/* Get a single real parameter. */
856
/* The only possible errors are gs_error_typecheck and gs_error_stackunderflow. */
857
/* If an error is returned, the return value is not updated. */
858
int
859
real_param(const ref * op, double *pparam)
860
103M
{
861
103M
    switch (r_type(op)) {
862
45.8M
        case t_integer:
863
45.8M
            *pparam = (double)op->value.intval;
864
45.8M
            break;
865
58.0M
        case t_real:
866
58.0M
            *pparam = op->value.realval;
867
58.0M
            break;
868
88
        case t__invalid:
869
88
            return_error(gs_error_stackunderflow);
870
2.24k
        default:
871
2.24k
            return_error(gs_error_typecheck);
872
103M
    }
873
103M
    return 0;
874
103M
}
875
int
876
float_param(const ref * op, float *pparam)
877
1.66M
{
878
1.66M
    double dval;
879
1.66M
    int code = real_param(op, &dval);
880
881
1.66M
    if (code >= 0)
882
1.66M
        *pparam = (float)dval; /* can't overflow */
883
1.66M
    return code;
884
1.66M
}
885
886
/* Get an integer parameter in a given range. */
887
int
888
int_param(const ref * op, int max_value, int *pparam)
889
965k
{
890
965k
    check_int_leu(*op, max_value);
891
965k
    *pparam = (int)op->value.intval;
892
965k
    return 0;
893
965k
}
894
895
/* Make real values on the operand stack. */
896
int
897
make_reals(ref * op, const double *pval, int count)
898
44.0k
{
899
    /* This should return gs_error_limitcheck if any real is too large */
900
    /* to fit into a float on the stack. */
901
220k
    for (; count--; op++, pval++)
902
176k
        make_real(op, *pval);
903
44.0k
    return 0;
904
44.0k
}
905
int
906
make_floats(ref * op, const float *pval, int count)
907
1.03M
{
908
    /* This should return gs_error_undefinedresult for infinities. */
909
4.95M
    for (; count--; op++, pval++)
910
3.92M
        make_real(op, *pval);
911
1.03M
    return 0;
912
1.03M
}
913
914
/* Compute the error code when check_proc fails. */
915
/* Note that the client, not this procedure, uses return_error. */
916
/* The stack underflow check is harmless in the off-stack case. */
917
int
918
check_proc_failed(const ref * pref)
919
165
{
920
165
    if (r_is_array(pref)) {
921
7
        if (r_has_attr(pref, a_executable))
922
0
            return gs_error_invalidaccess;
923
7
        else
924
7
            return gs_error_typecheck;
925
158
    } else {
926
158
        if (r_has_type(pref, t__invalid))
927
84
            return gs_error_stackunderflow;
928
74
        else
929
74
            return gs_error_typecheck;
930
158
    }
931
165
}
932
933
/* Compute the error code when a type check on the stack fails. */
934
/* Note that the client, not this procedure, uses return_error. */
935
int
936
check_type_failed(const ref * op)
937
46.1M
{
938
46.1M
    return (r_has_type(op, t__invalid) ? gs_error_stackunderflow : gs_error_typecheck);
939
46.1M
}
940
941
/* ------ Matrix utilities ------ */
942
943
/* Read a matrix operand. */
944
/* Return 0 if OK, error code if not. */
945
int
946
read_matrix(const gs_memory_t *mem, const ref * op, gs_matrix * pmat)
947
1.99M
{
948
1.99M
    int code;
949
1.99M
    ref values[6];
950
1.99M
    const ref *pvalues;
951
952
1.99M
    switch (r_type(op)) {
953
1.99M
        case t_array:
954
1.99M
            pvalues = op->value.refs;
955
1.99M
            break;
956
0
        case t_mixedarray:
957
3
        case t_shortarray:
958
3
            {
959
3
                int i;
960
961
3
                for (i = 0; i < 6; ++i) {
962
3
                    code = array_get(mem, op, (long)i, &values[i]);
963
3
                    if (code < 0)
964
3
                        return code;
965
3
                }
966
0
                pvalues = values;
967
0
            }
968
0
            break;
969
186
        default:
970
186
            return_op_typecheck(op);
971
1.99M
    }
972
1.99M
    check_read(*op);
973
1.99M
    if (r_size(op) != 6)
974
17
        return_error(gs_error_rangecheck);
975
1.99M
    code = float_params(pvalues + 5, 6, (float *)pmat);
976
1.99M
    return (code < 0 ? code : 0);
977
1.99M
}
978
979
/* Write a matrix operand. */
980
/* Return 0 if OK, error code if not. */
981
int
982
write_matrix_in(ref * op, const gs_matrix * pmat, gs_dual_memory_t *idmemory,
983
                gs_ref_memory_t *imem)
984
1.16M
{
985
1.16M
    ref *aptr;
986
1.16M
    const float *pel;
987
1.16M
    int i;
988
989
1.16M
    check_write_type(*op, t_array);
990
1.16M
    if (r_size(op) != 6)
991
0
        return_error(gs_error_rangecheck);
992
1.16M
    aptr = op->value.refs;
993
1.16M
    pel = (const float *)pmat;
994
8.18M
    for (i = 5; i >= 0; i--, aptr++, pel++) {
995
7.01M
        if (idmemory) {
996
6.91M
            ref_save(op, aptr, "write_matrix");
997
6.91M
            make_real_new(aptr, *pel);
998
6.91M
        } else {
999
98.5k
            make_tav(aptr, t_real, imemory_new_mask(imem), realval, *pel);
1000
98.5k
        }
1001
7.01M
    }
1002
1.16M
    return 0;
1003
1.16M
}