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.

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