[Ttssh2-commit] [8456] ファイル送信でバイナリオプションが使えるようにした

scmno****@osdn***** scmno****@osdn*****
2020年 1月 8日 (水) 00:44:01 JST


Revision: 8456
          https://osdn.net/projects/ttssh2/scm/svn/commits/8456
Author:   zmatsuo
Date:     2020-01-08 00:44:00 +0900 (Wed, 08 Jan 2020)
Log Message:
-----------
ファイル送信でバイナリオプションが使えるようにした

- [file]/[Send file...]
- ファイルドロップ

Modified Paths:
--------------
    trunk/teraterm/teraterm/clipboar.c
    trunk/teraterm/teraterm/sendmem.cpp
    trunk/teraterm/teraterm/sendmem.h
    trunk/teraterm/teraterm/vtwin.cpp
    trunk/teraterm/ttpmacro/fileread.cpp
    trunk/teraterm/ttpmacro/fileread.h

-------------- next part --------------
Modified: trunk/teraterm/teraterm/clipboar.c
===================================================================
--- trunk/teraterm/teraterm/clipboar.c	2020-01-07 04:34:10 UTC (rev 8455)
+++ trunk/teraterm/teraterm/clipboar.c	2020-01-07 15:44:00 UTC (rev 8456)
@@ -804,7 +804,7 @@
  *	@param	str_w	\x95\xB6\x8E\x9A\x97\xF1\x82ւ̃|\x83C\x83\x93\x83^
  *					malloc()\x82\xB3\x82ꂽ\x83o\x83b\x83t\x83@\x81A\x91\x97\x90M\x8A\xAE\x97\xB9\x8E\x9E\x82Ɏ\xA9\x93\xAE\x82\xC5free()\x82\xB3\x82\xEA\x82\xE9
  *	@param	str_len	\x95\xB6\x8E\x9A\x92\xB7(wchar_t\x92P\x88\xCA)
- *					0\x82̂Ƃ\xAB\x81Astr_w\x82\xA9\x82當\x8E\x9A\x97񒷂𓾂\xE9
+ *					0 \x82̏ꍇ\x82\xCD L'\0' \x82܂\xC5
  */
 static void CBSendStart(wchar_t *str_w, size_t str_len)
 {
@@ -812,9 +812,7 @@
 	if (str_len == 0) {
 		str_len = wcslen(str_w);
 	}
-	sm = SendMemInit(str_w,
-					 str_len * sizeof(wchar_t),
-					 SendMemTypeTextLF);
+	sm = SendMemTextW(str_w, str_len);
 	if (sm == NULL)
 		return;
 	if (ts.PasteDelayPerLine != 0) {

Modified: trunk/teraterm/teraterm/sendmem.cpp
===================================================================
--- trunk/teraterm/teraterm/sendmem.cpp	2020-01-07 04:34:10 UTC (rev 8455)
+++ trunk/teraterm/teraterm/sendmem.cpp	2020-01-07 15:44:00 UTC (rev 8456)
@@ -1,5 +1,5 @@
 /*
- * (C) 2019 TeraTerm Project
+ * (C) 2019-2020 TeraTerm Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -27,9 +27,12 @@
  */
 
 #include <windows.h>
+#include <stdio.h>
 #include <stdlib.h>
 #define _CRTDBG_MAP_ALLOC
 #include <crtdbg.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include "tttypes.h"
 #include "ttcommon.h"
@@ -49,6 +52,12 @@
 
 #include "sendmem.h"
 
+typedef enum {
+	SendMemTypeTextLF,		// wchar_t 0x0a
+	SendMemTypeTextCRLF,	// wchar_t 0x0d + 0x0a
+	SendMemTypeBinary,
+} SendMemType;
+
 // \x91\x97\x90M\x92\x86\x82\xC9VTWIN\x82ɔr\x91\xBC\x82\xF0\x82\xA9\x82\xAF\x82\xE9
 #define	USE_ENABLE_WINDOW	0	// 1=\x94r\x91\xBC\x82\xB7\x82\xE9
 
@@ -408,16 +417,9 @@
 }
 
 /**
- *	\x83\x81\x83\x82\x83\x8A\x82ɂ\xA0\x82\xE9\x83f\x81[\x83^\x82𑗐M\x82\xB7\x82\xE9
- *	\x83f\x81[\x83^\x82͑\x97\x90M\x8FI\x97\xB9\x8C\xE3\x82\xC9free()\x82\xB3\x82\xEA\x82\xE9
- *
- *	@param	ptr		\x83f\x81[\x83^\x82փ|\x83C\x83\x93\x83^(malloc()\x82\xB3\x82ꂽ\x97̈\xE6)
- *					\x91\x97\x90M\x8C\xE3(\x92\x86\x92f\x8C\xE3)\x81A\x8E\xA9\x93\xAE\x93I\x82\xC9free()\x82\xB3\x82\xEA\x82\xE9
- *	@param	len		\x83f\x81[\x83^\x92\xB7(byte)
- *					\x95\xB6\x8E\x9A\x97\xF1(wchar_t)\x82̏ꍇ\x82\xE0byte\x90\x94
- *	@param	type	\x95\xB6\x8E\x9A\x97\xF1(wchar_t,LF or CRLF or \x83o\x83C\x83i\x83\x8A)
+ *	\x8F\x89\x8A\xFA\x89\xBB
  */
-SendMem *SendMemInit(void *ptr, size_t len, SendMemType type)
+static SendMem *SendMemInit_()
 {
 	if (sendmem_work != NULL) {
 		// \x91\x97\x90M\x92\x86
@@ -427,20 +429,11 @@
 	if (p == NULL) {
 		return NULL;
 	}
-	if (type == SendMemTypeTextCRLF || type == SendMemTypeTextLF) {
-		// \x89\xFC\x8Ds\x83R\x81[\x83h\x82𒲐\xAE\x82\xB5\x82Ă\xA8\x82\xAD
-		size_t new_len = len / sizeof(wchar_t);
-		p->send_ptr = (BYTE *)NormalizeLineBreak((wchar_t *)ptr, &new_len);
-		p->send_len = new_len * sizeof(wchar_t);
-		free(ptr);
-	} else {
-		p->send_ptr = (BYTE *)ptr;
-		p->send_len = len;
-	}
-	if (p->send_ptr == NULL) {
-		return NULL;
-	}
-	p->type = type;
+
+	p->send_ptr = NULL;
+	p->send_len = 0;
+
+	p->type = SendMemTypeBinary;
 	p->local_echo_enable = FALSE;
 	p->delay_per_char = 0;  // (ms)
 	p->delay_per_line = 0;  // (ms)
@@ -451,6 +444,56 @@
 	return p;
 }
 
+/**
+ *	\x83\x81\x83\x82\x83\x8A\x82ɂ\xA0\x82\xE9\x83e\x83L\x83X\x83g\x82𑗐M\x82\xB7\x82\xE9
+ *	\x83f\x81[\x83^\x82͑\x97\x90M\x8FI\x97\xB9\x8C\xE3\x82\xC9free()\x82\xB3\x82\xEA\x82\xE9
+ *
+ *	@param	ptr		\x83f\x81[\x83^\x82փ|\x83C\x83\x93\x83^(malloc()\x82\xB3\x82ꂽ\x97̈\xE6)
+ *					\x91\x97\x90M\x8C\xE3(\x92\x86\x92f\x8C\xE3)\x81A\x8E\xA9\x93\xAE\x93I\x82\xC9free()\x82\xB3\x82\xEA\x82\xE9
+ *	@param	len		\x95\xB6\x8E\x9A\x97\xF1\x92\xB7(wchar_t\x92P\x88\xCA)
+ *					0 \x82̏ꍇ\x82\xCD L'\0' \x82܂\xC5
+ */
+SendMem *SendMemTextW(wchar_t *str, size_t len)
+{
+	SendMem *p = SendMemInit_();
+	if (p == NULL) {
+		return NULL;
+	}
+
+	if (len == 0) {
+		len = wcslen(str);
+	}
+
+	// \x89\xFC\x8Ds\x83R\x81[\x83h\x82𒲐\xAE\x82\xB5\x82Ă\xA8\x82\xAD
+	size_t new_len = len;
+	p->send_ptr = (BYTE *)NormalizeLineBreak((wchar_t *)str, &new_len);
+	p->send_len = new_len * sizeof(wchar_t);
+	free(str);
+	p->type = SendMemTypeTextLF;
+	return p;
+}
+
+/**
+ *	\x83\x81\x83\x82\x83\x8A\x82ɂ\xA0\x82\xE9\x83f\x81[\x83^\x82𑗐M\x82\xB7\x82\xE9
+ *	\x83f\x81[\x83^\x82͑\x97\x90M\x8FI\x97\xB9\x8C\xE3\x82\xC9free()\x82\xB3\x82\xEA\x82\xE9
+ *
+ *	@param	ptr		\x83f\x81[\x83^\x82փ|\x83C\x83\x93\x83^(malloc()\x82\xB3\x82ꂽ\x97̈\xE6)
+ *					\x91\x97\x90M\x8C\xE3(\x92\x86\x92f\x8C\xE3)\x81A\x8E\xA9\x93\xAE\x93I\x82\xC9free()\x82\xB3\x82\xEA\x82\xE9
+ *	@param	len		\x83f\x81[\x83^\x92\xB7(byte)
+ */
+SendMem *SendMemBinary(void *ptr, size_t len)
+{
+	SendMem *p = SendMemInit_();
+	if (p == NULL) {
+		return NULL;
+	}
+
+	p->send_ptr = (BYTE *)ptr;
+	p->send_len = len;
+	p->type = SendMemTypeBinary;
+	return p;
+}
+
 void SendMemInitEcho(SendMem *sm, BOOL echo)
 {
 	sm->local_echo_enable = echo;
@@ -529,18 +572,25 @@
 	return TRUE;
 }
 #else
-BOOL SendMemSendFile(const wchar_t *filename, BOOL binary)	// binary\x96\xA2\x91Ή\x9E
+BOOL SendMemSendFile(const wchar_t *filename, BOOL binary)
 {
-	binary = FALSE;
-
-	size_t str_len;
-	wchar_t *str_ptr = LoadFileWW(filename, &str_len);
-	if (str_ptr == NULL) {
-		return FALSE;
+	SendMem *sm;
+	if (!binary) {
+		size_t str_len;
+		wchar_t *str_ptr = LoadFileWW(filename, &str_len);
+		if (str_ptr == NULL) {
+			return FALSE;
+		}
+		sm = SendMemTextW(str_ptr, str_len);
 	}
-	str_len *= sizeof(wchar_t);
-
-	SendMem *sm = SendMemInit(str_ptr, str_len, SendMemTypeTextLF);
+	else {
+		size_t data_len;
+		unsigned char *data_ptr = LoadFileBinary(filename, &data_len);
+		if (data_ptr == NULL) {
+			return FALSE;
+		}
+		sm = SendMemBinary(data_ptr, data_len);
+	}
 	SendMemInitDialog(sm, hInst, HVTWin, ts.UILanguageFile);
 	SendMemInitDialogCaption(sm, L"send file");			// title
 	SendMemInitDialogFilename(sm, filename);
@@ -559,9 +609,9 @@
 BOOL SendMemPasteString(wchar_t *str)
 {
 	const size_t len = wcslen(str);
-	CommTextOutW(&cv, str, len);
+	CommTextOutW(&cv, str, (int)len);
 	if (ts.LocalEcho > 0) {
-		CommTextEchoW(&cv, str, len);
+		CommTextEchoW(&cv, str, (int)len);
 	}
 
 	free(str);

Modified: trunk/teraterm/teraterm/sendmem.h
===================================================================
--- trunk/teraterm/teraterm/sendmem.h	2020-01-07 04:34:10 UTC (rev 8455)
+++ trunk/teraterm/teraterm/sendmem.h	2020-01-07 15:44:00 UTC (rev 8456)
@@ -1,5 +1,5 @@
 /*
- * (C) 2019 TeraTerm Project
+ * (C) 2019-2020 TeraTerm Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -33,15 +33,10 @@
 extern "C" {
 #endif
 
-typedef enum {
-	SendMemTypeTextLF,		// wchar_t 0x0a
-	SendMemTypeTextCRLF,	// wchar_t 0x0d + 0x0a
-	SendMemTypeBinary,
-} SendMemType;
-
 typedef struct SendMemTag SendMem;
 
-SendMem *SendMemInit(void *ptr, size_t len, SendMemType type);
+SendMem *SendMemTextW(wchar_t *ptr, size_t len);
+SendMem *SendMemBinary(void *ptr, size_t len);
 void SendMemInitEcho(SendMem *sm, BOOL echo);
 void SendMemInitDelay(SendMem *sm, DWORD per_line, DWORD per_char);
 void SendMemInitDialog(SendMem *sm, HINSTANCE hInstance, HWND hWndParent, const char *UILanguageFile);

Modified: trunk/teraterm/teraterm/vtwin.cpp
===================================================================
--- trunk/teraterm/teraterm/vtwin.cpp	2020-01-07 04:34:10 UTC (rev 8455)
+++ trunk/teraterm/teraterm/vtwin.cpp	2020-01-07 15:44:00 UTC (rev 8456)
@@ -4359,7 +4359,6 @@
 			::DragAcceptFiles(hDlgWnd, TRUE);
 			SetDlgTexts(hDlgWnd, TextInfos, _countof(TextInfos), data->UILanguageFile);
 			CenterWindow(hDlgWnd, GetParent(hDlgWnd));
-			EnableWindow(GetDlgItem(hDlgWnd, IDC_SENDFILE_CHECK_BINARY), FALSE);
 			return TRUE;
 
 		case WM_COMMAND:
@@ -4741,12 +4740,10 @@
 // (2008.5.12 maya) changed to PropertySheet
 void CVTWindow::OnExternalSetup()
 {
-	DWORD ret;
-
 	SetDialogFont(ts.DialogFontName, ts.DialogFontPoint, ts.DialogFontCharSet,
 				  ts.UILanguageFile, "Tera Term", "DLG_TAHOMA_FONT");
 	CAddSettingPropSheetDlg CAddSetting(m_hInst, HVTWin);
-	ret = CAddSetting.DoModal();
+	INT_PTR ret = CAddSetting.DoModal();
 	switch (ret) {
 		case (DWORD)-1:
 		case IDABORT:

Modified: trunk/teraterm/ttpmacro/fileread.cpp
===================================================================
--- trunk/teraterm/ttpmacro/fileread.cpp	2020-01-07 04:34:10 UTC (rev 8455)
+++ trunk/teraterm/ttpmacro/fileread.cpp	2020-01-07 15:44:00 UTC (rev 8456)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2018-2019 TeraTerm Project
+ * Copyright (C) 2018-2020 TeraTerm Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -45,11 +45,13 @@
 
 /**
  *	\x83t\x83@\x83C\x83\x8B\x82\xF0\x83\x81\x83\x82\x83\x8A\x82ɓǂݍ\x9E\x82\xDE
- *	@param[out]	*_len	\x83T\x83C\x83Y(\x8DŌ\xE3\x82ɕt\x89\xC1\x82\xB3\x82\xEA\x82\xE9"\0\0"\x82\xF0\x8A܂\xDE)
+ *	@param[out]	*_len		\x83T\x83C\x83Y(terminater\x8A܂\xDE)
+ *	@param[in]	terminate	TRUE	\x8DŌ\xE3\x82\xC9 L'\0' ("\0\0")\x82\xF0\x95t\x89\xC1
+ *							FALSE	\x83t\x83@\x83C\x83\x8B\x82\xF0\x82\xBB\x82̂܂ܓǂݍ\x9E\x82\xDE
  *	@retval		\x83t\x83@\x83C\x83\x8B\x82̒\x86\x90g\x82ւ̃|\x83C\x83\x93\x83^(\x8Eg\x97p\x8C\xE3free()\x82\xB7\x82邱\x82\xC6)
- *				NULL=\x83G\x83\x89\x81[
+ *				NULL=\x83G\x83\x89\x81[ (fclose()\x82\xB7\x82邱\x82\xC6)
  */
-static void *LoadRawFile(FILE *fp, size_t *_len)
+static void *LoadRawFile(FILE *fp, size_t *_len, BOOL terminate)
 {
     fseek(fp, 0L, SEEK_END);
 	fpos_t pos;
@@ -56,17 +58,56 @@
 	fgetpos(fp, &pos);
     fseek(fp, 0L, SEEK_SET);
 	size_t len = (size_t)pos;
-	char *buf = (char *)malloc(len + 2);
-	buf[len] = 0;
-	buf[len+1] = 0;		// UTF-16\x91΍\xF4
-	fread(buf, 1, len, fp);
-	len += 2;
-	*_len = len;
+	size_t alloc_len = terminate ? len + 2 : len;
+	char *buf = (char *)malloc(alloc_len);
+	if (buf == NULL) {
+		return NULL;
+	}
+	size_t rlen = fread(buf, 1, len, fp);
+	if (rlen != len) {
+		free(buf);
+		return NULL;
+	}
+	if (terminate) {
+		buf[len] = 0;
+		buf[len+1] = 0;		// UTF-16\x91΍\xF4
+	}
+	*_len = alloc_len;
 	return buf;
 }
 
 /**
  *	\x83t\x83@\x83C\x83\x8B\x82\xF0\x83\x81\x83\x82\x83\x8A\x82ɓǂݍ\x9E\x82\xDE
+ *	@param[out]	*_len	\x83T\x83C\x83Y(\x8DŌ\xE3\x82ɕt\x89\xC1\x82\xB3\x82\xEA\x82\xE9"\0\0"\x82\xF0\x8A܂\xDE)
+ *	@retval		\x83t\x83@\x83C\x83\x8B\x82̒\x86\x90g\x82ւ̃|\x83C\x83\x93\x83^(\x8Eg\x97p\x8C\xE3free()\x82\xB7\x82邱\x82\xC6)
+ *				NULL=\x83G\x83\x89\x81[
+ */
+static void *LoadRawFile(FILE *fp, size_t *_len)
+{
+	return LoadRawFile(fp, _len, TRUE);
+}
+
+/**
+ *	\x83t\x83@\x83C\x83\x8B\x82\xF0\x83\x81\x83\x82\x83\x8A\x82ɓǂݍ\x9E\x82\xDE
+ *	\x89\xC1\x8DH\x82͍s\x82\xED\x82Ȃ\xA2
+ *	@param[out]	*_len	\x83T\x83C\x83Y
+ *	@retval		\x83t\x83@\x83C\x83\x8B\x82̒\x86\x90g\x82ւ̃|\x83C\x83\x93\x83^(\x8Eg\x97p\x8C\xE3free()\x82\xB7\x82邱\x82\xC6)
+ *				NULL=\x83G\x83\x89\x81[
+ */
+uint8_t *LoadFileBinary(const wchar_t *FileName, size_t *_len)
+{
+	FILE *fp;
+	_wfopen_s(&fp, FileName, L"rb");
+	if (fp == NULL) {
+		return NULL;
+	}
+	uint8_t *ptr = (uint8_t *)LoadRawFile(fp, _len, FALSE);
+	fclose(fp);
+	return ptr;
+}
+
+/**
+ *	\x83t\x83@\x83C\x83\x8B\x82\xF0\x83\x81\x83\x82\x83\x8A\x82ɓǂݍ\x9E\x82\xDE
  *	\x92\x86\x90g\x82\xCDUTF-8\x82ɕϊ\xB7\x82\xB3\x82\xEA\x82\xE9
  *	\x83t\x83@\x83C\x83\x8B\x82̍Ō\xE3\x82\xCD '\0'\x82Ń^\x81[\x83~\x83l\x81[\x83g\x82\xB3\x82\xEA\x82Ă\xA2\x82\xE9
  *

Modified: trunk/teraterm/ttpmacro/fileread.h
===================================================================
--- trunk/teraterm/ttpmacro/fileread.h	2020-01-07 04:34:10 UTC (rev 8455)
+++ trunk/teraterm/ttpmacro/fileread.h	2020-01-07 15:44:00 UTC (rev 8456)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2018-2019 TeraTerm Project
+ * Copyright (C) 2018-2020 TeraTerm Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -35,6 +35,7 @@
 wchar_t *LoadFileWA(const char *FileName, size_t *_len);
 char *LoadFileAA(const char *FileName, size_t *_len);
 wchar_t *LoadFileWW(const wchar_t *FileName, size_t *_len);
+unsigned char *LoadFileBinary(const wchar_t *FileName, size_t *_len);
 
 #ifdef __cplusplus
 }


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