Example #1
0
/*
 * pr_fact - print the factors of a number
 *
 * Print the factors of the number, from the lowest to the highest.
 * A factor will be printed multiple times if it divides the value
 * multiple times.
 *
 * Factors are printed with leading tabs.
 */
static void
pr_fact(BIGNUM *val)
{
	const ubig *fact;	/* The factor found. */

	/* Firewall - catch 0 and 1. */
	if (BN_is_zero(val))	/* Historical practice; 0 just exits. */
		exit(0);
	if (BN_is_one(val)) {
		printf("1: 1\n");
		return;
	}

	/* Factor value. */

	if (hflag) {
		fputs("0x", stdout);
		BN_print_fp(stdout, val);
	} else
		BN_print_dec_fp(stdout, val);
	putchar(':');
	for (fact = &prime[0]; !BN_is_one(val); ++fact) {
		/* Look for the smallest factor. */
		do {
			if (BN_mod_word(val, (BN_ULONG)*fact) == 0)
				break;
		} while (++fact <= pr_limit);

		/* Watch for primes larger than the table. */
		if (fact > pr_limit) {
#ifdef HAVE_OPENSSL
			BIGNUM *bnfact;

			bnfact = BN_new();
			BN_set_word(bnfact, *(fact - 1));
			if (!BN_sqr(bnfact, bnfact, ctx))
				errx(1, "error in BN_sqr()");
			if (BN_cmp(bnfact, val) > 0 ||
			    BN_is_prime(val, PRIME_CHECKS,
					NULL, NULL, NULL) == 1)
				pr_print(val);
			else
				pollard_pminus1(val);
#else
			pr_print(val);
#endif
			break;
		}

		/* Divide factor out until none are left. */
		do {
			printf(hflag ? " 0x%lx" : " %lu", *fact);
			BN_div_word(val, (BN_ULONG)*fact);
		} while (BN_mod_word(val, (BN_ULONG)*fact) == 0);

		/* Let the user know we're doing something. */
		fflush(stdout);
	}
	putchar('\n');
}
Example #2
0
static void pr_error(const char *iname, const char *msg, bool is_warning)
{
    fflush(stdout); fflush(stderr);
    char buf[1024];
    buf[0] = 0;
    if (pr_need_nl == 2)
        printClearLine(stdout);
    else if (pr_need_nl)
    {
        buf[0] = '\n';
        buf[1] = 0;
        printSetNl(0);
    }

    // This hack is needed, otherwise error messages may get lost
    // when the cursor is not yet at the bottom of the screen.
    // At least I can use some colors then...
    bool c = acc_isatty(STDERR_FILENO) ? 1 : 0;

    int fg = con_fg(stderr,FG_BRTRED);
    upx_snprintf(buf+strlen(buf),sizeof(buf)-strlen(buf),"%s: ", progname);
    pr_print(c,buf);
    //(void)con_fg(stderr,FG_RED);
    upx_snprintf(buf,sizeof(buf),"%s: ", iname);
    pr_print(c,buf);
    //(void)con_fg(stderr,FG_BRTRED);
    pr_print(c,msg);
    pr_print(c,"\n");
    fflush(stdout); fflush(stderr);
    fg = con_fg(stderr,fg);

    UNUSED(is_warning);
    UNUSED(fg);
}
Example #3
0
/* pollard p-1, algorithm from Jim Gillogly, May 2000 */
static void
pollard_pminus1(BIGNUM *val)
{
	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(x, PRIME_CHECKS, NULL, NULL,
			    NULL) == 1)
				pr_print(x);
			else
				pollard_pminus1(x);
			fflush(stdout);

			BN_div(num, NULL, val, x, ctx);
			if (BN_is_one(num))
				return;
			if (BN_is_prime(num, PRIME_CHECKS, NULL, NULL,
			    NULL) == 1) {
				pr_print(num);
				fflush(stdout);
				return;
			}
			BN_copy(val, num);
		}
		if (!BN_add_word(i, 1))
			errx(1, "error in BN_add_word()");
	}
}
Example #4
0
acpi_status
pr_add_device(
	BM_HANDLE		device_handle,
	void			**context)
{
	acpi_status		status = AE_OK;
	PR_CONTEXT		*processor = NULL;
	BM_DEVICE		*device = NULL;
	acpi_buffer		buffer;
	acpi_object		acpi_object;
	static u32		processor_count = 0;


	FUNCTION_TRACE("pr_add_device");

	if (!context || *context) {
		return_ACPI_STATUS(AE_BAD_PARAMETER);
	}

	status = bm_get_device_info(device_handle, &device);
	if (ACPI_FAILURE(status)) {
		return_ACPI_STATUS(status);
	}

	processor = acpi_os_callocate(sizeof(PR_CONTEXT));
	if (!processor) {
		return AE_NO_MEMORY;
	}

	processor->device_handle = device->handle;
	processor->acpi_handle = device->acpi_handle;

	/*
	 * Processor Block:
	 * ----------------
	 */
	memset(&acpi_object, 0, sizeof(acpi_object));

	buffer.length = sizeof(acpi_object);
	buffer.pointer = &acpi_object;

	status = acpi_evaluate_object(processor->acpi_handle, NULL, NULL, &buffer);
	if (ACPI_FAILURE(status)) {
		goto end;
	}

	/*
	 * Processor ID:
	 * -------------
	 * TBD:  We need to synchronize the processor ID values in ACPI
	 *       with those of the APIC.  For example, an IBM T20 has a
	 *       proc_id value of '1', where the Linux value for the
	 *       first CPU on this system is '0'.  Since x86 CPUs are
	 *       mapped 1:1 we can simply use a zero-based counter.  Note
	 *       that this assumes that processor objects are enumerated
	 *       in the proper order.
	 */
	/* processor->uid = acpi_object.processor.proc_id; */
	processor->uid = processor_count++;

	processor->pblk.length = acpi_object.processor.pblk_length;
	processor->pblk.address = acpi_object.processor.pblk_address;

	status = pr_power_add_device(processor);
	if (ACPI_FAILURE(status)) {
		goto end;
	}

	status = pr_perf_add_device(processor);
	if (ACPI_FAILURE(status)) {
		goto end;
	}

	status = pr_osl_add_device(processor);
	if (ACPI_FAILURE(status)) {
		goto end;
	}

	*context = processor;

	pr_print(processor);

end:
	if (ACPI_FAILURE(status)) {
		acpi_os_free(processor);
	}

	return_ACPI_STATUS(status);
}