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