From f6a10aecc218eddbbe13b61cb10c1d2afd1edf79 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 20 Nov 2016 17:45:04 -0800 Subject: [PATCH] WIP --- gaiji.c | 80 ++++++++++++++++++++++++++------------------------------- util.c | 12 ++++++++- 2 files changed, 48 insertions(+), 44 deletions(-) diff --git a/gaiji.c b/gaiji.c index 7852086..fac6fbe 100644 --- a/gaiji.c +++ b/gaiji.c @@ -101,46 +101,6 @@ static void encode_sequence(char output[], int size, const char utf8[]) { strncat(output, "}", size); } -static void decode_sequence(char output[], int size, const char input[]) { - const char* ptr_in = input; - char* ptr_out = output; - bool decode = false; - - while (*ptr_in != 0 && ptr_out - output < size - 1) { - if (strncmp(ptr_in, "{#", 2) == 0) { - decode = true; - ptr_in += 2; - } - - if (decode) { - const char high_ascii = *ptr_in++; - if (high_ascii == 0) { - break; - } - - const char low_ascii = *ptr_in++; - if (low_ascii == 0) { - break; - } - - if (high_ascii == '}') { - decode = false; - --ptr_in; - } - else { - assert(is_ascii_nibble(high_ascii)); - assert(is_ascii_nibble(low_ascii)); - *ptr_out++ = ascii_to_nibble(high_ascii) << 0x04 | ascii_to_nibble(low_ascii); - } - } - else { - *ptr_out++ = *ptr_in++; - } - } - - *ptr_out = 0; -} - /* * Exported functions */ @@ -194,7 +154,41 @@ void gaiji_build_stub(char output[], int size, int code, const Gaiji_Context* co } void gaiji_fixup_stub(char output[], int size, const char input[]) { - (void)output; - (void)size; - (void)input; + const char* ptr_in = input; + char* ptr_out = output; + bool decode = false; + + while (*ptr_in != 0 && ptr_out - output < size - 1) { + if (strncmp(ptr_in, "{#", 2) == 0) { + decode = true; + ptr_in += 2; + } + + if (decode) { + const char high_ascii = *ptr_in++; + if (high_ascii == 0) { + break; + } + + const char low_ascii = *ptr_in++; + if (low_ascii == 0) { + break; + } + + if (high_ascii == '}') { + decode = false; + --ptr_in; + } + else { + assert(is_ascii_nibble(high_ascii)); + assert(is_ascii_nibble(low_ascii)); + *ptr_out++ = ascii_to_nibble(high_ascii) << 0x04 | ascii_to_nibble(low_ascii); + } + } + else { + *ptr_out++ = *ptr_in++; + } + } + + *ptr_out = 0; } diff --git a/util.c b/util.c index d0de669..1d787fc 100644 --- a/util.c +++ b/util.c @@ -115,7 +115,17 @@ char* read_book_data(EB_Book* book, EB_Hookset* hookset, Gaiji_Context* context, return NULL; } - return error == EB_SUCCESS ? eucjp_to_utf8(data) : NULL; + if (error != EB_SUCCESS) { + return NULL; + } + + char * result = eucjp_to_utf8(data); + if (result == NULL) { + return NULL; + } + + gaiji_fixup_stub(result, strlen(result) + 1, result); + return result; } void free_book(Book* book) {