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.

487 lines
10 KiB

  1. /* File: D:\WACKER\tdll\translat.c (Created: 24-Aug-1994)
  2. *
  3. * Copyright 1994 by Hilgraeve Inc. -- Monroe, MI
  4. * All rights reserved
  5. *
  6. * $Revision: 8 $
  7. * $Date: 7/08/02 6:50p $
  8. */
  9. #include <windows.h>
  10. #pragma hdrstop
  11. #include <stdlib.h>
  12. #include "features.h"
  13. #include "stdtyp.h"
  14. #if defined(CHARACTER_TRANSLATION)
  15. #include "mc.h"
  16. #include "translat.h"
  17. #include "session.h"
  18. #include "tdll.h"
  19. #include "htchar.h"
  20. #include "misc.h" // mscStripName()
  21. #include "translat.hh"
  22. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  23. * FUNCTION:
  24. * CreateTranslateHandle
  25. *
  26. * DESCRIPTION:
  27. *
  28. * ARGUMENTS:
  29. *
  30. * RETURNS:
  31. *
  32. */
  33. HTRANSLATE CreateTranslateHandle(HSESSION hSession)
  34. {
  35. HHTRANSLATE hT = NULL;
  36. PST_TRANS_INT hI;
  37. size_t size;
  38. size = sizeof(ST_TRANSLATE);
  39. size += sizeof(ST_TRANS_INT);
  40. size += sizeof(LONG);
  41. hT = malloc(size);
  42. if (hT)
  43. {
  44. memset(hT, 0, size);
  45. hI = (PST_TRANS_INT)(hT + 1);
  46. hI->hSession = hSession;
  47. InitTranslateHandle((HTRANSLATE)hT, TRUE);
  48. }
  49. return (HTRANSLATE)hT;
  50. }
  51. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  52. * FUNCTION:
  53. *
  54. * DESCRIPTION:
  55. *
  56. * ARGUEMENTS:
  57. *
  58. * RETURNS:
  59. *
  60. */
  61. STATIC_FUNC int LoadTranslateDll(HTRANSLATE pH)
  62. {
  63. HHTRANSLATE pstH = (HHTRANSLATE)pH;
  64. PST_TRANS_INT hI;
  65. HANDLE sH = INVALID_HANDLE_VALUE;
  66. WIN32_FIND_DATA stF;
  67. TCHAR szFileName[MAX_PATH];
  68. TCHAR szPath[MAX_PATH];
  69. TCHAR *pDllName = TEXT("HTRN_*.DLL");
  70. if (pstH == NULL)
  71. {
  72. assert(0);
  73. return -1;
  74. }
  75. hI = (PST_TRANS_INT)(pstH + 1);
  76. //
  77. // The translation DLL is not in the path, so check in the
  78. // module's directory.
  79. //
  80. GetModuleFileName((HINSTANCE)0, szFileName, MAX_PATH);
  81. GetFullPathName(szFileName, MAX_PATH, szPath, NULL);
  82. mscStripName(szPath);
  83. if (StrCharGetStrLength(szPath) + StrCharGetStrLength(pDllName) <= MAX_PATH)
  84. {
  85. StrCharCat(szPath, pDllName);
  86. sH = FindFirstFile(szPath, &stF);
  87. }
  88. //
  89. // The translation DLL is not in the module's directory, so see if
  90. // it is in the same location as the HyperTerminal executable (which
  91. // we extract from the registry).
  92. //
  93. if (sH == INVALID_HANDLE_VALUE)
  94. {
  95. DWORD dwSize = MAX_PATH;
  96. HKEY hKey;
  97. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  98. TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\HyperTrm.exe"),
  99. 0,
  100. KEY_QUERY_VALUE,
  101. &hKey) == ERROR_SUCCESS)
  102. {
  103. if (RegQueryValueEx(hKey, NULL, 0, 0,
  104. szFileName, &dwSize) == ERROR_SUCCESS)
  105. {
  106. if (szFileName[0] == TEXT('\"'))
  107. {
  108. StrCharCopyN(szPath, &szFileName[1], MAX_PATH);
  109. }
  110. else
  111. {
  112. StrCharCopyN(szPath, szFileName, MAX_PATH);
  113. }
  114. mscStripName(szPath);
  115. if (StrCharGetStrLength(szPath) + StrCharGetStrLength(pDllName) <= MAX_PATH)
  116. {
  117. StrCharCat(szPath, pDllName);
  118. sH = FindFirstFile(szPath, &stF);
  119. }
  120. }
  121. RegCloseKey(hKey);
  122. }
  123. }
  124. if (sH != INVALID_HANDLE_VALUE)
  125. {
  126. mscStripName(szPath);
  127. if (StrCharGetStrLength(szPath) +
  128. StrCharGetStrLength(stF.cFileName) <= MAX_PATH)
  129. {
  130. StrCharCat(szPath, stF.cFileName);
  131. hI->hInstance = LoadLibrary(szPath);
  132. if (hI->hInstance)
  133. {
  134. /* Load library succeeded */
  135. (FARPROC)pstH->pfnCreate = GetProcAddress(hI->hInstance,
  136. TEXT("transCreateHandle"));
  137. /* Do we need to error check these things ? */
  138. (FARPROC)pstH->pfnInit = GetProcAddress(hI->hInstance,
  139. TEXT("transInitHandle"));
  140. (FARPROC)pstH->pfnLoad = GetProcAddress(hI->hInstance,
  141. TEXT("transLoadHandle"));
  142. (FARPROC)pstH->pfnSave = GetProcAddress(hI->hInstance,
  143. TEXT("transSaveHandle"));
  144. (FARPROC)pstH->pfnDestroy = GetProcAddress(hI->hInstance,
  145. TEXT("transDestroyHandle"));
  146. (FARPROC)pstH->pfnDoDialog = GetProcAddress(hI->hInstance,
  147. TEXT("transDoDialog"));
  148. (FARPROC)pstH->pfnIn = GetProcAddress(hI->hInstance,
  149. TEXT("transCharIn"));
  150. (FARPROC)pstH->pfnOut = GetProcAddress(hI->hInstance,
  151. TEXT("transCharOut"));
  152. pstH->pfnIsDeviceLoaded = translateInternalTrue;
  153. }
  154. else
  155. {
  156. pstH->pfnCreate = translateInternalVoid;
  157. pstH->pfnInit = translateInternalFalse;
  158. pstH->pfnLoad = translateInternalFalse;
  159. pstH->pfnSave = translateInternalFalse;
  160. pstH->pfnDestroy = translateInternalFalse;
  161. pstH->pfnDoDialog = translateInternalDoDlg;
  162. pstH->pfnIn = translateInternalCio;
  163. pstH->pfnOut = translateInternalCio;
  164. pstH->pfnIsDeviceLoaded = translateInternalFalse;
  165. }
  166. }
  167. FindClose(sH);
  168. }
  169. if ((*pstH->pfnIsDeviceLoaded)(pstH->pDllHandle))
  170. {
  171. /* TODO: create the new translation handle */
  172. pstH->pDllHandle = (*pstH->pfnCreate)(hI->hSession);
  173. if (pstH->pDllHandle)
  174. {
  175. (*pstH->pfnInit)(pstH->pDllHandle);
  176. }
  177. }
  178. return 0;
  179. }
  180. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  181. * FUNCTION:
  182. * InitTranslateHandle
  183. *
  184. * DESCRIPTION:
  185. * Returns the handle to a known state
  186. *
  187. * ARGUMENTS:
  188. * A translate handle
  189. *
  190. * RETURNS:
  191. * ZERO if everything is OK, otherwise a negative error code.
  192. *
  193. */
  194. int InitTranslateHandle(HTRANSLATE pH, BOOL LoadDLL)
  195. {
  196. HHTRANSLATE pstH = (HHTRANSLATE)pH;
  197. PST_TRANS_INT hI;
  198. if (pstH == NULL)
  199. {
  200. assert(0);
  201. return -1;
  202. }
  203. hI = (PST_TRANS_INT)(pstH + 1);
  204. /*
  205. * Clean up the old function if necessary
  206. */
  207. if (pstH->pfnIsDeviceLoaded)
  208. {
  209. /* Try not to call a NULL pointer */
  210. if ((*pstH->pfnIsDeviceLoaded)(pstH->pDllHandle))
  211. {
  212. /* Internally, we always return a FALSE */
  213. if (pstH->pfnDestroy)
  214. {
  215. (*pstH->pfnDestroy)(pstH->pDllHandle);
  216. }
  217. hI = (PST_TRANS_INT)(pstH + 1);
  218. if (hI->hInstance)
  219. {
  220. FreeLibrary(hI->hInstance);
  221. }
  222. hI->hInstance = (HINSTANCE)0;
  223. }
  224. }
  225. /*
  226. * Initialize the function pointers
  227. */
  228. pstH->pDllHandle = (VOID *)0;
  229. pstH->pfnCreate = translateInternalVoid;
  230. pstH->pfnInit = translateInternalFalse;
  231. pstH->pfnLoad = translateInternalFalse;
  232. pstH->pfnSave = translateInternalFalse;
  233. pstH->pfnDestroy = translateInternalFalse;
  234. pstH->pfnIsDeviceLoaded = translateInternalFalse;
  235. pstH->pfnDoDialog = translateInternalDoDlg;
  236. pstH->pfnIn = translateInternalCio;
  237. pstH->pfnOut = translateInternalCio;
  238. if (LoadDLL == TRUE)
  239. {
  240. LoadTranslateDll((HTRANSLATE)pstH);
  241. }
  242. return 0;
  243. }
  244. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  245. * FUNCTION:
  246. * LoadTranslateHandle
  247. *
  248. * DESCRIPTION:
  249. * Chacks to see if an acceptable DLL is available and loads it if it is.
  250. *
  251. * ARGUMENTS:
  252. *
  253. * RETURNS:
  254. *
  255. */
  256. int LoadTranslateHandle(HTRANSLATE pH)
  257. {
  258. HHTRANSLATE pstH = (HHTRANSLATE)pH;
  259. PST_TRANS_INT hI;
  260. if (pstH == NULL)
  261. {
  262. assert(0);
  263. return -1;
  264. }
  265. hI = (PST_TRANS_INT)(pstH + 1);
  266. if ((*pstH->pfnIsDeviceLoaded)(pstH->pDllHandle))
  267. {
  268. /* TODO: create the new translation handle */
  269. // pstH->pDllHandle = (*pstH->pfnCreate)(hI->hSession);
  270. if (pstH->pDllHandle)
  271. {
  272. // (*pstH->pfnInit)(pstH->pDllHandle);
  273. (*pstH->pfnLoad)(pstH->pDllHandle);
  274. }
  275. }
  276. return 0;
  277. }
  278. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  279. * FUNCTION:
  280. *
  281. * DESCRIPTION:
  282. *
  283. * ARGUMENTS:
  284. *
  285. * RETURNS:
  286. *
  287. */
  288. int SaveTranslateHandle(HTRANSLATE pH)
  289. {
  290. HHTRANSLATE pstH = (HHTRANSLATE)pH;
  291. if (pstH == NULL)
  292. {
  293. assert(0);
  294. return -1;
  295. }
  296. if ((*pstH->pfnIsDeviceLoaded)(pstH->pDllHandle))
  297. {
  298. if (pstH->pDllHandle)
  299. {
  300. (*pstH->pfnSave)(pstH->pDllHandle);
  301. }
  302. }
  303. return 0;
  304. }
  305. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  306. * FUNCTION:
  307. *
  308. * DESCRIPTION:
  309. *
  310. * ARGUMENTS:
  311. *
  312. * RETURNS:
  313. *
  314. */
  315. int DestroyTranslateHandle(HTRANSLATE pH)
  316. {
  317. HHTRANSLATE pstH = (HHTRANSLATE)pH;
  318. if (pstH == NULL)
  319. {
  320. assert(0);
  321. return -1;
  322. }
  323. /* Set everything back to a known state */
  324. InitTranslateHandle(pH, FALSE);
  325. //
  326. // Don't forget to destroy the translate handle so we don't
  327. // have a memory leak. REV: 03/20/2001.
  328. //
  329. if (pstH->pfnDestroy)
  330. {
  331. (*pstH->pfnDestroy)(pstH->pDllHandle);
  332. }
  333. free(pstH);
  334. pstH = NULL;
  335. pH = NULL;
  336. return 0;
  337. }
  338. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  339. * FUNCTION:
  340. *
  341. * DESCRIPTION:
  342. *
  343. * ARGUMENTS:
  344. *
  345. * RETURNS:
  346. *
  347. */
  348. static int translateInternalDoDlg(HWND hWnd, VOID *pV)
  349. {
  350. return FALSE;
  351. }
  352. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  353. * FUNCTION:
  354. * translateInternalFalse
  355. * translateInternalTrue
  356. *
  357. * DESCRIPTION:
  358. * Dummy fill in routines that return a constant
  359. *
  360. * ARGUMENTS:
  361. * pV -- an unused DLL translation handle (probably a NULL)
  362. *
  363. * RETURNS:
  364. * Either TRUE or FALSE, depending.
  365. *
  366. */
  367. static int translateInternalFalse(VOID *pV)
  368. {
  369. return FALSE;
  370. }
  371. static int translateInternalTrue(VOID *pV)
  372. {
  373. return TRUE;
  374. }
  375. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  376. * FUNCTION:
  377. * translateInternalVoid
  378. *
  379. * DESCRIPTION:
  380. * A dummy stub to fill in for a handle creation routine
  381. *
  382. * ARGUMENTS:
  383. * hSession -- the ever popular session handle
  384. *
  385. * RETURNS:
  386. * Always returns a NULL pointer
  387. *
  388. */
  389. static VOID *translateInternalVoid(HSESSION hSession)
  390. {
  391. return (VOID *)0;
  392. }
  393. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  394. * FUNCTION:
  395. * translateInternalCio
  396. *
  397. * DESCRIPTION:
  398. * Internal character in and character out function. This is simply a
  399. * loopback function that is used as a dummy function if the translation
  400. * DLL is not available. It is used as the default in the initialized
  401. * but not loaded structure.
  402. *
  403. * ARGUMENTS:
  404. * pV -- handle to the translation DLL (nothing in this case)
  405. * cC -- the character to be translated
  406. * nR -- the number of characters returned (returned to caller)
  407. * nS -- maximum number of characters that can be returned
  408. * pC -- where to return the returned characters
  409. *
  410. * RETURNS:
  411. * ZERO if everything is OK, otherwise a negative error code
  412. *
  413. */
  414. static int translateInternalCio(VOID *pV, TCHAR cC, int *nR, int nS, TCHAR *pC)
  415. {
  416. if (nS > 0)
  417. {
  418. *nR = 1;
  419. *pC = cC;
  420. return 0;
  421. }
  422. return (-1);
  423. }
  424. #endif