From 4afc3740da1b1af708dca2af31772d3049c1bf8b Mon Sep 17 00:00:00 2001
From: magnum <magnum>
Date: Fri, 19 Aug 2011 14:02:15 +0200
Subject: [PATCH 25/25] Fixed wordlist.c bug, enable pointer copy

---
 src/wordlist.c |   39 +++++++++++++++++++++++----------------
 1 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/src/wordlist.c b/src/wordlist.c
index bdab060..99cb2cd 100644
--- a/src/wordlist.c
+++ b/src/wordlist.c
@@ -197,6 +197,11 @@ static int get_progress(int *hundth_perc)
 
 static char *dummy_rules_apply(char *word, char *rule, int split, char *last)
 {
+	if (nWordFileLines)
+		// Dupes are already ditched, truncation already done
+		// and we just copy the pointer
+		return (last = word);
+
 	word[length] = 0;
 	if (strcmp(word, last))
 		return strcpy(last, word);
@@ -227,6 +232,8 @@ void do_wordlist_crack(struct db_main *db, char *name, int rules)
 
 	_db = db;
 
+	length = db->format->params.plaintext_length;
+
 	if (name) {
 		char *cp, csearch;
 
@@ -309,7 +316,7 @@ void do_wordlist_crack(struct db_main *db, char *name, int rules)
 					fprintf(stderr, "fread: Unexpected EOF\n");
 					error();
 				}
-				aep = word_file_str + file_len;
+			}
 #else
 		if (file_len < db->options->max_wordfile_memory ||
 		    db->options->max_wordfile_memory == 0)
@@ -318,15 +325,15 @@ void do_wordlist_crack(struct db_main *db, char *name, int rules)
 
 			/* probably should only be debug message, but I left it in */
 			log_event("loading wordfile %s into memory (%lu bytes, max_size=%u)\n", name, file_len, db->options->max_wordfile_memory);
-/* XXX: would need to alloc more for dummy_rules_apply()'s "blind truncation" */
+
 			word_file_str = mem_alloc(file_len+1);
 			if (fread(word_file_str, 1, file_len, word_file) != file_len) {
 				if (ferror(word_file))
 					pexit("fread");
 				fprintf(stderr, "fread: Unexpected EOF\n");
 				error();
-#endif
 			}
+#endif
 			aep = word_file_str + file_len;
 			*aep = 0;
 			csearch = '\n';
@@ -349,9 +356,10 @@ void do_wordlist_crack(struct db_main *db, char *name, int rules)
 				while ((ep < aep) && *ep && *ep != '\n' && *ep != '\r') ep++;
 				ec = *ep;
 				*ep = 0;
-				if (ep - cp >= LINE_BUFFER_SIZE)
-					cp[LINE_BUFFER_SIZE-1] = 0;
-				if (strncmp(cp, "#!comment", 9))
+				if (ep - cp > length)
+					cp[length] = 0;
+				if ((strncmp(cp, "#!comment", 9)) &&
+				    (!i || strcmp(cp, words[i-1])))
 					words[i++] = cp;
 				if (i == nWordFileLines)
 					break;
@@ -374,8 +382,8 @@ void do_wordlist_crack(struct db_main *db, char *name, int rules)
 			log_event("- Reading candidate passwords from stdin");
 		} else {
 			pipe_input = 1;
-			if (db->options->max_wordfile_memory < (2 * LINE_BUFFER_SIZE))
-				db->options->max_wordfile_memory = 2 * LINE_BUFFER_SIZE;
+			if (db->options->max_wordfile_memory < 0x20000)
+				db->options->max_wordfile_memory = 0x20000;
 			max_pipe_words = (db->options->max_wordfile_memory/16);
 
 			word_file_str = mem_alloc(db->options->max_wordfile_memory);
@@ -397,11 +405,13 @@ GRAB_NEXT_PIPE_LOAD:;
 				cpi = word_file_str;
 				cpe = (cpi + db->options->max_wordfile_memory) - LINE_BUFFER_SIZE;
 				while (nWordFileLines < max_pipe_words) {
-					if (!fgetl(cpi, LINE_BUFFER_SIZE, word_file)) {
+					if (!fgetl(cpi, length+3, word_file)) {
 						pipe_input = 0; /* We are now done.  After processing, do NOT goto the GRAB_NEXT... again */
 						break;
 					}
-					if (strncmp(cpi, "#!comment", 9)) {
+					cpi[length] = 0;
+					if ((strncmp(cpi, "#!comment", 9)) &&
+						(!(cpi != word_file_str) || strcmp(cpi, words[nWordFileLines-1]))) {
 						words[nWordFileLines++] = cpi;
 						cpi += (strlen(cpi)+1);
 						if (cpi > cpe)
@@ -414,8 +424,6 @@ GRAB_NEXT_PIPE_LOAD:;
 		}
 	}
 
-	length = db->format->params.plaintext_length;
-
 	if (rules) {
 		if (rpp_init(rule_ctx = &ctx, db->options->activewordlistrules)) {
 			log_event("! No wordlist mode rules found");
@@ -533,12 +541,11 @@ GRAB_NEXT_PIPE_LOAD:;
 #ifdef HAVE_MPI
 				if (!distributeWords || line_number % mpi_p == mpi_id)
 #endif
-#if 0 && ARCH_ALLOWS_UNALIGNED
-/* XXX: somehow this breaks things - why? */
+#if ARCH_ALLOWS_UNALIGNED
 					line = words[nCurLine++];
-#else /* 0 && ARCH_ALLOWS_UNALIGNED */
+#else
 					strcpy(line, words[nCurLine++]);
-#endif /* 0 && ARCH_ALLOWS_UNALIGNED */
+#endif
 			}
 			else {
 				if (!fgetl(line, LINE_BUFFER_SIZE, word_file))
-- 
1.7.4.1

