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