-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvarint_decoder.cpp
More file actions
52 lines (46 loc) · 1.49 KB
/
Copy pathvarint_decoder.cpp
File metadata and controls
52 lines (46 loc) · 1.49 KB
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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
class VarIntDecoder {
public:
/**
* Decodes a Bitcoin VarInt from a hex string.
* Logic:
* < 0xFD: 1 byte value
* 0xFD: Next 2 bytes (uint16_t)
* 0xFE: Next 4 bytes (uint32_t)
* 0xFF: Next 8 bytes (uint64_t)
*/
unsigned long decode(std::string hex, int& bytesRead) {
unsigned int firstByte = std::stoul(hex.substr(0, 2), nullptr, 16);
if (firstByte < 0xFD) {
bytesRead = 1;
return firstByte;
} else if (firstByte == 0xFD) {
bytesRead = 3;
return std::stoul(hex.substr(2, 4), nullptr, 16); // Simplified Little-Endian
} else if (firstByte == 0xFE) {
bytesRead = 5;
return std::stoul(hex.substr(2, 8), nullptr, 16);
} else {
bytesRead = 9;
return std::stoull(hex.substr(2, 16), nullptr, 16);
}
}
};
int main() {
VarIntDecoder decoder;
int bytesRead = 0;
// Example: 0xFD followed by 0x01FF (511 in decimal)
std::string testHex = "fdff01";
unsigned long value = decoder.decode(testHex, bytesRead);
std::cout << "--- Greyat Labs VarInt Decoder ---" << std::endl;
std::cout << "Raw Hex: " << testHex << std::endl;
if (bytesRead > 0) {
std::cout << "Decoded Value: " << value << std::endl;
std::cout << "Bytes Consumed: " << bytesRead << std::endl;
}
return 0;
}