gscparam.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /* Copyright (C) 1995, 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: gscparam.c,v 1.10 2004/08/04 19:36:12 stefan Exp $ */
  14. /* Default implementation of parameter lists */
  15. #include "memory_.h"
  16. #include "string_.h"
  17. #include "gx.h"
  18. #include "gserrors.h"
  19. #include "gsparam.h"
  20. #include "gsstruct.h"
  21. /* Forward references */
  22. typedef union c_param_value_s {
  23. GS_PARAM_VALUE_UNION(gs_c_param_list);
  24. } gs_c_param_value;
  25. /*typedef struct gs_c_param_s gs_c_param; *//* in gsparam.h */
  26. /* Define the GC type for a parameter list. */
  27. private_st_c_param_list();
  28. /* Lengths corresponding to various gs_param_type_xxx types */
  29. const byte gs_param_type_sizes[] = {
  30. GS_PARAM_TYPE_SIZES(sizeof(gs_c_param_list))
  31. };
  32. /* Lengths of *actual* data-containing type pointed to or contained by gs_param_type_xxx's */
  33. const byte gs_param_type_base_sizes[] = {
  34. GS_PARAM_TYPE_BASE_SIZES(0)
  35. };
  36. /*
  37. * Define a parameter list element. We use gs_param_type_any to identify
  38. * elements that have been requested but not yet written. The reading
  39. * procedures must recognize such elements as undefined, and ignore them.
  40. */
  41. struct gs_c_param_s {
  42. gs_c_param *next;
  43. gs_param_key_t key;
  44. bool free_key;
  45. gs_c_param_value value;
  46. gs_param_type type;
  47. void *alternate_typed_data;
  48. };
  49. /* GC descriptor and procedures */
  50. gs_private_st_composite(st_c_param, gs_c_param, "gs_c_param",
  51. c_param_enum_ptrs, c_param_reloc_ptrs);
  52. ENUM_PTRS_WITH(c_param_enum_ptrs, gs_c_param *param) {
  53. index -= 3;
  54. switch (param->type) {
  55. /* Only the aggregate types are handled specially. */
  56. case gs_param_type_dict:
  57. case gs_param_type_dict_int_keys:
  58. case gs_param_type_array:
  59. return ENUM_USING(st_c_param_list, &param->value.d,
  60. sizeof(param->value.d), index);
  61. default: {
  62. gs_param_typed_value value;
  63. value.value = *(const gs_param_value *)&param->value;
  64. value.type = param->type;
  65. return gs_param_typed_value_enum_ptrs(mem, &value, sizeof(value), index,
  66. pep, NULL, gcst);
  67. }
  68. }
  69. }
  70. case 0: return ENUM_OBJ(param->next);
  71. case 1: return ENUM_OBJ(param->alternate_typed_data);
  72. case 2:
  73. if (!param->key.persistent) {
  74. gs_const_string key;
  75. key.data = param->key.data;
  76. key.size = param->key.size;
  77. return ENUM_STRING(&key);
  78. } else
  79. return ENUM_OBJ(0); /* keep going */
  80. ENUM_PTRS_END
  81. RELOC_PTRS_WITH(c_param_reloc_ptrs, gs_c_param *param) {
  82. RELOC_VAR(param->next);
  83. RELOC_VAR(param->alternate_typed_data);
  84. if (!param->key.persistent) {
  85. gs_const_string key;
  86. key.data = param->key.data;
  87. key.size = param->key.size;
  88. RELOC_CONST_STRING_VAR(key);
  89. param->key.data = key.data;
  90. }
  91. switch (param->type) {
  92. /* Only the aggregate types are handled specially. */
  93. case gs_param_type_dict:
  94. case gs_param_type_dict_int_keys:
  95. case gs_param_type_array:
  96. RELOC_USING(st_c_param_list, &param->value.d, sizeof(param->value.d));
  97. break;
  98. default: {
  99. gs_param_typed_value value;
  100. value.value = *(gs_param_value *)&param->value;
  101. value.type = param->type;
  102. gs_param_typed_value_reloc_ptrs(&value, sizeof(value), NULL, gcst);
  103. *(gs_param_value *)&param->value = value.value;
  104. }
  105. }
  106. }
  107. RELOC_PTRS_END
  108. /* ---------------- Utilities ---------------- */
  109. gs_c_param_list *
  110. gs_c_param_list_alloc(gs_memory_t *mem, client_name_t cname)
  111. {
  112. return gs_alloc_struct(mem, gs_c_param_list, &st_c_param_list, cname);
  113. }
  114. private gs_c_param *
  115. c_param_find(const gs_c_param_list *plist, gs_param_name pkey, bool any)
  116. {
  117. gs_c_param *pparam = plist->head;
  118. uint len = strlen(pkey);
  119. for (; pparam != 0; pparam = pparam->next)
  120. if (pparam->key.size == len && !memcmp(pparam->key.data, pkey, len))
  121. return (pparam->type != gs_param_type_any || any ? pparam : 0);
  122. return 0;
  123. }
  124. /* ---------------- Writing parameters to a list ---------------- */
  125. private param_proc_begin_xmit_collection(c_param_begin_write_collection);
  126. private param_proc_end_xmit_collection(c_param_end_write_collection);
  127. private param_proc_xmit_typed(c_param_write_typed);
  128. private param_proc_request(c_param_request);
  129. private param_proc_requested(c_param_requested);
  130. private const gs_param_list_procs c_write_procs =
  131. {
  132. c_param_write_typed,
  133. c_param_begin_write_collection,
  134. c_param_end_write_collection,
  135. NULL, /* get_next_key */
  136. c_param_request,
  137. c_param_requested
  138. };
  139. /* Initialize a list for writing. */
  140. void
  141. gs_c_param_list_write(gs_c_param_list * plist, gs_memory_t * mem)
  142. {
  143. plist->memory = mem;
  144. plist->head = 0;
  145. plist->target = 0; /* not used for writing */
  146. plist->count = 0;
  147. plist->any_requested = false;
  148. plist->persistent_keys = true;
  149. gs_c_param_list_write_more(plist);
  150. }
  151. /* Set the target of a list. Only relevant for reading. */
  152. void
  153. gs_c_param_list_set_target(gs_c_param_list *plist, gs_param_list *target)
  154. {
  155. plist->target = target;
  156. }
  157. /* Re-enable a list for writing, without clearing it. */
  158. /* gs_c_param_list_write must have been called previously. */
  159. void
  160. gs_c_param_list_write_more(gs_c_param_list * plist)
  161. {
  162. plist->procs = &c_write_procs;
  163. plist->coll_type = gs_param_collection_dict_any;
  164. }
  165. /* Release a list. */
  166. void
  167. gs_c_param_list_release(gs_c_param_list * plist)
  168. {
  169. gs_memory_t *mem = plist->memory;
  170. gs_c_param *pparam;
  171. while ((pparam = plist->head) != 0) {
  172. gs_c_param *next = pparam->next;
  173. switch (pparam->type) {
  174. case gs_param_type_dict:
  175. case gs_param_type_dict_int_keys:
  176. case gs_param_type_array:
  177. gs_c_param_list_release(&pparam->value.d);
  178. break;
  179. case gs_param_type_string:
  180. case gs_param_type_name:
  181. case gs_param_type_int_array:
  182. case gs_param_type_float_array:
  183. case gs_param_type_string_array:
  184. case gs_param_type_name_array:
  185. if (!pparam->value.s.persistent)
  186. gs_free_const_object(mem, pparam->value.s.data,
  187. "gs_c_param_list_release data");
  188. break;
  189. default:
  190. break;
  191. }
  192. if (pparam->free_key) {
  193. /* We allocated this, so we must free it. */
  194. gs_free_const_string(mem, pparam->key.data, pparam->key.size,
  195. "gs_c_param_list_release key");
  196. }
  197. gs_free_object(mem, pparam->alternate_typed_data,
  198. "gs_c_param_list_release alternate data");
  199. gs_free_object(mem, pparam,
  200. "gs_c_param_list_release entry");
  201. plist->head = next;
  202. plist->count--;
  203. }
  204. }
  205. /* Add an entry to a list. Doesn't set: value, type, plist->head. */
  206. private gs_c_param *
  207. c_param_add(gs_c_param_list * plist, gs_param_name pkey)
  208. {
  209. gs_c_param *pparam =
  210. gs_alloc_struct(plist->memory, gs_c_param, &st_c_param,
  211. "c_param_add entry");
  212. uint len = strlen(pkey);
  213. if (pparam == 0)
  214. return 0;
  215. pparam->next = plist->head;
  216. if (!plist->persistent_keys) {
  217. /* We must copy the key. */
  218. byte *str = gs_alloc_string(plist->memory, len, "c_param_add key");
  219. if (str == 0) {
  220. gs_free_object(plist->memory, pparam, "c_param_add entry");
  221. return 0;
  222. }
  223. memcpy(str, pkey, len);
  224. pparam->key.data = str;
  225. pparam->key.persistent = false; /* we will free it */
  226. pparam->free_key = true;
  227. } else {
  228. pparam->key.data = (const byte *)pkey;
  229. pparam->key.persistent = true;
  230. pparam->free_key = false;
  231. }
  232. pparam->key.size = len;
  233. pparam->alternate_typed_data = 0;
  234. return pparam;
  235. }
  236. /* Write a dynamically typed parameter to a list. */
  237. private int
  238. c_param_write(gs_c_param_list * plist, gs_param_name pkey, void *pvalue,
  239. gs_param_type type)
  240. {
  241. unsigned top_level_sizeof = 0;
  242. unsigned second_level_sizeof = 0;
  243. gs_c_param *pparam = c_param_add(plist, pkey);
  244. if (pparam == 0)
  245. return_error(gs_error_VMerror);
  246. memcpy(&pparam->value, pvalue, gs_param_type_sizes[(int)type]);
  247. pparam->type = type;
  248. /* Need deeper copies of data if it's not persistent */
  249. switch (type) {
  250. gs_param_string const *curr_string;
  251. gs_param_string const *end_string;
  252. case gs_param_type_string_array:
  253. case gs_param_type_name_array:
  254. /* Determine how much mem needed to hold actual string data */
  255. curr_string = pparam->value.sa.data;
  256. end_string = curr_string + pparam->value.sa.size;
  257. for (; curr_string < end_string; ++curr_string)
  258. if (!curr_string->persistent)
  259. second_level_sizeof += curr_string->size;
  260. /* fall thru */
  261. case gs_param_type_string:
  262. case gs_param_type_name:
  263. case gs_param_type_int_array:
  264. case gs_param_type_float_array:
  265. if (!pparam->value.s.persistent) { /* Allocate & copy object pointed to by array or string */
  266. byte *top_level_memory = NULL;
  267. top_level_sizeof =
  268. pparam->value.s.size * gs_param_type_base_sizes[type];
  269. if (top_level_sizeof + second_level_sizeof > 0) {
  270. top_level_memory =
  271. gs_alloc_bytes_immovable(plist->memory,
  272. top_level_sizeof + second_level_sizeof,
  273. "c_param_write data");
  274. if (top_level_memory == 0) {
  275. gs_free_object(plist->memory, pparam, "c_param_write entry");
  276. return_error(gs_error_VMerror);
  277. }
  278. memcpy(top_level_memory, pparam->value.s.data, top_level_sizeof);
  279. }
  280. pparam->value.s.data = top_level_memory;
  281. /* String/name arrays need to copy actual str data */
  282. if (second_level_sizeof > 0) {
  283. byte *second_level_memory =
  284. top_level_memory + top_level_sizeof;
  285. curr_string = pparam->value.sa.data;
  286. end_string = curr_string + pparam->value.sa.size;
  287. for (; curr_string < end_string; ++curr_string)
  288. if (!curr_string->persistent) {
  289. memcpy(second_level_memory,
  290. curr_string->data, curr_string->size);
  291. ((gs_param_string *) curr_string)->data
  292. = second_level_memory;
  293. second_level_memory += curr_string->size;
  294. }
  295. }
  296. }
  297. break;
  298. default:
  299. break;
  300. }
  301. plist->head = pparam;
  302. plist->count++;
  303. return 0;
  304. }
  305. /* Individual writing routines. */
  306. private int
  307. c_param_begin_write_collection(gs_param_list * plist, gs_param_name pkey,
  308. gs_param_dict * pvalue, gs_param_collection_type_t coll_type)
  309. {
  310. gs_c_param_list *const cplist = (gs_c_param_list *)plist;
  311. gs_c_param_list *dlist =
  312. gs_c_param_list_alloc(cplist->memory,
  313. "c_param_begin_write_collection");
  314. if (dlist == 0)
  315. return_error(gs_error_VMerror);
  316. gs_c_param_list_write(dlist, cplist->memory);
  317. dlist->coll_type = coll_type;
  318. pvalue->list = (gs_param_list *) dlist;
  319. return 0;
  320. }
  321. private int
  322. c_param_end_write_collection(gs_param_list * plist, gs_param_name pkey,
  323. gs_param_dict * pvalue)
  324. {
  325. gs_c_param_list *const cplist = (gs_c_param_list *)plist;
  326. gs_c_param_list *dlist = (gs_c_param_list *) pvalue->list;
  327. return c_param_write(cplist, pkey, pvalue->list,
  328. (dlist->coll_type == gs_param_collection_dict_int_keys ?
  329. gs_param_type_dict_int_keys :
  330. dlist->coll_type == gs_param_collection_array ?
  331. gs_param_type_array : gs_param_type_dict));
  332. }
  333. private int
  334. c_param_write_typed(gs_param_list * plist, gs_param_name pkey,
  335. gs_param_typed_value * pvalue)
  336. {
  337. gs_c_param_list *const cplist = (gs_c_param_list *)plist;
  338. gs_param_collection_type_t coll_type;
  339. switch (pvalue->type) {
  340. case gs_param_type_dict:
  341. coll_type = gs_param_collection_dict_any;
  342. break;
  343. case gs_param_type_dict_int_keys:
  344. coll_type = gs_param_collection_dict_int_keys;
  345. break;
  346. case gs_param_type_array:
  347. coll_type = gs_param_collection_array;
  348. break;
  349. default:
  350. return c_param_write(cplist, pkey, &pvalue->value, pvalue->type);
  351. }
  352. return c_param_begin_write_collection
  353. (plist, pkey, &pvalue->value.d, coll_type);
  354. }
  355. /* Other procedures */
  356. private int
  357. c_param_request(gs_param_list * plist, gs_param_name pkey)
  358. {
  359. gs_c_param_list *const cplist = (gs_c_param_list *)plist;
  360. gs_c_param *pparam;
  361. cplist->any_requested = true;
  362. if (c_param_find(cplist, pkey, true))
  363. return 0;
  364. pparam = c_param_add(cplist, pkey);
  365. if (pparam == 0)
  366. return_error(gs_error_VMerror);
  367. pparam->type = gs_param_type_any; /* mark as undefined */
  368. cplist->head = pparam;
  369. return 0;
  370. }
  371. private int
  372. c_param_requested(const gs_param_list * plist, gs_param_name pkey)
  373. {
  374. const gs_c_param_list *const cplist = (const gs_c_param_list *)plist;
  375. gs_param_list *target = cplist->target;
  376. int code;
  377. if (!cplist->any_requested)
  378. return (target ? param_requested(target, pkey) : -1);
  379. if (c_param_find(cplist, pkey, true) != 0)
  380. return 1;
  381. if (!target)
  382. return 0;
  383. code = param_requested(target, pkey);
  384. return (code < 0 ? 0 : 1);
  385. }
  386. /* ---------------- Reading from a list to parameters ---------------- */
  387. private param_proc_begin_xmit_collection(c_param_begin_read_collection);
  388. private param_proc_end_xmit_collection(c_param_end_read_collection);
  389. private param_proc_xmit_typed(c_param_read_typed);
  390. private param_proc_next_key(c_param_get_next_key);
  391. private param_proc_get_policy(c_param_read_get_policy);
  392. private param_proc_signal_error(c_param_read_signal_error);
  393. private param_proc_commit(c_param_read_commit);
  394. private const gs_param_list_procs c_read_procs =
  395. {
  396. c_param_read_typed,
  397. c_param_begin_read_collection,
  398. c_param_end_read_collection,
  399. c_param_get_next_key,
  400. NULL, /* request, N/A */
  401. NULL, /* requested, N/A */
  402. c_param_read_get_policy,
  403. c_param_read_signal_error,
  404. c_param_read_commit
  405. };
  406. /* Switch a list from writing to reading. */
  407. void
  408. gs_c_param_list_read(gs_c_param_list * plist)
  409. {
  410. plist->procs = &c_read_procs;
  411. }
  412. /* Generic routine for reading a parameter from a list. */
  413. private int
  414. c_param_read_typed(gs_param_list * plist, gs_param_name pkey,
  415. gs_param_typed_value * pvalue)
  416. {
  417. gs_c_param_list *const cplist = (gs_c_param_list *)plist;
  418. gs_param_type req_type = pvalue->type;
  419. gs_c_param *pparam = c_param_find(cplist, pkey, false);
  420. int code;
  421. if (pparam == 0)
  422. return (cplist->target ?
  423. param_read_typed(cplist->target, pkey, pvalue) : 1);
  424. pvalue->type = pparam->type;
  425. switch (pvalue->type) {
  426. case gs_param_type_dict:
  427. case gs_param_type_dict_int_keys:
  428. case gs_param_type_array:
  429. gs_c_param_list_read(&pparam->value.d);
  430. pvalue->value.d.list = (gs_param_list *) & pparam->value.d;
  431. pvalue->value.d.size = pparam->value.d.count;
  432. return 0;
  433. default:
  434. break;
  435. }
  436. memcpy(&pvalue->value, &pparam->value,
  437. gs_param_type_sizes[(int)pparam->type]);
  438. code = param_coerce_typed(pvalue, req_type, NULL);
  439. /****** SHOULD LET param_coerce_typed DO THIS ******/
  440. if (code == gs_error_typecheck &&
  441. req_type == gs_param_type_float_array &&
  442. pvalue->type == gs_param_type_int_array
  443. ) {
  444. /* Convert int array to float dest */
  445. gs_param_float_array fa;
  446. int element;
  447. fa.size = pparam->value.ia.size;
  448. fa.persistent = false;
  449. if (pparam->alternate_typed_data == 0) {
  450. if ((pparam->alternate_typed_data
  451. = (void *)gs_alloc_bytes_immovable(cplist->memory,
  452. fa.size * sizeof(float),
  453. "gs_c_param_read alternate float array")) == 0)
  454. return_error(gs_error_VMerror);
  455. for (element = 0; element < fa.size; ++element)
  456. ((float *)(pparam->alternate_typed_data))[element]
  457. = (float)pparam->value.ia.data[element];
  458. }
  459. fa.data = (float *)pparam->alternate_typed_data;
  460. pvalue->value.fa = fa;
  461. return 0;
  462. }
  463. return code;
  464. }
  465. /* Individual reading routines. */
  466. private int
  467. c_param_begin_read_collection(gs_param_list * plist, gs_param_name pkey,
  468. gs_param_dict * pvalue, gs_param_collection_type_t coll_type)
  469. {
  470. gs_c_param_list *const cplist = (gs_c_param_list *)plist;
  471. gs_c_param *pparam = c_param_find(cplist, pkey, false);
  472. if (pparam == 0)
  473. return
  474. (cplist->target ?
  475. param_begin_read_collection(cplist->target,
  476. pkey, pvalue, coll_type) :
  477. 1);
  478. switch (pparam->type) {
  479. case gs_param_type_dict:
  480. if (coll_type != gs_param_collection_dict_any)
  481. return_error(gs_error_typecheck);
  482. break;
  483. case gs_param_type_dict_int_keys:
  484. if (coll_type == gs_param_collection_array)
  485. return_error(gs_error_typecheck);
  486. break;
  487. case gs_param_type_array:
  488. break;
  489. default:
  490. return_error(gs_error_typecheck);
  491. }
  492. gs_c_param_list_read(&pparam->value.d);
  493. pvalue->list = (gs_param_list *) & pparam->value.d;
  494. pvalue->size = pparam->value.d.count;
  495. return 0;
  496. }
  497. private int
  498. c_param_end_read_collection(gs_param_list * plist, gs_param_name pkey,
  499. gs_param_dict * pvalue)
  500. {
  501. return 0;
  502. }
  503. /* Other procedures */
  504. private int /* ret 0 ok, 1 if EOF, or -ve err */
  505. c_param_get_next_key(gs_param_list * plist, gs_param_enumerator_t * penum,
  506. gs_param_key_t * key)
  507. {
  508. gs_c_param_list *const cplist = (gs_c_param_list *)plist;
  509. gs_c_param *pparam =
  510. (penum->pvoid ? ((gs_c_param *) (penum->pvoid))->next :
  511. cplist->head);
  512. if (pparam == 0)
  513. return 1;
  514. penum->pvoid = pparam;
  515. *key = pparam->key;
  516. return 0;
  517. }
  518. private int
  519. c_param_read_get_policy(gs_param_list * plist, gs_param_name pkey)
  520. {
  521. return gs_param_policy_ignore;
  522. }
  523. private int
  524. c_param_read_signal_error(gs_param_list * plist, gs_param_name pkey, int code)
  525. {
  526. return code;
  527. }
  528. private int
  529. c_param_read_commit(gs_param_list * plist)
  530. {
  531. return 0;
  532. }