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.

206 lines
4.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. #include "main.hxx"
  13. static WCHAR *MapSyntaxToStr[] = {
  14. L"ADS_INVALID_TYPE ",
  15. L"ADS_DN_STRING ",
  16. L"ADS_CASE_EXACT_STRING ",
  17. L"ADS_CASE_IGNORE_STRING",
  18. L"ADS_PRINTABLE_STRING ",
  19. L"ADS_NUMERIC_STRING ",
  20. L"ADS_BOOLEAN ",
  21. L"ADS_INTEGER ",
  22. L"ADS_OCTET_STRING ",
  23. L"ADS_UTC_TIME ",
  24. L"ADS_LARGE_INTEGER ",
  25. L"ADS_PROV_SPECIFIC ",
  26. L"ADS_OBJECT_CLASS "
  27. };
  28. void
  29. PrintUsage(
  30. void
  31. )
  32. {
  33. printf("\nUsage: dsschema /b <TreeName> /a <attrlist> /u <UserName> <Password>");
  34. printf(" attrlist = list of the attributes to get the info for \n" );
  35. printf("\nFor Example: dsschema /b NDS://ntmarst /a \"ADsPath, cn, description\" ");
  36. printf(" /u admin.ms ntmarst\n");
  37. }
  38. //
  39. // Print the data depending on its type.
  40. //
  41. void
  42. PrintAttrDefinition(
  43. PADS_ATTR_DEF pAttrDefiniton,
  44. DWORD dwNumAttributes
  45. )
  46. {
  47. ULONG i, j, k;
  48. for (k=0; k < dwNumAttributes; k++) {
  49. wprintf( L"Attribute %s\n", (DWORD) pAttrDefiniton[k].pszAttrName );
  50. wprintf (L"Syntax = %s\n", MapSyntaxToStr[pAttrDefiniton[k].dwADsType]);
  51. wprintf (L"Min Range = %d\n", pAttrDefiniton[k].dwMinRange);
  52. wprintf (L"Max Range = %d\n", pAttrDefiniton[k].dwMaxRange);
  53. wprintf (L"MultiValued %s\n", pAttrDefiniton[k].fMultiValued ? L"Yes" : L"No");
  54. wprintf (L"\n");
  55. }
  56. printf("\n");
  57. }
  58. int
  59. AnsiToUnicodeString(
  60. LPSTR pAnsi,
  61. LPWSTR pUnicode,
  62. DWORD StringLength
  63. )
  64. {
  65. int iReturn;
  66. if( StringLength == NULL_TERMINATED )
  67. StringLength = strlen( pAnsi );
  68. iReturn = MultiByteToWideChar(CP_ACP,
  69. MB_PRECOMPOSED,
  70. pAnsi,
  71. StringLength + 1,
  72. pUnicode,
  73. StringLength + 1 );
  74. //
  75. // Ensure NULL termination.
  76. //
  77. pUnicode[StringLength] = 0;
  78. return iReturn;
  79. }
  80. int
  81. UnicodeToAnsiString(
  82. LPWSTR pUnicode,
  83. LPSTR pAnsi,
  84. DWORD StringLength
  85. )
  86. {
  87. LPSTR pTempBuf = NULL;
  88. INT rc = 0;
  89. if( StringLength == NULL_TERMINATED ) {
  90. //
  91. // StringLength is just the
  92. // number of characters in the string
  93. //
  94. StringLength = wcslen( pUnicode );
  95. }
  96. //
  97. // WideCharToMultiByte doesn't NULL terminate if we're copying
  98. // just part of the string, so terminate here.
  99. //
  100. pUnicode[StringLength] = 0;
  101. //
  102. // Include one for the NULL
  103. //
  104. StringLength++;
  105. //
  106. // Unfortunately, WideCharToMultiByte doesn't do conversion in place,
  107. // so allocate a temporary buffer, which we can then copy:
  108. //
  109. if( pAnsi == (LPSTR)pUnicode )
  110. {
  111. pTempBuf = (LPSTR)LocalAlloc( LPTR, StringLength );
  112. pAnsi = pTempBuf;
  113. }
  114. if( pAnsi )
  115. {
  116. rc = WideCharToMultiByte( CP_ACP,
  117. 0,
  118. pUnicode,
  119. StringLength,
  120. pAnsi,
  121. StringLength,
  122. NULL,
  123. NULL );
  124. }
  125. /* If pTempBuf is non-null, we must copy the resulting string
  126. * so that it looks as if we did it in place:
  127. */
  128. if( pTempBuf && ( rc > 0 ) )
  129. {
  130. pAnsi = (LPSTR)pUnicode;
  131. strcpy( pAnsi, pTempBuf );
  132. LocalFree( pTempBuf );
  133. }
  134. return rc;
  135. }
  136. LPWSTR
  137. AllocateUnicodeString(
  138. LPSTR pAnsiString
  139. )
  140. {
  141. LPWSTR pUnicodeString = NULL;
  142. if (!pAnsiString)
  143. return NULL;
  144. pUnicodeString = (LPWSTR)LocalAlloc(
  145. LPTR,
  146. strlen(pAnsiString)*sizeof(WCHAR) +sizeof(WCHAR)
  147. );
  148. if (pUnicodeString) {
  149. AnsiToUnicodeString(
  150. pAnsiString,
  151. pUnicodeString,
  152. NULL_TERMINATED
  153. );
  154. }
  155. return pUnicodeString;
  156. }
  157. void
  158. FreeUnicodeString(
  159. LPWSTR pUnicodeString
  160. )
  161. {
  162. if (!pUnicodeString)
  163. return;
  164. LocalFree(pUnicodeString);
  165. return;
  166. }