Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 {
19 size_t base = 10;
20 if(stream.flags() & std::ios::hex)
21 base = 16;
22 if(stream.flags() & std::ios::oct)
23 throw Invalid_Argument("Octal output of BigInt not supported");
24
25 if(n == 0)
26 stream.write("0", 1);
27 else
28 {
29 if(n < 0)
30 stream.write("-", 1);
31
32 std::string enc;
33
34 if(base == 10)
35 enc = n.to_dec_string();
36 else
37 enc = n.to_hex_string();
38
39 size_t skip = 0;
40 while(skip < enc.size() && enc[skip] == '0')
41 ++skip;
42 stream.write(&enc[skip], enc.size() - skip);
43 }
44 if(!stream.good())
45 throw Stream_IO_Error("BigInt output operator has failed");
46 return stream;
47 }
48
49 /*
50 * Read the BigInt from a stream
51 */
52 std::istream& operator>>(std::istream& stream, BigInt& n)
53 {
54 std::string str;
55 std::getline(stream, str);
56 if(stream.bad() || (stream.fail() && !stream.eof()))
57 throw Stream_IO_Error("BigInt input operator has failed");
58 n = BigInt(str);
59 return stream;
60 }
61
62 }
Botan::Invalid_Argument
Definition: exceptn.h:136
Botan::operator<<
int operator<<(int fd, Pipe &pipe)
Definition: fd_unix.cpp:17
Botan::BigInt
Definition: bigint.h:24
Botan::BigInt::to_hex_string
std::string to_hex_string() const
Definition: big_code.cpp:42
Botan::Stream_IO_Error
Definition: exceptn.h:306
Botan
Definition: alg_id.cpp:13
Botan::operator>>
int operator>>(int fd, Pipe &pipe)
Definition: fd_unix.cpp:40
Botan::BigInt::to_dec_string
std::string to_dec_string() const
Definition: big_code.cpp:15