Coverage Report

Created: 2022-10-31 07:00

/src/ghostpdl/psi/zfarc4.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
18
/* this is the ps interpreter interface to the arcfour cipher filter
19
   used in PDF encryption. We provide both Decode and Encode filters;
20
   the cipher is symmetric, so the call to the underlying routines is
21
   identical between the two filters */
22
23
#include "memory_.h"
24
#include "ghost.h"
25
#include "oper.h"
26
#include "gsstruct.h"
27
#include "ialloc.h"
28
#include "idict.h"
29
#include "stream.h"
30
#include "strimpl.h"
31
#include "ifilter.h"
32
#include "sarc4.h"
33
34
/* <source> <dict> arcfour/filter <file> */
35
36
static int
37
z_arcfour_d(i_ctx_t * i_ctx_p)
38
0
{
39
0
    os_ptr op = osp;   /* i_ctx_p->op_stack.stack.p defined in osstack.h */
40
0
    ref *sop = NULL;
41
0
    stream_arcfour_state state;
42
0
    int code;
43
44
0
    state.x = state.y = 0;
45
46
    /* extract the key from the parameter dictionary */
47
0
    check_type(*op, t_dictionary);
48
0
    check_dict_read(*op);
49
0
    if (dict_find_string(op, "Key", &sop) <= 0)
50
0
        return_error(gs_error_rangecheck);
51
0
    if (!r_has_type(sop, t_string))
52
0
      return_error(gs_error_typecheck);
53
54
0
    code = s_arcfour_set_key(&state, sop->value.const_bytes, r_size(sop)); /* lgtm [cpp/weak-cryptographic-algorithm] */
55
0
    if (code < 0)
56
0
        return code;
57
58
    /* we pass npop=0, since we've no arguments left to consume */
59
    /* we pass 0 instead of the usual rspace(sop) will allocate storage for
60
       filter state from the same memory pool as the stream it's coding. this
61
       causes no trouble because we maintain no pointers */
62
0
    return filter_read(i_ctx_p, 0, &s_arcfour_template,
63
0
                       (stream_state *) & state, 0);
64
0
}
65
66
/* encode version of the filter */
67
static int
68
z_arcfour_e(i_ctx_t * i_ctx_p)
69
0
{
70
0
    os_ptr op = osp;   /* i_ctx_p->op_stack.stack.p defined in osstack.h */
71
0
    ref *sop = NULL;
72
0
    stream_arcfour_state state;
73
0
    int code;
74
75
0
    state.x = state.y = 0;
76
77
    /* extract the key from the parameter dictionary */
78
0
    check_type(*op, t_dictionary);
79
0
    check_dict_read(*op);
80
0
    if (dict_find_string(op, "Key", &sop) <= 0)
81
0
        return_error(gs_error_rangecheck);
82
0
    if (!r_has_type(sop, t_string))
83
0
      return_error(gs_error_typecheck);
84
85
0
    if ((code = s_arcfour_set_key(&state, sop->value.const_bytes, r_size(sop))) < 0)
86
0
        return code;
87
88
    /* we pass npop=0, since we've no arguments left to consume */
89
    /* we pass 0 instead of the usual rspace(sop) will allocate storage for
90
       filter state from the same memory pool as the stream it's coding. this
91
       causes no trouble because we maintain no pointers */
92
0
    return filter_write(i_ctx_p, 0, &s_arcfour_template,
93
0
                        (stream_state *) & state, 0);
94
0
}
95
96
/* Match the above routines to their postscript filter names.
97
   This is how our static routines get called externally. */
98
const op_def zfarc4_op_defs[] = {
99
    op_def_begin_filter(),
100
    {"2ArcfourDecode", z_arcfour_d},
101
    {"2ArcfourEncode", z_arcfour_e},
102
    op_def_end(0)
103
};