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.

353 lines
8.5 KiB

  1. /////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998 Active Voice Corporation. All Rights Reserved.
  4. //
  5. // Active Agent(r) and Unified Communications(tm) are trademarks of Active Voice Corporation.
  6. //
  7. // Other brand and product names used herein are trademarks of their respective owners.
  8. //
  9. // The entire program and user interface including the structure, sequence, selection,
  10. // and arrangement of the dialog, the exclusively "yes" and "no" choices represented
  11. // by "1" and "2," and each dialog message are protected by copyrights registered in
  12. // the United States and by international treaties.
  13. //
  14. // Protected by one or more of the following United States patents: 5,070,526, 5,488,650,
  15. // 5,434,906, 5,581,604, 5,533,102, 5,568,540, 5,625,676, 5,651,054.
  16. //
  17. // Active Voice Corporation
  18. // Seattle, Washington
  19. // USA
  20. //
  21. /////////////////////////////////////////////////////////////////////////////////////////
  22. ////
  23. // tsmthunk.c - tsm thunk functions
  24. ////
  25. // This is a thunk layer to the telephone functions in avtsm.dll.
  26. // It's purpose is to allow an application to use avtsm.dll functions
  27. // only if they are available.
  28. //
  29. // To use this module, link TSMTHUNK.OBJ with your application
  30. // rather than with AVTSM.LIB. Before calling any Tsm
  31. // functions, call TsmThunkInit. Before exiting your application,
  32. // call TsmThunkTerm.
  33. //
  34. #include "winlocal.h"
  35. #include <stdlib.h>
  36. #include "avtsm.h"
  37. #include "tsmthunk.h"
  38. #include "loadlib.h"
  39. #include "mem.h"
  40. #include "trace.h"
  41. extern HINSTANCE g_hInstLib;
  42. ////
  43. // private definitions
  44. ////
  45. #define TSMTHUNK_LIBNAME "avtsm.dll"
  46. #ifdef TSMTHUNK
  47. #undef TSMTHUNK
  48. #endif
  49. extern HINSTANCE g_hInstLib;
  50. // tsmthunk control struct
  51. //
  52. typedef struct TSMTHUNK
  53. {
  54. DWORD dwVersion;
  55. HINSTANCE hInst;
  56. HTASK hTask;
  57. HINSTANCE hInstLib;
  58. } TSMTHUNK, FAR *LPTSMTHUNK;
  59. // tsmthunk function struct
  60. //
  61. typedef struct TSMTHUNKFN
  62. {
  63. int index;
  64. LPSTR lpszName;
  65. FARPROC lpfn;
  66. } TSMTHUNKFN, FAR *LPTSMTHUNKFN;
  67. enum
  68. {
  69. iTsmInit = 0,
  70. iTsmTerm,
  71. iTsmSetSpeed,
  72. iTsmConvert,
  73. iTsmSupportsSpeed,
  74. TSMTHUNK_MAXFUNCTIONS
  75. };
  76. static TSMTHUNKFN aTsmThunkFn[] =
  77. {
  78. iTsmInit, "TsmInit", NULL,
  79. iTsmTerm, "TsmTerm", NULL,
  80. iTsmSetSpeed, "TsmSetSpeed", NULL,
  81. iTsmConvert, "TsmConvert", NULL,
  82. iTsmSupportsSpeed, "TsmSupportsSpeed", NULL
  83. };
  84. // helper functions
  85. //
  86. static LPTSMTHUNK TsmThunkGetPtr(HTSMTHUNK hTsmThunk);
  87. static HTSMTHUNK TsmThunkGetHandle(LPTSMTHUNK lpTsmThunk);
  88. ////
  89. // public functions
  90. ////
  91. // TsmThunkInit - initialize tsmthunk engine
  92. // <dwVersion> (i) must be TSMTHUNK_VERSION
  93. // <hInst> (i) instance handle of calling module
  94. // return handle (NULL if error)
  95. //
  96. HTSMTHUNK DLLEXPORT WINAPI TsmThunkInit(DWORD dwVersion, HINSTANCE hInst)
  97. {
  98. BOOL fSuccess = TRUE;
  99. LPTSMTHUNK lpTsmThunk = NULL;
  100. if (dwVersion != TSMTHUNK_VERSION)
  101. fSuccess = TraceFALSE(NULL);
  102. else if (hInst == NULL)
  103. fSuccess = TraceFALSE(NULL);
  104. else if ((lpTsmThunk = (LPTSMTHUNK) MemAlloc(NULL, sizeof(TSMTHUNK), 0)) == NULL)
  105. fSuccess = TraceFALSE(NULL);
  106. else
  107. {
  108. int i;
  109. lpTsmThunk->dwVersion = dwVersion;
  110. lpTsmThunk->hInst = hInst;
  111. lpTsmThunk->hTask = GetCurrentTask();
  112. lpTsmThunk->hInstLib = NULL;
  113. // load the library if possible
  114. //
  115. if ((lpTsmThunk->hInstLib = LoadLibraryPath(TSMTHUNK_LIBNAME, g_hInstLib, 0)) == NULL)
  116. fSuccess = TraceFALSE(NULL);
  117. // get the address of each function in library
  118. //
  119. else for (i = 0; i < SIZEOFARRAY(aTsmThunkFn); ++i)
  120. {
  121. if (aTsmThunkFn[i].index != i)
  122. fSuccess = TraceFALSE(NULL);
  123. else if ((aTsmThunkFn[i].lpfn = GetProcAddress(lpTsmThunk->hInstLib,
  124. aTsmThunkFn[i].lpszName)) == NULL)
  125. {
  126. TracePrintf_1(NULL, 6,
  127. TEXT("GetProcAddress failed\n fn=%s\n"),
  128. (LPTSTR) aTsmThunkFn[i].lpszName);
  129. fSuccess = TraceFALSE(NULL);
  130. }
  131. }
  132. }
  133. if (!fSuccess)
  134. {
  135. TsmThunkTerm(TsmThunkGetHandle(lpTsmThunk));
  136. lpTsmThunk = NULL;
  137. }
  138. return fSuccess ? TsmThunkGetHandle(lpTsmThunk) : NULL;
  139. }
  140. // TsmThunkTerm - shut down tsmthunk engine
  141. // <hTsmThunk> (i) handle returned from TsmThunkInit
  142. // return 0 if success
  143. //
  144. int DLLEXPORT WINAPI TsmThunkTerm(HTSMTHUNK hTsmThunk)
  145. {
  146. BOOL fSuccess = TRUE;
  147. LPTSMTHUNK lpTsmThunk;
  148. if ((lpTsmThunk = TsmThunkGetPtr(hTsmThunk)) == NULL)
  149. fSuccess = TraceFALSE(NULL);
  150. else
  151. {
  152. // library no longer needed
  153. //
  154. FreeLibrary(lpTsmThunk->hInstLib);
  155. if ((lpTsmThunk = MemFree(NULL, lpTsmThunk)) != NULL)
  156. fSuccess = TraceFALSE(NULL);
  157. }
  158. return fSuccess ? 0 : -1;
  159. }
  160. // Tsm thunk functions
  161. //
  162. HTSM DLLEXPORT WINAPI TsmInit(DWORD dwVersion, HINSTANCE hInst,
  163. LPWAVEFORMATEX lpwfx, int nScaleEfficiency, long sizBufSrcMax,
  164. DWORD dwFlags)
  165. {
  166. BOOL fSuccess = TRUE;
  167. HTSM (WINAPI *lpfnTsmInit)(DWORD dwVersion, HINSTANCE hInst,
  168. LPWAVEFORMATEX lpwfx, int nScaleEfficiency, long sizBufSrcMax,
  169. DWORD dwFlags);
  170. HTSM hTsm;
  171. if (aTsmThunkFn[iTsmInit].index != iTsmInit)
  172. fSuccess = TraceFALSE(NULL);
  173. else if (((FARPROC) lpfnTsmInit = aTsmThunkFn[iTsmInit].lpfn) == NULL)
  174. fSuccess = TraceFALSE(NULL);
  175. else
  176. {
  177. hTsm = (*lpfnTsmInit)(dwVersion, hInst,
  178. lpwfx, nScaleEfficiency, sizBufSrcMax, dwFlags);
  179. }
  180. return fSuccess ? hTsm : NULL;
  181. }
  182. int DLLEXPORT WINAPI TsmTerm(HTSM hTsm)
  183. {
  184. BOOL fSuccess = TRUE;
  185. int (WINAPI *lpfnTsmTerm)(HTSM hTsm);
  186. int iRet;
  187. if (aTsmThunkFn[iTsmTerm].index != iTsmTerm)
  188. fSuccess = TraceFALSE(NULL);
  189. else if (((FARPROC) lpfnTsmTerm = aTsmThunkFn[iTsmTerm].lpfn) == NULL)
  190. fSuccess = TraceFALSE(NULL);
  191. else
  192. {
  193. iRet = (*lpfnTsmTerm)(hTsm);
  194. }
  195. return fSuccess ? iRet : -1;
  196. }
  197. int DLLEXPORT WINAPI TsmSetSpeed(HTSM hTsm, int nLevel, DWORD dwFlags)
  198. {
  199. BOOL fSuccess = TRUE;
  200. int (WINAPI *lpfnTsmSetSpeed)(HTSM hTsm, int nLevel, DWORD dwFlags);
  201. int iRet;
  202. if (aTsmThunkFn[iTsmSetSpeed].index != iTsmSetSpeed)
  203. fSuccess = TraceFALSE(NULL);
  204. else if (((FARPROC) lpfnTsmSetSpeed = aTsmThunkFn[iTsmSetSpeed].lpfn) == NULL)
  205. fSuccess = TraceFALSE(NULL);
  206. else
  207. {
  208. iRet = (*lpfnTsmSetSpeed)(hTsm, nLevel, dwFlags);
  209. }
  210. return fSuccess ? iRet : -1;
  211. }
  212. long DLLEXPORT WINAPI TsmConvert(HTSM hTsm,
  213. void _huge *hpBufSrc, long sizBufSrc,
  214. void _huge *hpBufDst, long sizBufDst,
  215. DWORD dwFlags)
  216. {
  217. BOOL fSuccess = TRUE;
  218. long (WINAPI *lpfnTsmConvert)(HTSM hTsm,
  219. void _huge *hpBufSrc, long sizBufSrc,
  220. void _huge *hpBufDst, long sizBufDst,
  221. DWORD dwFlags);
  222. long lRet;
  223. if (aTsmThunkFn[iTsmConvert].index != iTsmConvert)
  224. fSuccess = TraceFALSE(NULL);
  225. else if (((FARPROC) lpfnTsmConvert = aTsmThunkFn[iTsmConvert].lpfn) == NULL)
  226. fSuccess = TraceFALSE(NULL);
  227. else
  228. {
  229. lRet = (*lpfnTsmConvert)(hTsm,
  230. hpBufSrc, sizBufSrc, hpBufDst, sizBufDst, dwFlags);
  231. }
  232. return fSuccess ? lRet : -1;
  233. }
  234. BOOL DLLEXPORT WINAPI TsmSupportsSpeed(int nLevel, LPWAVEFORMATEX lpwfx, DWORD dwFlags)
  235. {
  236. BOOL fSuccess = TRUE;
  237. BOOL (WINAPI *lpfnTsmSupportsSpeed)(int nLevel, LPWAVEFORMATEX lpwfx, DWORD dwFlags);
  238. BOOL fRet;
  239. if (aTsmThunkFn[iTsmSupportsSpeed].index != iTsmSupportsSpeed)
  240. fSuccess = TraceFALSE(NULL);
  241. else if (((FARPROC) lpfnTsmSupportsSpeed = aTsmThunkFn[iTsmSupportsSpeed].lpfn) == NULL)
  242. fSuccess = TraceFALSE(NULL);
  243. else
  244. {
  245. fRet = (*lpfnTsmSupportsSpeed)(nLevel, lpwfx, dwFlags);
  246. }
  247. return fSuccess ? fRet : FALSE;
  248. }
  249. ////
  250. // helper functions
  251. ////
  252. // TsmThunkGetPtr - verify that tsmthunk handle is valid,
  253. // <hTsmThunk> (i) handle returned from TsmThunkInit
  254. // return corresponding tsmthunk pointer (NULL if error)
  255. //
  256. static LPTSMTHUNK TsmThunkGetPtr(HTSMTHUNK hTsmThunk)
  257. {
  258. BOOL fSuccess = TRUE;
  259. LPTSMTHUNK lpTsmThunk;
  260. if ((lpTsmThunk = (LPTSMTHUNK) hTsmThunk) == NULL)
  261. fSuccess = TraceFALSE(NULL);
  262. else if (IsBadWritePtr(lpTsmThunk, sizeof(TSMTHUNK)))
  263. fSuccess = TraceFALSE(NULL);
  264. #ifdef CHECKTASK
  265. // make sure current task owns the tsmthunk handle
  266. //
  267. else if (lpTsmThunk->hTask != GetCurrentTask())
  268. fSuccess = TraceFALSE(NULL);
  269. #endif
  270. return fSuccess ? lpTsmThunk : NULL;
  271. }
  272. // TsmThunkGetHandle - verify that tsmthunk pointer is valid,
  273. // <lpTsmThunk> (i) pointer to TSMTHUNK struct
  274. // return corresponding tsmthunk handle (NULL if error)
  275. //
  276. static HTSMTHUNK TsmThunkGetHandle(LPTSMTHUNK lpTsmThunk)
  277. {
  278. BOOL fSuccess = TRUE;
  279. HTSMTHUNK hTsmThunk;
  280. if ((hTsmThunk = (HTSMTHUNK) lpTsmThunk) == NULL)
  281. fSuccess = TraceFALSE(NULL);
  282. return fSuccess ? hTsmThunk : NULL;
  283. }