zdict.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /* Copyright (C) 1989, 2000 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: zdict.c,v 1.6 2004/08/04 19:36:13 stefan Exp $ */
  14. /* Dictionary operators */
  15. #include "ghost.h"
  16. #include "oper.h"
  17. #include "iddict.h"
  18. #include "dstack.h"
  19. #include "ilevel.h" /* for [count]dictstack */
  20. #include "iname.h" /* for dict_find_name */
  21. #include "ipacked.h" /* for inline dict lookup */
  22. #include "ivmspace.h"
  23. #include "store.h"
  24. /* <int> dict <dict> */
  25. int
  26. zdict(i_ctx_t *i_ctx_p)
  27. {
  28. os_ptr op = osp;
  29. check_type(*op, t_integer);
  30. #if arch_sizeof_int < arch_sizeof_long
  31. check_int_leu(*op, max_uint);
  32. #else
  33. if (op->value.intval < 0)
  34. return_error(e_rangecheck);
  35. #endif
  36. return dict_create((uint) op->value.intval, op);
  37. }
  38. /* <dict> maxlength <int> */
  39. private int
  40. zmaxlength(i_ctx_t *i_ctx_p)
  41. {
  42. os_ptr op = osp;
  43. check_type(*op, t_dictionary);
  44. check_dict_read(*op);
  45. make_int(op, dict_maxlength(op));
  46. return 0;
  47. }
  48. /* <dict> begin - */
  49. int
  50. zbegin(i_ctx_t *i_ctx_p)
  51. {
  52. os_ptr op = osp;
  53. check_type(*op, t_dictionary);
  54. check_dict_read(*op);
  55. if (dsp == dstop)
  56. return_error(e_dictstackoverflow);
  57. ++dsp;
  58. ref_assign(dsp, op);
  59. dict_set_top();
  60. pop(1);
  61. return 0;
  62. }
  63. /* - end - */
  64. int
  65. zend(i_ctx_t *i_ctx_p)
  66. {
  67. if (ref_stack_count_inline(&d_stack) == min_dstack_size) {
  68. /* We would underflow the d-stack. */
  69. return_error(e_dictstackunderflow);
  70. }
  71. while (dsp == dsbot) {
  72. /* We would underflow the current block. */
  73. ref_stack_pop_block(&d_stack);
  74. }
  75. dsp--;
  76. dict_set_top();
  77. return 0;
  78. }
  79. /* <key> <value> def - */
  80. /*
  81. * We make this into a separate procedure because
  82. * the interpreter will almost always call it directly.
  83. */
  84. int
  85. zop_def(i_ctx_t *i_ctx_p)
  86. {
  87. os_ptr op = osp;
  88. os_ptr op1 = op - 1;
  89. ref *pvslot;
  90. /* The following combines a check_op(2) with a type check. */
  91. switch (r_type(op1)) {
  92. case t_name: {
  93. /* We can use the fast single-probe lookup here. */
  94. uint nidx = name_index(imemory, op1);
  95. uint htemp;
  96. if_dict_find_name_by_index_top(nidx, htemp, pvslot) {
  97. if (dtop_can_store(op))
  98. goto ra;
  99. }
  100. break; /* handle all slower cases */
  101. }
  102. case t_null:
  103. return_error(e_typecheck);
  104. case t__invalid:
  105. return_error(e_stackunderflow);
  106. }
  107. /*
  108. * Combine the check for a writable top dictionary with
  109. * the global/local store check. See dstack.h for details.
  110. */
  111. if (!dtop_can_store(op)) {
  112. check_dict_write(*dsp);
  113. /*
  114. * If the dictionary is writable, the problem must be
  115. * an invalid store.
  116. */
  117. return_error(e_invalidaccess);
  118. }
  119. /*
  120. * Save a level of procedure call in the common (redefinition)
  121. * case. With the current interfaces, we pay a double lookup
  122. * in the uncommon case.
  123. */
  124. if (dict_find(dsp, op1, &pvslot) <= 0)
  125. return idict_put(dsp, op1, op);
  126. ra:
  127. ref_assign_old_inline(&dsp->value.pdict->values, pvslot, op,
  128. "dict_put(value)");
  129. return 0;
  130. }
  131. int
  132. zdef(i_ctx_t *i_ctx_p)
  133. {
  134. int code = zop_def(i_ctx_p);
  135. if (code >= 0) {
  136. pop(2);
  137. }
  138. return code;
  139. }
  140. /* <key> load <value> */
  141. private int
  142. zload(i_ctx_t *i_ctx_p)
  143. {
  144. os_ptr op = osp;
  145. ref *pvalue;
  146. switch (r_type(op)) {
  147. case t_name:
  148. /* Use the fast lookup. */
  149. if ((pvalue = dict_find_name(op)) == 0)
  150. return_error(e_undefined);
  151. ref_assign(op, pvalue);
  152. return 0;
  153. case t_null:
  154. return_error(e_typecheck);
  155. case t__invalid:
  156. return_error(e_stackunderflow);
  157. default: {
  158. /* Use an explicit loop. */
  159. uint size = ref_stack_count(&d_stack);
  160. uint i;
  161. for (i = 0; i < size; i++) {
  162. ref *dp = ref_stack_index(&d_stack, i);
  163. check_dict_read(*dp);
  164. if (dict_find(dp, op, &pvalue) > 0) {
  165. ref_assign(op, pvalue);
  166. return 0;
  167. }
  168. }
  169. return_error(e_undefined);
  170. }
  171. }
  172. }
  173. /* get - implemented in zgeneric.c */
  174. /* put - implemented in zgeneric.c */
  175. /* <dict> <key> .undef - */
  176. /* <dict> <key> undef - */
  177. private int
  178. zundef(i_ctx_t *i_ctx_p)
  179. {
  180. os_ptr op = osp;
  181. check_type(op[-1], t_dictionary);
  182. check_dict_write(op[-1]);
  183. idict_undef(op - 1, op); /* ignore undefined error */
  184. pop(2);
  185. return 0;
  186. }
  187. /* <dict> <key> known <bool> */
  188. private int
  189. zknown(i_ctx_t *i_ctx_p)
  190. {
  191. os_ptr op = osp;
  192. register os_ptr op1 = op - 1;
  193. ref *pvalue;
  194. check_type(*op1, t_dictionary);
  195. check_dict_read(*op1);
  196. make_bool(op1, (dict_find(op1, op, &pvalue) > 0 ? 1 : 0));
  197. pop(1);
  198. return 0;
  199. }
  200. /* <key> where <dict> true */
  201. /* <key> where false */
  202. int
  203. zwhere(i_ctx_t *i_ctx_p)
  204. {
  205. os_ptr op = osp;
  206. ref_stack_enum_t rsenum;
  207. check_op(1);
  208. ref_stack_enum_begin(&rsenum, &d_stack);
  209. do {
  210. const ref *const bot = rsenum.ptr;
  211. const ref *pdref = bot + rsenum.size;
  212. ref *pvalue;
  213. while (pdref-- > bot) {
  214. check_dict_read(*pdref);
  215. if (dict_find(pdref, op, &pvalue) > 0) {
  216. push(1);
  217. ref_assign(op - 1, pdref);
  218. make_true(op);
  219. return 0;
  220. }
  221. }
  222. } while (ref_stack_enum_next(&rsenum));
  223. make_false(op);
  224. return 0;
  225. }
  226. /* copy for dictionaries -- called from zcopy in zgeneric.c. */
  227. /* Only the type of *op has been checked. */
  228. int
  229. zcopy_dict(i_ctx_t *i_ctx_p)
  230. {
  231. os_ptr op = osp;
  232. os_ptr op1 = op - 1;
  233. int code;
  234. check_type(*op1, t_dictionary);
  235. check_dict_read(*op1);
  236. check_dict_write(*op);
  237. if (!imemory->gs_lib_ctx->dict_auto_expand &&
  238. (dict_length(op) != 0 || dict_maxlength(op) < dict_length(op1))
  239. )
  240. return_error(e_rangecheck);
  241. code = idict_copy(op1, op);
  242. if (code < 0)
  243. return code;
  244. /*
  245. * In Level 1 systems, we must copy the access attributes too.
  246. * The only possible effect this can have is to make the
  247. * copy read-only if the original dictionary is read-only.
  248. */
  249. if (!level2_enabled)
  250. r_copy_attrs(dict_access_ref(op), a_write, dict_access_ref(op1));
  251. ref_assign(op1, op);
  252. pop(1);
  253. return 0;
  254. }
  255. /* - currentdict <dict> */
  256. private int
  257. zcurrentdict(i_ctx_t *i_ctx_p)
  258. {
  259. os_ptr op = osp;
  260. push(1);
  261. ref_assign(op, dsp);
  262. return 0;
  263. }
  264. /* - countdictstack <int> */
  265. private int
  266. zcountdictstack(i_ctx_t *i_ctx_p)
  267. {
  268. os_ptr op = osp;
  269. uint count = ref_stack_count(&d_stack);
  270. push(1);
  271. if (!level2_enabled)
  272. count--; /* see dstack.h */
  273. make_int(op, count);
  274. return 0;
  275. }
  276. /* <array> dictstack <subarray> */
  277. private int
  278. zdictstack(i_ctx_t *i_ctx_p)
  279. {
  280. os_ptr op = osp;
  281. uint count = ref_stack_count(&d_stack);
  282. check_write_type(*op, t_array);
  283. if (!level2_enabled)
  284. count--; /* see dstack.h */
  285. return ref_stack_store(&d_stack, op, count, 0, 0, true, idmemory,
  286. "dictstack");
  287. }
  288. /* - cleardictstack - */
  289. private int
  290. zcleardictstack(i_ctx_t *i_ctx_p)
  291. {
  292. while (zend(i_ctx_p) >= 0)
  293. DO_NOTHING;
  294. return 0;
  295. }
  296. /* ------ Extensions ------ */
  297. /* <dict1> <dict2> .dictcopynew <dict2> */
  298. private int
  299. zdictcopynew(i_ctx_t *i_ctx_p)
  300. {
  301. os_ptr op = osp;
  302. os_ptr op1 = op - 1;
  303. int code;
  304. check_type(*op1, t_dictionary);
  305. check_dict_read(*op1);
  306. check_type(*op, t_dictionary);
  307. check_dict_write(*op);
  308. /* This is only recognized in Level 2 mode. */
  309. if (!imemory->gs_lib_ctx->dict_auto_expand)
  310. return_error(e_undefined);
  311. code = idict_copy_new(op1, op);
  312. if (code < 0)
  313. return code;
  314. ref_assign(op1, op);
  315. pop(1);
  316. return 0;
  317. }
  318. /* -mark- <key0> <value0> <key1> <value1> ... .dicttomark <dict> */
  319. /* This is the Level 2 >> operator. */
  320. private int
  321. zdicttomark(i_ctx_t *i_ctx_p)
  322. {
  323. uint count2 = ref_stack_counttomark(&o_stack);
  324. ref rdict;
  325. int code;
  326. uint idx;
  327. if (count2 == 0)
  328. return_error(e_unmatchedmark);
  329. count2--;
  330. if ((count2 & 1) != 0)
  331. return_error(e_rangecheck);
  332. code = dict_create(count2 >> 1, &rdict);
  333. if (code < 0)
  334. return code;
  335. /* << /a 1 /a 2 >> => << /a 1 >>, i.e., */
  336. /* we must enter the keys in top-to-bottom order. */
  337. for (idx = 0; idx < count2; idx += 2) {
  338. code = idict_put(&rdict,
  339. ref_stack_index(&o_stack, idx + 1),
  340. ref_stack_index(&o_stack, idx));
  341. if (code < 0) { /* There's no way to free the dictionary -- too bad. */
  342. return code;
  343. }
  344. }
  345. ref_stack_pop(&o_stack, count2);
  346. ref_assign(osp, &rdict);
  347. return code;
  348. }
  349. /* <dict> <key> .forceundef - */
  350. /*
  351. * This forces an "undef" even if the dictionary is not writable.
  352. * Like .forceput, it is meant to be used only in a few special situations,
  353. * and should not be accessible by name after initialization.
  354. */
  355. private int
  356. zforceundef(i_ctx_t *i_ctx_p)
  357. {
  358. os_ptr op = osp;
  359. check_type(op[-1], t_dictionary);
  360. /* Don't check_dict_write */
  361. idict_undef(op - 1, op); /* ignore undefined error */
  362. pop(2);
  363. return 0;
  364. }
  365. /* <dict> <key> .knownget <value> true */
  366. /* <dict> <key> .knownget false */
  367. private int
  368. zknownget(i_ctx_t *i_ctx_p)
  369. {
  370. os_ptr op = osp;
  371. register os_ptr op1 = op - 1;
  372. ref *pvalue;
  373. check_type(*op1, t_dictionary);
  374. check_dict_read(*op1);
  375. if (dict_find(op1, op, &pvalue) <= 0) {
  376. make_false(op1);
  377. pop(1);
  378. } else {
  379. ref_assign(op1, pvalue);
  380. make_true(op);
  381. }
  382. return 0;
  383. }
  384. /* <dict> <key> .knownundef <bool> */
  385. private int
  386. zknownundef(i_ctx_t *i_ctx_p)
  387. {
  388. os_ptr op = osp;
  389. os_ptr op1 = op - 1;
  390. int code;
  391. check_type(*op1, t_dictionary);
  392. check_dict_write(*op1);
  393. code = idict_undef(op1, op);
  394. make_bool(op1, code == 0);
  395. pop(1);
  396. return 0;
  397. }
  398. /* <dict> <int> .setmaxlength - */
  399. private int
  400. zsetmaxlength(i_ctx_t *i_ctx_p)
  401. {
  402. os_ptr op = osp;
  403. os_ptr op1 = op - 1;
  404. uint new_size;
  405. int code;
  406. check_type(*op1, t_dictionary);
  407. check_dict_write(*op1);
  408. check_type(*op, t_integer);
  409. #if arch_sizeof_int < arch_sizeof_long
  410. check_int_leu(*op, max_uint);
  411. #else
  412. if (op->value.intval < 0)
  413. return_error(e_rangecheck);
  414. #endif
  415. new_size = (uint) op->value.intval;
  416. if (dict_length(op - 1) > new_size)
  417. return_error(e_dictfull);
  418. code = idict_resize(op - 1, new_size);
  419. if (code >= 0)
  420. pop(2);
  421. return code;
  422. }
  423. /* ------ Initialization procedure ------ */
  424. /* We need to split the table because of the 16-element limit. */
  425. const op_def zdict1_op_defs[] = {
  426. {"0cleardictstack", zcleardictstack},
  427. {"1begin", zbegin},
  428. {"0countdictstack", zcountdictstack},
  429. {"0currentdict", zcurrentdict},
  430. {"2def", zdef},
  431. {"1dict", zdict},
  432. {"0dictstack", zdictstack},
  433. {"0end", zend},
  434. {"2known", zknown},
  435. {"1load", zload},
  436. {"1maxlength", zmaxlength},
  437. {"2.undef", zundef}, /* we need this even in Level 1 */
  438. {"1where", zwhere},
  439. op_def_end(0)
  440. };
  441. const op_def zdict2_op_defs[] = {
  442. /* Extensions */
  443. {"2.dictcopynew", zdictcopynew},
  444. {"1.dicttomark", zdicttomark},
  445. {"2.forceundef", zforceundef},
  446. {"2.knownget", zknownget},
  447. {"1.knownundef", zknownundef},
  448. {"2.setmaxlength", zsetmaxlength},
  449. op_def_end(0)
  450. };