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 // Reading differential outputDataBufferSize
71 usize_t diffCount = *(usize_t*)&_inputDataBuffer[_inputDataBufferPos];
72
73 // Size of differential buffer size
74 const size_t differentialBufferSize = sizeof(usize_t);
75
76 // If we reached maximum output, stop here
77 if (_inputDataBufferPos + differentialBufferSize >= _inputDataBufferSize)
78 JAFFAR_THROW_RUNTIME("[Error] Maximum input data position reached before differential buffer size decode (%lu + %lu > %lu)", _inputDataBufferPos, differentialBufferSize,
80
81 // Advancing position pointer to store the difference outputDataBufferSizeer
82 _inputDataBufferPos += differentialBufferSize;
83
84 // If we reached maximum output, stop here
85 if (_referenceDataBufferPos + outputDataSize > _referenceDataBufferSize)
86 JAFFAR_THROW_RUNTIME("[Error] Maximum reference data position exceeded before differential decode (%lu + %lu > %lu)", _referenceDataBufferPos, outputDataSize,
87 _referenceDataBufferSize);
88
89 // Encoding differential
90 usize_t output_size;
91 int ret = xd3_decode_memory(&_inputDataBuffer[_inputDataBufferPos], diffCount, &_referenceDataBuffer[_referenceDataBufferPos], outputDataSize, (uint8_t*)outputDataBuffer,
92 &output_size, outputDataSize, _useZlib ? 0 : XD3_NOCOMPRESS);
93
94 // If an error happened, print it here
95 if (ret != 0)
97 "[Error] unexpected error while decoding differential decompression. Probably maximum input data position reached after differential decode (%lu + %lu > %lu)",
99
100 // Increasing output data position pointer
101 _inputDataBufferPos += diffCount;
102
103 // Increasing the number of differential bytes processed
104 _differentialBytesCount += diffCount;
105
106 // Finally, increasing reference data position pointer
107 _referenceDataBufferPos += outputDataSize;
108 }
109
115 size_t getReferenceDataBufferPos() const { return _referenceDataBufferPos; }
116
124 size_t getDifferentialBytesCount() const { return _differentialBytesCount; }
125
126private:
130 const uint8_t* __restrict const _referenceDataBuffer;
131
135 const size_t _referenceDataBufferSize;
136
140 size_t _referenceDataBufferPos = 0;
141
145 size_t _differentialBytesCount = 0;
146
150 const bool _useZlib;
151};
152
153} // namespace deserializer
154
155} // 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 pop(void *const __restrict outputDataBuffer, const size_t outputDataSize) override
Definition deserializers/differential.hpp:66
size_t getDifferentialBytesCount() const
Definition deserializers/differential.hpp:124
size_t getReferenceDataBufferPos() const
Definition deserializers/differential.hpp:115
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 popContiguous(void *const __restrict outputDataBuffer, const size_t outputDataSize) override
Definition deserializers/differential.hpp:46
#define JAFFAR_THROW_RUNTIME(...)
Definition exceptions.hpp:22
Contains the base class for the data serializers.