[Ttssh2-commit] [6621] base64 decode を ISC 由来のコードから自前実装に変更 [Ttssh2-devel 3043]

svnno****@sourc***** svnno****@sourc*****
2017年 3月 9日 (木) 16:55:29 JST


Revision: 6621
          http://sourceforge.jp/projects/ttssh2/scm/svn/commits/6621
Author:   doda
Date:     2017-03-09 16:55:28 +0900 (Thu, 09 Mar 2017)
Log Message:
-----------
base64 decode を ISC 由来のコードから自前実装に変更 [Ttssh2-devel 3043]

Modified Paths:
--------------
    trunk/teraterm/ttpcmn/ttpcmn.def
    trunk/ttssh2/ttxssh/hosts.c
    trunk/ttssh2/ttxssh/hosts.h
    trunk/ttssh2/ttxssh/keyfiles.c

-------------- next part --------------
Modified: trunk/teraterm/ttpcmn/ttpcmn.def
===================================================================
--- trunk/teraterm/ttpcmn/ttpcmn.def	2017-03-07 08:57:41 UTC (rev 6620)
+++ trunk/teraterm/ttpcmn/ttpcmn.def	2017-03-09 07:55:28 UTC (rev 6621)
@@ -28,6 +28,8 @@
   GetDefaultFName @47
   ExtractFileName @48
   replaceInvalidFileNameChar @74
+  b64encode @75
+  b64decode @76
   
   SJIS2JIS @30
   SJIS2EUC @31

Modified: trunk/ttssh2/ttxssh/hosts.c
===================================================================
--- trunk/ttssh2/ttxssh/hosts.c	2017-03-07 08:57:41 UTC (rev 6620)
+++ trunk/ttssh2/ttxssh/hosts.c	2017-03-09 07:55:28 UTC (rev 6621)
@@ -264,128 +264,8 @@
 	return index;
 }
 
-// 
-// BASE64\x83f\x83R\x81[\x83h\x8F\x88\x97\x9D\x82\xF0\x8Ds\x82\xA4\x81B(rfc1521)
-// src\x83o\x83b\x83t\x83@\x82\xCD null-terminate \x82\xB5\x82Ă\xA2\x82\xE9\x95K\x97v\x82\xA0\x82\xE8\x81B
-//
-int uudecode(unsigned char *src, int srclen, unsigned char *target, int targsize)
-{
-	char pad = '=';
-	int tarindex, state, ch;
-	char *pos;
-
-	state = 0;
-	tarindex = 0;
-
-	while ((ch = *src++) != '\0') {
-		if (isspace(ch))	/* Skip whitespace anywhere. */
-			continue;
-
-		if (ch == pad)
-			break;
-
-		pos = strchr(base64, ch);
-		if (pos == 0) 		/* A non-base64 character. */
-			return (-1);
-
-		switch (state) {
-		case 0:
-			if (target) {
-				if (tarindex >= targsize)
-					return (-1);
-				target[tarindex] = (pos - base64) << 2;
-			}
-			state = 1;
-			break;
-		case 1:
-			if (target) {
-				if (tarindex + 1 >= targsize)
-					return (-1);
-				target[tarindex]   |=  (pos - base64) >> 4;
-				target[tarindex+1]  = ((pos - base64) & 0x0f) << 4 ;
-			}
-			tarindex++;
-			state = 2;
-			break;
-		case 2:
-			if (target) {
-				if (tarindex + 1 >= targsize)
-					return (-1);
-				target[tarindex]   |=  (pos - base64) >> 2;
-				target[tarindex+1]  = ((pos - base64) & 0x03) << 6;
-			}
-			tarindex++;
-			state = 3;
-			break;
-		case 3:
-			if (target) {
-				if (tarindex >= targsize)
-					return (-1);
-				target[tarindex] |= (pos - base64);
-			}
-			tarindex++;
-			state = 0;
-			break;
-		}
-	}
-
-	/*
-	 * We are done decoding Base-64 chars.  Let's see if we ended
-	 * on a byte boundary, and/or with erroneous trailing characters.
-	 */
-
-	if (ch == pad) {		/* We got a pad char. */
-		ch = *src++;		/* Skip it, get next. */
-		switch (state) {
-		case 0:		/* Invalid = in first position */
-		case 1:		/* Invalid = in second position */
-			return (-1);
-
-		case 2:		/* Valid, means one byte of info */
-			/* Skip any number of spaces. */
-			for (; ch != '\0'; ch = *src++)
-				if (!isspace(ch))
-					break;
-			/* Make sure there is another trailing = sign. */
-			if (ch != pad)
-				return (-1);
-			ch = *src++;		/* Skip the = */
-			/* Fall through to "single trailing =" case. */
-			/* FALLTHROUGH */
-
-		case 3:		/* Valid, means two bytes of info */
-			/*
-			 * We know this char is an =.  Is there anything but
-			 * whitespace after it?
-			 */
-			for (; ch != '\0'; ch = *src++)
-				if (!isspace(ch))
-					return (-1);
-
-			/*
-			 * Now make sure for cases 2 and 3 that the "extra"
-			 * bits that slopped past the last full byte were
-			 * zeros.  If we don't check them, they become a
-			 * subliminal channel.
-			 */
-			if (target && target[tarindex] != 0)
-				return (-1);
-		}
-	} else {
-		/*
-		 * We ended by seeing the end of the string.  Make sure we
-		 * have no partial bytes lying around.
-		 */
-		if (state != 0)
-			return (-1);
-	}
-
-	return (tarindex);
-}
-
-
 // SSH2\x8C\xAE\x82\xCD BASE64 \x8C`\x8E\xAE\x82Ŋi\x94[\x82\xB3\x82\xEA\x82Ă\xA2\x82\xE9
-static Key *parse_uudecode(char *data)
+static Key *parse_base64data(char *data)
 {
 	int count;
 	unsigned char *blob = NULL;
@@ -403,7 +283,7 @@
 	// BASE64\x83f\x83R\x81[\x83h
 	ch = data[count];
 	data[count] = '\0';  // \x82\xB1\x82\xB1\x82͉\xFC\x8Ds\x83R\x81[\x83h\x82̂͂\xB8\x82Ȃ̂ŏ\x91\x82\xAB\x92ׂ\xB5\x82Ă\xE0\x96\xE2\x91\xE8\x82Ȃ\xA2\x82͂\xB8
-	n = uudecode(data, count, blob, len);
+	n = b64decode(blob, len, data);
 	data[count] = ch;
 	if (n < 0) {
 		goto error;
@@ -593,8 +473,8 @@
 
 			index += eat_spaces(data + index);  // update index
 
-			// uudecode
-			key2 = parse_uudecode(data + index);
+			// base64 decode
+			key2 = parse_base64data(data + index);
 			if (key2 == NULL) {
 				return index + eat_to_end_of_line(data + index);
 			}
@@ -871,8 +751,8 @@
 
 			index += eat_spaces(data + index);  // update index
 
-			// uudecode
-			key = parse_uudecode(data + index);
+			// base64 decode
+			key = parse_base64data(data + index);
 			if (key == NULL) {
 				return index + eat_to_end_of_line(data + index);
 			}

Modified: trunk/ttssh2/ttxssh/hosts.h
===================================================================
--- trunk/ttssh2/ttxssh/hosts.h	2017-03-07 08:57:41 UTC (rev 6620)
+++ trunk/ttssh2/ttxssh/hosts.h	2017-03-09 07:55:28 UTC (rev 6621)
@@ -71,8 +71,6 @@
 void HOSTS_notify_disconnecting(PTInstVar pvar);
 void HOSTS_end(PTInstVar pvar);
 
-int uudecode(unsigned char *src, int srclen, unsigned char *target, int targsize);
-
 int HOSTS_compare_public_key(Key *src, Key *key);
 int HOSTS_hostkey_foreach(PTInstVar pvar, hostkeys_foreach_fn *callback, void *ctx);
 void HOSTS_add_host_key(PTInstVar pvar, Key *key);

Modified: trunk/ttssh2/ttxssh/keyfiles.c
===================================================================
--- trunk/ttssh2/ttxssh/keyfiles.c	2017-03-07 08:57:41 UTC (rev 6620)
+++ trunk/ttssh2/ttxssh/keyfiles.c	2017-03-09 07:55:28 UTC (rev 6621)
@@ -393,7 +393,7 @@
 			break;
 	}
 
-	/* uudecode */
+	/* base64 decode */
 	m1len = sizeof(MARK_BEGIN) - 1;
 	m2len = sizeof(MARK_END) - 1;
 	cp = buffer_ptr(blob);
@@ -422,18 +422,18 @@
 		goto error;
 	}
 
-	// \x83t\x83@\x83C\x83\x8B\x82̃X\x83L\x83\x83\x83\x93\x82\xAA\x8FI\x82\xED\x82\xC1\x82\xBD\x82̂ŁAuudecode\x82\xB7\x82\xE9\x81B
+	// \x83t\x83@\x83C\x83\x8B\x82̃X\x83L\x83\x83\x83\x93\x82\xAA\x8FI\x82\xED\x82\xC1\x82\xBD\x82̂ŁAbase64 decode\x82\xB7\x82\xE9\x81B
 	len = buffer_len(encoded);
 	if ((cp = buffer_append_space(copy_consumed, len)) == NULL) {
 		//error("%s: buffer_append_space", __func__);
 		goto error;
 	}
-	if ((dlen = uudecode(buffer_ptr(encoded), buffer_len(encoded), cp, len)) < 0) {
-		//error("%s: uudecode failed", __func__);
+	if ((dlen = b64decode(cp, len, buffer_ptr(encoded))) < 0) {
+		//error("%s: base64 decode failed", __func__);
 		goto error;
 	}
 	if ((unsigned int)dlen > len) {
-		//error("%s: crazy uudecode length %d > %u", __func__, dlen, len);
+		//error("%s: crazy base64 length %d > %u", __func__, dlen, len);
 		goto error;
 	}
 



Ttssh2-commit メーリングリストの案内