sstring.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* Copyright (C) 1993, 1995, 1996, 1997, 1998, 1999 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: sstring.c,v 1.5 2005/04/25 12:28:49 igor Exp $ */
  14. /* String and hexstring streams (filters) */
  15. #include "stdio_.h" /* includes std.h */
  16. #include "memory_.h"
  17. #include "string_.h"
  18. #include "strimpl.h"
  19. #include "sstring.h"
  20. #include "scanchar.h"
  21. /* ------ ASCIIHexEncode ------ */
  22. private_st_AXE_state();
  23. /* Initialize the state */
  24. private int
  25. s_AXE_init(stream_state * st)
  26. {
  27. stream_AXE_state *const ss = (stream_AXE_state *) st;
  28. return s_AXE_init_inline(ss);
  29. }
  30. /* Process a buffer */
  31. private int
  32. s_AXE_process(stream_state * st, stream_cursor_read * pr,
  33. stream_cursor_write * pw, bool last)
  34. {
  35. stream_AXE_state *const ss = (stream_AXE_state *) st;
  36. const byte *p = pr->ptr;
  37. byte *q = pw->ptr;
  38. int rcount = pr->limit - p;
  39. int wcount = pw->limit - q;
  40. int count;
  41. int pos = ss->count;
  42. const char *hex_digits = "0123456789ABCDEF";
  43. int status = 0;
  44. if (last && ss->EndOfData)
  45. wcount--; /* leave room for '>' */
  46. wcount -= (wcount + 64) / 65; /* leave room for \n */
  47. wcount >>= 1; /* 2 chars per input byte */
  48. count = (wcount < rcount ? (status = 1, wcount) : rcount);
  49. while (--count >= 0) {
  50. *++q = hex_digits[*++p >> 4];
  51. *++q = hex_digits[*p & 0xf];
  52. if (!(++pos & 31) && (count != 0 || !last))
  53. *++q = '\n';
  54. }
  55. if (last && status == 0 && ss->EndOfData)
  56. *++q = '>';
  57. pr->ptr = p;
  58. pw->ptr = q;
  59. ss->count = pos & 31;
  60. return status;
  61. }
  62. /* Stream template */
  63. const stream_template s_AXE_template =
  64. {&st_AXE_state, s_AXE_init, s_AXE_process, 1, 3
  65. };
  66. /* ------ ASCIIHexDecode ------ */
  67. private_st_AXD_state();
  68. /* Initialize the state */
  69. private int
  70. s_AXD_init(stream_state * st)
  71. {
  72. stream_AXD_state *const ss = (stream_AXD_state *) st;
  73. return s_AXD_init_inline(ss);
  74. }
  75. /* Process a buffer */
  76. private int
  77. s_AXD_process(stream_state * st, stream_cursor_read * pr,
  78. stream_cursor_write * pw, bool last)
  79. {
  80. stream_AXD_state *const ss = (stream_AXD_state *) st;
  81. int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace);
  82. switch (code) {
  83. case 0:
  84. if (ss->odd >= 0 && last) {
  85. if (pw->ptr == pw->limit)
  86. return 1;
  87. *++(pw->ptr) = ss->odd << 4;
  88. }
  89. /* falls through */
  90. case 1:
  91. /* We still need to read ahead and check for EOD. */
  92. for (; pr->ptr < pr->limit; pr->ptr++)
  93. if (scan_char_decoder[pr->ptr[1]] != ctype_space) {
  94. if (pr->ptr[1] == '>') {
  95. pr->ptr++;
  96. goto eod;
  97. }
  98. return 1;
  99. }
  100. return 0; /* still need to scan ahead */
  101. default:
  102. return code;
  103. case ERRC:
  104. ;
  105. }
  106. /*
  107. * Check for EOD. ERRC implies at least one more character
  108. * was read; we must unread it, since the caller might have
  109. * invoked the filter with exactly the right count to read all
  110. * the available data, and we might be reading past the end.
  111. */
  112. if (*pr->ptr != '>') { /* EOD */
  113. --(pr->ptr);
  114. return ERRC;
  115. }
  116. eod:if (ss->odd >= 0) {
  117. if (pw->ptr == pw->limit)
  118. return 1;
  119. *++(pw->ptr) = ss->odd << 4;
  120. }
  121. return EOFC;
  122. }
  123. /* Stream template */
  124. const stream_template s_AXD_template =
  125. {&st_AXD_state, s_AXD_init, s_AXD_process, 2, 1
  126. };
  127. /* ------ PSStringEncode ------ */
  128. /* Process a buffer */
  129. private int
  130. s_PSSE_process(stream_state * st, stream_cursor_read * pr,
  131. stream_cursor_write * pw, bool last)
  132. {
  133. const byte *p = pr->ptr;
  134. const byte *rlimit = pr->limit;
  135. byte *q = pw->ptr;
  136. byte *wlimit = pw->limit;
  137. int status = 0;
  138. /* This doesn't have to be very efficient. */
  139. while (p < rlimit) {
  140. int c = *++p;
  141. if (c < 32 || c >= 127) {
  142. const char *pesc;
  143. const char *const esc = "\n\r\t\b\f";
  144. if (c < 32 && c != 0 && (pesc = strchr(esc, c)) != 0) {
  145. if (wlimit - q < 2) {
  146. --p;
  147. status = 1;
  148. break;
  149. }
  150. *++q = '\\';
  151. *++q = "nrtbf"[pesc - esc];
  152. continue;
  153. }
  154. if (wlimit - q < 4) {
  155. --p;
  156. status = 1;
  157. break;
  158. }
  159. q[1] = '\\';
  160. q[2] = (c >> 6) + '0';
  161. q[3] = ((c >> 3) & 7) + '0';
  162. q[4] = (c & 7) + '0';
  163. q += 4;
  164. continue;
  165. } else if (c == '(' || c == ')' || c == '\\') {
  166. if (wlimit - q < 2) {
  167. --p;
  168. status = 1;
  169. break;
  170. }
  171. *++q = '\\';
  172. } else {
  173. if (q == wlimit) {
  174. --p;
  175. status = 1;
  176. break;
  177. }
  178. }
  179. *++q = c;
  180. }
  181. if (last && status == 0) {
  182. if (q == wlimit)
  183. status = 1;
  184. else
  185. *++q = ')';
  186. }
  187. pr->ptr = p;
  188. pw->ptr = q;
  189. return status;
  190. }
  191. /* Stream template */
  192. const stream_template s_PSSE_template =
  193. {&st_stream_state, NULL, s_PSSE_process, 1, 4
  194. };
  195. /* ------ PSStringDecode ------ */
  196. private_st_PSSD_state();
  197. /* Initialize the state */
  198. int
  199. s_PSSD_init(stream_state * st)
  200. {
  201. stream_PSSD_state *const ss = (stream_PSSD_state *) st;
  202. ss->from_string = false;
  203. return s_PSSD_partially_init_inline(ss);
  204. }
  205. /* Process a buffer */
  206. private int
  207. s_PSSD_process(stream_state * st, stream_cursor_read * pr,
  208. stream_cursor_write * pw, bool last)
  209. {
  210. stream_PSSD_state *const ss = (stream_PSSD_state *) st;
  211. const byte *p = pr->ptr;
  212. const byte *rlimit = pr->limit;
  213. byte *q = pw->ptr;
  214. byte *wlimit = pw->limit;
  215. int status = 0;
  216. int c;
  217. #define check_p(n)\
  218. if ( p == rlimit ) { p -= n; goto out; }
  219. #define check_q(n)\
  220. if ( q == wlimit ) { p -= n; status = 1; goto out; }
  221. while (p < rlimit) {
  222. c = *++p;
  223. if (c == '\\' && !ss->from_string) {
  224. check_p(1);
  225. switch ((c = *++p)) {
  226. case 'n':
  227. c = '\n';
  228. goto put;
  229. case 'r':
  230. c = '\r';
  231. goto put;
  232. case 't':
  233. c = '\t';
  234. goto put;
  235. case 'b':
  236. c = '\b';
  237. goto put;
  238. case 'f':
  239. c = '\f';
  240. goto put;
  241. default: /* ignore the \ */
  242. put:check_q(2);
  243. *++q = c;
  244. continue;
  245. case char_CR: /* ignore, check for following \n */
  246. check_p(2);
  247. if (p[1] == char_EOL)
  248. p++;
  249. continue;
  250. case char_EOL: /* ignore */
  251. continue;
  252. case '0':
  253. case '1':
  254. case '2':
  255. case '3':
  256. case '4':
  257. case '5':
  258. case '6':
  259. case '7':
  260. {
  261. int d;
  262. check_p(2);
  263. d = p[1];
  264. c -= '0';
  265. if (d >= '0' && d <= '7') {
  266. if (p + 1 == rlimit) {
  267. p -= 2;
  268. goto out;
  269. }
  270. check_q(2);
  271. c = (c << 3) + d - '0';
  272. d = p[2];
  273. if (d >= '0' && d <= '7') {
  274. c = (c << 3) + d - '0';
  275. p += 2;
  276. } else
  277. p++;
  278. } else
  279. check_q(2);
  280. *++q = c;
  281. continue;
  282. }
  283. }
  284. } else
  285. switch (c) {
  286. case '(':
  287. check_q(1);
  288. ss->depth++;
  289. break;
  290. case ')':
  291. if (ss->depth == 0) {
  292. status = EOFC;
  293. goto out;
  294. }
  295. check_q(1);
  296. ss->depth--;
  297. break;
  298. case char_CR: /* convert to \n */
  299. check_p(1);
  300. check_q(1);
  301. if (p[1] == char_EOL)
  302. p++;
  303. *++q = '\n';
  304. continue;
  305. case char_EOL:
  306. c = '\n';
  307. default:
  308. check_q(1);
  309. break;
  310. }
  311. *++q = c;
  312. }
  313. #undef check_p
  314. #undef check_q
  315. out:pr->ptr = p;
  316. pw->ptr = q;
  317. if (last && status == 0 && p != rlimit)
  318. status = ERRC;
  319. return status;
  320. }
  321. /* Stream template */
  322. const stream_template s_PSSD_template =
  323. {&st_PSSD_state, s_PSSD_init, s_PSSD_process, 4, 1
  324. };
  325. /* ------ Utilities ------ */
  326. /*
  327. * Convert hex data to binary. Return 1 if we filled the string, 0 if
  328. * we ran out of input data before filling the string, or ERRC on error.
  329. * The caller must set *odd_digit to -1 before the first call;
  330. * after each call, if an odd number of hex digits has been read (total),
  331. * *odd_digit is the odd digit value, otherwise *odd_digit = -1.
  332. * See strimpl.h for the definition of syntax.
  333. */
  334. int
  335. s_hex_process(stream_cursor_read * pr, stream_cursor_write * pw,
  336. int *odd_digit, hex_syntax syntax)
  337. {
  338. const byte *p = pr->ptr;
  339. const byte *rlimit = pr->limit;
  340. byte *q = pw->ptr;
  341. byte *wlimit = pw->limit;
  342. byte *q0 = q;
  343. byte val1 = (byte) * odd_digit;
  344. byte val2;
  345. uint rcount;
  346. byte *flimit;
  347. const byte *const decoder = scan_char_decoder;
  348. int code = 0;
  349. if (q >= wlimit)
  350. return 1;
  351. if (val1 <= 0xf)
  352. goto d2;
  353. d1:if ((rcount = (rlimit - p) >> 1) == 0)
  354. goto x1;
  355. /* Set up a fast end-of-loop check, so we don't have to test */
  356. /* both p and q against their respective limits. */
  357. flimit = (rcount < wlimit - q ? q + rcount : wlimit);
  358. f1:if ((val1 = decoder[p[1]]) <= 0xf &&
  359. (val2 = decoder[p[2]]) <= 0xf
  360. ) {
  361. p += 2;
  362. *++q = (val1 << 4) + val2;
  363. if (q < flimit)
  364. goto f1;
  365. if (q >= wlimit)
  366. goto px;
  367. }
  368. x1:if (p >= rlimit)
  369. goto end1;
  370. if ((val1 = decoder[*++p]) > 0xf) {
  371. if (val1 == ctype_space) {
  372. switch (syntax) {
  373. case hex_ignore_whitespace:
  374. goto x1;
  375. case hex_ignore_leading_whitespace:
  376. if (q == q0 && *odd_digit < 0)
  377. goto x1;
  378. --p;
  379. code = 1;
  380. goto end1;
  381. case hex_ignore_garbage:
  382. goto x1;
  383. }
  384. } else if (syntax == hex_ignore_garbage)
  385. goto x1;
  386. code = ERRC;
  387. goto end1;
  388. }
  389. d2:if (p >= rlimit) {
  390. *odd_digit = val1;
  391. goto ended;
  392. }
  393. if ((val2 = decoder[*++p]) > 0xf) {
  394. if (val2 == ctype_space)
  395. switch (syntax) {
  396. case hex_ignore_whitespace:
  397. goto d2;
  398. case hex_ignore_leading_whitespace:
  399. if (q == q0)
  400. goto d2;
  401. --p;
  402. *odd_digit = val1;
  403. code = 1;
  404. goto ended;
  405. case hex_ignore_garbage: /* pacify compilers */
  406. ;
  407. }
  408. if (syntax == hex_ignore_garbage)
  409. goto d2;
  410. *odd_digit = val1;
  411. code = ERRC;
  412. goto ended;
  413. }
  414. *++q = (val1 << 4) + val2;
  415. if (q < wlimit)
  416. goto d1;
  417. px:code = 1;
  418. end1:*odd_digit = -1;
  419. ended:pr->ptr = p;
  420. pw->ptr = q;
  421. return code;
  422. }