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.

288 lines
6.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows NT Security
  4. // Copyright (C) Microsoft Corporation, 1997 - 1999
  5. //
  6. // File: dbutils.cpp
  7. //
  8. // Contents: utilities
  9. //
  10. // History: 07-Feb-00 reidk Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <windows.h>
  14. #include <dbgdef.h>
  15. extern void * _CatDBAlloc(size_t len);
  16. extern void * _CatDBReAlloc(void *p, size_t len);
  17. extern void _CatDBFree(void *p);
  18. LPSTR _CatDBConvertWszToSz(LPCWSTR pwsz)
  19. {
  20. LPSTR psz = NULL;
  21. LONG cch = 0;
  22. cch = WideCharToMultiByte(
  23. GetACP(),
  24. 0, // dwFlags
  25. pwsz,
  26. -1, // cchWideChar, -1 => null terminated
  27. NULL,
  28. 0,
  29. NULL,
  30. NULL);
  31. if (cch == 0)
  32. {
  33. goto ErrorWideCharToMultiByte;
  34. }
  35. if (NULL == (psz = (CHAR *) _CatDBAlloc(cch + 1)))
  36. {
  37. goto ErrorMemory;
  38. }
  39. psz[cch] = '\0';
  40. if (0 == WideCharToMultiByte(
  41. GetACP(),
  42. 0, // dwFlags
  43. pwsz,
  44. -1, // cchWideChar, -1 => null terminated
  45. psz,
  46. cch,
  47. NULL,
  48. NULL))
  49. {
  50. goto ErrorWideCharToMultiByte;
  51. }
  52. CommonReturn:
  53. return (psz);
  54. ErrorReturn:
  55. if (psz != NULL)
  56. {
  57. _CatDBFree(psz);
  58. }
  59. psz = NULL;
  60. goto CommonReturn;
  61. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorWideCharToMultiByte)
  62. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  63. }
  64. LPWSTR _CATDBAllocAndCopyWSTR(LPCWSTR pwsz)
  65. {
  66. LPWSTR pwszTemp = NULL;
  67. if (NULL == (pwszTemp =
  68. (LPWSTR) _CatDBAlloc(sizeof(WCHAR) * (wcslen(pwsz) + 1) )))
  69. {
  70. goto ErrorMemory;
  71. }
  72. wcscpy(pwszTemp, pwsz);
  73. CommonReturn:
  74. return (pwszTemp);
  75. ErrorReturn:
  76. goto CommonReturn;
  77. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  78. }
  79. LPWSTR _CATDBAllocAndCopyWSTR2(LPCWSTR pwsz1, LPCWSTR pwsz2)
  80. {
  81. LPWSTR pwszTemp = NULL;
  82. if (NULL == (pwszTemp =
  83. (LPWSTR) _CatDBAlloc(sizeof(WCHAR) * ( wcslen(pwsz1) +
  84. wcslen(pwsz2) +
  85. 1))))
  86. {
  87. goto ErrorMemory;
  88. }
  89. wcscpy(pwszTemp, pwsz1);
  90. wcscat(pwszTemp, pwsz2);
  91. CommonReturn:
  92. return (pwszTemp);
  93. ErrorReturn:
  94. goto CommonReturn;
  95. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  96. }
  97. BOOL _CATDBStrCatWSTR(LPWSTR *ppwszAddTo, LPCWSTR pwszAdd)
  98. {
  99. BOOL fRet = TRUE;
  100. LPWSTR pwszTemp = NULL;
  101. if (NULL == (pwszTemp = (LPWSTR) _CatDBAlloc(sizeof(WCHAR) * ( wcslen(*ppwszAddTo) +
  102. wcslen(pwszAdd) +
  103. 1))))
  104. {
  105. goto ErrorMemory;
  106. }
  107. wcscpy(pwszTemp, *ppwszAddTo);
  108. wcscat(pwszTemp, pwszAdd);
  109. _CatDBFree(*ppwszAddTo);
  110. *ppwszAddTo = pwszTemp;
  111. CommonReturn:
  112. return (fRet);
  113. ErrorReturn:
  114. fRet = FALSE;
  115. goto CommonReturn;
  116. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  117. }
  118. BOOL _CATDBStrCat(LPSTR *ppszAddTo, LPCSTR pszAdd)
  119. {
  120. BOOL fRet = TRUE;
  121. LPSTR pszTemp = NULL;
  122. if (NULL == (pszTemp = (LPSTR) _CatDBAlloc(sizeof(char) * ( strlen(*ppszAddTo) +
  123. strlen(pszAdd) +
  124. 1))))
  125. {
  126. goto ErrorMemory;
  127. }
  128. strcpy(pszTemp, *ppszAddTo);
  129. strcat(pszTemp, pszAdd);
  130. _CatDBFree(*ppszAddTo);
  131. *ppszAddTo = pszTemp;
  132. CommonReturn:
  133. return (fRet);
  134. ErrorReturn:
  135. fRet = FALSE;
  136. goto CommonReturn;
  137. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  138. }
  139. LPWSTR _CATDBConstructWSTRPath(LPCWSTR pwsz1, LPCWSTR pwsz2)
  140. {
  141. LPWSTR pwszTemp = NULL;
  142. int nTotalLen = 0;
  143. int nLenStr1 = 0;
  144. //
  145. // Calculate the length of the resultant string as the sum of the length
  146. // of pwsz1, length of pwsz2, a NULL char, and a possible extra '\' char
  147. //
  148. nLenStr1 = wcslen(pwsz1);
  149. nTotalLen = nLenStr1 + wcslen(pwsz2) + 2;
  150. //
  151. // Allocate the string and copy pwsz1 into the buffer
  152. //
  153. if (NULL == (pwszTemp = (LPWSTR) _CatDBAlloc(sizeof(WCHAR) * nTotalLen)))
  154. {
  155. goto ErrorMemory;
  156. }
  157. wcscpy(pwszTemp, pwsz1);
  158. //
  159. // Add the extra '\' if needed
  160. //
  161. if (pwsz1[nLenStr1 - 1] != L'\\')
  162. {
  163. pwszTemp[nLenStr1] = L'\\';
  164. pwszTemp[nLenStr1 + 1] = L'\0';
  165. }
  166. //
  167. // Tack on pwsz2
  168. //
  169. wcscat(pwszTemp, pwsz2);
  170. CommonReturn:
  171. return (pwszTemp);
  172. ErrorReturn:
  173. goto CommonReturn;
  174. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  175. }
  176. LPSTR _CATDBConstructPath(LPCSTR psz1, LPCSTR psz2)
  177. {
  178. LPSTR pszTemp = NULL;
  179. int nTotalLen = 0;
  180. int nLenStr1 = 0;
  181. //
  182. // Calculate the length of the resultant string as the sum of the length
  183. // of psz1, length of psz2, a NULL char, and a possible extra '\' char
  184. //
  185. nLenStr1 = strlen(psz1);
  186. nTotalLen = nLenStr1 + strlen(psz2) + 2;
  187. //
  188. // Allocate the string and copy pwsz1 into the buffer
  189. //
  190. if (NULL == (pszTemp =
  191. (LPSTR) _CatDBAlloc(sizeof(char) * nTotalLen)))
  192. {
  193. goto ErrorMemory;
  194. }
  195. strcpy(pszTemp, psz1);
  196. //
  197. // Add the extra '\' if needed
  198. //
  199. if (psz1[nLenStr1 - 1] != '\\')
  200. {
  201. pszTemp[nLenStr1] = '\\';
  202. pszTemp[nLenStr1 + 1] = '\0';
  203. }
  204. //
  205. // Tack on pwsz2
  206. //
  207. strcat(pszTemp, psz2);
  208. CommonReturn:
  209. return (pszTemp);
  210. ErrorReturn:
  211. goto CommonReturn;
  212. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  213. }