From 3fb9b31192fdacc634099d5fa35a73fbd011bfa9 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Thu, 29 Mar 2018 03:08:43 +0000 Subject: [PATCH] fixed reply encoding in the unicode mode in the server --- lib/Makefile.in | 1 - lib/hcl-c.c | 9 ++--- lib/hcl-s.c | 89 +++++++++++++++++++++++++++---------------------- lib/main-c.c | 27 ++++++++++++--- 4 files changed, 77 insertions(+), 49 deletions(-) diff --git a/lib/Makefile.in b/lib/Makefile.in index 39e9f5b..03ab35c 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -420,7 +420,6 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ -runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ diff --git a/lib/hcl-c.c b/lib/hcl-c.c index a330f06..eef25ea 100644 --- a/lib/hcl-c.c +++ b/lib/hcl-c.c @@ -231,8 +231,10 @@ static HCL_INLINE int is_token_integer (hcl_client_t* client, hcl_oow_t* value) return 1; } -static HCL_INLINE hcl_ooch_t escape (hcl_ooch_t c) +static HCL_INLINE hcl_ooch_t unescape (hcl_ooch_t c) { + /* as of this writing, the server side only escapes \ and ". + * i don't know if n, r, f, t, v should be supported here */ switch (c) { case 'n': return '\n'; @@ -364,8 +366,7 @@ static int handle_char (hcl_client_t* client, hcl_ooci_t c, hcl_oow_t nbytes) { if (client->rep.u.reply_value_quoted.escaped) { - c = escape(c); - /* TODO: more escaping handling \xXXX \uXXXX \UXXXXX */ + c = unescape(c); } else if (c == '\\') { @@ -540,7 +541,7 @@ static int handle_char (hcl_client_t* client, hcl_ooci_t c, hcl_oow_t nbytes) { if (client->rep.u.attr_value_quoted.escaped) { - c = escape(c); + c = unescape(c); /* TODO: more escaping handling like \0NNN \xXXXX \uXXXX \UXXXX */ } else if (c == '\\') diff --git a/lib/hcl-s.c b/lib/hcl-s.c index a918938..685b0a7 100644 --- a/lib/hcl-s.c +++ b/lib/hcl-s.c @@ -1178,14 +1178,49 @@ void hcl_server_proto_start_reply (hcl_server_proto_t* proto) int hcl_server_proto_feed_reply (hcl_server_proto_t* proto, const hcl_ooch_t* ptr, hcl_oow_t len, int escape) { -#if defined(HCL_OOCH_IS_BCH) - /* nothing */ -#else +#if defined(HCL_OOCH_IS_UCH) hcl_oow_t bcslen, ucslen, donelen; int x; -#endif -#if defined(HCL_OOCH_IS_BCH) + donelen = 0; + while (donelen < len) + { + if (escape) + { + if (ptr[donelen] == '\\' || ptr[donelen] == '\"') + { + /* i know that these characters don't need encoding conversion */ + if (proto->reply.len >= HCL_COUNTOF(proto->reply.buf) && write_reply_chunk(proto) <=-1) return -1; + proto->reply.buf[proto->reply.len++] = '\\'; + } + bcslen = HCL_COUNTOF(proto->reply.buf) - proto->reply.len; + if (bcslen < HCL_BCSIZE_MAX) + { + if (write_reply_chunk(proto) <=-1) return -1; + bcslen = HCL_COUNTOF(proto->reply.buf) - proto->reply.len; + } + ucslen = 1; /* i must go one by one for escaping */ + } + else + { + bcslen = HCL_COUNTOF(proto->reply.buf) - proto->reply.len; + if (bcslen < HCL_BCSIZE_MAX) + { + if (write_reply_chunk(proto) <=-1) return -1; + bcslen = HCL_COUNTOF(proto->reply.buf) - proto->reply.len; + } + ucslen = len - donelen; + } + + x = hcl_convootobchars(proto->hcl, &ptr[donelen], &ucslen, &proto->reply.buf[proto->reply.len], &bcslen); + if (x <= -1 && ucslen <= 0) return -1; + + donelen += ucslen; + proto->reply.len += bcslen; + } + + return 0; +#else while (len > 0) { if (escape && (*ptr == '\\' || *ptr == '\"')) @@ -1200,33 +1235,7 @@ int hcl_server_proto_feed_reply (hcl_server_proto_t* proto, const hcl_ooch_t* pt } return 0; -#else - donelen = 0; - while (donelen < len) - { - if (escape && (*ptr == '\\' || *ptr == '\"')) - { - /* i know that these characters don't need encoding conversion */ - if (proto->reply.len >= HCL_COUNTOF(proto->reply.buf) && write_reply_chunk(proto) <=-1) return -1; - proto->reply.buf[proto->reply.len++] = '\\'; - } - - bcslen = HCL_COUNTOF(proto->reply.buf) - proto->reply.len; - if (bcslen < HCL_BCSIZE_MAX) - { - if (write_reply_chunk(proto) <=-1) return -1; - bcslen = HCL_COUNTOF(proto->reply.buf) - proto->reply.len; - } - ucslen = len - donelen; - - x = hcl_convootobchars(proto->hcl, &ptr[donelen], &ucslen, &proto->reply.buf[proto->reply.len], &bcslen); - if (x <= -1 && ucslen <= 0) return -1; - - donelen += ucslen; - proto->reply.len += bcslen; - } #endif - return 0; } int hcl_server_proto_end_reply (hcl_server_proto_t* proto, const hcl_ooch_t* failmsg) @@ -2254,14 +2263,15 @@ static void set_err_with_syserr (hcl_server_t* server, int syserr, const char* b hcl_seterrbfmtv (hcl, errnum, bfmt, ap); va_end (ap); - #if defined(HCL_OOCH_IS_BCH) - hcl->errmsg.len += hcl_copybcstr(&hcl->errmsg.buf[hcl->errmsg.len], HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len, b_dash); - hcl->errmsg.len += hcl_copybcstr(&hcl->errmsg.buf[hcl->errmsg.len], HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len, hcl->errmsg.tmpbuf.bch); - #else + #if defined(HCL_OOCH_IS_UCH) hcl->errmsg.len += hcl_copyucstr(&hcl->errmsg.buf[hcl->errmsg.len], HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len, u_dash); tmplen2 = HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len; hcl_convbtoucstr (hcl, hcl->errmsg.tmpbuf.bch, &tmplen, &hcl->errmsg.buf[hcl->errmsg.len], &tmplen2); hcl->errmsg.len += tmplen2; /* ignore conversion errors */ + #else + hcl->errmsg.len += hcl_copybcstr(&hcl->errmsg.buf[hcl->errmsg.len], HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len, b_dash); + hcl->errmsg.len += hcl_copybcstr(&hcl->errmsg.buf[hcl->errmsg.len], HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len, hcl->errmsg.tmpbuf.bch); + #endif } else @@ -2274,14 +2284,14 @@ static void set_err_with_syserr (hcl_server_t* server, int syserr, const char* b hcl_seterrbfmtv (hcl, errnum, bfmt, ap); va_end (ap); - #if defined(HCL_OOCH_IS_BCH) + #if defined(HCL_OOCH_IS_UCH) + hcl->errmsg.len += hcl_copyucstr(&hcl->errmsg.buf[hcl->errmsg.len], HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len, u_dash); + hcl->errmsg.len += hcl_copyucstr(&hcl->errmsg.buf[hcl->errmsg.len], HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len, hcl->errmsg.tmpbuf.uch); + #else hcl->errmsg.len += hcl_copybcstr(&hcl->errmsg.buf[hcl->errmsg.len], HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len, b_dash); tmplen2 = HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len; hcl_convutobcstr (hcl, hcl->errmsg.tmpbuf.uch, &tmplen, &hcl->errmsg.buf[hcl->errmsg.len], &tmplen2); hcl->errmsg.len += tmplen2; /* ignore conversion errors */ - #else - hcl->errmsg.len += hcl_copyucstr(&hcl->errmsg.buf[hcl->errmsg.len], HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len, u_dash); - hcl->errmsg.len += hcl_copyucstr(&hcl->errmsg.buf[hcl->errmsg.len], HCL_COUNTOF(hcl->errmsg.buf) - hcl->errmsg.len, hcl->errmsg.tmpbuf.uch); #endif } @@ -2748,6 +2758,7 @@ void hcl_server_seterrufmt (hcl_server_t* server, hcl_errnum_t errnum, const hcl HCL_ASSERT (server->dummy_hcl, HCL_COUNTOF(server->errmsg.buf) == HCL_COUNTOF(server->dummy_hcl->errmsg.buf)); server->errnum = errnum; + server->errnum = errnum; hcl_copyoochars (server->errmsg.buf, server->dummy_hcl->errmsg.buf, HCL_COUNTOF(server->errmsg.buf)); server->errmsg.len = server->dummy_hcl->errmsg.len; } diff --git a/lib/main-c.c b/lib/main-c.c index b51fd44..dab2b3d 100644 --- a/lib/main-c.c +++ b/lib/main-c.c @@ -449,18 +449,33 @@ static int start_reply (hcl_client_t* client, hcl_client_reply_type_t type, cons if (client_xtn->reply_count > 0) { - hcl_client_seterrbfmt (client, HCL_EFLOOD, "redundant reply received\n"); + hcl_client_seterrbfmt (client, HCL_EFLOOD, "\n<> redundant reply received\n"); return -1; } if (dptr) { /* short-form response - no end_reply will be called */ - + if (type == HCL_CLIENT_REPLY_TYPE_ERROR) + { + #if defined(HCL_OOCH_IS_UCH) + hcl_bch_t bcs[256]; + hcl_oow_t bcslen; + + hcl_conv_ucsn_to_bcsn_with_cmgr (dptr, &dlen, bcs, &bcslen, hcl_client_getcmgr(client)); + printf ("\nERROR - [%.*s]\n", (int)bcslen, bcs); + #else + printf ("\nERROR - [%.*s]\n", (int)dlen, dptr); + #endif + } + else + { + printf ("\nTOTAL DATA %lu bytes\n", (unsigned long int)client_xtn->data_length); + } - client_xtn->reply_count++; /*fflush (stdout);*/ - printf ("\nTOTAL DATA %lu bytes\n", (unsigned long int)client_xtn->data_length); + client_xtn->reply_count++; + } else { @@ -477,6 +492,8 @@ static int end_reply (hcl_client_t* client, hcl_client_end_reply_state_t state) if (state == HCL_CLIENT_END_REPLY_STATE_REVOKED) { /* nothing to do here */ + printf ("\n<> REPLY(%lu bytes) received so far has been revoked\n", (unsigned long int)client_xtn->data_length); + client_xtn->data_length = 0; } else { @@ -501,7 +518,7 @@ static int feed_data (hcl_client_t* client, const void* ptr, hcl_oow_t len) if (write_all(0, ptr, len) <= -1) { - hcl_client_seterrbfmt (client, HCL_EIOERR, "unabled to write data"); + hcl_client_seterrbfmt (client, HCL_EIOERR, "unable to write data"); return -1; }