jaffarCommon
Loading...
Searching...
No Matches
deserializers/differential.hpp
Go to the documentation of this file.
1#pragma once
2
8#include "../exceptions.hpp"
9#include "base.hpp"
10#include <xdelta3/xdelta3.h>
11
12namespace jaffarCommon
13{
14
15namespace deserializer
16{
17
23class Differential final : public deserializer::Base
24{
25public:
35 Differential(const void* __restrict inputDataBuffer = nullptr, const size_t inputDataBufferSize = std::numeric_limits<uint32_t>::max(),
36 const void* __restrict referenceDataBuffer = nullptr, const size_t referenceDataBufferSize = std::numeric_limits<uint32_t>::max(), const bool useZlib = false)
37 : deserializer::Base(inputDataBuffer, inputDataBufferSize)
38 , _referenceDataBuffer((const uint8_t*)referenceDataBuffer)
39 , _referenceDataBufferSize(referenceDataBufferSize)
40 , _useZlib(useZlib)
41 {
42 }
43
44 ~Differential() = default;
45
46 __JAFFAR_COMMON_INLINE__ void popContiguous(void* const __restrict outputDataBuffer, const size_t outputDataSize) override
47 {
48 // Making sure we do not exceed the maximum size estipulated
49 if (_inputDataBufferPos + outputDataSize > _inputDataBufferSize)
50 JAFFAR_THROW_RUNTIME("Maximum input data position reached before contiguous deserialization of (%lu + %lu > %lu) bytes", _inputDataBufferPos, outputDataSize,
52 if (_referenceDataBufferPos + outputDataSize > _referenceDataBufferSize)
53 JAFFAR_THROW_RUNTIME("[Error] Maximum reference data position to be exceeded on contiguous deserialization (%lu + %lu > %lu)", _referenceDataBufferPos, outputDataSize,
54 _referenceDataBufferSize);
55
56 // Only perform memcpy if the input block is not null
57 if (_inputDataBuffer != nullptr && outputDataBuffer != nullptr) memcpy(outputDataBuffer, &_inputDataBuffer[_inputDataBufferPos], outputDataSize);
58
59 // Moving input data pointer position
60 _inputDataBufferPos += outputDataSize;
61
62 // Moving reference data buffer position
63 _referenceDataBufferPos += outputDataSize;
64 }
65
66 __JAFFAR_COMMON_INLINE__ void pop(void* const __restrict outputDataBuffer, const size_t outputDataSize) override
67 {
68 if (outputDataBuffer == nullptr || _inputDataBuffer == nullptr) return;
69
70 // Size of differential buffer size
71 const size_t differentialBufferSize = sizeof(usize_t);
72
73 // If we reached maximum output, stop here -- this must be checked BEFORE reading diffCount below,
74 // otherwise we read out of bounds when positioned at the end of the input buffer
75 if (_inputDataBufferPos + differentialBufferSize >= _inputDataBufferSize)
76 JAFFAR_THROW_RUNTIME("[Error] Maximum input data position reached before differential buffer size decode (%lu + %lu > %lu)", _inputDataBufferPos, differentialBufferSize,
78
79 // Reading differential outputDataBufferSize
80 usize_t diffCount = *(usize_t*)&_inputDataBuffer[_inputDataBufferPos];
81
82 // Advancing position pointer to store the difference outputDataBufferSizeer
83 _inputDataBufferPos += differentialBufferSize;
84
85 // If we reached maximum output, stop here
86 if (_referenceDataBufferPos + outputDataSize > _referenceDataBufferSize)
87 JAFFAR_THROW_RUNTIME("[Error] Maximum reference data position exceeded before differential decode (%lu + %lu > %lu)", _referenceDataBufferPos, outputDataSize,
88 _referenceDataBufferSize);
89
90 // Encoding differential
91 usize_t output_size;
92 int ret = xd3_decode_memory(&_inputDataBuffer[_inputDataBufferPos], diffCount, &_referenceDataBuffer[_referenceDataBufferPos], outputDataSize, (uint8_t*)outputDataBuffer,
93 &output_size, outputDataSize, _useZlib ? 0 : XD3_NOCOMPRESS);
94
95 // If an error happened, print it here
96 if (ret != 0)
98 "[Error] unexpected error while decoding differential decompression. Probably maximum input data position reached after differential decode (%lu + %lu > %lu)",
100
101 // Increasing output data position pointer
102 _inputDataBufferPos += diffCount;
103
104 // Increasing the number of differential bytes processed
105 _differentialBytesCount += diffCount;
106
107 // Finally, increasing reference data position pointer
108 _referenceDataBufferPos += outputDataSize;
109 }
110
116 size_t getReferenceDataBufferPos() const { return _referenceDataBufferPos; }
117
125 size_t getDifferentialBytesCount() const { return _differentialBytesCount; }
126
127private:
131 const uint8_t* __restrict const _referenceDataBuffer;
132
136 const size_t _referenceDataBufferSize;
137
141 size_t _referenceDataBufferPos = 0;
142
146 size_t _differentialBytesCount = 0;
147
151 const bool _useZlib;
152};
153
154} // namespace deserializer
155
156} // namespace jaffarCommon
Definition deserializers/base.hpp:24
size_t _inputDataBufferPos
Definition deserializers/base.hpp:84
const size_t _inputDataBufferSize
Definition deserializers/base.hpp:79
const uint8_t *__restrict const _inputDataBuffer
Definition deserializers/base.hpp:74
Definition deserializers/differential.hpp:24
__JAFFAR_COMMON_INLINE__ void popContiguous(void *const __restrict outputDataBuffer, const size_t outputDataSize) override
Definition deserializers/differential.hpp:46
size_t getDifferentialBytesCount() const
Definition deserializers/differential.hpp:125
size_t getReferenceDataBufferPos() const
Definition deserializers/differential.hpp:116
Differential(const void *__restrict inputDataBuffer=nullptr, const size_t inputDataBufferSize=std::numeric_limits< uint32_t >::max(), const void *__restrict referenceDataBuffer=nullptr, const size_t referenceDataBufferSize=std::numeric_limits< uint32_t >::max(), const bool useZlib=false)
Definition deserializers/differential.hpp:35
__JAFFAR_COMMON_INLINE__ void pop(void *const __restrict outputDataBuffer, const size_t outputDataSize) override
Definition deserializers/differential.hpp:66
#define JAFFAR_THROW_RUNTIME(...)
Definition exceptions.hpp:22
Contains the base class for the data serializers.