/src/Botan-3.4.0/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 | 0 | std::ostream& operator<<(std::ostream& stream, const BigInt& n) { |
18 | 0 | const auto stream_flags = stream.flags(); |
19 | 0 | if(stream_flags & std::ios::oct) { |
20 | 0 | throw Invalid_Argument("Octal output of BigInt not supported"); |
21 | 0 | } |
22 | | |
23 | 0 | const size_t base = (stream_flags & std::ios::hex) ? 16 : 10; |
24 | |
|
25 | 0 | std::string enc; |
26 | |
|
27 | 0 | if(base == 10) { |
28 | 0 | enc = n.to_dec_string(); |
29 | 0 | } else { |
30 | 0 | enc = n.to_hex_string(); |
31 | 0 | } |
32 | |
|
33 | 0 | stream.write(enc.data(), enc.size()); |
34 | |
|
35 | 0 | if(!stream.good()) { |
36 | 0 | throw Stream_IO_Error("BigInt output operator has failed"); |
37 | 0 | } |
38 | 0 | return stream; |
39 | 0 | } |
40 | | |
41 | | /* |
42 | | * Read the BigInt from a stream |
43 | | */ |
44 | 0 | std::istream& operator>>(std::istream& stream, BigInt& n) { |
45 | 0 | std::string str; |
46 | 0 | std::getline(stream, str); |
47 | 0 | if(stream.bad() || (stream.fail() && !stream.eof())) { |
48 | 0 | throw Stream_IO_Error("BigInt input operator has failed"); |
49 | 0 | } |
50 | 0 | n = BigInt(str); |
51 | 0 | return stream; |
52 | 0 | } |
53 | | |
54 | | } // namespace Botan |