Commit 191c5e82 authored by Mathieu Valois's avatar Mathieu Valois
Browse files

single thread

parent df80fdf5
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -146,6 +146,7 @@ public:
	* @param threadarg : parameters and result storage
	*/
	static void* generate_stats_thread(void * threadarg);
	void handle_password(const std::string& password, const uint64_t& nbPasswords, ThreadData& td) const;

private:
	std::string filename;
+86 −79
Original line number Diff line number Diff line
@@ -69,44 +69,51 @@ int Statsgen::generate_stats() {
		return 0;
	}

	vector<pthread_t> threads(nbThread);

	for(uint i = 0; i < nbThread; i++ ) {
		td[i].thread_id = i + 1;
		configureThread(td[i]);

		if(i == 0){
			td[i].lineBegin = 0;
		}
		else{
			td[i].lineBegin =  td[i-1].lineEnd + 1;
		}
		td[i].lineEnd = (i+1)*nbline/nbThread;

		if(debug_enabled){
			cerr << "[DEBUG] " << "Thread " << td[i].thread_id << " analyse : " << td[i].lineBegin << " --> " << td[i].lineEnd << endl;
		}

		int rc = pthread_create(&threads[i], NULL, generate_stats_thread, (void *)&td[i] );

		if (rc) {
			cerr << "[ERROR] unable to create thread," << rc << endl;
			exit(-1);
		}
	}

	started = true;

	void *status;
	for(uint i = 0; i < nbThread; i++ ) {
		int rc = pthread_join(threads[i], &status);
		if (rc) {
			cerr << "[ERROR] unable to join," << rc << endl;
			exit(-1);
		}
		results += td[i];
		finished++;
	}
	ifstream inputfile(filename);
	string line;
	for(uint numline = 0; numline < nbline; ++numline){
		getline(inputfile, line);
		handle_password(line, 1, results);
	}

	// vector<pthread_t> threads(nbThread);

	// for(uint i = 0; i < nbThread; i++ ) {
	// 	td[i].thread_id = i + 1;
	// 	configureThread(td[i]);

	// 	if(i == 0){
	// 		td[i].lineBegin = 0;
	// 	}
	// 	else{
	// 		td[i].lineBegin =  td[i-1].lineEnd + 1;
	// 	}
	// 	td[i].lineEnd = (i+1)*nbline/nbThread;

	// 	if(debug_enabled){
	// 		cerr << "[DEBUG] " << "Thread " << td[i].thread_id << " analyse : " << td[i].lineBegin << " --> " << td[i].lineEnd << endl;
	// 	}

	// 	int rc = pthread_create(&threads[i], NULL, generate_stats_thread, (void *)&td[i] );

	// 	if (rc) {
	// 		cerr << "[ERROR] unable to create thread," << rc << endl;
	// 		exit(-1);
	// 	}
	// }

	// started = true;

	// void *status;
	// for(uint i = 0; i < nbThread; i++ ) {
	// 	int rc = pthread_join(threads[i], &status);
	// 	if (rc) {
	// 		cerr << "[ERROR] unable to join," << rc << endl;
	// 		exit(-1);
	// 	}
	// 	results += td[i];
	// 	finished++;
	// }

	if (!results.total_counter) {
		cerr << "[ERROR] Empty file or not existing file" << endl;
@@ -232,64 +239,64 @@ pair<uint, uint> get_masks(const string& password, PasswordStats& c){
	return make_pair(sizeSimpleMask, sizeAdvancedMask);
}

void handle_password(const string& password, const uint64_t& nbPasswords, ThreadData* my_data){
	my_data->total_counter += nbPasswords;
	if(my_data->use_regex && !regex_match(password,my_data->current_regex)){
void Statsgen::handle_password(const string& password, const uint64_t& nbPasswords, ThreadData& my_data) const {
	my_data.total_counter += nbPasswords;
	if(my_data.use_regex && !regex_match(password,my_data.current_regex)){
		return;
	}
	PasswordStats c;

	pair<uint, uint> masks = get_masks(password, c);

	if(c.pol.satisfies(my_data->sr, password.size())){
		my_data->sr.nbSecurePassword++;
	if(c.pol.satisfies(my_data.sr, password.size())){
		my_data.sr.nbSecurePassword++;
	}

	if (masks.first > my_data->limitSimplemask) {
	if (masks.first > my_data.limitSimplemask) {
		c.simplemask_string = "othermasks";
	}
	if (masks.second > my_data->limitAdvancedmask) {
	if (masks.second > my_data.limitAdvancedmask) {
		c.advancedmask_string = "othermasks";
	}

	my_data->total_filter += nbPasswords;
	my_data->length[ password.size() ] += nbPasswords;
	my_data->charactersets[ c.pol ] += nbPasswords;
	my_data->simplemasks[ c.simplemask_string ] += nbPasswords;
	my_data->advancedmasks[ c.advancedmask_string ] += nbPasswords;
	my_data->minMaxValue.updateMinMax(c.pol);
}

void* Statsgen::generate_stats_thread(void* threadarg) {
	ThreadData* my_data = (ThreadData *) threadarg;

	ifstream readfile(my_data->filename);
	uint64_t nbline = 0;
	string line;
	string password;
	uint64_t nbPasswords;

	while(getline(readfile, line)){
		++nbline;
		if (nbline < my_data->lineBegin){ continue; }
		if (nbline > my_data->lineEnd){ break; }
		if (line.size() == 0){ continue; }

		if (my_data->withcount) {
			istringstream is(line);
			is >> nbPasswords;
			is >> password;
		}
		else {
			nbPasswords = 1;
			password = line;
		}
		handle_password(password, nbPasswords, my_data);
	}

	readfile.close();
	pthread_exit(NULL);
}
	my_data.total_filter += nbPasswords;
	my_data.length[ password.size() ] += nbPasswords;
	my_data.charactersets[ c.pol ] += nbPasswords;
	my_data.simplemasks[ c.simplemask_string ] += nbPasswords;
	my_data.advancedmasks[ c.advancedmask_string ] += nbPasswords;
	my_data.minMaxValue.updateMinMax(c.pol);
}

// void* Statsgen::generate_stats_thread(void* threadarg) {
// 	ThreadData* my_data = (ThreadData *) threadarg;

// 	ifstream readfile(my_data->filename);
// 	uint64_t nbline = 0;
// 	string line;
// 	string password;
// 	uint64_t nbPasswords;

// 	while(getline(readfile, line)){
// 		++nbline;
// 		if (nbline < my_data->lineBegin){ continue; }
// 		if (nbline > my_data->lineEnd){ break; }
// 		if (line.size() == 0){ continue; }

// 		if (my_data->withcount) {
// 			istringstream is(line);
// 			is >> nbPasswords;
// 			is >> password;
// 		}
// 		else {
// 			nbPasswords = 1;
// 			password = line;
// 		}
// 		handle_password(password, nbPasswords, my_data);
// 	}

// 	readfile.close();
// 	pthread_exit(NULL);
// }

bool Statsgen::operator==(const Statsgen& o) const {
	return results == o.results;