zfunc4.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* Copyright (C) 1999, 2000 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: zfunc4.c,v 1.7.4.1 2002/01/17 02:59:35 dancoby Exp $ */
  16. /* PostScript language support for FunctionType 4 (PS Calculator) Functions */
  17. #include "memory_.h"
  18. #include "ghost.h"
  19. #include "oper.h"
  20. #include "opextern.h"
  21. #include "gsfunc.h"
  22. #include "gsfunc4.h"
  23. #include "gsutil.h"
  24. #include "idict.h"
  25. #include "ifunc.h"
  26. #include "iname.h"
  27. #include "dstack.h"
  28. /*
  29. * FunctionType 4 functions are not defined in the PostScript language. We
  30. * provide support for them because they are needed for PDF 1.3. In
  31. * addition to the standard FunctionType, Domain, and Range keys, they have
  32. * a Function key whose value is a procedure in a restricted subset of the
  33. * PostScript language. Specifically, the procedure must (recursively)
  34. * contain only integer, real, Boolean, and procedure constants (only as
  35. * literal operands of if and and ifelse), and operators chosen from the set
  36. * given below. Note that names other than true and false are not allowed:
  37. * the procedure must be 'bound'.
  38. *
  39. * The following list is taken directly from the PDF 1.3 documentation.
  40. */
  41. #define XOP(zfn) int zfn(P1(i_ctx_t *))
  42. XOP(zabs); XOP(zand); XOP(zatan); XOP(zbitshift);
  43. XOP(zceiling); XOP(zcos); XOP(zcvi); XOP(zcvr);
  44. XOP(zdiv); XOP(zexp); XOP(zfloor); XOP(zidiv);
  45. XOP(zln); XOP(zlog); XOP(zmod); XOP(zmul);
  46. XOP(zneg); XOP(znot); XOP(zor); XOP(zround);
  47. XOP(zsin); XOP(zsqrt); XOP(ztruncate); XOP(zxor);
  48. XOP(zeq); XOP(zge); XOP(zgt); XOP(zle); XOP(zlt); XOP(zne);
  49. XOP(z2copy);
  50. #undef XOP
  51. typedef struct calc_op_s {
  52. op_proc_t proc;
  53. gs_PtCr_opcode_t opcode;
  54. } calc_op_t;
  55. static const calc_op_t calc_ops[] = {
  56. /* Arithmetic operators */
  57. {zabs, PtCr_abs},
  58. {zadd, PtCr_add},
  59. {zand, PtCr_and},
  60. {zatan, PtCr_atan},
  61. {zbitshift, PtCr_bitshift},
  62. {zceiling, PtCr_ceiling},
  63. {zcos, PtCr_cos},
  64. {zcvi, PtCr_cvi},
  65. {zcvr, PtCr_cvr},
  66. {zdiv, PtCr_div},
  67. {zexp, PtCr_exp},
  68. {zfloor, PtCr_floor},
  69. {zidiv, PtCr_idiv},
  70. {zln, PtCr_ln},
  71. {zlog, PtCr_log},
  72. {zmod, PtCr_mod},
  73. {zmul, PtCr_mul},
  74. {zneg, PtCr_neg},
  75. {znot, PtCr_not},
  76. {zor, PtCr_or},
  77. {zround, PtCr_round},
  78. {zsin, PtCr_sin},
  79. {zsqrt, PtCr_sqrt},
  80. {zsub, PtCr_sub},
  81. {ztruncate, PtCr_truncate},
  82. {zxor, PtCr_xor},
  83. /* Comparison operators */
  84. {zeq, PtCr_eq},
  85. {zge, PtCr_ge},
  86. {zgt, PtCr_gt},
  87. {zle, PtCr_le},
  88. {zlt, PtCr_lt},
  89. {zne, PtCr_ne},
  90. /* Stack operators */
  91. {zcopy, PtCr_copy},
  92. {z2copy, PtCr_copy},
  93. {zdup, PtCr_dup},
  94. {zexch, PtCr_exch},
  95. {zindex, PtCr_index},
  96. {zpop, PtCr_pop},
  97. {zroll, PtCr_roll}
  98. /* Special operators */
  99. /*{zif, PtCr_if},*/
  100. /*{zifelse, PtCr_ifelse},*/
  101. /*{ztrue, PtCr_true},*/
  102. /*{zfalse, PtCr_false}*/
  103. };
  104. /* Fix up an if or ifelse forward reference. */
  105. private void
  106. psc_fixup(byte *p, byte *to)
  107. {
  108. int skip = to - (p + 3);
  109. p[1] = (byte)(skip >> 8);
  110. p[2] = (byte)skip;
  111. }
  112. /*
  113. * Check a calculator function for validity, optionally storing its encoded
  114. * representation and add the size of the encoded representation to *psize.
  115. * Note that we arbitrarily limit the depth of procedure nesting. pref is
  116. * known to be a procedure.
  117. */
  118. #define MAX_PSC_FUNCTION_NESTING 10
  119. private int
  120. check_psc_function(i_ctx_t *i_ctx_p, const ref *pref, int depth, byte *ops, int *psize)
  121. {
  122. long i;
  123. uint size = r_size(pref);
  124. for (i = 0; i < size; ++i) {
  125. byte no_ops[1 + max(sizeof(int), sizeof(float))];
  126. byte *p = (ops ? ops + *psize : no_ops);
  127. ref elt, elt2, elt3;
  128. ref * delp;
  129. int code;
  130. array_get(pref, i, &elt);
  131. switch (r_btype(&elt)) {
  132. case t_integer: {
  133. int i = elt.value.intval;
  134. #if ARCH_SIZEOF_INT < ARCH_SIZEOF_LONG
  135. if (i != elt.value.intval) /* check for truncation */
  136. return_error(e_rangecheck);
  137. #endif
  138. if (i == (byte)i) {
  139. *p = PtCr_byte;
  140. p[1] = (byte)i;
  141. *psize += 2;
  142. } else {
  143. *p = PtCr_int;
  144. memcpy(p + 1, &i, sizeof(i));
  145. *psize += 1 + sizeof(int);
  146. }
  147. break;
  148. }
  149. case t_real: {
  150. float f = elt.value.realval;
  151. *p = PtCr_float;
  152. memcpy(p + 1, &f, sizeof(f));
  153. *psize += 1 + sizeof(float);
  154. break;
  155. }
  156. case t_boolean:
  157. *p = (elt.value.boolval ? PtCr_true : PtCr_false);
  158. ++*psize;
  159. break;
  160. case t_name:
  161. if (!r_has_attr(&elt, a_executable))
  162. return_error(e_rangecheck);
  163. name_string_ref(&elt, &elt);
  164. if (!bytes_compare(elt.value.bytes, r_size(&elt),
  165. (const byte *)"true", 4)) {
  166. *p = PtCr_true;
  167. ++*psize;
  168. break;
  169. }
  170. if (!bytes_compare(elt.value.bytes, r_size(&elt),
  171. (const byte *)"false", 5)) {
  172. *p = PtCr_false;
  173. ++*psize;
  174. break;
  175. }
  176. /* Check if the name is a valid operator in systemdict */
  177. if (dict_find(systemdict, &elt, &delp) <= 0)
  178. return_error(e_undefined);
  179. if (r_btype(delp) != t_operator)
  180. return_error(e_typecheck);
  181. if (!r_has_attr(delp, a_executable))
  182. return_error(e_rangecheck);
  183. elt = *delp;
  184. /* Fall into the operator case */
  185. case t_operator: {
  186. int j;
  187. for (j = 0; j < countof(calc_ops); ++j)
  188. if (elt.value.opproc == calc_ops[j].proc) {
  189. *p = calc_ops[j].opcode;
  190. ++*psize;
  191. goto next;
  192. }
  193. return_error(e_rangecheck);
  194. }
  195. default: {
  196. if (!r_is_proc(&elt))
  197. return_error(e_typecheck);
  198. if (depth == MAX_PSC_FUNCTION_NESTING)
  199. return_error(e_limitcheck);
  200. if ((code = array_get(pref, ++i, &elt2)) < 0)
  201. return code;
  202. *psize += 3;
  203. code = check_psc_function(i_ctx_p, &elt, depth + 1, ops, psize);
  204. if (code < 0)
  205. return code;
  206. /* Check for {proc} if | {proc1} {proc2} ifelse */
  207. #define R_IS_OPER(pref, proc)\
  208. (r_btype(pref) == t_operator && r_has_attr(pref, a_executable) &&\
  209. (pref)->value.opproc == proc)
  210. if (R_IS_OPER(&elt2, zif)) {
  211. if (ops) {
  212. *p = PtCr_if;
  213. psc_fixup(p, ops + *psize);
  214. }
  215. } else if (!r_is_proc(&elt2))
  216. return_error(e_rangecheck);
  217. else if ((code == array_get(pref, ++i, &elt3)) < 0)
  218. return code;
  219. else if (R_IS_OPER(&elt3, zifelse)) {
  220. if (ops) {
  221. *p = PtCr_if;
  222. psc_fixup(p, ops + *psize + 3);
  223. p = ops + *psize;
  224. *p = PtCr_else;
  225. }
  226. *psize += 3;
  227. code = check_psc_function(i_ctx_p, &elt2, depth + 1, ops, psize);
  228. if (code < 0)
  229. return code;
  230. if (ops)
  231. psc_fixup(p, ops + *psize);
  232. } else
  233. return_error(e_rangecheck);
  234. #undef R_IS_OPER
  235. }
  236. }
  237. next:
  238. DO_NOTHING;
  239. }
  240. return 0;
  241. }
  242. #undef MAX_PSC_FUNCTION_NESTING
  243. /* Check prototype */
  244. build_function_proc(gs_build_function_4);
  245. /* Finish building a FunctionType 4 (PostScript Calculator) function. */
  246. int
  247. gs_build_function_4(i_ctx_t *i_ctx_p, const ref *op, const gs_function_params_t * mnDR,
  248. int depth, gs_function_t ** ppfn, gs_memory_t *mem)
  249. {
  250. gs_function_PtCr_params_t params;
  251. ref *proc;
  252. int code;
  253. byte *ops;
  254. int size;
  255. *(gs_function_params_t *)&params = *mnDR;
  256. params.ops.data = 0; /* in case of failure */
  257. params.ops.size = 0; /* ditto */
  258. if (dict_find_string(op, "Function", &proc) <= 0) {
  259. code = gs_note_error(e_rangecheck);
  260. goto fail;
  261. }
  262. if (!r_is_proc(proc)) {
  263. code = gs_note_error(e_typecheck);
  264. goto fail;
  265. }
  266. size = 0;
  267. code = check_psc_function(i_ctx_p, proc, 0, NULL, &size);
  268. if (code < 0)
  269. goto fail;
  270. ops = gs_alloc_string(mem, size + 1, "gs_build_function_4(ops)");
  271. if (ops == 0) {
  272. code = gs_note_error(e_VMerror);
  273. goto fail;
  274. }
  275. size = 0;
  276. check_psc_function(i_ctx_p, proc, 0, ops, &size); /* can't fail */
  277. ops[size] = PtCr_return;
  278. params.ops.data = ops;
  279. params.ops.size = size + 1;
  280. code = gs_function_PtCr_init(ppfn, &params, mem);
  281. if (code >= 0)
  282. return 0;
  283. /* free_params will free the ops string */
  284. fail:
  285. gs_function_PtCr_free_params(&params, mem);
  286. return (code < 0 ? code : gs_note_error(e_rangecheck));
  287. }