Lines 71-76
Link Here
|
71 |
#include <errno.h> |
71 |
#include <errno.h> |
72 |
#include <inttypes.h> |
72 |
#include <inttypes.h> |
73 |
#include <limits.h> |
73 |
#include <limits.h> |
|
|
74 |
#include <stdbool.h> |
74 |
#include <stdio.h> |
75 |
#include <stdio.h> |
75 |
#include <stdlib.h> |
76 |
#include <stdlib.h> |
76 |
#include <unistd.h> |
77 |
#include <unistd.h> |
Lines 97-104
Link Here
|
97 |
#define BN_is_one(v) (*(v) == 1) |
98 |
#define BN_is_one(v) (*(v) == 1) |
98 |
#define BN_mod_word(a, b) (*(a) % (b)) |
99 |
#define BN_mod_word(a, b) (*(a) % (b)) |
99 |
|
100 |
|
100 |
static int BN_dec2bn(BIGNUM **a, const char *str); |
101 |
static int BN_dec2bn(BIGNUM **, const char *); |
101 |
static int BN_hex2bn(BIGNUM **a, const char *str); |
102 |
static int BN_hex2bn(BIGNUM **, const char *); |
102 |
static BN_ULONG BN_div_word(BIGNUM *, BN_ULONG); |
103 |
static BN_ULONG BN_div_word(BIGNUM *, BN_ULONG); |
103 |
static void BN_print_fp(FILE *, const BIGNUM *); |
104 |
static void BN_print_fp(FILE *, const BIGNUM *); |
104 |
|
105 |
|
Lines 105-111
Link Here
|
105 |
#endif |
106 |
#endif |
106 |
|
107 |
|
107 |
static void BN_print_dec_fp(FILE *, const BIGNUM *); |
108 |
static void BN_print_dec_fp(FILE *, const BIGNUM *); |
108 |
|
109 |
static void convert_str2bn(BIGNUM **, char *); |
|
|
110 |
static bool is_hex_str(char *); |
109 |
static void pr_fact(BIGNUM *); /* print factors of a value */ |
111 |
static void pr_fact(BIGNUM *); /* print factors of a value */ |
110 |
static void pr_print(BIGNUM *); /* print a prime */ |
112 |
static void pr_print(BIGNUM *); /* print a prime */ |
111 |
static void usage(void); |
113 |
static void usage(void); |
Lines 148-168
Link Here
|
148 |
for (p = buf; isblank(*p); ++p); |
150 |
for (p = buf; isblank(*p); ++p); |
149 |
if (*p == '\n' || *p == '\0') |
151 |
if (*p == '\n' || *p == '\0') |
150 |
continue; |
152 |
continue; |
151 |
if (*p == '-') |
153 |
convert_str2bn(&val, p); |
152 |
errx(1, "negative numbers aren't permitted."); |
|
|
153 |
if (BN_dec2bn(&val, buf) == 0 && |
154 |
BN_hex2bn(&val, buf) == 0) |
155 |
errx(1, "%s: illegal numeric format.", buf); |
156 |
pr_fact(val); |
154 |
pr_fact(val); |
157 |
} |
155 |
} |
158 |
/* Factor the arguments. */ |
156 |
/* Factor the arguments. */ |
159 |
else |
157 |
else |
160 |
for (; *argv != NULL; ++argv) { |
158 |
for (p = *argv; p != NULL; p = *++argv) { |
161 |
if (argv[0][0] == '-') |
159 |
convert_str2bn(&val, p); |
162 |
errx(1, "negative numbers aren't permitted."); |
|
|
163 |
if (BN_dec2bn(&val, argv[0]) == 0 && |
164 |
BN_hex2bn(&val, argv[0]) == 0) |
165 |
errx(1, "%s: illegal numeric format.", argv[0]); |
166 |
pr_fact(val); |
160 |
pr_fact(val); |
167 |
} |
161 |
} |
168 |
exit(0); |
162 |
exit(0); |
Lines 346-352
Link Here
|
346 |
|
340 |
|
347 |
errno = 0; |
341 |
errno = 0; |
348 |
**a = strtoul(str, &p, 10); |
342 |
**a = strtoul(str, &p, 10); |
349 |
return (errno == 0 && (*p == '\n' || *p == '\0')); |
343 |
return (errno == 0 ? 1 : 0); /* OpenSSL returns 0 on error! */ |
350 |
} |
344 |
} |
351 |
|
345 |
|
352 |
static int |
346 |
static int |
Lines 356-362
Link Here
|
356 |
|
350 |
|
357 |
errno = 0; |
351 |
errno = 0; |
358 |
**a = strtoul(str, &p, 16); |
352 |
**a = strtoul(str, &p, 16); |
359 |
return (errno == 0 && (*p == '\n' || *p == '\0')); |
353 |
return (errno == 0 ? 1 : 0); /* OpenSSL returns 0 on error! */ |
360 |
} |
354 |
} |
361 |
|
355 |
|
362 |
static BN_ULONG |
356 |
static BN_ULONG |
Lines 370-372
Link Here
|
370 |
} |
364 |
} |
371 |
|
365 |
|
372 |
#endif |
366 |
#endif |
|
|
367 |
|
368 |
/* |
369 |
* Scan the string from left-to-right to see if the longest substring |
370 |
* is a valid hexadecimal number. |
371 |
*/ |
372 |
static bool |
373 |
is_hex_str(char *str) |
374 |
{ |
375 |
char c, *p; |
376 |
bool saw_hex = false; |
377 |
|
378 |
for (p = str; *p; p++) { |
379 |
if (isdigit(*p)) |
380 |
continue; |
381 |
c = tolower(*p); |
382 |
if (c >= 'a' && c <= 'f') { |
383 |
saw_hex = true; |
384 |
continue; |
385 |
} |
386 |
break; /* Not a hexadecimal digit. */ |
387 |
} |
388 |
return saw_hex; |
389 |
} |
390 |
|
391 |
/* Convert string pointed to by *str to a bignum. */ |
392 |
static void |
393 |
convert_str2bn(BIGNUM **val, char *p) |
394 |
{ |
395 |
int n = 0; |
396 |
|
397 |
if (*p == '+') p++; |
398 |
if (*p == '-') |
399 |
errx(1, "negative numbers aren't permitted."); |
400 |
if (*p == '0') { |
401 |
p++; |
402 |
if (*p == 'x' || *p == 'X') |
403 |
n = BN_hex2bn(val, ++p); |
404 |
} else { |
405 |
n = is_hex_str(p) ? BN_hex2bn(val, p) : BN_dec2bn(val, p); |
406 |
} |
407 |
if (n == 0) |
408 |
errx(1, "%s: illegal numeric format.", p); |
409 |
} |