/* Copyright (C) 1989, 1992 Aladdin Enterprises. All rights reserved.
Distributed by Free Software Foundation, Inc.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
/* Array/string/dictionary generic operators for PostScript */
#include "estack.h" /* for forall */
/* This file implements copy, get, put, getinterval, putinterval, */
/* length, and forall, which apply generically to */
/* arrays, strings, and dictionaries. (Copy also has a special */
/* meaning for copying the top N elements of the stack.) */
extern int zcopy_device(P1(os_ptr
));
extern int zcopy_dict(P1(os_ptr
));
int array_get(P3(ref
*, long, ref
*));
private int copy_interval(P4(os_ptr
, uint
, os_ptr
, const char *));
/* Note that this implements copy for arrays and strings, */
/* but not for dictionaries (see zcopy_dict in zdict.c) */
/* or devices (see zcopy_device in zdevice.c). */
zcopy(register os_ptr op
)
if ( (ulong
)op
->value
.intval
> op
- osbot
)
count
= op
->value
.intval
;
if ( op1
+ count
> ostop
)
memcpy((char *)op
, (char *)(op
- count
), count
* sizeof(ref
));
code
= copy_interval(op
, 0, op1
, "copy");
if ( code
) return code
; /* error */
r_set_size(op
, r_size(op1
));
zlength(register os_ptr op
)
make_int(op
, r_size(op
));
make_int(op
, dict_length(op
));
name_string_ref(op
, &str
);
make_int(op
, r_size(&str
));
if ( dict_find(op1
, op
, &pvalue
) <= 0 ) return e_undefined
;
check_type(*op
, t_integer
);
if ( (ulong
)(op
->value
.intval
) >= r_size(op1
) ) {
printf("Zget t_string %u >= %u\n", (ulong
)(op
->value
.intval
), r_size(op1
) );
{ int element
= op1
->value
.bytes
[(uint
)op
->value
.intval
];
check_type(*op
, t_integer
);
code
= array_get(op1
, op
->value
.intval
, op1
);
if ( code
< 0 ) return code
;
{ int code
= dict_put(op2
, op1
, op
);
if ( code
) return code
; /* error */
check_type(*op1
, t_integer
);
if ( (ulong
)(op1
->value
.intval
) >= r_size(op2
) )
ref_assign_old(op2
->value
.refs
+ (uint
)op1
->value
.intval
, op
, "put");
case t_mixedarray
: /* packed arrays are read-only */
check_type(*op1
, t_integer
);
if ( (ulong
)(op1
->value
.intval
) >= r_size(op2
) )
check_type(*op
, t_integer
);
if ( (ulong
)op
->value
.intval
> 0xff )
op2
->value
.bytes
[(uint
)op1
->value
.intval
] = (byte
)op
->value
.intval
;
zgetinterval(register os_ptr op
)
check_type(*op1
, t_integer
);
check_type(*op
, t_integer
);
case t_array
: case t_string
:
if ( (ulong
)op1
->value
.intval
> r_size(op2
) ) return e_rangecheck
;
index
= op1
->value
.intval
;
if ( (ulong
)op
->value
.intval
> r_size(op2
) - index
) return e_rangecheck
;
count
= op
->value
.intval
;
case t_array
: op2
->value
.refs
+= index
; break;
case t_string
: op2
->value
.bytes
+= index
; break;
{ ushort
*packed
= op2
->value
.packed
;
for ( ; index
--; ) packed
= packed_next(packed
);
op2
->value
.packed
= packed
;
case t_shortarray
: op2
->value
.packed
+= index
; break;
zputinterval(register os_ptr op
)
{ os_ptr opindex
= op
- 1;
os_ptr opto
= opindex
- 1;
check_type(*opindex
, t_integer
);
default: return e_typecheck
;
case t_mixedarray
: case t_shortarray
: return e_invalidaccess
;
case t_array
: case t_string
: ;
if ( (ulong
)opindex
->value
.intval
> r_size(opto
) )
code
= copy_interval(opto
, (uint
)(opindex
->value
.intval
), op
, "putinterval");
array_continue(P1(os_ptr
)),
dict_continue(P1(os_ptr
)),
string_continue(P1(os_ptr
)),
packedarray_continue(P1(os_ptr
)),
zforall(register os_ptr op
)
{ int (*cproc
)(P1(os_ptr
));
uint index
= 0; /* only used for dictionaries */
cproc
= packedarray_continue
;
/* Push a mark, the composite object, the iteration index, */
/* and the procedure, and invoke the continuation operator. */
/* Continuation operator for arrays */
array_continue(register os_ptr op
)
if ( r_size(obj
) ) /* continue */
push_op_estack(array_continue
, i_array_continue
); /* push continuation */
{ esp
-= 4; /* pop mark, object, index, proc */
/* Continuation operator for dictionaries */
dict_continue(register os_ptr op
)
int index
= (int)esp
[-1].value
.intval
;
push(2); /* make room for key and value */
if ( (index
= dict_next(obj
, index
, op
- 1)) >= 0 ) /* continue */
{ esp
[-1].value
.intval
= index
;
push_op_estack(dict_continue
, i_dict_continue
); /* push continuation */
{ pop(2); /* undo push */
esp
-= 4; /* pop mark, object, index, proc */
/* Continuation operator for strings */
string_continue(register os_ptr op
)
if ( r_size(obj
) ) /* continue */
make_int(op
, *obj
->value
.bytes
);
push_op_estack(string_continue
, i_string_continue
); /* push continuation */
{ esp
-= 4; /* pop mark, object, index, proc */
/* Continuation operator for packed arrays */
packedarray_continue(register os_ptr op
)
if ( r_size(obj
) ) /* continue */
{ ushort
*packed
= obj
->value
.packed
;
obj
->value
.packed
= packed_next(packed
);
push_op_estack(packedarray_continue
, i_packedarray_continue
); /* push continuation */
{ esp
-= 4; /* pop mark, object, index, proc */
/* ------ Initialization procedure ------ */
op_def zgeneric_op_defs
[] = {
{"3getinterval", zgetinterval
},
{"3putinterval", zputinterval
},
{"0%array_continue", array_continue
, &i_array_continue
},
{"0%dict_continue", dict_continue
, &i_dict_continue
},
{"0%packedarray_continue", packedarray_continue
, &i_packedarray_continue
},
{"0%string_continue", string_continue
, &i_string_continue
},
/* ------ Shared routines ------ */
/* Get an element from an array of some kind. */
/* This is also used to index into Encoding vectors, */
/* the error name vector, etc. */
array_get(ref
*aref
, long index_long
, ref
*pref
)
{ uint index
= (uint
)index_long
;
if ( (ulong
)index_long
>= r_size(aref
) )
{ ref
*pvalue
= aref
->value
.refs
+ index
;
ref_assign(pref
, pvalue
);
{ ushort
*packed
= aref
->value
.packed
;
for ( ; index
--; ) packed
= packed_next(packed
);
packed_get(packed
, pref
);
{ ushort
*packed
= aref
->value
.packed
+ index
;
packed_get(packed
, pref
);
/* Copy an interval from one operand to another. */
/* This is used by both putinterval and string/array copy. */
/* One operand is known to be an array or string, */
/* and the starting index is known to be less than or equal to */
/* its length; nothing else has been checked. */
copy_interval(os_ptr prto
, uint index
, os_ptr prfrom
, const char *cname
)
{ int fromtype
= r_type(prfrom
);
uint fromsize
= r_size(prfrom
);
if ( !(fromtype
== r_type(prto
) || (fromtype
== t_shortarray
|| fromtype
== t_mixedarray
) && r_type(prto
) == t_array
)
if ( fromsize
> r_size(prto
) - index
) return e_rangecheck
;
refcpy_to_old(prto
->value
.refs
+ index
, prfrom
->value
.refs
,
memcpy(prto
->value
.bytes
+ index
, prfrom
->value
.bytes
,
ushort
*packed
= prfrom
->value
.packed
;
ref
*pdest
= prto
->value
.refs
+ index
;
for ( i
= 0; i
< fromsize
; i
++, pdest
++ )
{ packed_get(packed
, &elt
);
ref_assign_old(pdest
, &elt
, cname
);
packed
= packed_next(packed
);