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.

320 lines
7.0 KiB

  1. /*
  2. * RUNT.C
  3. *
  4. * General-purpose functions and C runtime substitutes.
  5. *
  6. * Copyright 1992 - 1996 Microsoft Corporation. All Rights Reserved.
  7. */
  8. #include <_apipch.h>
  9. #define cbMinEntryID (CbNewENTRYID(sizeof(MAPIUID)))
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #ifndef PSTRCVR
  14. /* Functions related to the OLE Component Object model */
  15. STDAPI_(ULONG)
  16. UlRelease(LPVOID punk)
  17. {
  18. if (!punk)
  19. return 0L;
  20. AssertSz(!FBadUnknown((LPUNKNOWN) punk), TEXT("UlRelease: bad object ptr"));
  21. return ((LPUNKNOWN)punk)->lpVtbl->Release((LPUNKNOWN)punk);
  22. }
  23. STDAPI_(ULONG)
  24. UlAddRef(LPVOID punk)
  25. {
  26. AssertSz(!FBadUnknown((LPUNKNOWN) punk), TEXT("UlAddRef: bad object ptr"));
  27. return ((LPUNKNOWN)punk)->lpVtbl->AddRef((LPUNKNOWN)punk);
  28. }
  29. /* Functions related to the MAPI interface */
  30. /*
  31. * Retrieve a single property from a MAPIProp interface
  32. */
  33. STDAPI
  34. HrGetOneProp(LPMAPIPROP pmp, ULONG ulPropTag, LPSPropValue FAR *ppprop)
  35. {
  36. ULONG cValues;
  37. #ifndef WIN16
  38. SPropTagArray tg = { 1, { ulPropTag } };
  39. #else
  40. SPropTagArray tg;
  41. #endif
  42. HRESULT hr;
  43. #ifdef WIN16 // Set tg member's value.
  44. tg.cValues = 1;
  45. tg.aulPropTag[0] = ulPropTag;
  46. #endif
  47. AssertSz(!FBadUnknown((LPUNKNOWN) pmp), TEXT("HrGetOneProp: bad object ptr"));
  48. // Note: other parameters should be validated in the GetProps
  49. hr = pmp->lpVtbl->GetProps(pmp, &tg, MAPI_UNICODE, // ansi
  50. &cValues, ppprop);
  51. if (GetScode(hr) == MAPI_W_ERRORS_RETURNED)
  52. {
  53. hr = ResultFromScode((*ppprop)->Value.err);
  54. FreeBufferAndNull(ppprop); // Yes, we do want ppprop, not &ppprop
  55. }
  56. #ifdef DEBUG
  57. if (hr && GetScode(hr) != MAPI_E_NOT_FOUND) // too noisy
  58. DebugTraceResult(HrGetOneProp, hr);
  59. #endif
  60. return hr;
  61. }
  62. STDAPI
  63. HrSetOneProp(LPMAPIPROP pmp, LPSPropValue pprop)
  64. {
  65. HRESULT hr;
  66. LPSPropProblemArray pprob = NULL;
  67. AssertSz(!FBadUnknown((LPUNKNOWN) pmp), TEXT("HrSetOneProp: bad object ptr"));
  68. // Note: other parameters should be validated in the SetProps
  69. if (HR_SUCCEEDED(hr = pmp->lpVtbl->SetProps(pmp, 1, pprop, &pprob)))
  70. {
  71. if (pprob)
  72. {
  73. hr = ResultFromScode(pprob->aProblem->scode);
  74. FreeBufferAndNull(&pprob);
  75. }
  76. }
  77. // DebugTraceResult(HrSetOneProp, hr); // Too noisy
  78. return hr;
  79. }
  80. /*
  81. * Searches for a given property tag in a property tag array. If the
  82. * given property tag has type PT_UNSPECIFIED, matches only on the
  83. * property ID; otherwise, matches on the entire tag.
  84. */
  85. STDAPI_(BOOL)
  86. FPropExists(LPMAPIPROP pobj, ULONG ulPropTag)
  87. {
  88. LPSPropTagArray ptags = NULL;
  89. int itag;
  90. BOOL f = PROP_TYPE(ulPropTag) == PT_UNSPECIFIED;
  91. AssertSz(!FBadUnknown((LPUNKNOWN) pobj), TEXT("FPropExists: bad object ptr"));
  92. #ifdef DEBUG
  93. {
  94. HRESULT hr;
  95. if (hr = pobj->lpVtbl->GetPropList(pobj, MAPI_UNICODE, // ansi
  96. &ptags)) {
  97. DebugTraceResult(FPropExists, hr);
  98. return FALSE;
  99. }
  100. }
  101. #else
  102. if (pobj->lpVtbl->GetPropList(pobj, MAPI_UNICODE, // ansi
  103. &ptags)) {
  104. return(FALSE);
  105. }
  106. #endif
  107. for (itag = (int)(ptags->cValues - 1); itag >= 0; --itag) {
  108. if (ptags->aulPropTag[itag] == ulPropTag ||
  109. (f && PROP_ID(ptags->aulPropTag[itag]) == PROP_ID(ulPropTag))) {
  110. break;
  111. }
  112. }
  113. FreeBufferAndNull(&ptags);
  114. return(itag >= 0);
  115. }
  116. /*
  117. * Searches for a given property tag in a propset. If the given
  118. * property tag has type PT_UNSPECIFIED, matches only on the
  119. * property ID; otherwise, matches on the entire tag.
  120. */
  121. STDAPI_(LPSPropValue)
  122. PpropFindProp(LPSPropValue rgprop, ULONG cprop, ULONG ulPropTag)
  123. {
  124. BOOL f = PROP_TYPE(ulPropTag) == PT_UNSPECIFIED;
  125. LPSPropValue pprop = rgprop;
  126. if (!cprop || !rgprop)
  127. return NULL;
  128. AssertSz(!IsBadReadPtr(rgprop, (UINT)cprop*sizeof(SPropValue)), TEXT("PpropFindProp: rgprop fails address check"));
  129. while (cprop--)
  130. {
  131. if (pprop->ulPropTag == ulPropTag ||
  132. (f && PROP_ID(pprop->ulPropTag) == PROP_ID(ulPropTag)))
  133. return pprop;
  134. ++pprop;
  135. }
  136. return NULL;
  137. }
  138. /*
  139. * Destroys an SRowSet structure.
  140. */
  141. STDAPI_(void)
  142. FreeProws(LPSRowSet prows)
  143. {
  144. ULONG irow;
  145. if (! prows) {
  146. return;
  147. }
  148. //was: AssertSz(!FBadRowSet(prows), TEXT("FreeProws: prows fails address check"));
  149. #ifdef DEBUG
  150. if (FBadRowSet(prows)) {
  151. TraceSz( TEXT("FreeProws: prows fails address check"));
  152. }
  153. #endif // DEBUG
  154. for (irow = 0; irow < prows->cRows; ++irow) {
  155. MAPIFreeBuffer(prows->aRow[irow].lpProps);
  156. }
  157. FreeBufferAndNull(&prows);
  158. }
  159. /*
  160. * Destroys an ADRLIST structure.
  161. */
  162. STDAPI_(void)
  163. FreePadrlist(LPADRLIST padrlist)
  164. {
  165. ULONG iEntry;
  166. if (padrlist) {
  167. AssertSz(!FBadAdrList(padrlist), TEXT("FreePadrlist: padrlist fails address check"));
  168. for (iEntry = 0; iEntry < padrlist->cEntries; ++iEntry) {
  169. MAPIFreeBuffer(padrlist->aEntries[iEntry].rgPropVals);
  170. }
  171. FreeBufferAndNull(&padrlist);
  172. }
  173. }
  174. #endif // !PSTRCVR
  175. /* C runtime substitutes */
  176. //$ BUG? Assumes TEXT("first") byte of DBCS char in low byte of ch
  177. #if defined UNICODE
  178. #define FIsNextCh(_sz,_ch) (*_sz == _ch)
  179. #elif defined OLDSTUFF_DBCS
  180. #define FIsNextCh(_sz,_ch) (*((LPBYTE)_sz) != (BYTE)_ch && \
  181. (!IsDBCSLeadByte((BYTE)_ch) || ((LPBYTE)_sz)[1] == (_ch >> 8)))
  182. #else // string8
  183. #define FIsNextCh(_sz,_ch) (*_sz == _ch)
  184. #endif
  185. #if defined(DOS)
  186. #define TCharNext(sz) ((sz) + 1)
  187. #else
  188. #define TCharNext(sz) CharNext(sz)
  189. #endif
  190. #ifndef PSTRCVR
  191. STDAPI_(unsigned int)
  192. UFromSz(LPCTSTR sz)
  193. {
  194. unsigned int u = 0;
  195. unsigned int ch;
  196. AssertSz(!IsBadStringPtr(sz, 0xFFFF), TEXT("UFromSz: sz fails address check"));
  197. while ((ch = *sz) >= '0' && ch <= '9') {
  198. u = u * 10 + ch - '0';
  199. sz = TCharNext(sz);
  200. }
  201. return u;
  202. }
  203. #if 0
  204. // Original version from Dinarte's book: uses 1-relative indexes
  205. STDAPI_(void)
  206. ShellSort(LPVOID pv, UINT cv, LPVOID pvT, UINT cb, PFNSGNCMP fpCmp)
  207. {
  208. UINT i, j, h;
  209. for (h = 1; h <= cv / 9; h = 3*h+1)
  210. ;
  211. for (; h > 0; h /= 3)
  212. {
  213. for (i = h + 1; i <= cv; ++i)
  214. {
  215. MemCopy(pvT, (LPBYTE)pv + i*cb, cb);
  216. j = i;
  217. while (j > h && (*fpCmp)((LPBYTE)pv+(j-h)*cb, pvT) > 0)
  218. {
  219. MemCopy((LPBYTE)pv + j*cb, (LPBYTE)pv + (j-h)*cb, cb);
  220. j -= h;
  221. }
  222. MemCopy((LPBYTE)pv+j*cb, pvT, cb);
  223. }
  224. }
  225. }
  226. #else
  227. #define pELT(_i) ((LPBYTE)pv + (_i-1)*cb)
  228. STDAPI_(void)
  229. ShellSort(LPVOID pv, UINT cv, LPVOID pvT, UINT cb, PFNSGNCMP fpCmp)
  230. {
  231. UINT i, j, h;
  232. AssertSz(!IsBadWritePtr(pv, cv*cb), TEXT("ShellSort: pv fails address check"));
  233. AssertSz(!IsBadCodePtr((FARPROC) fpCmp), TEXT("ShellSort: fpCmp fails address check"));
  234. for (h = 1; h <= cv / 9; h = 3*h+1)
  235. ;
  236. for (; h > 0; h /= 3)
  237. {
  238. for (i = h+1; i <= cv; ++i)
  239. {
  240. MemCopy(pvT, pELT(i), cb);
  241. j = i;
  242. while (j > h && (*fpCmp)(pELT(j-h), pvT) > 0)
  243. {
  244. MemCopy(pELT(j), pELT(j-h), cb);
  245. j -= h;
  246. }
  247. MemCopy(pELT(j), pvT, cb);
  248. }
  249. }
  250. }
  251. #undef pELT
  252. #endif
  253. #endif // !PSTRCVR
  254. #ifdef __cplusplus
  255. }
  256. #endif