Skip to content

Commit c6e1e3c

Browse files
committed
Fix encoding of CR, LF, VT
1 parent 9e8455e commit c6e1e3c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/scsu.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ size_t convert_scsu_to_utf8(
101101
uint8_t shift = 0;
102102
uint8_t unicode = 0;
103103
uint8_t active_window = 0;
104+
uint32_t last_u = 0; // Unicode code point
104105
errno = 0;
105106
while (*inbytesleft && *outbytesleft) {
106107
uint8_t c = *src++; *inbytesleft -= 1;
@@ -171,8 +172,13 @@ size_t convert_scsu_to_utf8(
171172
*inbytesleft -= 2;
172173
continue;
173174
} else { errno = EINVAL; break; }
174-
} else if (c == 0x0A || c == 0x0D || c == 0x09) {
175-
u = ' '; /* Encode as space, hack */
175+
} else if (c == 0x09) {
176+
u = ' '; /* Encode tab as space, hack */
177+
} else if (c == 0x0A && last_u == 0x0D) {
178+
last_u = c;
179+
continue; /* Convert CRLF to LF */
180+
} else if (c == 0x0A || c == 0x0B || c == 0x0D) {
181+
u = '\n'; /* Encode CR, VT, LF as LF */
176182
} else if (c >= 0x20 && c <= 0x7F) { /* ASCII, pass through */
177183
u = c;
178184
} else if (c >= 0x80) {
@@ -217,6 +223,7 @@ size_t convert_scsu_to_utf8(
217223
*dst++ = (u & 0x7F);
218224
*outbytesleft -= 1;
219225
}
226+
last_u = u;
220227
}
221228
*outbuf = (char *)dst;
222229
*inbuf = (char *)src;

0 commit comments

Comments
 (0)