sbcp.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Copyright (C) 1993, 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: sbcp.c,v 1.3 2000/09/19 19:00:47 lpd Exp $ */
  16. /* BCP and TBCP filters */
  17. #include "stdio_.h"
  18. #include "strimpl.h"
  19. #include "sbcp.h"
  20. #define CtrlA 0x01
  21. #define CtrlC 0x03
  22. #define CtrlD 0x04
  23. #define CtrlE 0x05
  24. #define CtrlQ 0x11
  25. #define CtrlS 0x13
  26. #define CtrlT 0x14
  27. #define ESC 0x1b
  28. #define CtrlBksl 0x1c
  29. /* The following is not used yet. */
  30. /*private const char *TBCP_end_protocol_string = "\033%-12345X"; */
  31. /* ------ BCPEncode and TBCPEncode ------ */
  32. /* Process a buffer */
  33. private int
  34. s_xBCPE_process(stream_state * st, stream_cursor_read * pr,
  35. stream_cursor_write * pw, bool last, const byte * escaped)
  36. {
  37. const byte *p = pr->ptr;
  38. const byte *rlimit = pr->limit;
  39. uint rcount = rlimit - p;
  40. byte *q = pw->ptr;
  41. uint wcount = pw->limit - q;
  42. const byte *end = p + min(rcount, wcount);
  43. while (p < end) {
  44. byte ch = *++p;
  45. if (ch <= 31 && escaped[ch]) {
  46. if (p == rlimit) {
  47. p--;
  48. break;
  49. }
  50. *++q = CtrlA;
  51. ch ^= 0x40;
  52. if (--wcount < rcount)
  53. end--;
  54. }
  55. *++q = ch;
  56. }
  57. pr->ptr = p;
  58. pw->ptr = q;
  59. return (p == rlimit ? 0 : 1);
  60. }
  61. /* Actual process procedures */
  62. private int
  63. s_BCPE_process(stream_state * st, stream_cursor_read * pr,
  64. stream_cursor_write * pw, bool last)
  65. {
  66. static const byte escaped[32] =
  67. {
  68. 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  69. 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0
  70. };
  71. return s_xBCPE_process(st, pr, pw, last, escaped);
  72. }
  73. private int
  74. s_TBCPE_process(stream_state * st, stream_cursor_read * pr,
  75. stream_cursor_write * pw, bool last)
  76. {
  77. static const byte escaped[32] =
  78. {
  79. 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  80. 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0
  81. };
  82. return s_xBCPE_process(st, pr, pw, last, escaped);
  83. }
  84. /* Stream templates */
  85. const stream_template s_BCPE_template =
  86. {&st_stream_state, NULL, s_BCPE_process, 1, 2
  87. };
  88. const stream_template s_TBCPE_template =
  89. {&st_stream_state, NULL, s_TBCPE_process, 1, 2
  90. };
  91. /* ------ BCPDecode and TBCPDecode ------ */
  92. private_st_BCPD_state();
  93. /* Initialize the state */
  94. private int
  95. s_BCPD_init(stream_state * st)
  96. {
  97. stream_BCPD_state *const ss = (stream_BCPD_state *) st;
  98. ss->escaped = 0;
  99. ss->matched = ss->copy_count = 0;
  100. return 0;
  101. }
  102. /* Process a buffer */
  103. private int
  104. s_xBCPD_process(stream_state * st, stream_cursor_read * pr,
  105. stream_cursor_write * pw, bool last, bool tagged)
  106. {
  107. stream_BCPD_state *const ss = (stream_BCPD_state *) st;
  108. const byte *p = pr->ptr;
  109. const byte *rlimit = pr->limit;
  110. byte *q = pw->ptr;
  111. byte *wlimit = pw->limit;
  112. int copy_count = ss->copy_count;
  113. int status;
  114. bool escaped = ss->escaped;
  115. for (;;) {
  116. byte ch;
  117. if (copy_count) {
  118. if (q == wlimit) {
  119. status = (p < rlimit ? 1 : 0);
  120. break;
  121. }
  122. *++q = *++(ss->copy_ptr);
  123. copy_count--;
  124. continue;
  125. }
  126. if (p == rlimit) {
  127. status = 0;
  128. break;
  129. }
  130. ch = *++p;
  131. if (ch <= 31)
  132. switch (ch) {
  133. case CtrlA:
  134. if (escaped) {
  135. status = ERRC;
  136. goto out;
  137. }
  138. escaped = true;
  139. continue;
  140. case CtrlC:
  141. status = (*ss->signal_interrupt) (st);
  142. if (status < 0)
  143. goto out;
  144. continue;
  145. case CtrlD:
  146. if (escaped) {
  147. status = ERRC;
  148. goto out;
  149. }
  150. status = EOFC;
  151. goto out;
  152. case CtrlE:
  153. continue;
  154. case CtrlQ:
  155. continue;
  156. case CtrlS:
  157. continue;
  158. case CtrlT:
  159. status = (*ss->request_status) (st);
  160. if (status < 0)
  161. goto out;
  162. continue;
  163. case CtrlBksl:
  164. continue;
  165. }
  166. if (q == wlimit) {
  167. p--;
  168. status = 1;
  169. break;
  170. }
  171. if (escaped) {
  172. escaped = false;
  173. switch (ch) {
  174. case '[':
  175. if (!tagged) {
  176. status = ERRC;
  177. goto out;
  178. }
  179. /* falls through */
  180. case 'A':
  181. case 'C':
  182. case 'D':
  183. case 'E':
  184. case 'Q':
  185. case 'S':
  186. case 'T':
  187. case '\\':
  188. ch ^= 0x40;
  189. break;
  190. case 'M':
  191. if (!tagged) {
  192. status = ERRC;
  193. goto out;
  194. }
  195. continue;
  196. default:
  197. status = ERRC;
  198. goto out;
  199. }
  200. }
  201. *++q = ch;
  202. }
  203. out:ss->copy_count = copy_count;
  204. ss->escaped = escaped;
  205. pr->ptr = p;
  206. pw->ptr = q;
  207. return status;
  208. }
  209. /* Actual process procedures */
  210. private int
  211. s_BCPD_process(stream_state * st, stream_cursor_read * pr,
  212. stream_cursor_write * pw, bool last)
  213. {
  214. return s_xBCPD_process(st, pr, pw, last, false);
  215. }
  216. private int
  217. s_TBCPD_process(stream_state * st, stream_cursor_read * pr,
  218. stream_cursor_write * pw, bool last)
  219. {
  220. return s_xBCPD_process(st, pr, pw, last, true);
  221. }
  222. /* Stream templates */
  223. const stream_template s_BCPD_template =
  224. {&st_BCPD_state, s_BCPD_init, s_BCPD_process, 1, 1,
  225. NULL, NULL, s_BCPD_init
  226. };
  227. const stream_template s_TBCPD_template =
  228. {&st_BCPD_state, s_BCPD_init, s_TBCPD_process, 1, 1,
  229. NULL, NULL, s_BCPD_init
  230. };