stripctrl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * stripctrl.c: a facility for stripping control characters out of a
  3. * data stream (defined as any multibyte character in the system
  4. * locale which is neither printable nor \n), using the standard C
  5. * library multibyte character facilities.
  6. */
  7. #include <assert.h>
  8. #include <locale.h>
  9. #include <string.h>
  10. #include <wchar.h>
  11. #include <wctype.h>
  12. #include "putty.h"
  13. #ifndef WINSCP
  14. #include "terminal.h"
  15. #endif
  16. #include "misc.h"
  17. #include "marshal.h"
  18. #define SCC_BUFSIZE 64
  19. #define LINE_LIMIT 77
  20. typedef struct StripCtrlCharsImpl StripCtrlCharsImpl;
  21. struct StripCtrlCharsImpl {
  22. mbstate_t mbs_in, mbs_out;
  23. bool permit_cr;
  24. wchar_t substitution;
  25. char buf[SCC_BUFSIZE];
  26. size_t buflen;
  27. #ifndef WINSCP
  28. Terminal *term;
  29. bool last_term_utf;
  30. struct term_utf8_decode utf8;
  31. unsigned long (*translate)(Terminal *, term_utf8_decode *, unsigned char);
  32. #endif
  33. bool line_limit;
  34. bool line_start;
  35. size_t line_chars_remaining;
  36. BinarySink *bs_out;
  37. StripCtrlChars public;
  38. };
  39. static void stripctrl_locale_BinarySink_write(
  40. BinarySink *bs, const void *vp, size_t len);
  41. static void stripctrl_term_BinarySink_write(
  42. BinarySink *bs, const void *vp, size_t len);
  43. static StripCtrlCharsImpl *stripctrl_new_common(
  44. BinarySink *bs_out, bool permit_cr, wchar_t substitution)
  45. {
  46. StripCtrlCharsImpl *scc = snew(StripCtrlCharsImpl);
  47. memset(scc, 0, sizeof(StripCtrlCharsImpl)); /* zeroes mbstates */
  48. scc->bs_out = bs_out;
  49. scc->permit_cr = permit_cr;
  50. scc->substitution = substitution;
  51. return scc;
  52. }
  53. #ifndef WINSCP
  54. StripCtrlChars *stripctrl_new(
  55. BinarySink *bs_out, bool permit_cr, wchar_t substitution)
  56. {
  57. StripCtrlCharsImpl *scc = stripctrl_new_common(
  58. bs_out, permit_cr, substitution);
  59. BinarySink_INIT(&scc->public, stripctrl_locale_BinarySink_write);
  60. return &scc->public;
  61. }
  62. StripCtrlChars *stripctrl_new_term_fn(
  63. BinarySink *bs_out, bool permit_cr, wchar_t substitution,
  64. Terminal *term, unsigned long (*translate)(
  65. Terminal *, term_utf8_decode *, unsigned char))
  66. {
  67. StripCtrlCharsImpl *scc = stripctrl_new_common(
  68. bs_out, permit_cr, substitution);
  69. scc->term = term;
  70. scc->translate = translate;
  71. BinarySink_INIT(&scc->public, stripctrl_term_BinarySink_write);
  72. return &scc->public;
  73. }
  74. #endif
  75. void stripctrl_retarget(StripCtrlChars *sccpub, BinarySink *new_bs_out)
  76. {
  77. StripCtrlCharsImpl *scc =
  78. container_of(sccpub, StripCtrlCharsImpl, public);
  79. scc->bs_out = new_bs_out;
  80. stripctrl_reset(sccpub);
  81. }
  82. void stripctrl_reset(StripCtrlChars *sccpub)
  83. {
  84. StripCtrlCharsImpl *scc =
  85. container_of(sccpub, StripCtrlCharsImpl, public);
  86. /*
  87. * Clear all the fields that might have been in the middle of a
  88. * multibyte character or non-default shift state, so that we can
  89. * start converting a fresh piece of data to send to a channel
  90. * that hasn't seen the previous output.
  91. */
  92. #ifndef WINSCP
  93. memset(&scc->utf8, 0, sizeof(scc->utf8));
  94. #endif
  95. memset(&scc->mbs_in, 0, sizeof(scc->mbs_in));
  96. memset(&scc->mbs_out, 0, sizeof(scc->mbs_out));
  97. /*
  98. * Also, reset the line-limiting system to its starting state.
  99. */
  100. scc->line_start = true;
  101. }
  102. void stripctrl_free(StripCtrlChars *sccpub)
  103. {
  104. StripCtrlCharsImpl *scc =
  105. container_of(sccpub, StripCtrlCharsImpl, public);
  106. smemclr(scc, sizeof(StripCtrlCharsImpl));
  107. sfree(scc);
  108. }
  109. void stripctrl_enable_line_limiting(StripCtrlChars *sccpub)
  110. {
  111. StripCtrlCharsImpl *scc =
  112. container_of(sccpub, StripCtrlCharsImpl, public);
  113. scc->line_limit = true;
  114. scc->line_start = true;
  115. }
  116. #ifndef WINSCP
  117. static inline bool stripctrl_ctrlchar_ok(StripCtrlCharsImpl *scc, wchar_t wc)
  118. {
  119. return wc == L'\n' || (wc == L'\r' && scc->permit_cr);
  120. }
  121. static inline void stripctrl_check_line_limit(
  122. StripCtrlCharsImpl *scc, wchar_t wc, size_t width)
  123. {
  124. if (!scc->line_limit)
  125. return; /* nothing to do */
  126. if (scc->line_start) {
  127. put_datapl(scc->bs_out, PTRLEN_LITERAL("| "));
  128. scc->line_start = false;
  129. scc->line_chars_remaining = LINE_LIMIT;
  130. }
  131. if (wc == '\n') {
  132. scc->line_start = true;
  133. return;
  134. }
  135. if (scc->line_chars_remaining < width) {
  136. put_datapl(scc->bs_out, PTRLEN_LITERAL("\r\n> "));
  137. scc->line_chars_remaining = LINE_LIMIT;
  138. }
  139. assert(width <= scc->line_chars_remaining);
  140. scc->line_chars_remaining -= width;
  141. }
  142. static inline void stripctrl_locale_put_wc(StripCtrlCharsImpl *scc, wchar_t wc)
  143. {
  144. if (iswprint(wc) || stripctrl_ctrlchar_ok(scc, wc)) {
  145. /* Printable character, or one we're going to let through anyway. */
  146. } else if (scc->substitution) {
  147. wc = scc->substitution;
  148. } else {
  149. /* No defined substitution, so don't write any output wchar_t. */
  150. return;
  151. }
  152. stripctrl_check_line_limit(scc, wc, mk_wcwidth(wc));
  153. char outbuf[MB_LEN_MAX];
  154. size_t produced = wcrtomb(outbuf, wc, &scc->mbs_out);
  155. if (produced > 0)
  156. put_data(scc->bs_out, outbuf, produced);
  157. }
  158. static inline void stripctrl_term_put_wc(
  159. StripCtrlCharsImpl *scc, unsigned long wc)
  160. {
  161. ptrlen prefix = PTRLEN_LITERAL("");
  162. if (!(wc & ~0x9F)) {
  163. /* This is something the terminal interprets as a control
  164. * character. */
  165. if (!stripctrl_ctrlchar_ok(scc, wc)) {
  166. if (!scc->substitution)
  167. return;
  168. else
  169. wc = scc->substitution;
  170. }
  171. if (wc == '\012') {
  172. /* Precede \n with \r, because our terminal will not
  173. * generally be in the ONLCR mode where it assumes that
  174. * internally, and any \r on input has been stripped
  175. * out. */
  176. prefix = PTRLEN_LITERAL("\r");
  177. }
  178. }
  179. stripctrl_check_line_limit(scc, wc, term_char_width(scc->term, wc));
  180. if (prefix.len)
  181. put_datapl(scc->bs_out, prefix);
  182. char outbuf[6];
  183. size_t produced;
  184. /*
  185. * The Terminal implementation encodes 7-bit ASCII characters in
  186. * UTF-8 mode, and all printing characters in non-UTF-8 (i.e.
  187. * single-byte character set) mode, as values in the surrogate
  188. * range (a conveniently unused piece of space in this context)
  189. * whose low byte is the original 1-byte representation of the
  190. * character.
  191. */
  192. if ((wc - 0xD800) < (0xE000 - 0xD800))
  193. wc &= 0xFF;
  194. if (in_utf(scc->term)) {
  195. produced = encode_utf8(outbuf, wc);
  196. } else {
  197. outbuf[0] = wc;
  198. produced = 1;
  199. }
  200. if (produced > 0)
  201. put_data(scc->bs_out, outbuf, produced);
  202. }
  203. static inline size_t stripctrl_locale_try_consume(
  204. StripCtrlCharsImpl *scc, const char *p, size_t len)
  205. {
  206. wchar_t wc;
  207. mbstate_t mbs_orig = scc->mbs_in;
  208. size_t consumed = mbrtowc(&wc, p, len, &scc->mbs_in);
  209. if (consumed == (size_t)-2) {
  210. /*
  211. * The buffer is too short to see the end of the multibyte
  212. * character that it appears to be starting with. We return 0
  213. * for 'no data consumed', restore the conversion state from
  214. * before consuming the partial character, and our caller will
  215. * come back when it has more data available.
  216. */
  217. scc->mbs_in = mbs_orig;
  218. return 0;
  219. }
  220. if (consumed == (size_t)-1) {
  221. /*
  222. * The buffer contains an illegal multibyte sequence. There's
  223. * no really good way to recover from this, so we'll just
  224. * reset our input state, consume a single byte without
  225. * emitting anything, and hope we can resynchronise to
  226. * _something_ sooner or later.
  227. */
  228. memset(&scc->mbs_in, 0, sizeof(scc->mbs_in));
  229. return 1;
  230. }
  231. if (consumed == 0) {
  232. /*
  233. * A zero wide character is encoded by the data, but mbrtowc
  234. * hasn't told us how many input bytes it takes. There isn't
  235. * really anything good we can do here, so we just advance by
  236. * one byte in the hope that that was the NUL.
  237. *
  238. * (If it wasn't - that is, if we're in a multibyte encoding
  239. * in which the terminator of a normal C string is encoded in
  240. * some way other than a single zero byte - then probably lots
  241. * of other things will have gone wrong before we get here!)
  242. */
  243. stripctrl_locale_put_wc(scc, L'\0');
  244. return 1;
  245. }
  246. /*
  247. * Otherwise, this is the easy case: consumed > 0, and we've eaten
  248. * a valid multibyte character.
  249. */
  250. stripctrl_locale_put_wc(scc, wc);
  251. return consumed;
  252. }
  253. static void stripctrl_locale_BinarySink_write(
  254. BinarySink *bs, const void *vp, size_t len)
  255. {
  256. StripCtrlChars *sccpub = BinarySink_DOWNCAST(bs, StripCtrlChars);
  257. StripCtrlCharsImpl *scc =
  258. container_of(sccpub, StripCtrlCharsImpl, public);
  259. const char *p = (const char *)vp;
  260. const char *previous_locale = setlocale(LC_CTYPE, NULL);
  261. setlocale(LC_CTYPE, "");
  262. /*
  263. * Deal with any partial multibyte character buffered from last
  264. * time.
  265. */
  266. while (scc->buflen > 0) {
  267. size_t to_copy = SCC_BUFSIZE - scc->buflen;
  268. if (to_copy > len)
  269. to_copy = len;
  270. memcpy(scc->buf + scc->buflen, p, to_copy);
  271. { // WINSCP
  272. size_t consumed = stripctrl_locale_try_consume(
  273. scc, scc->buf, scc->buflen + to_copy);
  274. if (consumed >= scc->buflen) {
  275. /*
  276. * We've consumed a multibyte character that includes all
  277. * the data buffered from last time. So we can clear our
  278. * buffer and move on to processing the main input string
  279. * in situ, having first discarded whatever initial
  280. * segment of it completed our previous character.
  281. */
  282. size_t consumed_from_main_string = consumed - scc->buflen;
  283. assert(consumed_from_main_string <= len);
  284. p += consumed_from_main_string;
  285. len -= consumed_from_main_string;
  286. scc->buflen = 0;
  287. break;
  288. }
  289. if (consumed == 0) {
  290. /*
  291. * If we didn't manage to consume anything, i.e. the whole
  292. * buffer contains an incomplete sequence, it had better
  293. * be because our entire input string _this_ time plus
  294. * whatever leftover data we had from _last_ time still
  295. * comes to less than SCC_BUFSIZE. In other words, we've
  296. * already copied all the new data on to the end of our
  297. * buffer, and it still hasn't helped. So increment buflen
  298. * to reflect the new data, and return.
  299. */
  300. assert(to_copy == len);
  301. scc->buflen += to_copy;
  302. goto out;
  303. }
  304. /*
  305. * Otherwise, we've somehow consumed _less_ data than we had
  306. * buffered, and yet we weren't able to consume that data in
  307. * the last call to this function. That sounds impossible, but
  308. * I can think of one situation in which it could happen: if
  309. * we had an incomplete MB sequence last time, and now more
  310. * data has arrived, it turns out to be an _illegal_ one, so
  311. * we consume one byte in the hope of resynchronising.
  312. *
  313. * Anyway, in this case we move the buffer up and go back
  314. * round this initial loop.
  315. */
  316. scc->buflen -= consumed;
  317. memmove(scc->buf, scc->buf + consumed, scc->buflen);
  318. } // WINSCP
  319. }
  320. /*
  321. * Now charge along the main string.
  322. */
  323. while (len > 0) {
  324. size_t consumed = stripctrl_locale_try_consume(scc, p, len);
  325. if (consumed == 0)
  326. break;
  327. assert(consumed <= len);
  328. p += consumed;
  329. len -= consumed;
  330. }
  331. /*
  332. * Any data remaining should be copied into our buffer, to keep
  333. * for next time.
  334. */
  335. assert(len <= SCC_BUFSIZE);
  336. memcpy(scc->buf, p, len);
  337. scc->buflen = len;
  338. out:
  339. setlocale(LC_CTYPE, previous_locale);
  340. }
  341. static void stripctrl_term_BinarySink_write(
  342. BinarySink *bs, const void *vp, size_t len)
  343. {
  344. StripCtrlChars *sccpub = BinarySink_DOWNCAST(bs, StripCtrlChars);
  345. StripCtrlCharsImpl *scc =
  346. container_of(sccpub, StripCtrlCharsImpl, public);
  347. bool utf = in_utf(scc->term);
  348. if (utf != scc->last_term_utf) {
  349. scc->last_term_utf = utf;
  350. scc->utf8.state = 0;
  351. }
  352. for (const unsigned char *p = (const unsigned char *)vp;
  353. len > 0; len--, p++) {
  354. unsigned long t = scc->translate(scc->term, &scc->utf8, *p);
  355. if (t == UCSTRUNCATED) {
  356. stripctrl_term_put_wc(scc, 0xFFFD);
  357. /* go round again */
  358. t = scc->translate(scc->term, &scc->utf8, *p);
  359. }
  360. if (t == UCSINCOMPLETE)
  361. continue;
  362. if (t == UCSINVALID)
  363. t = 0xFFFD;
  364. stripctrl_term_put_wc(scc, t);
  365. }
  366. }
  367. char *stripctrl_string_ptrlen(StripCtrlChars *sccpub, ptrlen str)
  368. {
  369. strbuf *out = strbuf_new();
  370. stripctrl_retarget(sccpub, BinarySink_UPCAST(out));
  371. put_datapl(sccpub, str);
  372. stripctrl_retarget(sccpub, NULL);
  373. return strbuf_to_str(out);
  374. }
  375. #endif
  376. #ifdef STRIPCTRL_TEST
  377. /*
  378. gcc -std=c99 -DSTRIPCTRL_TEST -o scctest stripctrl.c marshal.c utils.c memory.c wcwidth.c -I . -I unix -I charset
  379. */
  380. void out_of_memory(void) { fprintf(stderr, "out of memory\n"); abort(); }
  381. void stripctrl_write(BinarySink *bs, const void *vdata, size_t len)
  382. {
  383. const uint8_t *p = vdata;
  384. printf("[");
  385. for (size_t i = 0; i < len; i++)
  386. printf("%*s%02x", i?1:0, "", (unsigned)p[i]);
  387. printf("]");
  388. }
  389. void stripctrl_test(StripCtrlChars *scc, ptrlen pl)
  390. {
  391. stripctrl_write(NULL, pl.ptr, pl.len);
  392. printf(" -> ");
  393. put_datapl(scc, pl);
  394. printf("\n");
  395. }
  396. int main(void)
  397. {
  398. struct foo { BinarySink_IMPLEMENTATION; } foo;
  399. BinarySink_INIT(&foo, stripctrl_write);
  400. StripCtrlChars *scc = stripctrl_new(BinarySink_UPCAST(&foo), false, '?');
  401. stripctrl_test(scc, PTRLEN_LITERAL("a\033[1mb"));
  402. stripctrl_test(scc, PTRLEN_LITERAL("a\xC2\x9B[1mb"));
  403. stripctrl_test(scc, PTRLEN_LITERAL("a\xC2\xC2[1mb"));
  404. stripctrl_test(scc, PTRLEN_LITERAL("\xC3"));
  405. stripctrl_test(scc, PTRLEN_LITERAL("\xA9"));
  406. stripctrl_test(scc, PTRLEN_LITERAL("\xE2\x80\x8F"));
  407. stripctrl_test(scc, PTRLEN_LITERAL("a\0b"));
  408. stripctrl_free(scc);
  409. return 0;
  410. }
  411. #endif /* STRIPCTRL_TEST */