[Ttssh2-commit] [7483] WideCharToMB()を修正

scmno****@osdn***** scmno****@osdn*****
2019年 3月 15日 (金) 00:38:25 JST


Revision: 7483
          http://sourceforge.jp/projects/ttssh2/scm/svn/commits/7483
Author:   zmatsuo
Date:     2019-03-15 00:38:24 +0900 (Fri, 15 Mar 2019)
Log Message:
-----------
WideCharToMB()を修正

Modified Paths:
--------------
    trunk/teraterm/common/codeconv.cpp
    trunk/teraterm/teraterm/vtterm.c

-------------- next part --------------
Modified: trunk/teraterm/common/codeconv.cpp
===================================================================
--- trunk/teraterm/common/codeconv.cpp	2019-03-14 15:38:11 UTC (rev 7482)
+++ trunk/teraterm/common/codeconv.cpp	2019-03-14 15:38:24 UTC (rev 7483)
@@ -320,7 +320,7 @@
 		} else {
 			goto error;
 		}
-	} else if (0xf0 <= c1 && c1 <= 0xf7 && u8_len >= 4) {
+	} else if (0xf0 <= c1 && c1 <= 0xf7) {
 		// 4byte
 		if (u8_len >= 4) {
 			const uint8_t c2 = *u8_ptr++;
@@ -504,6 +504,12 @@
 size_t UTF32ToUTF16(uint32_t u32, wchar_t *wstr_ptr, size_t wstr_len)
 {
 	size_t u16_out;
+	if (wstr_ptr == NULL) {
+		wstr_len = 2;
+	}
+	if (wstr_len == 0) {
+		return 0;
+	}
 	if (u32 < 0x10000) {
 		if (wstr_len >= 1) {
 			if (wstr_ptr != NULL) {
@@ -542,9 +548,19 @@
 {
 	uint16_t cp932;
 	size_t cp932_out;
-	if (u32 == 0) {
+	if (mb_ptr == NULL) {
+		mb_len = 2;		// 2byte\x82\xA0\x82\xEA\x82Α\xAB\x82\xE8\x82\xE9\x82͂\xB8
+	}
+	if (mb_len == 0) {
+		// \x8Fo\x97͐\xE6\x83T\x83C\x83Y\x82\xAA0
 		return 0;
 	}
+	if (u32 == 0) {
+		if (mb_ptr != NULL) {
+			*mb_ptr = 0;
+		}
+		return 1;
+	}
 	cp932 = UTF32_CP932(u32);
 	if (cp932 == 0) {
 		// \x95ϊ\xB7\x82ł\xAB\x82Ȃ\xA9\x82\xC1\x82\xBD
@@ -616,7 +632,7 @@
  *
  *	@param[in]		*wstr_ptr	wchar_t\x95\xB6\x8E\x9A\x97\xF1
  *	@param[in,out]	*wstr_len	wchar_t\x95\xB6\x8E\x9A\x97\xF1\x92\xB7
- *								NULL\x82܂\xBD\x82\xCD0\x82̂Ƃ\xAB\x8E\xA9\x93\xAE\x81AL'\0'\x82Ń^\x81[\x83~\x83l\x81[\x83g\x82\xB7\x82邱\x82\xC6)
+ *								NULL\x82܂\xBD\x82\xCD0\x82̂Ƃ\xAB\x8E\xA9\x93\xAE(L'\0'\x82Ń^\x81[\x83~\x83l\x81[\x83g\x82\xB7\x82邱\x82\xC6)
  *								NULL\x88ȊO\x82̂Ƃ\xAB\x93\xFC\x97͂\xB5\x82\xBD\x95\xB6\x8E\x9A\x90\x94\x82\xF0\x95Ԃ\xB7
  *	@param[in]		*mb_ptr		\x95ϊ\xB7\x82\xB5\x82\xBD\x95\xB6\x8E\x9A\x97\xF1\x82\xF0\x8E\xFB\x94[\x82\xB7\x82\xE9\x83|\x83C\x83\x93\x83^
  *								(NULL\x82̂Ƃ\xAB\x95ϊ\xB7\x82\xB9\x82\xB8\x82ɕ\xB6\x8E\x9A\x90\x94\x82\xF0\x83J\x83E\x83\x93\x83g\x82\xB7\x82\xE9)
@@ -650,42 +666,24 @@
 	}
 
 	while(mb_len > 0 && wstr_len > 0) {
-		const wchar_t u16 = *wstr_ptr++;
 		size_t mb_out;
-		wstr_len--;
-		wstr_in++;
-		if (u16 != 0) {
-			uint32_t u32 = u16;
-			// \x83T\x83\x8D\x83Q\x81[\x83g high?
-			if (IsHighSurrogate(u16)) {
-				if (wstr_len >= 1) {
-					const wchar_t u16_lo = *wstr_ptr++;
-					wstr_len--;
-					wstr_in++;
-					// \x83T\x83\x8D\x83Q\x81[\x83g low?
-					if (IsLowSurrogate(u16_lo)) {
-						// \x83T\x83\x8D\x83Q\x81[\x83g\x83y\x83A \x83f\x83R\x81[\x83h
-						u32 = 0x10000 + (u16 - 0xd800) * 0x400 + (u16_lo - 0xdc00);
-					} else {
-						goto unknown_code;
-					}
-				} else {
-					goto unknown_code;
-				}
-			}
-			mb_out = UTF32ToMB(u32, mb_ptr, mb_len);
-			if (mb_out == 0) {
-			unknown_code:
-				if (mb_ptr != NULL) {
-					// \x95ϊ\xB7\x82ł\xAB\x82Ȃ\xA9\x82\xC1\x82\xBD\x8Fꍇ
-					*mb_ptr = '?';
-				}
-				mb_out = 1;
-			}
-		} else {
-			// '\0'
+		uint32_t u32;
+		size_t wb_in = UTF16ToUTF32(wstr_ptr, wstr_len, &u32);
+		if (wb_in == 0) {
+			wstr_len -= 1;
+			wstr_in += 1;
+			wstr_ptr++;
+			goto unknown_code;
+		}
+		wstr_len -= wb_in;
+		wstr_in += wb_in;
+		wstr_ptr += wb_in;
+		mb_out = UTF32ToMB(u32, mb_ptr, mb_len);
+		if (mb_out == 0) {
+		unknown_code:
 			if (mb_ptr != NULL) {
-				*mb_ptr = 0;
+				// \x95ϊ\xB7\x82ł\xAB\x82Ȃ\xA9\x82\xC1\x82\xBD\x8Fꍇ
+				*mb_ptr = '?';
 			}
 			mb_out = 1;
 		}

Modified: trunk/teraterm/teraterm/vtterm.c
===================================================================
--- trunk/teraterm/teraterm/vtterm.c	2019-03-14 15:38:11 UTC (rev 7482)
+++ trunk/teraterm/teraterm/vtterm.c	2019-03-14 15:38:24 UTC (rev 7483)
@@ -53,7 +53,6 @@
 #include "ttime.h"
 #include "clipboar.h"
 #include "codeconv.h"
-#include "codeconv.h"
 
 #include "vtterm.h"
 


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