OSDN Git Service

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