OSDN Git Service

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