blob: 999cdd260a4848a4b176bb4b12b3c9208b005b31 [file] [log] [blame]
Svet Ganove6986e12015-06-04 14:52:15 -07001// 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>
Philip P. Moltmannac3d58c2016-03-04 15:19:21 -080012#include <ostream>
Svet Ganove6986e12015-06-04 14:52:15 -070013
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.
19std::string bigUnsignedToString(const BigUnsigned &x);
20std::string bigIntegerToString(const BigInteger &x);
21BigUnsigned stringToBigUnsigned(const std::string &s);
22BigInteger stringToBigInteger(const std::string &s);
23
24// Creates a BigInteger from data such as `char's; read below for details.
25template <class T>
26BigInteger dataToBigInteger(const T* data, BigInteger::Index length, BigInteger::Sign sign);
27
28// Outputs x to os, obeying the flags `dec', `hex', `bin', and `showbase'.
29std::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).
33std::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 */
50template <class T>
51BigInteger 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