Coverage Report

Created: 2022-10-31 07:00

/src/ghostpdl/psi/zmisc1.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2021 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
/* Miscellaneous Type 1 font operators */
18
#include "memory_.h"
19
#include "ghost.h"
20
#include "oper.h"
21
#include "gscrypt1.h"
22
#include "stream.h"   /* for getting state of PFBD stream */
23
#include "strimpl.h"
24
#include "sfilter.h"
25
#include "idict.h"
26
#include "idparam.h"
27
#include "ifilter.h"
28
29
/* <state> <from_string> <to_string> .type1encrypt <new_state> <substring> */
30
/* <state> <from_string> <to_string> .type1decrypt <new_state> <substring> */
31
static int type1crypt(i_ctx_t *,
32
                       int (*)(byte *, const byte *, uint, ushort *));
33
static int
34
ztype1encrypt(i_ctx_t *i_ctx_p)
35
0
{
36
0
    return type1crypt(i_ctx_p, gs_type1_encrypt);
37
0
}
38
static int
39
ztype1decrypt(i_ctx_t *i_ctx_p)
40
0
{
41
0
    return type1crypt(i_ctx_p, gs_type1_decrypt);
42
0
}
43
static int
44
type1crypt(i_ctx_t *i_ctx_p,
45
           int (*proc)(byte *, const byte *, uint, ushort *))
46
0
{
47
0
    os_ptr op = osp;
48
0
    crypt_state state;
49
0
    uint ssize;
50
51
0
    check_type(op[-2], t_integer);
52
0
    state = op[-2].value.intval;
53
0
    if (op[-2].value.intval != state)
54
0
        return_error(gs_error_rangecheck);  /* state value was truncated */
55
0
    check_read_type(op[-1], t_string);
56
0
    check_write_type(*op, t_string);
57
0
    ssize = r_size(op - 1);
58
0
    if (r_size(op) < ssize)
59
0
        return_error(gs_error_rangecheck);
60
0
    discard((*proc)(op->value.bytes, op[-1].value.const_bytes, ssize,
61
0
                    &state)); /* can't fail */
62
0
    op[-2].value.intval = state;
63
0
    op[-1] = *op;
64
0
    r_set_size(op - 1, ssize);
65
0
    pop(1);
66
0
    return 0;
67
0
}
68
69
/* Get the seed parameter for eexecEncode/Decode. */
70
/* Return npop if OK. */
71
static int
72
eexec_param(os_ptr op, ushort * pcstate)
73
0
{
74
0
    int npop = 1;
75
76
0
    if (r_has_type(op, t_dictionary))
77
0
        ++npop, --op;
78
0
    check_type(*op, t_integer);
79
0
    *pcstate = op->value.intval;
80
0
    if (op->value.intval != *pcstate)
81
0
        return_error(gs_error_rangecheck);  /* state value was truncated */
82
0
    return npop;
83
0
}
84
85
/* <target> <seed> eexecEncode/filter <file> */
86
/* <target> <seed> <dict_ignored> eexecEncode/filter <file> */
87
static int
88
zexE(i_ctx_t *i_ctx_p)
89
0
{
90
0
    os_ptr op = osp;
91
0
    stream_exE_state state;
92
0
    int code = eexec_param(op, &state.cstate);
93
94
0
    if (code < 0)
95
0
        return code;
96
0
    return filter_write(i_ctx_p, code, &s_exE_template, (stream_state *)&state, 0);
97
0
}
98
99
/* <source> <seed> eexecDecode/filter <file> */
100
/* <source> <dict> eexecDecode/filter <file> */
101
static int
102
zexD(i_ctx_t *i_ctx_p)
103
7.55k
{
104
7.55k
    os_ptr op = osp;
105
7.55k
    stream_exD_state state = {0};
106
7.55k
    int code = 0;
107
108
7.55k
    (*s_exD_template.set_defaults)((stream_state *)&state);
109
7.55k
    if (r_has_type(op, t_dictionary)) {
110
7.55k
        uint cstate = 0;
111
7.55k
        bool is_eexec = false;
112
113
7.55k
        check_dict_read(*op);
114
7.55k
        if ((code = dict_uint_param(op, "seed", 0, 0xffff, 0x10000,
115
7.55k
                                    &cstate)) < 0 ||
116
7.55k
            (code = dict_int_param(op, "lenIV", 0, max_int, 4,
117
7.55k
                                   &state.lenIV)) < 0 ||
118
7.55k
            (code = dict_bool_param(op, "eexec", false,
119
7.55k
                                   &is_eexec)) < 0 ||
120
7.55k
            (code = dict_bool_param(op, "keep_spaces", false,
121
7.55k
                                   &state.keep_spaces)) < 0
122
7.55k
            )
123
0
            return code;
124
7.55k
        state.cstate = cstate;
125
7.55k
        state.binary = (is_eexec ? -1 : 1);
126
7.55k
        code = 1;
127
7.55k
    } else {
128
0
        state.binary = 1;
129
0
        code = eexec_param(op, &state.cstate);
130
0
    }
131
7.55k
    if (code < 0)
132
0
        return code;
133
    /*
134
     * If we're reading a .PFB file, let the filter know about it,
135
     * so it can read recklessly to the end of the binary section.
136
     */
137
7.55k
    if (r_has_type(op - 1, t_file)) {
138
7.52k
        stream *s = (op - 1)->value.pfile;
139
140
7.52k
        if (s->state != 0 && s->state->templat == &s_PFBD_template) {
141
0
            stream_PFBD_state *pss = (stream_PFBD_state *)s->state;
142
143
0
            state.pfb_state = pss;
144
            /*
145
             * If we're reading the binary section of a PFB stream,
146
             * avoid the conversion from binary to hex and back again.
147
             */
148
0
            if (pss->record_type == 2) {
149
                /*
150
                 * The PFB decoder may have converted some data to hex
151
                 * already.  Convert it back if necessary.
152
                 */
153
0
                if (pss->binary_to_hex && sbufavailable(s) > 0) {
154
0
                    state.binary = 0; /* start as hex */
155
0
                    state.hex_left = sbufavailable(s);
156
0
                } else {
157
0
                    state.binary = 1;
158
0
                }
159
0
                pss->binary_to_hex = 0;
160
0
            }
161
0
        }
162
7.52k
    }
163
7.55k
    return filter_read(i_ctx_p, code, &s_exD_template, (stream_state *)&state, 0);
164
7.55k
}
165
166
/* ------ Initialization procedure ------ */
167
168
const op_def zmisc1_op_defs[] =
169
{
170
    {"3.type1encrypt", ztype1encrypt},
171
    {"3.type1decrypt", ztype1decrypt},
172
    op_def_begin_filter(),
173
    {"2eexecEncode", zexE},
174
    {"2eexecDecode", zexD},
175
    op_def_end(0)
176
};