From 13d08cbe978c4cd0de01aa0120c39470508cbbcb Mon Sep 17 00:00:00 2001 From: lloyd Date: Tue, 30 Sep 2008 22:41:49 +0000 Subject: 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 --- src/math/bigint/big_io.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/math/bigint/big_io.cpp (limited to 'src/math/bigint/big_io.cpp') 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 +#include + +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 buffer = BigInt::encode(n, base); + u32bit skip = 0; + while(buffer[skip] == '0' && skip < buffer.size()) + ++skip; + stream.write(reinterpret_cast(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; + } + +} -- cgit v1.2.3