X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fserialize.h;h=8b75cf8b83d7d2f1f91f8fccf1aec7a02222740e;hb=d11488abd05cb39a9f481e7c4c35f780197a3d28;hp=98b69aa228a017f128c274e579edecebc071ae1d;hpb=26ce92b3526430d4a40b2faccef4facb966d6a0a;p=novacoin.git diff --git a/src/serialize.h b/src/serialize.h index 98b69aa..8b75cf8 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -1,7 +1,7 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2011 The Bitcoin developers +// Copyright (c) 2009-2012 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying -// file license.txt or http://www.opensource.org/licenses/mit-license.php. +// file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_SERIALIZE_H #define BITCOIN_SERIALIZE_H @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include @@ -23,6 +23,7 @@ typedef long long int64; typedef unsigned long long uint64; #ifdef WIN32 +#define _WIN32_WINNT 0x0501 #include // This is used to attempt to keep keying material out of swap // Note that VirtualLock does not provide this as a guarantee on Windows, @@ -89,6 +90,7 @@ enum const bool fRead = false; \ unsigned int nSerSize = 0; \ ser_streamplaceholder s; \ + assert(fGetSize||fWrite||fRead); /* suppress warning */ \ s.nType = nType; \ s.nVersion = nVersion; \ {statements} \ @@ -102,6 +104,7 @@ enum const bool fWrite = true; \ const bool fRead = false; \ unsigned int nSerSize = 0; \ + assert(fGetSize||fWrite||fRead); /* suppress warning */ \ {statements} \ } \ template \ @@ -112,6 +115,7 @@ enum const bool fWrite = false; \ const bool fRead = true; \ unsigned int nSerSize = 0; \ + assert(fGetSize||fWrite||fRead); /* suppress warning */ \ {statements} \ } @@ -261,11 +265,11 @@ uint64 ReadCompactSize(Stream& is) -// -// Wrapper for serializing arrays and POD -// There's a clever template way to make arrays serialize normally, but MSVC6 doesn't support it -// #define FLATDATA(obj) REF(CFlatData((char*)&(obj), (char*)&(obj) + sizeof(obj))) + +/** Wrapper for serializing arrays and POD. + * There's a clever template way to make arrays serialize normally, but MSVC6 doesn't support it. + */ class CFlatData { protected: @@ -298,9 +302,7 @@ public: -// -// string stored as a fixed length field -// +/** string stored as a fixed length field */ template class CFixedFieldString { @@ -819,16 +821,48 @@ struct secure_allocator : public std::allocator }; - // -// Double ended buffer combining vector and stream-like interfaces. -// >> and << read and write unformatted data using the above serialization templates. -// Fills with data in linear time; some stringstream implementations take N^2 time. +// Allocator that clears its contents before deletion. // +template +struct zero_after_free_allocator : public std::allocator +{ + // MSVC8 default copy constructor is broken + typedef std::allocator base; + typedef typename base::size_type size_type; + typedef typename base::difference_type difference_type; + typedef typename base::pointer pointer; + typedef typename base::const_pointer const_pointer; + typedef typename base::reference reference; + typedef typename base::const_reference const_reference; + typedef typename base::value_type value_type; + zero_after_free_allocator() throw() {} + zero_after_free_allocator(const zero_after_free_allocator& a) throw() : base(a) {} + template + zero_after_free_allocator(const zero_after_free_allocator& a) throw() : base(a) {} + ~zero_after_free_allocator() throw() {} + template struct rebind + { typedef zero_after_free_allocator<_Other> other; }; + + void deallocate(T* p, std::size_t n) + { + if (p != NULL) + memset(p, 0, sizeof(T) * n); + std::allocator::deallocate(p, n); + } +}; + + + +/** Double ended buffer combining vector and stream-like interfaces. + * + * >> and << read and write unformatted data using the above serialization templates. + * Fills with data in linear time; some stringstream implementations take N^2 time. + */ class CDataStream { protected: - typedef std::vector > vector_type; + typedef std::vector > vector_type; vector_type vch; unsigned int nReadPos; short state; @@ -1184,12 +1218,12 @@ int main(int argc, char *argv[]) -// -// Automatic closing wrapper for FILE* -// - Will automatically close the file when it goes out of scope if not null. -// - If you're returning the file pointer, return file.release(). -// - If you need to close the file early, use file.fclose() instead of fclose(file). -// +/** RAII wrapper for FILE*. + * + * Will automatically close the file when it goes out of scope if not null. + * If you're returning the file pointer, return file.release(). + * If you need to close the file early, use file.fclose() instead of fclose(file). + */ class CAutoFile { protected: @@ -1200,8 +1234,6 @@ public: int nType; int nVersion; - typedef FILE element_type; - CAutoFile(FILE* filenew=NULL, int nTypeIn=SER_DISK, int nVersionIn=PROTOCOL_VERSION) { file = filenew;