2015-10-14 13:25:36 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2016-05-15 16:01:43 +00:00
|
|
|
Copyright (c) 2014-2016 Chung, Hyung-Hwan. All rights reserved.
|
2015-10-14 13:25:36 +00:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
#include "moo-prv.h"
|
2016-11-27 15:37:14 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
#define MOO_BCLEN_MAX 6
|
2015-10-14 13:25:36 +00:00
|
|
|
|
2016-12-05 15:44:53 +00:00
|
|
|
/* some naming conventions
|
|
|
|
* bchars, uchars -> pointer and length
|
|
|
|
* bcstr, ucstr -> null-terminated string pointer
|
2016-12-27 18:15:35 +00:00
|
|
|
* btouchars -> bchars to uchars
|
|
|
|
* utobchars -> uchars to bchars
|
|
|
|
* btoucstr -> bcstr to ucstr
|
|
|
|
* utobcstr -> ucstr to bcstr
|
2016-12-05 15:44:53 +00:00
|
|
|
*/
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t moo_hashbytes (const moo_oob_t* ptr, moo_oow_t len)
|
2015-10-28 14:58:58 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t h = 0;
|
|
|
|
const moo_uint8_t* bp, * be;
|
2015-10-28 14:58:58 +00:00
|
|
|
|
|
|
|
bp = ptr; be = bp + len;
|
|
|
|
while (bp < be) h = h * 31 + *bp++;
|
|
|
|
|
2017-01-06 13:27:49 +00:00
|
|
|
/* constrain the hash value to be representable in a small integer
|
|
|
|
* for convenience sake */
|
2017-01-09 09:54:49 +00:00
|
|
|
return h % ((moo_oow_t)MOO_SMOOI_MAX + 1);
|
2015-10-28 14:58:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_equaluchars (const moo_uch_t* str1, const moo_uch_t* str2, moo_oow_t len)
|
2016-11-29 05:25:08 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t i;
|
2016-11-29 05:25:08 +00:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (str1[i] != str2[i]) return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_equalbchars (const moo_bch_t* str1, const moo_bch_t* str2, moo_oow_t len)
|
2015-10-14 13:25:36 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t i;
|
2015-10-14 13:25:36 +00:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (str1[i] != str2[i]) return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_compucstr (const moo_uch_t* str1, const moo_uch_t* str2)
|
2015-10-14 13:25:36 +00:00
|
|
|
{
|
|
|
|
while (*str1 == *str2)
|
|
|
|
{
|
|
|
|
if (*str1 == '\0') return 0;
|
|
|
|
str1++, str2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*str1 > *str2)? 1: -1;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_compbcstr (const moo_bch_t* str1, const moo_bch_t* str2)
|
2015-10-14 13:25:36 +00:00
|
|
|
{
|
|
|
|
while (*str1 == *str2)
|
|
|
|
{
|
|
|
|
if (*str1 == '\0') return 0;
|
|
|
|
str1++, str2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*str1 > *str2)? 1: -1;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_compucbcstr (const moo_uch_t* str1, const moo_bch_t* str2)
|
2015-10-14 13:25:36 +00:00
|
|
|
{
|
|
|
|
while (*str1 == *str2)
|
|
|
|
{
|
|
|
|
if (*str1 == '\0') return 0;
|
|
|
|
str1++, str2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*str1 > *str2)? 1: -1;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_compucharsbcstr (const moo_uch_t* str1, moo_oow_t len, const moo_bch_t* str2)
|
2015-10-14 13:25:36 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* end = str1 + len;
|
2015-10-14 13:25:36 +00:00
|
|
|
while (str1 < end && *str2 != '\0' && *str1 == *str2) str1++, str2++;
|
|
|
|
if (str1 == end && *str2 == '\0') return 0;
|
|
|
|
if (*str1 == *str2) return (str1 < end)? 1: -1;
|
2016-12-31 17:19:40 +00:00
|
|
|
return (*str1 > *str2)? 1: -1;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_compbcharsbcstr (const moo_bch_t* str1, moo_oow_t len, const moo_bch_t* str2)
|
2016-12-31 17:19:40 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* end = str1 + len;
|
2016-12-31 17:19:40 +00:00
|
|
|
while (str1 < end && *str2 != '\0' && *str1 == *str2) str1++, str2++;
|
|
|
|
if (str1 == end && *str2 == '\0') return 0;
|
|
|
|
if (*str1 == *str2) return (str1 < end)? 1: -1;
|
2015-10-14 13:25:36 +00:00
|
|
|
return (*str1 > *str2)? 1: -1;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
void moo_copyuchars (moo_uch_t* dst, const moo_uch_t* src, moo_oow_t len)
|
2015-10-14 13:25:36 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t i;
|
2015-10-14 13:25:36 +00:00
|
|
|
for (i = 0; i < len; i++) dst[i] = src[i];
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
void moo_copybchars (moo_bch_t* dst, const moo_bch_t* src, moo_oow_t len)
|
2015-10-28 14:58:58 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t i;
|
2015-10-28 14:58:58 +00:00
|
|
|
for (i = 0; i < len; i++) dst[i] = src[i];
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
void moo_copybtouchars (moo_uch_t* dst, const moo_bch_t* src, moo_oow_t len)
|
2015-10-14 13:25:36 +00:00
|
|
|
{
|
2016-12-27 18:15:35 +00:00
|
|
|
/* copy without conversions.
|
2017-01-09 09:54:49 +00:00
|
|
|
* use moo_bctouchars() for conversion encoding */
|
|
|
|
moo_oow_t i;
|
2015-10-14 13:25:36 +00:00
|
|
|
for (i = 0; i < len; i++) dst[i] = src[i];
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t moo_copyucstr (moo_uch_t* dst, moo_oow_t len, const moo_uch_t* src)
|
2015-10-14 13:25:36 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_uch_t* p, * p2;
|
2015-10-14 13:25:36 +00:00
|
|
|
|
|
|
|
p = dst; p2 = dst + len - 1;
|
|
|
|
|
|
|
|
while (p < p2)
|
|
|
|
{
|
|
|
|
if (*src == '\0') break;
|
|
|
|
*p++ = *src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 0) *p = '\0';
|
|
|
|
return p - dst;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t moo_copybcstr (moo_bch_t* dst, moo_oow_t len, const moo_bch_t* src)
|
2015-10-14 13:25:36 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_bch_t* p, * p2;
|
2015-10-14 13:25:36 +00:00
|
|
|
|
|
|
|
p = dst; p2 = dst + len - 1;
|
|
|
|
|
|
|
|
while (p < p2)
|
|
|
|
{
|
|
|
|
if (*src == '\0') break;
|
|
|
|
*p++ = *src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 0) *p = '\0';
|
|
|
|
return p - dst;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t moo_countucstr (const moo_uch_t* str)
|
2015-11-03 14:08:43 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* ptr = str;
|
2015-11-03 14:08:43 +00:00
|
|
|
while (*ptr != '\0') ptr++;
|
|
|
|
return ptr - str;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t moo_countbcstr (const moo_bch_t* str)
|
2015-11-03 14:08:43 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* ptr = str;
|
2015-11-03 14:08:43 +00:00
|
|
|
while (*ptr != '\0') ptr++;
|
|
|
|
return ptr - str;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_uch_t* moo_finduchar (const moo_uch_t* ptr, moo_oow_t len, moo_uch_t c)
|
2015-10-14 13:25:36 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* end;
|
2015-10-14 13:25:36 +00:00
|
|
|
|
|
|
|
end = ptr + len;
|
|
|
|
while (ptr < end)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (*ptr == c) return (moo_uch_t*)ptr;
|
2015-10-14 13:25:36 +00:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2015-10-14 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_bch_t* moo_findbchar (const moo_bch_t* ptr, moo_oow_t len, moo_bch_t c)
|
2015-10-28 14:58:58 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* end;
|
2015-10-28 14:58:58 +00:00
|
|
|
|
|
|
|
end = ptr + len;
|
|
|
|
while (ptr < end)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
if (*ptr == c) return (moo_bch_t*)ptr;
|
2015-10-28 14:58:58 +00:00
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2015-10-28 14:58:58 +00:00
|
|
|
}
|
2016-11-27 15:37:14 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_uch_t* moo_rfinduchar (const moo_uch_t* ptr, moo_oow_t len, moo_uch_t c)
|
2016-12-05 15:44:53 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* cur;
|
2016-12-05 15:44:53 +00:00
|
|
|
|
|
|
|
cur = ptr + len;
|
|
|
|
while (cur > ptr)
|
|
|
|
{
|
|
|
|
--cur;
|
2017-01-09 09:54:49 +00:00
|
|
|
if (*cur == c) return (moo_uch_t*)cur;
|
2016-12-05 15:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2016-12-05 15:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_bch_t* moo_rfindbchar (const moo_bch_t* ptr, moo_oow_t len, moo_bch_t c)
|
2016-12-05 15:44:53 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* cur;
|
2016-12-05 15:44:53 +00:00
|
|
|
|
|
|
|
cur = ptr + len;
|
|
|
|
while (cur > ptr)
|
|
|
|
{
|
|
|
|
--cur;
|
2017-01-09 09:54:49 +00:00
|
|
|
if (*cur == c) return (moo_bch_t*)cur;
|
2016-12-05 15:44:53 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
2016-12-05 15:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_concatoocstrtosbuf (moo_t* moo, const moo_ooch_t* str, int id)
|
2016-12-05 15:44:53 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_sbuf_t* p;
|
|
|
|
moo_oow_t len;
|
2016-12-05 15:44:53 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
p = &moo->sbuf[id];
|
|
|
|
len = moo_countoocstr (str);
|
2016-12-05 15:44:53 +00:00
|
|
|
|
|
|
|
if (len > p->capa - p->len)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t newcapa;
|
|
|
|
moo_ooch_t* tmp;
|
2016-12-05 15:44:53 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
newcapa = MOO_ALIGN(p->len + len, 512); /* TODO: adjust this capacity */
|
2016-12-05 15:44:53 +00:00
|
|
|
|
|
|
|
/* +1 to handle line ending injection more easily */
|
2017-01-09 09:54:49 +00:00
|
|
|
tmp = moo_reallocmem (moo, p->ptr, (newcapa + 1) * MOO_SIZEOF(*tmp));
|
2016-12-05 15:44:53 +00:00
|
|
|
if (!tmp) return -1;
|
|
|
|
|
|
|
|
p->ptr = tmp;
|
|
|
|
p->capa = newcapa;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_copyoochars (&p->ptr[p->len], str, len);
|
2016-12-05 15:44:53 +00:00
|
|
|
p->len += len;
|
|
|
|
p->ptr[p->len] = '\0';
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_copyoocstrtosbuf (moo_t* moo, const moo_ooch_t* str, int id)
|
2016-12-05 15:44:53 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo->sbuf[id].len = 0;;
|
|
|
|
return moo_concatoocstrtosbuf (moo, str, id);
|
2016-12-05 15:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-27 15:37:14 +00:00
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static MOO_INLINE int bcsn_to_ucsn_with_cmgr (
|
|
|
|
const moo_bch_t* bcs, moo_oow_t* bcslen,
|
|
|
|
moo_uch_t* ucs, moo_oow_t* ucslen, moo_cmgr_t* cmgr, int all)
|
2016-11-27 15:37:14 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* p;
|
2016-11-27 15:37:14 +00:00
|
|
|
int ret = 0;
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t mlen;
|
2016-11-27 15:37:14 +00:00
|
|
|
|
|
|
|
if (ucs)
|
|
|
|
{
|
|
|
|
/* destination buffer is specified.
|
|
|
|
* copy the conversion result to the buffer */
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_uch_t* q, * qend;
|
2016-11-27 15:37:14 +00:00
|
|
|
|
|
|
|
p = bcs;
|
|
|
|
q = ucs;
|
|
|
|
qend = ucs + *ucslen;
|
|
|
|
mlen = *bcslen;
|
|
|
|
|
|
|
|
while (mlen > 0)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t n;
|
2016-11-27 15:37:14 +00:00
|
|
|
|
|
|
|
if (q >= qend)
|
|
|
|
{
|
|
|
|
/* buffer too small */
|
|
|
|
ret = -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = cmgr->bctouc (p, mlen, q);
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* invalid sequence */
|
|
|
|
if (all)
|
|
|
|
{
|
|
|
|
n = 1;
|
|
|
|
*q = '?';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n > mlen)
|
|
|
|
{
|
|
|
|
/* incomplete sequence */
|
|
|
|
if (all)
|
|
|
|
{
|
|
|
|
n = 1;
|
|
|
|
*q = '?';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
q++;
|
|
|
|
p += n;
|
|
|
|
mlen -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ucslen = q - ucs;
|
|
|
|
*bcslen = p - bcs;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no destination buffer is specified. perform conversion
|
|
|
|
* but don't copy the result. the caller can call this function
|
|
|
|
* without a buffer to find the required buffer size, allocate
|
|
|
|
* a buffer with the size and call this function again with
|
|
|
|
* the buffer. */
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_uch_t w;
|
|
|
|
moo_oow_t wlen = 0;
|
2016-11-27 15:37:14 +00:00
|
|
|
|
|
|
|
p = bcs;
|
|
|
|
mlen = *bcslen;
|
|
|
|
|
|
|
|
while (mlen > 0)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t n;
|
2016-11-27 15:37:14 +00:00
|
|
|
|
|
|
|
n = cmgr->bctouc (p, mlen, &w);
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
/* invalid sequence */
|
|
|
|
if (all) n = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n > mlen)
|
|
|
|
{
|
|
|
|
/* incomplete sequence */
|
|
|
|
if (all) n = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = -3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p += n;
|
|
|
|
mlen -= n;
|
|
|
|
wlen += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ucslen = wlen;
|
|
|
|
*bcslen = p - bcs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static MOO_INLINE int bcs_to_ucs_with_cmgr (
|
|
|
|
const moo_bch_t* bcs, moo_oow_t* bcslen,
|
|
|
|
moo_uch_t* ucs, moo_oow_t* ucslen, moo_cmgr_t* cmgr, int all)
|
2016-11-27 15:37:14 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_bch_t* bp;
|
|
|
|
moo_oow_t mlen, wlen;
|
2016-11-27 15:37:14 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
for (bp = bcs; *bp != '\0'; bp++) /* nothing */ ;
|
|
|
|
|
|
|
|
mlen = bp - bcs; wlen = *ucslen;
|
|
|
|
n = bcsn_to_ucsn_with_cmgr (bcs, &mlen, ucs, &wlen, cmgr, all);
|
|
|
|
if (ucs)
|
|
|
|
{
|
|
|
|
/* null-terminate the target buffer if it has room for it. */
|
|
|
|
if (wlen < *ucslen) ucs[wlen] = '\0';
|
|
|
|
else n = -2; /* buffer too small */
|
|
|
|
}
|
|
|
|
*bcslen = mlen; *ucslen = wlen;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static MOO_INLINE int ucsn_to_bcsn_with_cmgr (
|
|
|
|
const moo_uch_t* ucs, moo_oow_t* ucslen,
|
|
|
|
moo_bch_t* bcs, moo_oow_t* bcslen, moo_cmgr_t* cmgr)
|
2016-11-27 15:37:14 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* p = ucs;
|
|
|
|
const moo_uch_t* end = ucs + *ucslen;
|
2016-11-27 15:37:14 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (bcs)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t rem = *bcslen;
|
2016-11-27 15:37:14 +00:00
|
|
|
|
|
|
|
while (p < end)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t n;
|
2016-11-27 15:37:14 +00:00
|
|
|
|
|
|
|
if (rem <= 0)
|
|
|
|
{
|
|
|
|
ret = -2; /* buffer too small */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = cmgr->uctobc (*p, bcs, rem);
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break; /* illegal character */
|
|
|
|
}
|
|
|
|
if (n > rem)
|
|
|
|
{
|
|
|
|
ret = -2; /* buffer too small */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bcs += n; rem -= n; p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bcslen -= rem;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_bch_t bcsbuf[MOO_BCLEN_MAX];
|
|
|
|
moo_oow_t mlen = 0;
|
2016-11-27 15:37:14 +00:00
|
|
|
|
|
|
|
while (p < end)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t n;
|
2016-11-27 15:37:14 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
n = cmgr->uctobc (*p, bcsbuf, MOO_COUNTOF(bcsbuf));
|
2016-11-27 15:37:14 +00:00
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break; /* illegal character */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it assumes that bcsbuf is large enough to hold a character */
|
2017-01-09 09:54:49 +00:00
|
|
|
/*MOO_ASSERT (moo, n <= MOO_COUNTOF(bcsbuf));*/
|
2016-11-27 15:37:14 +00:00
|
|
|
|
|
|
|
p++; mlen += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this length excludes the terminating null character.
|
|
|
|
* this function doesn't even null-terminate the result. */
|
|
|
|
*bcslen = mlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ucslen = p - ucs;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ucs_to_bcs_with_cmgr (
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* ucs, moo_oow_t* ucslen,
|
|
|
|
moo_bch_t* bcs, moo_oow_t* bcslen, moo_cmgr_t* cmgr)
|
2016-11-27 15:37:14 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
const moo_uch_t* p = ucs;
|
2016-11-27 15:37:14 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (bcs)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t rem = *bcslen;
|
2016-11-27 15:37:14 +00:00
|
|
|
|
|
|
|
while (*p != '\0')
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t n;
|
2016-11-27 15:37:14 +00:00
|
|
|
|
|
|
|
if (rem <= 0)
|
|
|
|
{
|
|
|
|
ret = -2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = cmgr->uctobc (*p, bcs, rem);
|
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break; /* illegal character */
|
|
|
|
}
|
|
|
|
if (n > rem)
|
|
|
|
{
|
|
|
|
ret = -2;
|
|
|
|
break; /* buffer too small */
|
|
|
|
}
|
|
|
|
|
|
|
|
bcs += n; rem -= n; p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update bcslen to the length of the bcs string converted excluding
|
|
|
|
* terminating null */
|
|
|
|
*bcslen -= rem;
|
|
|
|
|
|
|
|
/* null-terminate the multibyte sequence if it has sufficient space */
|
|
|
|
if (rem > 0) *bcs = '\0';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* if ret is -2 and cs[cslen] == '\0',
|
|
|
|
* this means that the bcs buffer was lacking one
|
|
|
|
* slot for the terminating null */
|
|
|
|
ret = -2; /* buffer too small */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_bch_t bcsbuf[MOO_BCLEN_MAX];
|
|
|
|
moo_oow_t mlen = 0;
|
2016-11-27 15:37:14 +00:00
|
|
|
|
|
|
|
while (*p != '\0')
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t n;
|
2016-11-27 15:37:14 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
n = cmgr->uctobc (*p, bcsbuf, MOO_COUNTOF(bcsbuf));
|
2016-11-27 15:37:14 +00:00
|
|
|
if (n == 0)
|
|
|
|
{
|
|
|
|
ret = -1;
|
|
|
|
break; /* illegal character */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it assumes that bcs is large enough to hold a character */
|
2017-01-09 09:54:49 +00:00
|
|
|
/*MOO_ASSERT (moo, n <= MOO_COUNTOF(bcs));*/
|
2016-11-27 15:37:14 +00:00
|
|
|
|
|
|
|
p++; mlen += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this length holds the number of resulting multi-byte characters
|
|
|
|
* excluding the terminating null character */
|
|
|
|
*bcslen = mlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ucslen = p - ucs; /* the number of wide characters handled. */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-12-27 18:15:35 +00:00
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static moo_cmgr_t utf8_cmgr =
|
2016-11-27 15:37:14 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_utf8touc,
|
|
|
|
moo_uctoutf8
|
2016-11-27 15:37:14 +00:00
|
|
|
};
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_cmgr_t* moo_getutf8cmgr (void)
|
2016-11-27 15:37:14 +00:00
|
|
|
{
|
|
|
|
return &utf8_cmgr;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_convutf8touchars (const moo_bch_t* bcs, moo_oow_t* bcslen, moo_uch_t* ucs, moo_oow_t* ucslen)
|
2016-11-27 15:37:14 +00:00
|
|
|
{
|
2016-12-27 18:15:35 +00:00
|
|
|
/* the source is length bound */
|
|
|
|
return bcsn_to_ucsn_with_cmgr (bcs, bcslen, ucs, ucslen, &utf8_cmgr, 0);
|
2016-11-27 15:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_convutoutf8chars (const moo_uch_t* ucs, moo_oow_t* ucslen, moo_bch_t* bcs, moo_oow_t* bcslen)
|
2016-11-27 15:37:14 +00:00
|
|
|
{
|
2016-12-27 18:15:35 +00:00
|
|
|
/* length bound */
|
|
|
|
return ucsn_to_bcsn_with_cmgr (ucs, ucslen, bcs, bcslen, &utf8_cmgr);
|
2016-11-27 15:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_convutf8toucstr (const moo_bch_t* bcs, moo_oow_t* bcslen, moo_uch_t* ucs, moo_oow_t* ucslen)
|
2016-11-27 15:37:14 +00:00
|
|
|
{
|
2016-12-27 18:15:35 +00:00
|
|
|
/* null-terminated. */
|
|
|
|
return bcs_to_ucs_with_cmgr (bcs, bcslen, ucs, ucslen, &utf8_cmgr, 0);
|
2016-11-27 15:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_convutoutf8cstr (const moo_uch_t* ucs, moo_oow_t* ucslen, moo_bch_t* bcs, moo_oow_t* bcslen)
|
2016-11-27 15:37:14 +00:00
|
|
|
{
|
2016-12-27 18:15:35 +00:00
|
|
|
/* null-terminated */
|
|
|
|
return ucs_to_bcs_with_cmgr (ucs, ucslen, bcs, bcslen, &utf8_cmgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_convbtouchars (moo_t* moo, const moo_bch_t* bcs, moo_oow_t* bcslen, moo_uch_t* ucs, moo_oow_t* ucslen)
|
2016-12-27 18:15:35 +00:00
|
|
|
{
|
|
|
|
/* length bound */
|
2017-01-09 09:54:49 +00:00
|
|
|
return bcsn_to_ucsn_with_cmgr (bcs, bcslen, ucs, ucslen, moo->cmgr, 0);
|
2016-12-27 18:15:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_convutobchars (moo_t* moo, const moo_uch_t* ucs, moo_oow_t* ucslen, moo_bch_t* bcs, moo_oow_t* bcslen)
|
2016-12-27 18:15:35 +00:00
|
|
|
{
|
|
|
|
/* length bound */
|
2017-01-09 09:54:49 +00:00
|
|
|
return ucsn_to_bcsn_with_cmgr (ucs, ucslen, bcs, bcslen, moo->cmgr);
|
2016-12-27 18:15:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_convbtoucstr (moo_t* moo, const moo_bch_t* bcs, moo_oow_t* bcslen, moo_uch_t* ucs, moo_oow_t* ucslen)
|
2016-12-27 18:15:35 +00:00
|
|
|
{
|
|
|
|
/* null-terminated. */
|
2017-01-09 09:54:49 +00:00
|
|
|
return bcs_to_ucs_with_cmgr (bcs, bcslen, ucs, ucslen, moo->cmgr, 0);
|
2016-12-27 18:15:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_convutobcstr (moo_t* moo, const moo_uch_t* ucs, moo_oow_t* ucslen, moo_bch_t* bcs, moo_oow_t* bcslen)
|
2016-12-27 18:15:35 +00:00
|
|
|
{
|
|
|
|
/* null-terminated */
|
2017-01-09 09:54:49 +00:00
|
|
|
return ucs_to_bcs_with_cmgr (ucs, ucslen, bcs, bcslen, moo->cmgr);
|
2016-11-27 15:37:14 +00:00
|
|
|
}
|