diff options
author | nelsonb%netscape.com <devnull@localhost> | 2000-12-29 01:01:37 +0000 |
---|---|---|
committer | nelsonb%netscape.com <devnull@localhost> | 2000-12-29 01:01:37 +0000 |
commit | 1b720c2a353372e11c06771451f320ade89b2cb5 (patch) | |
tree | 42b06a42933c8b212342046c91d65fd3978eacd7 /security/nss/lib/freebl | |
parent | 461927dd724fdef4d38b947ca3ae91db50e63e90 (diff) | |
download | nss-hg-1b720c2a353372e11c06771451f320ade89b2cb5.tar.gz |
Implement new mpi function mpi_read_variable_radix(). It's like
mpi_read_radix(), except that if the input stream begins with a leading
zero or "0x", it will interpret the input as octal or hex, respectively.
Diffstat (limited to 'security/nss/lib/freebl')
-rw-r--r-- | security/nss/lib/freebl/mpi/mpi.c | 39 | ||||
-rw-r--r-- | security/nss/lib/freebl/mpi/mpi.h | 1 | ||||
-rw-r--r-- | security/nss/lib/freebl/mpi/tests/mptest-3.c | 4 |
3 files changed, 42 insertions, 2 deletions
diff --git a/security/nss/lib/freebl/mpi/mpi.c b/security/nss/lib/freebl/mpi/mpi.c index 1bb56faa7..dab04b6d2 100644 --- a/security/nss/lib/freebl/mpi/mpi.c +++ b/security/nss/lib/freebl/mpi/mpi.c @@ -2563,6 +2563,45 @@ mp_err mp_read_radix(mp_int *mp, const char *str, int radix) } /* end mp_read_radix() */ +mp_err mp_read_variable_radix(mp_int *a, const char * str, int default_radix) +{ + int radix = default_radix; + int cx; + mp_sign sig = ZPOS; + mp_err res; + + /* Skip leading non-digit characters until a digit or '-' or '+' */ + while ((cx = *str) != 0 && + (s_mp_tovalue(cx, radix) < 0) && + cx != '-' && + cx != '+') { + ++str; + } + + if (cx == '-') { + sig = NEG; + ++str; + } else if (cx == '+') { + sig = ZPOS; /* this is the default anyway... */ + ++str; + } + + if (str[0] == '0') { + if ((str[1] | 0x20) == 'x') { + radix = 16; + str += 2; + } else { + radix = 8; + str++; + } + } + res = mp_read_radix(a, str, radix); + if (res == MP_OKAY) { + MP_SIGN(a) = (s_mp_cmp_d(a, 0) == MP_EQ) ? ZPOS : sig; + } + return res; +} + /* }}} */ /* {{{ mp_radix_size(mp, radix) */ diff --git a/security/nss/lib/freebl/mpi/mpi.h b/security/nss/lib/freebl/mpi/mpi.h index 5eb73bc7a..d292acd94 100644 --- a/security/nss/lib/freebl/mpi/mpi.h +++ b/security/nss/lib/freebl/mpi/mpi.h @@ -274,6 +274,7 @@ mp_err mp_read_raw(mp_int *mp, char *str, int len); int mp_raw_size(mp_int *mp); mp_err mp_toraw(mp_int *mp, char *str); mp_err mp_read_radix(mp_int *mp, const char *str, int radix); +mp_err mp_read_variable_radix(mp_int *a, const char * str, int default_radix); int mp_radix_size(mp_int *mp, int radix); mp_err mp_toradix(mp_int *mp, char *str, int radix); int mp_tovalue(char ch, int r); diff --git a/security/nss/lib/freebl/mpi/tests/mptest-3.c b/security/nss/lib/freebl/mpi/tests/mptest-3.c index 3a7bdc89e..ac2dd2c6c 100644 --- a/security/nss/lib/freebl/mpi/tests/mptest-3.c +++ b/security/nss/lib/freebl/mpi/tests/mptest-3.c @@ -68,8 +68,8 @@ int main(int argc, char *argv[]) mp_init(&a); mp_init(&b); - mp_read_radix(&a, argv[1], 10); - mp_read_radix(&b, argv[2], 10); + mp_read_variable_radix(&a, argv[1], 10); + mp_read_variable_radix(&b, argv[2], 10); printf("a = "); mp_print(&a, stdout); fputc('\n', stdout); printf("b = "); mp_print(&b, stdout); fputc('\n', stdout); |