Replaced "fgets" with a "get_token" function in demo/mtest_opponent.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
diff --git a/demo/mtest_opponent.c b/demo/mtest_opponent.c
index edbecc0..7cdad2c 100644
--- a/demo/mtest_opponent.c
+++ b/demo/mtest_opponent.c
@@ -11,10 +11,49 @@ static void draw(const mp_int *a)
ndraw(a, "");
}
-#define FGETS(str, size, stream) \
+/*
+ Get tokens. It is just a very(!) simple fgets(3) that does not keep line endings.
+
+ Implementation follows along "man 3 fgets", some of which is quoted.
+ */
+static char *s_mp_get_token(char *s, int size, FILE *stream)
+{
+ char *s_bar = s;
+ int c;
+ bool eol_hit = false;
+
+ /* "fgets [...] reads in at most one less than size characters from stream" */
+ while (--size) {
+ /* "Reading stops after an EOF or a newline." We stop only for EOF here */
+ if ((c = fgetc(stream)) == EOF) {
+ /* "Returns [...] NULL on error or when end of file occurs while no characters have been read" */
+ if ((s_bar == s) || (ferror(stream) != 0)) {
+ return NULL;
+ }
+ break;
+ }
+ /* Ignore line-breaks but keep reading to get them out of the stream-buffer */
+ if ((c == '\n') || (c == '\r')) {
+ eol_hit = true;
+ continue;
+ }
+ /* Stop reading after linebreak */
+ if (eol_hit) {
+ /* We already read the character after the linebreak, put it back */
+ ungetc(c, stream);
+ break;
+ }
+ *s_bar++ = c;
+ }
+ /* "A terminating null byte ('\0') is stored after the last character in the buffer" */
+ *s_bar = '\0';
+ return s;
+}
+
+#define GET_TOKEN(str, size, stream) \
{ \
- char *ret = fgets(str, size, stream); \
- if (!ret) { fprintf(stderr, "\n%d: fgets failed\n", __LINE__); goto LBL_ERR; } \
+ char *ret = s_mp_get_token((str), (size), (stream)); \
+ if (!ret) { fprintf(stderr, "\n%d: s_mp_get_token failed\n", __LINE__); goto LBL_ERR; } \
}
static int mtest_opponent(void)
@@ -76,17 +115,16 @@ static int mtest_opponent(void)
printf("%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu ",
add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n,
expt_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n);
- FGETS(cmd, 4095, stdin);
- cmd[strlen(cmd) - 1u] = '\0';
+ GET_TOKEN(cmd, 4095, stdin);
printf("%-6s ]\r", cmd);
fflush(stdout);
if (strcmp(cmd, "mul2d") == 0) {
++mul2d_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
sscanf(buf, "%u", &rr);
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_mul_2d(&a, (int)rr, &a));
@@ -99,11 +137,11 @@ static int mtest_opponent(void)
}
} else if (strcmp(cmd, "div2d") == 0) {
++div2d_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
sscanf(buf, "%u", &rr);
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_div_2d(&a, (int)rr, &a, &e));
@@ -119,11 +157,11 @@ static int mtest_opponent(void)
}
} else if (strcmp(cmd, "add") == 0) {
++add_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_add(&d, &b, &d));
@@ -162,11 +200,11 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "sub") == 0) {
++sub_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_sub(&d, &b, &d));
@@ -180,11 +218,11 @@ static int mtest_opponent(void)
}
} else if (strcmp(cmd, "mul") == 0) {
++mul_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_mul(&d, &b, &d));
@@ -198,13 +236,13 @@ static int mtest_opponent(void)
}
} else if (strcmp(cmd, "div") == 0) {
++div_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&c, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&d, buf, 64));
DO(mp_div(&a, &b, &e, &f));
@@ -222,9 +260,9 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "sqr") == 0) {
++sqr_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_copy(&a, &c));
DO(mp_sqr(&c, &c));
@@ -237,11 +275,11 @@ static int mtest_opponent(void)
}
} else if (strcmp(cmd, "gcd") == 0) {
++gcd_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_gcd(&d, &b, &d));
@@ -256,11 +294,11 @@ static int mtest_opponent(void)
}
} else if (strcmp(cmd, "lcm") == 0) {
++lcm_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_lcm(&d, &b, &d));
@@ -275,13 +313,13 @@ static int mtest_opponent(void)
}
} else if (strcmp(cmd, "expt") == 0) {
++expt_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&c, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&d, buf, 64));
DO(mp_copy(&a, &e));
DO(mp_exptmod(&e, &b, &c, &e));
@@ -296,11 +334,11 @@ static int mtest_opponent(void)
}
} else if (strcmp(cmd, "invmod") == 0) {
++inv_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&c, buf, 64));
DO(mp_invmod(&a, &b, &d));
DO(mp_mulmod(&d, &a, &b, &e));
@@ -318,9 +356,9 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "div2") == 0) {
++div2_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_div_2(&a, &c));
if (mp_cmp(&c, &b) != MP_EQ) {
@@ -332,9 +370,9 @@ static int mtest_opponent(void)
}
} else if (strcmp(cmd, "mul2") == 0) {
++mul2_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_mul_2(&a, &c));
if (mp_cmp(&c, &b) != MP_EQ) {
@@ -346,11 +384,11 @@ static int mtest_opponent(void)
}
} else if (strcmp(cmd, "add_d") == 0) {
++add_d_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
sscanf(buf, "%d", &ix);
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_add_d(&a, (mp_digit)ix, &c));
if (mp_cmp(&b, &c) != MP_EQ) {
@@ -363,11 +401,11 @@ static int mtest_opponent(void)
}
} else if (strcmp(cmd, "sub_d") == 0) {
++sub_d_n;
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&a, buf, 64));
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
sscanf(buf, "%d", &ix);
- FGETS(buf, 4095, stdin);
+ GET_TOKEN(buf, 4095, stdin);
DO(mp_read_radix(&b, buf, 64));
DO(mp_sub_d(&a, (mp_digit)ix, &c));
if (mp_cmp(&b, &c) != MP_EQ) {
diff --git a/doc/bn.tex b/doc/bn.tex
index 6f1cb8c..1dfb34d 100644
--- a/doc/bn.tex
+++ b/doc/bn.tex
@@ -2409,9 +2409,25 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream);
mp_err mp_read_radix (mp_int *a, const char *str, int radix);
\end{alltt}
This will read a \texttt{NUL} terminated string in base \texttt{radix} from \texttt{str} into $a$.
-It will stop reading when it reads a character it does not recognize (which happens to include the
-\texttt{NUL} char\dots imagine that\dots). A single leading $-$ (ASCII \texttt{0x20}) sign can be
-used to denote a negative number. The input encoding is currently restricted to ASCII only.
+Valid values of \texttt{radix} are in the range $[2, 64]$.
+%It will stop reading when it reads a character it does not recognize (which happens to include the
+%\texttt{NUL} char\dots imagine that\dots).
+
+It returns \texttt{MP\_VAL} for any character {\em not} in the range allowed for the given base.
+The list of characters is the same as for base-64 and also in the same order.
+\begin{alltt}
+0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/
+\end{alltt}
+
+A single leading $-$ (ASCII \texttt{0x20}) sign can be used to denote a negative number. The
+plus sign $+$ (ASCII \texttt{0x2b}) is already in use (bases 63 and 64) and cannot be used
+to denote positivity, no matter how good the mood of the number is.
+
+For all bases smaller than 37 the list is case-insensitive, e.g.:~the two hexadecimal numbers
+$123abc_{16} = 1194684_{10}$ and $123ABC_{16} = 1194684_{10}$ are equivalent but the two base
+64 numbers $123abc_{64} = 1108232550_{10}$ and $123ABC_{64} = 1108124364_{10}$ are not.
+
+The input encoding is currently restricted to ASCII only.
If \texttt{MP\_NO\_FILE} is not defined a function to read from a file is also available.
\index{mp\_fread}