OSDN Git Service

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