zdict.c 11 KB

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