OSDN Git Service

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