gsparam2.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* Copyright (C) 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: gsparam2.c,v 1.6 2002/07/01 14:27:43 jeong Exp $ */
  14. /* Serialize and unserialize parameter lists */
  15. /* Initial version 2/1/98 by John Desrosiers (soho@crl.com) */
  16. /* 8/8/98 L. Peter Deutsch (ghost@aladdin.com) Rewritten to use streams */
  17. #include "gx.h"
  18. #include "memory_.h"
  19. #include "gserrors.h"
  20. #include "gsparams.h"
  21. #define MAX_PARAM_KEY 255
  22. /* ---------------- Serializer ---------------- */
  23. /* Forward references */
  24. private int sput_word(stream *dest, uint value);
  25. private int sput_bytes(stream *dest, const byte *data, uint size);
  26. /*
  27. * Serialize the contents of a gs_param_list, including sub-dictionaries,
  28. * onto a stream. The list must be in READ mode.
  29. */
  30. int
  31. gs_param_list_puts(stream *dest, gs_param_list *list)
  32. {
  33. int code = 0;
  34. gs_param_enumerator_t key_enum;
  35. gs_param_key_t key;
  36. param_init_enumerator(&key_enum);
  37. /* Each item is serialized as ("word" means compressed word):
  38. * word: key sizeof + 1, or 0 if end of list/dict
  39. * word: data type(gs_param_type_xxx)
  40. * byte[]: key, including trailing \0
  41. * (if simple type)
  42. * byte[]: unpacked representation of data
  43. * (if simple array or string)
  44. * word: # of elements
  45. * byte[]: data associated with array contents
  46. * (if string/name array)
  47. * word: # of elements
  48. * { word: length of string
  49. * byte[]: string data
  50. * } for each string in array
  51. * (if dict/dict_int_keys/hetero_array)
  52. * word: # of entries in collection
  53. * entries follow immediately
  54. */
  55. /* Enumerate all the keys; use keys to get their typed values */
  56. while ((code = param_get_next_key(list, &key_enum, &key)) == 0) {
  57. int value_top_sizeof;
  58. int value_base_sizeof;
  59. const void *data;
  60. uint size;
  61. /* Get next datum & put its type & key to stream */
  62. gs_param_typed_value value;
  63. char string_key[MAX_PARAM_KEY + 1];
  64. if (sizeof(string_key) < key.size + 1) {
  65. code = gs_note_error(gs_error_rangecheck);
  66. break;
  67. }
  68. memcpy(string_key, key.data, key.size);
  69. string_key[key.size] = 0;
  70. if ((code = param_read_typed(list, string_key, &value)) != 0) {
  71. if (code > 0)
  72. code = gs_note_error(gs_error_unknownerror);
  73. break;
  74. }
  75. sput_word(dest, (unsigned)key.size + 1);
  76. sput_word(dest, (unsigned)value.type);
  77. sput_bytes(dest, (byte *)string_key, key.size + 1);
  78. /* Put value & its size to stream */
  79. value_top_sizeof = gs_param_type_sizes[value.type];
  80. value_base_sizeof = gs_param_type_base_sizes[value.type];
  81. switch (value.type) {
  82. case gs_param_type_bool:
  83. case gs_param_type_int:
  84. case gs_param_type_long:
  85. case gs_param_type_float:
  86. sput_bytes(dest, (byte *)&value.value, value_top_sizeof);
  87. case gs_param_type_null:
  88. break;
  89. case gs_param_type_string:
  90. data = value.value.s.data, size = value.value.s.size;
  91. goto scalar_array;
  92. case gs_param_type_name:
  93. data = value.value.n.data, size = value.value.n.size;
  94. goto scalar_array;
  95. case gs_param_type_int_array:
  96. data = value.value.ia.data, size = value.value.ia.size;
  97. goto scalar_array;
  98. case gs_param_type_float_array:
  99. data = value.value.fa.data, size = value.value.fa.size;
  100. scalar_array: sput_word(dest, size);
  101. sput_bytes(dest, data, value_base_sizeof * size);
  102. break;
  103. case gs_param_type_string_array:
  104. data = value.value.sa.data, size = value.value.sa.size;
  105. goto string_array;
  106. case gs_param_type_name_array:
  107. data = value.value.na.data, size = value.value.na.size;
  108. string_array: sput_word(dest, size);
  109. {
  110. uint count;
  111. const gs_param_string *sa;
  112. for (count = size, sa = data; count-- > 0; ++sa) {
  113. sput_word(dest, sa->size);
  114. sput_bytes(dest, sa->data, sa->size);
  115. }
  116. }
  117. break;
  118. case gs_param_type_dict:
  119. case gs_param_type_dict_int_keys:
  120. case gs_param_type_array:
  121. sput_word(dest, value.value.d.size);
  122. code = gs_param_list_puts(dest, value.value.d.list);
  123. {
  124. int end_code =
  125. param_end_read_dict(list, key.data, &value.value.d);
  126. if (code >= 0)
  127. code = end_code;
  128. }
  129. break;
  130. default:
  131. code = gs_note_error(gs_error_unknownerror);
  132. break;
  133. }
  134. if (code < 0)
  135. break;
  136. }
  137. /* Write end marker, which is an (illegal) 0 key length */
  138. return (code < 0 ? code : sput_word(dest, 0));
  139. }
  140. /* Put a variable-length value on a stream. */
  141. private int
  142. sput_word(stream *dest, uint value)
  143. {
  144. int code = 0;
  145. do {
  146. byte chunk = value & 0x7f;
  147. if ((value >>= 7) != 0)
  148. chunk |= 0x80;
  149. if ((code = sputc(dest, chunk)) < 0)
  150. break;
  151. }
  152. while (value != 0);
  153. return code;
  154. }
  155. /* Put bytes on a stream. */
  156. private int
  157. sput_bytes(stream *dest, const byte *data, uint size)
  158. {
  159. uint ignore_count;
  160. return sputs(dest, data, size, &ignore_count);
  161. }
  162. /* ---------------- Unserializer ---------------- */
  163. /* Forward references */
  164. private int sget_word(stream *src, uint *pvalue);
  165. private int sget_bytes(stream *src, byte *data, uint size);
  166. /*
  167. * Unserialize a parameter list from a stream. The list must be in WRITE
  168. * mode.
  169. */
  170. int
  171. gs_param_list_gets(stream *src, gs_param_list *list, gs_memory_t *mem)
  172. {
  173. int code = 0;
  174. do {
  175. gs_param_typed_value typed;
  176. uint key_sizeof;
  177. int value_top_sizeof;
  178. int value_base_sizeof;
  179. uint temp;
  180. void *data;
  181. uint size;
  182. gs_param_type type;
  183. char string_key[MAX_PARAM_KEY + 1];
  184. /* key length 0 indicates end of data */
  185. if ((code = sget_word(src, &key_sizeof)) < 0 ||
  186. key_sizeof == 0 ||
  187. /* data type */
  188. (code = sget_word(src, &temp)) < 0)
  189. break;
  190. if (key_sizeof > sizeof(string_key)) {
  191. code = gs_note_error(gs_error_rangecheck);
  192. break;
  193. }
  194. /* key */
  195. code = sget_bytes(src, (byte *)string_key, key_sizeof);
  196. if (code < 0)
  197. break;
  198. /* Data values */
  199. type = (gs_param_type)temp;
  200. value_top_sizeof = gs_param_type_sizes[type];
  201. value_base_sizeof = gs_param_type_base_sizes[type];
  202. typed.type = type;
  203. switch (type) {
  204. case gs_param_type_bool:
  205. case gs_param_type_int:
  206. case gs_param_type_long:
  207. case gs_param_type_float:
  208. code = sget_bytes(src, (byte *)&typed.value, value_top_sizeof);
  209. case gs_param_type_null:
  210. goto put;
  211. default:
  212. ;
  213. }
  214. /* All other data values start with a size. */
  215. code = sget_word(src, &size);
  216. if (code < 0)
  217. break;
  218. switch (type) {
  219. case gs_param_type_string:
  220. case gs_param_type_name:
  221. case gs_param_type_int_array:
  222. case gs_param_type_float_array:
  223. data =
  224. (value_base_sizeof == 1 ?
  225. gs_alloc_string(mem, size, "param string/name") :
  226. gs_alloc_byte_array(mem, size, value_base_sizeof,
  227. "param scalar array"));
  228. if (data == 0) {
  229. code = gs_note_error(gs_error_VMerror);
  230. break;
  231. }
  232. typed.value.s.data = data;
  233. typed.value.s.persistent = false;
  234. typed.value.s.size = size;
  235. code = sget_bytes(src, data, size * value_base_sizeof);
  236. break;
  237. case gs_param_type_string_array:
  238. case gs_param_type_name_array:
  239. /****** SHOULD BE STRUCT ARRAY ******/
  240. data = gs_alloc_byte_array(mem, size, value_top_sizeof,
  241. "param string/name array");
  242. if (data == 0) {
  243. code = gs_note_error(gs_error_VMerror);
  244. break;
  245. }
  246. typed.value.sa.data = data;
  247. typed.value.sa.persistent = false;
  248. typed.value.sa.size = size;
  249. {
  250. gs_param_string *sa = data;
  251. byte *str_data;
  252. uint index, str_size;
  253. /* Clean pointers in case we bail out. */
  254. for (index = 0; index < size; ++index)
  255. sa[index].data = 0, sa[index].size = 0;
  256. for (index = 0; index < size; ++index, ++sa) {
  257. code = sget_word(src, &str_size);
  258. if (code < 0)
  259. break;
  260. str_data = gs_alloc_string(mem, str_size,
  261. "param string/name element");
  262. if (str_data == 0) {
  263. code = gs_note_error(gs_error_VMerror);
  264. break;
  265. }
  266. code = sget_bytes(src, str_data, str_size);
  267. if (code < 0)
  268. break;
  269. }
  270. }
  271. break;
  272. case gs_param_type_dict:
  273. case gs_param_type_dict_int_keys:
  274. case gs_param_type_array:
  275. typed.value.d.size = size;
  276. code = param_begin_write_collection
  277. (list, string_key, &typed.value.d,
  278. type - gs_param_type_dict);
  279. if (code < 0)
  280. break;
  281. code = gs_param_list_gets(src, typed.value.d.list, mem);
  282. {
  283. int end_code =
  284. param_end_write_collection(list, string_key,
  285. &typed.value.d);
  286. if (code >= 0)
  287. code = end_code;
  288. }
  289. break;
  290. default:
  291. code = gs_note_error(gs_error_unknownerror);
  292. break;
  293. }
  294. put: if (code < 0)
  295. break;
  296. if (typed.type != gs_param_type_dict &&
  297. typed.type != gs_param_type_dict_int_keys &&
  298. typed.type != gs_param_type_array
  299. )
  300. code = param_write_typed(list, string_key, &typed);
  301. }
  302. while (code >= 0);
  303. return code;
  304. }
  305. /* ---------- Utility functions -------- */
  306. /* Get a value stored with sput_word */
  307. private int
  308. sget_word(stream *src, uint *pvalue)
  309. {
  310. uint value = 0;
  311. int chunk;
  312. uint shift = 0;
  313. do {
  314. chunk = sgetc(src);
  315. if (chunk < 0)
  316. return chunk;
  317. value |= (chunk & 0x7f) << shift;
  318. shift += 7;
  319. }
  320. while (chunk & 0x80);
  321. *pvalue = value;
  322. return 0;
  323. }
  324. /* Get bytes from a stream */
  325. private int
  326. sget_bytes(stream *src, byte *data, uint size)
  327. {
  328. uint ignore_count;
  329. int status = sgets(src, data, size, &ignore_count);
  330. if (status < 0 && status != EOFC)
  331. return_error(gs_error_ioerror);
  332. };
  333. return 0;
  334. }