--- /dev/null
+#include "serialize.h"
+#include "hash.h"
+
+template<typename Stream>
+void WriteCompactSize(Stream& os, uint64_t nSize)
+{
+ if (nSize < 253)
+ {
+ unsigned char chSize = (unsigned char)nSize;
+ WRITEDATA(os, chSize);
+ }
+ else if (nSize <= std::numeric_limits<unsigned short>::max())
+ {
+ unsigned char chSize = 253;
+ unsigned short xSize = (unsigned short)nSize;
+ WRITEDATA(os, chSize);
+ WRITEDATA(os, xSize);
+ }
+ else if (nSize <= std::numeric_limits<unsigned int>::max())
+ {
+ unsigned char chSize = 254;
+ unsigned int xSize = (unsigned int)nSize;
+ WRITEDATA(os, chSize);
+ WRITEDATA(os, xSize);
+ }
+ else
+ {
+ unsigned char chSize = 255;
+ uint64_t xSize = nSize;
+ WRITEDATA(os, chSize);
+ WRITEDATA(os, xSize);
+ }
+ return;
+}
+
+
+template<typename Stream>
+uint64_t ReadCompactSize(Stream& is)
+{
+ unsigned char chSize;
+ READDATA(is, chSize);
+ uint64_t nSizeRet = 0;
+ if (chSize < 253)
+ {
+ nSizeRet = chSize;
+ }
+ else if (chSize == 253)
+ {
+ unsigned short xSize;
+ READDATA(is, xSize);
+ nSizeRet = xSize;
+ }
+ else if (chSize == 254)
+ {
+ unsigned int xSize;
+ READDATA(is, xSize);
+ nSizeRet = xSize;
+ }
+ else
+ {
+ uint64_t xSize;
+ READDATA(is, xSize);
+ nSizeRet = xSize;
+ }
+ if (nSizeRet > (uint64_t)MAX_SIZE)
+ throw std::ios_base::failure("ReadCompactSize() : size too large");
+ return nSizeRet;
+}
+
+
+
+// Template instantiation
+
+template void WriteCompactSize<CAutoFile>(CAutoFile&, uint64_t);
+template void WriteCompactSize<CDataStream>(CDataStream&, uint64_t);
+template void WriteCompactSize<CHashWriter>(CHashWriter&, uint64_t);
+
+template uint64_t ReadCompactSize<CAutoFile>(CAutoFile&);
+template uint64_t ReadCompactSize<CDataStream>(CDataStream&);
}
template<typename Stream>
-void WriteCompactSize(Stream& os, uint64_t nSize)
-{
- if (nSize < 253)
- {
- unsigned char chSize = (unsigned char)nSize;
- WRITEDATA(os, chSize);
- }
- else if (nSize <= std::numeric_limits<unsigned short>::max())
- {
- unsigned char chSize = 253;
- unsigned short xSize = (unsigned short)nSize;
- WRITEDATA(os, chSize);
- WRITEDATA(os, xSize);
- }
- else if (nSize <= std::numeric_limits<unsigned int>::max())
- {
- unsigned char chSize = 254;
- unsigned int xSize = (unsigned int)nSize;
- WRITEDATA(os, chSize);
- WRITEDATA(os, xSize);
- }
- else
- {
- unsigned char chSize = 255;
- uint64_t xSize = nSize;
- WRITEDATA(os, chSize);
- WRITEDATA(os, xSize);
- }
- return;
-}
+void WriteCompactSize(Stream& os, uint64_t nSize);
template<typename Stream>
-uint64_t ReadCompactSize(Stream& is)
-{
- unsigned char chSize;
- READDATA(is, chSize);
- uint64_t nSizeRet = 0;
- if (chSize < 253)
- {
- nSizeRet = chSize;
- }
- else if (chSize == 253)
- {
- unsigned short xSize;
- READDATA(is, xSize);
- nSizeRet = xSize;
- }
- else if (chSize == 254)
- {
- unsigned int xSize;
- READDATA(is, xSize);
- nSizeRet = xSize;
- }
- else
- {
- uint64_t xSize;
- READDATA(is, xSize);
- nSizeRet = xSize;
- }
- if (nSizeRet > (uint64_t)MAX_SIZE)
- throw std::ios_base::failure("ReadCompactSize() : size too large");
- return nSizeRet;
-}
+uint64_t ReadCompactSize(Stream& is);
// Variable-length integers: bytes are a MSB base-128 encoding of the number.
// The high bit in each byte signifies whether another digit follows. To make