CatapultServer  v0.5.0.1 (Elephant)
PacketPayloadParser.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "catapult/utils/Logging.h"
23 #include "catapult/types.h"
24 #include <vector>
25 
26 namespace catapult { namespace ionet {
27 
28  namespace detail {
29  template<typename TEntity, typename TIsValidPredicate>
30  bool IsEntityValid(const TEntity& entity, TIsValidPredicate isValid) {
31  if (isValid(entity))
32  return true;
33 
35  << "entity (header size = " << sizeof(TEntity)
36  << ") has failed validity check with size " << entity.Size;
37  return false;
38  }
39  }
40 
43  template<typename TEntity, typename TIsValidPredicate>
44  std::vector<size_t> ExtractEntityOffsets(const RawBuffer& buffer, TIsValidPredicate isValid) {
45  using ResultType = std::vector<size_t>;
46  if (0 == buffer.Size)
47  return ResultType();
48 
49 #define CATAPULT_LOG_EXCEEDS_REMAINING_BYTES(SIZE, MESSAGE) \
50  CATAPULT_LOG(warning) << MESSAGE << " (" << SIZE << ") exceeds remaining bytes (" << remainingBytes << ")"
51 
52  std::vector<size_t> offsets;
53  auto remainingBytes = buffer.Size;
54  while (0 != remainingBytes) {
55  if (sizeof(TEntity) > remainingBytes) {
56  CATAPULT_LOG_EXCEEDS_REMAINING_BYTES(sizeof(TEntity), "entity header size");
57  return ResultType();
58  }
59 
60  auto offset = buffer.Size - remainingBytes;
61  const auto& entity = reinterpret_cast<const TEntity&>(*(buffer.pData + offset));
62  if (!detail::IsEntityValid(entity, isValid))
63  return ResultType();
64 
65  if (entity.Size > remainingBytes) {
66  CATAPULT_LOG_EXCEEDS_REMAINING_BYTES(entity.Size, "entity size");
67  return ResultType();
68  }
69 
70  offsets.push_back(offset);
71  remainingBytes -= entity.Size;
72  }
73 
74 #undef CATAPULT_LOG_EXCEEDS_REMAINING_BYTES
75 
76  return offsets;
77  }
78 
79 #define CATAPULT_LOG_DATA_SIZE_ERROR(SIZE, MESSAGE) \
80  CATAPULT_LOG(warning) << "buffer data size (" << buffer.Size << ") " << MESSAGE << " (" << SIZE << ")"
81 
84  template<typename TEntity, typename TIsValidPredicate>
85  bool ContainsSingleEntity(const RawBuffer& buffer, TIsValidPredicate isValid) {
86  if (0 == buffer.Size)
87  return false;
88 
89  if (buffer.Size < sizeof(TEntity)) {
90  CATAPULT_LOG_DATA_SIZE_ERROR(sizeof(TEntity), "must be at least entity size");
91  return false;
92  }
93 
94  const auto& entity = reinterpret_cast<const TEntity&>(*buffer.pData);
95  if (!detail::IsEntityValid(entity, isValid))
96  return false;
97 
98  if (entity.Size != buffer.Size) {
99  CATAPULT_LOG_DATA_SIZE_ERROR(entity.Size, "is inconsistent with entity size");
100  return false;
101  }
102 
103  return true;
104  }
105 
108  template<typename TStructure>
109  size_t CountFixedSizeStructures(const RawBuffer& buffer) {
110  if (0 == buffer.Size)
111  return 0;
112 
113  constexpr auto Structure_Size = sizeof(TStructure);
114  if (0 != buffer.Size % Structure_Size) {
115  CATAPULT_LOG_DATA_SIZE_ERROR(Structure_Size, "contains fractional structures of size");
116  return 0;
117  }
118 
119  return buffer.Size / Structure_Size;
120  }
121 
122 #undef CATAPULT_LOG_DATA_SIZE_ERROR
123 }}
CATAPULT_LOG
#define CATAPULT_LOG(SEV)
Writes a log entry to the default logger with SEV severity.
Definition: Logging.h:340
catapult::ionet::CountFixedSizeStructures
size_t CountFixedSizeStructures(const RawBuffer &buffer)
Definition: PacketPayloadParser.h:109
catapult::ionet::ContainsSingleEntity
bool ContainsSingleEntity(const RawBuffer &buffer, TIsValidPredicate isValid)
Definition: PacketPayloadParser.h:85
colorPrint.warning
def warning(*args)
Definition: colorPrint.py:10
CATAPULT_LOG_EXCEEDS_REMAINING_BYTES
#define CATAPULT_LOG_EXCEEDS_REMAINING_BYTES(SIZE, MESSAGE)
catapult::ionet::ExtractEntityOffsets
std::vector< size_t > ExtractEntityOffsets(const RawBuffer &buffer, TIsValidPredicate isValid)
Definition: PacketPayloadParser.h:44
types.h
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::ionet::detail::IsEntityValid
bool IsEntityValid(const TEntity &entity, TIsValidPredicate isValid)
Definition: PacketPayloadParser.h:30
Logging.h
CATAPULT_LOG_DATA_SIZE_ERROR
#define CATAPULT_LOG_DATA_SIZE_ERROR(SIZE, MESSAGE)
Definition: PacketPayloadParser.h:79
catapult::utils::RawBuffer
BasicRawBuffer< const uint8_t > RawBuffer
A const binary buffer.
Definition: RawBuffer.h:62