]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/factor/factor.c
Merge llvm-project release/16.x llvmorg-16.0.4-0-gae42196bc493
[FreeBSD/FreeBSD.git] / usr.bin / factor / factor.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Landon Curt Noll.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #ifndef lint
34 #include <sys/cdefs.h>
35 #ifdef __COPYRIGHT
36 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
37         The Regents of the University of California.  All rights reserved.");
38 #endif
39 #ifdef __SCCSID
40 __SCCSID("@(#)factor.c  8.4 (Berkeley) 5/4/95");
41 #endif
42 #ifdef __RCSID
43 __RCSID("$NetBSD: factor.c,v 1.19 2009/08/12 05:54:31 dholland Exp $");
44 #endif
45 #ifdef __FBSDID
46 __FBSDID("$FreeBSD$");
47 #endif
48 #endif /* not lint */
49
50 /*
51  * factor - factor a number into primes
52  *
53  * By: Landon Curt Noll   chongo@toad.com,   ...!{sun,tolsoft}!hoptoad!chongo
54  *
55  *   chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
56  *
57  * usage:
58  *      factor [-h] [number] ...
59  *
60  * The form of the output is:
61  *
62  *      number: factor1 factor1 factor2 factor3 factor3 factor3 ...
63  *
64  * where factor1 <= factor2 <= factor3 <= ...
65  *
66  * If no args are given, the list of numbers are read from stdin.
67  */
68
69 #include <ctype.h>
70 #include <err.h>
71 #include <errno.h>
72 #include <inttypes.h>
73 #include <limits.h>
74 #include <stdbool.h>
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <unistd.h>
78
79 #include "primes.h"
80
81 #ifdef HAVE_OPENSSL
82
83 #include <openssl/bn.h>
84
85 #if OPENSSL_VERSION_NUMBER < 0x30000000L
86 static inline int
87 BN_check_prime(BIGNUM *p, BN_CTX *ctx, BN_GENCB *cb)
88 {
89         const int nchecks = 5;
90
91         return BN_is_prime_ex(p, nchecks, ctx, cb);
92 }
93 #endif
94
95 static void     pollard_pminus1(BIGNUM *); /* print factors for big numbers */
96
97 #else
98
99 typedef ubig    BIGNUM;
100 typedef u_long  BN_ULONG;
101
102 #define BN_CTX                  int
103 #define BN_CTX_new()            NULL
104 #define BN_new()                ((BIGNUM *)calloc(sizeof(BIGNUM), 1))
105 #define BN_is_zero(v)           (*(v) == 0)
106 #define BN_is_one(v)            (*(v) == 1)
107 #define BN_mod_word(a, b)       (*(a) % (b))
108
109 static int      BN_dec2bn(BIGNUM **, const char *);
110 static int      BN_hex2bn(BIGNUM **, const char *);
111 static BN_ULONG BN_div_word(BIGNUM *, BN_ULONG);
112 static void     BN_print_fp(FILE *, const BIGNUM *);
113
114 #endif
115
116 static void     BN_print_dec_fp(FILE *, const BIGNUM *);
117 static void     convert_str2bn(BIGNUM **, char *);
118 static bool     is_hex_str(char *);
119 static void     pr_fact(BIGNUM *);      /* print factors of a value */
120 static void     pr_print(BIGNUM *);     /* print a prime */
121 static void     usage(void);
122
123 static BN_CTX   *ctx;                   /* just use a global context */
124 static int      hflag;
125
126 int
127 main(int argc, char *argv[])
128 {
129         BIGNUM *val;
130         int ch;
131         char *p, buf[LINE_MAX];         /* > max number of digits. */
132
133         ctx = BN_CTX_new();
134         val = BN_new();
135         if (val == NULL)
136                 errx(1, "can't initialise bignum");
137
138         while ((ch = getopt(argc, argv, "h")) != -1)
139                 switch (ch) {
140                 case 'h':
141                         hflag++;
142                         break;
143                 case '?':
144                 default:
145                         usage();
146                 }
147         argc -= optind;
148         argv += optind;
149
150         /* No args supplied, read numbers from stdin. */
151         if (argc == 0)
152                 for (;;) {
153                         if (fgets(buf, sizeof(buf), stdin) == NULL) {
154                                 if (ferror(stdin))
155                                         err(1, "stdin");
156                                 exit (0);
157                         }
158                         for (p = buf; isblank(*p); ++p);
159                         if (*p == '\n' || *p == '\0')
160                                 continue;
161                         convert_str2bn(&val, p);
162                         pr_fact(val);
163                 }
164         /* Factor the arguments. */
165         else
166                 for (p = *argv; p != NULL; p = *++argv) {
167                         convert_str2bn(&val, p);
168                         pr_fact(val);
169                 }
170         exit(0);
171 }
172
173 /*
174  * pr_fact - print the factors of a number
175  *
176  * Print the factors of the number, from the lowest to the highest.
177  * A factor will be printed multiple times if it divides the value
178  * multiple times.
179  *
180  * Factors are printed with leading tabs.
181  */
182 static void
183 pr_fact(BIGNUM *val)
184 {
185         const ubig *fact;       /* The factor found. */
186
187         /* Firewall - catch 0 and 1. */
188         if (BN_is_zero(val))    /* Historical practice; 0 just exits. */
189                 exit(0);
190         if (BN_is_one(val)) {
191                 printf("1: 1\n");
192                 return;
193         }
194
195         /* Factor value. */
196
197         if (hflag) {
198                 fputs("0x", stdout);
199                 BN_print_fp(stdout, val);
200         } else
201                 BN_print_dec_fp(stdout, val);
202         putchar(':');
203         for (fact = &prime[0]; !BN_is_one(val); ++fact) {
204                 /* Look for the smallest factor. */
205                 do {
206                         if (BN_mod_word(val, (BN_ULONG)*fact) == 0)
207                                 break;
208                 } while (++fact <= pr_limit);
209
210                 /* Watch for primes larger than the table. */
211                 if (fact > pr_limit) {
212 #ifdef HAVE_OPENSSL
213                         BIGNUM *bnfact;
214
215                         bnfact = BN_new();
216                         BN_set_word(bnfact, *(fact - 1));
217                         if (!BN_sqr(bnfact, bnfact, ctx))
218                                 errx(1, "error in BN_sqr()");
219                         if (BN_cmp(bnfact, val) > 0 ||
220                             BN_check_prime(val, NULL, NULL) == 1)
221                                 pr_print(val);
222                         else
223                                 pollard_pminus1(val);
224 #else
225                         pr_print(val);
226 #endif
227                         break;
228                 }
229
230                 /* Divide factor out until none are left. */
231                 do {
232                         printf(hflag ? " 0x%" PRIx64 "" : " %" PRIu64 "", *fact);
233                         BN_div_word(val, (BN_ULONG)*fact);
234                 } while (BN_mod_word(val, (BN_ULONG)*fact) == 0);
235
236                 /* Let the user know we're doing something. */
237                 fflush(stdout);
238         }
239         putchar('\n');
240 }
241
242 static void
243 pr_print(BIGNUM *val)
244 {
245         if (hflag) {
246                 fputs(" 0x", stdout);
247                 BN_print_fp(stdout, val);
248         } else {
249                 putchar(' ');
250                 BN_print_dec_fp(stdout, val);
251         }
252 }
253
254 static void
255 usage(void)
256 {
257         fprintf(stderr, "usage: factor [-h] [value ...]\n");
258         exit(1);
259 }
260
261 #ifdef HAVE_OPENSSL
262
263 /* pollard p-1, algorithm from Jim Gillogly, May 2000 */
264 static void
265 pollard_pminus1(BIGNUM *val)
266 {
267         BIGNUM *base, *rbase, *num, *i, *x;
268
269         base = BN_new();
270         rbase = BN_new();
271         num = BN_new();
272         i = BN_new();
273         x = BN_new();
274
275         BN_set_word(rbase, 1);
276 newbase:
277         if (!BN_add_word(rbase, 1))
278                 errx(1, "error in BN_add_word()");
279         BN_set_word(i, 2);
280         BN_copy(base, rbase);
281
282         for (;;) {
283                 BN_mod_exp(base, base, i, val, ctx);
284                 if (BN_is_one(base))
285                         goto newbase;
286
287                 BN_copy(x, base);
288                 BN_sub_word(x, 1);
289                 if (!BN_gcd(x, x, val, ctx))
290                         errx(1, "error in BN_gcd()");
291
292                 if (!BN_is_one(x)) {
293                         if (BN_check_prime(x, NULL, NULL) == 1)
294                                 pr_print(x);
295                         else
296                                 pollard_pminus1(x);
297                         fflush(stdout);
298
299                         BN_div(num, NULL, val, x, ctx);
300                         if (BN_is_one(num))
301                                 return;
302                         if (BN_check_prime(num, NULL, NULL) == 1) {
303                                 pr_print(num);
304                                 fflush(stdout);
305                                 return;
306                         }
307                         BN_copy(val, num);
308                 }
309                 if (!BN_add_word(i, 1))
310                         errx(1, "error in BN_add_word()");
311         }
312 }
313
314 /*
315  * Sigh..  No _decimal_ output to file functions in BN.
316  */
317 static void
318 BN_print_dec_fp(FILE *fp, const BIGNUM *num)
319 {
320         char *buf;
321
322         buf = BN_bn2dec(num);
323         if (buf == NULL)
324                 return; /* XXX do anything here? */
325         fprintf(fp, "%s", buf);
326         free(buf);
327 }
328
329 #else
330
331 static void
332 BN_print_fp(FILE *fp, const BIGNUM *num)
333 {
334         fprintf(fp, "%lx", (unsigned long)*num);
335 }
336
337 static void
338 BN_print_dec_fp(FILE *fp, const BIGNUM *num)
339 {
340         fprintf(fp, "%lu", (unsigned long)*num);
341 }
342
343 static int
344 BN_dec2bn(BIGNUM **a, const char *str)
345 {
346         char *p;
347
348         errno = 0;
349         **a = strtoul(str, &p, 10);
350         return (errno == 0 ? 1 : 0);    /* OpenSSL returns 0 on error! */
351 }
352
353 static int
354 BN_hex2bn(BIGNUM **a, const char *str)
355 {
356         char *p;
357
358         errno = 0;
359         **a = strtoul(str, &p, 16);
360         return (errno == 0 ? 1 : 0);    /* OpenSSL returns 0 on error! */
361 }
362
363 static BN_ULONG
364 BN_div_word(BIGNUM *a, BN_ULONG b)
365 {
366         BN_ULONG mod;
367
368         mod = *a % b;
369         *a /= b;
370         return mod;
371 }
372
373 #endif
374
375 /*
376  * Scan the string from left-to-right to see if the longest substring
377  * is a valid hexadecimal number.
378  */
379 static bool
380 is_hex_str(char *str)
381 {
382         char c, *p;
383         bool saw_hex = false;
384
385         for (p = str; *p; p++) {
386                 if (isdigit(*p))
387                         continue;
388                 c = tolower(*p);
389                 if (c >= 'a' && c <= 'f') {
390                         saw_hex = true;
391                         continue;
392                 }
393                 break;  /* Not a hexadecimal digit. */
394         }
395         return saw_hex;
396 }
397
398 /* Convert string pointed to by *str to a bignum.  */
399 static void
400 convert_str2bn(BIGNUM **val, char *p)
401 {
402         int n = 0;
403
404         if (*p == '+') p++;
405         if (*p == '-')
406                 errx(1, "negative numbers aren't permitted.");
407         if (*p == '0') {
408                 p++;
409                 if (*p == 'x' || *p == 'X')
410                         n = BN_hex2bn(val, ++p);
411         } else {
412                 n = is_hex_str(p) ? BN_hex2bn(val, p) : BN_dec2bn(val, p);
413         }
414         if (n == 0)
415                 errx(1, "%s: illegal numeric format.", p);
416 }