Exemplo n.º 1
0
/*------------------------------------------------------------------*/
void write_poly(msieve_obj *obj, mp_t *n,
	       mp_poly_t *rat_poly,
	       mp_poly_t *alg_poly,
	       double skewness) {
	
	/* log a generated polynomial to the factor base file */

	uint32 i;
	FILE *fp;

	fp = fopen(obj->nfs_fbfile_name, "w");
	if (fp == NULL) {
		printf("error; cannot open factor base file '%s'\n",
					obj->nfs_fbfile_name);
		exit(-1);
	}

	fprintf(fp, "N %s\n", mp_sprintf(n, 10, obj->mp_sprintf_buf));
	if (skewness > 0)
		fprintf(fp, "SKEW %.2lf\n", skewness);

	for (i = 0; i <= rat_poly->degree; i++) {
		fprintf(fp, "R%u %c%s\n", i,
			(rat_poly->coeff[i].sign == POSITIVE? ' ' : '-'),
			mp_sprintf(&rat_poly->coeff[i].num, 10,
						obj->mp_sprintf_buf));
	}
	for (i = 0; i <= alg_poly->degree; i++) {
		fprintf(fp, "A%u %c%s\n", i,
			(alg_poly->coeff[i].sign == POSITIVE? ' ' : '-'),
			mp_sprintf(&alg_poly->coeff[i].num, 10,
						obj->mp_sprintf_buf));
	}
	fclose(fp);
}
Exemplo n.º 2
0
END_TEST

START_TEST (test_sprintf_fail) {
    char *dest;

    dest = malloc(128);

    mp_sprintf(dest, "%");
}
Exemplo n.º 3
0
END_TEST

// mp_sprintf
START_TEST (test_sprintf_ok) {
    char *dest;

    dest = malloc(128);

    mp_sprintf(dest, "%s", "test");

    fail_unless (strcmp(dest, "test") == 0,
            "mp_sprintf failed: %s", dest);
}
Exemplo n.º 4
0
/*--------------------------------------------------------------------*/
uint32 factor_mpqs(msieve_obj *obj, mp_t *n, 
			factor_list_t *factor_list) {

	uint32 bits, fb_size;
	prime_list_t prime_list;
	fb_t *factor_base;
	uint32 *modsqrt_array;
	sieve_param_t params;
	relation_t *relation_list = NULL;
	uint32 num_relations = 0;
	la_col_t *cycle_list = NULL;
	uint32 num_cycles = 0;
	poly_t *poly_list = NULL;
	mp_t *poly_a_list = NULL;
	uint64 *bitfield = NULL;
	uint32 multiplier;
	uint32 factor_found = 0;

	/* Calculate the factor base bound */

	bits = mp_bits(n);
	get_sieve_params(bits, &params);
	if (params.fb_size < 100)
		params.fb_size = 100;

	logprintf(obj, "commencing quadratic sieve (%u-digit input)\n",
			strlen(mp_sprintf(n, 10, obj->mp_sprintf_buf)));

	/* Make a prime list, use it to build a factor base */

	fill_prime_list(&prime_list, 2 * params.fb_size + 100, MAX_FB_PRIME);
	build_factor_base(n, &prime_list, &factor_base, 
				&modsqrt_array, &params.fb_size, 
				&multiplier);
	fb_size = params.fb_size;
	logprintf(obj, "using multiplier of %u\n", multiplier);
	free(prime_list.list);
	
	/* Proceed with the algorithm */

	do_sieving(obj, n, &poly_a_list, &poly_list, 
		   factor_base, modsqrt_array, &params, multiplier, 
		   &relation_list, &num_relations,
		   &cycle_list, &num_cycles);

	free(modsqrt_array);
	if (relation_list == NULL || cycle_list == NULL ||
	    cycle_list == NULL || poly_a_list == NULL || poly_list == NULL) {

		free(factor_base);
		return 0;
	}

	solve_linear_system(obj, fb_size, &bitfield, 
			relation_list, cycle_list, &num_cycles);

	if (bitfield != NULL && num_cycles > 0) {
		factor_found = find_factors(obj, n, factor_base, fb_size, 
					cycle_list, num_cycles, 
					relation_list, bitfield, 
					multiplier, poly_a_list, poly_list,
					factor_list);
	}

	free(factor_base);
	free(bitfield);
	free_cycle_list(cycle_list, num_cycles);
	qs_free_relation_list(relation_list, num_relations);
	free(poly_list);
	free(poly_a_list);
	return factor_found;
}
Exemplo n.º 5
0
int main (int argc, char **argv) {
    /* Local Vars */
    int             i;
    int             j;
    char            *filename;
    bonding_info    *info;
    char            *buf;
    char            *output = NULL;
    int             status = STATE_OK;

    /* Set signal handling and alarm */
    if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR)
        critical("Setup SIGALRM trap failed!");

    /* Process check arguments */
    if (process_arguments(argc, argv) != OK)
        unknown("Parsing arguments failed!");

    /* Start plugin timeout */
    alarm(mp_timeout);

    if (bonds==0) {
        DIR *dir;
        struct dirent   *entry;
        dir = opendir("/proc/net/bonding/");
        if (dir == NULL)
            critical("Can't open '/proc/net/bonding/'");

        while ((entry = readdir(dir)) != NULL) {
            if (strncmp(entry->d_name, "bond", 4) == 0) {
                mp_array_push(&bond, strdup(entry->d_name), &bonds);
            }
        }

        closedir(dir);
    }

    filename = mp_malloc(sizeof(char)*32);
    buf = mp_malloc(sizeof(char)*64);

    for(i=0; i < bonds; i++) {
        mp_sprintf(filename, "/proc/net/bonding/%s", bond[i]);
        info = parseBond(filename);
        if (info == NULL) {
            status = STATE_CRITICAL;
            mp_snprintf(buf, 64, "%s not found", bond[i]);
            mp_strcat_comma(&output, buf);
        } else if (info->mii_status) {
            char *up = NULL;
            char *down = NULL;
            for(j=0; j < info->slaves; j++) {
                if (info->slave[j]->mii_status) {
                    mp_strcat_comma(&up, info->slave[j]->interface);
                } else {
                    mp_strcat_comma(&down, info->slave[j]->interface);
                }
            }
            if (down) {
                mp_snprintf(buf, 64, "%s up (%s, down: %s)", bond[i], up, down);
                status = status == STATE_OK ? STATE_WARNING : status;
            } else {
                mp_snprintf(buf, 64, "%s up (%s)", bond[i], up);
            }
            mp_strcat_comma(&output, buf);
        } else {
            mp_snprintf(buf, 64, "%s down (%s)", bond[i], info->mode);
            mp_strcat_comma(&output, buf);
            status = STATE_CRITICAL;
        }

    }

    switch (status) {
        case STATE_OK:
            ok(output);
            break;
        case STATE_WARNING:
            warning(output);
            break;
        case STATE_CRITICAL:
            critical(output);
            break;
        case STATE_UNKNOWN:
            unknown(output);
            break;
    }

    critical("You should never reach this point.");
}
Exemplo n.º 6
0
/*--------------------------------------------------------------------*/
uint32 factor_gnfs(msieve_obj *obj, mp_t *n,
			factor_list_t *factor_list) {

	int32 status;
	uint32 bits;
	sieve_param_t params;
	mp_poly_t rat_poly;
	mp_poly_t alg_poly;
	uint32 relations_found = 0;
	uint32 max_relations = 0;
	uint32 factor_found = 0;

	/* Calculate the factor base bound */

	bits = mp_bits(n);
	get_sieve_params(bits, &params);

	logprintf(obj, "commencing number field sieve (%d-digit input)\n",
			strlen(mp_sprintf(n, 10, obj->mp_sprintf_buf)));

	/* generate or read in the NFS polynomials */

	memset(&rat_poly, 0, sizeof(rat_poly));
	memset(&alg_poly, 0, sizeof(alg_poly));
	status = read_poly(obj, n, &rat_poly, &alg_poly, &params.skewness);
	if (status != 0 && 
	   (obj->flags & (MSIEVE_FLAG_NFS_POLY1 | 
			  MSIEVE_FLAG_NFS_POLY2))) {
		status = find_poly(obj, n);
		status = read_poly(obj, n, &rat_poly, 
					&alg_poly, &params.skewness);
	}
	if (status != 0) {
		printf("error generating or reading NFS polynomials\n");
		return 0;
	}
	analyze_one_poly(obj, &rat_poly, &alg_poly, params.skewness);

	if ((obj->flags & MSIEVE_FLAG_STOP_SIEVING) ||
	    !(obj->flags & (MSIEVE_FLAG_NFS_SIEVE |
	    		    MSIEVE_FLAG_NFS_FILTER |
			    MSIEVE_FLAG_NFS_LA |
			    MSIEVE_FLAG_NFS_SQRT))) {
		return 0;
	}

	/* if we're supposed to be sieving, 
	   initialize the savefile */

	if (obj->flags & (MSIEVE_FLAG_NFS_SIEVE |
			  MSIEVE_FLAG_NFS_FILTER)) {

		/* figure out how many relations to look for, and 
		   quit early if that many have already been found */

		relations_found = nfs_init_savefile(obj, n);
		if (obj->max_relations > 0) {
			max_relations = relations_found + 
						obj->max_relations;
		}
		else {
			/* if no guidance on this is available, make a
			   wild guess: a fixed fraction of the total number 
			   of large primes possible */

			max_relations = 0.8 * (params.rfb_lp_size /
					(log((double)params.rfb_lp_size) - 1) +
					params.afb_lp_size /
					(log((double)params.afb_lp_size) - 1));
		}
	}

	/* this is a little tricky: perform only sieving (and
	   quit when enough relations are found), only filtering
	   (and quit if it fails, or continue the postprocessing
	   if it succeeds), or alternate between sieving and
	   filtering until the filtering succeeds */

	while (1) {
		if (!(obj->flags & (MSIEVE_FLAG_NFS_SIEVE |
				    MSIEVE_FLAG_NFS_FILTER))) {
			break;
		}

		if (obj->flags & MSIEVE_FLAG_NFS_SIEVE) {
			savefile_open(&obj->savefile, SAVEFILE_APPEND);
			relations_found = do_line_sieving(obj, &params, n, 
							relations_found,
							max_relations);
			savefile_close(&obj->savefile);
			if (relations_found == 0)
				break;
			if (!(obj->flags & MSIEVE_FLAG_NFS_FILTER))
				return 0;
		}

		if (obj->flags & MSIEVE_FLAG_STOP_SIEVING)
			return 0;

		if (obj->flags & MSIEVE_FLAG_NFS_FILTER) {

			max_relations = nfs_filter_relations(obj, n);
			if (max_relations == 0)
				break;
			logprintf(obj, "filtering wants %u more relations\n",
							max_relations);
			if (!(obj->flags & MSIEVE_FLAG_NFS_SIEVE))
				return 0;
			max_relations += relations_found;
		}
	}

	if (obj->flags & MSIEVE_FLAG_NFS_LA)
		nfs_solve_linear_system(obj, n);
		
	if (obj->flags & MSIEVE_FLAG_NFS_SQRT)
		factor_found = nfs_find_factors(obj, n, factor_list);

	return factor_found;
}
Exemplo n.º 7
0
/*------------------------------------------------------------------*/
static uint32 nfs_init_savefile(msieve_obj *obj, mp_t *n) {

	char buf[LINE_BUF_SIZE];
	uint32 relations_found = 0;
	savefile_t *savefile = &obj->savefile;
	uint32 update = 1;

	/* open the savefile; if the file already
	   exists and the first line contains n,
	   then we are restarting from a previous factorization */

	if (savefile_exists(savefile)) {
		savefile_open(savefile, SAVEFILE_READ);
		buf[0] = 0;
		savefile_read_line(buf, sizeof(buf), savefile);
		if (buf[0] == 'N') {
			mp_t read_n;
			mp_str2mp(buf + 2, &read_n, 10);
			if (mp_cmp(n, &read_n) == 0)
				update = 0;
		}
		savefile_close(savefile);
	}

	if (update && (obj->flags & MSIEVE_FLAG_NFS_SIEVE)) {
		/* If sieving is about to add new relations, truncate 
		   the file and write the present n. I hope you backed 
		   up savefiles you wanted! */

		savefile_open(savefile, SAVEFILE_WRITE);
		sprintf(buf, "N %s\n", mp_sprintf(n, 10, obj->mp_sprintf_buf));
		savefile_write_line(savefile, buf);
		savefile_flush(savefile);
		savefile_close(savefile);
	}
	else {
		/* we don't care how many relations are present,
		   so don't waste time counting them */
		return 0;
	}


	/* count the number of relations in the savefile;
	   do not verify any of them */

	savefile_open(savefile, SAVEFILE_READ);

	while (!savefile_eof(savefile)) {

		/* count relations; No checking of relations 
		   happens here */

		savefile_read_line(buf, sizeof(buf), savefile);

		while (!savefile_eof(savefile)) {
			if (isdigit(buf[0]) || buf[0] == '-')
				relations_found++;
			savefile_read_line(buf, sizeof(buf), savefile);
		}

		if (relations_found)
			logprintf(obj, "restarting with %u relations\n",
							relations_found);
	}
	savefile_close(savefile);
	return relations_found;
}
Exemplo n.º 8
0
char *sms_encode_text(const char *text) {
    int i;
    int len = 0;
    unsigned int bit;
    int bits;
    char *ptr;
    char *pdu;
    wchar_t *t = NULL;

    i = mbstowcs(NULL, text, 0);
    t = mp_malloc(sizeof(wchar_t)*(i+1));
    memset(t,0,i+1);
    i = mbstowcs(t, text, i+1);

    pdu = mp_malloc((i * 7)/4+3);
    memset(pdu,0,(i * 7)/4+3);
    sprintf(pdu, "XX");
    ptr = pdu;
    ptr += 2;

    bit = 0;
    bits = 0;

    for (i=0; i<wcslen(t); i++) {
        wchar_t *pos = wcschr(sms_gsm7, t[i]);
        if (pos == NULL) {
            pos = wcschr(sms_gsm7_ext, t[i]);
            if (pos == NULL) {
                continue;
            }
            // Excape
            bit |= 0x1B << bits;
            len += 1;
            bits += 7;
            bit |= (pos-sms_gsm7_ext) << bits;
            len += 1;
            bits += 7;
        } else {
            bit |= (pos-sms_gsm7) << bits;
            len += 1;
            bits += 7;
        }
        while (bits >= 8) {
            mp_sprintf(ptr, "%02X", (bit & 0xFF));
            ptr += 2;
            bit = bit >> 8;
            bits -= 8;
        }
    }
    if (bits > 0) {
        mp_sprintf(ptr, "%02X", (bit & 0xFF));
        ptr += 2;
    }

    // Add Length
    mp_asprintf(&ptr, "%02X", len);
    memcpy(pdu, ptr, 2);
    free(ptr);

    return pdu;
}