<commit_before>// Copyright (c) 2009-2010 <NAME>
// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_SERIALIZE_H
#define BITCOIN_SERIALIZE_H

#include <string>
#include <vector>
#include <map>
#include <set>
#include <cassert>
#include <limits>
#include <stdint.h>
#include <cstring>
#include <cstdio>

#include <boost/type_traits/is_fundamental.hpp>
#include <boost/tuple/tuple.hpp>

#include "allocators.h"
#include "ntp1/ntp1script.h"
#include "version.h"

class CAutoFile;
class CDataStream;
class CScript;

static const unsigned int MAX_SIZE = 0x02000000;

// Used to bypass the rule against non-const reference to temporary
// where it makes sense with wrappers such as CFlatData or CTxDB
template<typename T>
inline T& REF(const T& val)
{
    return const_cast<T&>(val);
}

/////////////////////////////////////////////////////////////////
//
// Templates for serializing to anything that looks like a stream,
// i.e. anything that supports .read(char*, int) and .write(char*, int)
//

enum
{
    // primary actions
    SER_NETWORK         = (1 << 0),
    SER_DISK            = (1 << 1),
    SER_GETHASH         = (1 << 2),

    // modifiers
    SER_SKIPSIG         = (1 << 16),
    SER_BLOCKHEADERONLY = (1 << 17),
};

#define IMPLEMENT_SERIALIZE(statements)    \
    unsigned int GetSerializeSize(int nType, int nVersion) const  \
    {                                           \
        CSerActionGetSerializeSize ser_action;  \
        const bool fGetSize = true;             \
        const bool fWrite = false;              \
        const bool fRead = false;               \
        unsigned int nSerSize = 0;              \
        ser_streamplaceholder s;                \
        assert(fGetSize||fWrite||fRead); /* suppress warning */ \
        s.nType = nType;                        \
        s.nVersion = nVersion;                  \
        {statements}                            \
        return nSerSize;                        \
    }                                           \
    template<typename Stream>                   \
    void Serialize(Stream& s, int nType, int nVersion) const  \
    {                                           \
        CSerActionSerialize ser_action;         \
        const bool fGetSize = false;            \
        const bool fWrite = true;               \
        const bool fRead = false;               \
        unsigned int nSerSize = 0;              \
        assert(fGetSize||fWrite||fRead); /* suppress warning */ \
        {statements}                            \
    }                                           \
    template<typename Stream>                   \
    void Unserialize(Stream& s, int nType, int nVersion)  \
    {                                           \
        CSerActionUnserialize ser_action;       \
        const bool fGetSize = false;            \
        const bool fWrite = false;              \
        const bool fRead = true;                \
        unsigned int nSerSize = 0;              \
        assert(fGetSize||fWrite||fRead); /* suppress warning */ \
        {statements}                            \
    }

#define READWRITE(obj)      (nSerSize += ::SerReadWrite(s, (obj), nType, nVersion, ser_action))






//
// Basic types
//
#define WRITEDATA(s, obj)   s.write((char*)&(obj), sizeof(obj))
#define READDATA(s, obj)    s.read((char*)&(obj), sizeof(obj))

inline unsigned int GetSerializeSize(char a,               int, int=0) { return sizeof(a); }
inline unsigned int GetSerializeSize(signed char a,        int, int=0) { return sizeof(a); }
inline unsigned int GetSerializeSize(unsigned char a,      int, int=0) { return sizeof(a); }
inline unsigned int GetSerializeSize(signed short a,       int, int=0) { return sizeof(a); }
inline unsigned int GetSerializeSize(unsigned short a,     int, int=0) { return sizeof(a); }
inline unsigned int GetSerializeSize(signed int a,         int, int=0) { return sizeof(a); }
inline unsigned int GetSerializeSize(unsigned int a,       int, int=0) { return sizeof(a); }
inline unsigned int GetSerializeSize(signed long /*a*/,        int, int=0) { return sizeof(int64_t); }
inline unsigned int GetSerializeSize(unsigned long /*a*/,      int, int=0) { return sizeof(uint64_t); }
inline unsigned int GetSerializeSize(signed long long a,   int, int=0) { return sizeof(a); }
inline unsigned int GetSerializeSize(unsigned long long a, int, int=0) { return sizeof(a); }
inline unsigned int GetSerializeSize(float a,              int, int=0) { return sizeof(a); }
inline unsigned int GetSerializeSize(double a,             int, int=0) { return sizeof(a); }

template<typename Stream> inline void Serialize(Stream& s, char a,               int, int=0) { WRITEDATA(s, a); }
template<typename Stream> inline void Serialize(Stream& s, signed char a,        int, int=0) { WRITEDATA(s, a); }
template<typename Stream> inline void Serialize(Stream& s, unsigned char a,      int, int=0) { WRITEDATA(s, a); }
template<typename Stream> inline void Serialize(Stream& s, signed short a,       int, int=0) { WRITEDATA(s, a); }
template<typename Stream> inline void Serialize(Stream& s, unsigned short a,     int, int=0) { WRITEDATA(s, a); }
template<typename Stream> inline void Serialize(Stream& s, signed int a,         int, int=0) { WRITEDATA(s, a); }
template<typename Stream> inline void Serialize(Stream& s, unsigned int a,       int, int=0) { WRITEDATA(s, a); }
template<typename Stream> inline void Serialize(Stream& s, signed long a,        int, int=0) { int64_t al =  static_cast<int64_t>(a);  WRITEDATA(s, al); }
template<typename Stream> inline void Serialize(Stream& s, unsigned long a,      int, int=0) { uint64_t al = static_cast<uint64_t>(a); WRITEDATA(s, al); }
template<typename Stream> inline void Serialize(Stream& s, signed long long a,   int, int=0) { WRITEDATA(s, a); }
template<typename Stream> inline void Serialize(Stream& s, unsigned long long a, int, int=0) { WRITEDATA(s, a); }
template<typename Stream> inline void Serialize(Stream& s, float a,              int, int=0) { WRITEDATA(s, a); }
template<typename Stream> inline void Serialize(Stream& s, double a,             int, int=0) { WRITEDATA(s, a); }

template<typename Stream> inline void Unserialize(Stream& s, char& a,               int, int=0) { READDATA(s, a); }
template<typename Stream> inline void Unserialize(Stream& s, signed char& a,        int, int=0) { READDATA(s, a); }
template<typename Stream> inline void Unserialize(Stream& s, unsigned char& a,      int, int=0) { READDATA(s, a); }
template<typename Stream> inline void Unserialize(Stream& s, signed short& a,       int, int=0) { READDATA(s, a); }
template<typename Stream> inline void Unserialize(Stream& s, unsigned short& a,     int, int=0) { READDATA(s, a); }
template<typename Stream> inline void Unserialize(Stream& s, signed int& a,         int, int=0) { READDATA(s, a); }
template<typename Stream> inline void Unserialize(Stream& s, unsigned int& a,       int, int=0) { READDATA(s, a); }
template<typename Stream> inline void Unserialize(Stream& s, signed long& a,        int, int=0) { int64_t al =  0;  READDATA(s, al); a = static_cast<int64_t>(al); }
template<typename Stream> inline void Unserialize(Stream& s, unsigned long& a,      int, int=0) { uint64_t al =  0; READDATA(s, al); a = static_cast<uint64_t>(al); }
template<typename Stream> inline void Unserialize(Stream& s, signed long long& a,   int, int=0) { READDATA(s, a); }
template<typename Stream> inline void Unserialize(Stream& s, unsigned long long& a, int, int=0) { READDATA(s, a); }
template<typename Stream> inline void Unserialize(Stream& s, float& a,              int, int=0) { READDATA(s, a); }
template<typename Stream> inline void Unserialize(Stream& s, double& a,             int, int=0) { READDATA(s, a); }

inline unsigned int GetSerializeSize(bool /*a*/, int, int=0)                          { return sizeof(char); }
template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0)    { char f=a; WRITEDATA(s, f); }
template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f; READDATA(s, f); a=f; }






//
// Compact size
//  size <  253        -- 1 byte
//  size <= USHRT_MAX  -- 3 bytes  (253 + 2 bytes)
//  size <= UINT_MAX   -- 5 bytes  (254 + 4 bytes)
//  size >  UINT_MAX   -- 9 bytes  (255 + 8 bytes)
//
inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
{
    if (nSize < 253)             return sizeof(unsigned char);
    else if (nSize <= std::numeric_limits<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
    else if (nSize <= std::numeric_limits<unsigned int>::max())  return sizeof(unsigned char) + sizeof(unsigned int);
    else                         return sizeof(unsigned char) + sizeof(uint64_t);
}

template<typename Stream>
void WriteCompactSize(Stream& os, uint64_t nSize)<commit_msg>Defined serialization targets for fundamental integer types<commit_after>// Copyright (c) 2009-2010 <NAME>
// Copyright (c) 2009-2012 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_SERIALIZE_H
#define BITCOIN_SERIALIZE_H

#include <string>
#include <vector>
#include <map>
#include <set>
#include <cassert>
#include <limits>
#include <stdint.h>
#include <cstring>
#include <cstdio>

#include <boost/type_traits/is_fundamental.hpp>
#include <boost/tuple/tuple.hpp>

#include "allocators.h"
#include "ntp1/ntp1script.h"
#include "version.h"

class CAutoFile;
class CDataStream;
class CScript;

static const unsigned int MAX_SIZE = 0x02000000;

// Used to bypass the rule against non-const reference to temporary
// where it makes sense with wrappers such as CFlatData or CTxDB
template<typename T>
inline T& REF(const T& val)
{
    return const_cast<T&>(val);
}

/////////////////////////////////////////////////////////////////
//
// Templates for serializing to anything that looks like a stream,
// i.e. anything that supports .read(char*, int) and .write(char*, int)
//

enum
{
    // primary actions
    SER_NETWORK         = (1 << 0),
    SER_DISK            = (1 << 1),
    SER_GETHASH         = (1 << 2),

    // modifiers
    SER_SKIPSIG         = (1 << 16),
    SER_BLOCKHEADERONLY = (1 << 17),
};

#define IMPLEMENT_SERIALIZE(statements)    \
    unsigned int GetSerializeSize(int nType, int nVersion) const  \
    {                                           \
        CSerActionGetSerializeSize ser_action;  \
        const bool fGetSize = true;             \
        const bool fWrite = false;              \
        const bool fRead = false;               \
        unsigned int nSerSize = 0;              \
        ser_streamplaceholder s;                \
        assert(fGetSize||fWrite||fRead); /* suppress warning */ \
        s.nType = nType;                        \
        s.nVersion = nVersion;                  \
        {statements}                            \
        return nSerSize;                        \
    }                                           \
    template<typename Stream>                   \
    void Serialize(Stream& s, int nType, int nVersion) const  \
    {                                           \
        CSerActionSerialize ser_action;         \
        const bool fGetSize = false;            \
        const bool fWrite = true;               \
        const bool fRead = false;               \
        unsigned int nSerSize = 0;              \
        assert(fGetSize||fWrite||fRead); /* suppress warning */ \
        {statements}                            \
    }                                           \
    template<typename Stream>                   \
    void Unserialize(Stream& s, int nType, int nVersion)  \
    {                                           \
        CSerActionUnserialize ser_action;       \
        const bool fGetSize = false;            \
        const bool fWrite = false;              \
        const bool fRead = true;                \
        unsigned int nSerSize = 0;              \
        assert(fGetSize||fWrite||fRead); /* suppress warning */ \
        {statements}                            \
    }

#define READWRITE(obj)      (nSerSize += ::SerReadWrite(s, (obj), nType, nVersion, ser_action))






//
// Basic types
//
#define WRITEDATA(s, obj)   s.write((char*)&(obj), sizeof(obj))
#define READDATA(s, obj)    s.read((char*)&(obj), sizeof(obj))

using SCharT     = int8_t;
using CharT      = char;
using UCharT     = uint8_t;
using ShortT     = int16_t;
using UShortT    = uint16_t;
using IntT       = int32_t;
using UIntT      = uint32_t;
using LongT      = int64_t;
using ULongT     = uint64_t;
using LongLongT  = int64_t;
using ULongLongT = uint64_t;

inline unsigned int GetSerializeSize(char /*a*/,               int, int=0) { return sizeof(CharT);      }
inline unsigned int GetSerializeSize(signed char /*a*/,        int, int=0) { return sizeof(SCharT);     }
inline unsigned int GetSerializeSize(unsigned char /*a*/,      int, int=0) { return sizeof(UCharT);     }
inline unsigned int GetSerializeSize(signed short /*a*/,       int, int=0) { return sizeof(ShortT);     }
inline unsigned int GetSerializeSize(unsigned short /*a*/,     int, int=0) { return sizeof(UShortT);    }
inline unsigned int GetSerializeSize(signed int /*a*/,         int, int=0) { return sizeof(IntT);       }
inline unsigned int GetSerializeSize(unsigned int /*a*/,       int, int=0) { return sizeof(UIntT);      }
inline unsigned int GetSerializeSize(signed long /*a*/,        int, int=0) { return sizeof(LongT);      }
inline unsigned int GetSerializeSize(unsigned long /*a*/,      int, int=0) { return sizeof(ULongT);     }
inline unsigned int GetSerializeSize(signed long long /*a*/,   int, int=0) { return sizeof(LongLongT);  }
inline unsigned int GetSerializeSize(unsigned long long /*a*/, int, int=0) { return sizeof(ULongLongT); }
inline unsigned int GetSerializeSize(float a,                  int, int=0) { return sizeof(a);          }
inline unsigned int GetSerializeSize(double a,                 int, int=0) { return sizeof(a);          }

template<typename Stream> inline void Serialize(Stream& s, char a,               int, int=0) { CharT al      = static_cast<CharT>(a);      WRITEDATA(s, al); }
template<typename Stream> inline void Serialize(Stream& s, signed char a,        int, int=0) { SCharT al     = static_cast<SCharT>(a);     WRITEDATA(s, al); }
template<typename Stream> inline void Serialize(Stream& s, unsigned char a,      int, int=0) { UCharT al     = static_cast<UCharT>(a);     WRITEDATA(s, al); }
template<typename Stream> inline void Serialize(Stream& s, signed short a,       int, int=0) { ShortT al     = static_cast<ShortT>(a);     WRITEDATA(s, al); }
template<typename Stream> inline void Serialize(Stream& s, unsigned short a,     int, int=0) { UShortT al    = static_cast<UShortT>(a);    WRITEDATA(s, al); }
template<typename Stream> inline void Serialize(Stream& s, signed int a,         int, int=0) { IntT al       = static_cast<IntT>(a);       WRITEDATA(s, al); }
template<typename Stream> inline void Serialize(Stream& s, unsigned int a,       int, int=0) { UIntT al      = static_cast<UIntT>(a);      WRITEDATA(s, al); }
template<typename Stream> inline void Serialize(Stream& s, signed long a,        int, int=0) { LongT al      = static_cast<LongT>(a);      WRITEDATA(s, al); }
template<typename Stream> inline void Serialize(Stream& s, unsigned long a,      int, int=0) { ULongT al     = static_cast<ULongT>(a);     WRITEDATA(s, al); }
template<typename Stream> inline void Serialize(Stream& s, signed long long a,   int, int=0) { LongLongT al  = static_cast<LongLongT>(a);  WRITEDATA(s, al); }
template<typename Stream> inline void Serialize(Stream& s, unsigned long long a, int, int=0) { ULongLongT al = static_cast<ULongLongT>(a); WRITEDATA(s, al); }
template<typename Stream> inline void Serialize(Stream& s, float a,              int, int=0) { WRITEDATA(s, a); }
template<typename Stream> inline void Serialize(Stream& s, double a,             int, int=0) { WRITEDATA(s, a); }

template<typename Stream> inline void Unserialize(Stream& s, char& a,               int, int=0) { CharT al = 0;      READDATA(s, al); a = static_cast<CharT>(al);      }
template<typename Stream> inline void Unserialize(Stream& s, signed char& a,        int, int=0) { SCharT al = 0;     READDATA(s, al); a = static_cast<SCharT>(al);     }
template<typename Stream> inline void Unserialize(Stream& s, unsigned char& a,      int, int=0) { UCharT al = 0;     READDATA(s, al); a = static_cast<UCharT>(al);     }
template<typename Stream> inline void Unserialize(Stream& s, signed short& a,       int, int=0) { ShortT al = 0;     READDATA(s, al); a = static_cast<ShortT>(al);     }
template<typename Stream> inline void Unserialize(Stream& s, unsigned short& a,     int, int=0) { UShortT al = 0;    READDATA(s, al); a = static_cast<UShortT>(al);    }
template<typename Stream> inline void Unserialize(Stream& s, signed int& a,         int, int=0) { IntT al = 0;       READDATA(s, al); a = static_cast<IntT>(al);       }
template<typename Stream> inline void Unserialize(Stream& s, unsigned int& a,       int, int=0) { UIntT al = 0;      READDATA(s, al); a = static_cast<UIntT>(al);      }
template<typename Stream> inline void Unserialize(Stream& s, signed long& a,        int, int=0) { LongT al = 0;      READDATA(s, al); a = static_cast<LongT>(al);      }
template<typename Stream> inline void Unserialize(Stream& s, unsigned long& a,      int, int=0) { ULongT al = 0;     READDATA(s, al); a = static_cast<ULongT>(al);     }
template<typename Stream> inline void Unserialize(Stream& s, signed long long& a,   int, int=0) { LongLongT al = 0;  READDATA(s, al); a = static_cast<LongLongT>(al);  }
template<typename Stream> inline void Unserialize(Stream& s, unsigned long long& a, int, int=0) { ULongLongT al = 0; READDATA(s, al); a = static_cast<ULongLongT>(al); }
template<typename Stream> inline void Unserialize(Stream& s, float& a,              int, int=0) { READDATA(s, a); }
template<typename Stream> inline void Unserialize(Stream& s, double& a,             int, int=0) { READDATA(s, a); }

inline unsigned int GetSerializeSize(bool /*a*/, int, int=0)                          { return sizeof(char); }
template<typename Stream> inline void Serialize(Stream& s, bool a, int, int=0)    { char f=a; WRITEDATA(s, f); }
template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0) { char f; READDATA(s, f); a=f; }






//
// Compact size
//  size <  253        -- 1 byte
//  size <= USHRT_MAX  -- 3 bytes  (253 + 2 bytes)
//  size <= UINT_MAX   -- 5 bytes  (254 + 4 bytes)
//  size >  UINT_MAX   -- 9 bytes  (255 + 8 bytes)
//
inline unsigned int GetSizeOfCompactSize(uint64_t nSize)
{
    if (nSize < 253)             return sizeof(unsigned char);
    else if (nSize <= std::numeric_limits<unsigned short>::max()) return sizeof(unsigned char) + sizeof(unsigned short);
    else if (nSize <= std::numeric_limits<unsigned int>::max())  return sizeof(unsigned char) + sizeof(unsigned int);
    else                         return sizeof(unsigned char) + sizeof(uint64_t);
}

template<typename Stream>
void WriteCompactSize(Stream& os, uint64_t nSize)
{'fraction_non_alphanumeric': 0.12656925293270221, 'fraction_numerical': 0.014354805515538176, 'mean_word_length': 2.500900576368876, 'pattern_counts': {'":': 0, '<': 155, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4967503', 'n_tokens_mistral': 6040, 'n_tokens_neox': 5863, 'n_words': 1804}