xref: /aosp_15_r20/external/pdfium/third_party/bigint/BigIntegerUtils.hh (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1*3ac0a46fSAndroid Build Coastguard Worker // Copyright 2014 The PDFium Authors
2*3ac0a46fSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*3ac0a46fSAndroid Build Coastguard Worker // found in the LICENSE file.
4*3ac0a46fSAndroid Build Coastguard Worker 
5*3ac0a46fSAndroid Build Coastguard Worker // Original code by Matt McCutchen, see the LICENSE file.
6*3ac0a46fSAndroid Build Coastguard Worker 
7*3ac0a46fSAndroid Build Coastguard Worker #ifndef BIGINTEGERUTILS_H
8*3ac0a46fSAndroid Build Coastguard Worker #define BIGINTEGERUTILS_H
9*3ac0a46fSAndroid Build Coastguard Worker 
10*3ac0a46fSAndroid Build Coastguard Worker #include "BigInteger.hh"
11*3ac0a46fSAndroid Build Coastguard Worker #include <string>
12*3ac0a46fSAndroid Build Coastguard Worker #include <iosfwd>
13*3ac0a46fSAndroid Build Coastguard Worker 
14*3ac0a46fSAndroid Build Coastguard Worker /* This file provides:
15*3ac0a46fSAndroid Build Coastguard Worker  * - Convenient std::string <-> BigUnsigned/BigInteger conversion routines
16*3ac0a46fSAndroid Build Coastguard Worker  * - std::ostream << operators for BigUnsigned/BigInteger */
17*3ac0a46fSAndroid Build Coastguard Worker 
18*3ac0a46fSAndroid Build Coastguard Worker // std::string conversion routines.  Base 10 only.
19*3ac0a46fSAndroid Build Coastguard Worker std::string bigUnsignedToString(const BigUnsigned &x);
20*3ac0a46fSAndroid Build Coastguard Worker std::string bigIntegerToString(const BigInteger &x);
21*3ac0a46fSAndroid Build Coastguard Worker BigUnsigned stringToBigUnsigned(const std::string &s);
22*3ac0a46fSAndroid Build Coastguard Worker BigInteger stringToBigInteger(const std::string &s);
23*3ac0a46fSAndroid Build Coastguard Worker 
24*3ac0a46fSAndroid Build Coastguard Worker // Creates a BigInteger from data such as `char's; read below for details.
25*3ac0a46fSAndroid Build Coastguard Worker template <class T>
26*3ac0a46fSAndroid Build Coastguard Worker BigInteger dataToBigInteger(const T* data, BigInteger::Index length, BigInteger::Sign sign);
27*3ac0a46fSAndroid Build Coastguard Worker 
28*3ac0a46fSAndroid Build Coastguard Worker // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
29*3ac0a46fSAndroid Build Coastguard Worker std::ostream &operator <<(std::ostream &os, const BigUnsigned &x);
30*3ac0a46fSAndroid Build Coastguard Worker 
31*3ac0a46fSAndroid Build Coastguard Worker // Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
32*3ac0a46fSAndroid Build Coastguard Worker // My somewhat arbitrary policy: a negative sign comes before a base indicator (like -0xFF).
33*3ac0a46fSAndroid Build Coastguard Worker std::ostream &operator <<(std::ostream &os, const BigInteger &x);
34*3ac0a46fSAndroid Build Coastguard Worker 
35*3ac0a46fSAndroid Build Coastguard Worker // BEGIN TEMPLATE DEFINITIONS.
36*3ac0a46fSAndroid Build Coastguard Worker 
37*3ac0a46fSAndroid Build Coastguard Worker /*
38*3ac0a46fSAndroid Build Coastguard Worker  * Converts binary data to a BigInteger.
39*3ac0a46fSAndroid Build Coastguard Worker  * Pass an array `data', its length, and the desired sign.
40*3ac0a46fSAndroid Build Coastguard Worker  *
41*3ac0a46fSAndroid Build Coastguard Worker  * Elements of `data' may be of any type `T' that has the following
42*3ac0a46fSAndroid Build Coastguard Worker  * two properties (this includes almost all integral types):
43*3ac0a46fSAndroid Build Coastguard Worker  *
44*3ac0a46fSAndroid Build Coastguard Worker  * (1) `sizeof(T)' correctly gives the amount of binary data in one
45*3ac0a46fSAndroid Build Coastguard Worker  * value of `T' and is a factor of `sizeof(Blk)'.
46*3ac0a46fSAndroid Build Coastguard Worker  *
47*3ac0a46fSAndroid Build Coastguard Worker  * (2) When a value of `T' is casted to a `Blk', the low bytes of
48*3ac0a46fSAndroid Build Coastguard Worker  * the result contain the desired binary data.
49*3ac0a46fSAndroid Build Coastguard Worker  */
50*3ac0a46fSAndroid Build Coastguard Worker template <class T>
dataToBigInteger(const T * data,BigInteger::Index length,BigInteger::Sign sign)51*3ac0a46fSAndroid Build Coastguard Worker BigInteger dataToBigInteger(const T* data, BigInteger::Index length, BigInteger::Sign sign) {
52*3ac0a46fSAndroid Build Coastguard Worker 	// really ceiling(numBytes / sizeof(BigInteger::Blk))
53*3ac0a46fSAndroid Build Coastguard Worker 	unsigned int pieceSizeInBits = 8 * sizeof(T);
54*3ac0a46fSAndroid Build Coastguard Worker 	unsigned int piecesPerBlock = sizeof(BigInteger::Blk) / sizeof(T);
55*3ac0a46fSAndroid Build Coastguard Worker 	unsigned int numBlocks = (length + piecesPerBlock - 1) / piecesPerBlock;
56*3ac0a46fSAndroid Build Coastguard Worker 
57*3ac0a46fSAndroid Build Coastguard Worker 	// Allocate our block array
58*3ac0a46fSAndroid Build Coastguard Worker 	BigInteger::Blk *blocks = new BigInteger::Blk[numBlocks];
59*3ac0a46fSAndroid Build Coastguard Worker 
60*3ac0a46fSAndroid Build Coastguard Worker 	BigInteger::Index blockNum, pieceNum, pieceNumHere;
61*3ac0a46fSAndroid Build Coastguard Worker 
62*3ac0a46fSAndroid Build Coastguard Worker 	// Convert
63*3ac0a46fSAndroid Build Coastguard Worker 	for (blockNum = 0, pieceNum = 0; blockNum < numBlocks; blockNum++) {
64*3ac0a46fSAndroid Build Coastguard Worker 		BigInteger::Blk curBlock = 0;
65*3ac0a46fSAndroid Build Coastguard Worker 		for (pieceNumHere = 0; pieceNumHere < piecesPerBlock && pieceNum < length;
66*3ac0a46fSAndroid Build Coastguard Worker 			pieceNumHere++, pieceNum++)
67*3ac0a46fSAndroid Build Coastguard Worker 			curBlock |= (BigInteger::Blk(data[pieceNum]) << (pieceSizeInBits * pieceNumHere));
68*3ac0a46fSAndroid Build Coastguard Worker 		blocks[blockNum] = curBlock;
69*3ac0a46fSAndroid Build Coastguard Worker 	}
70*3ac0a46fSAndroid Build Coastguard Worker 
71*3ac0a46fSAndroid Build Coastguard Worker 	// Create the BigInteger.
72*3ac0a46fSAndroid Build Coastguard Worker 	BigInteger x(blocks, numBlocks, sign);
73*3ac0a46fSAndroid Build Coastguard Worker 
74*3ac0a46fSAndroid Build Coastguard Worker 	delete [] blocks;
75*3ac0a46fSAndroid Build Coastguard Worker 	return x;
76*3ac0a46fSAndroid Build Coastguard Worker }
77*3ac0a46fSAndroid Build Coastguard Worker 
78*3ac0a46fSAndroid Build Coastguard Worker #endif
79