decoder.cpp
1#include "itch/feed/itch/decoder.hpp"2#include "itch/common/binary.hpp"34#include <cstddef>5#include <stdexcept>6#include <string>78namespace itch {9namespace {1011std::string symbol(const Bytes& bytes, size_t offset) {12 std::string value(bytes.begin() + static_cast<std::ptrdiff_t>(offset),13 bytes.begin() + static_cast<std::ptrdiff_t>(offset + 8));14 while (!value.empty() && value.back() == ' ') {15 value.pop_back();16 }17 return value;18}1920} // namespace2122size_t expected_length(char type) {23 switch (type) {24 case 'R':25 return 39;26 case 'A':27 return 36;28 case 'F':29 return 40;30 case 'E':31 return 31;32 case 'C':33 return 36;34 case 'X':35 return 23;36 case 'D':37 return 19;38 case 'U':39 return 35;40 // Recognized ITCH 5.0 messages that do not change the displayed book.41 case 'S':42 return 12;43 case 'H':44 return 25;45 case 'Y':46 return 20;47 case 'L':48 return 26;49 case 'V':50 return 35;51 case 'W':52 return 12;53 case 'K':54 return 28;55 case 'J':56 return 35;57 case 'h':58 return 21;59 case 'P':60 return 44;61 case 'Q':62 return 40;63 case 'B':64 return 19;65 case 'I':66 return 50;67 case 'N':68 return 20;69 case 'O':70 return 48;71 default:72 return 0;73 }74}7576std::optional<Event> decode(const Bytes& bytes, uint64_t frame) {77 if (bytes.empty()) {78 throw std::runtime_error("frame " + std::to_string(frame) + ": empty ITCH payload");79 }80 const char type = static_cast<char>(bytes[0]);81 const size_t length = expected_length(type);82 if (!length) {83 throw std::runtime_error("frame " + std::to_string(frame) + ": unknown ITCH type");84 }85 if (bytes.size() != length) {86 throw std::runtime_error("frame " + std::to_string(frame) + ": invalid " + type +87 " length");88 }8990 // Length validation above makes every field read below safe.91 if (type != 'R' && type != 'A' && type != 'F' && type != 'E' && type != 'C' && type != 'X' &&92 type != 'D' && type != 'U') {93 return std::nullopt;94 }9596 const uint16_t locate = static_cast<uint16_t>(read_big_endian(bytes.data() + 1, 2));97 const uint64_t timestamp = read_big_endian(bytes.data() + 5, 6);98 EventData data;99 if (type == 'R') {100 data = Directory{symbol(bytes, 11)};101 } else if (type == 'A' || type == 'F') {102 data = Add{symbol(bytes, 24), read_big_endian(bytes.data() + 11, 8),103 static_cast<char>(bytes[19]),104 static_cast<uint32_t>(read_big_endian(bytes.data() + 20, 4)),105 static_cast<uint32_t>(read_big_endian(bytes.data() + 32, 4))};106 } else if (type == 'E' || type == 'C' || type == 'X') {107 data = Reduce{read_big_endian(bytes.data() + 11, 8),108 static_cast<uint32_t>(read_big_endian(bytes.data() + 19, 4))};109 } else if (type == 'D') {110 data = Delete{read_big_endian(bytes.data() + 11, 8)};111 } else {112 data = Replace{read_big_endian(bytes.data() + 11, 8), read_big_endian(bytes.data() + 19, 8),113 static_cast<uint32_t>(read_big_endian(bytes.data() + 27, 4)),114 static_cast<uint32_t>(read_big_endian(bytes.data() + 31, 4))};115 }116 return Event{frame, timestamp, type, locate, std::move(data)};117}118119} // namespace itch