Both mp_write and mp_read are broken. The former would write a digit
`0' for a zero input, and attempt to prefix its output with a `-' sign
on negative input, both of which are impossible to decode unambiguously.
The latter would skip leading whitespace characters, which makes
encodings beginning with certain bytes decode incorrectly.
Include tests for these cases, and fix the bugs.
/* --- Read an initial character --- */
ch = ops->get(p);
- while (isspace(ch))
- ch = ops->get(p);
+ if (radix >= 0) {
+ while (isspace(ch))
+ ch = ops->get(p);
+ }
/* --- Handle an initial sign --- */
int rc;
if (MP_EQ(m, MP_ZERO))
- return (ops->put("0", 1, p));
+ return (ops->put(radix > 0 ? "0" : "\0", 1, p));
/* --- Set various things up --- */
/* --- If the number is negative, sort that out --- */
if (MP_NEGP(m)) {
+ assert(radix > 0);
if (ops->put("-", 1, p))
return (EOF);
m->f &= ~MP_NEG;
mptext-bin-in {
-10 010203040506070809 10 123456789 "";
-100 01172d4359 10 123456789 "";
+ -90 09124709 10 6713199 "";
}
mptext-bin-out {
10 123456789 -10 010203040506070809 "";
10 123456789 -100 01172d4359 "";
+ 10 6713199 -90 09124709 "";
+ 10 0 -10 00 "";
}