From bfcb24f532bf051a39b4511c3e9c87f10ec362e6 Mon Sep 17 00:00:00 2001
From: magnum <magnum>
Date: Wed, 5 Oct 2011 12:04:22 +0200
Subject: [PATCH 19/20] j7: Added dstlen argument to a couple unicode functions that lacked them

---
 src/cracker.c       |    6 +++---
 src/loader.c        |   10 +++++-----
 src/sapG_fmt_plug.c |    4 ++--
 src/status.c        |    8 ++++----
 src/unicode.c       |   12 ++++++------
 src/unicode.h       |    4 ++--
 6 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/src/cracker.c b/src/cracker.c
index 3eb0fbf..89fdf6a 100644
--- a/src/cracker.c
+++ b/src/cracker.c
@@ -110,10 +110,10 @@ static int crk_process_guess(struct db_salt *salt, struct db_password *pw,
 		if (options.utf8)
 			utf8key = key;
 		else {
-			utf8key = (char*)enc_to_utf8_r(key, utf8buf_key);
+			utf8key = (char*)enc_to_utf8_r(key, utf8buf_key, PLAINTEXT_BUFFER_SIZE);
 			// Double-check that the conversion was correct. Our
 			// fallback is to log, warn and use the original key instead.
-			utf8_to_enc_r((UTF8*)utf8key, tmp8);
+			utf8_to_enc_r((UTF8*)utf8key, tmp8, PLAINTEXT_BUFFER_SIZE);
 			if (strcmp(tmp8, key)) {
 				fprintf(stderr, "Warning, conversion failed %s -> %s -> %s - fallback to codepage\n", key, utf8key, tmp8);
 				log_event("Warning, conversion failed %s -> %s -> %s - fallback to codepage", key, utf8key, tmp8);
@@ -123,7 +123,7 @@ static int crk_process_guess(struct db_salt *salt, struct db_password *pw,
 		if (options.report_utf8) {
 			repkey = utf8key;
 			if (crk_db->options->flags & DB_LOGIN)
-				replogin = (char*)enc_to_utf8_r(pw->login, utf8login);
+				replogin = (char*)enc_to_utf8_r(pw->login, utf8login, PLAINTEXT_BUFFER_SIZE);
 		}
 		if (options.store_utf8)
 			key = utf8key;
diff --git a/src/loader.c b/src/loader.c
index ceac315..358fe79 100644
--- a/src/loader.c
+++ b/src/loader.c
@@ -650,7 +650,7 @@ static void ldr_remove_marked(struct db_main *db)
 				if (db->options->showuncracked) {
 					if (!options.utf8 && options.report_utf8) {
 						UTF8 utf8login[PLAINTEXT_BUFFER_SIZE + 1];
-						enc_to_utf8_r(current_pw->login, utf8login);
+						enc_to_utf8_r(current_pw->login, utf8login, PLAINTEXT_BUFFER_SIZE);
 						printf("%s%c%s\n",utf8login,db->options->field_sep_char,current_pw->source);
 					} else
 						printf("%s%c%s\n",current_pw->login,db->options->field_sep_char,current_pw->source);
@@ -934,8 +934,8 @@ static void ldr_show_pw_line(struct db_main *db, char *line)
 	int pass, found, chars;
 	int hash;
 	struct db_cracked *current;
-	UTF8 utf8login[LINE_BUFFER_SIZE];
-	UTF8 utf8source[LINE_BUFFER_SIZE];
+	UTF8 utf8login[LINE_BUFFER_SIZE + 1];
+	UTF8 utf8source[LINE_BUFFER_SIZE + 1];
 
 	format = NULL;
 	count = ldr_split_line(&login, &ciphertext, &gecos, &home,
@@ -948,8 +948,8 @@ static void ldr_show_pw_line(struct db_main *db, char *line)
 	show = !(db->options->flags & DB_PLAINTEXTS);
 
 	if (!options.utf8 && !options.store_utf8 && options.report_utf8) {
-		login = (char*)enc_to_utf8_r(login, utf8login);
-		enc_to_utf8_r(source, utf8source);
+		login = (char*)enc_to_utf8_r(login, utf8login, LINE_BUFFER_SIZE);
+		enc_to_utf8_r(source, utf8source, LINE_BUFFER_SIZE);
 		strnzcpy(source, (char*)utf8source, sizeof(source));
 	}
 
diff --git a/src/sapG_fmt_plug.c b/src/sapG_fmt_plug.c
index c2b4e52..785953a 100644
--- a/src/sapG_fmt_plug.c
+++ b/src/sapG_fmt_plug.c
@@ -142,9 +142,9 @@ static void sapcodvng_set_key(char *key, int index)
 		// convert from codepage -> Unicode -> UTF-8
 		UTF16 tmp16[PLAINTEXT_LENGTH + 1];
 #if ARCH_LITTLE_ENDIAN
-		enc_to_utf16(tmp16, PLAINTEXT_LENGTH + 1, (UTF8*)key, PLAINTEXT_LENGTH);
+		enc_to_utf16(tmp16, PLAINTEXT_LENGTH + 1, (UTF8*)key, strlen(key));
 #else
-		enc_to_utf16_be(tmp16, PLAINTEXT_LENGTH + 1, (UTF8*)key, PLAINTEXT_LENGTH);
+		enc_to_utf16_be(tmp16, PLAINTEXT_LENGTH + 1, (UTF8*)key, strlen(key));
 #endif
 		utf16_to_utf8_r(trPassword, PLAINTEXT_LENGTH + 1, tmp16);
 	}
diff --git a/src/status.c b/src/status.c
index 96e88d7..0133af1 100644
--- a/src/status.c
+++ b/src/status.c
@@ -442,8 +442,8 @@ static void status_print_cracking(char *percent)
 		UTF8 t2buf[PLAINTEXT_BUFFER_SIZE + 1];
 		char *t1, *t2;
 		if (options.report_utf8 && !options.utf8) {
-			t1 = (char*)enc_to_utf8_r(crk_get_key1(), t1buf);
-			t2 = (char*)enc_to_utf8_r(saved_key, t2buf);
+			t1 = (char*)enc_to_utf8_r(crk_get_key1(), t1buf, PLAINTEXT_BUFFER_SIZE);
+			t2 = (char*)enc_to_utf8_r(saved_key, t2buf, PLAINTEXT_BUFFER_SIZE);
 		} else {
 			t1 = crk_get_key1();
 			t2 = saved_key;
@@ -490,8 +490,8 @@ static void status_print_cracking(char *percent)
 		UTF8 t2buf[PLAINTEXT_BUFFER_SIZE + 1];
 		char *t1, *t2;
 		if (options.report_utf8 && !options.utf8) {
-			t1 = (char*)enc_to_utf8_r(crk_get_key1(), t1buf);
-			t2 = (char*)enc_to_utf8_r(saved_key, t2buf);
+			t1 = (char*)enc_to_utf8_r(crk_get_key1(), t1buf, PLAINTEXT_BUFFER_SIZE);
+			t2 = (char*)enc_to_utf8_r(saved_key, t2buf, PLAINTEXT_BUFFER_SIZE);
 		} else {
 			t1 = crk_get_key1();
 			t2 = saved_key;
diff --git a/src/unicode.c b/src/unicode.c
index dea059c..6515a9e 100644
--- a/src/unicode.c
+++ b/src/unicode.c
@@ -711,22 +711,22 @@ UTF8 *utf16_to_utf8_r (UTF8 *dst, int dst_len, const UTF16 *source) {
 }
 
 /* Thread-safe conversion from codepage to UTF-8 */
-UTF8 *enc_to_utf8_r (char *src, UTF8 *dst)
+UTF8 *enc_to_utf8_r (char *src, UTF8 *dst, int dstlen)
 {
 	UTF16 tmp16[LINE_BUFFER_SIZE + 1];
 	enc_to_utf16(tmp16, LINE_BUFFER_SIZE, (unsigned char*)src,
-	             LINE_BUFFER_SIZE);
-	dst = utf16_to_utf8_r(dst, LINE_BUFFER_SIZE, tmp16);
+	             strlen((char*)src));
+	dst = utf16_to_utf8_r(dst, dstlen, tmp16);
 	return dst;
 }
 
 /* Thread-safe conversion from UTF-8 to codepage */
-char * utf8_to_enc_r (UTF8 *src, char* dst)
+char * utf8_to_enc_r (UTF8 *src, char* dst, int dstlen)
 {
 	UTF16 tmp16[LINE_BUFFER_SIZE + 1];
 	utf8_to_utf16(tmp16, LINE_BUFFER_SIZE, (unsigned char*)src,
-	              LINE_BUFFER_SIZE);
-	dst = (char*)utf16_to_enc_r((UTF8*)dst, LINE_BUFFER_SIZE, tmp16);
+	              strlen((char*)src));
+	dst = (char*)utf16_to_enc_r((UTF8*)dst, dstlen, tmp16);
 	return dst;
 }
 
diff --git a/src/unicode.h b/src/unicode.h
index e94b1d5..08918e1 100644
--- a/src/unicode.h
+++ b/src/unicode.h
@@ -72,10 +72,10 @@ extern int enc_to_utf16(UTF16 * dst, unsigned int maxdstlen, const UTF8 * src, u
 extern int enc_to_utf16_be(UTF16 * dst, unsigned int maxdstlen, const UTF8 * src, unsigned int srclen);
 
 /* Thread-safe conversion from codepage to UTF-8 */
-UTF8 * enc_to_utf8_r (char *src, UTF8* dst);
+UTF8 * enc_to_utf8_r (char *src, UTF8* dst, int dstlen);
 
 /* Thread-safe conversion from UTF-8 to codepage */
-char * utf8_to_enc_r (UTF8 *src, char* dst);
+char * utf8_to_enc_r (UTF8 *src, char* dst, int dstlen);
 
 /* Convert back to UTF-8 (for get_key without a saved_plain) */
 extern UTF8 * utf16_to_utf8 (const UTF16* source);
-- 
1.7.4.1

