OSDN Git Service

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