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.

286 lines
5.5 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. if (0 == WideCharToMultiByte(
  40. GetACP(),
  41. 0, // dwFlags
  42. pwsz,
  43. -1, // cchWideChar, -1 => null terminated
  44. psz,
  45. cch,
  46. NULL,
  47. NULL))
  48. {
  49. goto ErrorWideCharToMultiByte;
  50. }
  51. CommonReturn:
  52. return (psz);
  53. ErrorReturn:
  54. if (psz != NULL)
  55. {
  56. _CatDBFree(psz);
  57. }
  58. psz = NULL;
  59. goto CommonReturn;
  60. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorWideCharToMultiByte)
  61. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  62. }
  63. LPWSTR _CATDBAllocAndCopyWSTR(LPCWSTR pwsz)
  64. {
  65. LPWSTR pwszTemp = NULL;
  66. if (NULL == (pwszTemp =
  67. (LPWSTR) _CatDBAlloc(sizeof(WCHAR) * (wcslen(pwsz) + 1) )))
  68. {
  69. goto ErrorMemory;
  70. }
  71. wcscpy(pwszTemp, pwsz);
  72. CommonReturn:
  73. return (pwszTemp);
  74. ErrorReturn:
  75. goto CommonReturn;
  76. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  77. }
  78. LPWSTR _CATDBAllocAndCopyWSTR2(LPCWSTR pwsz1, LPCWSTR pwsz2)
  79. {
  80. LPWSTR pwszTemp = NULL;
  81. if (NULL == (pwszTemp =
  82. (LPWSTR) _CatDBAlloc(sizeof(WCHAR) * ( wcslen(pwsz1) +
  83. wcslen(pwsz2) +
  84. 1))))
  85. {
  86. goto ErrorMemory;
  87. }
  88. wcscpy(pwszTemp, pwsz1);
  89. wcscat(pwszTemp, pwsz2);
  90. CommonReturn:
  91. return (pwszTemp);
  92. ErrorReturn:
  93. goto CommonReturn;
  94. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  95. }
  96. BOOL _CATDBStrCatWSTR(LPWSTR *ppwszAddTo, LPCWSTR pwszAdd)
  97. {
  98. BOOL fRet = TRUE;
  99. LPWSTR pwszTemp = NULL;
  100. if (NULL == (pwszTemp = (LPWSTR) _CatDBAlloc(sizeof(WCHAR) * ( wcslen(*ppwszAddTo) +
  101. wcslen(pwszAdd) +
  102. 1))))
  103. {
  104. goto ErrorMemory;
  105. }
  106. wcscpy(pwszTemp, *ppwszAddTo);
  107. wcscat(pwszTemp, pwszAdd);
  108. _CatDBFree(*ppwszAddTo);
  109. *ppwszAddTo = pwszTemp;
  110. CommonReturn:
  111. return (fRet);
  112. ErrorReturn:
  113. fRet = FALSE;
  114. goto CommonReturn;
  115. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  116. }
  117. BOOL _CATDBStrCat(LPSTR *ppszAddTo, LPCSTR pszAdd)
  118. {
  119. BOOL fRet = TRUE;
  120. LPSTR pszTemp = NULL;
  121. if (NULL == (pszTemp = (LPSTR) _CatDBAlloc(sizeof(char) * ( strlen(*ppszAddTo) +
  122. strlen(pszAdd) +
  123. 1))))
  124. {
  125. goto ErrorMemory;
  126. }
  127. strcpy(pszTemp, *ppszAddTo);
  128. strcat(pszTemp, pszAdd);
  129. _CatDBFree(*ppszAddTo);
  130. *ppszAddTo = pszTemp;
  131. CommonReturn:
  132. return (fRet);
  133. ErrorReturn:
  134. fRet = FALSE;
  135. goto CommonReturn;
  136. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  137. }
  138. LPWSTR _CATDBConstructWSTRPath(LPCWSTR pwsz1, LPCWSTR pwsz2)
  139. {
  140. LPWSTR pwszTemp = NULL;
  141. int nTotalLen = 0;
  142. int nLenStr1 = 0;
  143. //
  144. // Calculate the length of the resultant string as the sum of the length
  145. // of pwsz1, length of pwsz2, a NULL char, and a possible extra '\' char
  146. //
  147. nLenStr1 = wcslen(pwsz1);
  148. nTotalLen = nLenStr1 + wcslen(pwsz2) + 2;
  149. //
  150. // Allocate the string and copy pwsz1 into the buffer
  151. //
  152. if (NULL == (pwszTemp = (LPWSTR) _CatDBAlloc(sizeof(WCHAR) * nTotalLen)))
  153. {
  154. goto ErrorMemory;
  155. }
  156. wcscpy(pwszTemp, pwsz1);
  157. //
  158. // Add the extra '\' if needed
  159. //
  160. if (pwsz1[nLenStr1 - 1] != L'\\')
  161. {
  162. pwszTemp[nLenStr1] = L'\\';
  163. pwszTemp[nLenStr1 + 1] = L'\0';
  164. }
  165. //
  166. // Tack on pwsz2
  167. //
  168. wcscat(pwszTemp, pwsz2);
  169. CommonReturn:
  170. return (pwszTemp);
  171. ErrorReturn:
  172. goto CommonReturn;
  173. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  174. }
  175. LPSTR _CATDBConstructPath(LPCSTR psz1, LPCSTR psz2)
  176. {
  177. LPSTR pszTemp = NULL;
  178. int nTotalLen = 0;
  179. int nLenStr1 = 0;
  180. //
  181. // Calculate the length of the resultant string as the sum of the length
  182. // of psz1, length of psz2, a NULL char, and a possible extra '\' char
  183. //
  184. nLenStr1 = strlen(psz1);
  185. nTotalLen = nLenStr1 + strlen(psz2) + 2;
  186. //
  187. // Allocate the string and copy pwsz1 into the buffer
  188. //
  189. if (NULL == (pszTemp =
  190. (LPSTR) _CatDBAlloc(sizeof(char) * nTotalLen)))
  191. {
  192. goto ErrorMemory;
  193. }
  194. strcpy(pszTemp, psz1);
  195. //
  196. // Add the extra '\' if needed
  197. //
  198. if (psz1[nLenStr1 - 1] != '\\')
  199. {
  200. pszTemp[nLenStr1] = '\\';
  201. pszTemp[nLenStr1 + 1] = '\0';
  202. }
  203. //
  204. // Tack on pwsz2
  205. //
  206. strcat(pszTemp, psz2);
  207. CommonReturn:
  208. return (pszTemp);
  209. ErrorReturn:
  210. goto CommonReturn;
  211. TRACE_ERROR_EX(DBG_SS_CATDBSVC, ErrorMemory)
  212. }