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.

1828 lines
40 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name :
  4. tcputil.cxx
  5. Abstract:
  6. This module contains common utility routines for the TCP services
  7. Author:
  8. Johnl 09-Oct-1994 Created.
  9. --*/
  10. #include "tcpdllp.hxx"
  11. #include <isplat.h>
  12. #include <datetime.hxx>
  13. LPSTR
  14. ConvertUnicodeToAnsi(
  15. IN LPCWSTR lpszUnicode,
  16. IN LPSTR lpszAnsi,
  17. IN DWORD cbAnsi
  18. )
  19. /*++
  20. Description:
  21. Converts given null-terminated string into ANSI in the buffer supplied.
  22. Arguments:
  23. lpszUnicode null-terminated string in Unicode
  24. lpszAnsi buffer supplied to copy string after conversion.
  25. if ( lpszAnsi == NULL), then this module allocates space
  26. using TCP_ALLOC, which should be freed calling TCP_FREE
  27. by user.
  28. cbAnsi number of bytes in lpszAnsi if specified
  29. Returns:
  30. pointer to converted ANSI string. NULL on errors.
  31. History:
  32. MuraliK 12-01-1994 Created.
  33. --*/
  34. {
  35. DWORD cchLen;
  36. DWORD nBytes;
  37. LPSTR lpszAlloc = NULL;
  38. if ( lpszUnicode == NULL) {
  39. return (NULL);
  40. }
  41. if ( lpszAnsi == NULL) {
  42. //
  43. // multiply by 2 to accomodate DBCS
  44. //
  45. cchLen = wcslen( lpszUnicode);
  46. nBytes = (cchLen+1) * sizeof(CHAR) * 2;
  47. lpszAlloc = (LPSTR ) TCP_ALLOC( nBytes );
  48. } else {
  49. lpszAlloc = lpszAnsi;
  50. nBytes = cbAnsi;
  51. DBG_ASSERT(nBytes > 0);
  52. }
  53. if ( lpszAlloc != NULL) {
  54. cchLen = WideCharToMultiByte( CP_ACP,
  55. WC_COMPOSITECHECK,
  56. lpszUnicode,
  57. -1,
  58. lpszAlloc,
  59. nBytes,
  60. NULL, // lpszDefaultChar
  61. NULL // lpfDefaultUsed
  62. );
  63. DBG_ASSERT(cchLen == (strlen(lpszAlloc)+1) );
  64. if ( cchLen == 0 ) {
  65. //
  66. // There was a failure. Free up buffer if need be.
  67. //
  68. DBGPRINTF((DBG_CONTEXT,"WideCharToMultiByte failed with %d\n",
  69. GetLastError()));
  70. if ( lpszAnsi == NULL) {
  71. TCP_FREE( lpszAlloc);
  72. lpszAlloc = NULL;
  73. } else {
  74. lpszAlloc[cchLen] = '\0';
  75. }
  76. } else {
  77. DBG_ASSERT( cchLen <= nBytes );
  78. DBG_ASSERT(lpszAlloc[cchLen-1] == '\0');
  79. lpszAlloc[cchLen-1] = '\0';
  80. }
  81. }
  82. return ( lpszAlloc);
  83. } // ConvertUnicodeToAnsi
  84. /*******************************************************************
  85. NAME: ReadRegistryDword
  86. SYNOPSIS: Reads a DWORD value from the registry.
  87. ENTRY: hkey - Openned registry key to read
  88. pszValueName - The name of the value.
  89. dwDefaultValue - The default value to use if the
  90. value cannot be read.
  91. RETURNS DWORD - The value from the registry, or dwDefaultValue.
  92. ********************************************************************/
  93. DWORD ReadRegistryDwordA( HKEY hkey,
  94. LPCSTR pszValueName,
  95. DWORD dwDefaultValue )
  96. {
  97. DWORD err;
  98. DWORD dwBuffer;
  99. DWORD cbBuffer = sizeof(dwBuffer);
  100. DWORD dwType;
  101. if( hkey != NULL )
  102. {
  103. err = RegQueryValueExA( hkey,
  104. pszValueName,
  105. NULL,
  106. &dwType,
  107. (LPBYTE)&dwBuffer,
  108. &cbBuffer );
  109. if( ( err == NO_ERROR ) && ( dwType == REG_DWORD ) )
  110. {
  111. dwDefaultValue = dwBuffer;
  112. }
  113. }
  114. return dwDefaultValue;
  115. } // ReadRegistryDwordA()
  116. DWORD
  117. WriteRegistryDwordA(
  118. IN HKEY hkey,
  119. IN LPCSTR pszValueName,
  120. IN DWORD dwValue)
  121. /*++
  122. Description:
  123. Writes the given DWORD value into registry entry specified
  124. by hkey\pszValueName
  125. Arguments:
  126. hkey handle to registry key
  127. pszValueName name of the value
  128. dwValue new value for write
  129. Returns:
  130. Win32 error codes. NO_ERROR if successful.
  131. History:
  132. MuraliK 12-01-1994 Created.
  133. --*/
  134. {
  135. DWORD err;
  136. if ( (hkey == NULL) || (pszValueName == NULL) ) {
  137. err = ( ERROR_INVALID_PARAMETER);
  138. } else {
  139. err = RegSetValueExA( hkey,
  140. pszValueName,
  141. 0,
  142. REG_DWORD,
  143. (LPBYTE ) &dwValue,
  144. sizeof( dwValue));
  145. }
  146. return ( err);
  147. } // WriteRegistryDwordA()
  148. DWORD
  149. WriteRegistryStringA(
  150. IN HKEY hkey,
  151. IN LPCSTR pszValueName,
  152. IN LPCSTR pszValue,
  153. IN DWORD cbValue,
  154. IN DWORD dwType
  155. )
  156. /*++
  157. Description:
  158. Writes the given ANSI String into registry entry specified
  159. by hkey\pszValueName.
  160. Arguments:
  161. hkey handle to registry key
  162. pszValueName name of the value
  163. pszValue new value for write
  164. cbValue count of bytes of value written.
  165. Should include terminating null characters.
  166. dwType type of the value being written
  167. ( REG_SZ, REG_MULTI_SZ etc)
  168. Returns:
  169. Win32 error codes. NO_ERROR if successful.
  170. --*/
  171. {
  172. DWORD err;
  173. DBG_ASSERT(dwType != REG_MULTI_SZ);
  174. DBG_ASSERT( (dwType == REG_SZ) || (dwType == REG_EXPAND_SZ) );
  175. if ( (hkey == NULL) ||
  176. (pszValueName == NULL) ||
  177. (cbValue == 0) ) {
  178. err = ERROR_INVALID_PARAMETER;
  179. } else {
  180. err = RegSetValueExA(
  181. hkey,
  182. pszValueName,
  183. 0,
  184. dwType,
  185. (LPBYTE ) pszValue,
  186. cbValue); // + 1 for null character
  187. }
  188. return ( err);
  189. } // WriteRegistryStringA()
  190. DWORD
  191. WriteRegistryStringW(
  192. IN HKEY hkey,
  193. IN LPCWSTR pszValueName,
  194. IN LPCWSTR pszValue,
  195. IN DWORD cbValue,
  196. IN DWORD dwType)
  197. /*++
  198. Description:
  199. Writes the given ANSI String into registry entry specified
  200. by hkey\pszValueName.
  201. Arguments:
  202. hkey handle to registry key
  203. pszValueName name of the value
  204. pszValue new value for write
  205. cbValue count of bytes of value written.
  206. Should include terminating null characters.
  207. dwType type of the value being written
  208. ( REG_SZ, REG_MULTI_SZ etc)
  209. Returns:
  210. Win32 error codes. NO_ERROR if successful.
  211. --*/
  212. {
  213. DWORD err;
  214. LPSTR ansiValue = NULL;
  215. LPSTR ansiName = NULL;
  216. if ( (hkey == NULL) ||
  217. (pszValueName == NULL) ||
  218. (cbValue == 0) ) {
  219. err = ERROR_INVALID_PARAMETER;
  220. } else {
  221. //
  222. // Convert to ansi
  223. //
  224. ansiName = ConvertUnicodeToAnsi( pszValueName, NULL, 0 );
  225. ansiValue = ConvertUnicodeToAnsi( pszValue, NULL, 0 );
  226. if ( (ansiName != NULL) && (ansiValue != NULL) ) {
  227. err = WriteRegistryStringA(hkey,
  228. ansiName,
  229. ansiValue,
  230. strlen(ansiValue)+1,
  231. dwType
  232. );
  233. } else {
  234. err = ERROR_NOT_ENOUGH_MEMORY;
  235. }
  236. }
  237. if ( ansiName != NULL ) {
  238. TCP_FREE(ansiName);
  239. }
  240. if ( ansiValue != NULL ) {
  241. TCP_FREE(ansiValue);
  242. }
  243. return ( err);
  244. } // WriteRegistryStringW()
  245. /*******************************************************************
  246. NAME: ReadRegistryString
  247. SYNOPSIS: Allocates necessary buffer space for a registry
  248. string, then reads the string into the buffer.
  249. ENTRY: pszValueName - The name of the value.
  250. pszDefaultValue - The default value to use if the
  251. value cannot be read.
  252. fExpand - Expand environment strings if TRUE.
  253. RETURNS: TCHAR * - The string, NULL if error.
  254. NOTES: I always allocate one more character than actually
  255. necessary. This will ensure that any code expecting
  256. to read a REG_MULTI_SZ will not explode if the
  257. registry actually contains a REG_SZ.
  258. This function cannot be called until after
  259. InitializeGlobals().
  260. HISTORY:
  261. KeithMo 15-Mar-1993 Created.
  262. ********************************************************************/
  263. TCHAR * ReadRegistryString( HKEY hkey,
  264. LPCTSTR pszValueName,
  265. LPCTSTR pszDefaultValue,
  266. BOOL fExpand )
  267. {
  268. TCHAR * pszBuffer1;
  269. TCHAR * pszBuffer2;
  270. DWORD cbBuffer;
  271. DWORD dwType;
  272. DWORD err;
  273. //
  274. // Determine the buffer size.
  275. //
  276. pszBuffer1 = NULL;
  277. pszBuffer2 = NULL;
  278. cbBuffer = 0;
  279. if( hkey == NULL )
  280. {
  281. //
  282. // Pretend the key wasn't found.
  283. //
  284. err = ERROR_FILE_NOT_FOUND;
  285. }
  286. else
  287. {
  288. err = RegQueryValueEx( hkey,
  289. pszValueName,
  290. NULL,
  291. &dwType,
  292. NULL,
  293. &cbBuffer );
  294. if( ( err == NO_ERROR ) || ( err == ERROR_MORE_DATA ) )
  295. {
  296. if( ( dwType != REG_SZ ) &&
  297. ( dwType != REG_MULTI_SZ ) &&
  298. ( dwType != REG_EXPAND_SZ ) )
  299. {
  300. //
  301. // Type mismatch, registry data NOT a string.
  302. // Use default.
  303. //
  304. err = ERROR_FILE_NOT_FOUND;
  305. }
  306. else
  307. {
  308. //
  309. // Item found, allocate a buffer.
  310. //
  311. pszBuffer1 = (TCHAR *) TCP_ALLOC( cbBuffer+sizeof(TCHAR) );
  312. if( pszBuffer1 == NULL )
  313. {
  314. err = ERROR_NOT_ENOUGH_MEMORY;
  315. }
  316. else
  317. {
  318. //
  319. // Now read the value into the buffer.
  320. //
  321. err = RegQueryValueEx( hkey,
  322. pszValueName,
  323. NULL,
  324. NULL,
  325. (LPBYTE)pszBuffer1,
  326. &cbBuffer );
  327. }
  328. }
  329. }
  330. }
  331. if( err == ERROR_FILE_NOT_FOUND )
  332. {
  333. //
  334. // Item not found, use default value.
  335. //
  336. err = NO_ERROR;
  337. if( pszDefaultValue != NULL )
  338. {
  339. pszBuffer1 = (TCHAR *)TCP_ALLOC( (_tcslen(pszDefaultValue)+1) * sizeof(TCHAR) );
  340. if( pszBuffer1 == NULL )
  341. {
  342. err = ERROR_NOT_ENOUGH_MEMORY;
  343. }
  344. else
  345. {
  346. _tcscpy( pszBuffer1, pszDefaultValue );
  347. }
  348. }
  349. }
  350. if( err != NO_ERROR )
  351. {
  352. //
  353. // Tragic error reading registry, abort now.
  354. //
  355. goto ErrorCleanup;
  356. }
  357. //
  358. // pszBuffer1 holds the registry value. Now expand
  359. // the environment strings if necessary.
  360. //
  361. if( !fExpand || TsIsWindows95() )
  362. {
  363. return pszBuffer1;
  364. }
  365. //
  366. // Returns number of characters
  367. //
  368. cbBuffer = ExpandEnvironmentStrings( pszBuffer1,
  369. NULL,
  370. 0 );
  371. //
  372. // The ExpandEnvironmentStrings() API is kinda poor. In returning the
  373. // number of characters, we have no clue how large to make the buffer
  374. // in the case of DBCS characters. Lets assume that each character is
  375. // 2 bytes.
  376. //
  377. pszBuffer2 = (TCHAR *) TCP_ALLOC( (cbBuffer+1)*sizeof(WCHAR) );
  378. if( pszBuffer2 == NULL )
  379. {
  380. goto ErrorCleanup;
  381. }
  382. if( ExpandEnvironmentStrings( pszBuffer1,
  383. pszBuffer2,
  384. cbBuffer ) > cbBuffer )
  385. {
  386. goto ErrorCleanup;
  387. }
  388. //
  389. // pszBuffer2 now contains the registry value with
  390. // environment strings expanded.
  391. //
  392. TCP_FREE( pszBuffer1 );
  393. pszBuffer1 = NULL;
  394. return pszBuffer2;
  395. ErrorCleanup:
  396. //
  397. // Something tragic happend; free any allocated buffers
  398. // and return NULL to the caller, indicating failure.
  399. //
  400. if( pszBuffer1 != NULL )
  401. {
  402. TCP_FREE( pszBuffer1 );
  403. pszBuffer1 = NULL;
  404. }
  405. if( pszBuffer2 != NULL )
  406. {
  407. TCP_FREE( pszBuffer2 );
  408. pszBuffer2 = NULL;
  409. }
  410. return NULL;
  411. } // ReadRegistryString
  412. //
  413. // Chicago does not support the REG_MULTI_SZ registry value. As
  414. // a hack (er, workaround), we'll create *keys* in the registry
  415. // in place of REG_MULTI_SZ *values*. We'll then use the names
  416. // of any values under the key as the REG_MULTI_SZ entries. So,
  417. // instead of this:
  418. //
  419. // ..\Control\ServiceProvider
  420. // ProviderOrder = REG_MULTI_SZ "MSTCP"
  421. // "NWLINK"
  422. // "FOOBAR"
  423. //
  424. // We'll use this:
  425. //
  426. // ..\Control\Service\Provider\ProviderOrder
  427. // MSTCP = REG_SZ ""
  428. // NWLINK = REG_SZ ""
  429. // FOOBAR = REG_SZ ""
  430. //
  431. // This function takes an open registry key handle, enumerates
  432. // the names of values contained within the key, and constructs
  433. // a REG_MULTI_SZ string from the value names.
  434. //
  435. // Note that this function is not multithread safe; if another
  436. // thread (or process) creates or deletes values under the
  437. // specified key, the results are indeterminate.
  438. //
  439. // This function returns NULL on error. It returns non-NULL
  440. // on success, even if the resulting REG_MULTI_SZ is empty.
  441. //
  442. TCHAR *
  443. KludgeMultiSz(
  444. HKEY hkey,
  445. LPDWORD lpdwLength
  446. )
  447. {
  448. LONG err;
  449. DWORD iValue;
  450. DWORD cchTotal;
  451. DWORD cchValue;
  452. TCHAR szValue[MAX_PATH];
  453. LPTSTR lpMultiSz;
  454. LPTSTR lpTmp;
  455. LPTSTR lpEnd;
  456. //
  457. // Enumerate the values and total up the lengths.
  458. //
  459. iValue = 0;
  460. cchTotal = 0;
  461. for( ; ; )
  462. {
  463. cchValue = sizeof(szValue)/sizeof(TCHAR);
  464. err = RegEnumValue( hkey,
  465. iValue,
  466. szValue,
  467. &cchValue,
  468. NULL,
  469. NULL,
  470. NULL,
  471. NULL );
  472. if( err != NO_ERROR )
  473. {
  474. break;
  475. }
  476. //
  477. // Add the length of the value's name, plus one
  478. // for the terminator.
  479. //
  480. cchTotal += _tcslen( szValue ) + 1;
  481. //
  482. // Advance to next value.
  483. //
  484. iValue++;
  485. }
  486. //
  487. // Add one for the final terminating NULL.
  488. //
  489. cchTotal++;
  490. *lpdwLength = cchTotal;
  491. //
  492. // Allocate the MULTI_SZ buffer.
  493. //
  494. lpMultiSz = (TCHAR *) TCP_ALLOC( cchTotal * sizeof(TCHAR) );
  495. if( lpMultiSz == NULL )
  496. {
  497. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  498. return NULL;
  499. }
  500. memset( lpMultiSz, 0, cchTotal * sizeof(TCHAR) );
  501. //
  502. // Enumerate the values and append to the buffer.
  503. //
  504. iValue = 0;
  505. lpTmp = lpMultiSz;
  506. lpEnd = lpMultiSz + cchTotal;
  507. for( ; ; )
  508. {
  509. cchValue = sizeof(szValue)/sizeof(TCHAR);
  510. err = RegEnumValue( hkey,
  511. iValue,
  512. szValue,
  513. &cchValue,
  514. NULL,
  515. NULL,
  516. NULL,
  517. NULL );
  518. if( err != NO_ERROR )
  519. {
  520. break;
  521. }
  522. //
  523. // Compute the length of the value name (including
  524. // the terminating NULL).
  525. //
  526. cchValue = _tcslen( szValue ) + 1;
  527. //
  528. // Determine if there is room in the array, taking into
  529. // account the second NULL that terminates the string list.
  530. //
  531. if( ( lpTmp + cchValue + 1 ) > lpEnd )
  532. {
  533. break;
  534. }
  535. //
  536. // Append the value name.
  537. //
  538. _tcscpy( lpTmp, szValue );
  539. lpTmp += cchValue;
  540. //
  541. // Advance to next value.
  542. //
  543. iValue++;
  544. }
  545. //
  546. // Success!
  547. //
  548. return (LPTSTR)lpMultiSz;
  549. } // KludgeMultiSz
  550. BOOL
  551. ReadRegistryStr(
  552. IN HKEY hkeyReg,
  553. OUT STR & str,
  554. IN LPCTSTR lpszValueName,
  555. IN LPCTSTR lpszDefaultValue,
  556. IN BOOL fExpand )
  557. /*++
  558. Reads the registry string into the string buffer supplied.
  559. If there is no value in the registry the default value is set to
  560. be the value of the string.
  561. If an environment expansion is requested, it is also performed.
  562. Arguments:
  563. hkeyReg handle for registry entry
  564. str string to contain the result of read operation
  565. lpszValueName
  566. pointer to string containing the key name whose
  567. value needs to be fetched.
  568. lpszDefaultValue
  569. pointer to string containing a value which is used if no
  570. value exists in the registry.
  571. fExpand boolean flag indicating if an expansion is desired.
  572. Returns:
  573. FALSE if there is any error.
  574. TRUE when the string is successfully set.
  575. --*/
  576. {
  577. BOOL fReturn = FALSE;
  578. LPTSTR pszValueAlloc;
  579. pszValueAlloc = ReadRegistryString( hkeyReg, lpszValueName,
  580. lpszDefaultValue, fExpand);
  581. if ( pszValueAlloc != NULL) {
  582. fReturn = str.Copy( pszValueAlloc);
  583. TCP_FREE( pszValueAlloc);
  584. } else {
  585. DBG_ASSERT( fReturn == FALSE);
  586. }
  587. if ( !fReturn) {
  588. IF_DEBUG( ERROR) {
  589. DWORD err = GetLastError();
  590. DBGPRINTF(( DBG_CONTEXT,
  591. " Error %u in ReadRegistryString( %08x, %s).\n",
  592. err, hkeyReg, lpszValueName));
  593. SetLastError(err);
  594. }
  595. }
  596. return ( fReturn);
  597. } // ReadRegistryStr
  598. /*******************************************************************
  599. NAME: FlipSlashes
  600. SYNOPSIS: Flips the Unix-ish forward slashes ('/') into Dos-ish
  601. back slashes ('\').
  602. ENTRY: pszPath - The path to munge.
  603. RETURNS: TCHAR * - pszPath.
  604. HISTORY:
  605. KeithMo 04-Jun-1993 Created.
  606. ********************************************************************/
  607. TCHAR * FlipSlashes( TCHAR * pszPath )
  608. {
  609. TCHAR ch;
  610. TCHAR * pszScan = pszPath;
  611. while( ( ch = *pszScan ) != TEXT('\0') )
  612. {
  613. if( ch == TEXT('/') )
  614. {
  615. *pszScan = TEXT('\\');
  616. }
  617. pszScan++;
  618. }
  619. return pszPath;
  620. } // FlipSlashes
  621. /*++
  622. Copyright (c) 1991 Microsoft Corporation
  623. Module Name:
  624. i_ntoa.c
  625. Abstract:
  626. This module implements a routine to convert a numerical IP address
  627. into a dotted-decimal character string Internet address.
  628. Author:
  629. Mike Massa (mikemas) Sept 20, 1991
  630. Revision History:
  631. Who When What
  632. -------- -------- ----------------------------------------------
  633. mikemas 9-20-91 created
  634. davidtr 9-19-95 completely rewritten for performance
  635. muralik 3-Oct-1995 massaged it for Internet services
  636. Notes:
  637. Exports:
  638. InetNtoa()
  639. --*/
  640. #define UC(b) (((int)b)&0xff)
  641. //
  642. // This preinitialized array defines the strings to be used for
  643. // inet_ntoa. The index of each row corresponds to the value for a byte
  644. // in an IP address. The first three bytes of each row are the
  645. // char/string value for the byte, and the fourth byte in each row is
  646. // the length of the string required for the byte. This approach
  647. // allows a fast implementation with no jumps.
  648. //
  649. static BYTE NToACharStrings[][4] = {
  650. '0', 'x', 'x', 1,
  651. '1', 'x', 'x', 1,
  652. '2', 'x', 'x', 1,
  653. '3', 'x', 'x', 1,
  654. '4', 'x', 'x', 1,
  655. '5', 'x', 'x', 1,
  656. '6', 'x', 'x', 1,
  657. '7', 'x', 'x', 1,
  658. '8', 'x', 'x', 1,
  659. '9', 'x', 'x', 1,
  660. '1', '0', 'x', 2,
  661. '1', '1', 'x', 2,
  662. '1', '2', 'x', 2,
  663. '1', '3', 'x', 2,
  664. '1', '4', 'x', 2,
  665. '1', '5', 'x', 2,
  666. '1', '6', 'x', 2,
  667. '1', '7', 'x', 2,
  668. '1', '8', 'x', 2,
  669. '1', '9', 'x', 2,
  670. '2', '0', 'x', 2,
  671. '2', '1', 'x', 2,
  672. '2', '2', 'x', 2,
  673. '2', '3', 'x', 2,
  674. '2', '4', 'x', 2,
  675. '2', '5', 'x', 2,
  676. '2', '6', 'x', 2,
  677. '2', '7', 'x', 2,
  678. '2', '8', 'x', 2,
  679. '2', '9', 'x', 2,
  680. '3', '0', 'x', 2,
  681. '3', '1', 'x', 2,
  682. '3', '2', 'x', 2,
  683. '3', '3', 'x', 2,
  684. '3', '4', 'x', 2,
  685. '3', '5', 'x', 2,
  686. '3', '6', 'x', 2,
  687. '3', '7', 'x', 2,
  688. '3', '8', 'x', 2,
  689. '3', '9', 'x', 2,
  690. '4', '0', 'x', 2,
  691. '4', '1', 'x', 2,
  692. '4', '2', 'x', 2,
  693. '4', '3', 'x', 2,
  694. '4', '4', 'x', 2,
  695. '4', '5', 'x', 2,
  696. '4', '6', 'x', 2,
  697. '4', '7', 'x', 2,
  698. '4', '8', 'x', 2,
  699. '4', '9', 'x', 2,
  700. '5', '0', 'x', 2,
  701. '5', '1', 'x', 2,
  702. '5', '2', 'x', 2,
  703. '5', '3', 'x', 2,
  704. '5', '4', 'x', 2,
  705. '5', '5', 'x', 2,
  706. '5', '6', 'x', 2,
  707. '5', '7', 'x', 2,
  708. '5', '8', 'x', 2,
  709. '5', '9', 'x', 2,
  710. '6', '0', 'x', 2,
  711. '6', '1', 'x', 2,
  712. '6', '2', 'x', 2,
  713. '6', '3', 'x', 2,
  714. '6', '4', 'x', 2,
  715. '6', '5', 'x', 2,
  716. '6', '6', 'x', 2,
  717. '6', '7', 'x', 2,
  718. '6', '8', 'x', 2,
  719. '6', '9', 'x', 2,
  720. '7', '0', 'x', 2,
  721. '7', '1', 'x', 2,
  722. '7', '2', 'x', 2,
  723. '7', '3', 'x', 2,
  724. '7', '4', 'x', 2,
  725. '7', '5', 'x', 2,
  726. '7', '6', 'x', 2,
  727. '7', '7', 'x', 2,
  728. '7', '8', 'x', 2,
  729. '7', '9', 'x', 2,
  730. '8', '0', 'x', 2,
  731. '8', '1', 'x', 2,
  732. '8', '2', 'x', 2,
  733. '8', '3', 'x', 2,
  734. '8', '4', 'x', 2,
  735. '8', '5', 'x', 2,
  736. '8', '6', 'x', 2,
  737. '8', '7', 'x', 2,
  738. '8', '8', 'x', 2,
  739. '8', '9', 'x', 2,
  740. '9', '0', 'x', 2,
  741. '9', '1', 'x', 2,
  742. '9', '2', 'x', 2,
  743. '9', '3', 'x', 2,
  744. '9', '4', 'x', 2,
  745. '9', '5', 'x', 2,
  746. '9', '6', 'x', 2,
  747. '9', '7', 'x', 2,
  748. '9', '8', 'x', 2,
  749. '9', '9', 'x', 2,
  750. '1', '0', '0', 3,
  751. '1', '0', '1', 3,
  752. '1', '0', '2', 3,
  753. '1', '0', '3', 3,
  754. '1', '0', '4', 3,
  755. '1', '0', '5', 3,
  756. '1', '0', '6', 3,
  757. '1', '0', '7', 3,
  758. '1', '0', '8', 3,
  759. '1', '0', '9', 3,
  760. '1', '1', '0', 3,
  761. '1', '1', '1', 3,
  762. '1', '1', '2', 3,
  763. '1', '1', '3', 3,
  764. '1', '1', '4', 3,
  765. '1', '1', '5', 3,
  766. '1', '1', '6', 3,
  767. '1', '1', '7', 3,
  768. '1', '1', '8', 3,
  769. '1', '1', '9', 3,
  770. '1', '2', '0', 3,
  771. '1', '2', '1', 3,
  772. '1', '2', '2', 3,
  773. '1', '2', '3', 3,
  774. '1', '2', '4', 3,
  775. '1', '2', '5', 3,
  776. '1', '2', '6', 3,
  777. '1', '2', '7', 3,
  778. '1', '2', '8', 3,
  779. '1', '2', '9', 3,
  780. '1', '3', '0', 3,
  781. '1', '3', '1', 3,
  782. '1', '3', '2', 3,
  783. '1', '3', '3', 3,
  784. '1', '3', '4', 3,
  785. '1', '3', '5', 3,
  786. '1', '3', '6', 3,
  787. '1', '3', '7', 3,
  788. '1', '3', '8', 3,
  789. '1', '3', '9', 3,
  790. '1', '4', '0', 3,
  791. '1', '4', '1', 3,
  792. '1', '4', '2', 3,
  793. '1', '4', '3', 3,
  794. '1', '4', '4', 3,
  795. '1', '4', '5', 3,
  796. '1', '4', '6', 3,
  797. '1', '4', '7', 3,
  798. '1', '4', '8', 3,
  799. '1', '4', '9', 3,
  800. '1', '5', '0', 3,
  801. '1', '5', '1', 3,
  802. '1', '5', '2', 3,
  803. '1', '5', '3', 3,
  804. '1', '5', '4', 3,
  805. '1', '5', '5', 3,
  806. '1', '5', '6', 3,
  807. '1', '5', '7', 3,
  808. '1', '5', '8', 3,
  809. '1', '5', '9', 3,
  810. '1', '6', '0', 3,
  811. '1', '6', '1', 3,
  812. '1', '6', '2', 3,
  813. '1', '6', '3', 3,
  814. '1', '6', '4', 3,
  815. '1', '6', '5', 3,
  816. '1', '6', '6', 3,
  817. '1', '6', '7', 3,
  818. '1', '6', '8', 3,
  819. '1', '6', '9', 3,
  820. '1', '7', '0', 3,
  821. '1', '7', '1', 3,
  822. '1', '7', '2', 3,
  823. '1', '7', '3', 3,
  824. '1', '7', '4', 3,
  825. '1', '7', '5', 3,
  826. '1', '7', '6', 3,
  827. '1', '7', '7', 3,
  828. '1', '7', '8', 3,
  829. '1', '7', '9', 3,
  830. '1', '8', '0', 3,
  831. '1', '8', '1', 3,
  832. '1', '8', '2', 3,
  833. '1', '8', '3', 3,
  834. '1', '8', '4', 3,
  835. '1', '8', '5', 3,
  836. '1', '8', '6', 3,
  837. '1', '8', '7', 3,
  838. '1', '8', '8', 3,
  839. '1', '8', '9', 3,
  840. '1', '9', '0', 3,
  841. '1', '9', '1', 3,
  842. '1', '9', '2', 3,
  843. '1', '9', '3', 3,
  844. '1', '9', '4', 3,
  845. '1', '9', '5', 3,
  846. '1', '9', '6', 3,
  847. '1', '9', '7', 3,
  848. '1', '9', '8', 3,
  849. '1', '9', '9', 3,
  850. '2', '0', '0', 3,
  851. '2', '0', '1', 3,
  852. '2', '0', '2', 3,
  853. '2', '0', '3', 3,
  854. '2', '0', '4', 3,
  855. '2', '0', '5', 3,
  856. '2', '0', '6', 3,
  857. '2', '0', '7', 3,
  858. '2', '0', '8', 3,
  859. '2', '0', '9', 3,
  860. '2', '1', '0', 3,
  861. '2', '1', '1', 3,
  862. '2', '1', '2', 3,
  863. '2', '1', '3', 3,
  864. '2', '1', '4', 3,
  865. '2', '1', '5', 3,
  866. '2', '1', '6', 3,
  867. '2', '1', '7', 3,
  868. '2', '1', '8', 3,
  869. '2', '1', '9', 3,
  870. '2', '2', '0', 3,
  871. '2', '2', '1', 3,
  872. '2', '2', '2', 3,
  873. '2', '2', '3', 3,
  874. '2', '2', '4', 3,
  875. '2', '2', '5', 3,
  876. '2', '2', '6', 3,
  877. '2', '2', '7', 3,
  878. '2', '2', '8', 3,
  879. '2', '2', '9', 3,
  880. '2', '3', '0', 3,
  881. '2', '3', '1', 3,
  882. '2', '3', '2', 3,
  883. '2', '3', '3', 3,
  884. '2', '3', '4', 3,
  885. '2', '3', '5', 3,
  886. '2', '3', '6', 3,
  887. '2', '3', '7', 3,
  888. '2', '3', '8', 3,
  889. '2', '3', '9', 3,
  890. '2', '4', '0', 3,
  891. '2', '4', '1', 3,
  892. '2', '4', '2', 3,
  893. '2', '4', '3', 3,
  894. '2', '4', '4', 3,
  895. '2', '4', '5', 3,
  896. '2', '4', '6', 3,
  897. '2', '4', '7', 3,
  898. '2', '4', '8', 3,
  899. '2', '4', '9', 3,
  900. '2', '5', '0', 3,
  901. '2', '5', '1', 3,
  902. '2', '5', '2', 3,
  903. '2', '5', '3', 3,
  904. '2', '5', '4', 3,
  905. '2', '5', '5', 3
  906. };
  907. DWORD
  908. InetNtoa(
  909. IN struct in_addr inaddr,
  910. OUT CHAR * pchBuffer
  911. )
  912. /*++
  913. Routine Description:
  914. This function takes an Internet address structure specified by the
  915. in parameter. It returns an ASCII string representing the address
  916. in ".'' notation as "a.b.c.d".
  917. Arguments:
  918. inaddr - A structure which represents an Internet host address.
  919. pchBuffer - pointer to at least 16 character buffer for storing
  920. the result of conversion.
  921. Return Value:
  922. If no error occurs, InetNtoa() returns NO_ERROR with the buffer containing
  923. the text address in standard "." notation.
  924. Otherwise, it returns Win32 error code.
  925. --*/
  926. {
  927. PUCHAR p;
  928. PUCHAR buffer = (PUCHAR ) pchBuffer;
  929. PUCHAR b = buffer;
  930. if ( pchBuffer == NULL) {
  931. return ( ERROR_INSUFFICIENT_BUFFER);
  932. }
  933. //
  934. // We do not check for sufficient length of the buffer yet. !!
  935. //
  936. //
  937. // In an unrolled loop, calculate the string value for each of the four
  938. // bytes in an IP address. Note that for values less than 100 we will
  939. // do one or two extra assignments, but we save a test/jump with this
  940. // algorithm.
  941. //
  942. p = (PUCHAR) &inaddr;
  943. *b = NToACharStrings[*p][0];
  944. *(b+1) = NToACharStrings[*p][1];
  945. *(b+2) = NToACharStrings[*p][2];
  946. b += NToACharStrings[*p][3];
  947. *b++ = '.';
  948. p++;
  949. *b = NToACharStrings[*p][0];
  950. *(b+1) = NToACharStrings[*p][1];
  951. *(b+2) = NToACharStrings[*p][2];
  952. b += NToACharStrings[*p][3];
  953. *b++ = '.';
  954. p++;
  955. *b = NToACharStrings[*p][0];
  956. *(b+1) = NToACharStrings[*p][1];
  957. *(b+2) = NToACharStrings[*p][2];
  958. b += NToACharStrings[*p][3];
  959. *b++ = '.';
  960. p++;
  961. *b = NToACharStrings[*p][0];
  962. *(b+1) = NToACharStrings[*p][1];
  963. *(b+2) = NToACharStrings[*p][2];
  964. b += NToACharStrings[*p][3];
  965. *b = '\0';
  966. return ( NO_ERROR);
  967. } // InetNtoa()
  968. BOOL
  969. TcpSockSend(
  970. IN SOCKET sock,
  971. IN LPVOID pBuffer,
  972. IN DWORD cbBuffer,
  973. OUT PDWORD pcbTotalSent,
  974. IN DWORD nTimeout
  975. )
  976. /*++
  977. Description:
  978. Do async socket send
  979. Arguments:
  980. sock - socket
  981. pBuffer - buffer to send
  982. cbBuffer - size of buffer
  983. pcbTotalSent - bytes sent
  984. nTimeout - timeout in seconds to use
  985. Returns:
  986. FALSE if there is any error.
  987. TRUE otherwise
  988. --*/
  989. {
  990. INT serr = 0;
  991. INT cbSent;
  992. DWORD dwBytesSent = 0;
  993. ULONG one;
  994. PCHAR pWin95Buffer = NULL;
  995. DBG_ASSERT( pBuffer != NULL );
  996. //
  997. // On windows95, setup i/o handle to blocking mode,
  998. // as blocking I/O is requested
  999. //
  1000. if ( TsIsWindows95() ) {
  1001. one = 0;
  1002. ioctlsocket( sock, FIONBIO, &one );
  1003. //
  1004. // Probe for writability, if r/o, copy the buffer.
  1005. // This is a workaround for a win95 bug where static pages
  1006. // are getting dirtied when used for sends.
  1007. //
  1008. if ( IsBadWritePtr( pBuffer, 1 ) ) {
  1009. DBGPRINTF((DBG_CONTEXT,
  1010. "TcpSockSend RO[%x] detected. Doing copy.\n",pBuffer));
  1011. pWin95Buffer = (PCHAR)LocalAlloc(LMEM_FIXED, cbBuffer);
  1012. if ( pWin95Buffer != NULL ) {
  1013. CopyMemory(pWin95Buffer, pBuffer, cbBuffer);
  1014. pBuffer = pWin95Buffer;
  1015. } else {
  1016. serr = WSAENOBUFS;
  1017. goto exit;
  1018. }
  1019. }
  1020. }
  1021. //
  1022. // Loop until there's no more data to send.
  1023. //
  1024. while( cbBuffer > 0 ) {
  1025. //
  1026. // Wait for the socket to become writeable.
  1027. //
  1028. serr = 0;
  1029. if ( TsIsWindows95() ) {
  1030. BOOL fWrite = FALSE;
  1031. serr = WaitForSocketWorker(
  1032. INVALID_SOCKET,
  1033. sock,
  1034. NULL,
  1035. &fWrite,
  1036. nTimeout
  1037. );
  1038. }
  1039. if( serr == 0 ) {
  1040. //
  1041. // Write a block to the socket.
  1042. //
  1043. cbSent = send( sock, (CHAR *)pBuffer, (INT)cbBuffer, 0 );
  1044. if( cbSent < 0 ) {
  1045. //
  1046. // Socket error.
  1047. //
  1048. serr = WSAGetLastError();
  1049. DBGPRINTF((DBG_CONTEXT, "TcpSockSend error %d\n",serr));
  1050. } else {
  1051. dwBytesSent += (DWORD)cbSent;
  1052. IF_DEBUG( ERROR ) {
  1053. DBGPRINTF(( DBG_CONTEXT,
  1054. "HTTP: Synchronous send %d bytes @%p to socket %d\n",
  1055. cbSent, pBuffer, sock ));
  1056. }
  1057. }
  1058. }
  1059. if( serr != 0 ) {
  1060. break;
  1061. }
  1062. pBuffer = (LPVOID)( (LPBYTE)pBuffer + cbSent );
  1063. cbBuffer -= (DWORD)cbSent;
  1064. }
  1065. exit:
  1066. if (pcbTotalSent) {
  1067. *pcbTotalSent = dwBytesSent;
  1068. }
  1069. //
  1070. // Set up i/o handle to non-blocking mode , default for ATQ
  1071. //
  1072. if ( TsIsWindows95() ) {
  1073. one = 1;
  1074. ioctlsocket( sock, FIONBIO, &one );
  1075. if ( pWin95Buffer != NULL ) {
  1076. LocalFree(pWin95Buffer);
  1077. }
  1078. }
  1079. if ( serr == 0 ) {
  1080. return(TRUE);
  1081. } else {
  1082. IF_DEBUG( ERROR ) {
  1083. DBGPRINTF(( DBG_CONTEXT,
  1084. "HTTP: Synchronous send socket error %d on socket %d.\n",
  1085. serr, sock));
  1086. }
  1087. SetLastError(serr);
  1088. return(FALSE);
  1089. }
  1090. } // SockSend
  1091. BOOL
  1092. TcpSockRecv(
  1093. IN SOCKET sock,
  1094. IN LPVOID pBuffer,
  1095. IN DWORD cbBuffer,
  1096. OUT LPDWORD pbReceived,
  1097. IN DWORD nTimeout
  1098. )
  1099. /*++
  1100. Description:
  1101. Do async socket recv
  1102. Arguments:
  1103. sock - The target socket.
  1104. pBuffer - Will receive the data.
  1105. cbBuffer - The size (in bytes) of the buffer.
  1106. pbReceived - Will receive the actual number of bytes
  1107. nTimeout - timeout in seconds
  1108. Returns:
  1109. TRUE, if successful
  1110. --*/
  1111. {
  1112. INT serr = 0;
  1113. DWORD cbTotal = 0;
  1114. INT cbReceived;
  1115. DWORD dwBytesRecv = 0;
  1116. ULONG one;
  1117. BOOL fRead = FALSE;
  1118. DBG_ASSERT( pBuffer != NULL );
  1119. DBG_ASSERT( pbReceived != NULL );
  1120. //
  1121. // Set up i/o handle to blocking mode , as blocking I/O is requested
  1122. //
  1123. if ( TsIsWindows95() ) {
  1124. one = 0;
  1125. ioctlsocket( sock, FIONBIO, &one );
  1126. }
  1127. //
  1128. // Wait for the socket to become readable.
  1129. //
  1130. serr = WaitForSocketWorker(
  1131. sock,
  1132. INVALID_SOCKET,
  1133. &fRead,
  1134. NULL,
  1135. nTimeout
  1136. );
  1137. if( serr == 0 )
  1138. {
  1139. //
  1140. // Read a block from the socket.
  1141. //
  1142. DBG_ASSERT( fRead);
  1143. cbReceived = recv( sock, (CHAR *)pBuffer, (INT)cbBuffer, 0 );
  1144. if( cbReceived < 0 )
  1145. {
  1146. //
  1147. // Socket error.
  1148. //
  1149. serr = WSAGetLastError();
  1150. }
  1151. else {
  1152. cbTotal = cbReceived;
  1153. }
  1154. }
  1155. if( serr == 0 )
  1156. {
  1157. //
  1158. // Return total byte count to caller.
  1159. //
  1160. *pbReceived = cbTotal;
  1161. }
  1162. else
  1163. {
  1164. IF_DEBUG( ERROR )
  1165. {
  1166. DBGPRINTF(( DBG_CONTEXT,
  1167. "HTTP: Syncronous rcv socket error %d during recv on socket %d\n",
  1168. serr,
  1169. sock ));
  1170. }
  1171. }
  1172. //
  1173. // Set up i/o handle to blocking mode , as blocking I/O is requested
  1174. //
  1175. if ( TsIsWindows95() ) {
  1176. one = 0;
  1177. ioctlsocket( sock, FIONBIO, &one );
  1178. }
  1179. if ( serr == 0 ) {
  1180. return(TRUE);
  1181. } else {
  1182. SetLastError(serr);
  1183. return(FALSE);
  1184. }
  1185. } // SockRecv
  1186. INT
  1187. WaitForSocketWorker(
  1188. IN SOCKET sockRead,
  1189. IN SOCKET sockWrite,
  1190. IN LPBOOL pfRead,
  1191. IN LPBOOL pfWrite,
  1192. IN DWORD nTimeout
  1193. )
  1194. /*++
  1195. Description:
  1196. Wait routine
  1197. NOTES: Any (but not all) sockets may be INVALID_SOCKET. For
  1198. each socket that is INVALID_SOCKET, the corresponding
  1199. pf* parameter may be NULL.
  1200. Arguments:
  1201. sockRead - The socket to check for readability.
  1202. sockWrite - The socket to check for writeability.
  1203. pfRead - Will receive TRUE if sockRead is readable.
  1204. pfWrite - Will receive TRUE if sockWrite is writeable.
  1205. nTimeout - timeout in seconds
  1206. Returns:
  1207. SOCKERR - 0 if successful, !0 if not. Will return
  1208. WSAETIMEDOUT if the timeout period expired.
  1209. --*/
  1210. {
  1211. INT serr = 0;
  1212. TIMEVAL timeout;
  1213. LPTIMEVAL ptimeout;
  1214. fd_set fdsRead;
  1215. fd_set fdsWrite;
  1216. INT res;
  1217. //
  1218. // Ensure we got valid parameters.
  1219. //
  1220. if( ( sockRead == INVALID_SOCKET ) &&
  1221. ( sockWrite == INVALID_SOCKET ) ) {
  1222. return WSAENOTSOCK;
  1223. }
  1224. timeout.tv_sec = (LONG )nTimeout;
  1225. if( timeout.tv_sec == 0 ) {
  1226. //
  1227. // If the connection timeout == 0, then we have no timeout.
  1228. // So, we block and wait for the specified conditions.
  1229. //
  1230. ptimeout = NULL;
  1231. } else {
  1232. //
  1233. // The connectio timeout is > 0, so setup the timeout structure.
  1234. //
  1235. timeout.tv_usec = 0;
  1236. ptimeout = &timeout;
  1237. }
  1238. for( ; ; ) {
  1239. //
  1240. // Setup our socket sets.
  1241. //
  1242. FD_ZERO( &fdsRead );
  1243. FD_ZERO( &fdsWrite );
  1244. if( sockRead != INVALID_SOCKET ) {
  1245. FD_SET( sockRead, &fdsRead );
  1246. DBG_ASSERT( pfRead != NULL );
  1247. *pfRead = FALSE;
  1248. }
  1249. if( sockWrite != INVALID_SOCKET ) {
  1250. FD_SET( sockWrite, &fdsWrite );
  1251. DBG_ASSERT( pfWrite != NULL );
  1252. *pfWrite = FALSE;
  1253. }
  1254. //
  1255. // Wait for one of the conditions to be met.
  1256. //
  1257. res = select( 0, &fdsRead, &fdsWrite, NULL, ptimeout );
  1258. if( res == 0 ) {
  1259. //
  1260. // Timeout.
  1261. //
  1262. serr = WSAETIMEDOUT;
  1263. break;
  1264. } else if( res == SOCKET_ERROR ) {
  1265. //
  1266. // Bad news.
  1267. //
  1268. serr = WSAGetLastError();
  1269. break;
  1270. } else {
  1271. BOOL fSomethingWasSet = FALSE;
  1272. if( pfRead != NULL ) {
  1273. *pfRead = FD_ISSET( sockRead, &fdsRead );
  1274. fSomethingWasSet = TRUE;
  1275. }
  1276. if( pfWrite != NULL ) {
  1277. *pfWrite = FD_ISSET( sockWrite, &fdsWrite );
  1278. fSomethingWasSet = TRUE;
  1279. }
  1280. if( fSomethingWasSet ) {
  1281. //
  1282. // Success.
  1283. //
  1284. serr = 0;
  1285. break;
  1286. } else {
  1287. //
  1288. // select() returned with neither a timeout, nor
  1289. // an error, nor any bits set. This feels bad...
  1290. //
  1291. DBG_ASSERT( FALSE );
  1292. continue;
  1293. }
  1294. }
  1295. }
  1296. return serr;
  1297. } // WaitForSocketWorker()
  1298. BOOL
  1299. TcpSockTest(
  1300. IN SOCKET sock
  1301. )
  1302. /*++
  1303. Description:
  1304. Test the socket if still connected.
  1305. Use select, and if readable, use recv
  1306. Arguments:
  1307. sock - socket
  1308. Returns:
  1309. TRUE if the socket most likely is still connected
  1310. FALSE if the socket is disconnected or an error occured
  1311. --*/
  1312. {
  1313. TIMEVAL timeout;
  1314. fd_set fdsRead;
  1315. INT res;
  1316. CHAR bOneByte;
  1317. // select for read with zero timeout
  1318. FD_ZERO( &fdsRead );
  1319. FD_SET( sock, &fdsRead );
  1320. timeout.tv_sec = 0;
  1321. timeout.tv_usec = 0;
  1322. res = select( 0, &fdsRead, NULL, NULL, &timeout );
  1323. if ( res == 0 ) {
  1324. // No data to be read --
  1325. // have to assume socket is still connected
  1326. return TRUE;
  1327. } else if ( res == SOCKET_ERROR ) {
  1328. // Something went wrong during select -- assume disconnected
  1329. return FALSE;
  1330. }
  1331. DBG_ASSERT( res == 1 );
  1332. // recv 1 byte (PEEK)
  1333. // select returning 1 above guarantees recv will not block
  1334. res = recv( sock, &bOneByte, 1, MSG_PEEK );
  1335. if ( res == 0 || res == SOCKET_ERROR ) {
  1336. // Socket closed or an error -- socket is disconnected
  1337. return FALSE;
  1338. }
  1339. DBG_ASSERT( res == 1 );
  1340. // Read one byte successfully -- assume still connected
  1341. return TRUE;
  1342. } // SockTest
  1343. BOOL
  1344. DoSynchronousReadFile(
  1345. IN HANDLE hFile,
  1346. IN PCHAR Buffer,
  1347. IN DWORD nBuffer,
  1348. OUT PDWORD nRead,
  1349. IN LPOVERLAPPED Overlapped
  1350. )
  1351. /*++
  1352. Description:
  1353. Does Asynchronous file reads. Assumes that NT handles are
  1354. opened for OVERLAPPED I/O, win95 handles are not.
  1355. Arguments:
  1356. hFile - Handle to use for the read
  1357. Buffer - Buffer to read with
  1358. nBuffer - size of buffer
  1359. nRead - returns the number of bytes read
  1360. Overlapped - user supplied overlapped structure
  1361. Returns:
  1362. TRUE/FALSE
  1363. --*/
  1364. {
  1365. BOOL fNewEvent = FALSE;
  1366. OVERLAPPED ov;
  1367. BOOL fRet = FALSE;
  1368. if ( Overlapped == NULL ) {
  1369. Overlapped = &ov;
  1370. ov.Offset = 0;
  1371. ov.OffsetHigh = 0;
  1372. ov.hEvent = IIS_CREATE_EVENT(
  1373. "OVERLAPPED::hEvent",
  1374. &ov,
  1375. TRUE,
  1376. FALSE
  1377. );
  1378. if ( ov.hEvent == NULL ) {
  1379. DBGPRINTF((DBG_CONTEXT,"CreateEvent failed with %d\n",
  1380. GetLastError()));
  1381. goto ErrorExit;
  1382. }
  1383. fNewEvent = TRUE;
  1384. }
  1385. if ( !TsIsWindows95() ) {
  1386. DWORD err = NO_ERROR;
  1387. if ( !ReadFile( hFile,
  1388. Buffer,
  1389. nBuffer,
  1390. nRead,
  1391. Overlapped )) {
  1392. err = GetLastError();
  1393. if ( (err != ERROR_IO_PENDING) &&
  1394. (err != ERROR_HANDLE_EOF) ) {
  1395. DBGPRINTF((DBG_CONTEXT,"Error %d in ReadFile\n",
  1396. err));
  1397. goto ErrorExit;
  1398. }
  1399. }
  1400. if ( err == ERROR_IO_PENDING ) {
  1401. if ( !GetOverlappedResult( hFile,
  1402. Overlapped,
  1403. nRead,
  1404. TRUE )) {
  1405. err = GetLastError();
  1406. DBGPRINTF((DBG_CONTEXT,"Error %d in GetOverlappedResult\n",
  1407. err));
  1408. if ( err != ERROR_HANDLE_EOF ) {
  1409. goto ErrorExit;
  1410. }
  1411. }
  1412. }
  1413. } else {
  1414. //
  1415. // No async file i/o for win95
  1416. //
  1417. if ( !ReadFile( hFile,
  1418. Buffer,
  1419. nBuffer,
  1420. nRead,
  1421. NULL )) {
  1422. DBGPRINTF((DBG_CONTEXT,"Error %d in ReadFile\n",
  1423. GetLastError()));
  1424. goto ErrorExit;
  1425. }
  1426. }
  1427. fRet = TRUE;
  1428. ErrorExit:
  1429. if ( fNewEvent ) {
  1430. DBG_REQUIRE(CloseHandle( ov.hEvent ));
  1431. }
  1432. return(fRet);
  1433. } // DoSynchronousReadFile