/src/ghostpdl/psi/zmisc.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 | | /* Miscellaneous operators */ |
18 | | |
19 | | #include "errno_.h" |
20 | | #include "memory_.h" |
21 | | #include "string_.h" |
22 | | #include "ctype_.h" |
23 | | #include "ghost.h" |
24 | | #include "gp.h" |
25 | | #include "oper.h" |
26 | | #include "ialloc.h" |
27 | | #include "idict.h" |
28 | | #include "dstack.h" /* for name lookup in bind */ |
29 | | #include "iname.h" |
30 | | #include "ipacked.h" |
31 | | #include "ivmspace.h" |
32 | | #include "store.h" |
33 | | #include "igstate.h" /* for gs_currentcpsimode */ |
34 | | #include "memento.h" |
35 | | #include "iscan.h" |
36 | | |
37 | | /**********************************************************************/ |
38 | | |
39 | | /* <proc> bind <proc> */ |
40 | | static inline bool |
41 | | r_is_ex_oper(const ref *rp) |
42 | 223M | { |
43 | 223M | return (r_has_attr(rp, a_executable) && |
44 | 223M | (r_btype(rp) == t_operator || r_type(rp) == t_oparray)); |
45 | 223M | } |
46 | | static int |
47 | | zbind(i_ctx_t *i_ctx_p) |
48 | 16.6M | { |
49 | 16.6M | os_ptr op = osp; |
50 | 16.6M | uint depth = 1; |
51 | 16.6M | ref defn; |
52 | 16.6M | register os_ptr bsp; |
53 | | |
54 | 16.6M | check_op(1); |
55 | 16.6M | switch (r_type(op)) { |
56 | 88.1k | case t_array: |
57 | 88.1k | if (!r_has_attr(op, a_write)) { |
58 | 0 | return 0; /* per PLRM3 */ |
59 | 0 | } |
60 | 8.00M | case t_mixedarray: |
61 | 9.68M | case t_shortarray: |
62 | 9.68M | defn = *op; |
63 | 9.68M | break; |
64 | 1.53M | case t_oparray: |
65 | 1.53M | defn = *op->value.const_refs; |
66 | 1.53M | break; |
67 | 5.40M | default: |
68 | 5.40M | return_op_typecheck(op); |
69 | 16.6M | } |
70 | 16.6M | push(1); |
71 | 11.2M | *op = defn; |
72 | 11.2M | bsp = op; |
73 | | /* |
74 | | * We must not make the top-level procedure read-only, |
75 | | * but we must bind it even if it is read-only already. |
76 | | * |
77 | | * Here are the invariants for the following loop: |
78 | | * `depth' elements have been pushed on the ostack; |
79 | | * For i < depth, p = ref_stack_index(&o_stack, i): |
80 | | * *p is an array (or packedarray) ref. |
81 | | */ |
82 | 156M | while (depth) { |
83 | 1.32G | while (r_size(bsp)) { |
84 | 1.18G | ref_packed *const tpp = (ref_packed *)bsp->value.packed; /* break const */ |
85 | | |
86 | 1.18G | r_dec_size(bsp, 1); |
87 | 1.18G | if (r_is_packed(tpp)) { |
88 | | /* Check for a packed executable name */ |
89 | 749M | ushort elt = *tpp; |
90 | | |
91 | 749M | if (r_packed_is_exec_name(&elt)) { |
92 | 173M | ref nref; |
93 | 173M | ref *pvalue; |
94 | | |
95 | 173M | name_index_ref(imemory, packed_name_index(&elt), |
96 | 173M | &nref); |
97 | 173M | if ((pvalue = dict_find_name(&nref)) != 0 && |
98 | 173M | r_is_ex_oper(pvalue) |
99 | 173M | ) { |
100 | 143M | store_check_dest(bsp, pvalue); |
101 | | /* |
102 | | * Always save the change, since this can only |
103 | | * happen once. |
104 | | */ |
105 | 143M | ref_do_save(bsp, tpp, "bind"); |
106 | 143M | *tpp = pt_tag(pt_executable_operator) + |
107 | 143M | op_index(pvalue); |
108 | 143M | } |
109 | 173M | } |
110 | 749M | bsp->value.packed = tpp + 1; |
111 | 749M | } else { |
112 | 433M | ref *const tp = bsp->value.refs++; |
113 | | |
114 | 433M | switch (r_type(tp)) { |
115 | 116M | case t_name: /* bind the name if an operator */ |
116 | 116M | if (r_has_attr(tp, a_executable)) { |
117 | 79.3M | ref *pvalue; |
118 | | |
119 | 79.3M | if ((pvalue = dict_find_name(tp)) != 0 && |
120 | 79.3M | r_is_ex_oper(pvalue) |
121 | 79.3M | ) { |
122 | 40.7M | store_check_dest(bsp, pvalue); |
123 | 40.7M | ref_assign_old(bsp, tp, pvalue, "bind"); |
124 | 40.7M | } |
125 | 79.3M | } |
126 | 116M | break; |
127 | 116M | case t_array: /* push into array if writable */ |
128 | 1.67M | if (!r_has_attr(tp, a_write)) |
129 | 270k | break; |
130 | 96.5M | case t_mixedarray: |
131 | 134M | case t_shortarray: |
132 | 134M | if (r_has_attr(tp, a_executable)) { |
133 | | /* Make reference read-only */ |
134 | 134M | r_clear_attrs(tp, a_write); |
135 | 134M | if (bsp >= ostop) { |
136 | | /* Push a new stack block. */ |
137 | 0 | ref temp; |
138 | 0 | int code; |
139 | |
|
140 | 0 | temp = *tp; |
141 | 0 | osp = bsp; |
142 | 0 | code = ref_stack_push(&o_stack, 1); |
143 | 0 | if (code < 0) { |
144 | 0 | ref_stack_pop(&o_stack, depth); |
145 | 0 | return_error(code); |
146 | 0 | } |
147 | 0 | bsp = osp; |
148 | 0 | *bsp = temp; |
149 | 0 | } else |
150 | 134M | *++bsp = *tp; |
151 | 134M | depth++; |
152 | 134M | } |
153 | 433M | } |
154 | 433M | } |
155 | 1.18G | } |
156 | 145M | bsp--; |
157 | 145M | depth--; |
158 | 145M | if (bsp < osbot) { /* Pop back to the previous stack block. */ |
159 | 0 | osp = bsp; |
160 | 0 | ref_stack_pop_block(&o_stack); |
161 | 0 | bsp = osp; |
162 | 0 | } |
163 | 145M | } |
164 | 11.2M | osp = bsp; |
165 | 11.2M | return 0; |
166 | 11.2M | } |
167 | | |
168 | | /* - serialnumber <int> */ |
169 | | static int |
170 | | zserialnumber(i_ctx_t *i_ctx_p) |
171 | 0 | { |
172 | 0 | os_ptr op = osp; |
173 | |
|
174 | 0 | push(1); |
175 | 0 | make_int(op, gp_serialnumber()); |
176 | 0 | return 0; |
177 | 0 | } |
178 | | |
179 | | /* - realtime <int> */ |
180 | | static int |
181 | | zrealtime(i_ctx_t *i_ctx_p) |
182 | 0 | { |
183 | 0 | os_ptr op = osp; |
184 | 0 | long secs_ns[2]; |
185 | 0 | gs_lib_ctx_t *libctx = gs_lib_ctx_get_interp_instance(imemory); |
186 | |
|
187 | 0 | gp_get_realtime(secs_ns); |
188 | 0 | secs_ns[1] -= libctx->real_time_0[1]; |
189 | 0 | secs_ns[0] -= libctx->real_time_0[0]; |
190 | 0 | push(1); |
191 | 0 | make_int(op, secs_ns[0] * 1000 + secs_ns[1] / 1000000); |
192 | 0 | return 0; |
193 | 0 | } |
194 | | |
195 | | /* - usertime <int> */ |
196 | | static int |
197 | | zusertime(i_ctx_t *i_ctx_p) |
198 | 19 | { |
199 | 19 | gs_context_state_t *current = (gs_context_state_t *)i_ctx_p; |
200 | 19 | os_ptr op = osp; |
201 | 19 | long secs_ns[2]; |
202 | | |
203 | 19 | gp_get_usertime(secs_ns); |
204 | 19 | if (!current->usertime_inited) { |
205 | 17 | current->usertime_inited = true; |
206 | 17 | current->usertime_0[0] = secs_ns[0]; |
207 | 17 | current->usertime_0[1] = secs_ns[1]; |
208 | 17 | } |
209 | 19 | secs_ns[0] -= current->usertime_0[0]; |
210 | 19 | secs_ns[1] -= current->usertime_0[1]; |
211 | 19 | push(1); |
212 | 19 | make_int(op, secs_ns[0] * 1000 + secs_ns[1] / 1000000); |
213 | 19 | return 0; |
214 | 19 | } |
215 | | |
216 | | /* ---------------- Non-standard operators ---------------- */ |
217 | | |
218 | | /* <string> getenv <value_string> true */ |
219 | | /* <string> getenv false */ |
220 | | static int |
221 | | zgetenv(i_ctx_t *i_ctx_p) |
222 | 75.7k | { |
223 | 75.7k | os_ptr op = osp; |
224 | 75.7k | char *str; |
225 | 75.7k | byte *value; |
226 | 75.7k | int len = 0; |
227 | | |
228 | 75.7k | check_op(1); |
229 | 75.7k | check_read_type(*op, t_string); |
230 | 75.7k | str = ref_to_string(op, imemory, "getenv key"); |
231 | 75.7k | if (str == 0) |
232 | 0 | return_error(gs_error_VMerror); |
233 | 75.7k | if (gp_getenv(str, (char *)0, &len) > 0) { /* key missing */ |
234 | 75.7k | ifree_string((byte *) str, r_size(op) + 1, "getenv key"); |
235 | 75.7k | make_false(op); |
236 | 75.7k | return 0; |
237 | 75.7k | } |
238 | 0 | value = ialloc_string(len, "getenv value"); |
239 | 0 | if (value == 0) { |
240 | 0 | ifree_string((byte *) str, r_size(op) + 1, "getenv key"); |
241 | 0 | return_error(gs_error_VMerror); |
242 | 0 | } |
243 | 0 | DISCARD(gp_getenv(str, (char *)value, &len)); /* can't fail */ |
244 | 0 | ifree_string((byte *) str, r_size(op) + 1, "getenv key"); |
245 | | /* Delete the stupid C string terminator. */ |
246 | 0 | value = iresize_string(value, len, len - 1, |
247 | 0 | "getenv value"); /* can't fail */ |
248 | 0 | push(1); |
249 | 0 | make_string(op - 1, a_all | icurrent_space, len - 1, value); |
250 | 0 | make_true(op); |
251 | 0 | return 0; |
252 | 0 | } |
253 | | |
254 | | /* - .defaultpapersize <string> true */ |
255 | | /* - .defaultpapersize false */ |
256 | | static int |
257 | | zdefaultpapersize(i_ctx_t *i_ctx_p) |
258 | 10.8k | { |
259 | 10.8k | os_ptr op = osp; |
260 | 10.8k | byte *value; |
261 | 10.8k | int len = 0, i; |
262 | | |
263 | 10.8k | if (gp_defaultpapersize((char *)0, &len) > 0) { |
264 | | /* no default paper size */ |
265 | 10.8k | push(1); |
266 | 10.8k | make_false(op); |
267 | 10.8k | return 0; |
268 | 10.8k | } |
269 | | |
270 | 0 | value = ialloc_string(len, "defaultpapersize value"); |
271 | 0 | if (value == 0) { |
272 | 0 | return_error(gs_error_VMerror); |
273 | 0 | } |
274 | 0 | DISCARD(gp_defaultpapersize((char *)value, &len)); /* can't fail */ |
275 | | /* Note 'len' includes the NULL terminator, which we can ignore */ |
276 | 0 | for (i = 0;i < (len - 1); i++) |
277 | 0 | value[i] = tolower(value[i]); |
278 | | |
279 | | /* Delete the stupid C string terminator. */ |
280 | 0 | value = iresize_string(value, len, len - 1, |
281 | 0 | "defaultpapersize value"); /* can't fail */ |
282 | 0 | push(2); |
283 | 0 | make_string(op - 1, a_all | icurrent_space, len - 1, value); |
284 | 0 | make_true(op); |
285 | 0 | return 0; |
286 | 0 | } |
287 | | |
288 | | /* <name> <proc> .makeoperator <oper> */ |
289 | | static int |
290 | | zmakeoperator(i_ctx_t *i_ctx_p) |
291 | 1.99M | { |
292 | 1.99M | os_ptr op = osp; |
293 | 1.99M | op_array_table *opt; |
294 | 1.99M | uint count; |
295 | 1.99M | ref *tab; |
296 | | |
297 | 1.99M | check_op(2); |
298 | 1.99M | check_type(op[-1], t_name); |
299 | 1.99M | check_proc(*op); |
300 | 1.99M | switch (r_space(op)) { |
301 | 1.99M | case avm_global: |
302 | 1.99M | opt = &i_ctx_p->op_array_table_global; |
303 | 1.99M | break; |
304 | 0 | case avm_local: |
305 | 0 | opt = &i_ctx_p->op_array_table_local; |
306 | 0 | break; |
307 | 0 | default: |
308 | 0 | return_error(gs_error_invalidaccess); |
309 | 1.99M | } |
310 | 1.99M | count = opt->count; |
311 | 1.99M | tab = opt->table.value.refs; |
312 | | /* |
313 | | * restore doesn't reset op_array_table.count, but it does |
314 | | * remove entries from op_array_table.table. Since we fill |
315 | | * the table in order, we can detect that a restore has occurred |
316 | | * by checking whether what should be the most recent entry |
317 | | * is occupied. If not, we scan backwards over the vacated entries |
318 | | * to find the true end of the table. |
319 | | */ |
320 | 1.99M | while (count > 0 && r_has_type(&tab[count - 1], t_null)) |
321 | 0 | --count; |
322 | 1.99M | if (count == r_size(&opt->table)) |
323 | 0 | return_error(gs_error_limitcheck); |
324 | 1.99M | ref_assign_old(&opt->table, &tab[count], op, "makeoperator"); |
325 | 1.99M | opt->nx_table[count] = name_index(imemory, op - 1); |
326 | 1.99M | op_index_ref(imemory, opt->base_index + count, op - 1); |
327 | 1.99M | opt->count = count + 1; |
328 | 1.99M | pop(1); |
329 | 1.99M | return 0; |
330 | 1.99M | } |
331 | | |
332 | | /* - .oserrno <int> */ |
333 | | static int |
334 | | zoserrno(i_ctx_t *i_ctx_p) |
335 | 3.11k | { |
336 | 3.11k | os_ptr op = osp; |
337 | | |
338 | 3.11k | push(1); |
339 | 3.11k | make_int(op, errno); |
340 | 3.11k | return 0; |
341 | 3.11k | } |
342 | | |
343 | | /* <int> .setoserrno - */ |
344 | | static int |
345 | | zsetoserrno(i_ctx_t *i_ctx_p) |
346 | 24.7k | { |
347 | 24.7k | os_ptr op = osp; |
348 | | |
349 | 24.7k | check_op(1); |
350 | 24.7k | check_type(*op, t_integer); |
351 | 24.7k | errno = op->value.intval; |
352 | 24.7k | pop(1); |
353 | 24.7k | return 0; |
354 | 24.7k | } |
355 | | |
356 | | /* <int> .oserrorstring <string> true */ |
357 | | /* <int> .oserrorstring false */ |
358 | | static int |
359 | | zoserrorstring(i_ctx_t *i_ctx_p) |
360 | 3.09k | { |
361 | 3.09k | os_ptr op = osp; |
362 | 3.09k | const char *str; |
363 | 3.09k | int code; |
364 | 3.09k | uint len; |
365 | 3.09k | byte ch; |
366 | | |
367 | 3.09k | check_op(1); |
368 | 3.09k | check_type(*op, t_integer); |
369 | 3.09k | str = gp_strerror((int)op->value.intval); |
370 | 3.09k | if (str == 0 || (len = strlen(str)) == 0) { |
371 | 0 | make_false(op); |
372 | 0 | return 0; |
373 | 0 | } |
374 | 3.09k | check_ostack(1); |
375 | 3.09k | code = string_to_ref(str, op, iimemory, ".oserrorstring"); |
376 | 3.09k | if (code < 0) |
377 | 0 | return code; |
378 | | /* Strip trailing end-of-line characters. */ |
379 | 3.09k | while ((len = r_size(op)) != 0 && |
380 | 3.09k | ((ch = op->value.bytes[--len]) == '\r' || ch == '\n') |
381 | 3.09k | ) |
382 | 0 | r_dec_size(op, 1); |
383 | 3.09k | push(1); |
384 | 3.09k | make_true(op); |
385 | 3.09k | return 0; |
386 | 3.09k | } |
387 | | |
388 | | /* <string> <bool> .setdebug - */ |
389 | | static int |
390 | | zsetdebug(i_ctx_t *i_ctx_p) |
391 | 0 | { |
392 | 0 | os_ptr op = osp; |
393 | 0 | check_op(2); |
394 | 0 | check_read_type(op[-1], t_string); |
395 | 0 | check_type(*op, t_boolean); |
396 | 0 | { |
397 | 0 | int i; |
398 | |
|
399 | 0 | for (i = 0; i < r_size(op - 1); i++) |
400 | 0 | gs_debug[op[-1].value.bytes[i] & 127] = |
401 | 0 | op->value.boolval; |
402 | 0 | } |
403 | 0 | pop(2); |
404 | 0 | return 0; |
405 | 0 | } |
406 | | |
407 | | /* .mementolistnew - */ |
408 | | static int |
409 | | zmementolistnewblocks(i_ctx_t *i_ctx_p) |
410 | 0 | { |
411 | 0 | Memento_listNewBlocks(); |
412 | 0 | return 0; |
413 | 0 | } |
414 | | |
415 | | /* There are a few cases where a customer/user might want CPSI behavior |
416 | | * instead of the GS default behavior. cmyk_to_rgb and Type 1 char fill |
417 | | * method are two that have come up so far. This operator allows a PS |
418 | | * program to control the behavior without needing to recompile. |
419 | | */ |
420 | | /* <bool> .setCPSImode - */ |
421 | | static int |
422 | | zsetCPSImode(i_ctx_t *i_ctx_p) |
423 | 0 | { |
424 | 0 | os_ptr op = osp; |
425 | 0 | check_op(1); |
426 | 0 | check_type(*op, t_boolean); |
427 | 0 | gs_setcpsimode(imemory, op->value.boolval); |
428 | 0 | if (op->value.boolval) { |
429 | 0 | i_ctx_p->scanner_options |= SCAN_CPSI_MODE; |
430 | 0 | } |
431 | 0 | else { |
432 | 0 | i_ctx_p->scanner_options &= ~(int)SCAN_CPSI_MODE; |
433 | 0 | } |
434 | 0 | pop(1); |
435 | 0 | return 0; |
436 | 0 | } |
437 | | |
438 | | /* - .getCPSImode <bool> */ |
439 | | static int |
440 | | zgetCPSImode(i_ctx_t *i_ctx_p) |
441 | 0 | { |
442 | 0 | os_ptr op = osp; |
443 | |
|
444 | 0 | push(1); |
445 | 0 | make_bool(op, gs_currentcpsimode(imemory)); |
446 | 0 | return 0; |
447 | 0 | } |
448 | | |
449 | | /* <int> .setscanconverter - */ |
450 | | static int |
451 | | zsetscanconverter(i_ctx_t *i_ctx_p) |
452 | 0 | { |
453 | 0 | int val; |
454 | |
|
455 | 0 | os_ptr op = osp; |
456 | 0 | check_op(1); |
457 | 0 | if (r_has_type(op, t_boolean)) |
458 | 0 | val = (int)op->value.boolval; |
459 | 0 | else if (r_has_type(op, t_integer)) |
460 | 0 | val = op->value.intval; |
461 | 0 | else |
462 | 0 | return_op_typecheck(op); |
463 | | |
464 | 0 | gs_setscanconverter(igs, val); |
465 | 0 | pop(1); |
466 | 0 | return 0; |
467 | 0 | } |
468 | | |
469 | | /* - .getscanconverter <int> */ |
470 | | static int |
471 | | zgetscanconverter(i_ctx_t *i_ctx_p) |
472 | 0 | { |
473 | 0 | os_ptr op = osp; |
474 | |
|
475 | 0 | push(1); |
476 | 0 | make_int(op, gs_getscanconverter(imemory)); |
477 | 0 | return 0; |
478 | 0 | } |
479 | | /* ------ Initialization procedure ------ */ |
480 | | |
481 | | const op_def zmisc_a_op_defs[] = |
482 | | { |
483 | | {"1bind", zbind}, |
484 | | {"1getenv", zgetenv}, |
485 | | {"0.defaultpapersize", zdefaultpapersize}, |
486 | | {"2.makeoperator", zmakeoperator}, |
487 | | {"0.oserrno", zoserrno}, |
488 | | {"1.oserrorstring", zoserrorstring}, |
489 | | {"0realtime", zrealtime}, |
490 | | {"0serialnumber", zserialnumber}, |
491 | | {"2.setdebug", zsetdebug}, |
492 | | {"0.mementolistnewblocks", zmementolistnewblocks}, |
493 | | {"1.setoserrno", zsetoserrno}, |
494 | | {"0usertime", zusertime}, |
495 | | op_def_end(0) |
496 | | }; |
497 | | |
498 | | const op_def zmisc_b_op_defs[] = |
499 | | { |
500 | | {"1.setCPSImode", zsetCPSImode}, |
501 | | {"0.getCPSImode", zgetCPSImode}, |
502 | | {"1.setscanconverter", zsetscanconverter}, |
503 | | {"0.getscanconverter", zgetscanconverter}, |
504 | | op_def_end(0) |
505 | | }; |