/* Create an op_array table. */
static int
alloc_op_array_table(i_ctx_t *i_ctx_p, uint size, uint space,
                     op_array_table *opt)
{
    uint save_space = ialloc_space(idmemory);
    int code;

    ialloc_set_space(idmemory, space);
    code = ialloc_ref_array(&opt->table, a_readonly, size,
                            "op_array table");
    ialloc_set_space(idmemory, save_space);
    if (code < 0)
        return code;
    refset_null(opt->table.value.refs, size);
    opt->nx_table =
        (ushort *) ialloc_byte_array(size, sizeof(ushort),
                                     "op_array nx_table");
    if (opt->nx_table == 0)
        return_error(e_VMerror);
    opt->count = 0;
    opt->attrs = space | a_executable;
    return 0;
}
/* Common setup for encoding and decoding filters */
static int
bhc_setup(os_ptr op, stream_BHC_state * pbhcs)
{
    int code;
    int num_counts;
    int data[max_hc_length + 1 + 256 + max_zero_run + 1];
    uint dsize;
    int i;
    uint num_values, accum;
    ushort *counts;
    ushort *values;

    check_type(*op, t_dictionary);
    check_dict_read(*op);
    if ((code = dict_bool_param(op, "FirstBitLowOrder", false,
                                &pbhcs->FirstBitLowOrder)) < 0 ||
        (code = dict_int_param(op, "MaxCodeLength", 1, max_hc_length,
                               max_hc_length, &num_counts)) < 0 ||
        (code = dict_bool_param(op, "EndOfData", true,
                                &pbhcs->EndOfData)) < 0 ||
        (code = dict_uint_param(op, "EncodeZeroRuns", 2, 256,
                                256, &pbhcs->EncodeZeroRuns)) < 0 ||
    /* Note: the code returned from the following call */
    /* is actually the number of elements in the array. */
        (code = dict_int_array_param(imemory, op, "Tables", countof(data),
                                     data)) <= 0
        )
        return (code < 0 ? code : gs_note_error(e_rangecheck));
    dsize = code;
    if (dsize <= num_counts + 2)
        return_error(e_rangecheck);
    for (i = 0, num_values = 0, accum = 0; i <= num_counts;
         i++, accum <<= 1
        ) {
        int count = data[i];

        if (count < 0)
            return_error(e_rangecheck);
        num_values += count;
        accum += count;
    }
    if (dsize != num_counts + 1 + num_values ||
        accum != 1 << (num_counts + 1) ||
        pbhcs->EncodeZeroRuns >
        (pbhcs->EndOfData ? num_values - 1 : num_values)
        )
        return_error(e_rangecheck);
    for (; i < num_counts + 1 + num_values; i++) {
        int value = data[i];

        if (value < 0 || value >= num_values)
            return_error(e_rangecheck);
    }
    pbhcs->definition.counts = counts =
        (ushort *) ialloc_byte_array(num_counts + 1, sizeof(ushort),
                                     "bhc_setup(counts)");
    pbhcs->definition.values = values =
        (ushort *) ialloc_byte_array(num_values, sizeof(ushort),
                                     "bhc_setup(values)");
    if (counts == 0 || values == 0) {
        ifree_object(values, "bhc_setup(values)");
        ifree_object(counts, "bhc_setup(counts)");
        return_error(e_VMerror);
    }
    for (i = 0; i <= num_counts; i++)
        counts[i] = data[i];
    pbhcs->definition.counts = counts;
    pbhcs->definition.num_counts = num_counts;
    for (i = 0; i < num_values; i++)
        values[i] = data[i + num_counts + 1];
    pbhcs->definition.values = values;
    pbhcs->definition.num_values = num_values;
    return 0;
}
Exemple #3
0
/* <in1> ... <function_struct> %execfunction <out1> ... */
int
zexecfunction(i_ctx_t *i_ctx_p)
{
    os_ptr op = osp;

        /*
         * Since this operator's name begins with %, the name is not defined
         * in systemdict.  The only place this operator can ever appear is
         * in the execute-only closure created by .buildfunction.
         * Therefore, in principle it is unnecessary to check the argument.
         * However, we do a little checking anyway just on general
         * principles.  Note that since the argument may be an instance of
         * any subclass of gs_function_t, we currently have no way to check
         * its type.
         */
    if (!r_is_struct(op) ||
        !r_has_masked_attrs(op, a_executable | a_execute, a_executable | a_all)
        )
        return_error(gs_error_typecheck);
    {
        gs_function_t *pfn = (gs_function_t *) op->value.pstruct;
        int m = pfn->params.m, n = pfn->params.n;
        int diff = n - (m + 1);

        if (diff > 0)
            check_ostack(diff);
        {
            float params[20];	/* arbitrary size, just to avoid allocs */
            float *in;
            float *out;
            int code = 0;

            if (m + n <= countof(params)) {
                in = params;
            } else {
                in = (float *)ialloc_byte_array(m + n, sizeof(float),
                                                "%execfunction(in/out)");
                if (in == 0)
                    code = gs_note_error(gs_error_VMerror);
            }
            out = in + m;
            if (code < 0 ||
                (code = float_params(op - 1, m, in)) < 0 ||
                (code = gs_function_evaluate(pfn, in, out)) < 0
                )
                DO_NOTHING;
            else {
                if (diff > 0)
                    push(diff);	/* can't fail */
                else if (diff < 0) {
                    pop(-diff);
                    op = osp;
                }
                code = make_floats(op + 1 - n, out, n);
            }
            if (in != params)
                ifree_object(in, "%execfunction(in)");
            return code;
        }
    }
}
Exemple #4
0
/* <string> <numarray|numstring> xyshow - */
static int
moveshow(i_ctx_t *i_ctx_p, bool have_x, bool have_y)
{
    os_ptr op = osp;
    gs_text_enum_t *penum = NULL;
    int code = op_show_setup(i_ctx_p, op - 1);
    int format;
    uint i, size, widths_needed;
    float *values;
    bool CPSI_mode = gs_currentcpsimode(imemory);

    if (code != 0)
        return code;
    format = num_array_format(op);
    if (format < 0)
        return format;
    size = num_array_size(op, format);
    values = (float *)ialloc_byte_array(size, sizeof(float), "moveshow");
    if (values == 0)
        return_error(gs_error_VMerror);
    if (CPSI_mode)
        memset(values, 0, size * sizeof(values[0])); /* Safety. */
    if ((code = gs_xyshow_begin(igs, op[-1].value.bytes, r_size(op - 1),
                                (have_x ? values : (float *)0),
                                (have_y ? values : (float *)0),
                                size, imemory_local, &penum)) < 0) {
        ifree_object(values, "moveshow");
        if (penum)	/* if there was an error, the text_enum may not have been allocated */
            penum->text.x_widths = penum->text.y_widths = NULL;
        return code;
    }
    if (CPSI_mode) {
        /* CET 13-29.PS page 2 defines a longer width array
           then the text requires, and CPSI silently ignores extra elements.
           So we need to compute exact number of characters
           to know how many elements to load and type check. */
        code = gs_text_count_chars(igs, gs_get_text_params(penum), imemory);
        if (code < 0)
            return code;
        widths_needed = code;
        if (have_x && have_y)
            widths_needed <<= 1;
    } else
        widths_needed = size;
    for (i = 0; i < widths_needed; ++i) {
        ref value;

        switch (code = num_array_get(imemory, op, format, i, &value)) {
        case t_integer:
            values[i] = (float)value.value.intval; break;
        case t_real:
            values[i] = value.value.realval; break;
        case t_null:
            code = gs_note_error(gs_error_rangecheck);
            /* falls through */
        default:
            ifree_object(values, "moveshow");
        penum->text.x_widths = penum->text.y_widths = NULL;
            return code;
        }
    }
    if ((code = op_show_finish_setup(i_ctx_p, penum, 2, NULL)) < 0) {
        ifree_object(values, "moveshow");
        penum->text.x_widths = penum->text.y_widths = NULL;
        return code;
    }
    pop(2);
    return op_show_continue(i_ctx_p);
}