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.

330 lines
7.5 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Active Directory 1.1 Sample Code
  4. //
  5. // Copyright (C) Microsoft Corporation, 1992 - 1995
  6. //
  7. // File: util.cxx
  8. //
  9. // Contents: Ansi to Unicode conversions and misc helper functions
  10. //
  11. //----------------------------------------------------------------------------//------------------------------------------------------------------------------
  12. //
  13. // System Includes
  14. //
  15. #define INC_OLE2
  16. #include <windows.h>
  17. //
  18. // CRunTime Includes
  19. //
  20. #include <stdlib.h>
  21. #include <limits.h>
  22. #include <io.h>
  23. #include <stdio.h>
  24. //
  25. // Public OleDs includes
  26. //
  27. //
  28. // Private defines
  29. //
  30. #define BAIL_ON_NULL(p) \
  31. if (!(p)) { \
  32. goto error; \
  33. }
  34. #define BAIL_ON_FAILURE(hr) \
  35. if (FAILED(hr)) { \
  36. goto error; \
  37. }
  38. #include "activeds.h"
  39. #include "main.hxx"
  40. #include "adsi.h"
  41. void
  42. PrintUsage(
  43. void
  44. )
  45. {
  46. printf("\nUsage: csearch /b <baseObject> /f <search_filter> /a <attrlist> [/p <preference=value>] ");
  47. printf(" [/u <UserName> <Password>] [/t <flagName>=<value> \n");
  48. printf("\n where:\n" );
  49. printf(" baseObject = ADsPath of the base of the search\n");
  50. printf(" search_filter = search filter string in LDAP format\n" );
  51. printf(" attrlist = list of the attributes to display\n" );
  52. printf(" preference could be one of:\n");
  53. printf(" Asynchronous, AttrTypesOnly, DerefAliases, SizeLimit, TimeLimit,\n");
  54. printf(" TimeOut, PageSize, SearchScope, SecureAuth and EncryptPassword\n");
  55. printf(" flagName could be one of:\n");
  56. printf(" SecureAuth or UseEncrypt\n");
  57. printf(" value is yes/no for a Boolean and the respective integer for integers\n");
  58. printf(" scope is one of \"Base\", \"OneLevel\", or \"Subtree\"\n");
  59. printf("\nFor Example: csearch /b \"LDAP://ntdsdc0/O=Internet/DC=COM/");
  60. printf("DC=MICROSOFT/DC=NTDEV\" /f \"objectClass=Group\" /a \"ADsPath, name, description\" ");
  61. printf(" /u \"CN=NTDS,CN=Users,DC=NTDEV,DC=MICROSOFT,DC=COM,O=INTERNET\" \"\" \n");
  62. }
  63. //
  64. // Print the data depending on its type.
  65. //
  66. void
  67. PrintColumn(
  68. PADS_SEARCH_COLUMN pColumn,
  69. LPWSTR pszColumnName
  70. )
  71. {
  72. ULONG i, j, k;
  73. if (!pColumn) {
  74. return;
  75. }
  76. wprintf(
  77. L"%s = ",
  78. pszColumnName
  79. );
  80. for (k=0; k < pColumn->dwNumValues; k++) {
  81. if (k > 0)
  82. wprintf(L"# ");
  83. switch(pColumn->dwADsType) {
  84. case ADSTYPE_DN_STRING :
  85. wprintf(
  86. L"%s ",
  87. (LPWSTR) pColumn->pADsValues[k].DNString
  88. );
  89. break;
  90. case ADSTYPE_CASE_EXACT_STRING :
  91. wprintf(
  92. L"%s ",
  93. (LPWSTR) pColumn->pADsValues[k].CaseExactString
  94. );
  95. break;
  96. case ADSTYPE_CASE_IGNORE_STRING:
  97. wprintf(
  98. L"%s ",
  99. (LPWSTR) pColumn->pADsValues[k].CaseIgnoreString
  100. );
  101. break;
  102. case ADSTYPE_PRINTABLE_STRING :
  103. wprintf(
  104. L"%s ",
  105. (LPWSTR) pColumn->pADsValues[k].PrintableString
  106. );
  107. break;
  108. case ADSTYPE_NUMERIC_STRING :
  109. wprintf(
  110. L"%s ",
  111. (LPWSTR) pColumn->pADsValues[k].NumericString
  112. );
  113. break;
  114. case ADSTYPE_OBJECT_CLASS :
  115. wprintf(
  116. L"%s ",
  117. (LPWSTR) pColumn->pADsValues[k].ClassName
  118. );
  119. break;
  120. case ADSTYPE_BOOLEAN :
  121. wprintf(
  122. L"%s ",
  123. (DWORD) pColumn->pADsValues[k].Boolean ?
  124. L"TRUE" : L"FALSE"
  125. );
  126. break;
  127. case ADSTYPE_INTEGER :
  128. wprintf(
  129. L"%d ",
  130. (DWORD) pColumn->pADsValues[k].Integer
  131. );
  132. break;
  133. case ADSTYPE_OCTET_STRING :
  134. for (j=0; j<pColumn->pADsValues[k].OctetString.dwLength; j++) {
  135. printf(
  136. "%02x",
  137. ((BYTE *)pColumn->pADsValues[k].OctetString.lpValue)[j]
  138. );
  139. }
  140. break;
  141. case ADSTYPE_LARGE_INTEGER :
  142. wprintf(
  143. L"%I64d ",
  144. pColumn->pADsValues[k].LargeInteger
  145. );
  146. break;
  147. case ADSTYPE_UTC_TIME :
  148. wprintf(
  149. L"(date value) "
  150. );
  151. break;
  152. case ADSTYPE_PROV_SPECIFIC :
  153. wprintf(
  154. L"(provider specific value) "
  155. );
  156. break;
  157. }
  158. }
  159. printf("\n");
  160. }
  161. int
  162. AnsiToUnicodeString(
  163. LPSTR pAnsi,
  164. LPWSTR pUnicode,
  165. DWORD StringLength
  166. )
  167. {
  168. int iReturn;
  169. if( StringLength == NULL_TERMINATED )
  170. StringLength = strlen( pAnsi );
  171. iReturn = MultiByteToWideChar(CP_ACP,
  172. MB_PRECOMPOSED,
  173. pAnsi,
  174. StringLength + 1,
  175. pUnicode,
  176. StringLength + 1 );
  177. //
  178. // Ensure NULL termination.
  179. //
  180. pUnicode[StringLength] = 0;
  181. return iReturn;
  182. }
  183. int
  184. UnicodeToAnsiString(
  185. LPWSTR pUnicode,
  186. LPSTR pAnsi,
  187. DWORD StringLength
  188. )
  189. {
  190. LPSTR pTempBuf = NULL;
  191. INT rc = 0;
  192. if( StringLength == NULL_TERMINATED ) {
  193. //
  194. // StringLength is just the
  195. // number of characters in the string
  196. //
  197. StringLength = wcslen( pUnicode );
  198. }
  199. //
  200. // WideCharToMultiByte doesn't NULL terminate if we're copying
  201. // just part of the string, so terminate here.
  202. //
  203. pUnicode[StringLength] = 0;
  204. //
  205. // Include one for the NULL
  206. //
  207. StringLength++;
  208. //
  209. // Unfortunately, WideCharToMultiByte doesn't do conversion in place,
  210. // so allocate a temporary buffer, which we can then copy:
  211. //
  212. if( pAnsi == (LPSTR)pUnicode )
  213. {
  214. pTempBuf = (LPSTR)LocalAlloc( LPTR, StringLength );
  215. pAnsi = pTempBuf;
  216. }
  217. if( pAnsi )
  218. {
  219. rc = WideCharToMultiByte( CP_ACP,
  220. 0,
  221. pUnicode,
  222. StringLength,
  223. pAnsi,
  224. StringLength,
  225. NULL,
  226. NULL );
  227. }
  228. /* If pTempBuf is non-null, we must copy the resulting string
  229. * so that it looks as if we did it in place:
  230. */
  231. if( pTempBuf && ( rc > 0 ) )
  232. {
  233. pAnsi = (LPSTR)pUnicode;
  234. strcpy( pAnsi, pTempBuf );
  235. LocalFree( pTempBuf );
  236. }
  237. return rc;
  238. }
  239. LPWSTR
  240. AllocateUnicodeString(
  241. LPSTR pAnsiString
  242. )
  243. {
  244. LPWSTR pUnicodeString = NULL;
  245. if (!pAnsiString)
  246. return NULL;
  247. pUnicodeString = (LPWSTR)LocalAlloc(
  248. LPTR,
  249. strlen(pAnsiString)*sizeof(WCHAR) +sizeof(WCHAR)
  250. );
  251. if (pUnicodeString) {
  252. AnsiToUnicodeString(
  253. pAnsiString,
  254. pUnicodeString,
  255. NULL_TERMINATED
  256. );
  257. }
  258. return pUnicodeString;
  259. }
  260. void
  261. FreeUnicodeString(
  262. LPWSTR pUnicodeString
  263. )
  264. {
  265. if (!pUnicodeString)
  266. return;
  267. LocalFree(pUnicodeString);
  268. return;
  269. }