diff --git a/share/charset/UTF-8 b/share/charset/UTF-8 deleted file mode 100644 index 5fba8c41..00000000 Binary files a/share/charset/UTF-8 and /dev/null differ diff --git a/share/charset/UTF-8.s b/share/charset/UTF-8.s deleted file mode 100644 index a8754dba..00000000 --- a/share/charset/UTF-8.s +++ /dev/null @@ -1,78 +0,0 @@ -place64 @sicv_magic_v1 -place64 utf8_boundary, utf8_boundary - -utf8_boundary: -input8 &r0 -load64 _0x80, &r1 -lt &r0, &r1, &r2 -eq &r2, &r255, &r2 -jnz &r2, Lmultibyte -output8 &r0 -ret &r255 - -Lmultibyte: -load64 _0xC0, &r1 -load64 _0xFD, &r2 -lt &r0, &r1, &r1 -mt &r0, &r2, &r2 -or &r1, &r2, &r1 -eq &r1, &r255, &r1 -jnz &r1, Lvalid_multibyte -load64 ERROR, &r1 -ret &r1 - -Lvalid_multibyte: -output8 &r0 -load64 _0xE0, &r1 -lt &r0, &r1, &r1 -eq &r1, &r255, &r1 -input8 &r3 -output8 &r3 -jnz &r1, L3b -L2b: -ret &r255 -L3b: -input8 &r3 -output8 &r3 -load64 _0xF0, &r1 -lt &r0, &r1, &r1 -eq &r1, &r255, &r1 -jnz &r1, L4b -ret &r255 -L4b: -input8 &r3 -output8 &r3 -load64 _0xF8, &r1 -lt &r0, &r1, &r1 -eq &r1, &r255, &r1 -jnz &r1, L5b -ret &r255 -L5b: -input8 &r3 -output8 &r3 -load64 _0xFC, &r1 -lt &r0, &r1, &r1 -eq &r1, &r255, &r1 -jnz &r1, L6b -ret &r255 -L6b: -input8 &r3 -output8 &r3 -ret &r255 - -ERROR: -place64 0xffffffffffffffff -_0x80: -place64 0x80 -_0xC0: -place64 0xc0 -_0xFD: -place64 0xfd -_0xE0: -place64 0xe0 -_0xF0: -place64 0xf0 -_0xF8: -place64 0xf8 -_0xFC: -place64 0xfc diff --git a/src/locale/iconv.c b/src/locale/iconv.c index ae49fc4e..ac6b2467 100644 --- a/src/locale/iconv.c +++ b/src/locale/iconv.c @@ -1,433 +1,417 @@ -#include -#include -#include -#include -#include -#include -#include #include -#include +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include #include "locale_impl.h" -/* input/output instructions: inputN */ -#define VMOP_INPUT8 1 -#define VMOP_INPUT16 2 -#define VMOP_INPUT32 3 -#define VMOP_INPUT64 4 -#define VMOP_OUTPUT8 5 -#define VMOP_OUTPUT16 6 -#define VMOP_OUTPUT32 7 -#define VMOP_OUTPUT64 8 +// ==- Charset Library -== +// Charset libraries defines functions that converts a specific charset from/to UTF-8. Most charset libraries +// are dynamically loaded, with there are some exceptions are built-in (e.g. ASCII and UTF-8). -/* x-y=z instructions: op */ -#define VMOP_XOR 9 -#define VMOP_AND 10 -#define VMOP_OR 11 -#define VMOP_ADD 13 -#define VMOP_SUB 14 -#define VMOP_SMUL 15 -#define VMOP_UMUL 17 -#define VMOP_EQ 19 -#define VMOP_MT 20 -#define VMOP_LT 21 -#define VMOP_SHL 29 -#define VMOP_SHR 30 +#define CHARSET_STRLEN_MAX 24 -/* not instruction: not */ -#define VMOP_NOT 12 - -/* jnz instruction: jnz , */ -#define VMOP_JNZ 22 - -/* jmp instruction: jmp */ -#define VMOP_JMP 23 - -/* load instructions: loadN */ -#define VMOP_LOAD8 24 -#define VMOP_LOAD16 25 -#define VMOP_LOAD32 26 -#define VMOP_LOAD64 27 - -/* ptrload instructions: loadN */ -#define VMOP_PTRLOAD8 33 -#define VMOP_PTRLOAD16 34 -#define VMOP_PTRLOAD32 35 -#define VMOP_PTRLOAD64 36 - -/* store instruction: store64 */ -#define VMOP_STORE64 28 - -/* return instruction: ret */ -#define VMOP_RET 31 - -#define ICONV_CURRENT_VERSION 1 -#define ICONV_REG_COUNT 16 -#define ICONV_INSC_LEN 8 - -#define ICONV_ERR_EMPTY_STREAM -2 -#define ICONV_ERR_STREAM_OVERFLOW -3 -#define ICONV_ERR_ILLEGAL_OPCODE -4 -#define ICONV_ERR_MEMORY_OVERFLOW -5 - -#define ICONV_CHARSET_PATH_PREFIX "/share/iconv/" - -static const uint8_t vm_binary_magic[4] = { 'S', 'I', 'C', 'V' }; - -struct vm_binary_header { - uint8_t magic[4]; - uint32_t version; - uint32_t from_utf8_addr; - uint32_t to_utf8_addr; +struct charset_library { + int (*from_utf8)(char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb); + int (*to_utf8)(char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb); }; -struct iconv_vm { - uint8_t *memory; - size_t memlen; - size_t pc; - uint64_t reg[ICONV_REG_COUNT]; +static void escape_symbol_name(char *name) { + while (*name) { + if (*name == '-') { + *name = '_'; + } + name++; + } +} + +static struct charset_library open_charset_library(const char *charset) { + struct charset_library this = {NULL, NULL}; + + char escaped_charset[CHARSET_STRLEN_MAX + 1] = {0}; + char libname[sizeof("libSemiOSCharset") + CHARSET_STRLEN_MAX + sizeof(".so")] = {0}; + char fromname[sizeof("SemiOS_Charset_") + CHARSET_STRLEN_MAX + sizeof("_FromUTF8")] = {0}; + char toname[sizeof("SemiOS_Charset_") + CHARSET_STRLEN_MAX + sizeof("_ToUTF8")] = {0}; + + if (strlen(charset) >= sizeof(escaped_charset)) { + return this; + } + strncpy(escaped_charset, charset, sizeof(escaped_charset)); + escape_symbol_name(escaped_charset); + + strncat(libname, "libSemiOSCharset", sizeof(libname) - strlen(libname) - 1); + strncat(libname, charset, sizeof(libname) - strlen(libname) - 1); + strncat(libname, ".so", sizeof(libname) - strlen(libname) - 1); + + strncat(fromname, "SemiOS_Charset_", sizeof(fromname) - strlen(fromname) - 1); + strncat(fromname, escaped_charset, sizeof(fromname) - strlen(fromname) - 1); + strncat(fromname, "_FromUTF8", sizeof(fromname) - strlen(fromname) - 1); + + strncat(toname, "SemiOS_Charset_", sizeof(toname) - strlen(toname) - 1); + strncat(toname, escaped_charset, sizeof(toname) - strlen(toname) - 1); + strncat(toname, "_ToUTF8", sizeof(toname) - strlen(toname) - 1); + + void *dll = dlopen(libname, RTLD_LOCAL); + if (!dll) { + return this; + } + + this.from_utf8 = dlsym(dll, fromname); + this.to_utf8 = dlsym(dll, toname); + + return this; +} + +// ==- Builtin Charset Libraries -== + +static int SemiOS_Charset_UTF_8_FromUTF8( + char **restrict in, size_t *restrict inb, + char **restrict out, size_t *restrict outb +) { + size_t n = *inb < *outb ? *inb : *outb; + if (n) { + memcpy(*out, *in, n); + *in += n; + *out += n; + *inb -= n; + *outb -= n; + } + return *inb == 0 ? 0 : -1; +} +static int SemiOS_Charset_UTF_8_ToUTF8( + char **restrict in, size_t *restrict inb, + char **restrict out, size_t *restrict outb +) { + size_t n = *inb < *outb ? *inb : *outb; + if (n) { + memcpy(*out, *in, n); + *in += n; + *out += n; + *inb -= n; + *outb -= n; + } + return *inb == 0 ? 0 : -1; +} +static struct charset_library builtin_utf8 = { + SemiOS_Charset_UTF_8_FromUTF8, + SemiOS_Charset_UTF_8_ToUTF8, }; -struct iconv_stream { - char *buf; - size_t off; - size_t len; +static int SemiOS_Charset_ASCII_FromUTF8( + char **restrict in, size_t *restrict inb, + char **restrict out, size_t *restrict outb +) { + while (*inb > 0 && *outb > 0) { + unsigned char c = (unsigned char)**in; + if (c < 0x80) { + **out = (char)c; + (*in)++; (*out)++; + (*inb)--; (*outb)--; + } else { + return -2; + } + } + return *inb == 0 ? 0 : -1; +} +static int SemiOS_Charset_ASCII_ToUTF8( + char **restrict in, size_t *restrict inb, + char **restrict out, size_t *restrict outb +) { + while (*inb > 0 && *outb > 0) { + unsigned char c = (unsigned char)**in; + if (c < 0x80) { + **out = (char)c; + (*in)++; (*out)++; + (*inb)--; (*outb)--; + } else { + return -2; + } + } + return *inb == 0 ? 0 : -1; +} +static struct charset_library builtin_ascii = { + SemiOS_Charset_ASCII_FromUTF8, + SemiOS_Charset_ASCII_ToUTF8, }; -static struct iconv_vm iconv_vm_new(uint8_t *memory, size_t memlen) { - struct iconv_vm vm; - vm.memory = memory; - vm.memlen = memlen; - vm.pc = 0; - memset(&vm.reg, 0, sizeof(uint64_t) * ICONV_REG_COUNT); - return vm; +// ==- Charset Library Table -== +// The Charset Library Table maintains a thread-safe, global status of charset libraries. + +static pthread_mutex_t clt_lock = PTHREAD_MUTEX_INITIALIZER; +static int clt_initialized = 0; +static struct hsearch_data clt; + +static int initialize_clt() { + int status; + ENTRY item; + ENTRY *retval = NULL; + + status = hcreate_r(32, &clt); + if (!status) { + return -1; + } + + item.key = "UTF-8"; + item.data = &builtin_utf8; + if (hsearch_r(item, ENTER, &retval, &clt) == 0) { + goto init_error; + } + + item.key = "ASCII"; + item.data = &builtin_ascii; + if (hsearch_r(item, ENTER, &retval, &clt) == 0) { + goto init_error; + } + + clt_initialized = 1; + return 0; + +init_error: + hdestroy_r(&clt); + return -1; } -static uint64_t htonll(uint64_t val) { - return (((uint64_t) htonl(val)) << 32) + htonl(val >> 32); +static int lock_clt(void) { + int status = pthread_mutex_lock(&clt_lock); + if (status != 0) return -1; + if (!clt_initialized) { + if (initialize_clt() != 0) { + pthread_mutex_unlock(&clt_lock); + return -1; + } + } + return 0; } -static uint64_t ntohll(uint64_t val) { - return (((uint64_t) ntohl(val)) << 32) + ntohl(val >> 32); +static void unlock_clt() { + pthread_mutex_unlock(&clt_lock); } -static uint64_t vm_reg_read_raw(struct iconv_vm *vm, uint8_t n) { - if(n >= ICONV_REG_COUNT) { - return 0; - } - return vm->reg[n]; +static struct charset_library *push_clt(const char *name) { + ENTRY item = {NULL, NULL}; + ENTRY *retval = NULL; + + struct charset_library cl = open_charset_library(name); + if (!cl.from_utf8 || !cl.to_utf8) { + goto load_error; + } + struct charset_library *clp = malloc(sizeof(struct charset_library)); + if (!clp) { + goto load_error; + } + *clp = cl; + + item.key = calloc(strlen(name) + 1, 1); + if (!item.key) { + goto load_error; + } + strcpy(item.key, name); + item.data = clp; + if (hsearch_r(item, ENTER, &retval, &clt) == 0) { + goto load_error; + } + + unlock_clt(); + return clp; + +load_error: + unlock_clt(); + free(item.key); + free(item.data); + return NULL; } -static void vm_reg_write_raw(struct iconv_vm *vm, uint8_t n, uint64_t val) { - if(n >= ICONV_REG_COUNT) { - return; - } - vm->reg[n] = val; +static struct charset_library *get_clt(const char *name) { + int status; + ENTRY item; + ENTRY *retval = NULL; + + status = lock_clt(); + if (status != 0) { + return NULL; + } + + item.key = (char*)name; + if (hsearch_r(item, FIND, &retval, &clt) == 0) { + return push_clt(name); + } else { + unlock_clt(); + return (struct charset_library*)retval->data; + } } -static uint64_t vm_reg_read(struct iconv_vm *vm, uint8_t n) { - return ntohll(vm_reg_read_raw(vm, n)); -} +// ==- Implementing the Iconv API -== -static void vm_reg_write(struct iconv_vm *vm, uint8_t n, uint64_t val) { - vm_reg_write_raw(vm, n, htonll(val)); -} - -static int iconv_stream_read(struct iconv_stream *st, void *dst, size_t n) { - if(st->off + n > st->len) { - return -1; - } - memcpy(dst, st->buf + st->off, n); - st->off += n; - return 0; -} - -static int iconv_stream_write(struct iconv_stream *st, const void *src, size_t n) { - if(st->off + n > st->len) { - return -1; - } - memcpy(st->buf + st->off, src, n); - st->off += n; - return 0; -} - -static int vm_memory_read(struct iconv_vm *vm, size_t off, void *buf, size_t n) { - if(off + n > vm->memlen) { - return -1; - } - memcpy(buf, vm->memory + off, n); - return 0; -} - -static int vm_memory_write(struct iconv_vm *vm, size_t off, const void *buf, size_t n) { - if(off + n > vm->memlen) { - return -1; - } - memcpy(vm->memory + off, buf, n); - return 0; -} - -static int vm_execute(struct iconv_vm *vm, struct iconv_stream *in, struct iconv_stream *out) { - uint8_t insc[ICONV_INSC_LEN]; - size_t mop_nbytes; - char mop_buf[8]; - int64_t signed_calc_result; - char addr_buf[4]; - while(1) { - memset(&mop_buf, 0, 8); - if(vm_memory_read(vm, vm->pc, &insc, ICONV_INSC_LEN) == -1) { - return ICONV_ERR_MEMORY_OVERFLOW; - } - vm->pc += ICONV_INSC_LEN; - switch(insc[0]) { - case VMOP_INPUT8: - mop_nbytes = 1; - goto vmop_input; - case VMOP_INPUT16: - mop_nbytes = 2; - goto vmop_input; - case VMOP_INPUT32: - mop_nbytes = 4; - goto vmop_input; - case VMOP_INPUT64: - mop_nbytes = 8; - vmop_input: - if(iconv_stream_read(in, (mop_buf + 8 - mop_nbytes), mop_nbytes) == -1) { - return ICONV_ERR_EMPTY_STREAM; - } - vm_reg_write_raw(vm, insc[1], *(uint64_t*)&mop_buf); - break; - case VMOP_OUTPUT8: - mop_nbytes = 1; - goto vmop_output; - case VMOP_OUTPUT16: - mop_nbytes = 2; - goto vmop_output; - case VMOP_OUTPUT32: - mop_nbytes = 4; - goto vmop_output; - case VMOP_OUTPUT64: - mop_nbytes = 8; - vmop_output: - *(uint64_t*)&mop_buf = vm_reg_read_raw(vm, insc[1]); - if(iconv_stream_write(out, (mop_buf + 8 - mop_nbytes), mop_nbytes) == -1) { - return ICONV_ERR_STREAM_OVERFLOW; - } - break; - case VMOP_XOR: - vm_reg_write(vm, insc[3], vm_reg_read(vm, insc[1]) ^ vm_reg_read(vm, insc[2])); - break; - case VMOP_AND: - vm_reg_write(vm, insc[3], vm_reg_read(vm, insc[1]) & vm_reg_read(vm, insc[2])); - break; - case VMOP_OR: - vm_reg_write(vm, insc[3], vm_reg_read(vm, insc[1]) | vm_reg_read(vm, insc[2])); - break; - case VMOP_ADD: - vm_reg_write(vm, insc[3], vm_reg_read(vm, insc[1]) + vm_reg_read(vm, insc[2])); - break; - case VMOP_SUB: - vm_reg_write(vm, insc[3], vm_reg_read(vm, insc[1]) - vm_reg_read(vm, insc[2])); - break; - case VMOP_SMUL: - signed_calc_result = (int64_t)vm_reg_read(vm, insc[1]) * (int64_t)vm_reg_read(vm, insc[2]); - vm_reg_write(vm, insc[3], signed_calc_result); - break; - case VMOP_UMUL: - vm_reg_write(vm, insc[3], vm_reg_read(vm, insc[1]) * vm_reg_read(vm, insc[2])); - break; - case VMOP_EQ: - vm_reg_write(vm, insc[3], vm_reg_read(vm, insc[1]) == vm_reg_read(vm, insc[2])); - break; - case VMOP_LT: - vm_reg_write(vm, insc[3], vm_reg_read(vm, insc[1]) < vm_reg_read(vm, insc[2])); - break; - case VMOP_MT: - vm_reg_write(vm, insc[3], vm_reg_read(vm, insc[1]) > vm_reg_read(vm, insc[2])); - break; - case VMOP_SHL: - vm_reg_write(vm, insc[3], vm_reg_read(vm, insc[1]) << vm_reg_read(vm, insc[2])); - break; - case VMOP_SHR: - vm_reg_write(vm, insc[3], vm_reg_read(vm, insc[1]) >> vm_reg_read(vm, insc[2])); - break; - case VMOP_NOT: - vm_reg_write(vm, insc[2], ~vm_reg_read(vm, insc[1])); - break; - case VMOP_JNZ: - if(vm_reg_read(vm, insc[1])) { - memcpy(&addr_buf, insc + 2, 4); - goto vmop_jop; - } - break; - case VMOP_JMP: - memcpy(&addr_buf, insc + 1, 4); - vmop_jop: - vm->pc = ntohl(*(uint32_t*)&addr_buf); - break; - case VMOP_LOAD8: - mop_nbytes = 1; - goto vmop_load; - case VMOP_LOAD16: - mop_nbytes = 2; - goto vmop_load; - case VMOP_LOAD32: - mop_nbytes = 4; - goto vmop_load; - case VMOP_LOAD64: - mop_nbytes = 8; - vmop_load: - memcpy(&addr_buf, insc + 1, 4); - if(vm_memory_read(vm, ntohl(*(uint32_t*)&addr_buf), (mop_buf + 8 - mop_nbytes), mop_nbytes) == -1) { - return ICONV_ERR_MEMORY_OVERFLOW; - } - vm_reg_write_raw(vm, insc[5], *(uint64_t*)&mop_buf); - break; - case VMOP_PTRLOAD8: - mop_nbytes = 1; - goto vmop_ptr_load; - case VMOP_PTRLOAD16: - mop_nbytes = 2; - goto vmop_ptr_load; - case VMOP_PTRLOAD32: - mop_nbytes = 4; - goto vmop_ptr_load; - case VMOP_PTRLOAD64: - mop_nbytes = 8; - vmop_ptr_load: - if(vm_memory_read(vm, vm_reg_read(vm, insc[1]), (mop_buf + 8 - mop_nbytes), mop_nbytes) == -1) { - return ICONV_ERR_MEMORY_OVERFLOW; - } - vm_reg_write_raw(vm, insc[2], *(uint64_t*)&mop_buf); - break; - case VMOP_STORE64: - memcpy(&addr_buf, insc + 2, 4); - *(uint64_t*)&mop_buf = vm_reg_read_raw(vm, insc[1]); - if(vm_memory_write(vm, ntohl(*(uint32_t*)&addr_buf), &mop_buf, 8) == -1) { - return ICONV_ERR_MEMORY_OVERFLOW; - } - break; - case VMOP_RET: - return (int)vm_reg_read(vm, insc[1]); - default: - return ICONV_ERR_ILLEGAL_OPCODE; - } - } -} +#define ICONV_WORKBUF_SIZE (4096 * 4) struct iconv { - struct iconv_vm from; - struct iconv_vm to; + struct charset_library *from; + struct charset_library *to; + + char *pending; + size_t pending_len; + size_t pending_cap; + + char *workbuf; + size_t workbuf_cap; }; -static char *read_file(const char *path, size_t *size) { - struct stat st; - int fd = open(path, O_RDONLY); - if (fd == -1) { - return NULL; - } - if (fstat(fd, &st) == -1) { - return NULL; - } - if (st.st_size > 128 * 1024 || st.st_size <= sizeof(struct vm_binary_header)) { - return NULL; - } - char *buf = malloc(st.st_size); - memset(buf, 0, st.st_size); - while (read(fd, buf, st.st_size) > 0) {} - *size = st.st_size; - return buf; +static int iconv_charset_bad_name(const char *name) { + if (strlen(name) == 0) return -1; + while (*name) { + if (*name == '/') return -1; + name++; + } + return 0; } iconv_t iconv_open(const char *to, const char *from) { - size_t path_prefix_len = strlen(ICONV_CHARSET_PATH_PREFIX); - char *from_memory = NULL; - char *to_memory = NULL; - char *from_path = calloc(path_prefix_len + strlen(from) + 1, 1); - char *to_path = calloc(path_prefix_len + strlen(to) + 1, 1); - size_t from_size = 0; - size_t to_size = 0; - if (!from_path || !to_path) { - goto error; - } - strcpy(from_path, ICONV_CHARSET_PATH_PREFIX); - strcpy(to_path, ICONV_CHARSET_PATH_PREFIX); - strcpy(from_path + path_prefix_len, from); - strcpy(to_path + path_prefix_len, to); + if (iconv_charset_bad_name(from) || iconv_charset_bad_name(to)) { + errno = EINVAL; + return (iconv_t)(-1); + } - from_memory = read_file(from_path, &from_size); - to_memory = read_file(to_path, &to_size); - free(from_path); - free(to_path); - from_path = NULL; - to_path = NULL; - if (!from_memory || !to_memory) { - goto error; - } + struct iconv *iconv = malloc(sizeof(struct iconv)); + if (!iconv) { + errno = ENOMEM; + return (iconv_t)(-1); + } - struct iconv *result = malloc(sizeof(struct iconv)); - result->from = iconv_vm_new((uint8_t*)from_memory, from_size); - result->to = iconv_vm_new((uint8_t*)to_memory, to_size); + iconv->from = get_clt(from); + iconv->to = get_clt(to); + iconv->pending = NULL; + iconv->pending_len = 0; + iconv->pending_cap = 0; + iconv->workbuf = malloc(ICONV_WORKBUF_SIZE); + iconv->workbuf_cap = ICONV_WORKBUF_SIZE; - struct vm_binary_header *from_header = (struct vm_binary_header*)from_memory; - struct vm_binary_header *to_header = (struct vm_binary_header*)to_memory; - if(memcmp(&from_header->magic, &vm_binary_magic, sizeof(vm_binary_magic)) - || ntohl(from_header->version) != ICONV_CURRENT_VERSION - || memcmp(&to_header->magic, &vm_binary_magic, sizeof(vm_binary_magic)) - || ntohl(to_header->version) != ICONV_CURRENT_VERSION) { - goto error; - } + if (!iconv->from || !iconv->to || !iconv->workbuf) { + errno = iconv->workbuf ? EINVAL : ENOMEM; + free(iconv->workbuf); + free(iconv); + return (iconv_t)(-1); + } - return result; - -error: - free(from_path); - free(to_path); - free(from_memory); - free(to_memory); - errno = EINVAL; - return (iconv_t)-1; + return iconv; } -size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restrict out, size_t *restrict outb) { - struct iconv *iconv = cd; - char utf8_buf[6]; - struct iconv_stream utf8_stream; - struct iconv_stream in_stream; - struct iconv_stream out_stream; - struct vm_binary_header *from_header = (struct vm_binary_header*)iconv->from.memory; - struct vm_binary_header *to_header = (struct vm_binary_header*)iconv->to.memory; - utf8_stream.buf = utf8_buf; - utf8_stream.off = 0; - utf8_stream.len = 6; - in_stream.buf = *in; - in_stream.off = 0; - in_stream.len = *inb; - out_stream.buf = *out; - out_stream.off = 0; - out_stream.len = *outb; - int status; +size_t iconv( + iconv_t cd, + char **restrict in, size_t *restrict inb, + char **restrict out, size_t *restrict outb +) { + if ((ptrdiff_t)cd == -1) { + errno = EBADF; + return (size_t)-1; + } - for(;;) { - iconv->from.pc = ntohl(from_header->to_utf8_addr); - iconv->to.pc = ntohl(to_header->from_utf8_addr); - status = vm_execute(&iconv->from, &in_stream, &utf8_stream); - if(status < 0) break; - utf8_stream.off = 0; - status = vm_execute(&iconv->to, &utf8_stream, &out_stream); - if(status < 0) break; - utf8_stream.off = 0; - } - *in += in_stream.off; - *out += out_stream.off; - *inb -= in_stream.off; - *outb -= out_stream.off; - return in_stream.off; + struct iconv *ic = cd; + + if (in == NULL || *in == NULL) return 0; + + if (ic->pending_len > 0) { + char *psrc = ic->pending; + char *pin = psrc; + size_t pinb = ic->pending_len; + + int pr = ic->to->from_utf8(&pin, &pinb, out, outb); + size_t pconsumed = (size_t)(pin - psrc); + + if (pconsumed > 0) { + memmove(ic->pending, ic->pending + pconsumed, + ic->pending_len - pconsumed); + ic->pending_len -= pconsumed; + } + + if (pr == -2) { errno = EILSEQ; return (size_t)-1; } + if (ic->pending_len > 0) { errno = E2BIG; return (size_t)-1; } + } + + while (*inb > 0) { + size_t chunk = *inb; + if (chunk > ic->workbuf_cap / 4) + chunk = ic->workbuf_cap / 4; + + char *utf8buf = ic->workbuf; + char *utf8 = utf8buf; + size_t utf8b = ic->workbuf_cap; + + char *inp = *in; + size_t inbn = chunk; + + int r = ic->from->to_utf8(&inp, &inbn, &utf8, &utf8b); + + size_t consumed = (size_t)(inp - *in); + *in = inp; + *inb -= consumed; + + size_t produced = (size_t)(utf8 - utf8buf); + + if (produced > 0) { + char *wsrc = utf8buf; + size_t wn = produced; + + for (;;) { + char *winp = wsrc; + size_t winbn = wn; + int r2 = ic->to->from_utf8(&winp, &winbn, out, outb); + size_t wconsumed = (size_t)(winp - wsrc); + wsrc += wconsumed; + wn -= wconsumed; + + if (r2 == -2) { errno = EILSEQ; return (size_t)-1; } + if (r2 == 0) break; + + if (wconsumed == 0) { + if (ic->pending_len + wn > ic->pending_cap) { + size_t newcap = ic->pending_len + wn; + char *newp = realloc(ic->pending, newcap); + if (!newp) { errno = ENOMEM; return (size_t)-1; } + ic->pending = newp; + ic->pending_cap = newcap; + } + memcpy(ic->pending + ic->pending_len, wsrc, wn); + ic->pending_len += wn; + errno = E2BIG; + return (size_t)-1; + } + } + } + + if (r == -2) { errno = EILSEQ; return (size_t)-1; } + + if (r == -1) { + if (consumed == 0) { + errno = EINVAL; + return (size_t)-1; + } + continue; + } + + if (consumed == 0) { + errno = EILSEQ; + return (size_t)-1; + } + } + + return 0; } int iconv_close(iconv_t cd) { - struct iconv *iconv = cd; - free(iconv->from.memory); - free(iconv->to.memory); - free(iconv); - return 0; + if ((ptrdiff_t)cd == -1) { + errno = EBADF; + return -1; + } + + struct iconv *ic = cd; + free(ic->workbuf); + free(ic->pending); + free(ic); + return 0; } diff --git a/tools/compile-iconv.rs b/tools/compile-iconv.rs deleted file mode 100644 index 210b2e6f..00000000 --- a/tools/compile-iconv.rs +++ /dev/null @@ -1,147 +0,0 @@ -#[derive(Debug, Clone)] -struct Octo { - tag: Option, - content: [u8; 8], - reloc: Vec<(usize, String)>, -} - -fn main() { - let mut args = std::env::args().skip(1); - let src = args.next().expect("please specify source file"); - let dst = args.next().expect("please specify output file"); - - let mut octos = Vec::new(); - let src_content = std::fs::read_to_string(&src).expect("cannot read source file"); - let mut current_tag = None; - for (nline, line) in src_content.lines().enumerate() { - let line = line.trim(); - if line.is_empty() || line.starts_with("//") { - continue; - } - if let Some(tag) = line.strip_suffix(":") { - current_tag = Some(tag); - continue; - } - let Some((op, arglist)) = line.split_once(' ') else { - error(nline, "operation without arglist is currently disallowed"); - }; - let mut content = [0u8; 8]; - content[0] = do_op(op).unwrap_or_else(|| error(nline, "unrecognized op")); - let mut cursor = 1; - if content[0] == 0 { - cursor = 0; - } - let reloc = do_arglist(arglist, &mut content, &mut cursor); - octos.push(Octo { - tag: current_tag.take().map(ToString::to_string), - content, - reloc, - }); - } - - let mut tags = std::collections::HashMap::new(); - for (nocto, octo) in octos.iter().enumerate() { - if let Some(tag) = &octo.tag { - tags.insert(tag.to_string(), nocto * 8); - } - } - - let mut content = Vec::new(); - for octo in octos { - let mut current = octo.content.to_vec(); - for (offset, tag) in octo.reloc { - let tag_addr = *tags.get(&tag).expect(&format!("tag {tag} undefined")) as u32; - current[offset..offset + 4].copy_from_slice(&tag_addr.to_be_bytes()[..]); - } - content.append(&mut current); - } - - std::fs::write(dst, &content).expect("failed to write dst file"); -} - -fn error(line: usize, reason: &str) -> ! { - eprintln!("error: {reason} (source:{})", line + 1); - std::process::exit(1); -} - -fn do_op(s: &str) -> Option { - match s { - "place64" => Some(0), - "input8" => Some(1), - "input16" => Some(2), - "input32" => Some(3), - "input64" => Some(4), - "output8" => Some(5), - "output16" => Some(6), - "output32" => Some(7), - "output64" => Some(8), - "xor" => Some(9), - "and" => Some(10), - "or" => Some(11), - "add" => Some(13), - "sub" => Some(14), - "smul" => Some(15), - "umul" => Some(17), - "eq" => Some(19), - "mt" => Some(20), - "lt" => Some(21), - "shl" => Some(29), - "shr" => Some(30), - "not" => Some(12), - "jnz" => Some(22), - "jmp" => Some(23), - "load8" => Some(24), - "load16" => Some(25), - "load32" => Some(26), - "load64" => Some(27), - "store64" => Some(28), - "ret" => Some(31), - "ptrload8" => Some(33), - "ptrload16" => Some(34), - "ptrload32" => Some(35), - "ptrload64" => Some(36), - _ => None, - } -} - -fn do_arglist(s: &str, content: &mut [u8; 8], cursor: &mut usize) -> Vec<(usize, String)> { - let mut reloc = Vec::new(); - for arg in s.split(',') { - let arg = arg.trim(); - if let Some(x) = do_arg(arg, content, cursor) { - reloc.push(x); - } - } - reloc -} - -fn do_arg(s: &str, content: &mut [u8; 8], cursor: &mut usize) -> Option<(usize, String)> { - if let Ok(n) = s.parse::() { - *cursor = 8; - *content = n.to_be_bytes(); - None - } else if let Some(digit) = s.strip_prefix('0') { - let radix = match digit.as_bytes()[0] { - b'x' => 16, - b'o' => 8, - b'b' => 2, - _ => panic!("unknown radix"), - }; - *cursor = 8; - *content = u64::from_str_radix(&digit[1..], radix) - .unwrap() - .to_be_bytes(); - None - } else if let Some(reg_id) = s.strip_prefix("&r") { - content[*cursor] = reg_id.parse::().unwrap(); - *cursor += 1; - None - } else if s == "@sicv_magic_v1" { - *content = [b'S', b'I', b'C', b'V', 0, 0, 0, 1]; - *cursor = 8; - None - } else { - *cursor += 4; - Some((*cursor - 4, s.into())) - } -}