DSMInterface.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /***************************************************************************
  2. * Copyright ?2007 TWAIN Working Group:
  3. * Adobe Systems Incorporated, AnyDoc Software Inc., Eastman Kodak Company,
  4. * Fujitsu Computer Products of America, JFL Peripheral Solutions Inc.,
  5. * Ricoh Corporation, and Xerox Corporation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of the TWAIN Working Group nor the
  16. * names of its contributors may be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY TWAIN Working Group ``AS IS'' AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL TWAIN Working Group BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. ***************************************************************************/
  31. /**
  32. * @file DSMInterface.cpp
  33. * Common defines and typedefs used for accessing DSM for Twain Applications.
  34. * @author TWAIN Working Group
  35. * @date October 2007
  36. */
  37. #include "../stdafx.h"
  38. #include "CommonTWAIN.h"
  39. #include "DSMInterface.h"
  40. #include <iostream>
  41. using namespace std;
  42. // On Windows the official TWAINDSM.dll is signed. this function can verify the signature.
  43. #ifdef TWH_CMP_MSC
  44. /**
  45. * verify the embedded signature in the DSM.
  46. * @param[in] pwszSourceFile the path to the DSM to verify.
  47. */
  48. BOOL VerifyEmbeddedSignature(LPCWSTR pwszSourceFile);
  49. #endif
  50. #ifdef TWH_CMP_MSC
  51. HMODULE
  52. #else
  53. void*
  54. #endif
  55. gpDSM = 0; /**< global pointer to the DSM library */
  56. DSMENTRYPROC gpDSM_Entry = 0; /**< global pointer to the DSM entry point */
  57. TW_ENTRYPOINT g_DSM_Entry = {0}; /**< global pointer to the TWAIN entry point structure */
  58. #ifdef TWH_CMP_GNU
  59. #include <dlfcn.h>
  60. #endif
  61. TW_UINT16 _DSM_Entry( pTW_IDENTITY _pOrigin,
  62. pTW_IDENTITY _pDest,
  63. TW_UINT32 _DG,
  64. TW_UINT16 _DAT,
  65. TW_UINT16 _MSG,
  66. TW_MEMREF _pData)
  67. {
  68. TW_UINT16 ret = TWRC_FAILURE;
  69. if((0 == gpDSM) && !LoadDSMLib(kTWAIN_DSM_DIR kTWAIN_DSM_DLL_NAME))
  70. {
  71. cerr << "Could not load the DSM: " << kTWAIN_DSM_DIR kTWAIN_DSM_DLL_NAME << endl;
  72. return 0; /*< return 0. @todo are we sure of this return? */
  73. }
  74. if(0 != gpDSM_Entry)
  75. {
  76. ret = gpDSM_Entry(_pOrigin, _pDest, _DG, _DAT, _MSG, _pData);
  77. }
  78. return ret;
  79. }
  80. /////////////////////////////////////////////////////////////////////////////
  81. bool LoadDSMLib(char* _pszLibName)
  82. {
  83. // check if already opened
  84. if(0 != gpDSM)
  85. {
  86. return true;
  87. }
  88. #ifdef TWH_CMP_GNU
  89. char *error;
  90. #endif //TWH_CMP_GNU
  91. if((gpDSM=LOADLIBRARY(_pszLibName)) != 0)
  92. {
  93. // On Windows the official TWAINDSM.dll is signed. this function can verify the signature.
  94. #ifdef TWH_CMP_MSC
  95. WCHAR szPath[MAX_PATH];
  96. if(GetModuleFileNameW(gpDSM, szPath, MAX_PATH))
  97. {
  98. if(!VerifyEmbeddedSignature(szPath))
  99. {
  100. // Only continue using the DSM from trusted distributor
  101. unLoadDSMLib();
  102. return false;
  103. }
  104. }
  105. #endif //TWH_CMP_MSC
  106. if((gpDSM_Entry=(DSMENTRYPROC)LOADFUNCTION(gpDSM, "DSM_Entry")) == 0)
  107. {
  108. #ifdef TWH_CMP_MSC // dlsym returning NULL is not an error on Unix
  109. cerr << "Error - Could not find DSM_Entry function in DSM: " << _pszLibName << endl;
  110. return false;
  111. #endif //TWH_CMP_MSC
  112. }
  113. #ifdef TWH_CMP_GNU
  114. if ((error = dlerror()) != 0)
  115. {
  116. cerr << "App - dlsym: " << error << endl;
  117. return false;
  118. }
  119. #endif //TWH_CMP_GNU
  120. }
  121. else
  122. {
  123. cerr << "Error - Could not load DSM: " << _pszLibName << endl;
  124. #ifdef TWH_CMP_GNU
  125. cerr << "App - dlopen: " << dlerror() << endl;
  126. #endif //TWH_CMP_GNU
  127. return false;
  128. }
  129. #ifdef TWH_CMP_GNU
  130. #endif
  131. return true;
  132. }
  133. /////////////////////////////////////////////////////////////////////////////
  134. void unLoadDSMLib()
  135. {
  136. if(gpDSM)
  137. {
  138. UNLOADLIBRARY(gpDSM);
  139. gpDSM = 0;
  140. gpDSM_Entry = 0;
  141. }
  142. }
  143. //////////////////////////////////////////////////////////////////////////////
  144. // The following functions are defined in the DSM2,
  145. // For backwards compatibiltiy on windows call the default function
  146. TW_HANDLE _DSM_Alloc(TW_UINT32 _size)
  147. {
  148. if(g_DSM_Entry.DSM_MemAllocate)
  149. {
  150. return g_DSM_Entry.DSM_MemAllocate(_size);
  151. }
  152. #ifdef TWH_CMP_MSC
  153. return ::GlobalAlloc(GPTR, _size);
  154. #endif
  155. return 0;
  156. }
  157. //////////////////////////////////////////////////////////////////////////////
  158. void _DSM_Free(TW_HANDLE _hMemory)
  159. {
  160. if(g_DSM_Entry.DSM_MemFree)
  161. {
  162. return g_DSM_Entry.DSM_MemFree(_hMemory);
  163. }
  164. #ifdef TWH_CMP_MSC
  165. ::GlobalFree(_hMemory);
  166. #endif
  167. return;
  168. }
  169. //////////////////////////////////////////////////////////////////////////////
  170. TW_MEMREF _DSM_LockMemory(TW_HANDLE _hMemory)
  171. {
  172. if(g_DSM_Entry.DSM_MemLock)
  173. {
  174. return g_DSM_Entry.DSM_MemLock(_hMemory);
  175. }
  176. #ifdef TWH_CMP_MSC
  177. return (TW_MEMREF)::GlobalLock(_hMemory);
  178. #endif
  179. return 0;
  180. }
  181. //////////////////////////////////////////////////////////////////////////////
  182. void _DSM_UnlockMemory(TW_HANDLE _hMemory)
  183. {
  184. if(g_DSM_Entry.DSM_MemUnlock)
  185. {
  186. return g_DSM_Entry.DSM_MemUnlock(_hMemory);
  187. }
  188. #ifdef TWH_CMP_MSC
  189. ::GlobalUnlock(_hMemory);
  190. #endif
  191. return;
  192. }
  193. //////////////////////////////////////////////////////////////////////////////