/* Return <0 for error, or a number format. */ int num_array_format(const ref *op) { switch ( r_type(op) ) { case t_string: { /* Check that this is a legitimate encoded number string. */ const byte *bp = op->value.bytes; int format = bp[1]; if ( r_size(op) < 4 || bp[0] != bt_num_array_value || !num_is_valid(format) ) return_error(e_rangecheck); if ( sdecodeshort(bp + 2, format) != (r_size(op) - 4) / encoded_number_bytes(format) ) return_error(e_rangecheck); return format; } case t_array: case t_mixedarray: case t_shortarray: return num_array; default: return_error(e_typecheck); } }
/* or an error if the format is invalid. */ int num_array_get(const gs_memory_t *mem, const ref * op, int format, uint index, ref * np) { if (format == num_array) { int code = array_get(mem, op, (long)index, np); if (code < 0) return t_null; switch (r_type(np)) { case t_integer: return t_integer; case t_real: return t_real; default: return_error(e_typecheck); } } else { uint nbytes = encoded_number_bytes(format); if (index >= (r_size(op) - 4) / nbytes) return t_null; return sdecode_number(op->value.bytes + 4 + index * nbytes, format, np); } }
/* Get the number of elements in an encoded number array/string. */ uint num_array_size(const ref *op, int format) { return (format == num_array ? r_size(op) : (r_size(op) - 4) / encoded_number_bytes(format)); }