| 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 |
* �f�[�^�V���A���C�Y�����\���� |
| 58 |
*/ |
| 59 |
typedef struct { |
| 60 |
int size; // �f�[�^���T�C�Y(0���I��) |
| 61 |
int offset; // �����������I�t�Z�b�g |
| 62 |
enum { |
| 63 |
COPY = 0, // �f�[�^���R�s�[���� |
| 64 |
MALLOCED_WSTRING = 1, // malloc���������� |
| 65 |
} type; |
| 66 |
} TSerializeInfo; |
| 67 |
|
| 68 |
/** |
| 69 |
* �f�[�^���V���A���C�Y���� |
| 70 |
* |
| 71 |
* @param data �V���A���C�Y�����f�[�^ |
| 72 |
* @param info �V���A���C�Y���� |
| 73 |
* @param[out] size_ �V���A���C�Y����blob�T�C�Y(byte) |
| 74 |
* @return �V���A���C�Y����blob�f�[�^�����|�C���^ |
| 75 |
* �s�v����������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 |
* �A���V���A���C�Y���� |
| 123 |
* |
| 124 |
* @param blob �V���A���C�Y�������f�[�^�����|�C���^ |
| 125 |
* @param size �V���A���C�Y�������f�[�^���T�C�Y |
| 126 |
* @param info �V���A���C�Y���� |
| 127 |
* @param[out] data ���������f�[�^ |
| 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 |
// �f�[�^����������? |
| 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���O �X�L�b�v�����T�C�Y |
| 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���O �X�L�b�v�����T�C�Y |
| 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�����o�C�i������������������ |
| 223 |
* ���j |
| 224 |
* - �S�����R�s�[ |
| 225 |
* - ���I�m�������������������� |
| 226 |
*/ |
| 227 |
static const TSerializeInfo serialize_info[] = { |
| 228 |
{ sizeof(TTTSet), 0, TSerializeInfo::COPY}, // �S�����R�s�[ |
| 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 �\�������o�C�i���f�[�^������ |
| 243 |
* |
| 244 |
* @param ts TTTSet�\���������|�C���^ |
| 245 |
* @return �o�C�i�������f�[�^���T�C�Y |
| 246 |
* @return �o�C�i�������f�[�^���|�C���^ |
| 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 |
* �o�C�i���f�[�^��TTTSet �\�����������A |
| 266 |
* |
| 267 |
* @param[in] ptr �o�C�i���f�[�^�����|�C���^ |
| 268 |
* @param[in] size �o�C�i���f�[�^���T�C�Y |
| 269 |
* @param[out] ts TTTSet�\���������|�C���^ |
| 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 |
} |