/src/ghostpdl/pdf/pdf_stack.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2018-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 | | /* 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 | 411M | { |
25 | 411M | int code = 0; |
26 | 411M | if (num < 0) |
27 | 102k | return_error(gs_error_rangecheck); |
28 | | |
29 | 411M | if (pdfi_count_stack(ctx) < num) { |
30 | 32.8k | code = gs_note_error(gs_error_stackunderflow); |
31 | 32.8k | num = pdfi_count_stack(ctx); |
32 | 32.8k | pdfi_set_warning(ctx, 0, NULL, W_PDF_STACKUNDERFLOW, "pdfi_pop", NULL); |
33 | 32.8k | } |
34 | 1.06G | while(num) { |
35 | 656M | pdfi_countdown(ctx->stack_top[-1]); |
36 | 656M | ctx->stack_top--; |
37 | 656M | num--; |
38 | 656M | } |
39 | 411M | return code; |
40 | 411M | } |
41 | | |
42 | | int pdfi_push(pdf_context *ctx, pdf_obj *o) |
43 | 656M | { |
44 | 656M | pdf_obj **new_stack; |
45 | 656M | uint32_t entries = 0; |
46 | | |
47 | 656M | if (ctx->stack_top < ctx->stack_bot) |
48 | 93.4k | ctx->stack_top = ctx->stack_bot; |
49 | | |
50 | 656M | if (ctx->stack_top >= ctx->stack_limit) { |
51 | 542k | if (ctx->stack_size >= MAX_STACK_SIZE) |
52 | 1.07k | return_error(gs_error_pdf_stackoverflow); |
53 | | |
54 | 541k | new_stack = (pdf_obj **)gs_alloc_bytes(ctx->memory, (ctx->stack_size + INITIAL_STACK_SIZE) * sizeof (pdf_obj *), "pdfi_push_increase_interp_stack"); |
55 | 541k | if (new_stack == NULL) |
56 | 0 | return_error(gs_error_VMerror); |
57 | | |
58 | 541k | memcpy(new_stack, ctx->stack_bot, ctx->stack_size * sizeof(pdf_obj *)); |
59 | 541k | gs_free_object(ctx->memory, ctx->stack_bot, "pdfi_push_increase_interp_stack"); |
60 | | |
61 | 541k | entries = pdfi_count_total_stack(ctx); |
62 | | |
63 | 541k | ctx->stack_bot = new_stack; |
64 | 541k | ctx->stack_top = ctx->stack_bot + entries; |
65 | 541k | ctx->stack_size += INITIAL_STACK_SIZE; |
66 | 541k | ctx->stack_limit = ctx->stack_bot + ctx->stack_size; |
67 | 541k | } |
68 | | |
69 | 656M | *ctx->stack_top = o; |
70 | 656M | ctx->stack_top++; |
71 | 656M | pdfi_countup(o); |
72 | | |
73 | 656M | return 0; |
74 | 656M | } |
75 | | |
76 | | int pdfi_mark_stack(pdf_context *ctx, pdf_obj_type type) |
77 | 28.0M | { |
78 | 28.0M | pdf_obj *o; |
79 | 28.0M | int code; |
80 | | |
81 | 28.0M | if (type != PDF_ARRAY_MARK && type != PDF_DICT_MARK && type != PDF_PROC_MARK) |
82 | 0 | return_error(gs_error_typecheck); |
83 | | |
84 | 28.0M | code = pdfi_object_alloc(ctx, type, 0, &o); |
85 | 28.0M | if (code < 0) |
86 | 0 | return code; |
87 | | |
88 | 28.0M | code = pdfi_push(ctx, o); |
89 | 28.0M | if (code < 0) |
90 | 1 | pdfi_free_object(o); |
91 | 28.0M | return code; |
92 | 28.0M | } |
93 | | |
94 | | void pdfi_clearstack(pdf_context *ctx) |
95 | 30.8M | { |
96 | 30.8M | pdfi_pop(ctx, pdfi_count_stack(ctx)); |
97 | 30.8M | } |
98 | | |
99 | | int pdfi_count_to_mark(pdf_context *ctx, uint64_t *count) |
100 | 54.3M | { |
101 | 54.3M | pdf_obj *o = ctx->stack_top[- 1]; |
102 | 54.3M | int index = -1; |
103 | 54.3M | pdf_obj **save_bot = NULL; |
104 | | |
105 | 54.3M | save_bot = ctx->stack_bot + ctx->current_stream_save.stack_count; |
106 | | |
107 | 54.3M | *count = 0; |
108 | 314M | while (&ctx->stack_top[index] >= save_bot) { |
109 | 313M | if (pdfi_type_of(o) == PDF_ARRAY_MARK || pdfi_type_of(o) == PDF_DICT_MARK || pdfi_type_of(o) == PDF_PROC_MARK ) |
110 | 53.7M | return 0; |
111 | 260M | o = ctx->stack_top[--index]; |
112 | 260M | (*count)++; |
113 | 260M | } |
114 | 54.3M | return_error(gs_error_unmatchedmark); |
115 | 54.3M | } |
116 | | |
117 | | int pdfi_clear_to_mark(pdf_context *ctx) |
118 | 27.2M | { |
119 | 27.2M | int code; |
120 | 27.2M | uint64_t count; |
121 | | |
122 | 27.2M | code = pdfi_count_to_mark(ctx, &count); |
123 | 27.2M | if (code < 0) |
124 | 263k | return code; |
125 | 26.9M | return pdfi_pop(ctx, count + 1); |
126 | 27.2M | } |
127 | | |
128 | | int |
129 | | pdfi_destack_real(pdf_context *ctx, double *d) |
130 | 6.64M | { |
131 | 6.64M | int code; |
132 | | |
133 | 6.64M | if (pdfi_count_stack(ctx) < 1) |
134 | 50.7k | return_error(gs_error_stackunderflow); |
135 | | |
136 | 6.59M | code = pdfi_obj_to_real(ctx, ctx->stack_top[-1], d); |
137 | 6.59M | if (code < 0) { |
138 | 47.1k | pdfi_clearstack(ctx); |
139 | 47.1k | return code; |
140 | 47.1k | } |
141 | 6.54M | pdfi_pop(ctx, 1); |
142 | | |
143 | 6.54M | return 0; |
144 | 6.59M | } |
145 | | |
146 | | int |
147 | | pdfi_destack_reals(pdf_context *ctx, double *d, int n) |
148 | 37.1M | { |
149 | 37.1M | int i, code; |
150 | | |
151 | 37.1M | if (pdfi_count_stack(ctx) < n) { |
152 | 2.02M | pdfi_clearstack(ctx); |
153 | 2.02M | return_error(gs_error_stackunderflow); |
154 | 2.02M | } |
155 | | |
156 | 153M | for (i = 0; i < n; i++) { |
157 | 118M | code = pdfi_obj_to_real(ctx, ctx->stack_top[i-n], &d[i]); |
158 | 118M | if (code < 0) { |
159 | 193k | pdfi_clearstack(ctx); |
160 | 193k | return code; |
161 | 193k | } |
162 | 118M | } |
163 | 34.8M | pdfi_pop(ctx, n); |
164 | | |
165 | 34.8M | return 0; |
166 | 35.0M | } |
167 | | |
168 | | int |
169 | | pdfi_destack_floats(pdf_context *ctx, float *d, int n) |
170 | 2.41M | { |
171 | 2.41M | int i, code; |
172 | | |
173 | 2.41M | if (pdfi_count_stack(ctx) < n) { |
174 | 199k | pdfi_clearstack(ctx); |
175 | 199k | return_error(gs_error_stackunderflow); |
176 | 199k | } |
177 | | |
178 | 15.4M | for (i = 0; i < n; i++) { |
179 | 13.2M | code = pdfi_obj_to_float(ctx, ctx->stack_top[i-n], &d[i]); |
180 | 13.2M | if (code < 0) { |
181 | 7.43k | pdfi_clearstack(ctx); |
182 | 7.43k | return code; |
183 | 7.43k | } |
184 | 13.2M | } |
185 | 2.20M | pdfi_pop(ctx, n); |
186 | | |
187 | 2.20M | return 0; |
188 | 2.21M | } |
189 | | |
190 | | int |
191 | | pdfi_destack_int(pdf_context *ctx, int64_t *i) |
192 | 2.90M | { |
193 | 2.90M | int code; |
194 | | |
195 | 2.90M | if (pdfi_count_stack(ctx) < 1) |
196 | 2.63k | return_error(gs_error_stackunderflow); |
197 | | |
198 | 2.90M | code = pdfi_obj_to_int(ctx, ctx->stack_top[-1], i); |
199 | 2.90M | if (code < 0) { |
200 | 36.5k | pdfi_clearstack(ctx); |
201 | 36.5k | return code; |
202 | 36.5k | } |
203 | 2.86M | pdfi_pop(ctx, 1); |
204 | | |
205 | 2.86M | return 0; |
206 | 2.90M | } |
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 | } |