Debian Codegraph @

pdf/pdf_stack.c

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