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.

724 lines
14 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. convert.c
  5. Abstract:
  6. This module contains miscellaneous utility routines used by the
  7. DHCP server service.
  8. Author:
  9. Manny Weiser (mannyw) 12-Aug-1992
  10. Revision History:
  11. Madan Appiah (madana) 21-Oct-1992
  12. --*/
  13. #include <precomp.h>
  14. #include <winnls.h>
  15. #undef DhcpFreeMemory
  16. #undef DhcpAllocateMemory
  17. VOID
  18. DhcpFreeMemory(
  19. PVOID Mem
  20. )
  21. {
  22. LocalFree( Mem );
  23. }
  24. PVOID
  25. DhcpAllocateMemory(
  26. IN ULONG Size
  27. )
  28. {
  29. return LocalAlloc( LPTR, Size );
  30. }
  31. LPWSTR
  32. DhcpOemToUnicodeN(
  33. IN LPSTR Ansi,
  34. IN OUT LPWSTR Unicode,
  35. IN USHORT cChars
  36. )
  37. {
  38. OEM_STRING AnsiString;
  39. UNICODE_STRING UnicodeString;
  40. RtlInitString( &AnsiString, Ansi );
  41. UnicodeString.MaximumLength =
  42. cChars * sizeof( WCHAR );
  43. if( Unicode == NULL ) {
  44. UnicodeString.Buffer =
  45. DhcpAllocateMemory( UnicodeString.MaximumLength );
  46. }
  47. else {
  48. UnicodeString.Buffer = Unicode;
  49. }
  50. if ( UnicodeString.Buffer == NULL ) {
  51. return NULL;
  52. }
  53. if(!NT_SUCCESS( RtlOemStringToUnicodeString( &UnicodeString,
  54. &AnsiString,
  55. FALSE))){
  56. if( Unicode == NULL ) {
  57. DhcpFreeMemory( UnicodeString.Buffer );
  58. }
  59. return NULL;
  60. }
  61. return UnicodeString.Buffer;
  62. }
  63. LPWSTR
  64. DhcpOemToUnicode(
  65. IN LPSTR Ansi,
  66. IN OUT LPWSTR Unicode
  67. )
  68. /*++
  69. Routine Description:
  70. Convert an OEM (zero terminated) string to the corresponding UNICODE
  71. string.
  72. Arguments:
  73. Ansi - Specifies the ASCII zero terminated string to convert.
  74. Unicode - Specifies the pointer to the unicode buffer. If this
  75. pointer is NULL then this routine allocates buffer using
  76. DhcpAllocateMemory and returns. The caller should freeup this
  77. memory after use by calling DhcpFreeMemory.
  78. Return Value:
  79. NULL - There was some error in the conversion.
  80. Otherwise, it returns a pointer to the zero terminated UNICODE string in
  81. an allocated buffer. The buffer can be freed using DhcpFreeMemory.
  82. --*/
  83. {
  84. OEM_STRING AnsiString;
  85. RtlInitString( &AnsiString, Ansi );
  86. return DhcpOemToUnicodeN(
  87. Ansi,
  88. Unicode,
  89. (USHORT) RtlOemStringToUnicodeSize( &AnsiString )
  90. );
  91. }
  92. ULONG
  93. DhcpUnicodeToOemSize(
  94. IN LPWSTR Unicode
  95. )
  96. /*++
  97. Routine Description:
  98. This routine returns the number of bytes requried
  99. to store the OEM string equivalent of the provided
  100. UNICODE string.
  101. Arguments:
  102. Unicode -- the input unicode string.
  103. Return Values:
  104. 0 -- if the string cannot be converted or is NULL
  105. number of bytes of storage required
  106. --*/
  107. {
  108. UNICODE_STRING UnicodeString;
  109. if( NULL == Unicode ) return 0;
  110. RtlInitUnicodeString( &UnicodeString, Unicode );
  111. return (USHORT) RtlUnicodeStringToOemSize( &UnicodeString );
  112. }
  113. LPSTR
  114. DhcpUnicodeToOem(
  115. IN LPWSTR Unicode,
  116. IN LPSTR Ansi
  117. )
  118. /*++
  119. Routine Description:
  120. Convert an UNICODE (zero terminated) string to the corresponding OEM
  121. string.
  122. Arguments:
  123. Ansi - Specifies the UNICODE zero terminated string to convert.
  124. Ansi - Specifies the pointer to the oem buffer. If this
  125. pointer is NULL then this routine allocates buffer using
  126. DhcpAllocateMemory and returns. The caller should freeup this
  127. memory after use by calling DhcpFreeMemory.
  128. Return Value:
  129. NULL - There was some error in the conversion.
  130. Otherwise, it returns a pointer to the zero terminated OEM string in
  131. an allocated buffer. The buffer can be freed using DhcpFreeMemory.
  132. --*/
  133. {
  134. OEM_STRING AnsiString;
  135. UNICODE_STRING UnicodeString;
  136. RtlInitUnicodeString( &UnicodeString, Unicode );
  137. AnsiString.MaximumLength =
  138. (USHORT) RtlUnicodeStringToOemSize( &UnicodeString );
  139. if( Ansi == NULL ) {
  140. AnsiString.Buffer = DhcpAllocateMemory( AnsiString.MaximumLength
  141. ); }
  142. else {
  143. AnsiString.Buffer = Ansi;
  144. }
  145. if ( AnsiString.Buffer == NULL ) {
  146. return NULL;
  147. }
  148. if(!NT_SUCCESS( RtlUnicodeStringToOemString( &AnsiString,
  149. &UnicodeString,
  150. FALSE))){
  151. if( Ansi == NULL ) {
  152. DhcpFreeMemory( AnsiString.Buffer );
  153. }
  154. return NULL;
  155. }
  156. return AnsiString.Buffer;
  157. }
  158. DWORD
  159. ConvertUnicodeToUTF8(
  160. LPWSTR UnicodeString,
  161. DWORD UnicodeLength,
  162. LPBYTE UTF8String,
  163. DWORD UTF8Length
  164. )
  165. /*++
  166. Routine Description:
  167. This functions converts Unicodestring into Utf8 format.
  168. The unicodestring must be NULL terminated.
  169. Arguments:
  170. Unicodestring - Pointer to the unicodestring
  171. UnicodeLength - Length of above string, or pass -1 if the above string is
  172. NULL terminated.
  173. UTF8String - Buffer to receive UTF8String. If this is null then the function
  174. returns the # of bytes needed for this buffer for conversion.
  175. UTF8Length - Length of the above buffer. if this is 0, the function
  176. will return the required length.
  177. Return Value:
  178. No. of bytes converted.
  179. --*/
  180. {
  181. DWORD Result;
  182. Result = WideCharToMultiByte(
  183. CP_UTF8,
  184. 0,
  185. UnicodeString,
  186. UnicodeLength,
  187. UTF8String,
  188. UTF8Length,
  189. NULL,
  190. NULL
  191. );
  192. if (Result == 0 ) {
  193. ASSERT(FALSE);
  194. }
  195. return Result;
  196. }
  197. DWORD
  198. ConvertUTF8ToUnicode(
  199. LPBYTE UTF8String,
  200. DWORD UTF8Length,
  201. LPWSTR UnicodeString,
  202. DWORD UnicodeLength
  203. )
  204. /*++
  205. Routine Description:
  206. This functions converts Unicodestring into Utf8 format.
  207. The unicodestring must be NULL terminated.
  208. Arguments:
  209. UTF8String - Buffer to UTFString.
  210. UTF8Length - Length of above buffer ; or pass -1 if the above string is NULL terminated.
  211. Unicodestring - Pointer to the buffer receiving unicodestring.
  212. UnicodeLength - Length of the above buffer. if this is 0, the function
  213. will return the required length.
  214. Return Value:
  215. No. of bytes converted.
  216. --*/
  217. {
  218. DWORD Result;
  219. Result = MultiByteToWideChar(
  220. CP_UTF8,
  221. 0,
  222. UTF8String,
  223. UTF8Length,
  224. UnicodeString,
  225. UnicodeLength
  226. );
  227. if (Result == 0 ) {
  228. ASSERT(FALSE);
  229. }
  230. return Result;
  231. }
  232. VOID
  233. DhcpHexToString(
  234. LPWSTR Buffer,
  235. LPBYTE HexNumber,
  236. DWORD Length
  237. )
  238. /*++
  239. Routine Description:
  240. This functions converts are arbitrary length hex number to a Unicode
  241. string. The string is not NULL terminated.
  242. Arguments:
  243. Buffer - A pointer to a buffer for the resultant Unicode string.
  244. The buffer must be at least Length * 2 characters in size.
  245. HexNumber - The hex number to convert.
  246. Length - The length of HexNumber, in bytes.
  247. Return Value:
  248. None.
  249. --*/
  250. {
  251. DWORD i;
  252. int j;
  253. for (i = 0; i < Length * 2; i+=2 ) {
  254. j = *HexNumber & 0xF;
  255. if ( j <= 9 ) {
  256. Buffer[i+1] = j + L'0';
  257. } else {
  258. Buffer[i+1] = j + L'A' - 10;
  259. }
  260. j = *HexNumber >> 4;
  261. if ( j <= 9 ) {
  262. Buffer[i] = j + L'0';
  263. } else {
  264. Buffer[i] = j + L'A' - 10;
  265. }
  266. HexNumber++;
  267. }
  268. return;
  269. }
  270. VOID
  271. DhcpHexToAscii(
  272. LPSTR Buffer,
  273. LPBYTE HexNumber,
  274. DWORD Length
  275. )
  276. /*++
  277. Routine Description:
  278. This functions converts are arbitrary length hex number to an ASCII
  279. string. The string is not NUL terminated.
  280. Arguments:
  281. Buffer - A pointer to a buffer for the resultant Unicode string.
  282. The buffer must be at least Length * 2 characters in size.
  283. HexNumber - The hex number to convert.
  284. Length - The length of HexNumber, in bytes.
  285. Return Value:
  286. None.
  287. --*/
  288. {
  289. DWORD i;
  290. int j;
  291. for (i = 0; i < Length; i+=1 ) {
  292. j = *HexNumber & 0xF;
  293. if ( j <= 9 ) {
  294. Buffer[i+1] = j + '0';
  295. } else {
  296. Buffer[i+1] = j + 'A' - 10;
  297. }
  298. j = *HexNumber >> 4;
  299. if ( j <= 9 ) {
  300. Buffer[i] = j + '0';
  301. } else {
  302. Buffer[i] = j + 'A' - 10;
  303. }
  304. HexNumber++;
  305. }
  306. return;
  307. }
  308. VOID
  309. DhcpDecimalToString(
  310. LPWSTR Buffer,
  311. BYTE Number
  312. )
  313. /*++
  314. Routine Description:
  315. This functions converts a single byte decimal digit to a 3 character
  316. Unicode string. The string not NUL terminated.
  317. Arguments:
  318. Buffer - A pointer to a buffer for the resultant Unicode string.
  319. The buffer must be at least 3 characters in size.
  320. Number - The number to convert.
  321. Return Value:
  322. None.
  323. --*/
  324. {
  325. Buffer[2] = Number % 10 + L'0';
  326. Number /= 10;
  327. Buffer[1] = Number % 10 + L'0';
  328. Number /= 10;
  329. Buffer[0] = Number + L'0';
  330. return;
  331. }
  332. DWORD
  333. DhcpDottedStringToIpAddress(
  334. LPSTR String
  335. )
  336. /*++
  337. Routine Description:
  338. This functions converts a dotted decimal form ASCII string to a
  339. Host order IP address.
  340. Arguments:
  341. String - The address to convert.
  342. Return Value:
  343. The corresponding IP address.
  344. --*/
  345. {
  346. struct in_addr addr;
  347. addr.s_addr = inet_addr( String );
  348. return( ntohl(*(LPDWORD)&addr) );
  349. }
  350. LPSTR
  351. DhcpIpAddressToDottedString(
  352. DWORD IpAddress
  353. )
  354. /*++
  355. Routine Description:
  356. This functions converts a Host order IP address to a dotted decimal
  357. form ASCII string.
  358. Arguments:
  359. IpAddress - Host order IP Address.
  360. Return Value:
  361. String for IP Address.
  362. --*/
  363. {
  364. DWORD NetworkOrderIpAddress;
  365. NetworkOrderIpAddress = htonl(IpAddress);
  366. return(inet_ntoa( *(struct in_addr *)&NetworkOrderIpAddress));
  367. }
  368. DWORD
  369. DhcpStringToHwAddress(
  370. LPSTR AddressBuffer,
  371. LPSTR AddressString
  372. )
  373. /*++
  374. Routine Description:
  375. This functions converts an ASCII string to a hex number.
  376. Arguments:
  377. AddressBuffer - A pointer to a buffer which will contain the hex number.
  378. AddressString - The string to convert.
  379. Return Value:
  380. The number of bytes written to AddressBuffer.
  381. --*/
  382. {
  383. int i = 0;
  384. char c1, c2;
  385. int value1, value2;
  386. while ( *AddressString != 0) {
  387. c1 = (char)toupper(*AddressString);
  388. if ( isdigit(c1) ) {
  389. value1 = c1 - '0';
  390. } else if ( c1 >= 'A' && c1 <= 'F' ) {
  391. value1 = c1 - 'A' + 10;
  392. }
  393. else {
  394. break;
  395. }
  396. c2 = (char)toupper(*(AddressString+1));
  397. if ( isdigit(c2) ) {
  398. value2 = c2 - '0';
  399. } else if ( c2 >= 'A' && c2 <= 'F' ) {
  400. value2 = c2 - 'A' + 10;
  401. }
  402. else {
  403. break;
  404. }
  405. AddressBuffer [i] = value1 * 16 + value2;
  406. AddressString += 2;
  407. i++;
  408. }
  409. return( i );
  410. }
  411. #if 0
  412. DHCP_IP_ADDRESS
  413. DhcpHostOrder(
  414. DHCP_IP_ADDRESS NetworkOrderAddress
  415. )
  416. /*++
  417. Routine Description:
  418. This functions converts a network order IP address to host order.
  419. Arguments:
  420. NetworkOrderAddress - A network order IP address.
  421. Return Value:
  422. The host order IP address.
  423. --*/
  424. {
  425. return( ntohl( NetworkOrderAddress ) );
  426. }
  427. DHCP_IP_ADDRESS
  428. DhcpNetworkOrder(
  429. DHCP_IP_ADDRESS HostOrderAddress
  430. )
  431. /*++
  432. Routine Description:
  433. This functions converts a network order IP address to host order.
  434. Arguments:
  435. HostOrderAddress - A host order IP address.
  436. Return Value:
  437. The network order IP address.
  438. --*/
  439. {
  440. return( htonl( HostOrderAddress ) );
  441. }
  442. VOID
  443. DhcpIpAddressToString(
  444. LPWSTR Buffer,
  445. DWORD IpAddress
  446. )
  447. /*++
  448. Routine Description:
  449. This function converts an IP address from host order ASCII form.
  450. Arguments:
  451. Buffer - Points to a buffer to the receive the ASCII form. The
  452. buffer must be at least 8 WCHARs long.
  453. IpAddress - The IP address to convert.
  454. Return Value:
  455. Nothing.
  456. --*/
  457. {
  458. int i;
  459. int j;
  460. for (i = 7; i >= 0; i-- ) {
  461. j = IpAddress & 0xF;
  462. if ( j <= 9 ) {
  463. Buffer[i] = j + L'0';
  464. } else {
  465. Buffer[i] = j + L'A' - 10;
  466. }
  467. IpAddress >>=4;
  468. }
  469. return;
  470. }
  471. VOID
  472. DhcpStringToIpAddress(
  473. LPSTR Buffer,
  474. LPDHCP_IP_ADDRESS IpAddress,
  475. BOOL NetOrder
  476. )
  477. /*++
  478. Routine Description:
  479. This function converts an ASCII string IP Address to binary format.
  480. Arguments:
  481. Buffer - Pointer to buffer containing the IP address.
  482. IpAddress - Points to a buffer to contain the binary format IP address.
  483. NetOrder - If TRUE, the address is converted to a network order address.
  484. If FALSE, the address is converted to a host order address.
  485. Return Value:
  486. None.
  487. --*/
  488. {
  489. DWORD value;
  490. value = strtol( Buffer, NULL, 16 );
  491. if ( NetOrder ) {
  492. *IpAddress = htonl( value );
  493. } else {
  494. *IpAddress = value;
  495. }
  496. return;
  497. }
  498. #endif