zgeneric.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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: zgeneric.c,v 1.3 2000/09/19 19:00:54 lpd Exp $ */
  16. /* Array/string/dictionary generic operators for PostScript */
  17. #include "memory_.h"
  18. #include "ghost.h"
  19. #include "gsstruct.h" /* for st_bytes */
  20. #include "oper.h"
  21. #include "dstack.h" /* for systemdict */
  22. #include "estack.h" /* for forall */
  23. #include "iddict.h"
  24. #include "iname.h"
  25. #include "ipacked.h"
  26. #include "ivmspace.h"
  27. #include "store.h"
  28. /* This file implements copy, get, put, getinterval, putinterval, */
  29. /* length, and forall, which apply generically to */
  30. /* arrays, strings, and dictionaries. (Copy also has a special */
  31. /* meaning for copying the top N elements of the stack.) */
  32. /* See the comment in opdef.h for an invariant which allows */
  33. /* more efficient implementation of forall. */
  34. /* Forward references */
  35. private int zcopy_integer(P1(i_ctx_t *));
  36. private int zcopy_interval(P1(i_ctx_t *));
  37. private int copy_interval(P5(i_ctx_t *, os_ptr, uint, os_ptr, client_name_t));
  38. /* <various1> <various2> copy <various> */
  39. /* <obj1> ... <objn> <int> copy <obj1> ... <objn> <obj1> ... <objn> */
  40. /* Note that this implements copy for arrays and strings, */
  41. /* but not for dictionaries (see zcopy_dict in zdict.c). */
  42. int
  43. zcopy(i_ctx_t *i_ctx_p)
  44. {
  45. os_ptr op = osp;
  46. int type = r_type(op);
  47. if (type == t_integer)
  48. return zcopy_integer(i_ctx_p);
  49. check_op(2);
  50. switch (type) {
  51. case t_array:
  52. case t_string:
  53. return zcopy_interval(i_ctx_p);
  54. case t_dictionary:
  55. return zcopy_dict(i_ctx_p);
  56. default:
  57. return_op_typecheck(op);
  58. }
  59. }
  60. /* <obj1> ... <objn> <int> copy <obj1> ... <objn> <obj1> ... <objn> */
  61. private int
  62. zcopy_integer(i_ctx_t *i_ctx_p)
  63. {
  64. os_ptr op = osp;
  65. os_ptr op1 = op - 1;
  66. int count, i;
  67. int code;
  68. if ((ulong) op->value.intval > op - osbot) {
  69. /* There might be enough elements in other blocks. */
  70. check_int_ltu(*op, ref_stack_count(&o_stack));
  71. count = op->value.intval;
  72. } else if (op1 + (count = op->value.intval) <= ostop) {
  73. /* Fast case. */
  74. memcpy((char *)op, (char *)(op - count), count * sizeof(ref));
  75. push(count - 1);
  76. return 0;
  77. }
  78. /* Do it the slow, general way. */
  79. code = ref_stack_push(&o_stack, count - 1);
  80. if (code < 0)
  81. return code;
  82. for (i = 0; i < count; i++)
  83. *ref_stack_index(&o_stack, i) =
  84. *ref_stack_index(&o_stack, i + count);
  85. return 0;
  86. }
  87. /* <array1> <array2> copy <subarray2> */
  88. /* <string1> <string2> copy <substring2> */
  89. private int
  90. zcopy_interval(i_ctx_t *i_ctx_p)
  91. {
  92. os_ptr op = osp;
  93. os_ptr op1 = op - 1;
  94. int code = copy_interval(i_ctx_p, op, 0, op1, "copy");
  95. if (code < 0)
  96. return code;
  97. r_set_size(op, r_size(op1));
  98. *op1 = *op;
  99. pop(1);
  100. return 0;
  101. }
  102. /* <array|dict|name|packedarray|string> length <int> */
  103. private int
  104. zlength(i_ctx_t *i_ctx_p)
  105. {
  106. os_ptr op = osp;
  107. switch (r_type(op)) {
  108. case t_array:
  109. case t_string:
  110. case t_mixedarray:
  111. case t_shortarray:
  112. check_read(*op);
  113. make_int(op, r_size(op));
  114. return 0;
  115. case t_dictionary:
  116. check_dict_read(*op);
  117. make_int(op, dict_length(op));
  118. return 0;
  119. case t_name: {
  120. ref str;
  121. name_string_ref(op, &str);
  122. make_int(op, r_size(&str));
  123. return 0;
  124. }
  125. case t_astruct:
  126. if (gs_object_type(imemory, op->value.pstruct) != &st_bytes)
  127. return_error(e_typecheck);
  128. check_read(*op);
  129. make_int(op, gs_object_size(imemory, op->value.pstruct));
  130. return 0;
  131. default:
  132. return_op_typecheck(op);
  133. }
  134. }
  135. /* <array|packedarray|string> <index> get <obj> */
  136. /* <dict> <key> get <obj> */
  137. private int
  138. zget(i_ctx_t *i_ctx_p)
  139. {
  140. os_ptr op = osp;
  141. os_ptr op1 = op - 1;
  142. ref *pvalue;
  143. switch (r_type(op1)) {
  144. case t_dictionary:
  145. check_dict_read(*op1);
  146. if (dict_find(op1, op, &pvalue) <= 0)
  147. return_error(e_undefined);
  148. op[-1] = *pvalue;
  149. break;
  150. case t_string:
  151. check_read(*op1);
  152. check_int_ltu(*op, r_size(op1));
  153. make_int(op1, op1->value.bytes[(uint) op->value.intval]);
  154. break;
  155. default: {
  156. int code;
  157. check_type(*op, t_integer);
  158. check_read(*op1);
  159. code = array_get(op1, op->value.intval, op1);
  160. if (code < 0) { /* Might be a stackunderflow reported as typecheck. */
  161. if (code == e_typecheck)
  162. return_op_typecheck(op1);
  163. else
  164. return code;
  165. }
  166. }
  167. }
  168. pop(1);
  169. return 0;
  170. }
  171. /* <array> <index> <obj> put - */
  172. /* <dict> <key> <value> put - */
  173. /* <string> <index> <int> put - */
  174. private int
  175. zput(i_ctx_t *i_ctx_p)
  176. {
  177. os_ptr op = osp;
  178. os_ptr op1 = op - 1;
  179. os_ptr op2 = op1 - 1;
  180. byte *sdata;
  181. uint ssize;
  182. switch (r_type(op2)) {
  183. case t_dictionary:
  184. check_dict_write(*op2);
  185. {
  186. int code = idict_put(op2, op1, op);
  187. if (code < 0)
  188. return code; /* error */
  189. }
  190. break;
  191. case t_array:
  192. check_write(*op2);
  193. check_int_ltu(*op1, r_size(op2));
  194. store_check_dest(op2, op);
  195. {
  196. ref *eltp = op2->value.refs + (uint) op1->value.intval;
  197. ref_assign_old(op2, eltp, op, "put");
  198. }
  199. break;
  200. case t_mixedarray: /* packed arrays are read-only */
  201. case t_shortarray:
  202. return_error(e_invalidaccess);
  203. case t_string:
  204. sdata = op2->value.bytes;
  205. ssize = r_size(op2);
  206. str: check_write(*op2);
  207. check_int_ltu(*op1, ssize);
  208. check_int_leu(*op, 0xff);
  209. sdata[(uint)op1->value.intval] = (byte)op->value.intval;
  210. break;
  211. case t_astruct:
  212. if (gs_object_type(imemory, op2->value.pstruct) != &st_bytes)
  213. return_error(e_typecheck);
  214. sdata = r_ptr(op2, byte);
  215. ssize = gs_object_size(imemory, op2->value.pstruct);
  216. goto str;
  217. default:
  218. return_op_typecheck(op2);
  219. }
  220. pop(3);
  221. return 0;
  222. }
  223. /* <array> <index> <obj> .forceput - */
  224. /* <dict> <key> <value> .forceput - */
  225. /*
  226. * This forces a "put" even if the object is not writable, and (if the
  227. * object is systemdict or the save level is 0) even if the value is in
  228. * local VM. It is meant to be used only for replacing the value of
  229. * FontDirectory in systemdict when switching between local and global VM,
  230. * and a few similar applications. After initialization, this operator
  231. * should no longer be accessible by name.
  232. */
  233. private int
  234. zforceput(i_ctx_t *i_ctx_p)
  235. {
  236. os_ptr op = osp;
  237. os_ptr op1 = op - 1;
  238. os_ptr op2 = op - 2;
  239. int code;
  240. switch (r_type(op2)) {
  241. case t_array:
  242. check_int_ltu(*op1, r_size(op2));
  243. if (r_space(op2) > r_space(op)) {
  244. if (imemory_save_level(iimemory))
  245. return_error(e_invalidaccess);
  246. }
  247. {
  248. ref *eltp = op2->value.refs + (uint) op1->value.intval;
  249. ref_assign_old(op2, eltp, op, "put");
  250. }
  251. break;
  252. case t_dictionary:
  253. if (op2->value.pdict == systemdict->value.pdict ||
  254. !imemory_save_level(iimemory)
  255. ) {
  256. uint space = r_space(op2);
  257. r_set_space(op2, avm_local);
  258. code = idict_put(op2, op1, op);
  259. r_set_space(op2, space);
  260. } else
  261. code = idict_put(op2, op1, op);
  262. if (code < 0)
  263. return code;
  264. break;
  265. default:
  266. return_error(e_typecheck);
  267. }
  268. pop(3);
  269. return 0;
  270. }
  271. /* <seq:array|packedarray|string> <index> <count> getinterval <subseq> */
  272. private int
  273. zgetinterval(i_ctx_t *i_ctx_p)
  274. {
  275. os_ptr op = osp;
  276. os_ptr op1 = op - 1;
  277. os_ptr op2 = op1 - 1;
  278. uint index;
  279. uint count;
  280. switch (r_type(op2)) {
  281. default:
  282. return_op_typecheck(op2);
  283. case t_array:
  284. case t_string:
  285. case t_mixedarray:
  286. case t_shortarray:;
  287. }
  288. check_read(*op2);
  289. check_int_leu(*op1, r_size(op2));
  290. index = op1->value.intval;
  291. check_int_leu(*op, r_size(op2) - index);
  292. count = op->value.intval;
  293. switch (r_type(op2)) {
  294. case t_array:
  295. op2->value.refs += index;
  296. break;
  297. case t_string:
  298. op2->value.bytes += index;
  299. break;
  300. case t_mixedarray: {
  301. const ref_packed *packed = op2->value.packed;
  302. for (; index--;)
  303. packed = packed_next(packed);
  304. op2->value.packed = packed;
  305. break;
  306. }
  307. case t_shortarray:
  308. op2->value.packed += index;
  309. break;
  310. }
  311. r_set_size(op2, count);
  312. pop(2);
  313. return 0;
  314. }
  315. /* <array1> <index> <array2|packedarray2> putinterval - */
  316. /* <string1> <index> <string2> putinterval - */
  317. /* <bytestring1> <index> <string2> putinterval - */
  318. private int
  319. zputinterval(i_ctx_t *i_ctx_p)
  320. {
  321. os_ptr op = osp;
  322. os_ptr opindex = op - 1;
  323. os_ptr opto = opindex - 1;
  324. int code;
  325. switch (r_type(opto)) {
  326. default:
  327. return_op_typecheck(opto);
  328. case t_mixedarray:
  329. case t_shortarray:
  330. return_error(e_invalidaccess);
  331. case t_array:
  332. case t_string:
  333. check_write(*opto);
  334. check_int_leu(*opindex, r_size(opto));
  335. code = copy_interval(i_ctx_p, opto, (uint)(opindex->value.intval),
  336. op, "putinterval");
  337. break;
  338. case t_astruct: {
  339. uint dsize, ssize, index;
  340. check_write(*opto);
  341. if (gs_object_type(imemory, opto->value.pstruct) != &st_bytes)
  342. return_error(e_typecheck);
  343. dsize = gs_object_size(imemory, opto->value.pstruct);
  344. check_int_leu(*opindex, dsize);
  345. index = (uint)opindex->value.intval;
  346. check_read_type(*op, t_string);
  347. ssize = r_size(op);
  348. if (ssize > dsize - index)
  349. return_error(e_rangecheck);
  350. memcpy(r_ptr(opto, byte) + index, op->value.const_bytes, ssize);
  351. code = 0;
  352. break;
  353. }
  354. }
  355. if (code >= 0)
  356. pop(3);
  357. return code;
  358. }
  359. /* <array|packedarray|string> <<element> proc> forall - */
  360. /* <dict> <<key> <value> proc> forall - */
  361. private int
  362. array_continue(P1(i_ctx_t *)),
  363. dict_continue(P1(i_ctx_t *)),
  364. string_continue(P1(i_ctx_t *)),
  365. packedarray_continue(P1(i_ctx_t *));
  366. private int forall_cleanup(P1(i_ctx_t *));
  367. private int
  368. zforall(i_ctx_t *i_ctx_p)
  369. {
  370. os_ptr op = osp;
  371. os_ptr obj = op - 1;
  372. es_ptr ep = esp;
  373. es_ptr cproc = ep + 4;
  374. check_estack(6);
  375. switch (r_type(obj)) {
  376. default:
  377. return_op_typecheck(obj);
  378. case t_array:
  379. check_read(*obj);
  380. make_op_estack(cproc, array_continue);
  381. break;
  382. case t_dictionary:
  383. check_dict_read(*obj);
  384. make_int(cproc, dict_first(obj));
  385. ++cproc;
  386. make_op_estack(cproc, dict_continue);
  387. break;
  388. case t_string:
  389. check_read(*obj);
  390. make_op_estack(cproc, string_continue);
  391. break;
  392. case t_mixedarray:
  393. case t_shortarray:
  394. check_read(*obj);
  395. make_op_estack(cproc, packedarray_continue);
  396. break;
  397. }
  398. check_proc(*op);
  399. /*
  400. * Push:
  401. * - a mark;
  402. * - the composite object;
  403. * - the procedure;
  404. * - the iteration index (only for dictionaries, done above);
  405. * and invoke the continuation operator.
  406. */
  407. make_mark_estack(ep + 1, es_for, forall_cleanup);
  408. ep[2] = *obj;
  409. ep[3] = *op;
  410. esp = cproc - 1;
  411. pop(2);
  412. return (*real_opproc(cproc))(i_ctx_p);
  413. }
  414. /* Continuation operator for arrays */
  415. private int
  416. array_continue(i_ctx_t *i_ctx_p)
  417. {
  418. os_ptr op = osp;
  419. es_ptr obj = esp - 1;
  420. if (r_size(obj)) { /* continue */
  421. push(1);
  422. r_dec_size(obj, 1);
  423. *op = *obj->value.refs;
  424. obj->value.refs++;
  425. esp += 2;
  426. *esp = obj[1];
  427. return o_push_estack;
  428. } else { /* done */
  429. esp -= 3; /* pop mark, object, proc */
  430. return o_pop_estack;
  431. }
  432. }
  433. /* Continuation operator for dictionaries */
  434. private int
  435. dict_continue(i_ctx_t *i_ctx_p)
  436. {
  437. os_ptr op = osp;
  438. es_ptr obj = esp - 2;
  439. int index = (int)esp->value.intval;
  440. push(2); /* make room for key and value */
  441. if ((index = dict_next(obj, index, op - 1)) >= 0) { /* continue */
  442. esp->value.intval = index;
  443. esp += 2;
  444. *esp = obj[1];
  445. return o_push_estack;
  446. } else { /* done */
  447. pop(2); /* undo push */
  448. esp -= 4; /* pop mark, object, proc, index */
  449. return o_pop_estack;
  450. }
  451. }
  452. /* Continuation operator for strings */
  453. private int
  454. string_continue(i_ctx_t *i_ctx_p)
  455. {
  456. os_ptr op = osp;
  457. es_ptr obj = esp - 1;
  458. if (r_size(obj)) { /* continue */
  459. r_dec_size(obj, 1);
  460. push(1);
  461. make_int(op, *obj->value.bytes);
  462. obj->value.bytes++;
  463. esp += 2;
  464. *esp = obj[1];
  465. return o_push_estack;
  466. } else { /* done */
  467. esp -= 3; /* pop mark, object, proc */
  468. return o_pop_estack;
  469. }
  470. }
  471. /* Continuation operator for packed arrays */
  472. private int
  473. packedarray_continue(i_ctx_t *i_ctx_p)
  474. {
  475. os_ptr op = osp;
  476. es_ptr obj = esp - 1;
  477. if (r_size(obj)) { /* continue */
  478. const ref_packed *packed = obj->value.packed;
  479. r_dec_size(obj, 1);
  480. push(1);
  481. packed_get(packed, op);
  482. obj->value.packed = packed_next(packed);
  483. esp += 2;
  484. *esp = obj[1];
  485. return o_push_estack;
  486. } else { /* done */
  487. esp -= 3; /* pop mark, object, proc */
  488. return o_pop_estack;
  489. }
  490. }
  491. /* Vacuous cleanup procedure */
  492. private int
  493. forall_cleanup(i_ctx_t *i_ctx_p)
  494. {
  495. return 0;
  496. }
  497. /* ------ Initialization procedure ------ */
  498. const op_def zgeneric_op_defs[] =
  499. {
  500. {"1copy", zcopy},
  501. {"2forall", zforall},
  502. {"3.forceput", zforceput},
  503. {"2get", zget},
  504. {"3getinterval", zgetinterval},
  505. {"1length", zlength},
  506. {"3put", zput},
  507. {"3putinterval", zputinterval},
  508. /* Internal operators */
  509. {"0%array_continue", array_continue},
  510. {"0%dict_continue", dict_continue},
  511. {"0%packedarray_continue", packedarray_continue},
  512. {"0%string_continue", string_continue},
  513. op_def_end(0)
  514. };
  515. /* ------ Shared routines ------ */
  516. /* Copy an interval from one operand to another. */
  517. /* This is used by both putinterval and string/array copy. */
  518. /* The destination is known to be an array or string, */
  519. /* and the starting index is known to be less than or equal to */
  520. /* its length; nothing else has been checked. */
  521. private int
  522. copy_interval(i_ctx_t *i_ctx_p /* for ref_assign_old */, os_ptr prto,
  523. uint index, os_ptr prfrom, client_name_t cname)
  524. {
  525. int fromtype = r_type(prfrom);
  526. uint fromsize = r_size(prfrom);
  527. if (!(fromtype == r_type(prto) ||
  528. ((fromtype == t_shortarray || fromtype == t_mixedarray) &&
  529. r_type(prto) == t_array))
  530. )
  531. return_op_typecheck(prfrom);
  532. check_read(*prfrom);
  533. check_write(*prto);
  534. if (fromsize > r_size(prto) - index)
  535. return_error(e_rangecheck);
  536. switch (fromtype) {
  537. case t_array:
  538. { /* We have to worry about aliasing, */
  539. /* but refcpy_to_old takes care of it for us. */
  540. return refcpy_to_old(prto, index, prfrom->value.refs,
  541. fromsize, idmemory, cname);
  542. }
  543. case t_string:
  544. { /* memmove takes care of aliasing. */
  545. memmove(prto->value.bytes + index, prfrom->value.bytes,
  546. fromsize);
  547. }
  548. break;
  549. case t_mixedarray:
  550. case t_shortarray:
  551. { /* We don't have to worry about aliasing, because */
  552. /* packed arrays are read-only and hence the destination */
  553. /* can't be a packed array. */
  554. int i;
  555. const ref_packed *packed = prfrom->value.packed;
  556. ref *pdest = prto->value.refs + index;
  557. ref elt;
  558. for (i = 0; i < fromsize; i++, pdest++) {
  559. packed_get(packed, &elt);
  560. ref_assign_old(prto, pdest, &elt, cname);
  561. packed = packed_next(packed);
  562. }
  563. }
  564. break;
  565. }
  566. return 0;
  567. }