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.

318 lines
9.5 KiB

  1. /*******************************************************************************
  2. * CommonLx.h
  3. * This is the header file for the defines and constants used by sapi lexicon
  4. * and the tools
  5. *
  6. * Owner: yunusm Date: 07/01/99
  7. *
  8. * Copyright (c) 1999 Microsoft Corporation. All Rights Reserved.
  9. *******************************************************************************/
  10. #pragma once
  11. //--- Includes -----------------------------------------------------------------
  12. #include <stdio.h>
  13. #include "sapi.h"
  14. #include "spddkhlp.h"
  15. // Phone converter defines for the SpPhoneConverter class
  16. const static DWORD g_dwMaxLenPhone = 7; // Maximum number of unicode characters in phone string
  17. const static DWORD g_dwMaxLenId = 3; // Maximum number of ids that can be run together per phone string.
  18. // This number is 1 for SAPI converters but SR, TTS use this to encode one string into several ids
  19. // using in the form "aa 01235678".
  20. // The following defines used by the compression code for Lookup/Vendor lexicons
  21. #define MAXTOTALCBSIZE 9 // = CBSIZE + MAXELEMENTSIZE
  22. #define MAXELEMENTSIZE 5 // = greater of (LTSINDEXSIZE, POSSIZE)
  23. #define CBSIZE 4 // = LASTINFOFLAGSIZE + WORDINFOTYPESIZE
  24. #define LASTINFOFLAGSIZE 1
  25. #define WORDINFOTYPESIZE 3
  26. #define LTSINDEXSIZE 4
  27. #define POSSIZE 5 // a maximum of 32 parts of speech
  28. typedef enum tagSPLexWordInfoType
  29. {
  30. ePRON = 1,
  31. ePOS = 2
  32. } SPLEXWORDINFOTYPE;
  33. /*
  34. Control block layout
  35. struct CB
  36. {
  37. BYTE fLast : LASTINFOFLAGSIZE; // Is this the last Word Information piece
  38. BYTE WordInfoType : WORDINFOTYPESIZE; // Allow for 8 types
  39. };
  40. */
  41. typedef struct tagLookupLexInfo
  42. {
  43. GUID guidValidationId;
  44. GUID guidLexiconId;
  45. LANGID LangID;
  46. WORD wReserved;
  47. DWORD nNumberWords;
  48. DWORD nNumberProns;
  49. DWORD nMaxWordInfoLen;
  50. DWORD nLengthHashTable;
  51. DWORD nBitsPerHashEntry;
  52. DWORD nCompressedBlockBits;
  53. DWORD nWordCBSize;
  54. DWORD nPronCBSize;
  55. DWORD nPosCBSize;
  56. } LOOKUPLEXINFO, *PLOOKUPLEXINFO;
  57. typedef struct tagLtsLexInfo
  58. {
  59. GUID guidValidationId;
  60. GUID guidLexiconId;
  61. LANGID LangID;
  62. } LTSLEXINFO, *PLTSLEXINFO;
  63. // The following two typedefs used in Japanese and Chinese phone converters
  64. typedef struct SYLDIC
  65. {
  66. char *pKey;
  67. WCHAR *pString;
  68. } SYLDIC;
  69. typedef struct SYLDICW
  70. {
  71. WCHAR *pwKey;
  72. char *pString;
  73. } SYLDICW;
  74. //--- Validation functions ----------------------------------------------------
  75. inline BOOL SpIsBadLexType(DWORD dwFlag)
  76. {
  77. if (dwFlag != eLEXTYPE_USER &&
  78. dwFlag != eLEXTYPE_APP &&
  79. !(dwFlag >= eLEXTYPE_PRIVATE1 && dwFlag <= eLEXTYPE_PRIVATE20))
  80. {
  81. return TRUE;
  82. }
  83. else
  84. {
  85. return FALSE;
  86. }
  87. }
  88. inline BOOL SPIsBadPartOfSpeech(SPPARTOFSPEECH ePartOfSpeech)
  89. {
  90. SPPARTOFSPEECH eMask = (SPPARTOFSPEECH)~0xfff;
  91. SPPARTOFSPEECH ePOS = (SPPARTOFSPEECH)(ePartOfSpeech & eMask);
  92. if (ePartOfSpeech != SPPS_NotOverriden &&
  93. ePartOfSpeech != SPPS_Unknown &&
  94. ePOS != SPPS_Noun &&
  95. ePOS != SPPS_Verb &&
  96. ePOS != SPPS_Modifier &&
  97. ePOS != SPPS_Function &&
  98. ePOS != SPPS_Interjection)
  99. {
  100. return TRUE;
  101. }
  102. return FALSE;
  103. }
  104. inline BOOL SPIsBadLexWord(const WCHAR *pszWord)
  105. {
  106. return (SPIsBadStringPtr(pszWord) || !*pszWord || wcslen(pszWord) >= SP_MAX_WORD_LENGTH);
  107. }
  108. inline BOOL SPIsBadLexPronunciation(CComPtr<ISpPhoneConverter> spPhoneConv, const WCHAR *pszPronunciation)
  109. {
  110. HRESULT hr = S_OK;
  111. WCHAR szPhone[SP_MAX_PRON_LENGTH * (g_dwMaxLenPhone + 1)]; // we will not fail for lack of space
  112. if (SPIsBadStringPtr(pszPronunciation) || !*pszPronunciation ||
  113. (wcslen(pszPronunciation) >= SP_MAX_PRON_LENGTH))
  114. {
  115. return TRUE;
  116. }
  117. if (spPhoneConv)
  118. {
  119. hr = spPhoneConv->IdToPhone(pszPronunciation, szPhone);
  120. }
  121. return (FAILED(hr));
  122. }
  123. inline BOOL SPIsBadWordPronunciationList(SPWORDPRONUNCIATIONLIST *pWordPronunciationList)
  124. {
  125. return (SPIsBadWritePtr(pWordPronunciationList, sizeof(SPWORDPRONUNCIATIONLIST)) ||
  126. SPIsBadWritePtr(pWordPronunciationList->pvBuffer, pWordPronunciationList->ulSize));
  127. }
  128. inline BOOL SPIsBadWordList(SPWORDLIST *pWordList)
  129. {
  130. return (SPIsBadWritePtr(pWordList, sizeof(SPWORDLIST)) ||
  131. SPIsBadWritePtr(pWordList->pvBuffer, pWordList->ulSize));
  132. }
  133. inline HRESULT SPCopyPhoneString(const WCHAR *pszSource, WCHAR *pszTarget)
  134. {
  135. HRESULT hr = S_OK;
  136. if (SPIsBadWritePtr(pszTarget, (wcslen(pszSource) + 1) * sizeof(WCHAR)))
  137. {
  138. hr = E_INVALIDARG;
  139. }
  140. else
  141. {
  142. wcscpy(pszTarget, pszSource);
  143. }
  144. return hr;
  145. }
  146. /*****************************************************************************
  147. * GetWordHashValue *
  148. *------------------*
  149. *
  150. * Description:
  151. * Hash function for the Word hash tables. This hash function tries to create
  152. * a word hash value very dependant on the word text. The mean collison rate
  153. * on hash tables populated with this hash function is 1 per word access. This
  154. * result was when collisions were resolved using linear probing when
  155. * populating the hash table. Using non-linear probing might yield an even lower
  156. * mean collision rate.
  157. *
  158. * Return:
  159. * hash value
  160. **********************************************************************YUNUSM*/
  161. inline DWORD GetWordHashValue(PCWSTR pwszWord, // word string
  162. DWORD nLengthHash // length of hash table
  163. )
  164. {
  165. DWORD dHash = *pwszWord++;
  166. WCHAR c;
  167. WCHAR cPrev = (WCHAR)dHash;
  168. for (; *pwszWord; pwszWord++)
  169. {
  170. c = *pwszWord;
  171. dHash += ((c << (cPrev & 0x1F)) + (cPrev << (c & 0x1F)));
  172. cPrev = c;
  173. }
  174. return (((dHash << 16) - dHash) % nLengthHash);
  175. }
  176. /*******************************************************************************
  177. * ReallocSPWORDPRONList *
  178. *-----------------------*
  179. * Description:
  180. * Grow a SPWORDPRONUNCIATIONLIST if necessary
  181. *
  182. * Return:
  183. * S_OK
  184. * E_OUTOFMEMORY
  185. /**************************************************************** YUNUSM ******/
  186. inline HRESULT ReallocSPWORDPRONList(SPWORDPRONUNCIATIONLIST *pSPList, // buffer to grow
  187. DWORD dwSize // length to grow to
  188. )
  189. {
  190. SPDBG_FUNC("ReallocSPWORDPRONList");
  191. HRESULT hr = S_OK;
  192. if (pSPList->ulSize < dwSize)
  193. {
  194. BYTE *p = (BYTE *)CoTaskMemRealloc(pSPList->pvBuffer, dwSize);
  195. if (!p)
  196. {
  197. hr = E_OUTOFMEMORY;
  198. }
  199. else
  200. {
  201. pSPList->pvBuffer = p;
  202. pSPList->pFirstWordPronunciation = (SPWORDPRONUNCIATION *)p;
  203. pSPList->ulSize = dwSize;
  204. }
  205. }
  206. else
  207. {
  208. pSPList->pFirstWordPronunciation = (SPWORDPRONUNCIATION *)(pSPList->pvBuffer);
  209. }
  210. return hr;
  211. }
  212. /*******************************************************************************
  213. * ReallocSPWORDList *
  214. *-----------------------*
  215. * Description:
  216. * Grow a SPWORDLIST if necessary
  217. *
  218. * Return:
  219. * S_OK
  220. * E_OUTOFMEMORY
  221. /**************************************************************** YUNUSM ******/
  222. inline HRESULT ReallocSPWORDList(SPWORDLIST *pSPList, // buffer to grow
  223. DWORD dwSize // length to grow to
  224. )
  225. {
  226. SPDBG_FUNC("ReallocSPWORDList");
  227. HRESULT hr = S_OK;
  228. if (pSPList->ulSize < dwSize)
  229. {
  230. BYTE *p = (BYTE *)CoTaskMemRealloc(pSPList->pvBuffer, dwSize);
  231. if (!p)
  232. {
  233. hr = E_OUTOFMEMORY;
  234. }
  235. else
  236. {
  237. pSPList->pvBuffer = p;
  238. pSPList->pFirstWord = (SPWORD *)p;
  239. pSPList->ulSize = dwSize;
  240. }
  241. }
  242. else
  243. {
  244. pSPList->pFirstWord = (SPWORD *)(pSPList->pvBuffer);
  245. }
  246. return hr;
  247. }
  248. inline size_t PronSize(const WCHAR * const pwszPron)
  249. {
  250. // NB - SPWORDPRONUNCIATION struct size includes space for one SPPHONEID
  251. const size_t cb = sizeof(SPWORDPRONUNCIATION) + (wcslen(pwszPron) * sizeof(SPPHONEID));
  252. return (cb + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
  253. }
  254. inline size_t WordSize(const WCHAR * const pwszWord)
  255. {
  256. // SPWORD struct size with the aligned word size
  257. const size_t cb = sizeof(SPWORD) + ((wcslen(pwszWord) + 1) * sizeof(WCHAR));
  258. return (cb + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
  259. }
  260. /*******************************************************************************
  261. * CreateNextPronunciation *
  262. *-------------------------*
  263. * Description:
  264. * Returns a pointer to the location in the pronunciation array
  265. * where the next pronunciation in the list should start.
  266. * This function should be used only when creating the list.
  267. * Once the list is created, access the next pronunciation
  268. * through the ->pNextWordPronunciation member.
  269. *
  270. /**************************************************************** PACOGG ******/
  271. inline SPWORDPRONUNCIATION* CreateNextPronunciation(SPWORDPRONUNCIATION *pSpPron)
  272. {
  273. return (SPWORDPRONUNCIATION *)((BYTE *)pSpPron + PronSize(pSpPron->szPronunciation));
  274. }
  275. //--- End of File -------------------------------------------------------------