robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1#include "nvim/vterm/encoding.h" 2#include "nvim/vterm/vterm_internal_defs.h" 3 4#include "vterm/encoding.c.generated.h" 5 6#define UNICODE_INVALID 0xFFFD 7 8#if defined(DEBUG )&& DEBUG > 1 9# define DEBUG_PRINT_UTF8 10#endif 11 12struct UTF8DecoderData { 13// number of bytes remaining in this codepoint 14int bytes_remaining ; 15 16// number of bytes total in this codepoint once it's finished 17// (for detecting overlongs) 18int bytes_total ; 19 20int this_cp ; 21}; 22 23static void init_utf8 (VTermEncoding * enc ,void * data_ ) 24{ 25struct UTF8DecoderData * data = data_ ; 26 27data -> bytes_remaining = 0 ; 28data -> bytes_total = 0 ; 29} 30 31static void decode_utf8 (VTermEncoding * enc ,void * data_ ,uint32_t cp [],int * cpi ,int cplen , 32const char bytes [],size_t * pos ,size_t bytelen ) 33{ 34struct UTF8DecoderData * data = data_ ; 35 36#ifdef DEBUG_PRINT_UTF8 37printf ("BEGIN UTF-8\n" ); 38#endif 39 40for (;* pos < bytelen && * cpi < cplen ; (* pos )++ ) { 41uint8_t c = (uint8_t )bytes [* pos ]; 42 43#ifdef DEBUG_PRINT_UTF8 44printf (" pos=%zd c=%02x rem=%d\n" ,* pos ,c ,data -> bytes_remaining ); 45#endif 46 47if (c < 0x20 ) {// C0 48return ; 49 }else if (c >=0x20 && c < 0x7f ) { 50if (data -> bytes_remaining ) { 51cp [(* cpi )++ ]= UNICODE_INVALID ; 52 } 53 54cp [(* cpi )++ ]= c ; 55#ifdef DEBUG_PRINT_UTF8 56printf (" UTF-8 char: U+%04x\n" ,c ); 57#endif 58data -> bytes_remaining = 0 ; 59 }else if (c == 0x7f ) {// DEL 60return ; 61 }else if (c >=0x80 && c < 0xc0 ) { 62if (!data -> bytes_remaining ) { 63cp [(* cpi )++ ]= UNICODE_INVALID ; 64continue ; 65 } 66 67data -> this_cp <<=6 ; 68data -> this_cp |=c & 0x3f ; 69data -> bytes_remaining -- ; 70 71if (!data -> bytes_remaining ) { 72#ifdef DEBUG_PRINT_UTF8 73printf (" UTF-8 raw char U+%04x bytelen=%d " ,data -> this_cp ,data -> bytes_total ); 74#endif 75// Check for overlong sequences 76switch (data -> bytes_total ) { 77case 2 : 78if (data -> this_cp < 0x0080 ) { 79data -> this_cp = UNICODE_INVALID ; 80 } 81break ; 82case 3 : 83if (data -> this_cp < 0x0800 ) { 84data -> this_cp = UNICODE_INVALID ; 85 } 86break ; 87case 4 : 88if (data -> this_cp < 0x10000 ) { 89data -> this_cp = UNICODE_INVALID ; 90 } 91break ; 92case 5 : 93if (data -> this_cp < 0x200000 ) { 94data -> this_cp = UNICODE_INVALID ; 95 } 96break ; 97case 6 : 98if (data -> this_cp < 0x4000000 ) { 99data -> this_cp = UNICODE_INVALID ; 100 } 101break ; 102 } 103// Now look for plain invalid ones 104if ((data -> this_cp >=0xD800 && data -> this_cp <=0xDFFF ) 105|| data -> this_cp == 0xFFFE 106|| data -> this_cp == 0xFFFF ) { 107data -> this_cp = UNICODE_INVALID ; 108 } 109#ifdef DEBUG_PRINT_UTF8 110printf (" char: U+%04x\n" ,data -> this_cp ); 111#endif 112cp [(* cpi )++ ]= (uint32_t )data -> this_cp ; 113 } 114 }else if (c >=0xc0 && c < 0xe0 ) { 115if (data -> bytes_remaining ) { 116cp [(* cpi )++ ]= UNICODE_INVALID ; 117 } 118 119data -> this_cp = c & 0x1f ; 120data -> bytes_total = 2 ; 121data -> bytes_remaining = 1 ; 122 }else if (c >=0xe0 && c < 0xf0 ) { 123if (data -> bytes_remaining ) { 124cp [(* cpi )++ ]= UNICODE_INVALID ; 125 } 126 127data -> this_cp = c & 0x0f ; 128data -> bytes_total = 3 ; 129data -> bytes_remaining = 2 ; 130 }else if (c >=0xf0 && c < 0xf8 ) { 131if (data -> bytes_remaining ) { 132cp [(* cpi )++ ]= UNICODE_INVALID ; 133 } 134 135data -> this_cp = c & 0x07 ; 136data -> bytes_total = 4 ; 137data -> bytes_remaining = 3 ; 138 }else if (c >=0xf8 && c < 0xfc ) { 139if (data -> bytes_remaining ) { 140cp [(* cpi )++ ]= UNICODE_INVALID ; 141 } 142 143data -> this_cp = c & 0x03 ; 144data -> bytes_total = 5 ; 145data -> bytes_remaining = 4 ; 146 }else if (c >=0xfc && c < 0xfe ) { 147if (data -> bytes_remaining ) { 148cp [(* cpi )++ ]= UNICODE_INVALID ; 149 } 150 151data -> this_cp = c & 0x01 ; 152data -> bytes_total = 6 ; 153data -> bytes_remaining = 5 ; 154 }else { 155cp [(* cpi )++ ]= UNICODE_INVALID ; 156 } 157 } 158} 159 160static VTermEncoding encoding_utf8 = { 161 .init = & init_utf8 , 162 .decode = & decode_utf8 , 163}; 164 165static void decode_usascii (VTermEncoding * enc ,void * data ,uint32_t cp [],int * cpi ,int cplen , 166const char bytes [],size_t * pos ,size_t bytelen ) 167{ 168int is_gr = bytes [* pos ]& 0x80 ; 169 170for (;* pos < bytelen && * cpi < cplen ; (* pos )++ ) { 171uint8_t c = (uint8_t )(bytes [* pos ] ^is_gr ); 172 173if (c < 0x20 || c == 0x7f || c >=0x80 ) { 174return ; 175 } 176 177cp [(* cpi )++ ]= c ; 178 } 179} 180 181static VTermEncoding encoding_usascii = { 182 .decode = & decode_usascii , 183}; 184 185struct StaticTableEncoding { 186const VTermEncoding enc ; 187const uint32_t chars [128 ]; 188}; 189 190static void decode_table (VTermEncoding * enc ,void * data ,uint32_t cp [],int * cpi ,int cplen , 191const char bytes [],size_t * pos ,size_t bytelen ) 192{ 193struct StaticTableEncoding * table = (struct StaticTableEncoding * )enc ; 194int is_gr = bytes [* pos ]& 0x80 ; 195 196for (;* pos < bytelen && * cpi < cplen ; (* pos )++ ) { 197uint8_t c = (uint8_t )(bytes [* pos ] ^is_gr ); 198 199if (c < 0x20 || c == 0x7f || c >=0x80 ) { 200return ; 201 } 202 203if (table -> chars [c ]) { 204cp [(* cpi )++ ]= table -> chars [c ]; 205 }else { 206cp [(* cpi )++ ]= c ; 207 } 208 } 209} 210 211// https://en.wikipedia.org/wiki/DEC_Special_Graphics 212static const struct StaticTableEncoding encoding_DECdrawing = { 213 { .decode = & decode_table }, 214 { 215 [0x60 ]= 0x25C6 ,// BLACK DIAMOND 216 [0x61 ]= 0x2592 ,// MEDIUM SHADE (checkerboard) 217 [0x62 ]= 0x2409 ,// SYMBOL FOR HORIZONTAL TAB 218 [0x63 ]= 0x240C ,// SYMBOL FOR FORM FEED 219 [0x64 ]= 0x240D ,// SYMBOL FOR CARRIAGE RETURN 220 [0x65 ]= 0x240A ,// SYMBOL FOR LINE FEED 221 [0x66 ]= 0x00B0 ,// DEGREE SIGN 222 [0x67 ]= 0x00B1 ,// PLUS-MINUS SIGN (plus or minus) 223 [0x68 ]= 0x2424 ,// SYMBOL FOR NEW LINE 224 [0x69 ]= 0x240B ,// SYMBOL FOR VERTICAL TAB 225 [0x6a ]= 0x2518 ,// BOX DRAWINGS LIGHT UP AND LEFT (bottom-right corner) 226 [0x6b ]= 0x2510 ,// BOX DRAWINGS LIGHT DOWN AND LEFT (top-right corner) 227 [0x6c ]= 0x250C ,// BOX DRAWINGS LIGHT DOWN AND RIGHT (top-left corner) 228 [0x6d ]= 0x2514 ,// BOX DRAWINGS LIGHT UP AND RIGHT (bottom-left corner) 229 [0x6e ]= 0x253C ,// BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL (crossing lines) 230 [0x6f ]= 0x23BA ,// HORIZONTAL SCAN LINE-1 231 [0x70 ]= 0x23BB ,// HORIZONTAL SCAN LINE-3 232 [0x71 ]= 0x2500 ,// BOX DRAWINGS LIGHT HORIZONTAL 233 [0x72 ]= 0x23BC ,// HORIZONTAL SCAN LINE-7 234 [0x73 ]= 0x23BD ,// HORIZONTAL SCAN LINE-9 235 [0x74 ]= 0x251C ,// BOX DRAWINGS LIGHT VERTICAL AND RIGHT 236 [0x75 ]= 0x2524 ,// BOX DRAWINGS LIGHT VERTICAL AND LEFT 237 [0x76 ]= 0x2534 ,// BOX DRAWINGS LIGHT UP AND HORIZONTAL 238 [0x77 ]= 0x252C ,// BOX DRAWINGS LIGHT DOWN AND HORIZONTAL 239 [0x78 ]= 0x2502 ,// BOX DRAWINGS LIGHT VERTICAL 240 [0x79 ]= 0x2A7D ,// LESS-THAN OR SLANTED EQUAL-TO 241 [0x7a ]= 0x2A7E ,// GREATER-THAN OR SLANTED EQUAL-TO 242 [0x7b ]= 0x03C0 ,// GREEK SMALL LETTER PI 243 [0x7c ]= 0x2260 ,// NOT EQUAL TO 244 [0x7d ]= 0x00A3 ,// POUND SIGN 245 [0x7e ]= 0x00B7 ,// MIDDLE DOT 246 } 247}; 248 249static struct { 250VTermEncodingType type ; 251char designation ; 252VTermEncoding * enc ; 253} 254encodings []= { 255 {ENC_UTF8 ,'u' ,& encoding_utf8 }, 256 {ENC_SINGLE_94 ,'0' , (VTermEncoding * )& encoding_DECdrawing }, 257 {ENC_SINGLE_94 ,'B' ,& encoding_usascii }, 258 {0 }, 259}; 260 261VTermEncoding * vterm_lookup_encoding (VTermEncodingType type ,char designation ) 262{ 263for (int i = 0 ;encodings [i ].designation ;i ++ ) { 264if (encodings [i ].type == type && encodings [i ].designation == designation ) { 265return encodings [i ].enc ; 266 } 267 } 268return NULL ; 269}