sbcp.c 5.3 KB

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