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.

1284 lines
19 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. bigint.hxx
  5. Abstract:
  6. The BIG_INT class models a 64 bit signed integer.
  7. This class is meant to be light and will occupy only 64 bits of space.
  8. It should be manipulated exactly as an INT would be.
  9. There will be no constructor or destructor. A BIG_INT will be
  10. uninitialized until a value is assigned to it.
  11. This implementation of BIG_INT uses the NT LARGE_INTEGER structure.
  12. Author:
  13. Norbert P. Kusters (norbertk) 8-Jul-91
  14. --*/
  15. #if !defined(BIG_INT_DEFN)
  16. #define BIG_INT_DEFN
  17. #include <ulib.hxx>
  18. #if defined ( _AUTOCHECK_ )
  19. #define IFSUTIL_EXPORT
  20. #elif defined ( _IFSUTIL_MEMBER_ )
  21. #define IFSUTIL_EXPORT __declspec(dllexport)
  22. #else
  23. #define IFSUTIL_EXPORT __declspec(dllimport)
  24. #endif
  25. DEFINE_POINTER_AND_REFERENCE_TYPES( LARGE_INTEGER );
  26. DECLARE_CLASS( BIG_INT );
  27. class BIG_INT {
  28. public:
  29. NONVIRTUAL
  30. BIG_INT(
  31. );
  32. NONVIRTUAL
  33. BIG_INT(
  34. IN const INT LowPart
  35. );
  36. NONVIRTUAL
  37. BIG_INT(
  38. IN const UINT LowPart
  39. );
  40. NONVIRTUAL
  41. BIG_INT(
  42. IN const ULONG LowPart
  43. );
  44. NONVIRTUAL
  45. BIG_INT(
  46. IN const SLONG LowPart
  47. );
  48. NONVIRTUAL
  49. BIG_INT(
  50. IN const LARGE_INTEGER LargeInteger
  51. );
  52. NONVIRTUAL
  53. BIG_INT(
  54. IN const ULONGLONG UlongLong
  55. );
  56. NONVIRTUAL
  57. VOID
  58. operator=(
  59. IN const INT LowPart
  60. );
  61. NONVIRTUAL
  62. VOID
  63. operator=(
  64. IN const UINT LowPart
  65. );
  66. NONVIRTUAL
  67. VOID
  68. operator=(
  69. IN const SLONG LowPart
  70. );
  71. NONVIRTUAL
  72. VOID
  73. operator=(
  74. IN const ULONG LowPart
  75. );
  76. NONVIRTUAL
  77. VOID
  78. operator=(
  79. IN const LARGE_INTEGER LargeInteger
  80. );
  81. NONVIRTUAL
  82. VOID
  83. operator=(
  84. IN const LONG64 Long64
  85. )
  86. {
  87. x = Long64;
  88. }
  89. NONVIRTUAL
  90. VOID
  91. operator=(
  92. IN const ULONG64 Ulong64
  93. )
  94. {
  95. x = Ulong64;
  96. }
  97. NONVIRTUAL
  98. VOID
  99. Set(
  100. IN const ULONG LowPart,
  101. IN const SLONG HighPart
  102. );
  103. NONVIRTUAL
  104. IFSUTIL_EXPORT
  105. VOID
  106. Set(
  107. IN UCHAR ByteCount,
  108. IN PCUCHAR CompressedInteger
  109. );
  110. NONVIRTUAL
  111. const ULONG
  112. GetLowPart(
  113. ) CONST;
  114. NONVIRTUAL
  115. const SLONG
  116. GetHighPart(
  117. ) CONST;
  118. NONVIRTUAL
  119. LARGE_INTEGER
  120. GetLargeInteger(
  121. ) CONST;
  122. NONVIRTUAL
  123. LONGLONG
  124. GetQuadPart(
  125. ) CONST;
  126. NONVIRTUAL
  127. IFSUTIL_EXPORT
  128. VOID
  129. QueryCompressedInteger(
  130. OUT PUCHAR ByteCount,
  131. OUT PUCHAR CompressedInteger
  132. ) CONST;
  133. NONVIRTUAL
  134. VOID
  135. operator+=(
  136. IN const BIG_INT BigInt
  137. );
  138. NONVIRTUAL
  139. BIG_INT
  140. operator-(
  141. ) CONST;
  142. NONVIRTUAL
  143. VOID
  144. operator-=(
  145. IN const BIG_INT BigInt
  146. );
  147. FRIEND
  148. BIG_INT
  149. operator+(
  150. IN const BIG_INT Left,
  151. IN const BIG_INT Right
  152. );
  153. FRIEND
  154. BIG_INT
  155. operator-(
  156. IN const BIG_INT Left,
  157. IN const BIG_INT Right
  158. );
  159. FRIEND
  160. BIG_INT
  161. operator*(
  162. IN const BIG_INT Left,
  163. IN const SLONG Right
  164. );
  165. FRIEND
  166. BIG_INT
  167. operator*(
  168. IN const SLONG Left,
  169. IN const BIG_INT Right
  170. );
  171. FRIEND
  172. BIG_INT
  173. operator/(
  174. IN const BIG_INT Left,
  175. IN const BIG_INT Right
  176. );
  177. FRIEND
  178. BIG_INT
  179. operator%(
  180. IN const BIG_INT Left,
  181. IN const BIG_INT Right
  182. );
  183. FRIEND
  184. BOOLEAN
  185. operator==(
  186. IN const BIG_INT Left,
  187. IN const BIG_INT Right
  188. );
  189. FRIEND
  190. BOOLEAN
  191. operator!=(
  192. IN const BIG_INT Left,
  193. IN const BIG_INT Right
  194. );
  195. FRIEND
  196. BOOLEAN
  197. operator<(
  198. IN const BIG_INT Left,
  199. IN const BIG_INT Right
  200. );
  201. FRIEND
  202. BOOLEAN
  203. operator<=(
  204. IN const BIG_INT Left,
  205. IN const BIG_INT Right
  206. );
  207. FRIEND
  208. BOOLEAN
  209. operator>(
  210. IN const BIG_INT Left,
  211. IN const BIG_INT Right
  212. );
  213. FRIEND
  214. BOOLEAN
  215. operator>=(
  216. IN const BIG_INT Left,
  217. IN const BIG_INT Right
  218. );
  219. FRIEND
  220. BOOLEAN
  221. CompareLT(
  222. IN const BIG_INT Left,
  223. IN const BIG_INT Right
  224. );
  225. FRIEND
  226. BOOLEAN
  227. CompareLTEQ(
  228. IN const BIG_INT Left,
  229. IN const BIG_INT Right
  230. );
  231. FRIEND
  232. BOOLEAN
  233. CompareGT(
  234. IN const BIG_INT Left,
  235. IN const BIG_INT Right
  236. );
  237. FRIEND
  238. BOOLEAN
  239. CompareGTEQ(
  240. IN const BIG_INT Left,
  241. IN const BIG_INT Right
  242. );
  243. private:
  244. __int64 x;
  245. };
  246. INLINE
  247. BIG_INT::BIG_INT(
  248. )
  249. /*++
  250. Routine Description:
  251. Constructor for BIG_INT.
  252. Arguments:
  253. None.
  254. Return Value:
  255. None.
  256. --*/
  257. {
  258. }
  259. INLINE
  260. VOID
  261. BIG_INT::operator=(
  262. IN const INT LowPart
  263. )
  264. /*++
  265. Routine Description:
  266. This routine copies an INT into a BIG_INT.
  267. Arguments:
  268. LowPart - Supplies an integer.
  269. Return Value:
  270. None.
  271. --*/
  272. {
  273. x = LowPart;
  274. }
  275. INLINE
  276. VOID
  277. BIG_INT::operator=(
  278. IN const UINT LowPart
  279. )
  280. /*++
  281. Routine Description:
  282. This routine copies a UINT into a BIG_INT.
  283. Arguments:
  284. LowPart - Supplies an unsigned integer.
  285. Return Value:
  286. None.
  287. --*/
  288. {
  289. x = LowPart;
  290. }
  291. INLINE
  292. VOID
  293. BIG_INT::operator=(
  294. IN const SLONG LowPart
  295. )
  296. /*++
  297. Routine Description:
  298. This routine copies a LONG into a BIG_INT.
  299. Arguments:
  300. LowPart - Supplies a long integer.
  301. Return Value:
  302. None.
  303. --*/
  304. {
  305. x = LowPart;
  306. }
  307. INLINE
  308. VOID
  309. BIG_INT::operator=(
  310. IN const ULONG LowPart
  311. )
  312. /*++
  313. Routine Description:
  314. This routine copies a ULONG into a BIG_INT.
  315. Arguments:
  316. LowPart - Supplies an unsigned long integer.
  317. Return Value:
  318. None.
  319. --*/
  320. {
  321. x = LowPart;
  322. }
  323. INLINE
  324. VOID
  325. BIG_INT::operator=(
  326. IN const LARGE_INTEGER LargeInteger
  327. )
  328. /*++
  329. Routine Description:
  330. This routine copies a LARGE_INTEGER into a BIG_INT.
  331. Arguments:
  332. LargeInteger -- supplies a large integer
  333. Return Value:
  334. None.
  335. --*/
  336. {
  337. x = LargeInteger.QuadPart;
  338. }
  339. INLINE
  340. BIG_INT::BIG_INT(
  341. IN const INT LowPart
  342. )
  343. /*++
  344. Routine Description:
  345. Constructor for BIG_INT.
  346. Arguments:
  347. LowPart - Supplies an integer.
  348. Return Value:
  349. None.
  350. --*/
  351. {
  352. x = LowPart;
  353. }
  354. INLINE
  355. BIG_INT::BIG_INT(
  356. IN const UINT LowPart
  357. )
  358. /*++
  359. Routine Description:
  360. Constructor for BIG_INT.
  361. Arguments:
  362. LowPart - Supplies an unsigned integer.
  363. Return Value:
  364. None.
  365. --*/
  366. {
  367. x = LowPart;
  368. }
  369. INLINE
  370. BIG_INT::BIG_INT(
  371. IN const SLONG LowPart
  372. )
  373. /*++
  374. Routine Description:
  375. Constructor for BIG_INT.
  376. Arguments:
  377. LowPart - Supplies a long integer.
  378. Return Value:
  379. None.
  380. --*/
  381. {
  382. x = LowPart;
  383. }
  384. INLINE
  385. BIG_INT::BIG_INT(
  386. IN const ULONG LowPart
  387. )
  388. /*++
  389. Routine Description:
  390. Constructor for BIG_INT.
  391. Arguments:
  392. LowPart - Supplies an unsigned long integer.
  393. Return Value:
  394. None.
  395. --*/
  396. {
  397. x = LowPart;
  398. }
  399. INLINE
  400. BIG_INT::BIG_INT(
  401. IN const LARGE_INTEGER LargeInteger
  402. )
  403. /*++
  404. Routine Description:
  405. Constructor for BIG_INT to permit initialization with a LARGE_INTEGER
  406. Arguments:
  407. LargeInteger -- supplies a large integer.
  408. Return Value:
  409. None.
  410. --*/
  411. {
  412. x = LargeInteger.QuadPart;
  413. }
  414. INLINE
  415. BIG_INT::BIG_INT(
  416. IN const ULONGLONG UlongLong
  417. )
  418. /*++
  419. Routine Description:
  420. Constructor for BIG_INT to permit initialization with a ULONGLOGN
  421. Arguments:
  422. UlongLong -- supplies a unsigned 64-bit int.
  423. Return Value:
  424. None.
  425. --*/
  426. {
  427. x = UlongLong;
  428. }
  429. INLINE
  430. VOID
  431. BIG_INT::Set(
  432. IN const ULONG LowPart,
  433. IN const SLONG HighPart
  434. )
  435. /*++
  436. Routine Description:
  437. This routine sets a BIG_INT to an initial value.
  438. Arguments:
  439. LowPart - Supplies the low part of the BIG_INT.
  440. HighPart - Supplies the high part of the BIG_INT.
  441. Return Value:
  442. None.
  443. --*/
  444. {
  445. x = (__int64)(((ULONGLONG)HighPart << 32) | LowPart);
  446. }
  447. INLINE
  448. const ULONG
  449. BIG_INT::GetLowPart(
  450. ) CONST
  451. /*++
  452. Routine Description:
  453. This routine computes the low part of the BIG_INT.
  454. Arguments:
  455. None.
  456. Return Value:
  457. The low part of the BIG_INT.
  458. --*/
  459. {
  460. return (ULONG)(((ULONGLONG)x) & 0xFFFFFFFF);
  461. }
  462. // Note: billmc -- this could probably return an RCLONG, for
  463. // greater efficiency, but that generates warnings.
  464. INLINE
  465. const SLONG
  466. BIG_INT::GetHighPart(
  467. ) CONST
  468. /*++
  469. Routine Description:
  470. This routine computes the high part of the BIG_INT.
  471. Arguments:
  472. None.
  473. Return Value:
  474. The high part of the BIG_INT.
  475. --*/
  476. {
  477. LARGE_INTEGER r;
  478. r.QuadPart = x;
  479. return r.HighPart;
  480. }
  481. INLINE
  482. LARGE_INTEGER
  483. BIG_INT::GetLargeInteger(
  484. ) CONST
  485. /*++
  486. Routine Description:
  487. This routine returns the large integer embedded in the BIG_INT.
  488. Arguments:
  489. None.
  490. Return Value:
  491. The large-integer value of the BIG_INT.
  492. --*/
  493. {
  494. LARGE_INTEGER r;
  495. r.QuadPart = x;
  496. return r;
  497. }
  498. INLINE
  499. LONGLONG
  500. BIG_INT::GetQuadPart(
  501. ) CONST
  502. /*++
  503. Routine Description:
  504. This routine returns the large integer embedded in the BIG_INT.
  505. Arguments:
  506. None.
  507. Return Value:
  508. The large-integer value of the BIG_INT.
  509. --*/
  510. {
  511. return x;
  512. }
  513. INLINE
  514. VOID
  515. BIG_INT::operator+=(
  516. IN const BIG_INT BigInt
  517. )
  518. /*++
  519. Routine Description:
  520. This routine adds another BIG_INT to this one.
  521. Arguments:
  522. BigInt - Supplies the BIG_INT to add to the current BIG_INT.
  523. Return Value:
  524. None.
  525. --*/
  526. {
  527. x += BigInt.x;
  528. }
  529. INLINE
  530. BIG_INT
  531. BIG_INT::operator-(
  532. ) CONST
  533. /*++
  534. Routine Description:
  535. This routine computes the negation of the current BIG_INT.
  536. Arguments:
  537. None.
  538. Return Value:
  539. The negation of the current BIG_INT.
  540. --*/
  541. {
  542. BIG_INT r;
  543. r.x = -x;
  544. return r;
  545. }
  546. INLINE
  547. VOID
  548. BIG_INT::operator-=(
  549. IN const BIG_INT BigInt
  550. )
  551. /*++
  552. Routine Description:
  553. This routine subtracts a BIG_INT from this one.
  554. Arguments:
  555. BigInt - Supplies a BIG_INT to subtract from the current BIG_INT.
  556. Return Value:
  557. None.
  558. --*/
  559. {
  560. x -= BigInt.x;
  561. }
  562. INLINE
  563. BIG_INT
  564. operator+(
  565. IN const BIG_INT Left,
  566. IN const BIG_INT Right
  567. )
  568. /*++
  569. Routine Description:
  570. This routine computes the sum of two BIG_INTs.
  571. Arguments:
  572. Left - Supplies the left argument.
  573. Right - Supplies the right argument.
  574. Return Value:
  575. The sum of Left and Right.
  576. --*/
  577. {
  578. BIG_INT r;
  579. r.x = Left.x + Right.x;
  580. return r;
  581. }
  582. INLINE
  583. BIG_INT
  584. operator-(
  585. IN const BIG_INT Left,
  586. IN const BIG_INT Right
  587. )
  588. /*++
  589. Routine Description:
  590. This routine computes the difference of two BIG_INTs.
  591. Arguments:
  592. Left - Supplies the left argument.
  593. Right - Supplies the right argument.
  594. Return Value:
  595. The difference between Left and Right.
  596. --*/
  597. {
  598. BIG_INT r;
  599. r.x = Left.x - Right.x;
  600. return r;
  601. }
  602. INLINE
  603. BIG_INT
  604. operator*(
  605. IN const BIG_INT Left,
  606. IN const SLONG Right
  607. )
  608. /*++
  609. Routine Description:
  610. This routine computes the product of a BIG_INT and a LONG.
  611. Arguments:
  612. Left - Supplies the left argument.
  613. Right - Supplies the right argument.
  614. Return Value:
  615. The product of Left and Right.
  616. --*/
  617. {
  618. BIG_INT r;
  619. r.x = Left.x * Right;
  620. return r;
  621. }
  622. INLINE
  623. BIG_INT
  624. operator*(
  625. IN const SLONG Left,
  626. IN const BIG_INT Right
  627. )
  628. /*++
  629. Routine Description:
  630. This routine computes the product of a BIG_INT and a LONG.
  631. Arguments:
  632. Left - Supplies the left argument.
  633. Right - Supplies the right argument.
  634. Return Value:
  635. The product of Left and Right.
  636. --*/
  637. {
  638. return Right*Left;
  639. }
  640. INLINE
  641. BIG_INT
  642. operator/(
  643. IN const BIG_INT Left,
  644. IN const BIG_INT Right
  645. )
  646. /*++
  647. Routine Description:
  648. This routine computes the quotient of two BIG_INTs.
  649. Arguments:
  650. Left - Supplies the left argument.
  651. Right - Supplies the right argument.
  652. Return Value:
  653. The quotient of Left and Right.
  654. --*/
  655. {
  656. BIG_INT r;
  657. r.x = Left.x / Right.x;
  658. return r;
  659. }
  660. INLINE
  661. BIG_INT
  662. operator%(
  663. IN const BIG_INT Left,
  664. IN const BIG_INT Right
  665. )
  666. /*++
  667. Routine Description:
  668. This routine computes the modulus of two BIG_INTs.
  669. Arguments:
  670. Left - Supplies the left argument.
  671. Right - Supplies the right argument.
  672. Return Value:
  673. The modulus of Left and Right.
  674. --*/
  675. {
  676. BIG_INT r;
  677. r.x = Left.x % Right.x;
  678. return r;
  679. }
  680. INLINE
  681. BOOLEAN
  682. operator<(
  683. IN const BIG_INT Left,
  684. IN const BIG_INT Right
  685. )
  686. /*++
  687. Routine Description:
  688. This routine compares two BIG_INTs.
  689. Arguments:
  690. Left - Supplies the left argument.
  691. Right - Supplies the right argument.
  692. Return Value:
  693. FALSE - Left is not less than Right.
  694. TRUE - Left is less than Right.
  695. --*/
  696. {
  697. return Left.x < Right.x;
  698. }
  699. INLINE
  700. BOOLEAN
  701. operator<=(
  702. IN const BIG_INT Left,
  703. IN const BIG_INT Right
  704. )
  705. /*++
  706. Routine Description:
  707. This routine compares two BIG_INTs.
  708. Arguments:
  709. Left - Supplies the left argument.
  710. Right - Supplies the right argument.
  711. Return Value:
  712. FALSE - Left is not less than or equal to Right.
  713. TRUE - Left is less than or equal to Right.
  714. --*/
  715. {
  716. return Left.x <= Right.x;
  717. }
  718. INLINE
  719. BOOLEAN
  720. operator>(
  721. IN const BIG_INT Left,
  722. IN const BIG_INT Right
  723. )
  724. /*++
  725. Routine Description:
  726. This routine compares two BIG_INTs.
  727. Arguments:
  728. Left - Supplies the left argument.
  729. Right - Supplies the right argument.
  730. Return Value:
  731. FALSE - Left is not greater than Right.
  732. TRUE - Left is greater than Right.
  733. --*/
  734. {
  735. return Left.x > Right.x;
  736. }
  737. INLINE
  738. BOOLEAN
  739. operator>=(
  740. IN const BIG_INT Left,
  741. IN const BIG_INT Right
  742. )
  743. /*++
  744. Routine Description:
  745. This routine compares two BIG_INTs.
  746. Arguments:
  747. Left - Supplies the left argument.
  748. Right - Supplies the right argument.
  749. Return Value:
  750. FALSE - Left is not greater than or equal to Right.
  751. TRUE - Left is greater than or equal to Right.
  752. --*/
  753. {
  754. return Left.x >= Right.x;
  755. }
  756. INLINE
  757. BOOLEAN
  758. operator==(
  759. IN const BIG_INT Left,
  760. IN const BIG_INT Right
  761. )
  762. /*++
  763. Routine Description:
  764. This routine compares two BIG_INTs for equality.
  765. Arguments:
  766. Left - Supplies the left argument.
  767. Right - Supplies the right argument.
  768. Return Value:
  769. FALSE - Left is not equal to Right.
  770. TRUE - Left is equal to Right.
  771. --*/
  772. {
  773. return Left.x == Right.x;
  774. }
  775. INLINE
  776. BOOLEAN
  777. operator!=(
  778. IN const BIG_INT Left,
  779. IN const BIG_INT Right
  780. )
  781. /*++
  782. Routine Description:
  783. This routine compares two BIG_INTs for equality.
  784. Arguments:
  785. Left - Supplies the left argument.
  786. Right - Supplies the right argument.
  787. Return Value:
  788. FALSE - Left is equal to Right.
  789. TRUE - Left is not equal to Right.
  790. --*/
  791. {
  792. return Left.x != Right.x;
  793. }
  794. INLINE
  795. BOOLEAN
  796. CompareGTEQ(
  797. IN const BIG_INT Left,
  798. IN const BIG_INT Right
  799. )
  800. /*++
  801. Routine Description:
  802. This routine compares two BIG_INTs by treating them
  803. as unsigned numbers.
  804. Arguments:
  805. Left - Supplies the left argument.
  806. Right - Supplies the right argument.
  807. Return Value:
  808. FALSE - Left is not greater than or equal to Right.
  809. TRUE - Left is greater than or equal to Right.
  810. --*/
  811. {
  812. return (unsigned __int64)Left.x >= (unsigned __int64)Right.x;
  813. }
  814. INLINE
  815. BOOLEAN
  816. CompareGT(
  817. IN const BIG_INT Left,
  818. IN const BIG_INT Right
  819. )
  820. /*++
  821. Routine Description:
  822. This routine compares two BIG_INTs by treating them
  823. as unsigned numbers.
  824. Arguments:
  825. Left - Supplies the left argument.
  826. Right - Supplies the right argument.
  827. Return Value:
  828. FALSE - Left is not greater than Right.
  829. TRUE - Left is greater than Right.
  830. --*/
  831. {
  832. return (unsigned __int64)Left.x > (unsigned __int64)Right.x;
  833. }
  834. INLINE
  835. BOOLEAN
  836. CompareLTEQ(
  837. IN const BIG_INT Left,
  838. IN const BIG_INT Right
  839. )
  840. /*++
  841. Routine Description:
  842. This routine compares two BIG_INTs by treating them
  843. as unsigned numbers.
  844. Arguments:
  845. Left - Supplies the left argument.
  846. Right - Supplies the right argument.
  847. Return Value:
  848. FALSE - Left is not less than or equal to Right.
  849. TRUE - Left is less than or equal to Right.
  850. --*/
  851. {
  852. return (unsigned __int64)Left.x <= (unsigned __int64)Right.x;
  853. }
  854. INLINE
  855. BOOLEAN
  856. CompareLT(
  857. IN const BIG_INT Left,
  858. IN const BIG_INT Right
  859. )
  860. /*++
  861. Routine Description:
  862. This routine compares two BIG_INTs by treating them
  863. as unsigned numbers.
  864. Arguments:
  865. Left - Supplies the left argument.
  866. Right - Supplies the right argument.
  867. Return Value:
  868. FALSE - Left is not less than Right.
  869. TRUE - Left is less than Right.
  870. --*/
  871. {
  872. return (unsigned __int64)Left.x < (unsigned __int64)Right.x;
  873. }
  874. #endif // BIG_INT_DEFN