View | Details | Raw Unified | Return to bug 243136
Collapse All | Expand All

(-)usr.bin/factor/factor.6 (-3 / +14 lines)
Lines 36-42 Link Here
36
.\"
36
.\"
37
.\"   chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
37
.\"   chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
38
.\"
38
.\"
39
.Dd October 10, 2002
39
.Dd December 29, 2019
40
.Dt FACTOR 6
40
.Dt FACTOR 6
41
.Os
41
.Os
42
.Sh NAME
42
.Sh NAME
Lines 67-77 Link Here
67
.Nm
67
.Nm
68
is invoked with no arguments,
68
is invoked with no arguments,
69
.Nm
69
.Nm
70
reads numbers, one per line, from standard input, until end of file or error.
70
reads numbers, one per line, from standard input until end of file or 0
71
is entered or an error occurs.
71
Leading white-space and empty lines are ignored.
72
Leading white-space and empty lines are ignored.
73
.Pp
72
Numbers may be preceded by a single
74
Numbers may be preceded by a single
73
.Ql + .
75
.Ql + .
76
Numbers can be either decimal or hexadecimal strings where the longest
77
leading substring is used.
74
Numbers are terminated by a non-digit character (such as a newline).
78
Numbers are terminated by a non-digit character (such as a newline).
79
If the string contains only decimal digits, it is treated as a
80
decimal representation for a number.
81
A hexadecimal string can contain an optional
82
.Em 0x
83
or
84
.Em 0X
85
prefix.
75
After a number is read, it is factored.
86
After a number is read, it is factored.
76
.Pp
87
.Pp
77
The
88
The
Lines 89-95 Link Here
89
value must not be greater than the maximum.
100
value must not be greater than the maximum.
90
The default and maximum value of
101
The default and maximum value of
91
.Ar stop
102
.Ar stop
92
is 3825123056546413050.
103
is 18446744073709551615.
93
.Pp
104
.Pp
94
When the
105
When the
95
.Nm primes
106
.Nm primes
(-)usr.bin/factor/factor.c (-16 / +53 lines)
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
}
(-)usr.bin/primes/primes.c (-1 / +1 lines)
Lines 55-61 Link Here
55
 *	primes [-h] [start [stop]]
55
 *	primes [-h] [start [stop]]
56
 *
56
 *
57
 *	Print primes >= start and < stop.  If stop is omitted,
57
 *	Print primes >= start and < stop.  If stop is omitted,
58
 *	the value 4294967295 (2^32-1) is assumed.  If start is
58
 *	the value 18446744073709551615 (2^64-1) is assumed.  If start is
59
 *	omitted, start is read from standard input.
59
 *	omitted, start is read from standard input.
60
 *
60
 *
61
 * validation check: there are 664579 primes between 0 and 10^7
61
 * validation check: there are 664579 primes between 0 and 10^7

Return to bug 243136