Botan 3.9.0
Crypto and TLS for C&
big_io.cpp
Go to the documentation of this file.
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
12namespace Botan {
13
14/*
15* Write the BigInt into a stream
16*/
17std::ostream& operator<<(std::ostream& stream, const BigInt& n) {
18 const auto stream_flags = stream.flags();
19 if((stream_flags & std::ios::oct) != 0) {
20 throw Invalid_Argument("Octal output of BigInt not supported");
21 }
22
23 const size_t base = (stream_flags & std::ios::hex) != 0 ? 16 : 10;
24
25 if(base == 10) {
26 stream << n.to_dec_string();
27 } else {
28 stream << n.to_hex_string();
29 }
30
31 if(!stream.good()) {
32 throw Stream_IO_Error("BigInt output operator has failed");
33 }
34 return stream;
35}
36
37/*
38* Read the BigInt from a stream
39*/
40std::istream& operator>>(std::istream& stream, BigInt& n) {
41 std::string str;
42 std::getline(stream, str);
43 if(stream.bad() || (stream.fail() && !stream.eof())) {
44 throw Stream_IO_Error("BigInt input operator has failed");
45 }
46 n = BigInt(str);
47 return stream;
48}
49
50} // namespace Botan
std::string to_dec_string() const
Definition big_code.cpp:37
std::string to_hex_string() const
Definition big_code.cpp:104
std::ostream & operator<<(std::ostream &out, const OID &oid)
Definition asn1_obj.h:334
int operator>>(int fd, Pipe &pipe)
Definition fd_unix.cpp:39