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