qca
big_io.cpp
Go to the documentation of this file.00001 /* 00002 Copyright (C) 1999-2007 The Botan Project. All rights reserved. 00003 00004 Redistribution and use in source and binary forms, for any use, with or without 00005 modification, is permitted provided that the following conditions are met: 00006 00007 1. Redistributions of source code must retain the above copyright notice, this 00008 list of conditions, and the following disclaimer. 00009 00010 2. Redistributions in binary form must reproduce the above copyright notice, 00011 this list of conditions, and the following disclaimer in the documentation 00012 and/or other materials provided with the distribution. 00013 00014 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED 00015 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00016 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. 00017 00018 IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT, 00019 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00020 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00021 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00022 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00023 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 00024 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00025 */ 00026 // LICENSEHEADER_END 00027 namespace QCA { // WRAPNS_LINE 00028 /************************************************* 00029 * BigInt Input/Output Source File * 00030 * (C) 1999-2007 The Botan Project * 00031 *************************************************/ 00032 00033 } // WRAPNS_LINE 00034 #include <botan/bigint.h> 00035 namespace QCA { // WRAPNS_LINE 00036 } // WRAPNS_LINE 00037 #include <iostream> 00038 namespace QCA { // WRAPNS_LINE 00039 00040 namespace Botan { 00041 00042 #ifndef BOTAN_MINIMAL_BIGINT 00043 00044 /************************************************* 00045 * Write the BigInt into a stream * 00046 *************************************************/ 00047 std::ostream& operator<<(std::ostream& stream, const BigInt& n) 00048 { 00049 BigInt::Base base = BigInt::Decimal; 00050 if(stream.flags() & std::ios::hex) 00051 base = BigInt::Hexadecimal; 00052 else if(stream.flags() & std::ios::oct) 00053 base = BigInt::Octal; 00054 00055 if(n == 0) 00056 stream.write("0", 1); 00057 else 00058 { 00059 if(n < 0) 00060 stream.write("-", 1); 00061 SecureVector<byte> buffer = BigInt::encode(n, base); 00062 u32bit skip = 0; 00063 while(buffer[skip] == '0' && skip < buffer.size()) 00064 ++skip; 00065 stream.write((const char*)buffer.begin() + skip, buffer.size() - skip); 00066 } 00067 if(!stream.good()) 00068 throw Stream_IO_Error("BigInt output operator has failed"); 00069 return stream; 00070 } 00071 00072 /************************************************* 00073 * Read the BigInt from a stream * 00074 *************************************************/ 00075 std::istream& operator>>(std::istream& stream, BigInt& n) 00076 { 00077 std::string str; 00078 std::getline(stream, str); 00079 if(stream.bad() || (stream.fail() && !stream.eof())) 00080 throw Stream_IO_Error("BigInt input operator has failed"); 00081 n = BigInt(str); 00082 return stream; 00083 } 00084 00085 #endif 00086 00087 } 00088 } // WRAPNS_LINE
KDE 4.4 API Reference