Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

538 lines
15 KiB

  1. /**************************************************/
  2. /* */
  3. /* */
  4. /* Registry Key Function */
  5. /* */
  6. /* */
  7. /* Copyright (c) 1997-1999 Microsoft Corporation. */
  8. /**************************************************/
  9. #include "stdafx.h"
  10. #include "eudcedit.h"
  11. #include "registry.h"
  12. #include "util.h"
  13. #define STRSAFE_LIB
  14. #include <strsafe.h>
  15. static TCHAR subkey1[] = TEXT("EUDC");
  16. static TCHAR subkey2[] = TEXT("System\\CurrentControlSet\\control\\Nls\\Codepage\\EUDCCodeRange");
  17. static TCHAR SubKey[MAX_PATH];
  18. #ifdef IN_FONTS_DIR // IsFileUnderWindowsRoot()
  19. LPTSTR
  20. IsFileUnderWindowsRoot(
  21. LPTSTR TargetPath)
  22. {
  23. TCHAR WindowsRoot[MAX_PATH+1];
  24. UINT WindowsRootLength;
  25. if (!TargetPath)
  26. {
  27. return NULL;
  28. }
  29. WindowsRootLength = GetSystemWindowsDirectory(WindowsRoot,MAX_PATH);
  30. if( lstrcmpi(WindowsRoot,TargetPath) == 0)
  31. return (TargetPath + WindowsRootLength);
  32. return NULL;
  33. }
  34. void AdjustTypeFace(WCHAR *orgName, WCHAR *newName, int nDestLen)
  35. {
  36. HRESULT hresult;
  37. if ((!orgName) || (!newName))
  38. {
  39. return;
  40. }
  41. if (!lstrcmpW(orgName, L"\x5b8b\x4f53"))
  42. {
  43. //*STRSAFE* lstrcpy(newName, TEXT("Simsun"));
  44. hresult = StringCchCopy(newName , nDestLen, TEXT("Simsun"));
  45. if (!SUCCEEDED(hresult))
  46. {
  47. return ;
  48. }
  49. } else if (!lstrcmpW(orgName, L"\x65b0\x7d30\x660e\x9ad4"))
  50. {
  51. //*STRSAFE* lstrcpy(newName, TEXT("PMingLiU"));
  52. hresult = StringCchCopy(newName , nDestLen, TEXT("PMingLiU"));
  53. if (!SUCCEEDED(hresult))
  54. {
  55. return ;
  56. }
  57. } else if (!lstrcmpW(orgName, L"\xFF2d\xFF33\x0020\xFF30\x30b4\x30b7\x30c3\x30af"))
  58. {
  59. //*STRSAFE* lstrcpy(newName, TEXT("MS PGothic"));
  60. hresult = StringCchCopy(newName , nDestLen, TEXT("MS PGothic"));
  61. if (!SUCCEEDED(hresult))
  62. {
  63. return;
  64. }
  65. } else if (!lstrcmpW(orgName, L"\xad74\xb9bc"))
  66. {
  67. //*STRSAFE* lstrcpy(newName, TEXT("Gulim"));
  68. hresult = StringCchCopy(newName , nDestLen, TEXT("Gulim"));
  69. if (!SUCCEEDED(hresult))
  70. {
  71. return ;
  72. }
  73. } else
  74. {
  75. //*STRSAFE* lstrcpy(newName, orgName);
  76. hresult = StringCchCopy(newName , nDestLen, orgName);
  77. if (!SUCCEEDED(hresult))
  78. {
  79. return;
  80. }
  81. }
  82. }
  83. #endif // IN_FONTS_DIR
  84. /****************************************/
  85. /* */
  86. /* Inquiry EUDC registry */
  87. /* */
  88. /****************************************/
  89. BOOL
  90. InqTypeFace(
  91. TCHAR *typeface,
  92. TCHAR *filename,
  93. INT bufsiz)
  94. {
  95. HKEY phkey;
  96. DWORD cb, dwType;
  97. LONG rc;
  98. TCHAR FaceName[LF_FACESIZE];
  99. TCHAR SysName[LF_FACESIZE];
  100. HRESULT hresult;
  101. #ifdef BUILD_ON_WINNT // InqTypeFace()
  102. TCHAR FileName[MAX_PATH];
  103. #endif // BUILD_ON_WINNT
  104. if ((!typeface) || (!filename))
  105. {
  106. return FALSE;
  107. }
  108. GetStringRes(SysName, IDS_SYSTEMEUDCFONT_STR, ARRAYLEN(SysName));
  109. if( !lstrcmp(typeface, SysName)){
  110. //*STRSAFE* lstrcpy(FaceName,TEXT("SystemDefaultEUDCFont"));
  111. hresult = StringCchCopy(FaceName , ARRAYLEN(FaceName), TEXT("SystemDefaultEUDCFont"));
  112. if (!SUCCEEDED(hresult))
  113. {
  114. return FALSE;
  115. }
  116. }else {
  117. #ifdef IN_FONTS_DIR
  118. AdjustTypeFace(typeface, FaceName,ARRAYLEN(FaceName));
  119. #else
  120. //*STRSAFE* lstrcpy(FaceName, typeface);
  121. hresult = StringCchCopy(FaceName , ARRAYLEN(FaceName), typeface);
  122. if (!SUCCEEDED(hresult))
  123. {
  124. return FALSE;
  125. }
  126. #endif
  127. }
  128. if( RegOpenKeyEx( HKEY_CURRENT_USER, (LPCTSTR)SubKey, 0,
  129. KEY_ALL_ACCESS, &phkey) != ERROR_SUCCESS){
  130. return FALSE;
  131. }
  132. #ifdef IN_FONTS_DIR // InqTypeFace()
  133. cb = (DWORD)MAX_PATH*sizeof(WORD)/sizeof(BYTE);
  134. rc = RegQueryValueEx(phkey, FaceName, 0, &dwType,
  135. (LPBYTE)FileName, &cb);
  136. RegCloseKey(phkey);
  137. /*
  138. * if there is some error or no data, just return false.
  139. */
  140. if ((rc != ERROR_SUCCESS) || (FileName[0] == '\0')) {
  141. return (FALSE);
  142. }
  143. /*
  144. * expand %SystemRoot% to Windows direcotry.
  145. */
  146. ExpandEnvironmentStrings((LPCTSTR)FileName,(LPTSTR)filename,bufsiz);
  147. #else
  148. cb = (DWORD)bufsiz*sizeof(WORD)/sizeof(BYTE);
  149. rc = RegQueryValueEx(phkey, (TCHAR *)FaceName, 0, &dwType,
  150. (LPBYTE)filename, &cb);
  151. RegCloseKey(phkey);
  152. if ((rc != ERROR_SUCCESS) || (filename[0] == '\0')) {
  153. return (FALSE);
  154. }
  155. #endif // IN_FONTS_DIR
  156. #ifdef BUILD_ON_WINNT // InqTypeFace()
  157. /*
  158. * if this is not 'full path'. Build 'full path'.
  159. *
  160. * EUDC.TTE -> C:\WINNT40\FONTS\EUDC.TTE
  161. * 0123456...
  162. *
  163. * 1. filename should have drive letter.
  164. * 2. filename should have one '\\' ,at least, for root.
  165. */
  166. if ((filename[1] != ':') || (Mytcsstr((const TCHAR *)filename,TEXT("\\")) == NULL)) {
  167. /* backup original.. */
  168. //*STRSAFE* lstrcpy(FileName, (const TCHAR *)filename);
  169. hresult = StringCchCopy(FileName , ARRAYLEN(FileName), (const TCHAR *)filename);
  170. if (!SUCCEEDED(hresult))
  171. {
  172. return FALSE;
  173. }
  174. /* Get windows directory */
  175. GetSystemWindowsDirectory((TCHAR *)filename, MAX_PATH);
  176. #ifdef IN_FONTS_DIR // InqTypeFace()
  177. //*STRSAFE* lstrcat((TCHAR *)filename, TEXT("\\FONTS\\"));
  178. hresult = StringCchCat((TCHAR *) filename, ARRAYLEN( filename), TEXT("\\FONTS\\"));
  179. if (!SUCCEEDED(hresult))
  180. {
  181. return FALSE;
  182. }
  183. #else
  184. //*STRSAFE* strcat((char *)filename, "\\");
  185. hresult = StringCchCatA((char *) filename, sizeof( filename), "\\");
  186. if (!SUCCEEDED(hresult))
  187. {
  188. return FALSE;
  189. }
  190. #endif // IN_FONTS_DIR
  191. //*STRSAFE* lstrcat((TCHAR *) filename, FileName);
  192. hresult = StringCchCat((TCHAR *) filename, ARRAYLEN( filename), FileName);
  193. if (!SUCCEEDED(hresult))
  194. {
  195. return FALSE;
  196. }
  197. }
  198. #endif // BUILD_ON_WINNT
  199. #ifdef IN_FONTS_DIR // InqTypeFace()
  200. return (TRUE);
  201. #else
  202. return rc == ERROR_SUCCESS && filename[0] != '\0' ? TRUE : FALSE;
  203. #endif
  204. }
  205. /****************************************/
  206. /* */
  207. /* Registry EUDC font and file */
  208. /* */
  209. /****************************************/
  210. BOOL
  211. RegistTypeFace(
  212. TCHAR *typeface,
  213. TCHAR *filename)
  214. {
  215. HKEY phkey;
  216. LONG rc;
  217. TCHAR FaceName[LF_FACESIZE];
  218. TCHAR SysName[LF_FACESIZE];
  219. HRESULT hresult;
  220. #ifdef IN_FONTS_DIR // RegistTypeFace()
  221. LPTSTR SaveFileName;
  222. TCHAR FileName[MAX_PATH];
  223. #endif // IN_FONTS_DIR
  224. if ((!typeface) || (!filename))
  225. {
  226. return FALSE;
  227. }
  228. GetStringRes((TCHAR *)SysName, IDS_SYSTEMEUDCFONT_STR, ARRAYLEN(SysName));
  229. if( !lstrcmp((const TCHAR *)typeface, (const TCHAR *)SysName)){
  230. //*STRSAFE* lstrcpy(FaceName, TEXT("SystemDefaultEUDCFont"));
  231. hresult = StringCchCopy(FaceName , ARRAYLEN(FaceName), TEXT("SystemDefaultEUDCFont"));
  232. if (!SUCCEEDED(hresult))
  233. {
  234. return FALSE;
  235. }
  236. }else{
  237. #ifdef IN_FONTS_DIR
  238. AdjustTypeFace(typeface, FaceName,ARRAYLEN(FaceName));
  239. #else
  240. //*STRSAFE* lstrcpy(FaceName, (const TCHAR *)typeface);
  241. hresult = StringCchCopy(FaceName , ARRAYLEN(FaceName), (const TCHAR *)typeface);
  242. if (!SUCCEEDED(hresult))
  243. {
  244. return FALSE;
  245. }
  246. #endif
  247. }
  248. if( RegOpenKeyEx( HKEY_CURRENT_USER, (LPCTSTR)SubKey, 0,
  249. KEY_ALL_ACCESS, &phkey) != ERROR_SUCCESS){
  250. return FALSE;
  251. }
  252. #ifdef IN_FONTS_DIR // RegistTypeFace()
  253. /*
  254. * if registry data contains full path, and the file is under windows
  255. * directory, replace the hardcodeed path with %SystemRoot%....
  256. */
  257. if( (SaveFileName = IsFileUnderWindowsRoot((LPTSTR)filename)) != NULL) {
  258. //*STRSAFE* lstrcpy(FileName, TEXT("%SystemRoot%"));
  259. hresult = StringCchCopy(FileName , ARRAYLEN(FileName), TEXT("%SystemRoot%"));
  260. if (!SUCCEEDED(hresult))
  261. {
  262. return FALSE;
  263. }
  264. //*STRSAFE* if( *SaveFileName != '\\' ) lstrcat(FileName, TEXT("\\"));
  265. if( *SaveFileName != '\\' ) {
  266. hresult = StringCchCat(FileName , ARRAYLEN(FileName), TEXT("\\"));
  267. if (!SUCCEEDED(hresult))
  268. {
  269. return FALSE;
  270. }
  271. }
  272. //*STRSAFE* lstrcat(FileName, SaveFileName );
  273. hresult = StringCchCat(FileName , ARRAYLEN(FileName), SaveFileName );
  274. if (!SUCCEEDED(hresult))
  275. {
  276. return FALSE;
  277. }
  278. } else {
  279. //*STRSAFE* lstrcpy(FileName, (TCHAR *)filename );
  280. hresult = StringCchCopy(FileName , ARRAYLEN(FileName), (TCHAR *)filename );
  281. if (!SUCCEEDED(hresult))
  282. {
  283. return FALSE;
  284. }
  285. }
  286. rc = RegSetValueEx( phkey, (LPCTSTR)FaceName, 0,
  287. REG_SZ, (const BYTE *)FileName, (lstrlen((LPCTSTR)FileName)+1)*sizeof(WORD)/sizeof(BYTE));
  288. #else
  289. rc = RegSetValueEx( phkey, (LPCTSTR)FaceName, 0,
  290. REG_SZ, (const BYTE *)filename, (lstrlen((LPCTSTR)filename)+1)*sizeof(WORD)/sizeof(BYTE));
  291. #endif // IN_FONTS_DIR
  292. RegCloseKey(phkey);
  293. return rc == ERROR_SUCCESS ? TRUE : FALSE;
  294. }
  295. /****************************************/
  296. /* */
  297. /* Delete Registry string */
  298. /* */
  299. /****************************************/
  300. BOOL
  301. DeleteReg(
  302. TCHAR *typeface)
  303. {
  304. HKEY phkey;
  305. LONG rc;
  306. TCHAR FaceName[LF_FACESIZE];
  307. TCHAR SysName[LF_FACESIZE];
  308. HRESULT hresult;
  309. if (!typeface)
  310. {
  311. return FALSE;
  312. }
  313. GetStringRes((TCHAR *)SysName, IDS_SYSTEMEUDCFONT_STR, ARRAYLEN(SysName));
  314. if( !lstrcmp((const TCHAR *)typeface, (const TCHAR *)SysName)){
  315. //*STRSAFE* lstrcpy((TCHAR *)FaceName, TEXT("SystemDefaultEUDCFont"));
  316. hresult = StringCchCopy((TCHAR *)FaceName, ARRAYLEN(FaceName), TEXT("SystemDefaultEUDCFont"));
  317. if (!SUCCEEDED(hresult))
  318. {
  319. return FALSE;
  320. }
  321. }else{
  322. #ifdef IN_FONTS_DIR
  323. AdjustTypeFace(typeface, FaceName,ARRAYLEN(FaceName));
  324. #else
  325. //*STRSAFE* lstrcpy((TCHAR *)FaceName, (const TCHAR *)typeface);
  326. hresult = StringCchCopy((TCHAR *)FaceName, ARRAYLEN(FaceName), (const TCHAR *)typeface);
  327. if (!SUCCEEDED(hresult))
  328. {
  329. return FALSE;
  330. }
  331. #endif
  332. }
  333. if( RegOpenKeyEx(HKEY_CURRENT_USER, (LPCTSTR)SubKey, 0,
  334. KEY_ALL_ACCESS, &phkey) != ERROR_SUCCESS){
  335. return FALSE;
  336. }
  337. rc = RegDeleteValue( phkey, (LPTSTR)FaceName);
  338. RegCloseKey(phkey);
  339. return rc == ERROR_SUCCESS ? TRUE : FALSE;
  340. }
  341. /****************************************/
  342. /* */
  343. /* Create Registry Subkey */
  344. /* */
  345. /****************************************/
  346. BOOL
  347. CreateRegistrySubkey()
  348. {
  349. HKEY phkey;
  350. DWORD dwdisp;
  351. int LocalCP;
  352. TCHAR CodePage[10];
  353. int result;
  354. HRESULT hresult;
  355. /* New Registry */
  356. LocalCP = GetACP();
  357. //*STRSAFE* wsprintf( CodePage, TEXT("%d"), LocalCP);
  358. hresult = StringCchPrintf(CodePage , ARRAYLEN(CodePage), TEXT("%d"), LocalCP);
  359. if (!SUCCEEDED(hresult))
  360. {
  361. return FALSE;
  362. }
  363. //*STRSAFE* lstrcpy(SubKey, subkey1);
  364. hresult = StringCchCopy(SubKey , ARRAYLEN(SubKey), subkey1);
  365. if (!SUCCEEDED(hresult))
  366. {
  367. return FALSE;
  368. }
  369. //*STRSAFE* lstrcat(SubKey, TEXT("\\"));
  370. hresult = StringCchCat(SubKey , ARRAYLEN(SubKey), TEXT("\\"));
  371. if (!SUCCEEDED(hresult))
  372. {
  373. return FALSE;
  374. }
  375. //*STRSAFE* lstrcat(SubKey, CodePage);
  376. hresult = StringCchCat(SubKey , ARRAYLEN(SubKey), CodePage);
  377. if (!SUCCEEDED(hresult))
  378. {
  379. return FALSE;
  380. }
  381. if( RegOpenKeyEx( HKEY_CURRENT_USER, (LPCTSTR)SubKey, 0,
  382. KEY_ALL_ACCESS, &phkey) != ERROR_SUCCESS){
  383. result = RegCreateKeyEx(HKEY_CURRENT_USER,
  384. (LPCTSTR)SubKey, 0, TEXT(""),
  385. REG_OPTION_NON_VOLATILE,
  386. KEY_ALL_ACCESS, NULL, &phkey, &dwdisp);
  387. if( result == ERROR_SUCCESS)
  388. RegCloseKey( phkey);
  389. else return FALSE;
  390. }else RegCloseKey(phkey);
  391. return TRUE;
  392. }
  393. /****************************************/
  394. /* */
  395. /* Inquiry Code range registry */
  396. /* */
  397. /****************************************/
  398. BOOL
  399. InqCodeRange(
  400. TCHAR *Codepage,
  401. BYTE *Coderange,
  402. INT bufsiz)
  403. {
  404. HKEY phkey;
  405. DWORD cb, dwType;
  406. LONG rc;
  407. if ((!Codepage) || (!Coderange))
  408. {
  409. return FALSE;
  410. }
  411. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)subkey2, 0,
  412. KEY_READ, &phkey) != ERROR_SUCCESS) {
  413. return FALSE;
  414. }
  415. cb = (DWORD)bufsiz * sizeof(WORD)/sizeof(BYTE);
  416. rc = RegQueryValueEx(phkey, (TCHAR *)Codepage, 0, &dwType,
  417. (LPBYTE)Coderange, &cb);
  418. RegCloseKey(phkey);
  419. return rc == ERROR_SUCCESS && Coderange[0] != '\0' ? TRUE : FALSE;
  420. }
  421. BOOL
  422. DeleteRegistrySubkey()
  423. {
  424. HKEY phkey;
  425. if( RegOpenKeyEx( HKEY_CURRENT_USER, (LPCTSTR)SubKey, 0,
  426. KEY_ALL_ACCESS, &phkey) == ERROR_SUCCESS){
  427. RegCloseKey(phkey);
  428. return RegDeleteKey(HKEY_CURRENT_USER, (LPCTSTR)SubKey);
  429. }
  430. return TRUE;
  431. }
  432. BOOL
  433. FindFontSubstitute(TCHAR *orgFontName, TCHAR *sbstFontName, int nDestLen)
  434. {
  435. static TCHAR fsKey[] = TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\FontSubstitutes");
  436. HRESULT hresult;
  437. if ((!orgFontName) || (!sbstFontName))
  438. {
  439. return FALSE;
  440. }
  441. *sbstFontName = 0;
  442. //*STRSAFE* lstrcpy(sbstFontName, orgFontName);
  443. hresult = StringCchCopy(sbstFontName , nDestLen, orgFontName);
  444. if (!SUCCEEDED(hresult))
  445. {
  446. return FALSE;
  447. }
  448. HKEY phkey;
  449. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)fsKey, 0,
  450. KEY_QUERY_VALUE, &phkey) != ERROR_SUCCESS) {
  451. return FALSE;
  452. }
  453. DWORD valueNameSize = LF_FACESIZE + 50; //should be facename + ',' + codepage
  454. TCHAR valueName[LF_FACESIZE + 50];
  455. DWORD valueType;
  456. DWORD valueDataSize = (LF_FACESIZE + 50) * sizeof(TCHAR); //should be facename + ',' + codepage
  457. BYTE valueData[(LF_FACESIZE + 50) * sizeof(TCHAR)];
  458. LONG ret;
  459. DWORD idx = 0;
  460. while ((ret = RegEnumValue(phkey, idx, valueName, &valueNameSize, 0,
  461. &valueType, valueData, &valueDataSize)) != ERROR_NO_MORE_ITEMS)
  462. {
  463. if (ret != ERROR_SUCCESS)
  464. {
  465. RegCloseKey(phkey);
  466. return FALSE;
  467. }
  468. Truncate(valueName, _T(','));
  469. if (!lstrcmpi(valueName, orgFontName))
  470. {
  471. Truncate((TCHAR *)valueData, _T(','));
  472. //*STRSAFE* lstrcpy(sbstFontName, (TCHAR *)valueData);
  473. hresult = StringCchCopy(sbstFontName , nDestLen, (TCHAR *)valueData);
  474. if (!SUCCEEDED(hresult))
  475. {
  476. return FALSE;
  477. }
  478. break;
  479. }
  480. idx ++;
  481. valueNameSize = LF_FACESIZE + 50;
  482. valueDataSize = (LF_FACESIZE + 50) * sizeof(TCHAR);
  483. }
  484. RegCloseKey(phkey);
  485. return TRUE;
  486. }
  487. void Truncate(TCHAR *str, TCHAR delim)
  488. {
  489. TCHAR *pchr = _tcschr(str, delim);
  490. if (pchr)
  491. *pchr = 0;
  492. }