OSDN Git Service

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