OSDN Git Service

Add protection for password edit controls.
[ffftp/ffftp.git] / registry.c
1 /*=============================================================================\r
2 *                                                               レジストリ関係\r
3 *\r
4 *\r
5 ===============================================================================\r
6 / Copyright (C) 1997-2007 Sota. All rights reserved.\r
7 /\r
8 / Redistribution and use in source and binary forms, with or without \r
9 / modification, are permitted provided that the following conditions \r
10 / are met:\r
11 /\r
12 /  1. Redistributions of source code must retain the above copyright \r
13 /     notice, this list of conditions and the following disclaimer.\r
14 /  2. Redistributions in binary form must reproduce the above copyright \r
15 /     notice, this list of conditions and the following disclaimer in the \r
16 /     documentation and/or other materials provided with the distribution.\r
17 /\r
18 / THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR \r
19 / IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES \r
20 / OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. \r
21 / IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, \r
22 / INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, \r
23 / BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
24 / USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
25 / ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
26 / (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF \r
27 / THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28 /============================================================================*/\r
29 \r
30 // VC 8.0(2005)以上でのみ rand_s を利用可能\r
31 #if 1400 <= _MSC_VER\r
32 //#define _CRT_RAND_S\r
33 #endif\r
34 \r
35 #define STRICT\r
36 // IPv6対応\r
37 #include <winsock2.h>\r
38 #include <windows.h>\r
39 #include <stdio.h>\r
40 #include <stdlib.h>\r
41 #include <time.h>\r
42 #include <string.h>\r
43 #include <windowsx.h>\r
44 #include <process.h>\r
45 \r
46 \r
47 #include "common.h"\r
48 #include "resource.h"\r
49 #include "sample.h"\r
50 #include "sha.h"\r
51 #include "aes.h"\r
52 \r
53 \r
54 /*===== プロトタイプ =====*/\r
55 \r
56 static void SaveStr(HKEY hKey, char *Key, char *Str, char *DefaultStr);\r
57 static void SaveIntNum(HKEY hKey, char *Key, int Num, int DefaultNum);\r
58 static void MakeFontData(LOGFONT Font, HFONT hFont, char *Buf);\r
59 static int RestoreFontData(char *Str, LOGFONT *Font);\r
60 \r
61 static void EncodePassword(char *Str, char *Buf);\r
62 static void EncodePasswordOriginal(char *Str, char *Buf);\r
63 static void EncodePassword2(char *Str, char *Buf, const char *Key);\r
64 static void EncodePassword3(char *Str, char *Buf, const char *Key);\r
65 \r
66 static void DecodePassword(char *Str, char *Buf);\r
67 static void DecodePasswordOriginal(char *Str, char *Buf);\r
68 static void DecodePassword2(char *Str, char *Buf, const char *Key);\r
69 static void DecodePassword3(char *Str, char *Buf, const char *Key);\r
70 static int CreateAesKey(unsigned char *AesKey, const char* Key);\r
71 \r
72 static void SetRegType(int Type);\r
73 static int OpenReg(char *Name, void **Handle);\r
74 static int CreateReg(char *Name, void **Handle);\r
75 static int CloseReg(void *Handle);\r
76 static int OpenSubKey(void *Parent, char *Name, void **Handle);\r
77 static int CreateSubKey(void *Parent, char *Name, void **Handle);\r
78 static int CloseSubKey(void *Handle);\r
79 static int DeleteSubKey(void *Handle, char *Name);\r
80 static int DeleteValue(void *Handle, char *Name);\r
81 static int ReadIntValueFromReg(void *Handle, char *Name, int *Value);\r
82 static int WriteIntValueToReg(void *Handle, char *Name, int Value);\r
83 static int ReadStringFromReg(void *Handle, char *Name, char *Str, DWORD Size);\r
84 static int WriteStringToReg(void *Handle, char *Name, char *Str);\r
85 static int ReadMultiStringFromReg(void *Handle, char *Name, char *Str, DWORD Size);\r
86 static int WriteMultiStringToReg(void *Handle, char *Name, char *Str);\r
87 static int ReadBinaryFromReg(void *Handle, char *Name, void *Bin, DWORD Size);\r
88 static int WriteBinaryToReg(void *Handle, char *Name, void *Bin, int Len);\r
89 // 暗号化通信対応\r
90 static int StrCatOut(char *Src, int Len, char *Dst);\r
91 static int StrReadIn(char *Src, int Max, char *Dst);\r
92 \r
93 int CheckPasswordValidity( char* Password, int length, const char* HashStr );\r
94 void CreatePasswordHash( char* Password, int length, char* HashStr );\r
95 void SetHashSalt( DWORD salt );\r
96 \r
97 DWORD GetRandamDWRODValue(void);\r
98 \r
99 /* 2010.01.30 genta 追加 */\r
100 static char SecretKey[FMAX_PATH+1];\r
101 static int SecretKeyLength;\r
102 static int IsMasterPasswordError = PASSWORD_OK;\r
103 \r
104 static int IsRndSourceInit = 0;\r
105 static ulong RndSource[9];\r
106 \r
107 // UTF-8対応\r
108 static int IniKanjiCode = KANJI_NOCNV;\r
109 \r
110 /*===== 外部参照 =====*/\r
111 \r
112 /* 設定値 */\r
113 extern int WinPosX;\r
114 extern int WinPosY;\r
115 extern int WinWidth;\r
116 extern int WinHeight;\r
117 extern int LocalWidth;\r
118 extern int TaskHeight;\r
119 extern int LocalTabWidth[4];\r
120 extern int RemoteTabWidth[6];\r
121 extern char UserMailAdrs[USER_MAIL_LEN+1];\r
122 extern char ViewerName[VIEWERS][FMAX_PATH+1];\r
123 extern HFONT ListFont;\r
124 extern LOGFONT ListLogFont;\r
125 extern int LocalFileSort;\r
126 extern int LocalDirSort;\r
127 extern int RemoteFileSort;\r
128 extern int RemoteDirSort;\r
129 extern int TransMode;\r
130 extern int ConnectOnStart;\r
131 extern int DebugConsole;\r
132 extern int SaveWinPos;\r
133 extern char AsciiExt[ASCII_EXT_LEN+1];\r
134 extern int RecvMode;\r
135 extern int SendMode;\r
136 extern int MoveMode;\r
137 extern int ListType;\r
138 extern int CacheEntry;\r
139 extern int CacheSave;\r
140 extern char DefaultLocalPath[FMAX_PATH+1];\r
141 extern int SaveTimeStamp;\r
142 extern int FindMode;\r
143 extern int DotFile;\r
144 extern int DclickOpen;\r
145 extern SOUNDFILE Sound[SOUND_TYPES];\r
146 extern int FnameCnv;\r
147 extern int ConnectAndSet;\r
148 extern int TimeOut;\r
149 extern int RmEOF;\r
150 extern int RegType;\r
151 extern char FwallHost[HOST_ADRS_LEN+1];\r
152 extern char FwallUser[USER_NAME_LEN+1];\r
153 extern char FwallPass[PASSWORD_LEN+1];\r
154 extern int FwallPort;\r
155 extern int FwallType;\r
156 extern int FwallDefault;\r
157 extern int FwallSecurity;\r
158 extern int FwallResolv;\r
159 extern int FwallLower;\r
160 extern int FwallDelimiter;\r
161 extern int PasvDefault;\r
162 extern char MirrorNoTrn[MIRROR_LEN+1];\r
163 extern char MirrorNoDel[MIRROR_LEN+1];\r
164 extern int MirrorFnameCnv;\r
165 //extern int MirrorFolderCnv;\r
166 extern int RasClose;\r
167 extern int RasCloseNotify;\r
168 extern int FileHist;\r
169 extern char DefAttrList[DEFATTRLIST_LEN+1];\r
170 extern SIZE HostDlgSize;\r
171 extern SIZE BmarkDlgSize;\r
172 extern SIZE MirrorDlgSize;\r
173 extern int Sizing;\r
174 extern int SortSave;\r
175 extern char TmpPath[FMAX_PATH+1];\r
176 extern int QuickAnonymous;\r
177 extern int PassToHist;\r
178 extern int VaxSemicolon;\r
179 extern int SendQuit;\r
180 extern int NoRasControl;\r
181 extern int SuppressSave;\r
182 \r
183 extern int UpExistMode;\r
184 extern int ExistMode;\r
185 extern int DispIgnoreHide;\r
186 extern int DispDrives;\r
187 extern int MirUpDelNotify;\r
188 extern int MirDownDelNotify;\r
189 \r
190 extern int FolderAttr;\r
191 extern int FolderAttrNum;\r
192 \r
193 // 暗号化通信対応\r
194 extern BYTE CertificateCacheHash[MAX_CERT_CACHE_HASH][20];\r
195 extern BYTE SSLRootCAFileHash[20];\r
196 // ファイルアイコン表示対応\r
197 extern int DispFileIcon;\r
198 // ディレクトリ自動作成\r
199 extern int MakeAllDir;\r
200 \r
201 /*----- マスタパスワードの設定 ----------------------------------------------\r
202 *\r
203 *       Parameter\r
204 *               const char* Password : マスターパスワード\r
205 *\r
206 *       Return Value\r
207 *               なし\r
208 *----------------------------------------------------------------------------*/\r
209 void SetMasterPassword( const char* Password )\r
210 {\r
211         ZeroMemory( SecretKey, MAX_PASSWORD_LEN + 12 );\r
212         if( Password != NULL ){\r
213                 strncpy( SecretKey, Password, MAX_PASSWORD_LEN );\r
214         }\r
215         else {\r
216                 strcpy( SecretKey, DEFAULT_PASSWORD );\r
217         }\r
218         SecretKeyLength = strlen( SecretKey );\r
219         \r
220         /* 未検証なので,初期状態はOKにする (強制再設定→保存にを可能にする)*/\r
221         IsMasterPasswordError = PASSWORD_OK;\r
222 }\r
223 \r
224 /*----- マスタパスワードの状態取得 ----------------------------------------------\r
225 *\r
226 *       Parameter\r
227 *               なし\r
228 *\r
229 *       Return Value\r
230 *               PASSWORD_OK : OK\r
231 *               PASSWORD_UNMATCH : パスワード不一致\r
232 *               BAD_PASSWORD_HASH : パスワード確認失敗\r
233 *----------------------------------------------------------------------------*/\r
234 int GetMasterPasswordStatus(void)\r
235 {\r
236         return IsMasterPasswordError;\r
237 }\r
238 \r
239 /*----- レジストリ/INIファイルのマスターパスワードの検証を行う ------------\r
240 *\r
241 *       Parameter\r
242 *               なし\r
243 *\r
244 *       Return Value\r
245 *               \r
246 *----------------------------------------------------------------------------*/\r
247 \r
248 int ValidateMasterPassword(void)\r
249 {\r
250         void *hKey3;\r
251         int i;\r
252 \r
253         SetRegType(REGTYPE_INI);\r
254         if((i = OpenReg("FFFTP", &hKey3)) != FFFTP_SUCCESS)\r
255         {\r
256                 if(AskForceIni() == NO)\r
257                 {\r
258                         SetRegType(REGTYPE_REG);\r
259                         i = OpenReg("FFFTP", &hKey3);\r
260                 }\r
261         }\r
262         if(i == FFFTP_SUCCESS){\r
263                 char checkbuf[48];\r
264                 int salt = 0;\r
265 \r
266                 if( ReadIntValueFromReg(hKey3, "CredentialSalt", &salt)){\r
267                         SetHashSalt( salt );\r
268                 }\r
269                 if( ReadStringFromReg(hKey3, "CredentialCheck", checkbuf, sizeof( checkbuf )) == FFFTP_SUCCESS ){\r
270                         switch( CheckPasswordValidity( SecretKey, SecretKeyLength, checkbuf ) ){\r
271                         case 0: /* not match */\r
272                                 IsMasterPasswordError = PASSWORD_UNMATCH;\r
273                                 break;\r
274                         case 1: /* match */\r
275                                 IsMasterPasswordError = PASSWORD_OK;\r
276                                 break;\r
277                         case 2: /* invalid hash */\r
278                         default:\r
279                                 IsMasterPasswordError = BAD_PASSWORD_HASH;\r
280                                 break;\r
281                         }\r
282                 }\r
283                 CloseReg(hKey3);\r
284                 return YES;\r
285         }\r
286         return NO;\r
287 }\r
288 \r
289 /*----- レジストリ/INIファイルに設定値を保存 ---------------------------------\r
290 *\r
291 *       Parameter\r
292 *               なし\r
293 *\r
294 *       Return Value\r
295 *               なし\r
296 *----------------------------------------------------------------------------*/\r
297 \r
298 void SaveRegistry(void)\r
299 {\r
300         void *hKey3;\r
301         void *hKey4;\r
302         void *hKey5;\r
303         // 暗号化通信対応\r
304 //      char Str[FMAX_PATH+1];\r
305         char Str[PRIVATE_KEY_LEN*4+1];\r
306         char Buf[FMAX_PATH+1];\r
307         int i;\r
308         int n;\r
309         HOSTDATA DefaultHost;\r
310         HOSTDATA Host;\r
311         HISTORYDATA Hist;\r
312         HISTORYDATA DefaultHist;\r
313         \r
314         if( GetMasterPasswordStatus() == PASSWORD_UNMATCH ){\r
315                 /* 2010.01.30 genta: マスターパスワードが不一致の場合は不用意に上書きしない */\r
316                 return;\r
317         }\r
318 \r
319         SetRegType(RegType);\r
320         if(CreateReg("FFFTP", &hKey3) == FFFTP_SUCCESS)\r
321         {\r
322                 char buf[48];\r
323                 int salt = GetTickCount();\r
324         \r
325                 WriteIntValueToReg(hKey3, "Version", VER_NUM);\r
326                 WriteIntValueToReg(hKey3, "CredentialSalt", salt);\r
327                 \r
328                 SetHashSalt( salt );\r
329                 /* save password hash */\r
330                 CreatePasswordHash( SecretKey, SecretKeyLength, buf );\r
331                 WriteStringToReg(hKey3, "CredentialCheck", buf);\r
332 \r
333                 if(CreateSubKey(hKey3, "Options", &hKey4) == FFFTP_SUCCESS)\r
334                 {\r
335                         WriteIntValueToReg(hKey4, "NoSave", SuppressSave);\r
336 \r
337                         if(SuppressSave != YES)\r
338                         {\r
339                                 WriteIntValueToReg(hKey4, "WinPosX", WinPosX);\r
340                                 WriteIntValueToReg(hKey4, "WinPosY", WinPosY);\r
341                                 WriteIntValueToReg(hKey4, "WinWidth", WinWidth);\r
342                                 WriteIntValueToReg(hKey4, "WinHeight", WinHeight);\r
343                                 WriteIntValueToReg(hKey4, "LocalWidth", LocalWidth);\r
344                                 WriteIntValueToReg(hKey4, "TaskHeight", TaskHeight);\r
345                                 WriteBinaryToReg(hKey4, "LocalColm", LocalTabWidth, sizeof(LocalTabWidth));\r
346                                 WriteBinaryToReg(hKey4, "RemoteColm", RemoteTabWidth, sizeof(RemoteTabWidth));\r
347                                 WriteIntValueToReg(hKey4, "SwCmd", Sizing);\r
348 \r
349                                 WriteStringToReg(hKey4, "UserMail", UserMailAdrs);\r
350                                 WriteStringToReg(hKey4, "Viewer", ViewerName[0]);\r
351                                 WriteStringToReg(hKey4, "Viewer2", ViewerName[1]);\r
352                                 WriteStringToReg(hKey4, "Viewer3", ViewerName[2]);\r
353 \r
354                                 WriteIntValueToReg(hKey4, "TrType", TransMode);\r
355                                 WriteIntValueToReg(hKey4, "Recv", RecvMode);\r
356                                 WriteIntValueToReg(hKey4, "Send", SendMode);\r
357                                 WriteIntValueToReg(hKey4, "Move", MoveMode);\r
358                                 WriteStringToReg(hKey4, "Path", DefaultLocalPath);\r
359                                 WriteIntValueToReg(hKey4, "Time", SaveTimeStamp);\r
360                                 WriteIntValueToReg(hKey4, "EOF", RmEOF);\r
361                                 WriteIntValueToReg(hKey4, "Scolon", VaxSemicolon);\r
362 \r
363                                 WriteIntValueToReg(hKey4, "RecvEx", ExistMode);\r
364                                 WriteIntValueToReg(hKey4, "SendEx", UpExistMode);\r
365 \r
366                                 WriteIntValueToReg(hKey4, "LFsort", LocalFileSort);\r
367                                 WriteIntValueToReg(hKey4, "LDsort", LocalDirSort);\r
368                                 WriteIntValueToReg(hKey4, "RFsort", RemoteFileSort);\r
369                                 WriteIntValueToReg(hKey4, "RDsort", RemoteDirSort);\r
370                                 WriteIntValueToReg(hKey4, "SortSave", SortSave);\r
371 \r
372                                 WriteIntValueToReg(hKey4, "ListType", ListType);\r
373                                 WriteIntValueToReg(hKey4, "Cache", CacheEntry);\r
374                                 WriteIntValueToReg(hKey4, "CacheSave", CacheSave);\r
375                                 WriteIntValueToReg(hKey4, "DotFile", DotFile);\r
376                                 WriteIntValueToReg(hKey4, "Dclick", DclickOpen);\r
377 \r
378                                 WriteIntValueToReg(hKey4, "ConS", ConnectOnStart);\r
379                                 WriteIntValueToReg(hKey4, "OldDlg", ConnectAndSet);\r
380                                 WriteIntValueToReg(hKey4, "RasClose", RasClose);\r
381                                 WriteIntValueToReg(hKey4, "RasNotify", RasCloseNotify);\r
382                                 WriteIntValueToReg(hKey4, "Qanony", QuickAnonymous);\r
383                                 WriteIntValueToReg(hKey4, "PassHist", PassToHist);\r
384                                 WriteIntValueToReg(hKey4, "SendQuit", SendQuit);\r
385                                 WriteIntValueToReg(hKey4, "NoRas", NoRasControl);\r
386 \r
387                                 WriteIntValueToReg(hKey4, "Debug", DebugConsole);\r
388                                 WriteIntValueToReg(hKey4, "WinPos", SaveWinPos);\r
389                                 WriteIntValueToReg(hKey4, "RegExp", FindMode);\r
390                                 WriteIntValueToReg(hKey4, "Reg", RegType);\r
391 \r
392                                 WriteMultiStringToReg(hKey4, "AsciiFile", AsciiExt);\r
393                                 WriteIntValueToReg(hKey4, "LowUp", FnameCnv);\r
394                                 WriteIntValueToReg(hKey4, "Tout", TimeOut);\r
395 \r
396                                 WriteMultiStringToReg(hKey4, "NoTrn", MirrorNoTrn);\r
397                                 WriteMultiStringToReg(hKey4, "NoDel", MirrorNoDel);\r
398                                 WriteIntValueToReg(hKey4, "MirFile", MirrorFnameCnv);\r
399                                 WriteIntValueToReg(hKey4, "MirUNot", MirUpDelNotify);\r
400                                 WriteIntValueToReg(hKey4, "MirDNot", MirDownDelNotify);\r
401 \r
402                                 MakeFontData(ListLogFont, ListFont, Str);\r
403                                 WriteStringToReg(hKey4, "ListFont", Str);\r
404                                 WriteIntValueToReg(hKey4, "ListHide", DispIgnoreHide);\r
405                                 WriteIntValueToReg(hKey4, "ListDrv", DispDrives);\r
406 \r
407                                 WriteStringToReg(hKey4, "FwallHost", FwallHost);\r
408                                 WriteStringToReg(hKey4, "FwallUser", FwallUser);\r
409                                 EncodePassword(FwallPass, Str);\r
410                                 WriteStringToReg(hKey4, "FwallPass", Str);\r
411                                 WriteIntValueToReg(hKey4, "FwallPort", FwallPort);\r
412                                 WriteIntValueToReg(hKey4, "FwallType", FwallType);\r
413                                 WriteIntValueToReg(hKey4, "FwallDef", FwallDefault);\r
414                                 WriteIntValueToReg(hKey4, "FwallSec", FwallSecurity);\r
415                                 WriteIntValueToReg(hKey4, "PasvDef", PasvDefault);\r
416                                 WriteIntValueToReg(hKey4, "FwallRes", FwallResolv);\r
417                                 WriteIntValueToReg(hKey4, "FwallLow", FwallLower);\r
418                                 WriteIntValueToReg(hKey4, "FwallDel", FwallDelimiter);\r
419 \r
420                                 WriteIntValueToReg(hKey4, "SndConSw", Sound[SND_CONNECT].On);\r
421                                 WriteIntValueToReg(hKey4, "SndTrnSw", Sound[SND_TRANS].On);\r
422                                 WriteIntValueToReg(hKey4, "SndErrSw", Sound[SND_ERROR].On);\r
423                                 WriteStringToReg(hKey4, "SndCon", Sound[SND_CONNECT].Fname);\r
424                                 WriteStringToReg(hKey4, "SndTrn", Sound[SND_TRANS].Fname);\r
425                                 WriteStringToReg(hKey4, "SndErr", Sound[SND_ERROR].Fname);\r
426 \r
427                                 WriteMultiStringToReg(hKey4, "DefAttr", DefAttrList);\r
428 \r
429                                 // 環境依存の不具合対策\r
430 //                              GetTempPath(FMAX_PATH, Str);\r
431                                 GetAppTempPath(Str);\r
432                                 SetYenTail(Str);\r
433                                 SaveStr(hKey4, "Tmp", TmpPath, Str);\r
434 \r
435                                 WriteBinaryToReg(hKey4, "Hdlg", &HostDlgSize, sizeof(SIZE));\r
436                                 WriteBinaryToReg(hKey4, "Bdlg", &BmarkDlgSize, sizeof(SIZE));\r
437                                 WriteBinaryToReg(hKey4, "Mdlg", &MirrorDlgSize, sizeof(SIZE));\r
438 \r
439                                 WriteIntValueToReg(hKey4, "FAttrSw", FolderAttr);\r
440                                 WriteIntValueToReg(hKey4, "FAttr", FolderAttrNum);\r
441 \r
442                                 WriteIntValueToReg(hKey4, "HistNum", FileHist);\r
443 \r
444                                 /* Ver1.54a以前の形式のヒストリデータは削除 */\r
445                                 DeleteValue(hKey4, "Hist");\r
446 \r
447                                 /* ヒストリの設定を保存 */\r
448                                 CopyDefaultHistory(&DefaultHist);\r
449                                 n = 0;\r
450                                 for(i = AskHistoryNum(); i > 0; i--)\r
451                                 {\r
452                                         if(GetHistoryByNum(i-1, &Hist) == FFFTP_SUCCESS)\r
453                                         {\r
454                                                 sprintf(Str, "History%d", n);\r
455                                                 if(CreateSubKey(hKey4, Str, &hKey5) == FFFTP_SUCCESS)\r
456                                                 {\r
457                                                         SaveStr(hKey5, "HostAdrs", Hist.HostAdrs, DefaultHist.HostAdrs);\r
458                                                         SaveStr(hKey5, "UserName", Hist.UserName, DefaultHist.UserName);\r
459                                                         SaveStr(hKey5, "Account", Hist.Account, DefaultHist.Account);\r
460                                                         SaveStr(hKey5, "LocalDir", Hist.LocalInitDir, NULL);\r
461                                                         SaveStr(hKey5, "RemoteDir", Hist.RemoteInitDir, DefaultHist.RemoteInitDir);\r
462                                                         SaveStr(hKey5, "Chmod", Hist.ChmodCmd, DefaultHist.ChmodCmd);\r
463                                                         SaveStr(hKey5, "Nlst", Hist.LsName, DefaultHist.LsName);\r
464                                                         SaveStr(hKey5, "Init", Hist.InitCmd, DefaultHist.InitCmd);\r
465                                                         EncodePassword(Hist.PassWord, Str);\r
466                                                         SaveStr(hKey5, "Password", Str, DefaultHist.PassWord);\r
467                                                         SaveIntNum(hKey5, "Port", Hist.Port, DefaultHist.Port);\r
468                                                         SaveIntNum(hKey5, "Kanji", Hist.KanjiCode, DefaultHist.KanjiCode);\r
469                                                         SaveIntNum(hKey5, "KanaCnv", Hist.KanaCnv, DefaultHist.KanaCnv);\r
470                                                         SaveIntNum(hKey5, "NameKanji", Hist.NameKanjiCode, DefaultHist.NameKanjiCode);\r
471                                                         SaveIntNum(hKey5, "NameKana", Hist.NameKanaCnv, DefaultHist.NameKanaCnv);\r
472                                                         SaveIntNum(hKey5, "Pasv", Hist.Pasv, DefaultHist.Pasv);\r
473                                                         SaveIntNum(hKey5, "Fwall", Hist.FireWall, DefaultHist.FireWall);\r
474                                                         SaveIntNum(hKey5, "List", Hist.ListCmdOnly, DefaultHist.ListCmdOnly);\r
475                                                         SaveIntNum(hKey5, "NLST-R", Hist.UseNLST_R, DefaultHist.UseNLST_R);\r
476                                                         SaveIntNum(hKey5, "Tzone", Hist.TimeZone, DefaultHist.TimeZone);\r
477                                                         SaveIntNum(hKey5, "Type", Hist.HostType, DefaultHist.HostType);\r
478                                                         SaveIntNum(hKey5, "Sync", Hist.SyncMove, DefaultHist.SyncMove);\r
479                                                         SaveIntNum(hKey5, "Fpath", Hist.NoFullPath, DefaultHist.NoFullPath);\r
480                                                         WriteBinaryToReg(hKey5, "Sort", &Hist.Sort, sizeof(Hist.Sort));\r
481                                                         SaveIntNum(hKey5, "Secu", Hist.Security, DefaultHist.Security);\r
482                                                         WriteIntValueToReg(hKey5, "TrType", Hist.Type);\r
483                                                         SaveIntNum(hKey5, "Dial", Hist.Dialup, DefaultHist.Dialup);\r
484                                                         SaveIntNum(hKey5, "UseIt", Hist.DialupAlways, DefaultHist.DialupAlways);\r
485                                                         SaveIntNum(hKey5, "Notify", Hist.DialupNotify, DefaultHist.DialupNotify);\r
486                                                         SaveStr(hKey5, "DialTo", Hist.DialEntry, DefaultHist.DialEntry);\r
487                                                         // 暗号化通信対応\r
488                                                         SaveIntNum(hKey5, "NoEncryption", Hist.UseNoEncryption, DefaultHist.UseNoEncryption);\r
489                                                         SaveIntNum(hKey5, "FTPES", Hist.UseFTPES, DefaultHist.UseFTPES);\r
490                                                         SaveIntNum(hKey5, "FTPIS", Hist.UseFTPIS, DefaultHist.UseFTPIS);\r
491                                                         SaveIntNum(hKey5, "SFTP", Hist.UseSFTP, DefaultHist.UseSFTP);\r
492                                                         EncodePassword(Hist.PrivateKey, Str);\r
493                                                         SaveStr(hKey5, "PKey", Str, DefaultHist.PrivateKey);\r
494                                                         // 同時接続対応\r
495                                                         SaveIntNum(hKey5, "ThreadCount", Hist.MaxThreadCount, DefaultHist.MaxThreadCount);\r
496                                                         SaveIntNum(hKey5, "ReuseCmdSkt", Hist.ReuseCmdSkt, DefaultHist.ReuseCmdSkt);\r
497                                                         // MLSD対応\r
498                                                         SaveIntNum(hKey5, "MLSD", Hist.UseMLSD, DefaultHist.UseMLSD);\r
499                                                         // IPv6対応\r
500                                                         SaveIntNum(hKey5, "NetType", Hist.NetType, DefaultHist.NetType);\r
501                                                         // 自動切断対策\r
502                                                         SaveIntNum(hKey5, "Noop", Hist.NoopInterval, DefaultHist.NoopInterval);\r
503                                                         // 再転送対応\r
504                                                         SaveIntNum(hKey5, "ErrMode", Hist.TransferErrorMode, DefaultHist.TransferErrorMode);\r
505                                                         SaveIntNum(hKey5, "ErrNotify", Hist.TransferErrorNotify, DefaultHist.TransferErrorNotify);\r
506 \r
507                                                         CloseSubKey(hKey5);\r
508                                                         n++;\r
509                                                 }\r
510                                         }\r
511                                 }\r
512                                 WriteIntValueToReg(hKey4, "SavedHist", n);\r
513 \r
514                                 /* 余分なヒストリがあったら削除 */\r
515                                 for(; n < 999; n++)\r
516                                 {\r
517                                         sprintf(Str, "History%d", n);\r
518                                         if(DeleteSubKey(hKey4, Str) != FFFTP_SUCCESS)\r
519                                                 break;\r
520                                 }\r
521 \r
522                                 /* ホストの設定を保存 */\r
523                                 CopyDefaultHost(&DefaultHost);\r
524                                 i = 0;\r
525                                 while(CopyHostFromList(i, &Host) == FFFTP_SUCCESS)\r
526                                 {\r
527                                         sprintf(Str, "Host%d", i);\r
528                                         if(CreateSubKey(hKey4, Str, &hKey5) == FFFTP_SUCCESS)\r
529                                         {\r
530 //                                              SaveIntNum(hKey5, "Set", Host.Level, DefaultHost.Level);\r
531                                                 WriteIntValueToReg(hKey5, "Set", Host.Level);\r
532                                                 SaveStr(hKey5, "HostName", Host.HostName, DefaultHost.HostName);\r
533                                                 if((Host.Level & SET_LEVEL_GROUP) == 0)\r
534                                                 {\r
535                                                         SaveStr(hKey5, "HostAdrs", Host.HostAdrs, DefaultHost.HostAdrs);\r
536                                                         SaveStr(hKey5, "UserName", Host.UserName, DefaultHost.UserName);\r
537                                                         SaveStr(hKey5, "Account", Host.Account, DefaultHost.Account);\r
538                                                         SaveStr(hKey5, "LocalDir", Host.LocalInitDir, NULL);\r
539                                                         SaveStr(hKey5, "RemoteDir", Host.RemoteInitDir, DefaultHost.RemoteInitDir);\r
540                                                         SaveStr(hKey5, "Chmod", Host.ChmodCmd, DefaultHost.ChmodCmd);\r
541                                                         SaveStr(hKey5, "Nlst", Host.LsName, DefaultHost.LsName);\r
542                                                         SaveStr(hKey5, "Init", Host.InitCmd, DefaultHost.InitCmd);\r
543 \r
544                                                         if(Host.Anonymous == NO)\r
545                                                                 EncodePassword(Host.PassWord, Str);\r
546                                                         else\r
547                                                                 strcpy(Str, DefaultHost.PassWord);\r
548                                                         SaveStr(hKey5, "Password", Str, DefaultHost.PassWord);\r
549 \r
550                                                         SaveIntNum(hKey5, "Port", Host.Port, DefaultHost.Port);\r
551                                                         SaveIntNum(hKey5, "Anonymous", Host.Anonymous, DefaultHost.Anonymous);\r
552                                                         SaveIntNum(hKey5, "Kanji", Host.KanjiCode, DefaultHost.KanjiCode);\r
553                                                         SaveIntNum(hKey5, "KanaCnv", Host.KanaCnv, DefaultHost.KanaCnv);\r
554                                                         SaveIntNum(hKey5, "NameKanji", Host.NameKanjiCode, DefaultHost.NameKanjiCode);\r
555                                                         SaveIntNum(hKey5, "NameKana", Host.NameKanaCnv, DefaultHost.NameKanaCnv);\r
556                                                         SaveIntNum(hKey5, "Pasv", Host.Pasv, DefaultHost.Pasv);\r
557                                                         SaveIntNum(hKey5, "Fwall", Host.FireWall, DefaultHost.FireWall);\r
558                                                         SaveIntNum(hKey5, "List", Host.ListCmdOnly, DefaultHost.ListCmdOnly);\r
559                                                         SaveIntNum(hKey5, "NLST-R", Host.UseNLST_R, DefaultHost.UseNLST_R);\r
560                                                         SaveIntNum(hKey5, "Last", Host.LastDir, DefaultHost.LastDir);\r
561                                                         SaveIntNum(hKey5, "Tzone", Host.TimeZone, DefaultHost.TimeZone);\r
562                                                         SaveIntNum(hKey5, "Type", Host.HostType, DefaultHost.HostType);\r
563                                                         SaveIntNum(hKey5, "Sync", Host.SyncMove, DefaultHost.SyncMove);\r
564                                                         SaveIntNum(hKey5, "Fpath", Host.NoFullPath, DefaultHost.NoFullPath);\r
565                                                         WriteBinaryToReg(hKey5, "Sort", &Host.Sort, sizeof(Host.Sort));\r
566                                                         SaveIntNum(hKey5, "Secu", Host.Security, DefaultHost.Security);\r
567 \r
568                                                         WriteMultiStringToReg(hKey5, "Bmarks", Host.BookMark);\r
569 \r
570                                                         SaveIntNum(hKey5, "Dial", Host.Dialup, DefaultHost.Dialup);\r
571                                                         SaveIntNum(hKey5, "UseIt", Host.DialupAlways, DefaultHost.DialupAlways);\r
572                                                         SaveIntNum(hKey5, "Notify", Host.DialupNotify, DefaultHost.DialupNotify);\r
573                                                         SaveStr(hKey5, "DialTo", Host.DialEntry, DefaultHost.DialEntry);\r
574                                                         // 暗号化通信対応\r
575                                                         SaveIntNum(hKey5, "NoEncryption", Host.UseNoEncryption, DefaultHost.UseNoEncryption);\r
576                                                         SaveIntNum(hKey5, "FTPES", Host.UseFTPES, DefaultHost.UseFTPES);\r
577                                                         SaveIntNum(hKey5, "FTPIS", Host.UseFTPIS, DefaultHost.UseFTPIS);\r
578                                                         SaveIntNum(hKey5, "SFTP", Host.UseSFTP, DefaultHost.UseSFTP);\r
579                                                         EncodePassword(Host.PrivateKey, Str);\r
580                                                         SaveStr(hKey5, "PKey", Str, DefaultHost.PrivateKey);\r
581                                                         // 同時接続対応\r
582                                                         SaveIntNum(hKey5, "ThreadCount", Host.MaxThreadCount, DefaultHost.MaxThreadCount);\r
583                                                         SaveIntNum(hKey5, "ReuseCmdSkt", Host.ReuseCmdSkt, DefaultHost.ReuseCmdSkt);\r
584                                                         // MLSD対応\r
585                                                         SaveIntNum(hKey5, "MLSD", Host.UseMLSD, DefaultHost.UseMLSD);\r
586                                                         // IPv6対応\r
587                                                         SaveIntNum(hKey5, "NetType", Host.NetType, DefaultHost.NetType);\r
588                                                         // 自動切断対策\r
589                                                         SaveIntNum(hKey5, "Noop", Host.NoopInterval, DefaultHost.NoopInterval);\r
590                                                         // 再転送対応\r
591                                                         SaveIntNum(hKey5, "ErrMode", Host.TransferErrorMode, DefaultHost.TransferErrorMode);\r
592                                                         SaveIntNum(hKey5, "ErrNotify", Host.TransferErrorNotify, DefaultHost.TransferErrorNotify);\r
593                                                 }\r
594                                                 CloseSubKey(hKey5);\r
595                                         }\r
596                                         i++;\r
597                                 }\r
598                                 WriteIntValueToReg(hKey4, "SetNum", i);\r
599 \r
600                                 /* 余分なホストの設定があったら削除 */\r
601                                 for(; i < 998; i++)\r
602                                 {\r
603                                         sprintf(Str, "Host%d", i);\r
604                                         if(DeleteSubKey(hKey4, Str) != FFFTP_SUCCESS)\r
605                                                 break;\r
606                                 }\r
607 \r
608                                 if((i = AskCurrentHost()) == HOSTNUM_NOENTRY)\r
609                                         i = 0;\r
610                                 WriteIntValueToReg(hKey4, "CurSet", i);\r
611 \r
612                                 // 暗号化通信対応\r
613                                 WriteBinaryToReg(hKey4, "CertCacheHash", &CertificateCacheHash, sizeof(CertificateCacheHash));\r
614                                 strcpy(Buf, "");\r
615                                 StrCatOut((char*)&SSLRootCAFileHash, sizeof(SSLRootCAFileHash), Buf);\r
616                                 EncodePassword(Buf, Str);\r
617                                 WriteStringToReg(hKey4, "RootCertHash", Str);\r
618                                 // ファイルアイコン表示対応\r
619                                 WriteIntValueToReg(hKey4, "ListIcon", DispFileIcon);\r
620                                 // ディレクトリ自動作成\r
621                                 WriteIntValueToReg(hKey4, "MakeDir", MakeAllDir);\r
622                         }\r
623                         CloseSubKey(hKey4);\r
624                 }\r
625                 CloseReg(hKey3);\r
626         }\r
627         return;\r
628 }\r
629 \r
630 /*----- レジストリ/INIファイルから設定値を呼び出す ---------------------------\r
631 *\r
632 *       この関数を複数回呼び出すと,ホスト設定が追加される.\r
633 *\r
634 *       Parameter\r
635 *               なし\r
636 *\r
637 *       Return Value\r
638 *               YES: 読み出し成功\r
639 *               NO:  読み出し失敗(設定無し)\r
640 *----------------------------------------------------------------------------*/\r
641 \r
642 int LoadRegistry(void)\r
643 {\r
644         void *hKey3;\r
645         void *hKey4;\r
646         void *hKey5;\r
647         int i;\r
648         int Sets;\r
649         // 暗号化通信対応\r
650 //      char Str[256];  /* ASCII_EXT_LENより大きい事 */\r
651         char Str[PRIVATE_KEY_LEN*4+1];\r
652         char Buf[FMAX_PATH+1];\r
653         char *Pos;\r
654         char *Pos2;\r
655         HOSTDATA Host;\r
656         HISTORYDATA Hist;\r
657         int Sts;\r
658         int Version;\r
659 \r
660         Sts = NO;\r
661 \r
662         SetRegType(REGTYPE_INI);\r
663         if((i = OpenReg("FFFTP", &hKey3)) != FFFTP_SUCCESS)\r
664         {\r
665                 if(AskForceIni() == NO)\r
666                 {\r
667                         SetRegType(REGTYPE_REG);\r
668                         i = OpenReg("FFFTP", &hKey3);\r
669                 }\r
670         }\r
671 \r
672         if(i == FFFTP_SUCCESS)\r
673         {\r
674 //              char checkbuf[48];\r
675                 int salt = 0;\r
676                 Sts = YES;\r
677 \r
678                 ReadIntValueFromReg(hKey3, "Version", &Version);\r
679                 // UTF-8対応\r
680                 if(Version < 1980)\r
681                         IniKanjiCode = KANJI_SJIS;\r
682 \r
683                 if(OpenSubKey(hKey3, "Options", &hKey4) == FFFTP_SUCCESS)\r
684                 {\r
685                         ReadIntValueFromReg(hKey4, "WinPosX", &WinPosX);\r
686                         ReadIntValueFromReg(hKey4, "WinPosY", &WinPosY);\r
687                         ReadIntValueFromReg(hKey4, "WinWidth", &WinWidth);\r
688                         ReadIntValueFromReg(hKey4, "WinHeight", &WinHeight);\r
689                         ReadIntValueFromReg(hKey4, "LocalWidth", &LocalWidth);\r
690                         /* ↓旧バージョンのバグ対策 */\r
691                         LocalWidth = max1(0, LocalWidth);\r
692                         ReadIntValueFromReg(hKey4, "TaskHeight", &TaskHeight);\r
693                         /* ↓旧バージョンのバグ対策 */\r
694                         TaskHeight = max1(0, TaskHeight);\r
695                         ReadBinaryFromReg(hKey4, "LocalColm", &LocalTabWidth, sizeof(LocalTabWidth));\r
696                         ReadBinaryFromReg(hKey4, "RemoteColm", &RemoteTabWidth, sizeof(RemoteTabWidth));\r
697                         ReadIntValueFromReg(hKey4, "SwCmd", &Sizing);\r
698 \r
699                         ReadStringFromReg(hKey4, "UserMail", UserMailAdrs, USER_MAIL_LEN+1);\r
700                         ReadStringFromReg(hKey4, "Viewer", ViewerName[0], FMAX_PATH+1);\r
701                         ReadStringFromReg(hKey4, "Viewer2", ViewerName[1], FMAX_PATH+1);\r
702                         ReadStringFromReg(hKey4, "Viewer3", ViewerName[2], FMAX_PATH+1);\r
703 \r
704                         ReadIntValueFromReg(hKey4, "TrType", &TransMode);\r
705                         ReadIntValueFromReg(hKey4, "Recv", &RecvMode);\r
706                         ReadIntValueFromReg(hKey4, "Send", &SendMode);\r
707                         ReadIntValueFromReg(hKey4, "Move", &MoveMode);\r
708                         ReadStringFromReg(hKey4, "Path", DefaultLocalPath, FMAX_PATH+1);\r
709                         ReadIntValueFromReg(hKey4, "Time", &SaveTimeStamp);\r
710                         ReadIntValueFromReg(hKey4, "EOF", &RmEOF);\r
711                         ReadIntValueFromReg(hKey4, "Scolon", &VaxSemicolon);\r
712 \r
713                         ReadIntValueFromReg(hKey4, "RecvEx", &ExistMode);\r
714                         ReadIntValueFromReg(hKey4, "SendEx", &UpExistMode);\r
715 \r
716                         ReadIntValueFromReg(hKey4, "LFsort", &LocalFileSort);\r
717                         ReadIntValueFromReg(hKey4, "LDsort", &LocalDirSort);\r
718                         ReadIntValueFromReg(hKey4, "RFsort", &RemoteFileSort);\r
719                         ReadIntValueFromReg(hKey4, "RDsort", &RemoteDirSort);\r
720                         ReadIntValueFromReg(hKey4, "SortSave", &SortSave);\r
721 \r
722                         ReadIntValueFromReg(hKey4, "ListType", &ListType);\r
723                         ReadIntValueFromReg(hKey4, "Cache", &CacheEntry);\r
724                         ReadIntValueFromReg(hKey4, "CacheSave", &CacheSave);\r
725                         ReadIntValueFromReg(hKey4, "DotFile", &DotFile);\r
726                         ReadIntValueFromReg(hKey4, "Dclick", &DclickOpen);\r
727 \r
728                         ReadIntValueFromReg(hKey4, "ConS", &ConnectOnStart);\r
729                         ReadIntValueFromReg(hKey4, "OldDlg", &ConnectAndSet);\r
730                         ReadIntValueFromReg(hKey4, "RasClose", &RasClose);\r
731                         ReadIntValueFromReg(hKey4, "RasNotify", &RasCloseNotify);\r
732                         ReadIntValueFromReg(hKey4, "Qanony", &QuickAnonymous);\r
733                         ReadIntValueFromReg(hKey4, "PassHist", &PassToHist);\r
734                         ReadIntValueFromReg(hKey4, "SendQuit", &SendQuit);\r
735                         ReadIntValueFromReg(hKey4, "NoRas", &NoRasControl);\r
736 \r
737                         ReadIntValueFromReg(hKey4, "Debug", &DebugConsole);\r
738                         ReadIntValueFromReg(hKey4, "WinPos", &SaveWinPos);\r
739                         ReadIntValueFromReg(hKey4, "RegExp", &FindMode);\r
740                         ReadIntValueFromReg(hKey4, "Reg", &RegType);\r
741 \r
742                         if(ReadMultiStringFromReg(hKey4, "AsciiFile", AsciiExt, ASCII_EXT_LEN+1) == FFFTP_FAIL)\r
743                         {\r
744                                 /* 旧ASCIIモードの拡張子の設定を新しいものに変換 */\r
745                                 ReadStringFromReg(hKey4, "Ascii", Str, ASCII_EXT_LEN+1);\r
746                                 memset(AsciiExt, NUL, ASCII_EXT_LEN+1);\r
747                                 Pos = Str;\r
748                                 while(*Pos != NUL)\r
749                                 {\r
750                                         if((Pos2 = strchr(Pos, ';')) == NULL)\r
751                                                 Pos2 = strchr(Pos, NUL);\r
752                                         if((Pos2 - Pos) > 0)\r
753                                         {\r
754                                                 if((StrMultiLen(AsciiExt) + (Pos2 - Pos) + 2) >= ASCII_EXT_LEN)\r
755                                                         break;\r
756                                                 strcpy(AsciiExt + StrMultiLen(AsciiExt), "*.");\r
757                                                 strncpy(AsciiExt + StrMultiLen(AsciiExt) - 1, Pos, (Pos2 - Pos));\r
758                                         }\r
759                                         Pos = Pos2;\r
760                                         if(*Pos == ';')\r
761                                                 Pos++;\r
762                                 }\r
763                         }\r
764 \r
765                         ReadIntValueFromReg(hKey4, "LowUp", &FnameCnv);\r
766                         ReadIntValueFromReg(hKey4, "Tout", &TimeOut);\r
767 \r
768                         ReadMultiStringFromReg(hKey4, "NoTrn", MirrorNoTrn, MIRROR_LEN+1);\r
769                         ReadMultiStringFromReg(hKey4, "NoDel", MirrorNoDel, MIRROR_LEN+1);\r
770                         ReadIntValueFromReg(hKey4, "MirFile", &MirrorFnameCnv);\r
771                         ReadIntValueFromReg(hKey4, "MirUNot", &MirUpDelNotify);\r
772                         ReadIntValueFromReg(hKey4, "MirDNot", &MirDownDelNotify);\r
773 \r
774                         if(ReadStringFromReg(hKey4, "ListFont", Str, 256) == FFFTP_SUCCESS)\r
775                         {\r
776                                 if(RestoreFontData(Str, &ListLogFont) == FFFTP_SUCCESS)\r
777                                         ListFont = CreateFontIndirect(&ListLogFont);\r
778                         }\r
779                         ReadIntValueFromReg(hKey4, "ListHide", &DispIgnoreHide);\r
780                         ReadIntValueFromReg(hKey4, "ListDrv", &DispDrives);\r
781 \r
782                         ReadStringFromReg(hKey4, "FwallHost", FwallHost, HOST_ADRS_LEN+1);\r
783                         ReadStringFromReg(hKey4, "FwallUser", FwallUser, USER_NAME_LEN+1);\r
784                         ReadStringFromReg(hKey4, "FwallPass", Str, 255);\r
785                         DecodePassword(Str, FwallPass);\r
786                         ReadIntValueFromReg(hKey4, "FwallPort", &FwallPort);\r
787                         ReadIntValueFromReg(hKey4, "FwallType", &FwallType);\r
788                         ReadIntValueFromReg(hKey4, "FwallDef", &FwallDefault);\r
789                         ReadIntValueFromReg(hKey4, "FwallSec", &FwallSecurity);\r
790                         ReadIntValueFromReg(hKey4, "PasvDef", &PasvDefault);\r
791                         ReadIntValueFromReg(hKey4, "FwallRes", &FwallResolv);\r
792                         ReadIntValueFromReg(hKey4, "FwallLow", &FwallLower);\r
793                         ReadIntValueFromReg(hKey4, "FwallDel", &FwallDelimiter);\r
794 \r
795                         ReadIntValueFromReg(hKey4, "SndConSw", &Sound[SND_CONNECT].On);\r
796                         ReadIntValueFromReg(hKey4, "SndTrnSw", &Sound[SND_TRANS].On);\r
797                         ReadIntValueFromReg(hKey4, "SndErrSw", &Sound[SND_ERROR].On);\r
798                         ReadStringFromReg(hKey4, "SndCon", Sound[SND_CONNECT].Fname, FMAX_PATH+1);\r
799                         ReadStringFromReg(hKey4, "SndTrn", Sound[SND_TRANS].Fname, FMAX_PATH+1);\r
800                         ReadStringFromReg(hKey4, "SndErr", Sound[SND_ERROR].Fname, FMAX_PATH+1);\r
801 \r
802                         ReadMultiStringFromReg(hKey4, "DefAttr", DefAttrList, DEFATTRLIST_LEN+1);\r
803 \r
804                         ReadStringFromReg(hKey4, "Tmp", TmpPath, FMAX_PATH+1);\r
805 \r
806                         ReadBinaryFromReg(hKey4, "Hdlg", &HostDlgSize, sizeof(SIZE));\r
807                         ReadBinaryFromReg(hKey4, "Bdlg", &BmarkDlgSize, sizeof(SIZE));\r
808                         ReadBinaryFromReg(hKey4, "Mdlg", &MirrorDlgSize, sizeof(SIZE));\r
809 \r
810                         ReadIntValueFromReg(hKey4, "FAttrSw", &FolderAttr);\r
811                         ReadIntValueFromReg(hKey4, "FAttr", &FolderAttrNum);\r
812 \r
813                         ReadIntValueFromReg(hKey4, "NoSave", &SuppressSave);\r
814 \r
815                         ReadIntValueFromReg(hKey4, "HistNum", &FileHist);\r
816 //                      ReadMultiStringFromReg(hKey4, "Hist", Hist, (FMAX_PATH+1)*HISTORY_MAX+1);\r
817 \r
818                         /* ヒストリの設定を読み込む */\r
819                         Sets = 0;\r
820                         ReadIntValueFromReg(hKey4, "SavedHist", &Sets);\r
821 \r
822                         for(i = 0; i < Sets; i++)\r
823                         {\r
824                                 sprintf(Str, "History%d", i);\r
825                                 if(OpenSubKey(hKey4, Str, &hKey5) == FFFTP_SUCCESS)\r
826                                 {\r
827                                         CopyDefaultHistory(&Hist);\r
828 \r
829                                         ReadStringFromReg(hKey5, "HostAdrs", Hist.HostAdrs, HOST_ADRS_LEN+1);\r
830                                         ReadStringFromReg(hKey5, "UserName", Hist.UserName, USER_NAME_LEN+1);\r
831                                         ReadStringFromReg(hKey5, "Account", Hist.Account, ACCOUNT_LEN+1);\r
832                                         ReadStringFromReg(hKey5, "LocalDir", Hist.LocalInitDir, INIT_DIR_LEN+1);\r
833                                         ReadStringFromReg(hKey5, "RemoteDir", Hist.RemoteInitDir, INIT_DIR_LEN+1);\r
834                                         ReadStringFromReg(hKey5, "Chmod", Hist.ChmodCmd, CHMOD_CMD_LEN+1);\r
835                                         ReadStringFromReg(hKey5, "Nlst", Hist.LsName, NLST_NAME_LEN+1);\r
836                                         ReadStringFromReg(hKey5, "Init", Hist.InitCmd, INITCMD_LEN+1);\r
837                                         ReadIntValueFromReg(hKey5, "Port", &Hist.Port);\r
838                                         ReadIntValueFromReg(hKey5, "Kanji", &Hist.KanjiCode);\r
839                                         ReadIntValueFromReg(hKey5, "KanaCnv", &Hist.KanaCnv);\r
840                                         ReadIntValueFromReg(hKey5, "NameKanji", &Hist.NameKanjiCode);\r
841                                         ReadIntValueFromReg(hKey5, "NameKana", &Hist.NameKanaCnv);\r
842                                         ReadIntValueFromReg(hKey5, "Pasv", &Hist.Pasv);\r
843                                         ReadIntValueFromReg(hKey5, "Fwall", &Hist.FireWall);\r
844                                         ReadIntValueFromReg(hKey5, "List", &Hist.ListCmdOnly);\r
845                                         ReadIntValueFromReg(hKey5, "NLST-R", &Hist.UseNLST_R);\r
846                                         ReadIntValueFromReg(hKey5, "Tzone", &Hist.TimeZone);\r
847                                         ReadIntValueFromReg(hKey5, "Type", &Hist.HostType);\r
848                                         ReadIntValueFromReg(hKey5, "Sync", &Hist.SyncMove);\r
849                                         ReadIntValueFromReg(hKey5, "Fpath", &Hist.NoFullPath);\r
850                                         ReadBinaryFromReg(hKey5, "Sort", &Hist.Sort, sizeof(Hist.Sort));\r
851                                         ReadIntValueFromReg(hKey5, "Secu", &Hist.Security);\r
852                                         ReadIntValueFromReg(hKey5, "TrType", &Hist.Type);\r
853                                         strcpy(Str, "");\r
854                                         ReadStringFromReg(hKey5, "Password", Str, 255);\r
855                                         DecodePassword(Str, Hist.PassWord);\r
856                                         ReadIntValueFromReg(hKey5, "Dial", &Hist.Dialup);\r
857                                         ReadIntValueFromReg(hKey5, "UseIt", &Hist.DialupAlways);\r
858                                         ReadIntValueFromReg(hKey5, "Notify", &Hist.DialupNotify);\r
859                                         ReadStringFromReg(hKey5, "DialTo", Hist.DialEntry, RAS_NAME_LEN+1);\r
860                                         // 暗号化通信対応\r
861                                         ReadIntValueFromReg(hKey5, "NoEncryption", &Hist.UseNoEncryption);\r
862                                         ReadIntValueFromReg(hKey5, "FTPES", &Hist.UseFTPES);\r
863                                         ReadIntValueFromReg(hKey5, "FTPIS", &Hist.UseFTPIS);\r
864                                         ReadIntValueFromReg(hKey5, "SFTP", &Hist.UseSFTP);\r
865                                         strcpy(Str, "");\r
866                                         ReadStringFromReg(hKey5, "PKey", Str, PRIVATE_KEY_LEN*4+1);\r
867                                         DecodePassword(Str, Hist.PrivateKey);\r
868                                         // 同時接続対応\r
869                                         ReadIntValueFromReg(hKey5, "ThreadCount", &Hist.MaxThreadCount);\r
870                                         ReadIntValueFromReg(hKey5, "ReuseCmdSkt", &Hist.ReuseCmdSkt);\r
871                                         // MLSD対応\r
872                                         ReadIntValueFromReg(hKey5, "MLSD", &Hist.UseMLSD);\r
873                                         // IPv6対応\r
874                                         ReadIntValueFromReg(hKey5, "NetType", &Hist.NetType);\r
875                                         // 自動切断対策\r
876                                         ReadIntValueFromReg(hKey5, "Noop", &Hist.NoopInterval);\r
877                                         // 再転送対応\r
878                                         ReadIntValueFromReg(hKey5, "ErrMode", &Hist.TransferErrorMode);\r
879                                         ReadIntValueFromReg(hKey5, "ErrNotify", &Hist.TransferErrorNotify);\r
880 \r
881                                         CloseSubKey(hKey5);\r
882                                         AddHistoryToHistory(&Hist);\r
883                                 }\r
884                         }\r
885 \r
886                         /* ホストの設定を読み込む */\r
887                         Sets = 0;\r
888                         ReadIntValueFromReg(hKey4, "SetNum", &Sets);\r
889 \r
890                         for(i = 0; i < Sets; i++)\r
891                         {\r
892                                 sprintf(Str, "Host%d", i);\r
893                                 if(OpenSubKey(hKey4, Str, &hKey5) == FFFTP_SUCCESS)\r
894                                 {\r
895                                         CopyDefaultHost(&Host);\r
896                                         /* 下位互換性のため */\r
897                                         // SourceForge.JPによるフォーク\r
898 //                                      if(Version < VER_NUM)\r
899                                         if(Version < 1921)\r
900                                         {\r
901                                                 Host.Pasv = NO;\r
902                                                 Host.ListCmdOnly = NO;\r
903                                         }\r
904                                         // 1.97b以前はデフォルトでShift_JIS\r
905                                         if(Version < 1980)\r
906                                                 Host.NameKanjiCode = KANJI_SJIS;\r
907                                         ReadIntValueFromReg(hKey5, "Set", &Host.Level);\r
908 \r
909                                         ReadStringFromReg(hKey5, "HostName", Host.HostName, HOST_NAME_LEN+1);\r
910                                         ReadStringFromReg(hKey5, "HostAdrs", Host.HostAdrs, HOST_ADRS_LEN+1);\r
911                                         ReadStringFromReg(hKey5, "UserName", Host.UserName, USER_NAME_LEN+1);\r
912                                         ReadStringFromReg(hKey5, "Account", Host.Account, ACCOUNT_LEN+1);\r
913                                         ReadStringFromReg(hKey5, "LocalDir", Host.LocalInitDir, INIT_DIR_LEN+1);\r
914                                         ReadStringFromReg(hKey5, "RemoteDir", Host.RemoteInitDir, INIT_DIR_LEN+1);\r
915                                         ReadStringFromReg(hKey5, "Chmod", Host.ChmodCmd, CHMOD_CMD_LEN+1);\r
916                                         ReadStringFromReg(hKey5, "Nlst", Host.LsName, NLST_NAME_LEN+1);\r
917                                         ReadStringFromReg(hKey5, "Init", Host.InitCmd, INITCMD_LEN+1);\r
918                                         ReadIntValueFromReg(hKey5, "Port", &Host.Port);\r
919                                         ReadIntValueFromReg(hKey5, "Anonymous", &Host.Anonymous);\r
920                                         ReadIntValueFromReg(hKey5, "Kanji", &Host.KanjiCode);\r
921                                         // 1.98b以前のUTF-8はBOMあり\r
922                                         if(Version < 1983)\r
923                                         {\r
924                                                 if(Host.KanjiCode == KANJI_UTF8N)\r
925                                                         Host.KanjiCode = KANJI_UTF8BOM;\r
926                                         }\r
927                                         ReadIntValueFromReg(hKey5, "KanaCnv", &Host.KanaCnv);\r
928                                         ReadIntValueFromReg(hKey5, "NameKanji", &Host.NameKanjiCode);\r
929                                         ReadIntValueFromReg(hKey5, "NameKana", &Host.NameKanaCnv);\r
930                                         ReadIntValueFromReg(hKey5, "Pasv", &Host.Pasv);\r
931                                         ReadIntValueFromReg(hKey5, "Fwall", &Host.FireWall);\r
932                                         ReadIntValueFromReg(hKey5, "List", &Host.ListCmdOnly);\r
933                                         ReadIntValueFromReg(hKey5, "NLST-R", &Host.UseNLST_R);\r
934                                         ReadIntValueFromReg(hKey5, "Last", &Host.LastDir);\r
935                                         ReadIntValueFromReg(hKey5, "Tzone", &Host.TimeZone);\r
936                                         ReadIntValueFromReg(hKey5, "Type", &Host.HostType);\r
937                                         ReadIntValueFromReg(hKey5, "Sync", &Host.SyncMove);\r
938                                         ReadIntValueFromReg(hKey5, "Fpath", &Host.NoFullPath);\r
939                                         ReadBinaryFromReg(hKey5, "Sort", &Host.Sort, sizeof(Host.Sort));\r
940                                         ReadIntValueFromReg(hKey5, "Secu", &Host.Security);\r
941                                         if(Host.Anonymous != YES)\r
942                                         {\r
943                                                 strcpy(Str, "");\r
944                                                 ReadStringFromReg(hKey5, "Password", Str, 255);\r
945                                                 DecodePassword(Str, Host.PassWord);\r
946                                         }\r
947                                         else\r
948                                                 strcpy(Host.PassWord, UserMailAdrs);\r
949 \r
950                                         ReadMultiStringFromReg(hKey5, "Bmarks", Host.BookMark, BOOKMARK_SIZE);\r
951 \r
952                                         ReadIntValueFromReg(hKey5, "Dial", &Host.Dialup);\r
953                                         ReadIntValueFromReg(hKey5, "UseIt", &Host.DialupAlways);\r
954                                         ReadIntValueFromReg(hKey5, "Notify", &Host.DialupNotify);\r
955                                         ReadStringFromReg(hKey5, "DialTo", Host.DialEntry, RAS_NAME_LEN+1);\r
956                                         // 暗号化通信対応\r
957                                         ReadIntValueFromReg(hKey5, "NoEncryption", &Host.UseNoEncryption);\r
958                                         ReadIntValueFromReg(hKey5, "FTPES", &Host.UseFTPES);\r
959                                         ReadIntValueFromReg(hKey5, "FTPIS", &Host.UseFTPIS);\r
960                                         ReadIntValueFromReg(hKey5, "SFTP", &Host.UseSFTP);\r
961                                         strcpy(Str, "");\r
962                                         ReadStringFromReg(hKey5, "PKey", Str, PRIVATE_KEY_LEN*4+1);\r
963                                         DecodePassword(Str, Host.PrivateKey);\r
964                                         // 同時接続対応\r
965                                         ReadIntValueFromReg(hKey5, "ThreadCount", &Host.MaxThreadCount);\r
966                                         ReadIntValueFromReg(hKey5, "ReuseCmdSkt", &Host.ReuseCmdSkt);\r
967                                         // 1.98d以前で同時接続数が1より大きい場合はソケットの再利用なし\r
968                                         if(Version < 1985)\r
969                                         {\r
970                                                 if(Host.MaxThreadCount > 1)\r
971                                                         Host.ReuseCmdSkt = NO;\r
972                                         }\r
973                                         // MLSD対応\r
974                                         ReadIntValueFromReg(hKey5, "MLSD", &Host.UseMLSD);\r
975                                         // IPv6対応\r
976                                         ReadIntValueFromReg(hKey5, "NetType", &Host.NetType);\r
977                                         // 自動切断対策\r
978                                         ReadIntValueFromReg(hKey5, "Noop", &Host.NoopInterval);\r
979                                         // 再転送対応\r
980                                         ReadIntValueFromReg(hKey5, "ErrMode", &Host.TransferErrorMode);\r
981                                         ReadIntValueFromReg(hKey5, "ErrNotify", &Host.TransferErrorNotify);\r
982 \r
983                                         CloseSubKey(hKey5);\r
984 \r
985                                         AddHostToList(&Host, -1, Host.Level);\r
986                                 }\r
987                         }\r
988 \r
989                         ReadIntValueFromReg(hKey4, "CurSet", &Sets);\r
990                         SetCurrentHost(Sets);\r
991 \r
992                         // 暗号化通信対応\r
993                         ReadBinaryFromReg(hKey4, "CertCacheHash", &CertificateCacheHash, sizeof(CertificateCacheHash));\r
994                         ReadStringFromReg(hKey4, "RootCertHash", Str, PRIVATE_KEY_LEN*4+1);\r
995                         DecodePassword(Str, Buf);\r
996                         StrReadIn(Buf, sizeof(SSLRootCAFileHash), (char*)&SSLRootCAFileHash);\r
997                         // ファイルアイコン表示対応\r
998                         ReadIntValueFromReg(hKey4, "ListIcon", &DispFileIcon);\r
999                         // ディレクトリ自動作成\r
1000                         ReadIntValueFromReg(hKey4, "MakeDir", &MakeAllDir);\r
1001 \r
1002                         CloseSubKey(hKey4);\r
1003                 }\r
1004                 CloseReg(hKey3);\r
1005         }\r
1006         else\r
1007         {\r
1008                 /*===== 最初の起動時(設定が無い) =====*/\r
1009 \r
1010 #if 0\r
1011                 strcpy(UserMailAdrs, "");\r
1012                 strcpy(Str, "");\r
1013                 if(InputDialogBox(mailadrs_dlg, HWND_DESKTOP, NULL, Str, USER_MAIL_LEN+1, &i, IDH_HELP_TOPIC_0000001) == YES)\r
1014                         strcpy(UserMailAdrs, Str);\r
1015 \r
1016                 for(i = 0; i < SAMPLE_HOSTS; i++)\r
1017                 {\r
1018                         CopyDefaultHost(&Host);\r
1019                         Host.Level = Sample[i].Level;\r
1020                         strcpy(Host.PassWord, UserMailAdrs);\r
1021                         strcpy(Host.HostName, Sample[i].HostName);\r
1022                         strcpy(Host.HostAdrs, Sample[i].HostAdrs);\r
1023                         strcpy(Host.UserName, "anonymous");\r
1024                         AddHostToList(&Host, -1, Host.Level);\r
1025                 }\r
1026 #endif\r
1027         }\r
1028         return(Sts);\r
1029 }\r
1030 \r
1031 \r
1032 /*----- 隠しドライブ情報を取得 ------------------------------------------------\r
1033 *\r
1034 *       Parameter\r
1035 *               なし\r
1036 *\r
1037 *       Return Value\r
1038 *               DWORD \r
1039 *                       YES/NO=設定無し\r
1040 *----------------------------------------------------------------------------*/\r
1041 \r
1042 DWORD LoadHideDriveListRegistry(void)\r
1043 {\r
1044         HKEY hKey1;\r
1045         HKEY hKey2;\r
1046         HKEY hKey3;\r
1047         HKEY hKey4;\r
1048         HKEY hKey5;\r
1049         HKEY hKey6;\r
1050         DWORD Size;\r
1051         DWORD Type;\r
1052         DWORD Ret;\r
1053 \r
1054         Ret = 0;\r
1055         if(RegOpenKeyEx(HKEY_CURRENT_USER, "Software", 0, KEY_READ, &hKey1) == ERROR_SUCCESS)\r
1056         {\r
1057                 if(RegOpenKeyEx(hKey1, "Microsoft", 0, KEY_READ, &hKey2) == ERROR_SUCCESS)\r
1058                 {\r
1059                         if(RegOpenKeyEx(hKey2, "Windows", 0, KEY_READ, &hKey3) == ERROR_SUCCESS)\r
1060                         {\r
1061                                 if(RegOpenKeyEx(hKey3, "CurrentVersion", 0, KEY_READ, &hKey4) == ERROR_SUCCESS)\r
1062                                 {\r
1063                                         if(RegOpenKeyEx(hKey4, "Policies", 0, KEY_READ, &hKey5) == ERROR_SUCCESS)\r
1064                                         {\r
1065                                                 if(RegOpenKeyEx(hKey5, "Explorer", 0, KEY_READ, &hKey6) == ERROR_SUCCESS)\r
1066                                                 {\r
1067                                                         Size = sizeof(DWORD);\r
1068                                                         RegQueryValueEx(hKey6, "NoDrives", NULL, &Type, (BYTE *)&Ret, &Size);\r
1069                                                         RegCloseKey(hKey6);\r
1070                                                 }\r
1071                                                 RegCloseKey(hKey5);\r
1072                                         }\r
1073                                         RegCloseKey(hKey4);\r
1074                                 }\r
1075                                 RegCloseKey(hKey3);\r
1076                         }\r
1077                         RegCloseKey(hKey2);\r
1078                 }\r
1079                 RegCloseKey(hKey1);\r
1080         }\r
1081         return(Ret);\r
1082 }\r
1083 \r
1084 \r
1085 /*----- レジストリの設定値をクリア --------------------------------------------\r
1086 *\r
1087 *       Parameter\r
1088 *               なし\r
1089 *\r
1090 *       Return Value\r
1091 *               なし\r
1092 *----------------------------------------------------------------------------*/\r
1093 \r
1094 void ClearRegistry(void)\r
1095 {\r
1096         HKEY hKey2;\r
1097         HKEY hKey3;\r
1098         HKEY hKey4;\r
1099         DWORD Dispos;\r
1100         char Str[20];\r
1101         int i;\r
1102 \r
1103         if(RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\Sota", 0, "", REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, NULL, &hKey2, &Dispos) == ERROR_SUCCESS)\r
1104         {\r
1105                 if(RegCreateKeyEx(hKey2, "FFFTP", 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey3, &Dispos) == ERROR_SUCCESS)\r
1106                 {\r
1107                         if(RegCreateKeyEx(hKey3, "Options", 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey4, &Dispos) == ERROR_SUCCESS)\r
1108                         {\r
1109                                 for(i = 0; ; i++)\r
1110                                 {\r
1111                                         sprintf(Str, "Host%d", i);\r
1112                                         if(RegDeleteKey(hKey4, Str) != ERROR_SUCCESS)\r
1113                                                 break;\r
1114                                 }\r
1115                                 for(i = 0; ; i++)\r
1116                                 {\r
1117                                         sprintf(Str, "History%d", i);\r
1118                                         if(RegDeleteKey(hKey4, Str) != ERROR_SUCCESS)\r
1119                                                 break;\r
1120                                 }\r
1121                                 RegCloseKey(hKey4);\r
1122                         }\r
1123                         RegDeleteKey(hKey3, "Options");\r
1124                         RegCloseKey(hKey3);\r
1125                 }\r
1126                 RegDeleteKey(hKey2, "FFFTP");\r
1127                 RegCloseKey(hKey2);\r
1128         }\r
1129         return;\r
1130 }\r
1131 \r
1132 \r
1133 /*----- 設定をファイルに保存 --------------------------------------------------\r
1134 *\r
1135 *       Parameter\r
1136 *               なし\r
1137 *\r
1138 *       Return Value\r
1139 *               なし\r
1140 *----------------------------------------------------------------------------*/\r
1141 \r
1142 void SaveSettingsToFile(void)\r
1143 {\r
1144         char Tmp[FMAX_PATH*2];\r
1145         char Fname[FMAX_PATH+1];\r
1146         // 任意のコードが実行されるバグ修正\r
1147         char CurDir[FMAX_PATH+1];\r
1148         char SysDir[FMAX_PATH+1];\r
1149 \r
1150         if(RegType == REGTYPE_REG)\r
1151         {\r
1152                 strcpy(Fname, "FFFTP.reg");\r
1153                 if(SelectFile(GetMainHwnd(), Fname, MSGJPN286, MSGJPN287, "reg", OFN_EXTENSIONDIFFERENT | OFN_OVERWRITEPROMPT, 1) == TRUE)\r
1154                 {\r
1155                         sprintf(Tmp, "/e \x22%s\x22 HKEY_CURRENT_USER\\Software\\sota\\FFFTP", Fname);\r
1156                         // 任意のコードが実行されるバグ修正\r
1157 //                      if(ShellExecute(NULL, "open", "regedit", Tmp, ".", SW_SHOW) <= (HINSTANCE)32)\r
1158 //                      {\r
1159 //                              MessageBox(NULL, MSGJPN285, "FFFTP", MB_OK);\r
1160 //                      }\r
1161                         if(GetCurrentDirectory(FMAX_PATH, CurDir) > 0)\r
1162                         {\r
1163                                 if(GetSystemDirectory(SysDir, FMAX_PATH) > 0)\r
1164                                 {\r
1165                                         if(SetCurrentDirectory(SysDir))\r
1166                                         {\r
1167                                                 if(ShellExecute(NULL, "open", "regedit", Tmp, NULL, SW_SHOW) <= (HINSTANCE)32)\r
1168                                                 {\r
1169                                                         MessageBox(NULL, MSGJPN285, "FFFTP", MB_OK);\r
1170                                                 }\r
1171                                                 SetCurrentDirectory(CurDir);\r
1172                                         }\r
1173                                 }\r
1174                         }\r
1175                 }\r
1176         }\r
1177         else\r
1178         {\r
1179                 strcpy(Fname, "FFFTP-Backup.ini");\r
1180                 if(SelectFile(GetMainHwnd(), Fname, MSGJPN286, MSGJPN288, "ini", OFN_EXTENSIONDIFFERENT | OFN_OVERWRITEPROMPT, 1) == TRUE)\r
1181                 {\r
1182                         CopyFile(AskIniFilePath(), Fname, FALSE);\r
1183                 }\r
1184         }\r
1185         return;\r
1186 }\r
1187 \r
1188 \r
1189 /*----- 設定をファイルから復元 ------------------------------------------------\r
1190 *\r
1191 *       Parameter\r
1192 *               なし\r
1193 *\r
1194 *       Return Value\r
1195 *               int     ロードしたかどうか (YES/NO)\r
1196 *----------------------------------------------------------------------------*/\r
1197 \r
1198 int LoadSettingsFromFile(void)\r
1199 {\r
1200         int Ret;\r
1201         char Tmp[FMAX_PATH*2];\r
1202         char Fname[FMAX_PATH+1];\r
1203         // 任意のコードが実行されるバグ修正\r
1204         char CurDir[FMAX_PATH+1];\r
1205         char SysDir[FMAX_PATH+1];\r
1206 \r
1207         Ret = NO;\r
1208         strcpy(Fname, "");\r
1209         if(SelectFile(GetMainHwnd(), Fname, MSGJPN291, MSGJPN290, "", OFN_FILEMUSTEXIST, 0) == TRUE)\r
1210         {\r
1211                 if((strlen(Fname) >= 5) && (_stricmp(&Fname[strlen(Fname)-4], ".reg") == 0))\r
1212                 {\r
1213                         sprintf(Tmp, "\x22%s\x22", Fname);\r
1214                         // 任意のコードが実行されるバグ修正\r
1215 //                      if(ShellExecute(NULL, "open", "regedit", Tmp, ".", SW_SHOW) <= (HINSTANCE)32)\r
1216 //                      {\r
1217 //                              MessageBox(NULL, MSGJPN285, "FFFTP", MB_OK);\r
1218 //                      }\r
1219 //                      else\r
1220 //                      {\r
1221 //                              Ret = YES;\r
1222 //                              /* レジストリエディタが終了するのを待つ */\r
1223 //                              WaitForSingleObject(Info.hProcess, INFINITE);\r
1224 //                      }\r
1225                         if(GetCurrentDirectory(FMAX_PATH, CurDir) > 0)\r
1226                         {\r
1227                                 if(GetSystemDirectory(SysDir, FMAX_PATH) > 0)\r
1228                                 {\r
1229                                         if(SetCurrentDirectory(SysDir))\r
1230                                         {\r
1231                                                 if(ShellExecute(NULL, "open", "regedit", Tmp, NULL, SW_SHOW) <= (HINSTANCE)32)\r
1232                                                 {\r
1233                                                         MessageBox(NULL, MSGJPN285, "FFFTP", MB_OK);\r
1234                                                 }\r
1235                                                 else\r
1236                                                 {\r
1237                                                         Ret = YES;\r
1238                                                         /* レジストリエディタが終了するのを待つ */\r
1239 //                                                      WaitForSingleObject(Info.hProcess, INFINITE);\r
1240                                                 }\r
1241                                                 SetCurrentDirectory(CurDir);\r
1242                                         }\r
1243                                 }\r
1244                         }\r
1245                 }\r
1246                 else if((strlen(Fname) >= 5) && (_stricmp(&Fname[strlen(Fname)-4], ".ini") == 0))\r
1247                 {\r
1248                         CopyFile(Fname, AskIniFilePath(), FALSE);\r
1249                         Ret = YES;\r
1250                 }\r
1251                 else\r
1252                         MessageBox(NULL, MSGJPN293, "FFFTP", MB_OK);\r
1253         }\r
1254         return(Ret);\r
1255 }\r
1256 \r
1257 \r
1258 \r
1259 \r
1260 /*----- レジストリ/INIファイルに文字列をセーブ --------------------------------\r
1261 *\r
1262 *       Parameter\r
1263 *               HKEY hKey : レジストリキー\r
1264 *               char *Key : キー名\r
1265 *               char *Str : セーブする文字列\r
1266 *               char *DefaultStr : デフォルトの文字列\r
1267 *\r
1268 *       Return Value\r
1269 *               なし\r
1270 *\r
1271 *       Note\r
1272 *               文字列がデフォルトの文字列と同じならセーブしない\r
1273 *----------------------------------------------------------------------------*/\r
1274 \r
1275 static void SaveStr(HKEY hKey, char *Key, char *Str, char *DefaultStr)\r
1276 {\r
1277         if((DefaultStr != NULL) && (strcmp(Str, DefaultStr) == 0))\r
1278                 DeleteValue(hKey, Key);\r
1279         else\r
1280                 WriteStringToReg(hKey, Key, Str);\r
1281 \r
1282         return;\r
1283 }\r
1284 \r
1285 \r
1286 /*----- レジストリ/INIファイルに数値(INT)をセーブ -----------------------------\r
1287 *\r
1288 *       Parameter\r
1289 *               HKEY hKey : レジストリキー\r
1290 *               char *Key : キー名\r
1291 *               int Num : セーブする値\r
1292 *               int DefaultNum : デフォルトの値\r
1293 *\r
1294 *       Return Value\r
1295 *               なし\r
1296 *\r
1297 *       Note\r
1298 *               数値がデフォルトの値と同じならセーブしない\r
1299 *----------------------------------------------------------------------------*/\r
1300 \r
1301 static void SaveIntNum(HKEY hKey, char *Key, int Num, int DefaultNum)\r
1302 {\r
1303         if(Num == DefaultNum)\r
1304                 DeleteValue(hKey, Key);\r
1305         else\r
1306                 WriteIntValueToReg(hKey, Key, Num);\r
1307 \r
1308         return;\r
1309 }\r
1310 \r
1311 \r
1312 /*----- LOGFONTデータを文字列に変換する ---------------------------------------\r
1313 *\r
1314 *       Parameter\r
1315 *               LOGFONT Font : フォントデータ\r
1316 *               HFONT hFont : フォントのハンドル\r
1317 *                       NULL = デフォルトのフォント\r
1318 *               char *Buf : バッファ\r
1319 *\r
1320 *       Return Value\r
1321 *               なし\r
1322 *----------------------------------------------------------------------------*/\r
1323 \r
1324 static void MakeFontData(LOGFONT Font, HFONT hFont, char *Buf)\r
1325 {\r
1326         *Buf = NUL;\r
1327         if(hFont != NULL)\r
1328                 sprintf(Buf, "%d %d %d %d %d %d %d %d %d %d %d %d %d %s",\r
1329                         Font.lfHeight, Font.lfWidth, Font.lfEscapement, Font.lfOrientation,\r
1330                         Font.lfWeight, Font.lfItalic, Font.lfUnderline, Font.lfStrikeOut,\r
1331                         Font.lfCharSet, Font.lfOutPrecision, Font.lfClipPrecision,\r
1332                         Font.lfQuality, Font.lfPitchAndFamily, Font.lfFaceName);\r
1333         return;\r
1334 }\r
1335 \r
1336 \r
1337 /*----- 文字列をLOGFONTデータに変換する ---------------------------------------\r
1338 *\r
1339 *       Parameter\r
1340 *               char *Str : 文字列\r
1341 *               LOGFONT *Font : フォントデータ\r
1342 *\r
1343 *       Return Value\r
1344 *               int ステータス\r
1345 *                       FFFTP_SUCCESS/FFFTP_FAIL=変換できない\r
1346 *----------------------------------------------------------------------------*/\r
1347 \r
1348 static int RestoreFontData(char *Str, LOGFONT *Font)\r
1349 {\r
1350         int i;\r
1351         int Sts;\r
1352 \r
1353         Sts = FFFTP_FAIL;\r
1354         if(sscanf(Str, "%d %d %d %d %d %d %d %d %d %d %d %d %d",\r
1355                         &(Font->lfHeight), &(Font->lfWidth), &(Font->lfEscapement), &(Font->lfOrientation),\r
1356                         &(Font->lfWeight), &(Font->lfItalic), &(Font->lfUnderline), &(Font->lfStrikeOut),\r
1357                         &(Font->lfCharSet), &(Font->lfOutPrecision), &(Font->lfClipPrecision),\r
1358                         &(Font->lfQuality), &(Font->lfPitchAndFamily)) == 13)\r
1359         {\r
1360                 for(i = 13; i > 0; i--)\r
1361                 {\r
1362                         if((Str = strchr(Str, ' ')) == NULL)\r
1363                                 break;\r
1364                         Str++;\r
1365                 }\r
1366                 if(i == 0)\r
1367                 {\r
1368                         strcpy(Font->lfFaceName, Str);\r
1369                         Sts = FFFTP_SUCCESS;\r
1370                 }\r
1371         }\r
1372 \r
1373         if(Sts == FFFTP_FAIL)\r
1374                 memset(Font, NUL, sizeof(LOGFONT));\r
1375 \r
1376         return(Sts);\r
1377 }\r
1378 \r
1379 /*----- パスワードを暗号化する ------------------------------------------------\r
1380 *\r
1381 *       Parameter\r
1382 *               char *Str : パスワード\r
1383 *               char *Buf : 暗号化したパスワードを格納するバッファ\r
1384 *\r
1385 *       Return Value\r
1386 *               なし\r
1387 *----------------------------------------------------------------------------*/\r
1388 static void EncodePassword(char *Str, char *Buf)\r
1389 {\r
1390         EncodePassword3( Str, Buf, SecretKey );\r
1391 }\r
1392 \r
1393 /*----- パスワードを暗号化する(オリジナルアルゴリズム)  ------------------\r
1394 *\r
1395 *       Parameter\r
1396 *               char *Str : パスワード\r
1397 *               char *Buf : 暗号化したパスワードを格納するバッファ\r
1398 *\r
1399 *       Return Value\r
1400 *               なし\r
1401 *----------------------------------------------------------------------------*/\r
1402 \r
1403 static void EncodePasswordOriginal(char *Str, char *Buf)\r
1404 {\r
1405         unsigned char *Get;\r
1406         unsigned char *Put;\r
1407         int Rnd;\r
1408         int Ch;\r
1409 \r
1410         srand((unsigned)time(NULL));\r
1411 \r
1412         Get = (unsigned char *)Str;\r
1413         Put = (unsigned char *)Buf;\r
1414         \r
1415         if( *Get == NUL ){\r
1416                 *Put = NUL;\r
1417                 return;\r
1418         }\r
1419 \r
1420         /* 識別子を先頭に置く */\r
1421         Put[0] = '0';\r
1422         Put[1] = 'A';\r
1423         Put += 2;\r
1424 \r
1425         while(*Get != NUL)\r
1426         {\r
1427                 Rnd = rand() % 3;\r
1428                 Ch = ((int)*Get++) << Rnd;\r
1429                 Ch = (unsigned char)Ch | (unsigned char)(Ch >> 8);\r
1430                 *Put++ = 0x40 | ((Rnd & 0x3) << 4) | (Ch & 0xF);\r
1431                 *Put++ = 0x40 | ((Ch >> 4) & 0xF);\r
1432                 if((*(Put-2) & 0x1) != 0)\r
1433                         *Put++ = (rand() % 62) + 0x40;\r
1434         }\r
1435         *Put = NUL;\r
1436         return;\r
1437 }\r
1438 \r
1439 /*----- パスワードを暗号化する(オリジナルアルゴリズム^Key)  ----------------\r
1440 *\r
1441 *       Parameter\r
1442 *               char *Str : パスワード\r
1443 *               char *Buf : 暗号化したパスワードを格納するバッファ\r
1444 *               const char *Key : 暗号化キー\r
1445 *\r
1446 *       Return Value\r
1447 *               なし\r
1448 *----------------------------------------------------------------------------*/\r
1449 \r
1450 static void EncodePassword2(char *Str, char *Buf, const char* Key)\r
1451 {\r
1452         unsigned char *Get;\r
1453         unsigned char *Put;\r
1454         int Rnd;\r
1455         int Ch;\r
1456 \r
1457         /* 2010.01.31 genta Key */\r
1458         unsigned char *KeyHead = (unsigned char *)Key;\r
1459         unsigned char *KeyEnd = KeyHead + strlen(KeyHead);\r
1460         unsigned char *KeyCurrent = KeyHead;\r
1461 \r
1462         srand((unsigned)time(NULL));\r
1463 \r
1464         Get = (unsigned char *)Str;\r
1465         Put = (unsigned char *)Buf;\r
1466         \r
1467         if( *Get == NUL ){\r
1468                 *Put = NUL;\r
1469                 return;\r
1470         }\r
1471 \r
1472         /* 識別子を先頭に置く */\r
1473         Put[0] = '0';\r
1474         Put[1] = 'B';\r
1475         Put += 2;\r
1476 \r
1477         while(*Get != NUL)\r
1478         {\r
1479                 Rnd = rand() % 3;\r
1480                 Ch = ((int)(*Get++ ^ *KeyCurrent)) << Rnd;\r
1481                 Ch = (unsigned char)Ch | (unsigned char)(Ch >> 8);\r
1482                 *Put++ = 0x40 | ((Rnd & 0x3) << 4) | (Ch & 0xF);\r
1483                 *Put++ = 0x40 | ((Ch >> 4) & 0xF);\r
1484                 if((*(Put-2) & 0x1) != 0)\r
1485                         *Put++ = (rand() % 62) + 0x40;\r
1486 \r
1487                 /* 2010.01.31 genta Key */\r
1488                 if( ++KeyCurrent == KeyEnd ){\r
1489                         KeyCurrent = KeyHead;\r
1490                 }\r
1491         }\r
1492         *Put = NUL;\r
1493         return;\r
1494 }\r
1495 \r
1496 /*----- パスワードを暗号化する(AES) ------------------------------------------\r
1497 *\r
1498 *       Parameter\r
1499 *               char *Str : パスワード\r
1500 *               char *Buf : 暗号化したパスワードを格納するバッファ\r
1501 *               const char *Key : 暗号化キー\r
1502 *\r
1503 *       Return Value\r
1504 *               なし\r
1505 *----------------------------------------------------------------------------*/\r
1506 \r
1507 static void EncodePassword3(char *Str, char *Buf, const char *Key)\r
1508 {\r
1509         unsigned char *Put;\r
1510         unsigned char *AesEncBuf;\r
1511         unsigned char *StrPadBuf;\r
1512         size_t StrLen;\r
1513         size_t StrPadLen;\r
1514         size_t StrPadIndex;\r
1515         size_t IvIndex;\r
1516         size_t EncBufIndex;\r
1517         DWORD RandValue;\r
1518         int RandByteCount;\r
1519         unsigned char AesKey[32];\r
1520         unsigned char AesCbcIv[AES_BLOCK_SIZE];\r
1521         aes_encrypt_ctx Ctx;\r
1522 \r
1523         Buf[0] = NUL;\r
1524 \r
1525         Put = (unsigned char *)Buf;\r
1526         StrLen = strlen(Str);\r
1527         StrPadLen = ((StrLen + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;\r
1528 \r
1529         /* 最低長を32文字とする */\r
1530 //      StrPadLen = min1(StrPadLen, AES_BLOCK_SIZE * 2);\r
1531         StrPadLen = max1(StrPadLen, AES_BLOCK_SIZE * 2);\r
1532 \r
1533         if((StrPadBuf = malloc(StrPadLen)) != NULL)\r
1534         {\r
1535                 if((AesEncBuf = malloc(StrPadLen)) != NULL)\r
1536                 {\r
1537                         BOOL PutState;\r
1538 \r
1539                         PutState = FALSE;\r
1540                         strncpy(StrPadBuf, Str, StrPadLen);\r
1541                         \r
1542                         /* PAD部分を乱数で埋める StrPad[StrLen](が有効な場合) は NUL */\r
1543                         for(StrPadIndex = StrLen + 1; StrPadIndex < StrPadLen;)\r
1544                         {\r
1545                                 RandValue = GetRandamDWRODValue();\r
1546                                 for(RandByteCount = 0; RandByteCount < 4; RandByteCount++)\r
1547                                 {\r
1548                                         if(StrPadIndex < StrPadLen)\r
1549                                         {\r
1550                                                 StrPadBuf[StrPadIndex++] = (unsigned char)(RandValue & 0xff);\r
1551                                                 RandValue >>= 8;\r
1552                                         }\r
1553                                 }\r
1554                         }\r
1555                         \r
1556                         // IVの初期化\r
1557                         for(IvIndex = 0; IvIndex < AES_BLOCK_SIZE;)\r
1558                         {\r
1559                                 RandValue = GetRandamDWRODValue();\r
1560                                 for(RandByteCount = 0; RandByteCount < 4; RandByteCount++)\r
1561                                 {\r
1562                                         if(IvIndex < AES_BLOCK_SIZE)\r
1563                                         {\r
1564                                                 AesCbcIv[IvIndex++] = (unsigned char)(RandValue & 0xff);\r
1565                                                 RandValue >>= 8;\r
1566                                         }\r
1567                                 }\r
1568                         }\r
1569                         Put[0] = '0';\r
1570                         Put[1] = 'C';\r
1571                         Put += 2;\r
1572                         for(IvIndex = 0; IvIndex < AES_BLOCK_SIZE; IvIndex++)\r
1573                         {\r
1574                                 sprintf(Put, "%02x", AesCbcIv[IvIndex]);\r
1575                                 Put += 2;\r
1576                         }\r
1577                         *Put++ = ':';\r
1578 \r
1579                         if(CreateAesKey(AesKey, Key) == FFFTP_SUCCESS)\r
1580                         {\r
1581                                 aes_encrypt_key(AesKey, 32, &Ctx);\r
1582 \r
1583                                 if(aes_cbc_encrypt(StrPadBuf, AesEncBuf, StrPadLen, AesCbcIv, &Ctx) == EXIT_SUCCESS)\r
1584                                 {\r
1585                                         for(EncBufIndex = 0; EncBufIndex < StrPadLen; EncBufIndex++)\r
1586                                         {\r
1587                                                 sprintf(Put, "%02x", AesEncBuf[EncBufIndex]);\r
1588                                                 Put += 2;\r
1589                                         }\r
1590                                         *Put = NUL;\r
1591                                         PutState = TRUE;\r
1592                                 }\r
1593                         }\r
1594                         if(FALSE == PutState)\r
1595                         {\r
1596                                 Buf[0] = NUL;\r
1597                         }\r
1598                         free(StrPadBuf);\r
1599                 }\r
1600                 free(AesEncBuf);\r
1601         }\r
1602         return;\r
1603 }\r
1604 \r
1605 \r
1606 /*----- パスワードの暗号化を解く ----------------------------------------------\r
1607 *\r
1608 *       Parameter\r
1609 *               char *Str : 暗号化したパスワード\r
1610 *               char *Buf : パスワードを格納するバッファ\r
1611 *\r
1612 *       Return Value\r
1613 *               なし\r
1614 *----------------------------------------------------------------------------*/\r
1615 \r
1616 static void DecodePassword(char *Str, char *Buf)\r
1617 {\r
1618         unsigned char *Get;\r
1619         unsigned char *Put;\r
1620 \r
1621         Get = (unsigned char *)Str;\r
1622         Put = (unsigned char *)Buf;\r
1623         \r
1624         if( *Get == NUL ){\r
1625                 *Put = NUL;\r
1626         }\r
1627         else if( 0x40 <= *Get && *Get < 0x80 ){\r
1628                 /* Original algorithm */\r
1629                 DecodePasswordOriginal( Str, Buf );\r
1630         }\r
1631         else if( strncmp( Get, "0A", 2 ) == 0 ){\r
1632                 DecodePasswordOriginal( Str + 2, Buf );\r
1633         }\r
1634         else if( strncmp( Get, "0B", 2 ) == 0 ){\r
1635                 DecodePassword2( Str + 2, Buf, SecretKey );\r
1636         }\r
1637         else if( strncmp( Get, "0C", 2 ) == 0 ){\r
1638                 DecodePassword3( Str + 2, Buf, SecretKey );\r
1639         }\r
1640         else {\r
1641                 //      unknown encoding\r
1642                 *Put = NUL;\r
1643                 return;\r
1644         }\r
1645 }\r
1646 \r
1647 /*----- パスワードの暗号化を解く(オリジナルアルゴリズム) -------------------\r
1648 *\r
1649 *       Parameter\r
1650 *               char *Str : 暗号化したパスワード\r
1651 *               char *Buf : パスワードを格納するバッファ\r
1652 *\r
1653 *       Return Value\r
1654 *               なし\r
1655 *----------------------------------------------------------------------------*/\r
1656 static void DecodePasswordOriginal(char *Str, char *Buf)\r
1657 {\r
1658         unsigned char *Get;\r
1659         unsigned char *Put;\r
1660         int Rnd;\r
1661         int Ch;\r
1662 \r
1663         Get = (unsigned char *)Str;\r
1664         Put = (unsigned char *)Buf;\r
1665 \r
1666         while(*Get != NUL)\r
1667         {\r
1668                 Rnd = ((unsigned int)*Get >> 4) & 0x3;\r
1669                 Ch = (*Get & 0xF) | ((*(Get+1) & 0xF) << 4);\r
1670                 Ch <<= 8;\r
1671                 if((*Get & 0x1) != 0)\r
1672                         Get++;\r
1673                 Get += 2;\r
1674                 Ch >>= Rnd;\r
1675                 Ch = (Ch & 0xFF) | ((Ch >> 8) & 0xFF);\r
1676                 *Put++ = Ch;\r
1677         }\r
1678         *Put = NUL;\r
1679         return;\r
1680 }\r
1681 \r
1682 /*----- パスワードの暗号化を解く(オリジナルアルゴリズム^Key) -------------------\r
1683 *\r
1684 *       Parameter\r
1685 *               char *Str : 暗号化したパスワード\r
1686 *               char *Buf : パスワードを格納するバッファ\r
1687 *               const char *Key : 暗号化キー\r
1688 *\r
1689 *       Return Value\r
1690 *               なし\r
1691 *----------------------------------------------------------------------------*/\r
1692 static void DecodePassword2(char *Str, char *Buf, const char* Key)\r
1693 {\r
1694         int Rnd;\r
1695         int Ch;\r
1696         unsigned char *Get = (unsigned char *)Str;\r
1697         unsigned char *Put = (unsigned char *)Buf;\r
1698 \r
1699         /* 2010.01.31 genta Key */\r
1700         unsigned char *KeyHead = (unsigned char *)Key;\r
1701         unsigned char *KeyEnd = KeyHead + strlen(KeyHead);\r
1702         unsigned char *KeyCurrent = KeyHead;\r
1703 \r
1704         while(*Get != NUL)\r
1705         {\r
1706                 Rnd = ((unsigned int)*Get >> 4) & 0x3;\r
1707                 Ch = (*Get & 0xF) | ((*(Get+1) & 0xF) << 4);\r
1708                 Ch <<= 8;\r
1709                 if((*Get & 0x1) != 0)\r
1710                         Get++;\r
1711                 Get += 2;\r
1712                 Ch >>= Rnd;\r
1713                 Ch = (Ch & 0xFF) | ((Ch >> 8) & 0xFF);\r
1714                 *Put++ = Ch ^ *KeyCurrent;\r
1715                 \r
1716                 /* 2010.01.31 genta Key */\r
1717                 if( ++KeyCurrent == KeyEnd ){\r
1718                         KeyCurrent = KeyHead;\r
1719                 }\r
1720         }\r
1721         *Put = NUL;\r
1722         return;\r
1723 }\r
1724 \r
1725 /*----- パスワードの暗号化を解く(AES) ---------------------------------------\r
1726 *\r
1727 *       Parameter\r
1728 *               char *Str : 暗号化したパスワード\r
1729 *               char *Buf : パスワードを格納するバッファ\r
1730 *               const char *Key : 暗号化キー\r
1731 *\r
1732 *       Return Value\r
1733 *               なし\r
1734 *----------------------------------------------------------------------------*/\r
1735 \r
1736 static void DecodePassword3(char *Str, char *Buf, const char *Key)\r
1737 {\r
1738         char *Get;\r
1739         unsigned char *EncBuf;\r
1740         size_t StrLen;\r
1741         size_t IvIndex;\r
1742         size_t EncBufIndex;\r
1743         size_t EncBufLen;\r
1744         unsigned char AesKey[32];\r
1745         unsigned char AesCbcIv[AES_BLOCK_SIZE];\r
1746         aes_decrypt_ctx Ctx;\r
1747 \r
1748         Buf[0] = NUL;\r
1749 \r
1750         Get = Str;\r
1751         StrLen = strlen(Str);\r
1752 \r
1753         if(AES_BLOCK_SIZE * 2 + 1 < StrLen)\r
1754         {\r
1755 \r
1756                 EncBufLen = (StrLen - 1 ) / 2 - AES_BLOCK_SIZE;\r
1757                 if((EncBuf = malloc(EncBufLen)) != NULL)\r
1758                 {\r
1759                         for(IvIndex = 0; IvIndex < AES_BLOCK_SIZE; IvIndex++)\r
1760                         {\r
1761                                 AesCbcIv[IvIndex]  = hex2bin(*Get++) << 4;\r
1762                                 AesCbcIv[IvIndex] |= hex2bin(*Get++);\r
1763                         }\r
1764 \r
1765                         if(*Get++ == ':')\r
1766                         {\r
1767                                 if(CreateAesKey(AesKey, Key) == FFFTP_SUCCESS)\r
1768                                 {\r
1769                                         aes_decrypt_key(AesKey, 32, &Ctx);\r
1770 \r
1771                                         for(EncBufIndex = 0; EncBufIndex < EncBufLen; EncBufIndex++)\r
1772                                         {\r
1773                                                 EncBuf[EncBufIndex]  = hex2bin(*Get++) << 4;\r
1774                                                 EncBuf[EncBufIndex] |= hex2bin(*Get++);\r
1775                                         }\r
1776                                         if(aes_cbc_decrypt(EncBuf, Buf, EncBufLen, AesCbcIv, &Ctx) == EXIT_SUCCESS)\r
1777                                         {\r
1778                                                 Buf[EncBufLen] = NUL;\r
1779                                         }\r
1780                                 }\r
1781                         }\r
1782                         free(EncBuf);\r
1783                 }\r
1784         }\r
1785         return;\r
1786 }\r
1787 \r
1788 /*----- AES用固定長キーを作成 ----------------------------------------------\r
1789 *\r
1790 *       Parameter\r
1791 *               unsigned char *AesKey : AES暗号鍵\r
1792 *               const char *Key : 暗号化キー\r
1793 *\r
1794 *       Return Value\r
1795 *               int ステータス (FFFTP_SUCCESS/FFFTP_FAIL)\r
1796 *       Note\r
1797 *               SHA-1をもちいて32Byte鍵を生成する\r
1798 *----------------------------------------------------------------------------*/\r
1799 \r
1800 static int CreateAesKey(unsigned char *AesKey, const char* Key)\r
1801 {\r
1802         char* HashKey;\r
1803         uint32 HashKeyLen;\r
1804         uint32 results[10];\r
1805         int ByteOffset;\r
1806         int KeyIndex;\r
1807         int ResIndex;\r
1808 \r
1809         HashKeyLen = strlen(Key) + 16;\r
1810         if((HashKey = malloc(HashKeyLen + 1)) == NULL){\r
1811                 return (FFFTP_FAIL);\r
1812         }\r
1813 \r
1814         strcpy(HashKey, Key);\r
1815         strcat(HashKey, ">g^r=@N7=//z<[`:");\r
1816     sha_memory((uchar *)HashKey, HashKeyLen, results);\r
1817 \r
1818         strcpy(HashKey, Key);\r
1819         strcat(HashKey, "VG77dO1#EyC]$|C@");\r
1820     sha_memory((uchar *)HashKey, HashKeyLen, results + 5);\r
1821 \r
1822         KeyIndex = 0;\r
1823         ResIndex = 0;\r
1824         while(ResIndex < 8){\r
1825                 for(ByteOffset = 0; ByteOffset < 4; ByteOffset++){\r
1826                         AesKey[KeyIndex++] = (results[ResIndex] >> ByteOffset * 8) & 0xff;\r
1827                 }\r
1828                 ResIndex++;\r
1829         }\r
1830         free(HashKey);\r
1831 \r
1832         return (FFFTP_SUCCESS);\r
1833 }\r
1834 \r
1835 \r
1836 /*===== レジストリとINIファイルのアクセス処理 ============*/\r
1837 \r
1838 \r
1839 /*===== INIファイル用のレジストリデータ =====*/\r
1840 \r
1841 typedef struct regdatatbl {\r
1842         char KeyName[80+1];                     /* キー名 */\r
1843         char ValTbl[REG_SECT_MAX];      /* 値のテーブル */\r
1844         int ValLen;                                     /* 値データのバイト数 */\r
1845         int Mode;                                       /* キーのモード */\r
1846         struct regdatatbl *Next;\r
1847 } REGDATATBL;\r
1848 \r
1849 /*===== プロトタイプ =====*/\r
1850 \r
1851 static BOOL WriteOutRegToFile(REGDATATBL *Pos);\r
1852 static int ReadInReg(char *Name, REGDATATBL **Handle);\r
1853 // 暗号化通信対応\r
1854 //static int StrCatOut(char *Src, int Len, char *Dst);\r
1855 //static int StrReadIn(char *Src, int Max, char *Dst);\r
1856 static char *ScanValue(void *Handle, char *Name);\r
1857 \r
1858 \r
1859 /*===== ローカルなワーク =====*/\r
1860 \r
1861 static int TmpRegType;\r
1862 \r
1863 \r
1864 \r
1865 /*----- レジストリのタイプを設定する ------------------------------------------\r
1866 *\r
1867 *       Parameter\r
1868 *               int Type : タイプ (REGTYPE_xxx)\r
1869 *\r
1870 *       Return Value\r
1871 *               int ステータス\r
1872 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1873 *----------------------------------------------------------------------------*/\r
1874 \r
1875 static void SetRegType(int Type)\r
1876 {\r
1877         TmpRegType = Type;\r
1878         return;\r
1879 }\r
1880 \r
1881 \r
1882 /*----- レジストリ/INIファイルをオープンする(読み込み)-----------------------\r
1883 *\r
1884 *       Parameter\r
1885 *               char *Name : レジストリ名\r
1886 *               void **Handle : ハンドルを返すワーク\r
1887 *\r
1888 *       Return Value\r
1889 *               int ステータス\r
1890 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1891 *----------------------------------------------------------------------------*/\r
1892 \r
1893 static int OpenReg(char *Name, void **Handle)\r
1894 {\r
1895         int Sts;\r
1896         char Tmp[FMAX_PATH+1];\r
1897 \r
1898         Sts = FFFTP_FAIL;\r
1899         if(TmpRegType == REGTYPE_REG)\r
1900         {\r
1901                 strcpy(Tmp, "Software\\Sota\\");\r
1902                 strcat(Tmp, Name);\r
1903                 if(RegOpenKeyEx(HKEY_CURRENT_USER, Tmp, 0, KEY_READ, (HKEY *)Handle) == ERROR_SUCCESS)\r
1904                         Sts = FFFTP_SUCCESS;\r
1905         }\r
1906         else\r
1907         {\r
1908                 if((Sts = ReadInReg(Name, (REGDATATBL **)Handle)) == FFFTP_SUCCESS)\r
1909                         ((REGDATATBL *)(*Handle))->Mode = 0;\r
1910         }\r
1911         return(Sts);\r
1912 }\r
1913 \r
1914 \r
1915 /*----- レジストリ/INIファイルを作成する(書き込み)---------------------------\r
1916 *\r
1917 *       Parameter\r
1918 *               char *Name : レジストリ名\r
1919 *               void **Handle : ハンドルを返すワーク\r
1920 *\r
1921 *       Return Value\r
1922 *               int ステータス\r
1923 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1924 *----------------------------------------------------------------------------*/\r
1925 \r
1926 static int CreateReg(char *Name, void **Handle)\r
1927 {\r
1928         int Sts;\r
1929         char Tmp[FMAX_PATH+1];\r
1930         DWORD Dispos;\r
1931 \r
1932         Sts = FFFTP_FAIL;\r
1933         if(TmpRegType == REGTYPE_REG)\r
1934         {\r
1935                 strcpy(Tmp, "Software\\Sota\\");\r
1936                 strcat(Tmp, Name);\r
1937                 if(RegCreateKeyEx(HKEY_CURRENT_USER, Tmp, 0, "", REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY | KEY_SET_VALUE, NULL, (HKEY *)Handle, &Dispos) == ERROR_SUCCESS)\r
1938                         Sts = FFFTP_SUCCESS;\r
1939         }\r
1940         else\r
1941         {\r
1942                 if((*Handle = malloc(sizeof(REGDATATBL))) != NULL)\r
1943                 {\r
1944                         strcpy(((REGDATATBL *)(*Handle))->KeyName, Name);\r
1945                         ((REGDATATBL *)(*Handle))->ValLen = 0;\r
1946                         ((REGDATATBL *)(*Handle))->Next = NULL;\r
1947                         ((REGDATATBL *)(*Handle))->Mode = 1;\r
1948                         Sts = FFFTP_SUCCESS;\r
1949                 }\r
1950         }\r
1951         return(Sts);\r
1952 }\r
1953 \r
1954 \r
1955 /*----- レジストリ/INIファイルをクローズする ----------------------------------\r
1956 *\r
1957 *       Parameter\r
1958 *               void *Handle : ハンドル\r
1959 *\r
1960 *       Return Value\r
1961 *               int ステータス\r
1962 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
1963 *----------------------------------------------------------------------------*/\r
1964 \r
1965 static int CloseReg(void *Handle)\r
1966 {\r
1967         REGDATATBL *Pos;\r
1968         REGDATATBL *Next;\r
1969         FILE *Strm;\r
1970 \r
1971         if(TmpRegType == REGTYPE_REG)\r
1972         {\r
1973                 RegCloseKey(Handle);\r
1974 \r
1975                 /* INIファイルを削除 */\r
1976                 if((Strm = fopen(AskIniFilePath(), "rt")) != NULL)\r
1977                 {\r
1978                         fclose(Strm);\r
1979                         MoveFileToTrashCan(AskIniFilePath());\r
1980                 }\r
1981         }\r
1982         else\r
1983         {\r
1984                 if(((REGDATATBL *)Handle)->Mode == 1)\r
1985                 {\r
1986                         if(WriteOutRegToFile(Handle) == TRUE)\r
1987                         {\r
1988 //                              /* レジストリをクリア */\r
1989 //                              ClearRegistry();\r
1990                         }\r
1991                 }\r
1992                 /* テーブルを削除 */\r
1993                 Pos = Handle;\r
1994                 while(Pos != NULL)\r
1995                 {\r
1996                         Next = Pos->Next;\r
1997                         free(Pos);\r
1998                         Pos = Next;\r
1999                 }\r
2000         }\r
2001         return(FFFTP_SUCCESS);\r
2002 }\r
2003 \r
2004 \r
2005 /*----- レジストリ情報をINIファイルに書き込む ---------------------------------\r
2006 *\r
2007 *       Parameter\r
2008 *               REGDATATBL *Pos : レジストリデータ\r
2009 *\r
2010 *       Return Value\r
2011 *               なし\r
2012 *----------------------------------------------------------------------------*/\r
2013 \r
2014 static BOOL WriteOutRegToFile(REGDATATBL *Pos)\r
2015 {\r
2016         FILE *Strm;\r
2017         char *Disp;\r
2018         BOOL Ret;\r
2019 \r
2020         Ret = FALSE;\r
2021         if((Strm = fopen(AskIniFilePath(), "wt")) != NULL)\r
2022         {\r
2023                 fprintf(Strm, MSGJPN239);\r
2024                 while(Pos != NULL)\r
2025                 {\r
2026                         fprintf(Strm, "\n[%s]\n", Pos->KeyName);\r
2027 \r
2028                         Disp = Pos->ValTbl;\r
2029                         while(Disp < (Pos->ValTbl + Pos->ValLen))\r
2030                         {\r
2031                                 fprintf(Strm, "%s\n", Disp);\r
2032                                 Disp = Disp + strlen(Disp) + 1;\r
2033                         }\r
2034                         Pos = Pos->Next;\r
2035                 }\r
2036                 fclose(Strm);\r
2037                 Ret = TRUE;\r
2038         }\r
2039         else\r
2040                 MessageBox(NULL, MSGJPN240, "FFFTP", MB_OK);\r
2041 \r
2042         return(Ret);\r
2043 }\r
2044 \r
2045 \r
2046 /*----- INIファイルからレジストリ情報を読み込む -------------------------------\r
2047 *\r
2048 *       Parameter\r
2049 *               char *Name : 名前\r
2050 *               void *Handle : ハンドル\r
2051 *\r
2052 *       Return Value\r
2053 *               int ステータス\r
2054 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2055 *----------------------------------------------------------------------------*/\r
2056 \r
2057 static int ReadInReg(char *Name, REGDATATBL **Handle)\r
2058 {\r
2059         FILE *Strm;\r
2060         char *Buf;\r
2061         char *Tmp;\r
2062         char *Data;\r
2063         REGDATATBL *New;\r
2064         REGDATATBL *Pos;\r
2065         int Sts;\r
2066 \r
2067         Sts = FFFTP_FAIL;\r
2068         *Handle = NULL;\r
2069 \r
2070         if((Strm = fopen(AskIniFilePath(), "rt")) != NULL)\r
2071         {\r
2072                 if((Buf = malloc(REG_SECT_MAX)) != NULL)\r
2073                 {\r
2074                         while(fgets(Buf, REG_SECT_MAX, Strm) != NULL)\r
2075                         {\r
2076                                 if(*Buf != '#')\r
2077                                 {\r
2078                                         if((Tmp = strchr(Buf, '\n')) != NULL)\r
2079                                                 *Tmp = NUL;\r
2080 \r
2081                                         if(*Buf == '[')\r
2082                                         {\r
2083                                                 if((New = malloc(sizeof(REGDATATBL))) != NULL)\r
2084                                                 {\r
2085                                                         if((Tmp = strchr(Buf, ']')) != NULL)\r
2086                                                                 *Tmp = NUL;\r
2087                                                         strcpy(New->KeyName, Buf+1);\r
2088                                                         New->ValLen = 0;\r
2089                                                         New->Next = NULL;\r
2090                                                         Data = New->ValTbl;\r
2091                                                 }\r
2092                                                 if(*Handle == NULL)\r
2093                                                         *Handle = New;\r
2094                                                 else\r
2095                                                 {\r
2096                                                         Pos = *Handle;\r
2097                                                         while(Pos->Next != NULL)\r
2098                                                                 Pos = Pos->Next;\r
2099                                                         Pos->Next = New;\r
2100                                                 }\r
2101                                         }\r
2102                                         else if(strlen(Buf) > 0)\r
2103                                         {\r
2104                                                 strcpy(Data, Buf);\r
2105                                                 Data += strlen(Buf) + 1;\r
2106                                                 New->ValLen += strlen(Buf) + 1;\r
2107                                         }\r
2108                                 }\r
2109                         }\r
2110                         Sts = FFFTP_SUCCESS;\r
2111                         free(Buf);\r
2112                 }\r
2113                 fclose(Strm);\r
2114         }\r
2115         return(Sts);\r
2116 }\r
2117 \r
2118 \r
2119 /*----- サブキーをオープンする ------------------------------------------------\r
2120 *\r
2121 *       Parameter\r
2122 *               void *Parent : 親のハンドル\r
2123 *               char *Name : 名前\r
2124 *               void **Handle : ハンドルを返すワーク\r
2125 *\r
2126 *       Return Value\r
2127 *               int ステータス\r
2128 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2129 *----------------------------------------------------------------------------*/\r
2130 \r
2131 static int OpenSubKey(void *Parent, char *Name, void **Handle)\r
2132 {\r
2133         int Sts;\r
2134         char Key[80];\r
2135         REGDATATBL *Pos;\r
2136 \r
2137         Sts = FFFTP_FAIL;\r
2138         if(TmpRegType == REGTYPE_REG)\r
2139         {\r
2140                 if(RegOpenKeyEx(Parent, Name, 0, KEY_READ, (HKEY *)Handle) == ERROR_SUCCESS)\r
2141                         Sts = FFFTP_SUCCESS;\r
2142         }\r
2143         else\r
2144         {\r
2145                 strcpy(Key, ((REGDATATBL *)Parent)->KeyName);\r
2146                 strcat(Key, "\\");\r
2147                 strcat(Key, Name);\r
2148                 Pos = Parent;\r
2149                 while(Pos != NULL)\r
2150                 {\r
2151                         if(strcmp(Pos->KeyName, Key) == 0)\r
2152                         {\r
2153                                 *Handle = Pos;\r
2154                                 Sts = FFFTP_SUCCESS;\r
2155                                 break;\r
2156                         }\r
2157                         Pos = Pos->Next;\r
2158                 }\r
2159         }\r
2160         return(Sts);\r
2161 }\r
2162 \r
2163 \r
2164 /*----- サブキーを作成する ----------------------------------------------------\r
2165 *\r
2166 *       Parameter\r
2167 *               void *Parent : 親のハンドル\r
2168 *               char *Name : 名前\r
2169 *               void **Handle : ハンドルを返すワーク\r
2170 *\r
2171 *       Return Value\r
2172 *               int ステータス\r
2173 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2174 *----------------------------------------------------------------------------*/\r
2175 \r
2176 static int CreateSubKey(void *Parent, char *Name, void **Handle)\r
2177 {\r
2178         int Sts;\r
2179         DWORD Dispos;\r
2180         REGDATATBL *Pos;\r
2181 \r
2182         Sts = FFFTP_FAIL;\r
2183         if(TmpRegType == REGTYPE_REG)\r
2184         {\r
2185                 if(RegCreateKeyEx(Parent, Name, 0, "", REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, (HKEY *)Handle, &Dispos) == ERROR_SUCCESS)\r
2186                         Sts = FFFTP_SUCCESS;\r
2187         }\r
2188         else\r
2189         {\r
2190                 if((*Handle = malloc(sizeof(REGDATATBL))) != NULL)\r
2191                 {\r
2192                         strcpy(((REGDATATBL *)(*Handle))->KeyName, ((REGDATATBL *)Parent)->KeyName);\r
2193                         strcat(((REGDATATBL *)(*Handle))->KeyName, "\\");\r
2194                         strcat(((REGDATATBL *)(*Handle))->KeyName, Name);\r
2195 \r
2196                         ((REGDATATBL *)(*Handle))->ValLen = 0;\r
2197                         ((REGDATATBL *)(*Handle))->Next = NULL;\r
2198 \r
2199                         Pos = (REGDATATBL *)Parent;\r
2200                         while(Pos->Next != NULL)\r
2201                                 Pos = Pos->Next;\r
2202                         Pos->Next = *Handle;\r
2203                         Sts = FFFTP_SUCCESS;\r
2204                 }\r
2205         }\r
2206         return(Sts);\r
2207 }\r
2208 \r
2209 \r
2210 /*----- サブキーをクローズする ------------------------------------------------\r
2211 *\r
2212 *       Parameter\r
2213 *               void *Handle : ハンドル\r
2214 *\r
2215 *       Return Value\r
2216 *               int ステータス\r
2217 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2218 *----------------------------------------------------------------------------*/\r
2219 \r
2220 static int CloseSubKey(void *Handle)\r
2221 {\r
2222         if(TmpRegType == REGTYPE_REG)\r
2223                 RegCloseKey(Handle);\r
2224         else\r
2225         {\r
2226                 /* Nothing */\r
2227         }\r
2228         return(FFFTP_SUCCESS);\r
2229 }\r
2230 \r
2231 \r
2232 /*----- サブキーを削除する ----------------------------------------------------\r
2233 *\r
2234 *       Parameter\r
2235 *               void *Handle : ハンドル\r
2236 *               char *Name : 名前\r
2237 *\r
2238 *       Return Value\r
2239 *               int ステータス\r
2240 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2241 *----------------------------------------------------------------------------*/\r
2242 \r
2243 static int DeleteSubKey(void *Handle, char *Name)\r
2244 {\r
2245         int Sts;\r
2246 \r
2247         Sts = FFFTP_FAIL;\r
2248         if(TmpRegType == REGTYPE_REG)\r
2249         {\r
2250                 if(RegDeleteKey(Handle, Name) == ERROR_SUCCESS)\r
2251                         Sts = FFFTP_SUCCESS;\r
2252         }\r
2253         else\r
2254         {\r
2255                 Sts = FFFTP_FAIL;\r
2256         }\r
2257         return(Sts);\r
2258 }\r
2259 \r
2260 \r
2261 /*----- 値を削除する ----------------------------------------------------------\r
2262 *\r
2263 *       Parameter\r
2264 *               void *Handle : ハンドル\r
2265 *               char *Name : 名前\r
2266 *\r
2267 *       Return Value\r
2268 *               int ステータス\r
2269 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2270 *----------------------------------------------------------------------------*/\r
2271 \r
2272 static int DeleteValue(void *Handle, char *Name)\r
2273 {\r
2274         int Sts;\r
2275 \r
2276         Sts = FFFTP_FAIL;\r
2277         if(TmpRegType == REGTYPE_REG)\r
2278         {\r
2279                 if(RegDeleteValue(Handle, Name) == ERROR_SUCCESS)\r
2280                         Sts = FFFTP_SUCCESS;\r
2281         }\r
2282         else\r
2283         {\r
2284                 Sts = FFFTP_FAIL;\r
2285         }\r
2286         return(Sts);\r
2287 }\r
2288 \r
2289 \r
2290 /*----- INT値を読み込む -------------------------------------------------------\r
2291 *\r
2292 *       Parameter\r
2293 *               void *Handle : ハンドル\r
2294 *               char *Name : 名前\r
2295 *               int *Value : INT値を返すワーク\r
2296 *\r
2297 *       Return Value\r
2298 *               int ステータス\r
2299 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2300 *----------------------------------------------------------------------------*/\r
2301 \r
2302 static int ReadIntValueFromReg(void *Handle, char *Name, int *Value)\r
2303 {\r
2304         int Sts;\r
2305         DWORD Size;\r
2306         char *Pos;\r
2307 \r
2308         Sts = FFFTP_FAIL;\r
2309         if(TmpRegType == REGTYPE_REG)\r
2310         {\r
2311                 Size = sizeof(int);\r
2312                 if(RegQueryValueEx(Handle, Name, NULL, NULL, (BYTE *)Value, &Size) == ERROR_SUCCESS)\r
2313                         Sts = FFFTP_SUCCESS;\r
2314         }\r
2315         else\r
2316         {\r
2317                 if((Pos = ScanValue(Handle, Name)) != NULL)\r
2318                 {\r
2319                         *Value = atoi(Pos);\r
2320                         Sts = FFFTP_SUCCESS;\r
2321                 }\r
2322         }\r
2323         return(Sts);\r
2324 }\r
2325 \r
2326 \r
2327 /*----- INT値を書き込む -------------------------------------------------------\r
2328 *\r
2329 *       Parameter\r
2330 *               void *Handle : ハンドル\r
2331 *               char *Name : 名前\r
2332 *               int Value : INT値\r
2333 *\r
2334 *       Return Value\r
2335 *               int ステータス\r
2336 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2337 *----------------------------------------------------------------------------*/\r
2338 \r
2339 static int WriteIntValueToReg(void *Handle, char *Name, int Value)\r
2340 {\r
2341         REGDATATBL *Pos;\r
2342         char *Data;\r
2343         char Tmp[20];\r
2344 \r
2345         if(TmpRegType == REGTYPE_REG)\r
2346                 RegSetValueEx(Handle, Name, 0, REG_DWORD, (CONST BYTE *)&Value, sizeof(int));\r
2347         else\r
2348         {\r
2349                 Pos = (REGDATATBL *)Handle;\r
2350                 Data = Pos->ValTbl + Pos->ValLen;\r
2351                 strcpy(Data, Name);\r
2352                 strcat(Data, "=");\r
2353                 sprintf(Tmp, "%d", Value);\r
2354                 strcat(Data, Tmp);\r
2355                 Pos->ValLen += strlen(Data) + 1;\r
2356         }\r
2357         return(FFFTP_SUCCESS);\r
2358 }\r
2359 \r
2360 \r
2361 /*----- 文字列を読み込む ------------------------------------------------------\r
2362 *\r
2363 *       Parameter\r
2364 *               void *Handle : ハンドル\r
2365 *               char *Name : 名前\r
2366 *               char *Str : 文字列を返すワーク\r
2367 *               DWORD Size : 最大サイズ\r
2368 *\r
2369 *       Return Value\r
2370 *               int ステータス\r
2371 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2372 *----------------------------------------------------------------------------*/\r
2373 \r
2374 static int ReadStringFromReg(void *Handle, char *Name, char *Str, DWORD Size)\r
2375 {\r
2376         int Sts;\r
2377         char *Pos;\r
2378         // UTF-8対応\r
2379         DWORD TempSize;\r
2380         char* pa0;\r
2381         wchar_t* pw0;\r
2382 \r
2383         Sts = FFFTP_FAIL;\r
2384         if(TmpRegType == REGTYPE_REG)\r
2385         {\r
2386                 if(RegQueryValueEx(Handle, Name, NULL, NULL, (BYTE *)Str, &Size) == ERROR_SUCCESS)\r
2387                 {\r
2388                         if(*(Str + Size - 1) != NUL)\r
2389                                 *(Str + Size) = NUL;\r
2390                         Sts = FFFTP_SUCCESS;\r
2391                 }\r
2392         }\r
2393         else\r
2394         {\r
2395                 if((Pos = ScanValue(Handle, Name)) != NULL)\r
2396                 {\r
2397                         // UTF-8対応\r
2398 //                      Size = min1(Size-1, strlen(Pos));\r
2399 //                      Size = StrReadIn(Pos, Size, Str);\r
2400 //                      *(Str + Size) = NUL;\r
2401 //                      Sts = FFFTP_SUCCESS;\r
2402                         switch(IniKanjiCode)\r
2403                         {\r
2404                         case KANJI_NOCNV:\r
2405                                 TempSize = min1(Size-1, strlen(Pos));\r
2406                                 TempSize = StrReadIn(Pos, TempSize, Str);\r
2407                                 *(Str + TempSize) = NUL;\r
2408                                 Sts = FFFTP_SUCCESS;\r
2409                                 if(!CheckStringM(Str))\r
2410                                         break;\r
2411                                 // UTF-8ではない可能性がある\r
2412                                 // Shift_JISとみなす\r
2413                         case KANJI_SJIS:\r
2414                                 if(pa0 = AllocateStringA(Size * 4))\r
2415                                 {\r
2416                                         if(pw0 = AllocateStringW(Size * 4 * 4))\r
2417                                         {\r
2418                                                 TempSize = min1((Size * 4) - 1, strlen(Pos));\r
2419                                                 TempSize = StrReadIn(Pos, TempSize, pa0);\r
2420                                                 *(pa0 + TempSize) = NUL;\r
2421                                                 AtoW(pw0, Size * 4 * 4, pa0, -1);\r
2422                                                 WtoM(Str, Size, pw0, -1);\r
2423                                                 TerminateStringM(Str, Size);\r
2424                                                 Sts = FFFTP_SUCCESS;\r
2425                                                 FreeDuplicatedString(pw0);\r
2426                                         }\r
2427                                         FreeDuplicatedString(pa0);\r
2428                                 }\r
2429                                 break;\r
2430                         }\r
2431                 }\r
2432         }\r
2433         return(Sts);\r
2434 }\r
2435 \r
2436 \r
2437 /*----- 文字列を書き込む ------------------------------------------------------\r
2438 *\r
2439 *       Parameter\r
2440 *               void *Handle : ハンドル\r
2441 *               char *Name : 名前\r
2442 *               char *Str :文字列\r
2443 *\r
2444 *       Return Value\r
2445 *               int ステータス\r
2446 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2447 *----------------------------------------------------------------------------*/\r
2448 \r
2449 static int WriteStringToReg(void *Handle, char *Name, char *Str)\r
2450 {\r
2451         REGDATATBL *Pos;\r
2452         char *Data;\r
2453 \r
2454         if(TmpRegType == REGTYPE_REG)\r
2455                 RegSetValueEx(Handle, Name, 0, REG_SZ, (CONST BYTE *)Str, strlen(Str)+1);\r
2456         else\r
2457         {\r
2458                 Pos = (REGDATATBL *)Handle;\r
2459                 Data = Pos->ValTbl + Pos->ValLen;\r
2460                 strcpy(Data, Name);\r
2461                 strcat(Data, "=");\r
2462                 Pos->ValLen += strlen(Data);\r
2463                 Data = Pos->ValTbl + Pos->ValLen;\r
2464                 Pos->ValLen += StrCatOut(Str, strlen(Str), Data) + 1;\r
2465         }\r
2466         return(FFFTP_SUCCESS);\r
2467 }\r
2468 \r
2469 \r
2470 /*----- マルチ文字列を読み込む ------------------------------------------------\r
2471 *\r
2472 *       Parameter\r
2473 *               void *Handle : ハンドル\r
2474 *               char *Name : 名前\r
2475 *               char *Str : 文字列を返すワーク\r
2476 *               DWORD Size : 最大サイズ\r
2477 *\r
2478 *       Return Value\r
2479 *               int ステータス\r
2480 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2481 *----------------------------------------------------------------------------*/\r
2482 \r
2483 static int ReadMultiStringFromReg(void *Handle, char *Name, char *Str, DWORD Size)\r
2484 {\r
2485         int Sts;\r
2486         char *Pos;\r
2487         // UTF-8対応\r
2488         DWORD TempSize;\r
2489         char* pa0;\r
2490         wchar_t* pw0;\r
2491 \r
2492         Sts = FFFTP_FAIL;\r
2493         if(TmpRegType == REGTYPE_REG)\r
2494         {\r
2495                 if(RegQueryValueEx(Handle, Name, NULL, NULL, (BYTE *)Str, &Size) == ERROR_SUCCESS)\r
2496                 {\r
2497                         if(*(Str + Size - 1) != NUL)\r
2498                                 *(Str + Size) = NUL;\r
2499                         Sts = FFFTP_SUCCESS;\r
2500                 }\r
2501         }\r
2502         else\r
2503         {\r
2504                 if((Pos = ScanValue(Handle, Name)) != NULL)\r
2505                 {\r
2506                         // UTF-8対応\r
2507 //                      Size = min1(Size-1, strlen(Pos));\r
2508 //                      Size = StrReadIn(Pos, Size, Str);\r
2509 //                      *(Str + Size) = NUL;\r
2510 //                      Sts = FFFTP_SUCCESS;\r
2511                         switch(IniKanjiCode)\r
2512                         {\r
2513                         case KANJI_NOCNV:\r
2514                                 TempSize = min1(Size - 2, strlen(Pos));\r
2515                                 TempSize = StrReadIn(Pos, TempSize, Str);\r
2516                                 *(Str + TempSize) = NUL;\r
2517                                 *(Str + TempSize + 1) = NUL;\r
2518                                 Sts = FFFTP_SUCCESS;\r
2519                                 if(!CheckMultiStringM(Str))\r
2520                                         break;\r
2521                                 // UTF-8ではない可能性がある\r
2522                                 // Shift_JISとみなす\r
2523                         case KANJI_SJIS:\r
2524                                 if(pa0 = AllocateStringA(Size * 4))\r
2525                                 {\r
2526                                         if(pw0 = AllocateStringW(Size * 4 * 4))\r
2527                                         {\r
2528                                                 TempSize = min1((Size * 4) - 2, strlen(Pos));\r
2529                                                 TempSize = StrReadIn(Pos, TempSize, pa0);\r
2530                                                 *(pa0 + TempSize) = NUL;\r
2531                                                 *(pa0 + TempSize + 1) = NUL;\r
2532                                                 AtoWMultiString(pw0, Size * 4 * 4, pa0);\r
2533                                                 WtoMMultiString(Str, Size, pw0);\r
2534                                                 TerminateStringM(Str, Size);\r
2535                                                 TerminateStringM(Str, Size - 1);\r
2536                                                 Sts = FFFTP_SUCCESS;\r
2537                                                 FreeDuplicatedString(pw0);\r
2538                                         }\r
2539                                         FreeDuplicatedString(pa0);\r
2540                                 }\r
2541                                 break;\r
2542                         }\r
2543                 }\r
2544         }\r
2545         return(Sts);\r
2546 }\r
2547 \r
2548 \r
2549 /*----- マルチ文字列を書き込む ------------------------------------------------\r
2550 *\r
2551 *       Parameter\r
2552 *               void *Handle : ハンドル\r
2553 *               char *Name : 名前\r
2554 *               char *Str : 文字列\r
2555 *\r
2556 *       Return Value\r
2557 *               int ステータス\r
2558 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2559 *----------------------------------------------------------------------------*/\r
2560 \r
2561 static int WriteMultiStringToReg(void *Handle, char *Name, char *Str)\r
2562 {\r
2563         REGDATATBL *Pos;\r
2564         char *Data;\r
2565 \r
2566         if(TmpRegType == REGTYPE_REG)\r
2567                 RegSetValueEx(Handle, Name, 0, REG_MULTI_SZ, (CONST BYTE *)Str, StrMultiLen(Str)+1);\r
2568         else\r
2569         {\r
2570                 Pos = (REGDATATBL *)Handle;\r
2571                 Data = Pos->ValTbl + Pos->ValLen;\r
2572                 strcpy(Data, Name);\r
2573                 strcat(Data, "=");\r
2574                 Pos->ValLen += strlen(Data);\r
2575                 Data = Pos->ValTbl + Pos->ValLen;\r
2576                 Pos->ValLen += StrCatOut(Str, StrMultiLen(Str), Data) + 1;\r
2577         }\r
2578         return(FFFTP_SUCCESS);\r
2579 }\r
2580 \r
2581 \r
2582 /*----- バイナリを読み込む-----------------------------------------------------\r
2583 *\r
2584 *       Parameter\r
2585 *               void *Handle : ハンドル\r
2586 *               char *Name : 名前\r
2587 *               void *Bin : バイナリを返すワーク\r
2588 *               DWORD Size : 最大サイズ\r
2589 *\r
2590 *       Return Value\r
2591 *               int ステータス\r
2592 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2593 *----------------------------------------------------------------------------*/\r
2594 \r
2595 static int ReadBinaryFromReg(void *Handle, char *Name, void *Bin, DWORD Size)\r
2596 {\r
2597         int Sts;\r
2598         char *Pos;\r
2599 \r
2600         Sts = FFFTP_FAIL;\r
2601         if(TmpRegType == REGTYPE_REG)\r
2602         {\r
2603                 if(RegQueryValueEx(Handle, Name, NULL, NULL, (BYTE *)Bin, &Size) == ERROR_SUCCESS)\r
2604                         Sts = FFFTP_SUCCESS;\r
2605         }\r
2606         else\r
2607         {\r
2608                 if((Pos = ScanValue(Handle, Name)) != NULL)\r
2609                 {\r
2610                         Size = min1(Size, strlen(Pos));\r
2611                         Size = StrReadIn(Pos, Size, Bin);\r
2612                         Sts = FFFTP_SUCCESS;\r
2613                 }\r
2614         }\r
2615         return(Sts);\r
2616 }\r
2617 \r
2618 \r
2619 /*----- バイナリを書き込む ----------------------------------------------------\r
2620 *\r
2621 *       Parameter\r
2622 *               void *Handle : ハンドル\r
2623 *               char *Name : 名前\r
2624 *               void *Bin : バイナリ\r
2625 *               int Len : 長さ\r
2626 *\r
2627 *       Return Value\r
2628 *               int ステータス\r
2629 *                       FFFTP_SUCCESS/FFFTP_FAIL\r
2630 *----------------------------------------------------------------------------*/\r
2631 \r
2632 static int WriteBinaryToReg(void *Handle, char *Name, void *Bin, int Len)\r
2633 {\r
2634         REGDATATBL *Pos;\r
2635         char *Data;\r
2636 \r
2637         if(TmpRegType == REGTYPE_REG)\r
2638                 RegSetValueEx(Handle, Name, 0, REG_BINARY, (CONST BYTE *)Bin, Len);\r
2639         else\r
2640         {\r
2641                 Pos = (REGDATATBL *)Handle;\r
2642                 Data = Pos->ValTbl + Pos->ValLen;\r
2643                 strcpy(Data, Name);\r
2644                 strcat(Data, "=");\r
2645                 Pos->ValLen += strlen(Data);\r
2646                 Data = Pos->ValTbl + Pos->ValLen;\r
2647                 Pos->ValLen += StrCatOut(Bin, Len, Data) + 1;\r
2648         }\r
2649         return(FFFTP_SUCCESS);\r
2650 }\r
2651 \r
2652 \r
2653 /*----- 文字列をバッファに追加書き込みする ------------------------------------\r
2654 *\r
2655 *       Parameter\r
2656 *               char *Src : 文字列\r
2657 *               int len : 文字列の長さ\r
2658 *               char *Dst : 書き込みするバッファ\r
2659 *\r
2660 *       Return Value\r
2661 *               int 追加したバイト数\r
2662 *----------------------------------------------------------------------------*/\r
2663 \r
2664 static int StrCatOut(char *Src, int Len, char *Dst)\r
2665 {\r
2666         int Count;\r
2667 \r
2668         Dst += strlen(Dst);\r
2669         Count = 0;\r
2670         for(; Len > 0; Len--)\r
2671         {\r
2672                 if(*Src == '\\')\r
2673                 {\r
2674                         *Dst++ = '\\';\r
2675                         *Dst++ = '\\';\r
2676                         Count += 2;\r
2677                 }\r
2678                 else if((*Src >= 0x20) && (*Src <= 0x7E))\r
2679                 {\r
2680                         *Dst++ = *Src;\r
2681                         Count++;\r
2682                 }\r
2683                 else\r
2684                 {\r
2685                         sprintf(Dst, "\\%02X", *(unsigned char *)Src);\r
2686                         Dst += 3;\r
2687                         Count += 3;\r
2688                 }\r
2689                 Src++;\r
2690         }\r
2691         *Dst = NUL;\r
2692         return(Count);\r
2693 }\r
2694 \r
2695 \r
2696 /*----- 文字列をバッファに読み込む --------------------------------------------\r
2697 *\r
2698 *       Parameter\r
2699 *               char *Src : 文字列\r
2700 *               int Max : 最大サイズ\r
2701 *               char *Dst : 書き込みするバッファ\r
2702 *\r
2703 *       Return Value\r
2704 *               int 読み込んだバイト数\r
2705 *----------------------------------------------------------------------------*/\r
2706 \r
2707 static int StrReadIn(char *Src, int Max, char *Dst)\r
2708 {\r
2709         int Count;\r
2710         int Tmp;\r
2711 \r
2712         Count = 0;\r
2713         while(*Src != NUL)\r
2714         {\r
2715                 if(Count >= Max)\r
2716                         break;\r
2717 \r
2718                 if(*Src == '\\')\r
2719                 {\r
2720                         Src++;\r
2721                         if(*Src == '\\')\r
2722                                 *Dst = '\\';\r
2723                         else\r
2724                         {\r
2725                                 sscanf(Src, "%02x", &Tmp);\r
2726                                 *Dst = Tmp;\r
2727                                 Src++;\r
2728                         }\r
2729                 }\r
2730                 else\r
2731                         *Dst = *Src;\r
2732 \r
2733                 Count++;\r
2734                 Dst++;\r
2735                 Src++;\r
2736         }\r
2737         return(Count);\r
2738 }\r
2739 \r
2740 \r
2741 /*----- 値を検索する ----------------------------------------------------------\r
2742 *\r
2743 *       Parameter\r
2744 *               char *Handle : ハンドル\r
2745 *               char *Name : 名前\r
2746 *\r
2747 *       Return Value\r
2748 *               char *値データの先頭\r
2749 *                       NULL=指定の名前の値が見つからない\r
2750 *----------------------------------------------------------------------------*/\r
2751 \r
2752 static char *ScanValue(void *Handle, char *Name)\r
2753 {\r
2754         REGDATATBL *Cur;\r
2755         char *Pos;\r
2756         char *Ret;\r
2757 \r
2758         Ret = NULL;\r
2759         Cur = Handle;\r
2760         Pos = Cur->ValTbl;\r
2761         while(Pos < (Cur->ValTbl + Cur->ValLen))\r
2762         {\r
2763                 if((strncmp(Name, Pos, strlen(Name)) == 0) &&\r
2764                    (*(Pos + strlen(Name)) == '='))\r
2765                 {\r
2766                         Ret = Pos + strlen(Name) + 1;\r
2767                         break;\r
2768                 }\r
2769                 Pos += strlen(Pos) + 1;\r
2770         }\r
2771         return(Ret);\r
2772 }\r
2773 \r
2774 \r
2775 /*----------- パスワードの妥当性を確認する ------------------------------------\r
2776 *\r
2777 *       Parameter\r
2778 *               char *Password: パスワード文字列\r
2779 *               char *HashStr: SHA-1ハッシュ文字列\r
2780 *\r
2781 *       Return Value\r
2782 *               int 0 不一致\r
2783 *                       1: 一致\r
2784 *                       2: 異常\r
2785 *----------------------------------------------------------------------------*/\r
2786 int CheckPasswordValidity( char* Password, int length, const char* HashStr )\r
2787 {\r
2788         ulong hash1[5];\r
2789         ulong hash2[5];\r
2790         \r
2791         int i, j;\r
2792         \r
2793         const char* p = HashStr;\r
2794         \r
2795         /* 空文字列は一致したことにする */\r
2796         if( HashStr[0] == NUL ) return 1;\r
2797 \r
2798         /* Hashをチェックするする*/\r
2799         if( strlen(HashStr) != 40 )     return 2;\r
2800 \r
2801         /* Hashをデコードする*/\r
2802         for( i = 0; i < 5; i++ ){\r
2803                 ulong decode = 0;\r
2804                 for( j = 0; j < 8; j++ ){\r
2805                         if( *p < 0x40 || 0x40 + 15 < *p ){\r
2806                                 return 2;\r
2807                         }\r
2808                         decode = (decode << 4 ) + (*p - 0x40);\r
2809                         ++p;\r
2810                 }\r
2811                 hash1[i] = decode;\r
2812         }\r
2813         \r
2814         /* Password をハッシュする */\r
2815         sha_memory( Password, length, hash2 );\r
2816         \r
2817         if( memcmp( (char*)hash1, (char*)hash2, sizeof( hash1 )) == 0 ){\r
2818                 return 1;\r
2819         }\r
2820         return 0;\r
2821 }\r
2822 \r
2823 /*----------- パスワードの妥当性チェックのための文字列を作成する ------------\r
2824 *\r
2825 *       Parameter\r
2826 *               char *Password: パスワード文字列\r
2827 *               char *Str: SHA-1ハッシュ文字列格納場所 (41bytes以上)\r
2828 *\r
2829 *       Return Value\r
2830 *               None\r
2831 *----------------------------------------------------------------------------*/\r
2832 void CreatePasswordHash( char* Password, int length, char* HashStr )\r
2833 {\r
2834         ulong hash[5];\r
2835         int i, j;\r
2836         unsigned char *p = (unsigned char *)HashStr;\r