idparam.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* Copyright (C) 1992, 1995, 1997, 1998 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: idparam.c,v 1.9 2004/08/04 19:36:12 stefan Exp $ */
  14. /* Utilities for getting parameters out of dictionaries. */
  15. #include "memory_.h"
  16. #include "string_.h" /* for strlen */
  17. #include "ghost.h"
  18. #include "ierrors.h"
  19. #include "gsmatrix.h" /* for dict_matrix_param */
  20. #include "gsuid.h"
  21. #include "idict.h"
  22. #include "idparam.h" /* interface definition */
  23. #include "ilevel.h"
  24. #include "imemory.h" /* for iutil.h */
  25. #include "iname.h"
  26. #include "iutil.h"
  27. #include "oper.h" /* for check_proc */
  28. #include "store.h" /* for making empty proc */
  29. /* Get a Boolean parameter from a dictionary. */
  30. /* Return 0 if found, 1 if defaulted, <0 if wrong type. */
  31. int
  32. dict_bool_param(const ref * pdict, const char *kstr,
  33. bool defaultval, bool * pvalue)
  34. {
  35. ref *pdval;
  36. if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
  37. *pvalue = defaultval;
  38. return 1;
  39. }
  40. if (!r_has_type(pdval, t_boolean))
  41. return_error(e_typecheck);
  42. *pvalue = pdval->value.boolval;
  43. return 0;
  44. }
  45. /* Get an integer or null parameter from a dictionary. */
  46. /* Return 0 if found, 1 if defaulted, <0 if invalid. */
  47. /* If the parameter is null, return 2 without setting *pvalue. */
  48. /* Note that the default value may be out of range, in which case */
  49. /* a missing value will return e_rangecheck rather than 1. */
  50. int
  51. dict_int_null_param(const ref * pdict, const char *kstr, int minval,
  52. int maxval, int defaultval, int *pvalue)
  53. {
  54. ref *pdval;
  55. int code;
  56. long ival;
  57. if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
  58. ival = defaultval;
  59. code = 1;
  60. } else {
  61. switch (r_type(pdval)) {
  62. case t_integer:
  63. ival = pdval->value.intval;
  64. break;
  65. case t_real:
  66. /* Allow an integral real, because Fontographer */
  67. /* (which violates the Adobe specs in other ways */
  68. /* as well) sometimes generates output that */
  69. /* needs this. */
  70. if (pdval->value.realval < minval || pdval->value.realval > maxval)
  71. return_error(e_rangecheck);
  72. ival = (long)pdval->value.realval;
  73. if (ival != pdval->value.realval)
  74. return_error(e_rangecheck);
  75. break;
  76. case t_null:
  77. return 2;
  78. default:
  79. return_error(e_typecheck);
  80. }
  81. code = 0;
  82. }
  83. if (ival < minval || ival > maxval)
  84. return_error(e_rangecheck);
  85. *pvalue = (int)ival;
  86. return code;
  87. }
  88. /* Get an integer parameter from a dictionary. */
  89. /* Return like dict_int_null_param, but return e_typecheck for null. */
  90. int
  91. dict_int_param(const ref * pdict, const char *kstr, int minval, int maxval,
  92. int defaultval, int *pvalue)
  93. {
  94. int code = dict_int_null_param(pdict, kstr, minval, maxval,
  95. defaultval, pvalue);
  96. return (code == 2 ? gs_note_error(e_typecheck) : code);
  97. }
  98. /* Get an unsigned integer parameter from a dictionary. */
  99. /* Return 0 if found, 1 if defaulted, <0 if invalid. */
  100. /* Note that the default value may be out of range, in which case */
  101. /* a missing value will return e_rangecheck rather than 1. */
  102. int
  103. dict_uint_param(const ref * pdict, const char *kstr,
  104. uint minval, uint maxval, uint defaultval, uint * pvalue)
  105. {
  106. ref *pdval;
  107. int code;
  108. uint ival;
  109. if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
  110. ival = defaultval;
  111. code = 1;
  112. } else {
  113. check_type_only(*pdval, t_integer);
  114. if (pdval->value.intval != (uint) pdval->value.intval)
  115. return_error(e_rangecheck);
  116. ival = (uint) pdval->value.intval;
  117. code = 0;
  118. }
  119. if (ival < minval || ival > maxval)
  120. return_error(e_rangecheck);
  121. *pvalue = ival;
  122. return code;
  123. }
  124. /* Get a float parameter from a dictionary. */
  125. /* Return 0 if found, 1 if defaulted, <0 if wrong type. */
  126. int
  127. dict_float_param(const ref * pdict, const char *kstr,
  128. floatp defaultval, float *pvalue)
  129. {
  130. ref *pdval;
  131. if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
  132. *pvalue = defaultval;
  133. return 1;
  134. }
  135. switch (r_type(pdval)) {
  136. case t_integer:
  137. *pvalue = (float)pdval->value.intval;
  138. return 0;
  139. case t_real:
  140. *pvalue = pdval->value.realval;
  141. return 0;
  142. }
  143. return_error(e_typecheck);
  144. }
  145. /* Get an integer array from a dictionary. */
  146. /* See idparam.h for specification. */
  147. int
  148. dict_int_array_check_param(const ref * pdict, const char *kstr, uint len,
  149. int *ivec, int under_error, int over_error)
  150. {
  151. ref *pdval;
  152. const ref *pa;
  153. int *pi = ivec;
  154. uint size;
  155. int i;
  156. if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0)
  157. return 0;
  158. if (!r_has_type(pdval, t_array))
  159. return_error(e_typecheck);
  160. size = r_size(pdval);
  161. if (size > len)
  162. return_error(over_error);
  163. pa = pdval->value.const_refs;
  164. for (i = 0; i < size; i++, pa++, pi++) {
  165. /* See dict_int_param above for why we allow reals here. */
  166. switch (r_type(pa)) {
  167. case t_integer:
  168. if (pa->value.intval != (int)pa->value.intval)
  169. return_error(e_rangecheck);
  170. *pi = (int)pa->value.intval;
  171. break;
  172. case t_real:
  173. if (pa->value.realval < min_int ||
  174. pa->value.realval > max_int ||
  175. pa->value.realval != (int)pa->value.realval
  176. )
  177. return_error(e_rangecheck);
  178. *pi = (int)pa->value.realval;
  179. break;
  180. default:
  181. return_error(e_typecheck);
  182. }
  183. }
  184. return (size == len || under_error >= 0 ? size :
  185. gs_note_error(under_error));
  186. }
  187. int
  188. dict_int_array_param(const ref * pdict, const char *kstr,
  189. uint maxlen, int *ivec)
  190. {
  191. return dict_int_array_check_param(pdict, kstr, maxlen, ivec,
  192. 0, e_limitcheck);
  193. }
  194. int
  195. dict_ints_param(const ref * pdict, const char *kstr,
  196. uint len, int *ivec)
  197. {
  198. return dict_int_array_check_param(pdict, kstr, len, ivec,
  199. e_rangecheck, e_rangecheck);
  200. }
  201. /* Get a float array from a dictionary. */
  202. /* Return the element count if OK, <0 if invalid. */
  203. /* If the parameter is missing, then if defaultvec is NULL, return 0; */
  204. /* if defaultvec is not NULL, copy it into fvec (maxlen elements) */
  205. /* and return maxlen. */
  206. int
  207. dict_float_array_check_param(const gs_memory_t *mem,
  208. const ref * pdict, const char *kstr,
  209. uint len, float *fvec, const float *defaultvec,
  210. int under_error, int over_error)
  211. {
  212. ref *pdval;
  213. uint size;
  214. int code;
  215. if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
  216. if (defaultvec == NULL)
  217. return 0;
  218. memcpy(fvec, defaultvec, len * sizeof(float));
  219. return len;
  220. }
  221. if (!r_is_array(pdval))
  222. return_error(e_typecheck);
  223. size = r_size(pdval);
  224. if (size > len)
  225. return_error(over_error);
  226. code = process_float_array(mem, pdval, size, fvec);
  227. return (code < 0 ? code :
  228. size == len || under_error >= 0 ? size :
  229. gs_note_error(under_error));
  230. }
  231. int
  232. dict_float_array_param(const gs_memory_t *mem,
  233. const ref * pdict, const char *kstr,
  234. uint maxlen, float *fvec, const float *defaultvec)
  235. {
  236. return dict_float_array_check_param(mem ,pdict, kstr, maxlen, fvec,
  237. defaultvec, 0, e_limitcheck);
  238. }
  239. int
  240. dict_floats_param(const gs_memory_t *mem,
  241. const ref * pdict, const char *kstr,
  242. uint maxlen, float *fvec, const float *defaultvec)
  243. {
  244. return dict_float_array_check_param(mem, pdict, kstr, maxlen,
  245. fvec, defaultvec,
  246. e_rangecheck, e_rangecheck);
  247. }
  248. /*
  249. * Get a procedure from a dictionary. If the key is missing,
  250. * defaultval = false means substitute t__invalid;
  251. * defaultval = true means substitute an empty procedure.
  252. * In either case, return 1.
  253. */
  254. int
  255. dict_proc_param(const ref * pdict, const char *kstr, ref * pproc,
  256. bool defaultval)
  257. {
  258. ref *pdval;
  259. if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0) {
  260. if (defaultval)
  261. make_empty_const_array(pproc, a_readonly + a_executable);
  262. else
  263. make_t(pproc, t__invalid);
  264. return 1;
  265. }
  266. check_proc(*pdval);
  267. *pproc = *pdval;
  268. return 0;
  269. }
  270. /* Get a matrix from a dictionary. */
  271. int
  272. dict_matrix_param(const gs_memory_t *mem, const ref * pdict, const char *kstr, gs_matrix * pmat)
  273. {
  274. ref *pdval;
  275. if (pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0)
  276. return_error(e_typecheck);
  277. return read_matrix(mem, pdval, pmat);
  278. }
  279. /* Get a UniqueID or XUID from a dictionary. */
  280. /* Return 0 if UniqueID, 1 if XUID, <0 if error. */
  281. /* If there is no uid, return default. */
  282. int
  283. dict_uid_param(const ref * pdict, gs_uid * puid, int defaultval,
  284. gs_memory_t * mem, const i_ctx_t *i_ctx_p)
  285. {
  286. ref *puniqueid;
  287. if (pdict == 0) {
  288. uid_set_invalid(puid);
  289. return defaultval;
  290. }
  291. /* In a Level 2 environment, check for XUID first. */
  292. if (level2_enabled &&
  293. dict_find_string(pdict, "XUID", &puniqueid) > 0
  294. ) {
  295. long *xvalues;
  296. uint size, i;
  297. if (!r_has_type(puniqueid, t_array))
  298. return_error(e_typecheck);
  299. size = r_size(puniqueid);
  300. if (size == 0)
  301. return_error(e_rangecheck);
  302. xvalues = (long *)gs_alloc_byte_array(mem, size, sizeof(long),
  303. "get XUID");
  304. if (xvalues == 0)
  305. return_error(e_VMerror);
  306. /* Get the values from the XUID array. */
  307. for (i = 0; i < size; i++) {
  308. const ref *pvalue = puniqueid->value.const_refs + i;
  309. if (!r_has_type(pvalue, t_integer)) {
  310. gs_free_object(mem, xvalues, "get XUID");
  311. return_error(e_typecheck);
  312. }
  313. xvalues[i] = pvalue->value.intval;
  314. }
  315. uid_set_XUID(puid, xvalues, size);
  316. return 1;
  317. }
  318. /* If no UniqueID entry, set the UID to invalid, */
  319. /* because UniqueID need not be present in all fonts, */
  320. /* and if it is, the legal range is 0 to 2^24-1. */
  321. if (dict_find_string(pdict, "UniqueID", &puniqueid) <= 0) {
  322. uid_set_invalid(puid);
  323. return defaultval;
  324. } else {
  325. if (!r_has_type(puniqueid, t_integer) ||
  326. puniqueid->value.intval < 0 ||
  327. puniqueid->value.intval > 0xffffffL
  328. )
  329. return_error(e_rangecheck);
  330. /* Apparently fonts created by Fontographer often have */
  331. /* a UniqueID of 0, contrary to Adobe's specifications. */
  332. /* Treat 0 as equivalent to -1 (no UniqueID). */
  333. if (puniqueid->value.intval == 0) {
  334. uid_set_invalid(puid);
  335. return defaultval;
  336. } else
  337. uid_set_UniqueID(puid, puniqueid->value.intval);
  338. }
  339. return 0;
  340. }
  341. /* Check that a UID in a dictionary is equal to an existing, valid UID. */
  342. bool
  343. dict_check_uid_param(const ref * pdict, const gs_uid * puid)
  344. {
  345. ref *puniqueid;
  346. if (uid_is_XUID(puid)) {
  347. uint size = uid_XUID_size(puid);
  348. uint i;
  349. if (dict_find_string(pdict, "XUID", &puniqueid) <= 0)
  350. return false;
  351. if (!r_has_type(puniqueid, t_array) ||
  352. r_size(puniqueid) != size
  353. )
  354. return false;
  355. for (i = 0; i < size; i++) {
  356. const ref *pvalue = puniqueid->value.const_refs + i;
  357. if (!r_has_type(pvalue, t_integer))
  358. return false;
  359. if (pvalue->value.intval != uid_XUID_values(puid)[i])
  360. return false;
  361. }
  362. return true;
  363. } else {
  364. if (dict_find_string(pdict, "UniqueID", &puniqueid) <= 0)
  365. return false;
  366. return (r_has_type(puniqueid, t_integer) &&
  367. puniqueid->value.intval == puid->id);
  368. }
  369. }