/src/ghostpdl/pdf/pdf_stack.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2018-2022 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 | | /* Stack operations for the PDF interpreter */ |
17 | | |
18 | | #include "ghostpdf.h" |
19 | | #include "pdf_types.h" |
20 | | #include "pdf_int.h" |
21 | | #include "pdf_stack.h" |
22 | | |
23 | | int pdfi_pop(pdf_context *ctx, int num) |
24 | 190M | { |
25 | 190M | int code = 0; |
26 | 190M | if (num < 0) |
27 | 35.8k | return_error(gs_error_rangecheck); |
28 | | |
29 | 190M | if (pdfi_count_stack(ctx) < num) { |
30 | 2.32k | code = gs_note_error(gs_error_stackunderflow); |
31 | 2.32k | num = pdfi_count_stack(ctx); |
32 | 2.32k | pdfi_set_warning(ctx, 0, NULL, W_PDF_STACKUNDERFLOW, "pdfi_pop", NULL); |
33 | 2.32k | } |
34 | 528M | while(num) { |
35 | 338M | pdfi_countdown(ctx->stack_top[-1]); |
36 | 338M | ctx->stack_top--; |
37 | 338M | num--; |
38 | 338M | } |
39 | 190M | return code; |
40 | 190M | } |
41 | | |
42 | | int pdfi_push(pdf_context *ctx, pdf_obj *o) |
43 | 338M | { |
44 | 338M | pdf_obj **new_stack; |
45 | 338M | uint32_t entries = 0; |
46 | | |
47 | 338M | if (ctx->stack_top < ctx->stack_bot) |
48 | 40.3k | ctx->stack_top = ctx->stack_bot; |
49 | | |
50 | 338M | if (ctx->stack_top >= ctx->stack_limit) { |
51 | 291k | if (ctx->stack_size >= MAX_STACK_SIZE) |
52 | 1.07k | return_error(gs_error_pdf_stackoverflow); |
53 | | |
54 | 290k | new_stack = (pdf_obj **)gs_alloc_bytes(ctx->memory, (ctx->stack_size + INITIAL_STACK_SIZE) * sizeof (pdf_obj *), "pdfi_push_increase_interp_stack"); |
55 | 290k | if (new_stack == NULL) |
56 | 0 | return_error(gs_error_VMerror); |
57 | | |
58 | 290k | memcpy(new_stack, ctx->stack_bot, ctx->stack_size * sizeof(pdf_obj *)); |
59 | 290k | gs_free_object(ctx->memory, ctx->stack_bot, "pdfi_push_increase_interp_stack"); |
60 | | |
61 | 290k | entries = pdfi_count_total_stack(ctx); |
62 | | |
63 | 290k | ctx->stack_bot = new_stack; |
64 | 290k | ctx->stack_top = ctx->stack_bot + entries; |
65 | 290k | ctx->stack_size += INITIAL_STACK_SIZE; |
66 | 290k | ctx->stack_limit = ctx->stack_bot + ctx->stack_size; |
67 | 290k | } |
68 | | |
69 | 338M | *ctx->stack_top = o; |
70 | 338M | ctx->stack_top++; |
71 | 338M | pdfi_countup(o); |
72 | | |
73 | 338M | return 0; |
74 | 338M | } |
75 | | |
76 | | int pdfi_mark_stack(pdf_context *ctx, pdf_obj_type type) |
77 | 14.6M | { |
78 | 14.6M | pdf_obj *o; |
79 | 14.6M | int code; |
80 | | |
81 | 14.6M | if (type != PDF_ARRAY_MARK && type != PDF_DICT_MARK && type != PDF_PROC_MARK) |
82 | 0 | return_error(gs_error_typecheck); |
83 | | |
84 | 14.6M | code = pdfi_object_alloc(ctx, type, 0, &o); |
85 | 14.6M | if (code < 0) |
86 | 0 | return code; |
87 | | |
88 | 14.6M | code = pdfi_push(ctx, o); |
89 | 14.6M | if (code < 0) |
90 | 1 | pdfi_free_object(o); |
91 | 14.6M | return code; |
92 | 14.6M | } |
93 | | |
94 | | void pdfi_clearstack(pdf_context *ctx) |
95 | 11.0M | { |
96 | 11.0M | pdfi_pop(ctx, pdfi_count_stack(ctx)); |
97 | 11.0M | } |
98 | | |
99 | | int pdfi_count_to_mark(pdf_context *ctx, uint64_t *count) |
100 | 27.6M | { |
101 | 27.6M | pdf_obj *o = ctx->stack_top[- 1]; |
102 | 27.6M | int index = -1; |
103 | 27.6M | pdf_obj **save_bot = NULL; |
104 | | |
105 | 27.6M | save_bot = ctx->stack_bot + ctx->current_stream_save.stack_count; |
106 | | |
107 | 27.6M | *count = 0; |
108 | 675M | while (&ctx->stack_top[index] >= save_bot) { |
109 | 674M | if (pdfi_type_of(o) == PDF_ARRAY_MARK || pdfi_type_of(o) == PDF_DICT_MARK) |
110 | 27.3M | return 0; |
111 | 647M | o = ctx->stack_top[--index]; |
112 | 647M | (*count)++; |
113 | 647M | } |
114 | 27.6M | return_error(gs_error_unmatchedmark); |
115 | 27.6M | } |
116 | | |
117 | | int pdfi_clear_to_mark(pdf_context *ctx) |
118 | 13.9M | { |
119 | 13.9M | int code; |
120 | 13.9M | uint64_t count; |
121 | | |
122 | 13.9M | code = pdfi_count_to_mark(ctx, &count); |
123 | 13.9M | if (code < 0) |
124 | 247k | return code; |
125 | 13.6M | return pdfi_pop(ctx, count + 1); |
126 | 13.9M | } |
127 | | |
128 | | int |
129 | | pdfi_destack_real(pdf_context *ctx, double *d) |
130 | 2.06M | { |
131 | 2.06M | int code; |
132 | | |
133 | 2.06M | if (pdfi_count_stack(ctx) < 1) |
134 | 8.03k | return_error(gs_error_stackunderflow); |
135 | | |
136 | 2.06M | code = pdfi_obj_to_real(ctx, ctx->stack_top[-1], d); |
137 | 2.06M | if (code < 0) { |
138 | 10.8k | pdfi_clearstack(ctx); |
139 | 10.8k | return code; |
140 | 10.8k | } |
141 | 2.05M | pdfi_pop(ctx, 1); |
142 | | |
143 | 2.05M | return 0; |
144 | 2.06M | } |
145 | | |
146 | | int |
147 | | pdfi_destack_reals(pdf_context *ctx, double *d, int n) |
148 | 25.9M | { |
149 | 25.9M | int i, code; |
150 | | |
151 | 25.9M | if (pdfi_count_stack(ctx) < n) { |
152 | 513k | pdfi_clearstack(ctx); |
153 | 513k | return_error(gs_error_stackunderflow); |
154 | 513k | } |
155 | | |
156 | 127M | for (i = 0; i < n; i++) { |
157 | 102M | code = pdfi_obj_to_real(ctx, ctx->stack_top[i-n], &d[i]); |
158 | 102M | if (code < 0) { |
159 | 81.3k | pdfi_clearstack(ctx); |
160 | 81.3k | return code; |
161 | 81.3k | } |
162 | 102M | } |
163 | 25.3M | pdfi_pop(ctx, n); |
164 | | |
165 | 25.3M | return 0; |
166 | 25.4M | } |
167 | | |
168 | | int |
169 | | pdfi_destack_floats(pdf_context *ctx, float *d, int n) |
170 | 700k | { |
171 | 700k | int i, code; |
172 | | |
173 | 700k | if (pdfi_count_stack(ctx) < n) { |
174 | 34.1k | pdfi_clearstack(ctx); |
175 | 34.1k | return_error(gs_error_stackunderflow); |
176 | 34.1k | } |
177 | | |
178 | 4.64M | for (i = 0; i < n; i++) { |
179 | 3.98M | code = pdfi_obj_to_float(ctx, ctx->stack_top[i-n], &d[i]); |
180 | 3.98M | if (code < 0) { |
181 | 3.78k | pdfi_clearstack(ctx); |
182 | 3.78k | return code; |
183 | 3.78k | } |
184 | 3.98M | } |
185 | 662k | pdfi_pop(ctx, n); |
186 | | |
187 | 662k | return 0; |
188 | 665k | } |
189 | | |
190 | | int |
191 | | pdfi_destack_int(pdf_context *ctx, int64_t *i) |
192 | 540k | { |
193 | 540k | int code; |
194 | | |
195 | 540k | if (pdfi_count_stack(ctx) < 1) |
196 | 121 | return_error(gs_error_stackunderflow); |
197 | | |
198 | 540k | code = pdfi_obj_to_int(ctx, ctx->stack_top[-1], i); |
199 | 540k | if (code < 0) { |
200 | 6.41k | pdfi_clearstack(ctx); |
201 | 6.41k | return code; |
202 | 6.41k | } |
203 | 533k | pdfi_pop(ctx, 1); |
204 | | |
205 | 533k | return 0; |
206 | 540k | } |
207 | | |
208 | | int |
209 | | pdfi_destack_ints(pdf_context *ctx, int64_t *i64, int n) |
210 | 0 | { |
211 | 0 | int i, code; |
212 | |
|
213 | 0 | if (pdfi_count_stack(ctx) < n) { |
214 | 0 | pdfi_clearstack(ctx); |
215 | 0 | return_error(gs_error_stackunderflow); |
216 | 0 | } |
217 | | |
218 | 0 | for (i = 0; i < n; i++) { |
219 | 0 | code = pdfi_obj_to_int(ctx, ctx->stack_top[i-n], &i64[i]); |
220 | 0 | if (code < 0) { |
221 | 0 | pdfi_clearstack(ctx); |
222 | 0 | return code; |
223 | 0 | } |
224 | 0 | } |
225 | 0 | pdfi_pop(ctx, n); |
226 | |
|
227 | 0 | return 0; |
228 | 0 | } |