From 007b0f18d6f9f1b069b86a29cdfb69f1493302ef Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 18 Jan 2019 10:43:02 -0800 Subject: mpi: use wchar_t string for text-to-bignum. * mpi/mpi.c (mp_read_radix): Take const wchar_t * string rather than unsigned char *. (s_mp_tovalue): Take character argument as wchar_t rather than int. * mpi/mpi.h (mp_read_radix): Declaration updated. * lib.c (int_str): Avoid a malloc/free and UTF-8 conversion by passing the original wide string to mp_read_radix. This removes a TODO dating back to December 2011. --- lib.c | 5 +---- mpi/mpi.c | 6 +++--- mpi/mpi.h | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/lib.c b/lib.c index a1a203b7..925bf638 100644 --- a/lib.c +++ b/lib.c @@ -4600,11 +4600,8 @@ val int_str(val str, val base) if ((value == LONG_MAX || value == LONG_MIN) && errno == ERANGE) { val bignum = make_bignum(); - size_t size; - unsigned char *ucs = utf8_dup_to_buf(wcs, &size, 1); - mp_err err = mp_read_radix(mp(bignum), ucs, b); + mp_err err = mp_read_radix(mp(bignum), wcs, b); - free(ucs); /* TODO: make wchar_t version of mp_read_radix. */ gc_hint(str); if (err != MP_OKAY) diff --git a/mpi/mpi.c b/mpi/mpi.c index 198a5a86..d3e4cb05 100644 --- a/mpi/mpi.c +++ b/mpi/mpi.c @@ -156,7 +156,7 @@ int s_mp_cmp_d(mp_int *a, mp_digit d); /* magnitude digit compare */ mp_size s_mp_ispow2(mp_int *v); /* is v a power of 2? */ int s_mp_ispow2d(mp_digit d); /* is d a power of 2? */ -int s_mp_tovalue(int ch, int r); /* convert ch to value */ +int s_mp_tovalue(wchar_t ch, int r); /* convert ch to value */ char s_mp_todigit(int val, int r, int low); /* convert val to digit */ size_t s_mp_outlen(mp_size bits, int r); /* output length in bytes */ @@ -2568,7 +2568,7 @@ mp_size mp_is_pow_two(mp_int *mp) * characters are ignored, and the function reads until a non-digit * character or the end of the string. */ -mp_err mp_read_radix(mp_int *mp, unsigned char *str, int radix) +mp_err mp_read_radix(mp_int *mp, const wchar_t *str, int radix) { size_t ix = 0; int val = 0; @@ -3959,7 +3959,7 @@ int s_mp_ispow2d(mp_digit d) * The results will be odd if you use a radix < 2 or > 62, you are * expected to know what you're up to. */ -int s_mp_tovalue(int ch, int r) +int s_mp_tovalue(wchar_t ch, int r) { int val, xch; diff --git a/mpi/mpi.h b/mpi/mpi.h index 8bd469f7..1106abde 100644 --- a/mpi/mpi.h +++ b/mpi/mpi.h @@ -189,7 +189,7 @@ mp_size mp_is_pow_two(mp_int *mp); #define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str)) #endif -mp_nign mp_err mp_read_radix(mp_int *mp, unsigned char *str, int radix); +mp_nign mp_err mp_read_radix(mp_int *mp, const wchar_t *str, int radix); mp_size mp_radix_size(mp_int *mp, int radix); mp_size mp_value_radix_size(mp_size num, mp_size qty, int radix); mp_err mp_toradix(mp_int *mp, unsigned char *str, int radix); -- cgit v1.2.3