diff options
author | lloyd <[email protected]> | 2008-09-30 22:41:49 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-30 22:41:49 +0000 |
commit | 13d08cbe978c4cd0de01aa0120c39470508cbbcb (patch) | |
tree | ff93739131cbca0dbdf23a31cd4b7611faf5aa6e /src/math/bigint/big_io.cpp | |
parent | 8854fe339f2e1f81091ba65c042824e8cc62cbbc (diff) |
Rearrange BigInt directories:
math/bigint - BigInt implementation
math/numbertheory - Math stuff built on top of BigInt
Coming soon: math/gfp (parts of pk/ecdsa)
Update deps in the pk files
Diffstat (limited to 'src/math/bigint/big_io.cpp')
-rw-r--r-- | src/math/bigint/big_io.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/math/bigint/big_io.cpp b/src/math/bigint/big_io.cpp new file mode 100644 index 000000000..3c201e8b2 --- /dev/null +++ b/src/math/bigint/big_io.cpp @@ -0,0 +1,53 @@ +/************************************************* +* BigInt Input/Output Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/bigint.h> +#include <iostream> + +namespace Botan { + +/************************************************* +* Write the BigInt into a stream * +*************************************************/ +std::ostream& operator<<(std::ostream& stream, const BigInt& n) + { + BigInt::Base base = BigInt::Decimal; + if(stream.flags() & std::ios::hex) + base = BigInt::Hexadecimal; + else if(stream.flags() & std::ios::oct) + base = BigInt::Octal; + + if(n == 0) + stream.write("0", 1); + else + { + if(n < 0) + stream.write("-", 1); + SecureVector<byte> buffer = BigInt::encode(n, base); + u32bit skip = 0; + while(buffer[skip] == '0' && skip < buffer.size()) + ++skip; + stream.write(reinterpret_cast<const char*>(buffer.begin()) + skip, + buffer.size() - skip); + } + if(!stream.good()) + throw Stream_IO_Error("BigInt output operator has failed"); + return stream; + } + +/************************************************* +* Read the BigInt from a stream * +*************************************************/ +std::istream& operator>>(std::istream& stream, BigInt& n) + { + std::string str; + std::getline(stream, str); + if(stream.bad() || (stream.fail() && !stream.eof())) + throw Stream_IO_Error("BigInt input operator has failed"); + n = BigInt(str); + return stream; + } + +} |