stripctrl.c 14 KB

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