From 6407e18e603ef62cbf9ea28cbd36aa6a640040ed Mon Sep 17 00:00:00 2001
From: magnum <magnum>
Date: Mon, 25 Jul 2011 14:00:19 +0200
Subject: [PATCH 09/10] Refuse to run Unicode formats with other than ansi/utf8

---
 src/john.c  |    8 ++++++++
 src/rules.c |   16 ++++++++--------
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/john.c b/src/john.c
index 4ab2560..545329f 100644
--- a/src/john.c
+++ b/src/john.c
@@ -286,6 +286,7 @@ static void john_load(void)
 			ldr_load_pw_file(&database, current->data);
 		} while ((current = current->next));
 
+		// Unicode (UTF-16) formats may lack UTF-8 support (initially)
 		if (options.utf8 && database.password_count &&
 		    database.format->params.flags & FMT_UNICODE &&
 		    !(database.format->params.flags & FMT_UTF8)) {
@@ -293,6 +294,13 @@ static void john_load(void)
 				error();
 		}
 
+		// We can only convert from 8859-1 or UTF8 to Unicode (UTF-16)
+		if ((options.koi8_r || options.cp1251) && database.password_count &&
+		    database.format->params.flags & FMT_UNICODE) {
+			fprintf(stderr, "This format can only run in 8859-1 or UTF-8 mode\n");
+				error();
+		}
+
 		if ((options.flags & FLG_CRACKING_CHK) &&
 		    database.password_count) {
 			log_init(LOG_NAME, NULL, options.session);
diff --git a/src/rules.c b/src/rules.c
index 50927fc..898243b 100644
--- a/src/rules.c
+++ b/src/rules.c
@@ -107,10 +107,10 @@ static struct {
 #define CHARS_UPPER_8859_1 \
 	"\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD8\xD9\xDA\xDB\xDC\xDD\xDE"
 
-#define CHARS_LOWER_KIOR_8 \
+#define CHARS_LOWER_KOI8_R \
 	"\xA3\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xFF"
 
-#define CHARS_UPPER_KIOR_8 \
+#define CHARS_UPPER_KOI8_R \
 	"\xB3\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF"
 
 #define CHARS_LOWER_CP1251  \
@@ -230,8 +230,8 @@ static void rules_init_classes(void)
 		rules_init_class('l', CHARS_LOWER CHARS_LOWER_8859_1);
 		rules_init_class('u', CHARS_UPPER CHARS_UPPER_8859_1);
 	} else if (options.koi8_r) {
-		rules_init_class('l', CHARS_LOWER CHARS_LOWER_KIOR_8);
-		rules_init_class('u', CHARS_UPPER CHARS_UPPER_KIOR_8);
+		rules_init_class('l', CHARS_LOWER CHARS_LOWER_KOI8_R);
+		rules_init_class('u', CHARS_UPPER CHARS_UPPER_KOI8_R);
 	} else if (options.cp1251) {
 		rules_init_class('l', CHARS_LOWER CHARS_LOWER_CP1251);
 		rules_init_class('u', CHARS_UPPER CHARS_UPPER_CP1251);
@@ -244,8 +244,8 @@ static void rules_init_classes(void)
 		rules_init_class('a', CHARS_LOWER CHARS_UPPER CHARS_LOWER_8859_1 CHARS_UPPER_8859_1);
 		rules_init_class('x', CHARS_LOWER CHARS_UPPER CHARS_LOWER_8859_1 CHARS_UPPER_8859_1 CHARS_DIGITS);
 	} else if (options.koi8_r) {
-		rules_init_class('a', CHARS_LOWER CHARS_UPPER CHARS_LOWER_KIOR_8 CHARS_UPPER_KIOR_8);
-		rules_init_class('x', CHARS_LOWER CHARS_UPPER CHARS_LOWER_KIOR_8 CHARS_UPPER_KIOR_8 CHARS_DIGITS);
+		rules_init_class('a', CHARS_LOWER CHARS_UPPER CHARS_LOWER_KOI8_R CHARS_UPPER_KOI8_R);
+		rules_init_class('x', CHARS_LOWER CHARS_UPPER CHARS_LOWER_KOI8_R CHARS_UPPER_KOI8_R CHARS_DIGITS);
 	} else if (options.cp1251) {
 		rules_init_class('a', CHARS_LOWER CHARS_UPPER CHARS_LOWER_CP1251 CHARS_UPPER_CP1251);
 		rules_init_class('x', CHARS_LOWER CHARS_UPPER CHARS_LOWER_CP1251 CHARS_UPPER_CP1251 CHARS_DIGITS);
@@ -283,8 +283,8 @@ static void rules_init_convs(void)
 		conv_tolower = rules_init_conv(CHARS_UPPER CHARS_UPPER_8859_1, CHARS_LOWER CHARS_LOWER_8859_1);
 		conv_toupper = rules_init_conv(CHARS_LOWER CHARS_LOWER_8859_1, CHARS_UPPER CHARS_UPPER_8859_1);
 	} else if (options.koi8_r) {
-		conv_tolower = rules_init_conv(CHARS_UPPER CHARS_UPPER_KIOR_8, CHARS_LOWER CHARS_LOWER_KIOR_8);
-		conv_toupper = rules_init_conv(CHARS_LOWER CHARS_LOWER_KIOR_8, CHARS_UPPER CHARS_UPPER_KIOR_8);
+		conv_tolower = rules_init_conv(CHARS_UPPER CHARS_UPPER_KOI8_R, CHARS_LOWER CHARS_LOWER_KOI8_R);
+		conv_toupper = rules_init_conv(CHARS_LOWER CHARS_LOWER_KOI8_R, CHARS_UPPER CHARS_UPPER_KOI8_R);
 	} else if (options.cp1251) {
 		conv_tolower = rules_init_conv(CHARS_UPPER CHARS_UPPER_CP1251, CHARS_LOWER CHARS_LOWER_CP1251);
 		conv_toupper = rules_init_conv(CHARS_LOWER CHARS_LOWER_CP1251, CHARS_UPPER CHARS_UPPER_CP1251);
-- 
1.7.4.1

