-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitcoinStream.h
99 lines (85 loc) · 2.34 KB
/
BitcoinStream.h
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
#pragma once
#include <cstddef>
#include <cinttypes>
using namespace std;
struct BitcoinStream {
void* begin;
void* pointer;
size_t size;
BitcoinStream(void* _pointer, size_t _size) :
begin(_pointer),pointer(_pointer), size(_size) {
}
int64_t remaining() {
return (int64_t)size - (int64_t)(static_cast<char*>(pointer) - static_cast<char*>(begin) + 1);
}
inline void advance(size_t cnt) {
pointer = static_cast<char*>(pointer) + cnt;
}
inline uint8_t readU8()
{
uint8_t ret = *(uint8_t *)pointer;
advance(sizeof(uint8_t));
return ret;
}
// Read two bytes from the block-chain input stream.
inline uint16_t readU16()
{
uint16_t ret = *(uint16_t *)pointer;
advance(sizeof(uint16_t));
return ret;
}
// Read four bytes from the block-chain input stream.
inline uint32_t readU32()
{
uint32_t ret = *(uint32_t *)pointer;
advance(sizeof(uint32_t));
return ret;
}
// Read eight bytes from the block-chain input stream.
inline uint64_t readU64()
{
uint64_t ret = *(uint64_t *)pointer;
advance(sizeof(uint64_t));
return ret;
}
// Return the current stream pointer representing a 32byte hash and advance the read pointer accordingly
inline uint8_t *readHash()
{
uint8_t *ret = (uint8_t*)pointer;
advance(32);
return ret;
}
// reads a variable length integer.
// See the documentation from here: https://en.bitcoin.it/wiki/Protocol_specification#Variable_length_integer
inline uint32_t readVarint()
{
uint32_t ret = 0;
uint8_t v = readU8();
if ( v < 0xFD ) // If it's less than 0xFD use this value as the unsigned integer
{
ret = (uint32_t)v;
}
else
{
uint16_t v = readU16();
if ( v < 0xFFFF )
{
ret = (uint32_t)v;
}
else
{
uint32_t v = readU32();
if ( v < 0xFFFFFFFF )
{
ret = (uint32_t)v;
}
else
{
uint64_t v = readU64();
ret = (uint32_t)v;
}
}
}
return ret;
}
};