Botan 1.10.17
big_io.cpp
Go to the documentation of this file.
1/*
2* BigInt Input/Output
3* (C) 1999-2007 Jack Lloyd
4*
5* Distributed under the terms of the Botan license
6*/
7
8#include <botan/bigint.h>
9#include <iostream>
10
11namespace Botan {
12
13/*
14* Write the BigInt into a stream
15*/
16std::ostream& operator<<(std::ostream& stream, const BigInt& n)
17 {
19 if(stream.flags() & std::ios::hex)
21 else if(stream.flags() & std::ios::oct)
22 base = BigInt::Octal;
23
24 if(n == 0)
25 stream.write("0", 1);
26 else
27 {
28 if(n < 0)
29 stream.write("-", 1);
30 SecureVector<byte> buffer = BigInt::encode(n, base);
31 size_t skip = 0;
32 while(buffer[skip] == '0' && skip < buffer.size())
33 ++skip;
34 stream.write(reinterpret_cast<const char*>(&buffer[0]) + skip,
35 buffer.size() - skip);
36 }
37 if(!stream.good())
38 throw Stream_IO_Error("BigInt output operator has failed");
39 return stream;
40 }
41
42/*
43* Read the BigInt from a stream
44*/
45std::istream& operator>>(std::istream& stream, BigInt& n)
46 {
47 std::string str;
48 std::getline(stream, str);
49 if(stream.bad() || (stream.fail() && !stream.eof()))
50 throw Stream_IO_Error("BigInt input operator has failed");
51 n = BigInt(str);
52 return stream;
53 }
54
55}
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
Definition big_code.cpp:64
size_t size() const
Definition secmem.h:29
int operator>>(int fd, Pipe &pipe)
Definition fd_unix.cpp:39
int operator<<(int fd, Pipe &pipe)
Definition fd_unix.cpp:17
Stream_IO_Error(const std::string &err)
Definition exceptn.h:167