• R/O
  • SSH
  • HTTPS

ttssh2: Commit


Commit MetaInfo

Revision9547 (tree)
Time2021-11-27 22:49:15
Authorzmatsuo

Log Message

セッションの複製が動作しなかったので修正

- セッション複製部分を別ファイルに分離

- ttcmn_dup.cpp, h

- プロジェクトファイル修正

Change Summary

Incremental Difference

--- trunk/teraterm/ttpcmn/CMakeLists.txt (revision 9546)
+++ trunk/teraterm/ttpcmn/CMakeLists.txt (revision 9547)
@@ -8,8 +8,11 @@
88 language.c
99 language.h
1010 ttcmn.c
11+ ttcmn_cominfo.c
12+ ttcmn_cominfo.h
13+ ttcmn_dup.cpp
14+ ttcmn_dup.h
1115 ttcmn_notify.cpp
12- ttcmn_cominfo.c
1316 ttpcmn-version.rc
1417 ttpcmn.def
1518 ${COMMON_SRC}
--- trunk/teraterm/ttpcmn/ttcmn.c (revision 9546)
+++ trunk/teraterm/ttpcmn/ttcmn.c (revision 9547)
@@ -49,6 +49,8 @@
4949 #include "compat_win.h"
5050 #include "asprintf.h"
5151
52+#include "ttcmn_dup.h"
53+
5254 #define DllExport __declspec(dllexport)
5355 #include "ttcommon.h"
5456
@@ -56,8 +58,6 @@
5658 typedef struct {
5759 size_t size_tmap; /* sizeof TMap */
5860 size_t size_tttset; /* sizeof TTTSet */
59- /* Setup information from "teraterm.ini" */
60- TTTSet ts;
6161 // Window list
6262 int NWin;
6363 HWND WinList[MAXNWIN];
@@ -71,6 +71,9 @@
7171 WINDOWPLACEMENT WinPrevRect[MAXNWIN];
7272 BOOL WinUndoFlag;
7373 int WinUndoStyle;
74+ // Duplicate Teraterm data
75+ HANDLE DuplicateDataHandle;
76+ DWORD DuplicateDataSizeLow;
7477 } TMap;
7578 typedef TMap *PMap;
7679
@@ -96,17 +99,38 @@
9699 WIN_SIDEBYSIDE,
97100 };
98101
102+static const char DupDataName[] = "dupdata";
99103
100104 void WINAPI CopyShmemToTTSet(PTTSet ts)
101105 {
102- // 現在の設定を共有メモリからコピーする
103- memcpy(ts, &pm->ts, sizeof(TTTSet));
106+ DWORD size = pm->DuplicateDataSizeLow;
107+ HANDLE handle = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, size, DupDataName);
108+ void *ptr = (void *)MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
109+ TTCMNUnserialize(ptr, (size_t)size, ts);
110+ UnmapViewOfFile(ptr);
111+ CloseHandle(handle);
112+ pm->DuplicateDataSizeLow = 0;
104113 }
105114
106115 void WINAPI CopyTTSetToShmem(PTTSet ts)
107116 {
108- // 現在の設定を共有メモリへコピーする
109- memcpy(&pm->ts, ts, sizeof(TTTSet));
117+ size_t size;
118+ void *ptr = TTCMNSerialize(ts, &size);
119+ if (ptr != NULL) {
120+ HANDLE handle = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, (DWORD)size, DupDataName);
121+ void *dest = (void *)MapViewOfFile(handle, FILE_MAP_ALL_ACCESS,0,0,0);
122+ memcpy(dest, ptr, size);
123+ UnmapViewOfFile(dest);
124+ //CloseHandle(handle); do not close, 使用しない状態になるとなくなってしまう
125+
126+ if (pm->DuplicateDataHandle != NULL) {
127+ // 1回前の情報を削除する
128+ CloseHandle(pm->DuplicateDataHandle);
129+ }
130+ pm->DuplicateDataHandle = handle;
131+ pm->DuplicateDataSizeLow = (DWORD)size;
132+ free(ptr);
133+ }
110134 }
111135
112136 static void CopyFiles(const wchar_t *file_list[], const wchar_t *src_dir, const wchar_t *dest_dir)
@@ -146,14 +170,12 @@
146170 }
147171 else {
148172 /* only the first instance uses saved position */
149- pm->ts.VTPos.x = CW_USEDEFAULT;
150- pm->ts.VTPos.y = CW_USEDEFAULT;
151- pm->ts.TEKPos.x = CW_USEDEFAULT;
152- pm->ts.TEKPos.y = CW_USEDEFAULT;
173+ ts->VTPos.x = CW_USEDEFAULT;
174+ ts->VTPos.y = CW_USEDEFAULT;
175+ ts->TEKPos.x = CW_USEDEFAULT;
176+ ts->TEKPos.y = CW_USEDEFAULT;
153177 }
154178
155- memcpy(ts,&(pm->ts),sizeof(TTTSet));
156-
157179 // if (FirstInstance) { の部分から移動 (2008.3.13 maya)
158180 // 起動時には、共有メモリの HomeDir と SetupFName は空になる
159181 /* Get home directory (ttermpro.exeのフォルダ) */
--- trunk/teraterm/ttpcmn/ttcmn_dup.cpp (nonexistent)
+++ trunk/teraterm/ttpcmn/ttcmn_dup.cpp (revision 9547)
@@ -0,0 +1,280 @@
1+/*
2+ * Copyright (C) 1994-1998 T. Teranishi
3+ * (C) 2021- TeraTerm Project
4+ * All rights reserved.
5+ *
6+ * Redistribution and use in source and binary forms, with or without
7+ * modification, are permitted provided that the following conditions
8+ * are met:
9+ *
10+ * 1. Redistributions of source code must retain the above copyright
11+ * notice, this list of conditions and the following disclaimer.
12+ * 2. Redistributions in binary form must reproduce the above copyright
13+ * notice, this list of conditions and the following disclaimer in the
14+ * documentation and/or other materials provided with the distribution.
15+ * 3. The name of the author may not be used to endorse or promote products
16+ * derived from this software without specific prior written permission.
17+ *
18+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+ */
29+
30+#include <direct.h>
31+#include <string.h>
32+#include <stdio.h>
33+#include <windows.h>
34+#include <assert.h>
35+#include <crtdbg.h>
36+#include <wchar.h>
37+#if (defined(_MSC_VER) && (_MSC_VER >= 1600)) || !defined(_MSC_VER)
38+#include <stdint.h>
39+#endif
40+
41+#include "teraterm.h"
42+#include "tttypes.h"
43+#include "ttlib.h"
44+#include "tt_res.h"
45+#include "codeconv.h"
46+#include "compat_win.h"
47+#include "asprintf.h"
48+#include "ttcommon.h"
49+
50+#include "ttcmn_dup.h"
51+
52+#if defined(_MSC_VER) && (_MSC_VER < 1600)
53+typedef unsigned char uint8_t;
54+#endif
55+
56+/**
57+ * データシリアライズ情報構造体
58+ */
59+typedef struct {
60+ int size; // データのサイズ(0で終了)
61+ int offset; // 先頭からのオフセット
62+ enum {
63+ COPY = 0, // データをコピーする
64+ MALLOCED_WSTRING = 1, // mallocした領域の
65+ } type;
66+} TSerializeInfo;
67+
68+/**
69+ * データをシリアライズする
70+ *
71+ * @param data シリアライズするデータ
72+ * @param info シリアライズ情報
73+ * @param[out] size_ シリアライズしたblobサイズ(byte)
74+ * @return シリアライズしたblobデータへのポインタ
75+ * 不要になったらfree()すること
76+ */
77+static uint8_t *SerializeData(const void *data, const TSerializeInfo *info, size_t *size_)
78+{
79+ const unsigned char *src = (unsigned char *)data;
80+ uint8_t *blob = NULL;
81+ size_t size = 0;
82+ while (info->size != 0) {
83+ switch (info->type) {
84+ default:
85+ assert(FALSE);
86+ // fall torught
87+ case TSerializeInfo::COPY: {
88+ const size_t l = info->size;
89+ unsigned char *d;
90+ const unsigned char *s = (unsigned char *)src + info->offset;
91+ blob = (uint8_t *)realloc(blob, size + l);
92+ d = blob + size;
93+ memcpy(d, s, l);
94+ size += l;
95+ break;
96+ }
97+ case TSerializeInfo::MALLOCED_WSTRING: {
98+ wchar_t **s = (wchar_t **)((unsigned char *)src + info->offset);
99+ const wchar_t *src_str = *s;
100+ size_t str_len = 0;
101+ if (src_str != NULL) {
102+ str_len = wcslen(src_str) + 1;
103+ }
104+ const size_t str_len_byte = str_len * sizeof(wchar_t);
105+ const size_t alloc_len = sizeof(size_t) + str_len_byte;
106+ blob = (uint8_t *)realloc(blob, size + alloc_len);
107+ uint8_t *d = blob + size;
108+ *(size_t *)d = str_len;
109+ d += sizeof(size_t);
110+ wmemcpy((wchar_t *)d, src_str, str_len);
111+ size += alloc_len;
112+ break;
113+ }
114+ }
115+ info++;
116+ }
117+ *size_ = size;
118+ return blob;
119+}
120+
121+/**
122+ * アンシリアライズする
123+ *
124+ * @param blob シリアライズされたデータへのポインタ
125+ * @param size シリアライズされたデータのサイズ
126+ * @param info シリアライズ情報
127+ * @param[out] data 書き戻すデータ
128+ */
129+static void UnserializeData(const void *blob, size_t size, const TSerializeInfo *info,
130+ void *data)
131+{
132+ const unsigned char *src = (const unsigned char *)blob;
133+ const unsigned char *end = src + size;
134+ while (info->size != 0) {
135+ switch (info->type) {
136+ default:
137+ assert(FALSE);
138+ break;
139+ case TSerializeInfo::COPY: {
140+ unsigned char *d = (unsigned char *)data + info->offset;
141+ const size_t l = info->size;
142+ memcpy(d, src, l);
143+ src += l;
144+ break;
145+ }
146+ case TSerializeInfo::MALLOCED_WSTRING: {
147+ wchar_t **d = (wchar_t **)((unsigned char *)data + info->offset);
148+ assert(info->size == sizeof(wchar_t *));
149+ const size_t l = *(size_t *)src;
150+ src += sizeof(size_t);
151+ if (l == 0) {
152+ *d = NULL;
153+ }
154+ else {
155+ const size_t byte_len = l * sizeof(wchar_t);
156+ wchar_t *str = (wchar_t *)malloc(byte_len);
157+ wmemcpy(str, (wchar_t *)src, l);
158+ *d = str;
159+ src += byte_len;
160+ }
161+ break;
162+ }
163+ };
164+ if (src > end) {
165+ // データがおかしい?
166+ assert(FALSE);
167+ break;
168+ }
169+ info++;
170+ }
171+}
172+
173+static const uint8_t signature[] = {
174+ 'T', 'E', 'R', 'A', 'T' , 'E', 'R', 'M',
175+ TT_VERSION_MAJOR,
176+ TT_VERSION_MINOR,
177+ sizeof(void *), // 4 or 8 = 32bit or 64bit
178+ 0, // reserver
179+ (uint8_t)((sizeof(TTTSet) >> (8 * 0)) & 0xff),
180+ (uint8_t)((sizeof(TTTSet) >> (8 * 1)) & 0xff),
181+ (uint8_t)((sizeof(TTTSet) >> (8 * 2)) & 0xff),
182+ (uint8_t)((sizeof(TTTSet) >> (8 * 3)) & 0xff),
183+};
184+
185+/**
186+ * @return 0以外 スキップするサイズ
187+ * 0 error(異なった)
188+ */
189+static uint8_t *MakeSignature(size_t *size)
190+{
191+ uint8_t *p = (uint8_t *)malloc(sizeof(signature));
192+ memcpy(p, signature, sizeof(signature));
193+ *size = sizeof(signature);
194+ return p;
195+}
196+
197+/**
198+ * @return 0以外 スキップするサイズ
199+ * 0 error(異なった)
200+ */
201+static size_t CheckSignature(const uint8_t *ptr, size_t size)
202+{
203+ if (size < sizeof(signature)) {
204+ return 0;
205+ }
206+ if (memcmp(ptr, signature, sizeof(signature)) != 0) {
207+ return 0;
208+ }
209+ return sizeof(signature);
210+}
211+
212+#if defined(_MSC_VER) && (_MSC_VER < 1600)
213+#define offsetof(s,m) ((size_t)&(((s*)0)->m))
214+#endif
215+
216+#define MALLOCED_WSTRING_INFO(st, member) \
217+ sizeof(((st *)0)->member), \
218+ offsetof(st, member), \
219+ TSerializeInfo::MALLOCED_WSTRING
220+
221+/**
222+ * TTTsetを1つのバイナリにするための情報
223+ * 方針
224+ * - 全体をコピー
225+ * - 動的確保された文字列を追加
226+ */
227+static const TSerializeInfo serialize_info[] = {
228+ { sizeof(TTTSet), 0, TSerializeInfo::COPY}, // 全体をコピー
229+ { MALLOCED_WSTRING_INFO(TTTSet, HomeDirW) },
230+ { MALLOCED_WSTRING_INFO(TTTSet, SetupFNameW) },
231+ { MALLOCED_WSTRING_INFO(TTTSet, KeyCnfFNW) },
232+ { MALLOCED_WSTRING_INFO(TTTSet, LogFNW) },
233+ { MALLOCED_WSTRING_INFO(TTTSet, MacroFNW) },
234+ { MALLOCED_WSTRING_INFO(TTTSet, UILanguageFileW) },
235+ { MALLOCED_WSTRING_INFO(TTTSet, UILanguageFileW_ini) },
236+ { MALLOCED_WSTRING_INFO(TTTSet, ExeDirW) },
237+ { MALLOCED_WSTRING_INFO(TTTSet, LogDirW) },
238+ { 0, 0, TSerializeInfo::COPY },
239+};
240+
241+/**
242+ * TTTSet 構造体をバイナリデータに変換
243+ *
244+ * @param ts TTTSet構造体へのポインタ
245+ * @return バイナリへのデータのサイズ
246+ * @return バイナリへのデータへポインタ
247+ */
248+void *TTCMNSerialize(const TTTSet *ts, size_t *size)
249+{
250+ size_t signature_size;
251+ uint8_t *signature_data = MakeSignature(&signature_size);
252+ size_t data_size;
253+ uint8_t *data = SerializeData(ts, serialize_info, &data_size);
254+
255+ uint8_t *dest = (uint8_t *)malloc(signature_size + data_size);
256+ memcpy(dest, signature_data, signature_size);
257+ memcpy(dest + signature_size, data, data_size);
258+ free(data);
259+ free(signature_data);
260+ *size = signature_size + data_size;
261+ return dest;
262+}
263+
264+/**
265+ * バイナリデータをTTTSet 構造体に変換、
266+ *
267+ * @param[in] ptr バイナリデータへのポインタ
268+ * @param[in] size バイナリデータのサイズ
269+ * @param[out] ts TTTSet構造体へのポインタ
270+ */
271+void TTCMNUnserialize(const void *ptr, size_t size, TTTSet *ts)
272+{
273+ const uint8_t *data = (uint8_t *)ptr;
274+ size_t signature_size = CheckSignature(data, size);
275+ if (signature_size != 0) {
276+ data += signature_size;
277+ size -= signature_size;
278+ UnserializeData(data, size, serialize_info, ts);
279+ }
280+}
--- trunk/teraterm/ttpcmn/ttcmn_dup.h (nonexistent)
+++ trunk/teraterm/ttpcmn/ttcmn_dup.h (revision 9547)
@@ -0,0 +1,45 @@
1+/*
2+ * Copyright (C) 1994-1998 T. Teranishi
3+ * (C) 2021- TeraTerm Project
4+ * All rights reserved.
5+ *
6+ * Redistribution and use in source and binary forms, with or without
7+ * modification, are permitted provided that the following conditions
8+ * are met:
9+ *
10+ * 1. Redistributions of source code must retain the above copyright
11+ * notice, this list of conditions and the following disclaimer.
12+ * 2. Redistributions in binary form must reproduce the above copyright
13+ * notice, this list of conditions and the following disclaimer in the
14+ * documentation and/or other materials provided with the distribution.
15+ * 3. The name of the author may not be used to endorse or promote products
16+ * derived from this software without specific prior written permission.
17+ *
18+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+ */
29+
30+#pragma once
31+
32+#include <windows.h>
33+
34+#include "tttypes.h"
35+
36+#ifdef __cplusplus
37+extern "C" {
38+#endif
39+
40+void *TTCMNSerialize(const TTTSet *ts, size_t *size);
41+void TTCMNUnserialize(const void *ptr, size_t size, TTTSet *ts);
42+
43+#ifdef __cplusplus
44+}
45+#endif
Show on old repository browser