Botan
3.4.0
Crypto and TLS for C&
src
lib
math
bigint
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
12
namespace
Botan
{
13
14
/*
15
* Write the BigInt into a stream
16
*/
17
std::ostream&
operator<<
(std::ostream& stream,
const
BigInt
& n) {
18
const
auto
stream_flags = stream.flags();
19
if
(stream_flags & std::ios::oct) {
20
throw
Invalid_Argument
(
"Octal output of BigInt not supported"
);
21
}
22
23
const
size_t
base = (stream_flags & std::ios::hex) ? 16 : 10;
24
25
std::string enc;
26
27
if
(base == 10) {
28
enc = n.
to_dec_string
();
29
}
else
{
30
enc = n.
to_hex_string
();
31
}
32
33
stream.write(enc.data(), enc.size());
34
35
if
(!stream.good()) {
36
throw
Stream_IO_Error
(
"BigInt output operator has failed"
);
37
}
38
return
stream;
39
}
40
41
/*
42
* Read the BigInt from a stream
43
*/
44
std::istream&
operator>>
(std::istream& stream,
BigInt
& n) {
45
std::string str;
46
std::getline(stream, str);
47
if
(stream.bad() || (stream.fail() && !stream.eof())) {
48
throw
Stream_IO_Error
(
"BigInt input operator has failed"
);
49
}
50
n =
BigInt
(str);
51
return
stream;
52
}
53
54
}
// namespace Botan
Botan::BigInt
Definition
bigint.h:26
Botan::BigInt::to_dec_string
std::string to_dec_string() const
Definition
big_code.cpp:15
Botan::BigInt::to_hex_string
std::string to_hex_string() const
Definition
big_code.cpp:85
Botan::Invalid_Argument
Definition
exceptn.h:131
Botan::Stream_IO_Error
Definition
exceptn.h:288
Botan
Definition
alg_id.cpp:13
Botan::operator<<
std::ostream & operator<<(std::ostream &out, const OID &oid)
Definition
asn1_oid.cpp:140
Botan::operator>>
int operator>>(int fd, Pipe &pipe)
Definition
fd_unix.cpp:39
Generated by
1.10.0