示例#1
0
文件: main.c 项目: 0rbiter/matasano
int main(int argc, char **argv)
{
	char *conffile = "usr/share/libexttextcat/fpdb.conf";
	//char *conffile = "fpdb.conf";
	void *h = textcat_Init(conffile);

        /*      
        s1c1();
        s1c2();
        s1c3();
        s1c4();
        s1c5();
	*/
	hamming_test();
        char *filename6 = "/home/uniscon169/matasano/src/challenge6keys.txt";
        struct file_o *filebuffer6 = read_bytes(filename6);
        file_o_init(filebuffer6);
        char *b64string = xmalloc(1);
        char *wholestring = get_string(&filebuffer6);
        long length;
        char *STR_XOR = xmalloc(1);
        int keys_total = 3;
        struct histogram *hist = hist_o_init(keys_total);
        length = active_b64_decode_string(&b64string, wholestring, strlen(wholestring));
        get_keylength(&hist, 20, b64string, length, keys_total);
        transpose(&hist, &hist->tdata, &hist->data, length, hist->ham->keylength[0]);
        //hist->scores->testkey = xrealloc(hist->scores->testkey, (hist->tdata->elements + 1) * sizeof(char));
        int x;
        char ENCCHAR[] = "A\0";
        for(long c = 0; c < hist->tdata->elements; c++) {
                for(x=0; x < 256; x++) {
                        ENCCHAR[0] = x;
                        memset(STR_XOR, hist->tdata->elements, 0);
                        xor_bytes_to_string(&STR_XOR, hist->tdata->blocks[c], hist->tdata->lengths[c], ENCCHAR, 1);
			printf( "Language: %s\n", textcat_Classify(h, STR_XOR, hist->tdata->lengths[c]));
                        //get_score(STR_XOR, hist->tdata->lengths[c], ENCCHAR, -100, 3.0f, 0);
                        add_betterscore(&hist, c, STR_XOR, ENCCHAR[0]);
                }
        }
        printf("\n");
        //STR_XOR = xrealloc(STR_XOR, (hist->inputlength + 1) * sizeof(char));
        //xor_bytes_to_string(&STR_XOR, hist->data, length, hist->scores->testkey, 5);
        printf("\n\n\n");
        //puts(STR_XOR);
        xfree(STR_XOR);
        xfree(wholestring);
        xfree(b64string);
        file_o_destroy(filebuffer6);
        hist_o_destroy(hist); 
        double tempf = 0;
        for(x = 0; x < 26; x++)
                tempf += pow((double) (letterscore_en[x] / 100.0), 2.0);
        printf("\n%0.5f\n\n", tempf);
	textcat_Done(h);
}
void run_slave(int myrank) {
	void *my_tc = llamapun_textcat_Init();
	char filename[FILE_NAME_SIZE];
	char message[RETURN_MESSAGE_SIZE];
	MPI_Status status;
	while (1) {
		MPI_Recv(&filename, FILE_NAME_SIZE, MPI_CHAR, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
		if (status.MPI_TAG == 0) {
			printf("%2d - exiting\n", myrank);
			break;
		} else if (status.MPI_TAG == 1) {
			//do the actual job

			//printf("%2d - %s\n", myrank, filename);

			xmlDoc *doc = read_document(filename);
			if (doc == NULL) {
				snprintf(message, RETURN_MESSAGE_SIZE, "Couldn't load document %s\n", filename);
				MPI_Send(message, RETURN_MESSAGE_SIZE, MPI_CHAR, /*dest = */ 0, /*tag = message */ 1, MPI_COMM_WORLD);
			}
			
			dnmPtr dnm = create_DNM(xmlDocGetRootElement(doc), DNM_SKIP_TAGS);

			if (dnm == NULL) {
				fprintf(stderr, "%2d - Couldn't create DNM - exiting\n", myrank);
				exit(1);
			}
			char *result = textcat_Classify(my_tc, dnm->plaintext, dnm->size_plaintext);
			if (strncmp(result, "[english]", strlen("[english]"))) {  //isn't primarily english
				snprintf(message, RETURN_MESSAGE_SIZE, "%s\t%s\n", filename, result);
				MPI_Send(message, RETURN_MESSAGE_SIZE, MPI_CHAR, /*dest = */ 0, /*tag = message */ 1, MPI_COMM_WORLD);
			}
			else {
				snprintf(message, RETURN_MESSAGE_SIZE, "%s\tenglish\n", filename);
				printf("%2d - %s", myrank, message);
				MPI_Send(message, RETURN_MESSAGE_SIZE, MPI_CHAR, /*dest = */ 0, /*tag = nothing special */ 0, MPI_COMM_WORLD);
			}

			//clean up
			free_DNM(dnm);
			xmlFreeDoc(doc);
		} else {
			fprintf(stderr, "%2d - Error: Unkown tag: %d - exiting\n", myrank, status.MPI_TAG);
			break;
		}
	}
	//clean up
	textcat_Done(my_tc);
	xmlCleanupParser();
}