/src/ghostpdl/psi/zarray.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2023 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., 39 Mesa Street, Suite 108A, San Francisco, |
13 | | CA 94129, USA, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* Array operators */ |
18 | | #include "memory_.h" |
19 | | #include "ghost.h" |
20 | | #include "ialloc.h" |
21 | | #include "ipacked.h" |
22 | | #include "oper.h" |
23 | | #include "store.h" |
24 | | |
25 | | /* The generic operators (copy, get, put, getinterval, putinterval, */ |
26 | | /* length, and forall) are implemented in zgeneric.c. */ |
27 | | |
28 | | /* <int> array <array> */ |
29 | | int |
30 | | zarray(i_ctx_t *i_ctx_p) |
31 | 41.0M | { |
32 | 41.0M | os_ptr op = osp; |
33 | 41.0M | uint size; |
34 | 41.0M | int code; |
35 | | |
36 | 41.0M | check_op(1); |
37 | 41.0M | check_type(*op, t_integer); |
38 | 41.0M | if (op->value.intval < 0) |
39 | 9 | return_error(gs_error_rangecheck); |
40 | 41.0M | if (op->value.intval > max_array_size) |
41 | 1 | return_error(gs_error_limitcheck); |
42 | 41.0M | size = op->value.intval; |
43 | 41.0M | code = ialloc_ref_array((ref *)op, a_all, size, "array"); |
44 | 41.0M | if (code < 0) |
45 | 0 | return code; |
46 | 41.0M | refset_null(op->value.refs, size); |
47 | 41.0M | return 0; |
48 | 41.0M | } |
49 | | |
50 | | /* <array> aload <obj_0> ... <obj_n-1> <array> */ |
51 | | static int |
52 | | zaload(i_ctx_t *i_ctx_p) |
53 | 11.2M | { |
54 | 11.2M | os_ptr op = osp; |
55 | 11.2M | ref aref; |
56 | 11.2M | uint asize; |
57 | | |
58 | 11.2M | check_op(1); |
59 | 11.2M | ref_assign(&aref, op); |
60 | 11.2M | if (!r_is_array(&aref)) |
61 | 11.2M | return_op_typecheck(op); |
62 | 11.2M | check_read(aref); |
63 | 11.2M | asize = r_size(&aref); |
64 | 11.2M | if (asize > ostop - op) { /* Use the slow, general algorithm. */ |
65 | 229 | int code = ref_stack_push(&o_stack, asize); |
66 | 229 | uint i; |
67 | 229 | const ref_packed *packed = aref.value.packed; |
68 | | |
69 | 229 | if (code < 0) |
70 | 0 | return code; |
71 | 2.21M | for (i = asize; i > 0; i--, packed = packed_next(packed)) { |
72 | 2.21M | ref *o = ref_stack_index(&o_stack, i); |
73 | 2.21M | if (o == NULL) |
74 | 0 | continue; |
75 | 2.21M | packed_get(imemory, packed, o); |
76 | 2.21M | } |
77 | 229 | *osp = aref; |
78 | 229 | return 0; |
79 | 229 | } |
80 | 11.2M | if (r_has_type(&aref, t_array)) |
81 | 11.1M | memcpy(op, aref.value.refs, asize * sizeof(ref)); |
82 | 101k | else { |
83 | 101k | uint i; |
84 | 101k | const ref_packed *packed = aref.value.packed; |
85 | 101k | os_ptr pdest = op; |
86 | | |
87 | 2.09M | for (i = 0; i < asize; i++, pdest++, packed = packed_next(packed)) |
88 | 1.99M | packed_get(imemory, packed, pdest); |
89 | 101k | } |
90 | 11.2M | push(asize); |
91 | 11.2M | ref_assign(op, &aref); |
92 | 11.2M | return 0; |
93 | 11.2M | } |
94 | | |
95 | | /* <obj_0> ... <obj_n-1> <array> astore <array> */ |
96 | | static int |
97 | | zastore(i_ctx_t *i_ctx_p) |
98 | 39.6M | { |
99 | 39.6M | os_ptr op = osp; |
100 | 39.6M | uint size; |
101 | 39.6M | int code; |
102 | | |
103 | 39.6M | check_op(1); |
104 | 39.6M | if (!r_is_array(op)) |
105 | 39.6M | return_op_typecheck(op); |
106 | 39.6M | size = r_size(op); |
107 | | /* Amazingly, the following is valid: 0 array noaccess astore */ |
108 | 39.6M | if (size == 0) |
109 | 205k | return 0; |
110 | 39.4M | if (!r_has_type_attrs(op, t_array, a_write)) |
111 | 0 | return_error(gs_error_invalidaccess); |
112 | 39.4M | if (size > op - osbot) { |
113 | | /* The store operation might involve other stack segments. */ |
114 | 9.83k | ref arr, *o; |
115 | | |
116 | 9.83k | if (size >= ref_stack_count(&o_stack)) |
117 | 0 | return_error(gs_error_stackunderflow); |
118 | 9.83k | arr = *op; |
119 | 9.83k | code = ref_stack_store(&o_stack, &arr, size, 1, 0, true, idmemory, |
120 | 9.83k | "astore"); |
121 | 9.83k | if (code < 0) |
122 | 0 | return code; |
123 | 9.83k | ref_stack_pop(&o_stack, size); |
124 | 9.83k | o = ref_stack_index(&o_stack, 0); |
125 | 9.83k | if (o == NULL) |
126 | 0 | return_error(gs_error_stackunderflow); |
127 | 9.83k | *o = arr; |
128 | 39.4M | } else { |
129 | 39.4M | code = refcpy_to_old(op, 0, op - size, size, idmemory, "astore"); |
130 | 39.4M | if (code < 0) |
131 | 0 | return code; |
132 | 39.4M | op[-(int)size] = *op; |
133 | 39.4M | pop(size); |
134 | 39.4M | } |
135 | 39.4M | return 0; |
136 | 39.4M | } |
137 | | |
138 | | /* ------ Initialization procedure ------ */ |
139 | | |
140 | | const op_def zarray_op_defs[] = |
141 | | { |
142 | | {"1aload", zaload}, |
143 | | {"1array", zarray}, |
144 | | {"1astore", zastore}, |
145 | | op_def_end(0) |
146 | | }; |