/usr/include/bitcoin/impl/serialize/misc.ipp is in libbitcoin-dev 2.0-2.4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | /*
* Copyright (c) 2011-2013 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* libbitcoin is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License with
* additional permissions to the one published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version. For more information see LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_IMPL_SATOSHI_SERIALIZE_IPP
#define LIBBITCOIN_IMPL_SATOSHI_SERIALIZE_IPP
#include <boost/optional.hpp>
namespace libbitcoin {
// message headers
template <typename Iterator>
Iterator satoshi_save(const header_type& head, Iterator result)
{
auto serial = make_serializer(result);
serial.write_4_bytes(head.magic);
serial.write_fixed_string(head.command, command_size);
serial.write_4_bytes(head.payload_length);
if (head.checksum != 0)
serial.write_4_bytes(head.checksum);
return serial.iterator();
}
template <typename Iterator>
void satoshi_load(const Iterator first, const Iterator last,
header_type& head)
{
auto deserial = make_deserializer(first, last);
head.magic = deserial.read_4_bytes();
head.command = deserial.read_fixed_string(command_size);
head.payload_length = deserial.read_4_bytes();
head.checksum = 0;
}
// version messages
template <typename Iterator>
Iterator satoshi_save(const version_type& packet, Iterator result)
{
auto serial = make_serializer(result);
serial.write_4_bytes(packet.version);
serial.write_8_bytes(packet.services);
serial.write_8_bytes(packet.timestamp);
serial.write_network_address(packet.address_me);
serial.write_network_address(packet.address_you);
serial.write_8_bytes(packet.nonce);
serial.write_string(packet.user_agent);
serial.write_4_bytes(packet.start_height);
return serial.iterator();
}
template <typename Iterator>
void satoshi_load(const Iterator first, const Iterator last,
version_type& packet)
{
auto deserial = make_deserializer(first, last);
packet.version = deserial.read_4_bytes();
packet.services = deserial.read_8_bytes();
packet.timestamp = deserial.read_8_bytes();
packet.address_me = deserial.read_network_address();
// Ignored field
packet.address_me.timestamp = 0;
if (packet.version < 106)
{
BITCOIN_ASSERT(std::distance(first, last) >= 46);
return;
}
packet.address_you = deserial.read_network_address();
// Ignored field
packet.address_you.timestamp = 0;
packet.nonce = deserial.read_8_bytes();
packet.user_agent = deserial.read_string();
if (packet.version < 209)
{
BITCOIN_ASSERT(std::distance(first, last) >= 46 + 26 + 8 + 1);
return;
}
packet.start_height = deserial.read_4_bytes();
BITCOIN_ASSERT(std::distance(first, last) >= 81 + 4);
}
// verack messages
template <typename Iterator>
Iterator satoshi_save(const verack_type& packet, Iterator result)
{
BITCOIN_ASSERT(satoshi_raw_size(packet) == 0);
return result;
}
template <typename Iterator>
void satoshi_load(const Iterator, const Iterator, verack_type& packet)
{
BITCOIN_ASSERT(satoshi_raw_size(packet) == 0);
}
// addr messages
template <typename Iterator>
Iterator satoshi_save(const address_type& packet, Iterator result)
{
auto serial = make_serializer(result);
serial.write_variable_uint(packet.addresses.size());
for (const network_address_type& net_address: packet.addresses)
{
serial.write_4_bytes(net_address.timestamp);
serial.write_network_address(net_address);
}
return serial.iterator();
}
template <typename Iterator>
void satoshi_load(const Iterator first, const Iterator last,
address_type& packet)
{
auto deserial = make_deserializer(first, last);
uint64_t count = deserial.read_variable_uint();
for (size_t i = 0; i < count; ++i)
{
uint32_t timestamp = deserial.read_4_bytes();
network_address_type addr = deserial.read_network_address();
addr.timestamp = timestamp;
packet.addresses.push_back(addr);
}
BITCOIN_ASSERT(deserial.iterator() == first + satoshi_raw_size(packet));
BITCOIN_ASSERT(deserial.iterator() == last);
}
// getaddr messages
template <typename Iterator>
Iterator satoshi_save(const get_address_type& packet, Iterator result)
{
BITCOIN_ASSERT(satoshi_raw_size(packet) == 0);
return result;
}
template <typename Iterator>
void satoshi_load(const Iterator, const Iterator, get_address_type& packet)
{
BITCOIN_ASSERT(satoshi_raw_size(packet) == 0);
}
// inventory related stuff
uint32_t inventory_type_to_number(inventory_type_id inv_type);
inventory_type_id inventory_type_from_number(uint32_t raw_type);
template <typename Message>
size_t raw_size_inventory_impl(const Message& packet)
{
return variable_uint_size(packet.inventories.size()) +
36 * packet.inventories.size();
}
template <typename Message, typename Iterator>
Iterator save_inventory_impl(const Message& packet, Iterator result)
{
auto serial = make_serializer(result);
serial.write_variable_uint(packet.inventories.size());
for (const inventory_vector_type inv: packet.inventories)
{
uint32_t raw_type = inventory_type_to_number(inv.type);
serial.write_4_bytes(raw_type);
serial.write_hash(inv.hash);
}
return serial.iterator();
}
template <typename Message, typename Iterator>
void load_inventory_impl(const Iterator first, const Iterator last,
Message& packet)
{
auto deserial = make_deserializer(first, last);
uint64_t count = deserial.read_variable_uint();
for (size_t i = 0; i < count; ++i)
{
inventory_vector_type inv;
uint32_t raw_type = deserial.read_4_bytes();
inv.type = inventory_type_from_number(raw_type);
inv.hash = deserial.read_hash();
packet.inventories.push_back(inv);
}
BITCOIN_ASSERT(deserial.iterator() == first + satoshi_raw_size(packet));
BITCOIN_ASSERT(deserial.iterator() == last);
}
// inv messages
template <typename Iterator>
Iterator satoshi_save(const inventory_type& packet, Iterator result)
{
return save_inventory_impl(packet, result);
}
template <typename Iterator>
void satoshi_load(const Iterator first, const Iterator last,
inventory_type& packet)
{
load_inventory_impl(first, last, packet);
}
// getdata messages
template <typename Iterator>
Iterator satoshi_save(const get_data_type& packet, Iterator result)
{
return save_inventory_impl(packet, result);
}
template <typename Iterator>
void satoshi_load(const Iterator first, const Iterator last,
get_data_type& packet)
{
load_inventory_impl(first, last, packet);
}
// getblocks messages
const std::string satoshi_command(const get_blocks_type&);
size_t satoshi_raw_size(const get_blocks_type& packet);
template <typename Iterator>
Iterator satoshi_save(const get_blocks_type& packet, Iterator result)
{
auto serial = make_serializer(result);
serial.write_4_bytes(protocol_version);
serial.write_variable_uint(packet.start_hashes.size());
for (hash_digest start_hash: packet.start_hashes)
serial.write_hash(start_hash);
serial.write_hash(packet.hash_stop);
return serial.iterator();
}
template <typename Iterator>
void satoshi_load(const Iterator first, const Iterator last,
get_blocks_type& packet)
{
auto deserial = make_deserializer(first, last);
// Discard protocol version because it is stupid
deserial.read_4_bytes();
uint32_t count = deserial.read_variable_uint();
for (size_t i = 0; i < count; ++i)
{
hash_digest start_hash = deserial.read_hash();
packet.start_hashes.push_back(start_hash);
}
packet.hash_stop = deserial.read_hash();
BITCOIN_ASSERT(deserial.iterator() == first + satoshi_raw_size(packet));
BITCOIN_ASSERT(deserial.iterator() == last);
}
// ping messages
const std::string satoshi_command(const ping_type&);
size_t satoshi_raw_size(const ping_type& packet);
template <typename Iterator>
Iterator satoshi_save(const ping_type& packet, Iterator result)
{
auto serial = make_serializer(result);
serial.write_8_bytes(packet.nonce);
return serial.iterator();
}
template <typename Iterator>
void satoshi_load(const Iterator first, const Iterator last,
ping_type& packet)
{
auto deserial = make_deserializer(first, last);
packet.nonce = deserial.read_8_bytes();
BITCOIN_ASSERT(deserial.iterator() == first + satoshi_raw_size(packet));
BITCOIN_ASSERT(deserial.iterator() == last);
}
// pong messages
const std::string satoshi_command(const pong_type&);
size_t satoshi_raw_size(const pong_type& packet);
template <typename Iterator>
Iterator satoshi_save(const pong_type& packet, Iterator result)
{
auto serial = make_serializer(result);
serial.write_8_bytes(packet.nonce);
return serial.iterator();
}
template <typename Iterator>
void satoshi_load(const Iterator first, const Iterator last,
pong_type& packet)
{
auto deserial = make_deserializer(first, last);
packet.nonce = deserial.read_8_bytes();
BITCOIN_ASSERT(deserial.iterator() == first + satoshi_raw_size(packet));
BITCOIN_ASSERT(deserial.iterator() == last);
}
} // libbitcoin
#endif
|