-
Notifications
You must be signed in to change notification settings - Fork 24
/
DeltaFilter.hpp
99 lines (85 loc) · 2.86 KB
/
DeltaFilter.hpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* MCM file compressor
Copyright (C) 2015, Google Inc.
Authors: Mathieu Chartier
LICENSE
This file is part of the MCM file compressor.
MCM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MCM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MCM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _DELTA_FILTER_HPP_
#define _DELTA_FILTER_HPP_
#include <memory>
#include "Filter.hpp"
#include "Memory.hpp"
// Encoding format:
// E8/E9 FF/00 XX XX XX
// E8/E8 B2 <char> -> E8/E9 <char>
// E8/E9 <other> -> E8/E9 <other>
class DeltaFilter : public ByteStreamFilter<16 * KB, 20 * KB> {
public:
DeltaFilter(Stream* stream, uint32_t bytes, uint32_t offset /* multiple of bytes*/) : ByteStreamFilter(stream), prev_(0), bytes_(bytes), offset_(offset), pos_(0) {
prev_storage_.reset(new uint32_t[offset]);
prev_ = prev_storage_.get();
std::fill(prev_, prev_ + offset, 0U);
}
virtual void forwardFilter(uint8_t* out, size_t* out_count, uint8_t* in, size_t* in_count) {
process<true>(out, out_count, in, in_count);
}
virtual void reverseFilter(uint8_t* out, size_t* out_count, uint8_t* in, size_t* in_count) {
process<false>(out, out_count, in, in_count);
}
static uint32_t getMaxExpansion() {
return 1;
}
void dumpInfo() const {
}
void setSpecific(uint32_t s) {
}
private:
template <bool encode>
void process(uint8_t* out, size_t* out_count, uint8_t* in, size_t* in_count) {
const auto max_c = std::min(*out_count, *in_count);
if (max_c < bytes_) {
memcpy(out, in, max_c);
*out_count = *in_count = max_c;
} else {
size_t i;
for (i = 0; i <= max_c - bytes_; i += bytes_) {
uint32_t cur = static_cast<uint32_t>(readBytes<uint32_t, false>(in + i, bytes_));
uint32_t prev = prev_[pos_];
if (encode) {
prev_[pos_] = cur;
cur -= prev;
} else {
cur += prev;
prev_[pos_] = cur;
}
writeBytes<uint32_t, false>(out + i, bytes_, cur);
if (++pos_ == offset_) {
pos_ = 0;
}
}
*out_count = *in_count = i;
}
}
std::unique_ptr<uint32_t[]> prev_storage_;
uint32_t* prev_;
size_t bytes_;
size_t offset_;
size_t pos_;
};
template <uint32_t bytes, uint32_t offset>
class FixedDeltaFilter : public DeltaFilter {
public:
FixedDeltaFilter(Stream* stream) : DeltaFilter(stream, bytes, offset) {}
void setOpt(size_t) {}
};
#endif