Coverage Report

Created: 2020-02-14 15:38

/src/botan/src/lib/math/bigint/big_io.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* BigInt Input/Output
3
* (C) 1999-2007 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/bigint.h>
9
#include <istream>
10
#include <ostream>
11
12
namespace Botan {
13
14
/*
15
* Write the BigInt into a stream
16
*/
17
std::ostream& operator<<(std::ostream& stream, const BigInt& n)
18
0
   {
19
0
   size_t base = 10;
20
0
   if(stream.flags() & std::ios::hex)
21
0
      base = 16;
22
0
   if(stream.flags() & std::ios::oct)
23
0
      throw Invalid_Argument("Octal output of BigInt not supported");
24
0
25
0
   if(n == 0)
26
0
      stream.write("0", 1);
27
0
   else
28
0
      {
29
0
      if(n < 0)
30
0
         stream.write("-", 1);
31
0
32
0
      std::string enc;
33
0
34
0
      if(base == 10)
35
0
         enc = n.to_dec_string();
36
0
      else
37
0
         enc = n.to_hex_string();
38
0
39
0
      size_t skip = 0;
40
0
      while(skip < enc.size() && enc[skip] == '0')
41
0
         ++skip;
42
0
      stream.write(&enc[skip], enc.size() - skip);
43
0
      }
44
0
   if(!stream.good())
45
0
      throw Stream_IO_Error("BigInt output operator has failed");
46
0
   return stream;
47
0
   }
48
49
/*
50
* Read the BigInt from a stream
51
*/
52
std::istream& operator>>(std::istream& stream, BigInt& n)
53
0
   {
54
0
   std::string str;
55
0
   std::getline(stream, str);
56
0
   if(stream.bad() || (stream.fail() && !stream.eof()))
57
0
      throw Stream_IO_Error("BigInt input operator has failed");
58
0
   n = BigInt(str);
59
0
   return stream;
60
0
   }
61
62
}