+ static BIGNUM *sval;
+ static int ex = 1;
+ BIGNUM *pval;
+
+ if (hflag) {
+ if (sval == NULL) {
+ sval = BN_dup(val);
+ return;
+ }
+
+ if (val != NULL && BN_cmp(val, sval) == 0) {
+ ex++;
+ return;
+ }
+ pval = sval;
+ } else if (val == NULL) {
+ return;
+ } else {
+ pval = val;
+ }
+
+ if (xflag) {
+ fputs(" 0x", stdout);
+ BN_print_fp(stdout, pval);
+ } else {
+ putchar(' ');
+ BN_print_dec_fp(stdout, pval);
+ }
+
+ if (hflag) {
+ if (ex > 1)
+ pr_exp(ex, xflag);
+
+ if (val != NULL) {
+ BN_copy(sval, val);
+ } else {
+ BN_free(sval);
+ sval = NULL;
+ }
+ ex = 1;
+ }
+}
+
+static void
+usage(void)
+{
+ fprintf(stderr, "Usage: %s [-hx] [value ...]\n", getprogname());
+ exit(1);
+}
+
+#ifdef HAVE_OPENSSL
+
+/* pollard p-1, algorithm from Jim Gillogly, May 2000 */
+static void
+pollard_pminus1(BIGNUM *val, int hflag, int xflag)
+{
+ BIGNUM *base, *rbase, *num, *i, *x;
+
+ base = BN_new();
+ rbase = BN_new();
+ num = BN_new();
+ i = BN_new();
+ x = BN_new();
+
+ BN_set_word(rbase, 1);
+newbase:
+ if (!BN_add_word(rbase, 1))
+ errx(1, "error in BN_add_word()");
+ BN_set_word(i, 2);
+ BN_copy(base, rbase);
+
+ for (;;) {
+ BN_mod_exp(base, base, i, val, ctx);
+ if (BN_is_one(base))
+ goto newbase;
+
+ BN_copy(x, base);
+ BN_sub_word(x, 1);
+ if (!BN_gcd(x, x, val, ctx))
+ errx(1, "error in BN_gcd()");
+
+ if (!BN_is_one(x)) {
+ if (BN_is_prime_ex(x, PRIME_CHECKS, NULL, NULL) == 1)
+ pr_print(x, hflag, xflag);
+ else
+ pollard_pminus1(x, hflag, xflag);
+ fflush(stdout);
+
+ BN_div(num, NULL, val, x, ctx);
+ if (BN_is_one(num))
+ return;
+ if (BN_is_prime_ex(num, PRIME_CHECKS, NULL,
+ NULL) == 1) {
+ pr_print(num, hflag, xflag);
+ fflush(stdout);
+ return;
+ }
+ BN_copy(val, num);
+ }
+ if (!BN_add_word(i, 1))
+ errx(1, "error in BN_add_word()");
+ }
+}
+
+/*
+ * Sigh.. No _decimal_ output to file functions in BN.
+ */
+static void
+BN_print_dec_fp(FILE *fp, const BIGNUM *num)
+{
+ char *buf;
+
+ buf = BN_bn2dec(num);
+ if (buf == NULL)
+ return; /* XXX do anything here? */
+ fprintf(fp, "%s", buf);
+ free(buf);
+}
+
+#else
+
+static void
+BN_print_fp(FILE *fp, const BIGNUM *num)
+{
+ fprintf(fp, "%lx", (unsigned long)*num);
+}
+
+static void
+BN_print_dec_fp(FILE *fp, const BIGNUM *num)
+{
+ fprintf(fp, "%lu", (unsigned long)*num);
+}
+
+static int
+BN_dec2bn(BIGNUM **a, const char *str)
+{
+ char *p;
+
+ errno = 0;
+ **a = strtoul(str, &p, 10);
+ return (errno == 0 ? 1 : 0); /* OpenSSL returns 0 on error! */
+}
+
+static int
+BN_hex2bn(BIGNUM **a, const char *str)
+{
+ char *p;
+
+ errno = 0;
+ **a = strtoul(str, &p, 16);
+ return (errno == 0 ? 1 : 0); /* OpenSSL returns 0 on error! */
+}
+
+static BN_ULONG
+BN_div_word(BIGNUM *a, BN_ULONG b)
+{
+ BN_ULONG mod;
+
+ mod = *a % b;
+ *a /= b;
+ return mod;
+}
+
+static BIGNUM *
+BN_dup(const BIGNUM *a)
+{
+ BIGNUM *b = BN_new();
+ BN_copy(b, a);
+ return b;
+}
+
+#endif
+
+/* Convert string pointed to by *str to a bignum. */
+static void
+convert_str2bn(BIGNUM **val, char *p)
+{
+ int n = 0;
+
+ if (*p == '+') p++;
+ if (*p == '-')
+ errx(1, "negative numbers aren't permitted.");
+ if (*p == '0' && (p[1] == 'x' || p[1] == 'X')) {
+ n = BN_hex2bn(val, p + 2);
+ } else {
+ n = BN_dec2bn(val, p);
+ }
+ if (n == 0)
+ errx(1, "%s: illegal numeric format.", p);