zdict.c 13.9 KB
Newer Older
1
/* Copyright (C) 2001-2019 Artifex Software, Inc.
2
   All Rights Reserved.
3

4 5 6
   This software is provided AS-IS with no warranty, either express or
   implied.

7 8 9 10 11
   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
12 13
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
14 15
*/

16

17 18 19 20 21 22 23 24 25 26
/* Dictionary operators */
#include "ghost.h"
#include "oper.h"
#include "iddict.h"
#include "dstack.h"
#include "ilevel.h"		/* for [count]dictstack */
#include "iname.h"		/* for dict_find_name */
#include "ipacked.h"		/* for inline dict lookup */
#include "ivmspace.h"
#include "store.h"
27
#include "iscan.h"              /* for SCAN_PDF_RULES */
28 29 30 31 32 33 34 35 36

/* <int> dict <dict> */
int
zdict(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    check_type(*op, t_integer);
    if (op->value.intval < 0)
37
        return_error(gs_error_rangecheck);
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    return dict_create((uint) op->value.intval, op);
}

/* <dict> maxlength <int> */
static int
zmaxlength(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    check_type(*op, t_dictionary);
    check_dict_read(*op);
    make_int(op, dict_maxlength(op));
    return 0;
}

/* <dict> begin - */
int
zbegin(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    check_type(*op, t_dictionary);
    check_dict_read(*op);
61
    if ( dsp == dstop ) {
62 63 64
        int code = ref_stack_extend(&d_stack, 1);

        if ( code < 0 ) {
65
            if (code == gs_error_dictstackoverflow) {
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
                /* Adobe doesn't restore the operand that caused stack */
                /* overflow. We do the same to match CET 20-02-02      */
                pop(1);
            }
            return code;
        }
    }
    ++dsp;
    ref_assign(dsp, op);
    dict_set_top();
    pop(1);
    return 0;
}

/* - end - */
int
zend(i_ctx_t *i_ctx_p)
{
    if (ref_stack_count_inline(&d_stack) == min_dstack_size) {
85
        /* We would underflow the d-stack. */
86
        return_error(gs_error_dictstackunderflow);
87 88
    }
    while (dsp == dsbot) {
89 90
        /* We would underflow the current block. */
        ref_stack_pop_block(&d_stack);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    }
    dsp--;
    dict_set_top();
    return 0;
}

/* <key> <value> def - */
/*
 * We make this into a separate procedure because
 * the interpreter will almost always call it directly.
 */
int
zop_def(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    ref *pvslot;

    /* The following combines a check_op(2) with a type check. */
    switch (r_type(op1)) {
111 112 113 114 115 116 117 118 119 120 121 122
        case t_name: {
            /* We can use the fast single-probe lookup here. */
            uint nidx = name_index(imemory, op1);
            uint htemp;

            if_dict_find_name_by_index_top(nidx, htemp, pvslot) {
                if (dtop_can_store(op))
                    goto ra;
            }
            break;		/* handle all slower cases */
            }
        case t_null:
123
            return_error(gs_error_typecheck);
124
        case t__invalid:
125
            return_error(gs_error_stackunderflow);
126 127 128 129 130 131
    }
    /*
     * Combine the check for a writable top dictionary with
     * the global/local store check.  See dstack.h for details.
     */
    if (!dtop_can_store(op)) {
132 133 134 135 136
        check_dict_write(*dsp);
        /*
         * If the dictionary is writable, the problem must be
         * an invalid store.
         */
137
        return_error(gs_error_invalidaccess);
138 139 140 141 142 143 144
    }
    /*
     * Save a level of procedure call in the common (redefinition)
     * case.  With the current interfaces, we pay a double lookup
     * in the uncommon case.
     */
    if (dict_find(dsp, op1, &pvslot) <= 0)
145
        return idict_put(dsp, op1, op);
146 147
ra:
    if ((pvslot->tas.type_attrs & (&i_ctx_p->memory)->test_mask) == 0)
148
        alloc_save_change(idmemory, &dsp->value.pdict->values, (ref_packed *)pvslot, "dict_put(value)");
149 150 151 152 153 154 155 156 157 158
    ref_assign_new_inline(pvslot,op);

    return 0;
}
int
zdef(i_ctx_t *i_ctx_p)
{
    int code = zop_def(i_ctx_p);

    if (code >= 0) {
159
        pop(2);
160 161 162 163 164 165 166 167 168 169 170 171
    }
    return code;
}

/* <key> load <value> */
static int
zload(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    ref *pvalue;

    switch (r_type(op)) {
172 173 174
        case t_name:
            /* Use the fast lookup. */
            if ((pvalue = dict_find_name(op)) == 0)
175
                return_error(gs_error_undefined);
176 177 178
            ref_assign(op, pvalue);
            return 0;
        case t_null:
179
            return_error(gs_error_typecheck);
180
        case t__invalid:
181
            return_error(gs_error_stackunderflow);
182 183 184 185 186 187 188 189 190 191 192 193 194 195
        default: {
                /* Use an explicit loop. */
                uint size = ref_stack_count(&d_stack);
                uint i;

                for (i = 0; i < size; i++) {
                    ref *dp = ref_stack_index(&d_stack, i);

                    check_dict_read(*dp);
                    if (dict_find(dp, op, &pvalue) > 0) {
                        ref_assign(op, pvalue);
                        return 0;
                    }
                }
196
                return_error(gs_error_undefined);
197
            }
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    }
}

/* get - implemented in zgeneric.c */

/* put - implemented in zgeneric.c */

/* <dict> <key> .undef - */
/* <dict> <key> undef - */
static int
zundef(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    int code;

    check_type(*op1, t_dictionary);
215
    check_dict_write(*op1);
216
    code = idict_undef(op1, op);
217
    if (code < 0 && code != gs_error_undefined) /* ignore undefined error */
218
        return code;
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    pop(2);
    return 0;
}

/* <dict> <key> known <bool> */
static int
zknown(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    register os_ptr op1 = op - 1;
    ref *pvalue;
    int code;

    check_type(*op1, t_dictionary);
    check_dict_read(*op1);
    code = dict_find(op1, op, &pvalue);
    switch (code) {
236
    case gs_error_dictfull:
237
        code = 0;
238
    case 0: case 1:
239
        break;
240
    default:
241
        return code;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    }
    make_bool(op1, code);
    pop(1);
    return 0;
}

/* <key> where <dict> true */
/* <key> where false */
int
zwhere(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    ref_stack_enum_t rsenum;

    check_op(1);
    ref_stack_enum_begin(&rsenum, &d_stack);
    do {
259 260 261 262 263 264 265 266
        const ref *const bot = rsenum.ptr;
        const ref *pdref = bot + rsenum.size;
        ref *pvalue;
        int code;

        while (pdref-- > bot) {
            check_dict_read(*pdref);
            code = dict_find(pdref, op, &pvalue);
267
            if (code < 0 && code != gs_error_dictfull)
268 269 270 271 272 273 274 275
                return code;
            if (code > 0) {
                push(1);
                ref_assign(op - 1, pdref);
                make_true(op);
                return 0;
            }
        }
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    } while (ref_stack_enum_next(&rsenum));
    make_false(op);
    return 0;
}

/* copy for dictionaries -- called from zcopy in zgeneric.c. */
/* Only the type of *op has been checked. */
int
zcopy_dict(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    int code;

    check_type(*op1, t_dictionary);
    check_dict_read(*op1);
    check_dict_write(*op);
    if (!imemory->gs_lib_ctx->dict_auto_expand &&
294 295
        (dict_length(op) != 0 || dict_maxlength(op) < dict_length(op1))
        )
296
        return_error(gs_error_rangecheck);
297 298
    code = idict_copy(op1, op);
    if (code < 0)
299
        return code;
300 301 302 303 304 305
    /*
     * In Level 1 systems, we must copy the access attributes too.
     * The only possible effect this can have is to make the
     * copy read-only if the original dictionary is read-only.
     */
    if (!level2_enabled)
306
        r_copy_attrs(dict_access_ref(op), a_write, dict_access_ref(op1));
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
    ref_assign(op1, op);
    pop(1);
    return 0;
}

/* - currentdict <dict> */
static int
zcurrentdict(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    push(1);
    ref_assign(op, dsp);
    return 0;
}

/* - countdictstack <int> */
static int
zcountdictstack(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    uint count = ref_stack_count(&d_stack);

    push(1);
    if (!level2_enabled)
332
        count--;		/* see dstack.h */
333 334 335 336 337 338 339 340 341 342 343 344
    make_int(op, count);
    return 0;
}

/* <array> dictstack <subarray> */
static int
zdictstack(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    uint count = ref_stack_count(&d_stack);

    if (!level2_enabled)
345
        count--;		/* see dstack.h */
346
    if (!r_is_array(op))
347
        return_op_typecheck(op);
348
    if (r_size(op) < count)
349
        return_error(gs_error_rangecheck);
350
    if (!r_has_type_attrs(op, t_array, a_write))
351
        return_error(gs_error_invalidaccess);
352
    return ref_stack_store(&d_stack, op, count, 0, 0, true, idmemory,
353
                           "dictstack");
354 355 356 357 358 359 360
}

/* - cleardictstack - */
static int
zcleardictstack(i_ctx_t *i_ctx_p)
{
    while (zend(i_ctx_p) >= 0)
361
        DO_NOTHING;
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    return 0;
}

/* ------ Extensions ------ */

/* -mark- <key0> <value0> <key1> <value1> ... .dicttomark <dict> */
/* This is the Level 2 >> operator. */
static int
zdicttomark(i_ctx_t *i_ctx_p)
{
    uint count2 = ref_stack_counttomark(&o_stack);
    ref rdict;
    int code;
    uint idx;

    if (count2 == 0)
378
        return_error(gs_error_unmatchedmark);
379 380
    count2--;
    if ((count2 & 1) != 0)
381
        return_error(gs_error_rangecheck);
382 383
    code = dict_create(count2 >> 1, &rdict);
    if (code < 0)
384
        return code;
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    if ((i_ctx_p->scanner_options & SCAN_PDF_RULES) != 0) {
       for (idx = count2; idx > 0; idx -= 2) {
           code = idict_put(&rdict,
                            ref_stack_index(&o_stack, idx - 1),
                            ref_stack_index(&o_stack, idx - 2));
           if (code < 0) {         /* There's no way to free the dictionary -- too bad. */
               return code;
           }
       }
    }
    else {
       /* << /a 1 /a 2 >> => << /a 1 >>, i.e., */
       /* we must enter the keys in top-to-bottom order. */
       for (idx = 0; idx < count2; idx += 2) {
           code = idict_put(&rdict,
                            ref_stack_index(&o_stack, idx + 1),
                            ref_stack_index(&o_stack, idx));
           if (code < 0) {         /* There's no way to free the dictionary -- too bad. */
               return code;
           }
       }
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
    }
    ref_stack_pop(&o_stack, count2);
    ref_assign(osp, &rdict);
    return code;
}

/* <dict1> <dict2> .forcecopynew <dict2> */
/*
 * This operator is a special-purpose accelerator for use by 'restore' (see
 * gs_dps1.ps).  Note that this operator does *not* require that dict2 be
 * writable.  Hence it is in the same category of "dangerous" operators as
 * .forceput and .forceundef.
 */
static int
zforcecopynew(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    int code;

    check_type(*op1, t_dictionary);
    check_dict_read(*op1);
    check_type(*op, t_dictionary);
    /*check_dict_write(*op);*/	/* see above */
    /* This is only recognized in Level 2 mode. */
    if (!imemory->gs_lib_ctx->dict_auto_expand)
432
        return_error(gs_error_undefined);
433 434
    code = idict_copy_new(op1, op);
    if (code < 0)
435
        return code;
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
    ref_assign(op1, op);
    pop(1);
    return 0;
}

/* <dict> <key> .forceundef - */
/*
 * This forces an "undef" even if the dictionary is not writable.
 * Like .forceput, it is meant to be used only in a few special situations,
 * and should not be accessible by name after initialization.
 */
static int
zforceundef(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

    check_type(op[-1], t_dictionary);
    /* Don't check_dict_write */
    idict_undef(op - 1, op);	/* ignore undefined error */
    pop(2);
    return 0;
}

/* <dict> <key> .knownget <value> true */
/* <dict> <key> .knownget false */
static int
zknownget(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    register os_ptr op1 = op - 1;
    ref *pvalue;

    check_type(*op1, t_dictionary);
    check_dict_read(*op1);
    if (dict_find(op1, op, &pvalue) <= 0) {
471 472
        make_false(op1);
        pop(1);
473
    } else {
474 475
        ref_assign(op1, pvalue);
        make_true(op);
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
    }
    return 0;
}

/* <dict> <key> .knownundef <bool> */
static int
zknownundef(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    int code;

    check_type(*op1, t_dictionary);
    check_dict_write(*op1);
    code = idict_undef(op1, op);
    make_bool(op1, code == 0);
    pop(1);
    return 0;
}

/* <dict> <int> .setmaxlength - */
static int
zsetmaxlength(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;
    os_ptr op1 = op - 1;
    uint new_size;
    int code;

    check_type(*op1, t_dictionary);
506
    check_dict_write(*op1);
507 508
    check_type(*op, t_integer);
    if (op->value.intval < 0)
509
        return_error(gs_error_rangecheck);
510 511
    new_size = (uint) op->value.intval;
    if (dict_length(op - 1) > new_size)
512
        return_error(gs_error_dictfull);
513 514
    code = idict_resize(op - 1, new_size);
    if (code >= 0)
515
        pop(2);
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
    return code;
}

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

/* We need to split the table because of the 16-element limit. */
const op_def zdict1_op_defs[] = {
    {"0cleardictstack", zcleardictstack},
    {"1begin", zbegin},
    {"0countdictstack", zcountdictstack},
    {"0currentdict", zcurrentdict},
    {"2def", zdef},
    {"1dict", zdict},
    {"0dictstack", zdictstack},
    {"0end", zend},
    {"2known", zknown},
    {"1load", zload},
    {"1maxlength", zmaxlength},
    {"2.undef", zundef},	/* we need this even in Level 1 */
    {"1where", zwhere},
    op_def_end(0)
};
const op_def zdict2_op_defs[] = {
539
                /* Extensions */
540 541 542 543 544 545
    {"1.dicttomark", zdicttomark},
    {"2.forcecopynew", zforcecopynew},
    {"2.forceundef", zforceundef},
    {"2.knownget", zknownget},
    {"1.knownundef", zknownundef},
    {"2.setmaxlength", zsetmaxlength},
546 547 548 549 550 551 552
        /*
         * In Level 2, >> is a synonym for .dicttomark, and undef for
         * .undef.  By giving the former their own entries, they will not be
         * "eq" to .dicttomark and .undef, but that doesn't matter, since
         * we're doing this only for the sake of Adobe- compatible error
         * stacks.
         */
553 554 555 556 557
    op_def_begin_level2(),
    {"1>>", zdicttomark},
    {"2undef", zundef},
    op_def_end(0)
};