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.

309 lines
7.7 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. #include "main.hxx"
  13. void
  14. PrintUsage(
  15. void
  16. )
  17. {
  18. printf("\nUsage: dssrch /b <baseObject> /f <search_filter> [/f <attrlist>] [/p <preference>=value>] ");
  19. printf(" [/u <UserName> <Password>] [/t <flagName>=<value> \n");
  20. printf("\n where:\n" );
  21. printf(" baseObject = ADsPath of the base of the search\n");
  22. printf(" search_filter = search filter string in LDAP format\n" );
  23. printf(" attrlist = list of the attributes to display\n" );
  24. printf(" preference could be one of:\n");
  25. printf(" Asynchronous, AttrTypesOnly, DerefAliases, SizeLimit, TimeLimit,\n");
  26. printf(" TimeOut, PageSize, SearchScope, SortOn, CacheResults\n");
  27. printf(" flagName could be one of:\n");
  28. printf(" SecureAuth or UseEncrypt\n");
  29. printf(" value is yes/no for a Boolean and the respective integer for integers\n");
  30. printf(" scope is one of \"Base\", \"OneLevel\", or \"Subtree\"\n");
  31. printf("\nFor Example: dssrch /b NDS://ntmarst/ms /f \"(object Class=*)\" ");
  32. printf(" /a \"ADsPath, name, description\" /p searchScope=onelevel /p searchscope=onelevel\n\n OR \n");
  33. printf("\n dssrch /b \"LDAP://O=Internet/DC=COM/");
  34. printf("DC=MICROSOFT/DC=NTDEV\" /f \"(objectClass=*)\" /a \"ADsPath, name, usnchanged\" ");
  35. printf(" /u \"CN=NTDS,CN=Users,DC=NTDEV,DC=MICROSOFT,DC=COM,O=INTERNET\" \"\" ");
  36. printf("/p searchScope=onelevel /t secureauth=yes /p SortOn=name /p CacheResults=no\n");
  37. }
  38. //
  39. // Print the data depending on its type.
  40. //
  41. void
  42. PrintColumn(
  43. PADS_SEARCH_COLUMN pColumn,
  44. LPWSTR pszColumnName
  45. )
  46. {
  47. ULONG i, j, k;
  48. if (!pColumn) {
  49. return;
  50. }
  51. wprintf(
  52. L"%s = ",
  53. pszColumnName
  54. );
  55. for (k=0; k < pColumn->dwNumValues; k++) {
  56. if (k > 0)
  57. wprintf(L"# ");
  58. switch(pColumn->dwADsType) {
  59. case ADSTYPE_DN_STRING :
  60. wprintf(
  61. L"%s ",
  62. (LPWSTR) pColumn->pADsValues[k].DNString
  63. );
  64. break;
  65. case ADSTYPE_CASE_EXACT_STRING :
  66. wprintf(
  67. L"%s ",
  68. (LPWSTR) pColumn->pADsValues[k].CaseExactString
  69. );
  70. break;
  71. case ADSTYPE_CASE_IGNORE_STRING:
  72. wprintf(
  73. L"%s ",
  74. (LPWSTR) pColumn->pADsValues[k].CaseIgnoreString
  75. );
  76. break;
  77. case ADSTYPE_PRINTABLE_STRING :
  78. wprintf(
  79. L"%s ",
  80. (LPWSTR) pColumn->pADsValues[k].PrintableString
  81. );
  82. break;
  83. case ADSTYPE_NUMERIC_STRING :
  84. wprintf(
  85. L"%s ",
  86. (LPWSTR) pColumn->pADsValues[k].NumericString
  87. );
  88. break;
  89. case ADSTYPE_OBJECT_CLASS :
  90. wprintf(
  91. L"%s ",
  92. (LPWSTR) pColumn->pADsValues[k].ClassName
  93. );
  94. break;
  95. case ADSTYPE_BOOLEAN :
  96. wprintf(
  97. L"%s ",
  98. (DWORD) pColumn->pADsValues[k].Boolean ?
  99. L"TRUE" : L"FALSE"
  100. );
  101. break;
  102. case ADSTYPE_INTEGER :
  103. wprintf(
  104. L"%d ",
  105. (DWORD) pColumn->pADsValues[k].Integer
  106. );
  107. break;
  108. case ADSTYPE_OCTET_STRING :
  109. for (j=0; j<pColumn->pADsValues[k].OctetString.dwLength; j++) {
  110. printf(
  111. "%02x",
  112. ((BYTE *)pColumn->pADsValues[k].OctetString.lpValue)[j]
  113. );
  114. }
  115. break;
  116. case ADSTYPE_LARGE_INTEGER :
  117. wprintf(
  118. L"%I64d ",
  119. pColumn->pADsValues[k].LargeInteger
  120. );
  121. break;
  122. case ADSTYPE_UTC_TIME :
  123. // convert system time to printable time
  124. {
  125. SYSTEMTIME *pst = &(pColumn->pADsValues[k].UTCTime);
  126. // use am/pm semantics, match output from US version of dir /4
  127. WORD wHour = (pst->wHour > (WORD)12) ?
  128. (pst->wHour - (WORD)12) :
  129. pst->wHour;
  130. wprintf(
  131. L"%02d/%02d/%04d %02d:%02d%c ",
  132. pst->wMonth, pst->wDay, pst->wYear,
  133. wHour, pst->wMinute, (wHour == pst->wHour) ? L'a' : L'p'
  134. );
  135. }
  136. break;
  137. case ADSTYPE_PROV_SPECIFIC :
  138. wprintf(
  139. L"(provider specific value) "
  140. );
  141. break;
  142. }
  143. }
  144. printf("\n");
  145. }
  146. int
  147. AnsiToUnicodeString(
  148. LPSTR pAnsi,
  149. LPWSTR pUnicode,
  150. DWORD StringLength
  151. )
  152. {
  153. int iReturn;
  154. if( StringLength == NULL_TERMINATED )
  155. StringLength = strlen( pAnsi );
  156. iReturn = MultiByteToWideChar(CP_ACP,
  157. MB_PRECOMPOSED,
  158. pAnsi,
  159. StringLength + 1,
  160. pUnicode,
  161. StringLength + 1 );
  162. //
  163. // Ensure NULL termination.
  164. //
  165. pUnicode[StringLength] = 0;
  166. return iReturn;
  167. }
  168. int
  169. UnicodeToAnsiString(
  170. LPWSTR pUnicode,
  171. LPSTR pAnsi,
  172. DWORD StringLength
  173. )
  174. {
  175. LPSTR pTempBuf = NULL;
  176. INT rc = 0;
  177. if( StringLength == NULL_TERMINATED ) {
  178. //
  179. // StringLength is just the
  180. // number of characters in the string
  181. //
  182. StringLength = wcslen( pUnicode );
  183. }
  184. //
  185. // WideCharToMultiByte doesn't NULL terminate if we're copying
  186. // just part of the string, so terminate here.
  187. //
  188. pUnicode[StringLength] = 0;
  189. //
  190. // Include one for the NULL
  191. //
  192. StringLength++;
  193. //
  194. // Unfortunately, WideCharToMultiByte doesn't do conversion in place,
  195. // so allocate a temporary buffer, which we can then copy:
  196. //
  197. if( pAnsi == (LPSTR)pUnicode )
  198. {
  199. pTempBuf = (LPSTR)LocalAlloc( LPTR, StringLength );
  200. pAnsi = pTempBuf;
  201. }
  202. if( pAnsi )
  203. {
  204. rc = WideCharToMultiByte( CP_ACP,
  205. 0,
  206. pUnicode,
  207. StringLength,
  208. pAnsi,
  209. StringLength,
  210. NULL,
  211. NULL );
  212. }
  213. /* If pTempBuf is non-null, we must copy the resulting string
  214. * so that it looks as if we did it in place:
  215. */
  216. if( pTempBuf && ( rc > 0 ) )
  217. {
  218. pAnsi = (LPSTR)pUnicode;
  219. strcpy( pAnsi, pTempBuf );
  220. LocalFree( pTempBuf );
  221. }
  222. return rc;
  223. }
  224. LPWSTR
  225. AllocateUnicodeString(
  226. LPSTR pAnsiString
  227. )
  228. {
  229. LPWSTR pUnicodeString = NULL;
  230. if (!pAnsiString)
  231. return NULL;
  232. pUnicodeString = (LPWSTR)LocalAlloc(
  233. LPTR,
  234. strlen(pAnsiString)*sizeof(WCHAR) +sizeof(WCHAR)
  235. );
  236. if (pUnicodeString) {
  237. AnsiToUnicodeString(
  238. pAnsiString,
  239. pUnicodeString,
  240. NULL_TERMINATED
  241. );
  242. }
  243. return pUnicodeString;
  244. }
  245. void
  246. FreeUnicodeString(
  247. LPWSTR pUnicodeString
  248. )
  249. {
  250. if (!pUnicodeString)
  251. return;
  252. LocalFree(pUnicodeString);
  253. return;
  254. }