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.

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