Botan
1.10.17
src
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
* Distributed under the terms of the Botan license
6
*/
7
8
#include <botan/bigint.h>
9
#include <iostream>
10
11
namespace
Botan
{
12
13
/*
14
* Write the BigInt into a stream
15
*/
16
std::ostream&
operator<<
(std::ostream& stream,
const
BigInt
& n)
17
{
18
BigInt::Base
base =
BigInt::Decimal
;
19
if
(stream.flags() & std::ios::hex)
20
base =
BigInt::Hexadecimal
;
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
*/
45
std::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
}
Botan::BigInt
Definition
bigint.h:23
Botan::BigInt::Base
Base
Definition
bigint.h:28
Botan::BigInt::Octal
@ Octal
Definition
bigint.h:28
Botan::BigInt::Decimal
@ Decimal
Definition
bigint.h:28
Botan::BigInt::Hexadecimal
@ Hexadecimal
Definition
bigint.h:28
Botan::BigInt::BigInt
BigInt()
Definition
bigint.h:446
Botan::BigInt::encode
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
Definition
big_code.cpp:64
Botan::MemoryRegion::size
size_t size() const
Definition
secmem.h:29
Botan::SecureVector
Definition
secmem.h:329
Botan
Definition
algo_base.h:14
Botan::operator>>
int operator>>(int fd, Pipe &pipe)
Definition
fd_unix.cpp:39
Botan::operator<<
int operator<<(int fd, Pipe &pipe)
Definition
fd_unix.cpp:17
Botan::Stream_IO_Error::Stream_IO_Error
Stream_IO_Error(const std::string &err)
Definition
exceptn.h:167
Generated by
1.18.0