File indexing completed on 2024-05-12 04:45:13

0001 /*
0002 Copyright (C) 1999-2007 The Botan Project. All rights reserved.
0003 
0004 Redistribution and use in source and binary forms, for any use, with or without
0005 modification, is permitted provided that the following conditions are met:
0006 
0007 1. Redistributions of source code must retain the above copyright notice, this
0008 list of conditions, and the following disclaimer.
0009 
0010 2. Redistributions in binary form must reproduce the above copyright notice,
0011 this list of conditions, and the following disclaimer in the documentation
0012 and/or other materials provided with the distribution.
0013 
0014 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED
0015 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0016 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
0017 
0018 IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT,
0019 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
0020 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0022 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
0023 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
0024 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025 */
0026 // LICENSEHEADER_END
0027 namespace QCA { // WRAPNS_LINE
0028 /*************************************************
0029  * BigInt Input/Output Source File                *
0030  * (C) 1999-2007 The Botan Project                *
0031  *************************************************/
0032 
0033 } // WRAPNS_LINE
0034 #include <botan/bigint.h>
0035 namespace QCA { // WRAPNS_LINE
0036 } // WRAPNS_LINE
0037 #include <iostream>
0038 namespace QCA { // WRAPNS_LINE
0039 
0040 namespace Botan {
0041 
0042 #ifndef BOTAN_MINIMAL_BIGINT
0043 
0044 /*************************************************
0045  * Write the BigInt into a stream                 *
0046  *************************************************/
0047 std::ostream &operator<<(std::ostream &stream, const BigInt &n)
0048 {
0049     BigInt::Base base = BigInt::Decimal;
0050     if (stream.flags() & std::ios::hex)
0051         base = BigInt::Hexadecimal;
0052     else if (stream.flags() & std::ios::oct)
0053         base = BigInt::Octal;
0054 
0055     if (n == 0)
0056         stream.write("0", 1);
0057     else {
0058         if (n < 0)
0059             stream.write("-", 1);
0060         SecureVector<byte> buffer = BigInt::encode(n, base);
0061         u32bit             skip   = 0;
0062         while (skip < buffer.size() && buffer[skip] == '0')
0063             ++skip;
0064         stream.write((const char *)buffer.begin() + skip, buffer.size() - skip);
0065     }
0066     if (!stream.good())
0067         throw Stream_IO_Error("BigInt output operator has failed");
0068     return stream;
0069 }
0070 
0071 /*************************************************
0072  * Read the BigInt from a stream                  *
0073  *************************************************/
0074 std::istream &operator>>(std::istream &stream, BigInt &n)
0075 {
0076     std::string str;
0077     std::getline(stream, str);
0078     if (stream.bad() || (stream.fail() && !stream.eof()))
0079         throw Stream_IO_Error("BigInt input operator has failed");
0080     n = BigInt(str);
0081     return stream;
0082 }
0083 
0084 #endif
0085 
0086 }
0087 } // WRAPNS_LINE