1
0

might work now

This commit is contained in:
dtb 2023-09-03 02:23:32 -04:00
parent 8920624eb6
commit 1689f78225

View File

@ -2,41 +2,29 @@
#include <string.h> /* memset(3) */ #include <string.h> /* memset(3) */
#include "ascii.h" /* ASCII_HEXADECIMAL_DIGITS_LOWER, #include "ascii.h" /* ASCII_HEXADECIMAL_DIGITS_LOWER,
* ASCII_HEXADECIMAL_DIGITS_UPPER */ * ASCII_HEXADECIMAL_DIGITS_UPPER */
#define SKIPLINE while((c = getc(stdin)) != '\n' && c != EOF)
static char *hex = ASCII_HEXADECIMAL_DIGITS_UPPER static char *hex = ASCII_HEXADECIMAL_DIGITS_UPPER
ASCII_HEXADECIMAL_DIGITS_LOWER; ASCII_HEXADECIMAL_DIGITS_LOWER;
void print_hexascii(unsigned char *hexes, int n){
if(n % 2 != 0)
return;
while(n --> 0){
putc((char)(((hex - strchr(hex, hexes[0])) << 4)
+ (hex - strchr(hex, hexes[1]))), stdout);
++hexes;
}
}
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
int c; int c;
int i; int i;
int l; /* line counter */ int l; /* line counter */
char *n; char *n;
unsigned char utf32_hex[8]; /* nybbles */
long int utf32_lit; long int utf32_lit;
unsigned char utf8_hex[8]; /* nybbles */ unsigned char utf8_bytes[8];
long int utf8_lit;
c = '\0'; c = '\0';
i = 0; i = 0;
l = 1; l = 1;
while(c != EOF){ while(c != EOF){
memset(utf32_hex, 0, sizeof utf32_hex); memset(utf8_bytes, '0', sizeof utf8_bytes);
memset(utf8_hex, '0', sizeof utf8_hex);
for( for(
i = 0, n = NULL, utf32_lit = 0, utf8_lit = 0; i = 0, n = NULL, utf32_lit = 0;
(c = getc(stdin)) != '\n' (c = getc(stdin)) != '\n'
&& c != EOF && c != EOF
&& i < (sizeof utf32_hex) / (sizeof *utf32_hex); && i < 10;
++i ++i
){ ){
if( if(
@ -48,58 +36,37 @@ int main(int argc, char *argv[]){
){ ){
fprintf(stderr, "%s: %s: Syntax error.\n", fprintf(stderr, "%s: %s: Syntax error.\n",
argv[0], l); argv[0], l);
while((c = getc(stdin)) != '\n' && c != EOF); SKIPLINE;
i = -1; i = -1;
break; break;
} }
if(n != NULL){ if(n != NULL)
n -= 16; utf32_lit = (utf32_lit << 4) + (n - hex) % 16;
utf32_hex[i - 2] = *(n -= 16 * (n - hex > 16));
}else if(i >= 2)
utf32_hex[i - 2] = c;
} }
if(i == -1 || i < 3) if(i < 3){
if(c != '\n' && c != EOF)
SKIPLINE;
if(c == EOF)
return 0;
continue; continue;
while(utf32_hex[7] == '\0'){ /* slow but easy */
for(i = 0; i < 7; ++i)
utf32_hex[i + 1] = utf32_hex[i];
utf32_hex[0] = '0';
} }
/* this code is embarrassing */ switch(i = (utf32_lit < 65536)
for(i = 0; i < 8; ++i) + (utf32_lit < 2048)
utf32_lit = (utf32_lit << 4) + (utf32_lit < 128)){
+ strchr(hex, utf32_hex[i]) - hex; case 0: utf8_bytes[0] =
if(utf32_lit < 128){ ((utf32_lit >> 18) & 7) + 240; /* 11110xxx */
utf8_hex[7] = utf32_hex[7]; case 1: utf8_bytes[1] = i == 1
utf8_hex[6] = utf32_hex[6]; ? ((utf32_lit >> 12) & 15) + 224 /* 1110xxxx */
i = 6; : ((utf32_lit >> 12) & 63) + 80; /* 10xxxxxx */
goto done; case 2: utf8_bytes[2] = i == 2
}else{ ? ((utf32_lit >> 6) & 31) + 192 /* 110xxxxx */
utf8_hex[7] = hex[utf32_lit & 15]; : ((utf32_lit >> 6) & 63) + 80; /* 10xxxxxx */
utf8_hex[6] = hex[((utf32_lit >> 4) & 3) + 8]; case 3: utf8_bytes[3] = i == 3
? utf8_bytes[3] = utf32_lit & 127 /* 0xxxxxxx */
: (utf32_lit & 63) + 80; /* 10xxxxxx */
} }
if(utf32_lit < 2048){ for( ; i < 4; ++i)
utf8_hex[5] = hex[(utf32_lit >> 6) & 15]; putc(utf8_bytes[i], stdout);
utf8_hex[4] = hex[((utf32_lit >> 10) & 1) + 12];
i = 4;
goto done;
}else{
utf8_hex[5] = hex[(utf32_lit >> 6) & 15];
utf8_hex[4] = hex[((utf32_lit >> 10) & 3) + 8];
}
if(utf32_lit < 65536){
utf8_hex[3] = hex[(utf32_lit >> 12) & 15];
utf8_hex[2] = 14;
i = 2;
goto done;
}else{
utf8_hex[3] = hex[(utf32_lit >> 12) & 15];
utf8_hex[2] = hex[((utf32_lit >> 16) & 3) + 8];
utf8_hex[1] = hex[(utf32_lit >> 21) & 3];
utf8_hex[0] = hex[15];
i = 0;
}
done: print_hexascii(utf8_hex + i, 8 - i);
++l; ++l;
} }
} }