fixed reply encoding in the unicode mode in the server

This commit is contained in:
hyung-hwan 2018-03-29 03:08:43 +00:00
parent 116512ae9d
commit 3fb9b31192
4 changed files with 77 additions and 49 deletions

View File

@ -420,7 +420,6 @@ pdfdir = @pdfdir@
prefix = @prefix@
program_transform_name = @program_transform_name@
psdir = @psdir@
runstatedir = @runstatedir@
sbindir = @sbindir@
sharedstatedir = @sharedstatedir@
srcdir = @srcdir@

View File

@ -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 == '\\')

View File

@ -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;
}

View File

@ -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<<WARNING>> 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<<WARNING>> 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;
}