1 /* Copyright (C) 2001-2006 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, modified
8 or distributed except as expressly authorized under the terms of that
9 license. Refer to licensing information at http://www.artifex.com/
10 or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11 San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
12 */
13
14 /* $Id: zgeneric.c 9778 2009-06-05 05:55:54Z alexcher $ */
15 /* Array/string/dictionary generic operators for PostScript */
16 #include "memory_.h"
17 #include "ghost.h"
18 #include "gsstruct.h" /* for st_bytes */
19 #include "oper.h"
20 #include "dstack.h" /* for systemdict */
21 #include "estack.h" /* for forall */
22 #include "iddict.h"
23 #include "iname.h"
24 #include "ipacked.h"
25 #include "ivmspace.h"
26 #include "store.h"
27
28 /* This file implements copy, get, put, getinterval, putinterval, */
29 /* length, and forall, which apply generically to */
30 /* arrays, strings, and dictionaries. (Copy also has a special */
31 /* meaning for copying the top N elements of the stack.) */
32
33 /* See the comment in opdef.h for an invariant which allows */
34 /* more efficient implementation of forall. */
35
36 /* Forward references */
37 static int zcopy_integer(i_ctx_t *);
38 static int zcopy_interval(i_ctx_t *);
39 static int copy_interval(i_ctx_t *, os_ptr, uint, os_ptr, client_name_t);
40
41 /* <various1> <various2> copy <various> */
42 /* <obj1> ... <objn> <int> copy <obj1> ... <objn> <obj1> ... <objn> */
43 /* Note that this implements copy for arrays and strings, */
44 /* but not for dictionaries (see zcopy_dict in zdict.c). */
45 int
zcopy(i_ctx_t * i_ctx_p)46 zcopy(i_ctx_t *i_ctx_p)
47 {
48 os_ptr op = osp;
49 int type = r_type(op);
50
51 if (type == t_integer)
52 return zcopy_integer(i_ctx_p);
53 check_op(2);
54 switch (type) {
55 case t_array:
56 case t_string:
57 return zcopy_interval(i_ctx_p);
58 case t_dictionary:
59 return zcopy_dict(i_ctx_p);
60 default:
61 return_op_typecheck(op);
62 }
63 }
64
65 /* <obj1> ... <objn> <int> copy <obj1> ... <objn> <obj1> ... <objn> */
66 static int
zcopy_integer(i_ctx_t * i_ctx_p)67 zcopy_integer(i_ctx_t *i_ctx_p)
68 {
69 os_ptr op = osp;
70 os_ptr op1 = op - 1;
71 int count, i;
72 int code;
73
74 if ((uint) op->value.intval > (uint)(op - osbot)) {
75 /* There might be enough elements in other blocks. */
76 check_type(*op, t_integer);
77 if (op->value.intval >= (int)ref_stack_count(&o_stack))
78 return_error(e_stackunderflow);
79 if (op->value.intval < 0)
80 return_error(e_rangecheck);
81 check_int_ltu(*op, ref_stack_count(&o_stack));
82 count = op->value.intval;
83 } else if (op1 + (count = op->value.intval) <= ostop) {
84 /* Fast case. */
85 memcpy((char *)op, (char *)(op - count), count * sizeof(ref));
86 push(count - 1);
87 return 0;
88 }
89 /* Do it the slow, general way. */
90 code = ref_stack_push(&o_stack, count - 1);
91 if (code < 0)
92 return code;
93 for (i = 0; i < count; i++)
94 *ref_stack_index(&o_stack, i) =
95 *ref_stack_index(&o_stack, i + count);
96 return 0;
97 }
98
99 /* <array1> <array2> copy <subarray2> */
100 /* <string1> <string2> copy <substring2> */
101 static int
zcopy_interval(i_ctx_t * i_ctx_p)102 zcopy_interval(i_ctx_t *i_ctx_p)
103 {
104 os_ptr op = osp;
105 os_ptr op1 = op - 1;
106 int code = copy_interval(i_ctx_p, op, 0, op1, "copy");
107
108 if (code < 0)
109 return code;
110 r_set_size(op, r_size(op1));
111 *op1 = *op;
112 pop(1);
113 return 0;
114 }
115
116 /* <array|dict|name|packedarray|string> length <int> */
117 static int
zlength(i_ctx_t * i_ctx_p)118 zlength(i_ctx_t *i_ctx_p)
119 {
120 os_ptr op = osp;
121 switch (r_type(op)) {
122 case t_array:
123 case t_string:
124 case t_mixedarray:
125 case t_shortarray:
126 check_read(*op);
127 make_int(op, r_size(op));
128 return 0;
129 case t_dictionary:
130 check_dict_read(*op);
131 make_int(op, dict_length(op));
132 return 0;
133 case t_name: {
134 ref str;
135
136 name_string_ref(imemory, op, &str);
137 make_int(op, r_size(&str));
138 return 0;
139 }
140 case t_astruct:
141 if (gs_object_type(imemory, op->value.pstruct) != &st_bytes)
142 return_error(e_typecheck);
143 check_read(*op);
144 make_int(op, gs_object_size(imemory, op->value.pstruct));
145 return 0;
146 default:
147 return_op_typecheck(op);
148 }
149 }
150
151 /* <array|packedarray|string> <index> get <obj> */
152 /* <dict> <key> get <obj> */
153 static int
zget(i_ctx_t * i_ctx_p)154 zget(i_ctx_t *i_ctx_p)
155 {
156 int code;
157 os_ptr op = osp;
158 os_ptr op1 = op - 1;
159 ref *pvalue;
160
161 switch (r_type(op1)) {
162 case t_dictionary:
163 check_dict_read(*op1);
164 if (dict_find(op1, op, &pvalue) <= 0)
165 return_error(e_undefined);
166 op[-1] = *pvalue;
167 break;
168 case t_string:
169 check_read(*op1);
170 check_int_ltu(*op, r_size(op1));
171 make_int(op1, op1->value.bytes[(uint) op->value.intval]);
172 break;
173 case t_array:
174 case t_mixedarray:
175 case t_shortarray:
176 check_type(*op, t_integer);
177 check_read(*op1);
178 code = array_get(imemory, op1, op->value.intval, op1);
179 if (code < 0)
180 return code;
181 break;
182 case t__invalid:
183 return_error(e_stackunderflow);
184 default:
185 return_error(e_typecheck);
186 }
187 pop(1);
188 return 0;
189 }
190
191 /* <array> <index> <obj> put - */
192 /* <dict> <key> <value> put - */
193 /* <string> <index> <int> put - */
194 static int
zput(i_ctx_t * i_ctx_p)195 zput(i_ctx_t *i_ctx_p)
196 {
197 os_ptr op = osp;
198 os_ptr op1 = op - 1;
199 os_ptr op2 = op1 - 1;
200 byte *sdata;
201 uint ssize;
202
203 switch (r_type(op2)) {
204 case t_dictionary:
205 if (i_ctx_p->in_superexec == 0)
206 check_dict_write(*op2);
207 {
208 int code = idict_put(op2, op1, op);
209
210 if (code < 0)
211 return code; /* error */
212 }
213 break;
214 case t_array:
215 check_write(*op2);
216 check_int_ltu(*op1, r_size(op2));
217 store_check_dest(op2, op);
218 {
219 ref *eltp = op2->value.refs + (uint) op1->value.intval;
220
221 ref_assign_old(op2, eltp, op, "put");
222 }
223 break;
224 case t_mixedarray: /* packed arrays are read-only */
225 case t_shortarray:
226 return_error(e_invalidaccess);
227 case t_string:
228 sdata = op2->value.bytes;
229 ssize = r_size(op2);
230 str: check_write(*op2);
231 check_int_ltu(*op1, ssize);
232 check_int_leu(*op, 0xff);
233 sdata[(uint)op1->value.intval] = (byte)op->value.intval;
234 break;
235 case t_astruct:
236 if (gs_object_type(imemory, op2->value.pstruct) != &st_bytes)
237 return_error(e_typecheck);
238 sdata = r_ptr(op2, byte);
239 ssize = gs_object_size(imemory, op2->value.pstruct);
240 goto str;
241 default:
242 return_op_typecheck(op2);
243 }
244 pop(3);
245 return 0;
246 }
247
248 /* <array> <index> <obj> .forceput - */
249 /* <dict> <key> <value> .forceput - */
250 /*
251 * This forces a "put" even if the object is not writable, and (if the
252 * object is systemdict or the save level is 0) even if the value is in
253 * local VM. It is meant to be used only for replacing the value of
254 * FontDirectory in systemdict when switching between local and global VM,
255 * and a few similar applications. After initialization, this operator
256 * should no longer be accessible by name.
257 */
258 static int
zforceput(i_ctx_t * i_ctx_p)259 zforceput(i_ctx_t *i_ctx_p)
260 {
261 os_ptr op = osp;
262 os_ptr op1 = op - 1;
263 os_ptr op2 = op - 2;
264 int code;
265
266 switch (r_type(op2)) {
267 case t_array:
268 check_int_ltu(*op1, r_size(op2));
269 if (r_space(op2) > r_space(op)) {
270 if (imemory_save_level(iimemory))
271 return_error(e_invalidaccess);
272 }
273 {
274 ref *eltp = op2->value.refs + (uint) op1->value.intval;
275
276 ref_assign_old(op2, eltp, op, "put");
277 }
278 break;
279 case t_dictionary:
280 if (op2->value.pdict == systemdict->value.pdict ||
281 !imemory_save_level(iimemory)
282 ) {
283 uint space = r_space(op2);
284
285 r_set_space(op2, avm_local);
286 code = idict_put(op2, op1, op);
287 r_set_space(op2, space);
288 } else
289 code = idict_put(op2, op1, op);
290 if (code < 0)
291 return code;
292 break;
293 default:
294 return_error(e_typecheck);
295 }
296 pop(3);
297 return 0;
298 }
299
300 /* <seq:array|packedarray|string> <index> <count> getinterval <subseq> */
301 static int
zgetinterval(i_ctx_t * i_ctx_p)302 zgetinterval(i_ctx_t *i_ctx_p)
303 {
304 os_ptr op = osp;
305 os_ptr op1 = op - 1;
306 os_ptr op2 = op1 - 1;
307 uint index;
308 uint count;
309
310 switch (r_type(op2)) {
311 default:
312 return_op_typecheck(op2);
313 case t_array:
314 case t_string:
315 case t_mixedarray:
316 case t_shortarray:;
317 }
318 check_read(*op2);
319 check_int_leu(*op1, r_size(op2));
320 index = op1->value.intval;
321 check_int_leu(*op, r_size(op2) - index);
322 count = op->value.intval;
323 switch (r_type(op2)) {
324 case t_array:
325 op2->value.refs += index;
326 break;
327 case t_string:
328 op2->value.bytes += index;
329 break;
330 case t_mixedarray: {
331 const ref_packed *packed = op2->value.packed;
332
333 for (; index--;)
334 packed = packed_next(packed);
335 op2->value.packed = packed;
336 break;
337 }
338 case t_shortarray:
339 op2->value.packed += index;
340 break;
341 }
342 r_set_size(op2, count);
343 pop(2);
344 return 0;
345 }
346
347 /* <array1> <index> <array2|packedarray2> putinterval - */
348 /* <string1> <index> <string2> putinterval - */
349 /* <bytestring1> <index> <string2> putinterval - */
350 static int
zputinterval(i_ctx_t * i_ctx_p)351 zputinterval(i_ctx_t *i_ctx_p)
352 {
353 os_ptr op = osp;
354 os_ptr opindex = op - 1;
355 os_ptr opto = opindex - 1;
356 int code;
357
358 switch (r_type(opto)) {
359 default:
360 return_error(e_typecheck);
361 case t__invalid:
362 if (r_type(op) != t_array && r_type(op) != t_string && r_type(op) != t__invalid)
363 return_error(e_typecheck); /* to match Distiller */
364 else
365 return_error(e_stackunderflow);
366 case t_mixedarray:
367 case t_shortarray:
368 return_error(e_invalidaccess);
369 case t_array:
370 case t_string:
371 check_write(*opto);
372 check_int_leu(*opindex, r_size(opto));
373 code = copy_interval(i_ctx_p, opto, (uint)(opindex->value.intval),
374 op, "putinterval");
375 break;
376 case t_astruct: {
377 uint dsize, ssize, index;
378
379 check_write(*opto);
380 if (gs_object_type(imemory, opto->value.pstruct) != &st_bytes)
381 return_error(e_typecheck);
382 dsize = gs_object_size(imemory, opto->value.pstruct);
383 check_int_leu(*opindex, dsize);
384 index = (uint)opindex->value.intval;
385 check_read_type(*op, t_string);
386 ssize = r_size(op);
387 if (ssize > dsize - index)
388 return_error(e_rangecheck);
389 memcpy(r_ptr(opto, byte) + index, op->value.const_bytes, ssize);
390 code = 0;
391 break;
392 }
393 }
394 if (code >= 0)
395 pop(3);
396 return code;
397 }
398
399 /* <array|packedarray|string> <<element> proc> forall - */
400 /* <dict> <<key> <value> proc> forall - */
401 static int
402 array_continue(i_ctx_t *),
403 dict_continue(i_ctx_t *),
404 string_continue(i_ctx_t *),
405 packedarray_continue(i_ctx_t *);
406 static int forall_cleanup(i_ctx_t *);
407 static int
zforall(i_ctx_t * i_ctx_p)408 zforall(i_ctx_t *i_ctx_p)
409 {
410 os_ptr op = osp;
411 os_ptr obj = op - 1;
412 es_ptr ep = esp;
413 es_ptr cproc = ep + 4;
414
415 check_estack(6);
416 check_proc(*op);
417 switch (r_type(obj)) {
418 default:
419 return_op_typecheck(obj);
420 case t_array:
421 check_read(*obj);
422 make_op_estack(cproc, array_continue);
423 break;
424 case t_dictionary:
425 check_dict_read(*obj);
426 make_int(cproc, dict_first(obj));
427 ++cproc;
428 make_op_estack(cproc, dict_continue);
429 break;
430 case t_string:
431 check_read(*obj);
432 make_op_estack(cproc, string_continue);
433 break;
434 case t_mixedarray:
435 case t_shortarray:
436 check_read(*obj);
437 make_op_estack(cproc, packedarray_continue);
438 break;
439 }
440 /*
441 * Push:
442 * - a mark;
443 * - the composite object;
444 * - the procedure;
445 * - the iteration index (only for dictionaries, done above);
446 * and invoke the continuation operator.
447 */
448 make_mark_estack(ep + 1, es_for, forall_cleanup);
449 ep[2] = *obj;
450 ep[3] = *op;
451 esp = cproc - 1;
452 pop(2);
453 return (*real_opproc(cproc))(i_ctx_p);
454 }
455 /* Continuation operator for arrays */
456 static int
array_continue(i_ctx_t * i_ctx_p)457 array_continue(i_ctx_t *i_ctx_p)
458 {
459 os_ptr op = osp;
460 es_ptr obj = esp - 1;
461
462 if (r_size(obj)) { /* continue */
463 push(1);
464 r_dec_size(obj, 1);
465 *op = *obj->value.refs;
466 obj->value.refs++;
467 esp += 2;
468 *esp = obj[1];
469 return o_push_estack;
470 } else { /* done */
471 esp -= 3; /* pop mark, object, proc */
472 return o_pop_estack;
473 }
474 }
475 /* Continuation operator for dictionaries */
476 static int
dict_continue(i_ctx_t * i_ctx_p)477 dict_continue(i_ctx_t *i_ctx_p)
478 {
479 os_ptr op = osp;
480 es_ptr obj = esp - 2;
481 int index = esp->value.intval;
482
483 push(2); /* make room for key and value */
484 if ((index = dict_next(obj, index, op - 1)) >= 0) { /* continue */
485 esp->value.intval = index;
486 esp += 2;
487 *esp = obj[1];
488 return o_push_estack;
489 } else { /* done */
490 pop(2); /* undo push */
491 esp -= 4; /* pop mark, object, proc, index */
492 return o_pop_estack;
493 }
494 }
495 /* Continuation operator for strings */
496 static int
string_continue(i_ctx_t * i_ctx_p)497 string_continue(i_ctx_t *i_ctx_p)
498 {
499 os_ptr op = osp;
500 es_ptr obj = esp - 1;
501
502 if (r_size(obj)) { /* continue */
503 r_dec_size(obj, 1);
504 push(1);
505 make_int(op, *obj->value.bytes);
506 obj->value.bytes++;
507 esp += 2;
508 *esp = obj[1];
509 return o_push_estack;
510 } else { /* done */
511 esp -= 3; /* pop mark, object, proc */
512 return o_pop_estack;
513 }
514 }
515 /* Continuation operator for packed arrays */
516 static int
packedarray_continue(i_ctx_t * i_ctx_p)517 packedarray_continue(i_ctx_t *i_ctx_p)
518 {
519 os_ptr op = osp;
520 es_ptr obj = esp - 1;
521
522 if (r_size(obj)) { /* continue */
523 const ref_packed *packed = obj->value.packed;
524
525 r_dec_size(obj, 1);
526 push(1);
527 packed_get(imemory, packed, op);
528 obj->value.packed = packed_next(packed);
529 esp += 2;
530 *esp = obj[1];
531 return o_push_estack;
532 } else { /* done */
533 esp -= 3; /* pop mark, object, proc */
534 return o_pop_estack;
535 }
536 }
537 /* Vacuous cleanup procedure */
538 static int
forall_cleanup(i_ctx_t * i_ctx_p)539 forall_cleanup(i_ctx_t *i_ctx_p)
540 {
541 return 0;
542 }
543
544 /* ------ Initialization procedure ------ */
545
546 const op_def zgeneric_op_defs[] =
547 {
548 {"1copy", zcopy},
549 {"2forall", zforall},
550 {"3.forceput", zforceput},
551 {"2get", zget},
552 {"3getinterval", zgetinterval},
553 {"1length", zlength},
554 {"3put", zput},
555 {"3putinterval", zputinterval},
556 /* Internal operators */
557 {"0%array_continue", array_continue},
558 {"0%dict_continue", dict_continue},
559 {"0%packedarray_continue", packedarray_continue},
560 {"0%string_continue", string_continue},
561 op_def_end(0)
562 };
563
564 /* ------ Shared routines ------ */
565
566 /* Copy an interval from one operand to another. */
567 /* This is used by both putinterval and string/array copy. */
568 /* The destination is known to be an array or string, */
569 /* and the starting index is known to be less than or equal to */
570 /* its length; nothing else has been checked. */
571 static int
copy_interval(i_ctx_t * i_ctx_p,os_ptr prto,uint index,os_ptr prfrom,client_name_t cname)572 copy_interval(i_ctx_t *i_ctx_p /* for ref_assign_old */, os_ptr prto,
573 uint index, os_ptr prfrom, client_name_t cname)
574 {
575 int fromtype = r_type(prfrom);
576 uint fromsize = r_size(prfrom);
577
578 if (!(fromtype == r_type(prto) ||
579 ((fromtype == t_shortarray || fromtype == t_mixedarray) &&
580 r_type(prto) == t_array))
581 )
582 return_op_typecheck(prfrom);
583 check_read(*prfrom);
584 check_write(*prto);
585 if (fromsize > r_size(prto) - index)
586 return_error(e_rangecheck);
587 switch (fromtype) {
588 case t_array:
589 { /* We have to worry about aliasing, */
590 /* but refcpy_to_old takes care of it for us. */
591 return refcpy_to_old(prto, index, prfrom->value.refs,
592 fromsize, idmemory, cname);
593 }
594 case t_string:
595 { /* memmove takes care of aliasing. */
596 memmove(prto->value.bytes + index, prfrom->value.bytes,
597 fromsize);
598 }
599 break;
600 case t_mixedarray:
601 case t_shortarray:
602 { /* We don't have to worry about aliasing, because */
603 /* packed arrays are read-only and hence the destination */
604 /* can't be a packed array. */
605 uint i;
606 const ref_packed *packed = prfrom->value.packed;
607 ref *pdest = prto->value.refs + index;
608 ref elt;
609
610 for (i = 0; i < fromsize; i++, pdest++) {
611 packed_get(imemory, packed, &elt);
612 ref_assign_old(prto, pdest, &elt, cname);
613 packed = packed_next(packed);
614 }
615 }
616 break;
617 }
618 return 0;
619 }
620