Source code of Windows XP (NT5)
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.

315 lines
9.0 KiB

  1. /*************************************************************************
  2. * *
  3. * UTIL.C *
  4. * *
  5. * Copyright (C) Microsoft Corporation 1990-1994 *
  6. * All Rights reserved. *
  7. * *
  8. **************************************************************************
  9. * *
  10. * Module Intent *
  11. * General purpose routines *
  12. * *
  13. **************************************************************************
  14. * *
  15. * Current Owner: BinhN *
  16. * *
  17. **************************************************************************
  18. * *
  19. * Released by Development: (date) *
  20. * *
  21. *************************************************************************/
  22. #include <mvopsys.h>
  23. #include <mem.h>
  24. #include <mvsearch.h>
  25. #include "common.h"
  26. #ifdef _DEBUG
  27. static BYTE NEAR s_aszModule[] = __FILE__; /* Used by error return functions.*/
  28. #endif
  29. /*************************************************************************
  30. *
  31. * INTERNAL GLOBAL FUNCTIONS
  32. * Those functions should be declared FAR to cause less problems with
  33. * with inter-segment calls, unless they are explicitly known to be
  34. * called from the same segment. Those functions should be declared
  35. * in an internal include file
  36. *************************************************************************/
  37. PUBLIC BOOL PASCAL FAR StringDiff (LPB, LPB);
  38. PUBLIC BOOL PASCAL FAR StringDiff2 (LPB, LPB);
  39. PUBLIC WORD PASCAL FAR CbByteUnpack(LPDW, LPB);
  40. PUBLIC int PASCAL FAR NCmpS (LPB, LPB);
  41. PUBLIC int PASCAL FAR StrNoCaseCmp (LPB, LPB, WORD);
  42. PUBLIC DWORD PASCAL FAR GetMacLong (LPB);
  43. PUBLIC WORD PASCAL FAR GetMacWord (LPB);
  44. PUBLIC DWORD PASCAL FAR SwapLong (DWORD);
  45. PUBLIC WORD PASCAL FAR SwapWord (WORD);
  46. PUBLIC int PASCAL FAR SwapBuffer (LPW, DWORD);
  47. /*************************************************************************
  48. * @doc INTERNAL
  49. *
  50. * @func BOOL PASCAL FAR | StringDiff |
  51. * Given two pascal strings, this function will check to see if
  52. * they are identical
  53. *
  54. * @parm LPB | lpStr1 |
  55. * First Pascal string
  56. *
  57. * @parm LPB | lpStr2 |
  58. * Second Pascal string
  59. *
  60. * @rdesc TRUE if the 2 strings are different, else FALSE
  61. *
  62. * @comm This function is for speed so no strings validity is checked
  63. *************************************************************************/
  64. PUBLIC BOOL PASCAL FAR StringDiff (LPB lpStr1, LPB lpStr2)
  65. {
  66. register BYTE cByte;
  67. /* Check the strings' lengths. If they are different then done */
  68. if ((cByte = *lpStr1++) != *lpStr2++)
  69. return TRUE;
  70. /* Check invidual bytes */
  71. for (; cByte > 0; cByte--) {
  72. if (*lpStr1++ != *lpStr2++)
  73. return TRUE;
  74. }
  75. return FALSE;
  76. }
  77. /*************************************************************************
  78. * @doc INTERNAL
  79. *
  80. * @func BOOL PASCAL FAR | StringDiff2 |
  81. * Given two pascal strings, this function will check to see if
  82. * they are identical. Both strings must have 2 byte size fields
  83. *
  84. * @parm LPB | lpStr1 |
  85. * First Pascal string
  86. *
  87. * @parm LPB | lpStr2 |
  88. * Second Pascal string
  89. *
  90. * @rdesc TRUE if the 2 strings are different, else FALSE
  91. *
  92. * @comm This function is for speed so no strings validity is checked
  93. *************************************************************************/
  94. PUBLIC BOOL PASCAL FAR StringDiff2 (LPB lpStr1, LPB lpStr2)
  95. {
  96. register WORD cByte;
  97. /* Check the strings' lengths. If they are different then done */
  98. // erinfox: GETWORD byte-swaps on Mac
  99. if ((cByte = *(WORD UNALIGNED * UNALIGNED)(lpStr1)) != *(WORD UNALIGNED * UNALIGNED)(lpStr2))
  100. return TRUE;
  101. lpStr1 += sizeof (SHORT);
  102. lpStr2 += sizeof (SHORT);
  103. /* Check invidual bytes */
  104. for (; cByte > 0; cByte--) {
  105. if (*lpStr1++ != *lpStr2++)
  106. return TRUE;
  107. }
  108. return FALSE;
  109. }
  110. /*************************************************************************
  111. * @doc INTERNAL
  112. *
  113. * @func WORD PASCAL FAR | CbByteUnpack |
  114. * Unpack a number
  115. *
  116. * @parm LPDW | lpdwOut |
  117. * Pointer to result number
  118. *
  119. * @parm LPB | lpbIn |
  120. * Input compacted sequence of bytes
  121. *
  122. * @rdesc The function will return the number of bytes scanned. The
  123. * content of lpdwOut is updated
  124. *************************************************************************/
  125. PUBLIC WORD PASCAL FAR CbByteUnpack(LPDW lpdwOut, LPB lpbIn)
  126. {
  127. register int cb = 1;
  128. register unsigned int cShift = 7;
  129. DWORD dwSave = 0;
  130. dwSave |= *lpbIn & 0x7F;
  131. while (*lpbIn & 0x80) { /* Hi-bit set */
  132. lpbIn++;
  133. dwSave |= ((DWORD)(*lpbIn & 0x7F)) << cShift;
  134. cb++;
  135. cShift += 7;
  136. }
  137. *lpdwOut = dwSave;
  138. return((WORD) cb);
  139. }
  140. /*************************************************************************
  141. * @doc INTERNAL
  142. *
  143. * @func int PASCAL FAR | NCmpS |
  144. * This function will compare two pascal strings
  145. *
  146. * @parm LPB | lpStr1 |
  147. * First Pascal string
  148. *
  149. * @parm LPB | lpStr2 |
  150. * Second Pascal string
  151. *
  152. * @rdesc The returned values are:
  153. * < 0 : lpStr1 < lpStr2
  154. * = 0 : if the strings are identical
  155. * > 0 : lpStr1 > lpStr2
  156. *
  157. * @comm This function is for speed so no strings validity is checked
  158. *************************************************************************/
  159. PUBLIC int PASCAL FAR NCmpS (LPB lpStr1, LPB lpStr2)
  160. {
  161. register int wlen;
  162. register int fRet;
  163. /* Get the minimum length */
  164. if ((fRet = *(LPUW)lpStr1 - *(LPUW)lpStr2) > 0)
  165. wlen = *(LPUW)lpStr2;
  166. else
  167. wlen = *(LPUW)lpStr1;
  168. /* Skip the lengths */
  169. lpStr1 += sizeof(WORD);
  170. lpStr2 += sizeof(WORD);
  171. /* Start compare byte per byte */
  172. for (; wlen > 0; wlen--, lpStr1++, lpStr2++) {
  173. if (*lpStr1 != *lpStr2)
  174. break;
  175. }
  176. if (wlen == 0) return fRet;
  177. return (*lpStr1 - *lpStr2);
  178. }
  179. /*************************************************************************
  180. * @doc INTERNAL
  181. *
  182. * @func int PASCAL FAR | StrNoCaseCmp |
  183. * String compare with case insensitive
  184. *
  185. * @parm LPB | lpb1 |
  186. * Pointer to string 1
  187. *
  188. * @parm LPB | lpb2 |
  189. * Pointer to string 2
  190. *
  191. * @parm WORD | wLen |
  192. * String length
  193. *
  194. * @rdesc 0 if the 2 strings match, 1 if not
  195. *************************************************************************/
  196. PUBLIC int PASCAL FAR StrNoCaseCmp (LPB lpb1, LPB lpb2, WORD wLen)
  197. {
  198. int fRet;
  199. for (; wLen > 0; wLen--, lpb1++, lpb2++) {
  200. if ((fRet = ((*lpb1 | 0x20) != (*lpb2 | 0x20))))
  201. return fRet;
  202. }
  203. return 0;
  204. }
  205. PUBLIC VOID PASCAL FAR FreeHandle (HANDLE hd)
  206. {
  207. if (hd) {
  208. _GLOBALUNLOCK(hd);
  209. _GLOBALFREE(hd);
  210. }
  211. }
  212. #if defined(_DEBUG) && !defined(_MSDN) && !defined(MOSMAP)
  213. /*************************************************************************
  214. * @doc INTERNAL INDEX RETRIEVAL
  215. *
  216. * @func LPV PASCAL FAR | DebugGlobalLockedStructMemAlloc |
  217. * This function allocates and return a pointer to a block of
  218. * memory. The first element of the structure must be the handle
  219. * to this block of memory
  220. *
  221. * @parm WORD | size |
  222. * Size of the structure block.
  223. *
  224. * @parm LSZ | szFilename |
  225. * Filename where the call is made
  226. *
  227. * @parm int | Line |
  228. * Source line where the call is made
  229. *
  230. * @rdesc NULL if OOM, or pointer to the structure
  231. *************************************************************************/
  232. PUBLIC LPV PASCAL FAR DebugGlobalLockedStructMemAlloc (unsigned int size,
  233. LSZ szFilename, WORD Line)
  234. {
  235. HANDLE hMem;
  236. HANDLE FAR *lpMem;
  237. if ((hMem = _GlobalAlloc(DLLGMEM_ZEROINIT, (DWORD)size,
  238. szFilename, Line)) == 0)
  239. return NULL;
  240. lpMem = (HANDLE FAR *)_GLOBALLOCK(hMem);
  241. *lpMem = hMem;
  242. return (LPV)lpMem;
  243. }
  244. #endif
  245. #if 0
  246. /*************************************************************************
  247. * @doc INTERNAL INDEX RETRIEVAL
  248. *
  249. * @func BOOL PASCAL FAR | MapErr |
  250. * Map an MVFS error to index/search error
  251. *
  252. * @parm WORD | error |
  253. * mvfs error
  254. *
  255. * @rdesc Return indexer/searcher error
  256. *************************************************************************/
  257. PUBLIC BOOL PASCAL FAR MapErr(WORD error)
  258. {
  259. switch (error) {
  260. case ERR_FAILED:
  261. return FAIL;
  262. case ERR_SUCCESS:
  263. return SUCCEED;
  264. case ERR_EXIST:
  265. return ERR_EXIST;
  266. case ERR_INVALID:
  267. case ERR_INVALID_HANDLE:
  268. return ERR_INVALID_FS_FILE;
  269. case ERR_BADARG:
  270. case ERR_NOTSUPPORTED:
  271. return ERR_BADARG;
  272. case ERR_MEMORY:
  273. return ERR_MEMORY;
  274. case ERR_NOPERMISSION:
  275. return ERR_CANTWRITE;
  276. case ERR_BADVERSION:
  277. return ERR_BADVERSION;
  278. case ERR_DISKFULL:
  279. return ERR_DISKFULL;
  280. case ERR_NOHANDLE:
  281. return ERR_NOHANDLE;
  282. case ERR_BUFOVERFLOW:
  283. return ERR_TREETOOBIG;
  284. case ERR_ASSERT:
  285. default:
  286. return ERR_ASSERT;
  287. }
  288. }
  289. #endif