summaryrefslogtreecommitdiff
path: root/psi/zgeneric.c
blob: 3e4fe2549c3ee31799e2edb113405eff05a20a06 (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
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
615
616
617
618
619
620
/* Copyright (C) 2001-2023 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.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* Array/string/dictionary generic operators for PostScript */
#include "memory_.h"
#include "ghost.h"
#include "gsstruct.h"		/* for st_bytes */
#include "oper.h"
#include "dstack.h"		/* for systemdict */
#include "estack.h"		/* for forall */
#include "iddict.h"
#include "iname.h"
#include "ipacked.h"
#include "ivmspace.h"
#include "store.h"

/* This file implements copy, get, put, getinterval, putinterval, */
/* length, and forall, which apply generically to */
/* arrays, strings, and dictionaries.  (Copy also has a special */
/* meaning for copying the top N elements of the stack.) */

/* See the comment in opdef.h for an invariant which allows */
/* more efficient implementation of forall. */

/* Forward references */
static int zcopy_integer(i_ctx_t *);
static int zcopy_interval(i_ctx_t *);
static int copy_interval(i_ctx_t *, os_ptr, uint, os_ptr, client_name_t);

/* <various1> <various2> copy <various> */
/* <obj1> ... <objn> <int> copy <obj1> ... <objn> <obj1> ... <objn> */
/* Note that this implements copy for arrays and strings, */
/* but not for dictionaries (see zcopy_dict in zdict.c). */
int
zcopy(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    int type = r_type(op);

    if (type == t_integer)
        return zcopy_integer(i_ctx_p);
    check_op(2);
    switch (type) {
        case t_array:
        case t_string:
            return zcopy_interval(i_ctx_p);
        case t_dictionary:
            return zcopy_dict(i_ctx_p);
        default:
            return_op_typecheck(op);
    }
}

/* <obj1> ... <objn> <int> copy <obj1> ... <objn> <obj1> ... <objn> */
static int
zcopy_integer(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    int count, i;
    int code;

    if ((uint) op->value.intval > (uint)(op - osbot)) {
        /* There might be enough elements in other blocks. */
        check_type(*op, t_integer);
        if (op->value.intval >= (int)ref_stack_count(&o_stack))
            return_error(gs_error_stackunderflow);
        if (op->value.intval < 0)
            return_error(gs_error_rangecheck);
        check_int_ltu(*op, ref_stack_count(&o_stack));
        count = op->value.intval;
    } else if (op1 + (count = op->value.intval) <= ostop) {
        /* Fast case. */
        memcpy((char *)op, (char *)(op - count), count * sizeof(ref));
        push(count - 1);
        return 0;
    }
    /* Do it the slow, general way. */
    code = ref_stack_push(&o_stack, count - 1);
    if (code < 0)
        return code;
    for (i = 0; i < count; i++)
        *ref_stack_index(&o_stack, i) =
            *ref_stack_index(&o_stack, i + count);
    return 0;
}

/* <array1> <array2> copy <subarray2> */
/* <string1> <string2> copy <substring2> */
static int
zcopy_interval(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    int code = copy_interval(i_ctx_p, op, 0, op1, "copy");

    if (code < 0)
        return code;
    r_set_size(op, r_size(op1));
    *op1 = *op;
    pop(1);
    return 0;
}

/* <array|dict|name|packedarray|string> length <int> */
static int
zlength(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    switch (r_type(op)) {
        case t_array:
        case t_string:
        case t_mixedarray:
        case t_shortarray:
            check_read(*op);
            make_int(op, r_size(op));
            return 0;
        case t_dictionary:
            check_dict_read(*op);
            make_int(op, dict_length(op));
            return 0;
        case t_name: {
            ref str;

            name_string_ref(imemory, op, &str);
            make_int(op, r_size(&str));
            return 0;
        }
        case t_astruct:
            if (gs_object_type(imemory, op->value.pstruct) != &st_bytes)
                return_error(gs_error_typecheck);
            check_read(*op);
            make_int(op, gs_object_size(imemory, op->value.pstruct));
            return 0;
        default:
            return_op_typecheck(op);
    }
}

/* <array|packedarray|string> <index> get <obj> */
/* <dict> <key> get <obj> */
static int
zget(i_ctx_t *i_ctx_p)
{
    int code;
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    ref *pvalue;

    switch (r_type(op1)) {
        case t_dictionary:
            check_dict_read(*op1);
            if (dict_find(op1, op, &pvalue) <= 0)
                return_error(gs_error_undefined);
            op[-1] = *pvalue;
            break;
        case t_string:
            check_read(*op1);
            check_int_ltu(*op, r_size(op1));
            make_int(op1, op1->value.bytes[(uint) op->value.intval]);
            break;
        case t_array:
        case t_mixedarray:
        case t_shortarray:
            check_type(*op, t_integer);
            check_read(*op1);
            code = array_get(imemory, op1, op->value.intval, op1);
            if (code < 0)
                return code;
            break;
        case t__invalid:
            return_error(gs_error_stackunderflow);
        default:
            return_error(gs_error_typecheck);
    }
    pop(1);
    return 0;
}

/* <array> <index> <obj> put - */
/* <dict> <key> <value> put - */
/* <string> <index> <int> put - */
static int
zput(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    os_ptr op2 = op1 - 1;
    byte *sdata;
    uint ssize;

    switch (r_type(op2)) {
        case t_dictionary:
            check_dict_write(*op2);
            {
                int code = idict_put(op2, op1, op);

                if (code < 0)
                    return code;	/* error */
            }
            break;
        case t_array:
            check_write(*op2);
            check_int_ltu(*op1, r_size(op2));
            store_check_dest(op2, op);
            {
                ref *eltp = op2->value.refs + (uint) op1->value.intval;

                ref_assign_old(op2, eltp, op, "put");
            }
            break;
        case t_mixedarray:	/* packed arrays are read-only */
        case t_shortarray:
            return_error(gs_error_invalidaccess);
        case t_string:
            sdata = op2->value.bytes;
            ssize = r_size(op2);
str:	    check_write(*op2);
            check_int_ltu(*op1, ssize);
            check_int_leu(*op, 0xff);
            sdata[(uint)op1->value.intval] = (byte)op->value.intval;
            break;
        case t_astruct:
            if (gs_object_type(imemory, op2->value.pstruct) != &st_bytes)
                return_error(gs_error_typecheck);
            sdata = r_ptr(op2, byte);
            ssize = gs_object_size(imemory, op2->value.pstruct);
            goto str;
        default:
            return_op_typecheck(op2);
    }
    pop(3);
    return 0;
}

/* <array> <index> <obj> .forceput - */
/* <dict> <key> <value> .forceput - */
/*
 * This forces a "put" even if the object is not writable, and (if the
 * object is systemdict or the save level is 0) even if the value is in
 * local VM.  It is meant to be used only for replacing the value of
 * FontDirectory in systemdict when switching between local and global VM,
 * and a few similar applications.  After initialization, this operator
 * should no longer be accessible by name.
 */
static int
zforceput(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    os_ptr op2 = op - 2;
    int code;

    switch (r_type(op2)) {
    case t_array:
        check_int_ltu(*op1, r_size(op2));
        if (r_space(op2) > r_space(op)) {
            if (imemory_save_level(iimemory))
                return_error(gs_error_invalidaccess);
        }
        {
            ref *eltp = op2->value.refs + (uint) op1->value.intval;

            ref_assign_old(op2, eltp, op, "put");
        }
        break;
    case t_dictionary:
        if (op2->value.pdict == systemdict->value.pdict ||
            !imemory_save_level(iimemory)
            ) {
            uint space = r_space(op2);

            r_set_space(op2, avm_local);
            code = idict_put(op2, op1, op);
            r_set_space(op2, space);
        } else
            code = idict_put(op2, op1, op);
        if (code < 0)
            return code;
        break;
    default:
        return_error(gs_error_typecheck);
    }
    pop(3);
    return 0;
}

/* <seq:array|packedarray|string> <index> <count> getinterval <subseq> */
static int
zgetinterval(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    os_ptr op2 = op1 - 1;
    uint index;
    uint count;

    switch (r_type(op2)) {
        default:
            return_op_typecheck(op2);
        case t_array:
        case t_string:
        case t_mixedarray:
        case t_shortarray:;
    }
    check_read(*op2);
    check_int_leu(*op1, r_size(op2));
    index = op1->value.intval;
    check_int_leu(*op, r_size(op2) - index);
    count = op->value.intval;
    switch (r_type(op2)) {
        case t_array:
            op2->value.refs += index;
            break;
        case t_string:
            op2->value.bytes += index;
            break;
        case t_mixedarray: {
            const ref_packed *packed = op2->value.packed;

            for (; index--;)
                packed = packed_next(packed);
            op2->value.packed = packed;
            break;
        }
        case t_shortarray:
            op2->value.packed += index;
            break;
    }
    r_set_size(op2, count);
    pop(2);
    return 0;
}

/* <array1> <index> <array2|packedarray2> putinterval - */
/* <string1> <index> <string2> putinterval - */
/* <bytestring1> <index> <string2> putinterval - */
static int
zputinterval(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr opindex = op - 1;
    os_ptr opto = opindex - 1;
    int code;

    switch (r_type(opto)) {
        default:
            return_error(gs_error_typecheck);
        case t__invalid:
            if (r_type(op) != t_array && r_type(op) != t_string && r_type(op) != t__invalid)
                return_error(gs_error_typecheck); /* to match Distiller */
            else
                return_error(gs_error_stackunderflow);
        case t_mixedarray:
        case t_shortarray:
            return_error(gs_error_invalidaccess);
        case t_array:
        case t_string:
            check_write(*opto);
            check_int_leu(*opindex, r_size(opto));
            code = copy_interval(i_ctx_p, opto, (uint)(opindex->value.intval),
                                 op, "putinterval");
            break;
        case t_astruct: {
            uint dsize, ssize, index;

            check_write(*opto);
            if (gs_object_type(imemory, opto->value.pstruct) != &st_bytes)
                return_error(gs_error_typecheck);
            dsize = gs_object_size(imemory, opto->value.pstruct);
            check_int_leu(*opindex, dsize);
            index = (uint)opindex->value.intval;
            check_read_type(*op, t_string);
            ssize = r_size(op);
            if (ssize > dsize - index)
                return_error(gs_error_rangecheck);
            memcpy(r_ptr(opto, byte) + index, op->value.const_bytes, ssize);
            code = 0;
            break;
        }
    }
    if (code >= 0)
        pop(3);
    return code;
}

/* <array|packedarray|string> <<element> proc> forall - */
/* <dict> <<key> <value> proc> forall - */
static int
    array_continue(i_ctx_t *),
    dict_continue(i_ctx_t *),
    string_continue(i_ctx_t *),
    packedarray_continue(i_ctx_t *);
static int forall_cleanup(i_ctx_t *);
static int
zforall(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr obj = op - 1;
    es_ptr ep = esp;
    es_ptr cproc = ep + 4;

    check_estack(6);
    check_proc(*op);
    switch (r_type(obj)) {
        default:
            return_op_typecheck(obj);
        case t_array:
            check_read(*obj);
            make_op_estack(cproc, array_continue);
            break;
        case t_dictionary:
            check_dict_read(*obj);
            make_int(cproc, dict_first(obj));
            ++cproc;
            make_op_estack(cproc, dict_continue);
            break;
        case t_string:
            check_read(*obj);
            make_op_estack(cproc, string_continue);
            break;
        case t_mixedarray:
        case t_shortarray:
            check_read(*obj);
            make_op_estack(cproc, packedarray_continue);
            break;
    }
    /*
     * Push:
     *   - a mark;
     *   - the composite object;
     *   - the procedure;
     *   - the iteration index (only for dictionaries, done above);
     * and invoke the continuation operator.
     */
    make_mark_estack(ep + 1, es_for, forall_cleanup);
    ep[2] = *obj;
    ep[3] = *op;
    esp = cproc - 1;
    ref_stack_pop(&o_stack, 2);
    return (*real_opproc(cproc))(i_ctx_p);
}
/* Continuation operator for arrays */
static int
array_continue(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    es_ptr obj = esp - 1;

    if (r_size(obj)) {		/* continue */
        push(1);
        r_dec_size(obj, 1);
        *op = *obj->value.refs;
        obj->value.refs++;
        esp += 2;
        *esp = obj[1];
        return o_push_estack;
    } else {			/* done */
        esp -= 3;		/* pop mark, object, proc */
        return o_pop_estack;
    }
}
/* Continuation operator for dictionaries */
static int
dict_continue(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    es_ptr obj = esp - 2;
    int index = esp->value.intval;

    push(2);			/* make room for key and value */
    if ((index = dict_next(obj, index, op - 1)) >= 0) {	/* continue */
        esp->value.intval = index;
        esp += 2;
        *esp = obj[1];
        return o_push_estack;
    } else {			/* done */
        pop(2);			/* undo push */
        esp -= 4;		/* pop mark, object, proc, index */
        return o_pop_estack;
    }
}
/* Continuation operator for strings */
static int
string_continue(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    es_ptr obj = esp - 1;

    if (r_size(obj)) {		/* continue */
        push(1);		/* check for result space on stack BEFORE changing string size */
        r_dec_size(obj, 1);	/* Bug 701550 :-O */
        make_int(op, *obj->value.bytes);
        obj->value.bytes++;
        esp += 2;
        *esp = obj[1];
        return o_push_estack;
    } else {			/* done */
        esp -= 3;		/* pop mark, object, proc */
        return o_pop_estack;
    }
}
/* Continuation operator for packed arrays */
static int
packedarray_continue(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    es_ptr obj = esp - 1;

    if (r_size(obj)) {		/* continue */
        const ref_packed *packed = obj->value.packed;

        r_dec_size(obj, 1);
        push(1);
        packed_get(imemory, packed, op);
        obj->value.packed = packed_next(packed);
        esp += 2;
        *esp = obj[1];
        return o_push_estack;
    } else {			/* done */
        esp -= 3;		/* pop mark, object, proc */
        return o_pop_estack;
    }
}
/* Vacuous cleanup procedure */
static int
forall_cleanup(i_ctx_t *i_ctx_p)
{
    return 0;
}

/* ------ Initialization procedure ------ */

const op_def zgeneric_op_defs[] =
{
    {"1copy", zcopy},
    {"2forall", zforall},
    {"3.forceput", zforceput},
    {"2get", zget},
    {"3getinterval", zgetinterval},
    {"1length", zlength},
    {"3put", zput},
    {"3putinterval", zputinterval},
                /* Internal operators */
    {"0%array_continue", array_continue},
    {"0%dict_continue", dict_continue},
    {"0%packedarray_continue", packedarray_continue},
    {"0%string_continue", string_continue},
    op_def_end(0)
};

/* ------ Shared routines ------ */

/* Copy an interval from one operand to another. */
/* This is used by both putinterval and string/array copy. */
/* The destination is known to be an array or string, */
/* and the starting index is known to be less than or equal to */
/* its length; nothing else has been checked. */
static int
copy_interval(i_ctx_t *i_ctx_p /* for ref_assign_old */, os_ptr prto,
              uint index, os_ptr prfrom, client_name_t cname)
{
    int fromtype = r_type(prfrom);
    uint fromsize = r_size(prfrom);

    if (!(fromtype == r_type(prto) ||
          ((fromtype == t_shortarray || fromtype == t_mixedarray) &&
           r_type(prto) == t_array))
        )
        return_op_typecheck(prfrom);
    check_read(*prfrom);
    check_write(*prto);
    if (fromsize > r_size(prto) - index)
        return_error(gs_error_rangecheck);
    switch (fromtype) {
        case t_array:
            {			/* We have to worry about aliasing, */
                /* but refcpy_to_old takes care of it for us. */
                return refcpy_to_old(prto, index, prfrom->value.refs,
                                     fromsize, idmemory, cname);
            }
        case t_string:
            {	/* memmove takes care of aliasing. */
                memmove(prto->value.bytes + index, prfrom->value.bytes,
                        fromsize);
            }
            break;
        case t_mixedarray:
        case t_shortarray:
            {	/* We don't have to worry about aliasing, because */
                /* packed arrays are read-only and hence the destination */
                /* can't be a packed array. */
                uint i;
                const ref_packed *packed = prfrom->value.packed;
                ref *pdest = prto->value.refs + index;
                ref elt;

                for (i = 0; i < fromsize; i++, pdest++) {
                    packed_get(imemory, packed, &elt);
                    ref_assign_old(prto, pdest, &elt, cname);
                    packed = packed_next(packed);
                }
            }
            break;
    }
    return 0;
}