-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmoduled_adaptor.hh
More file actions
123 lines (104 loc) · 3.78 KB
/
Copy pathmoduled_adaptor.hh
File metadata and controls
123 lines (104 loc) · 3.78 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright (C) Flamewing 2016 <flamewing.sonic@gmail.com>
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with main.c; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
*/
#ifndef LIB_MODULED_ADAPTOR_HH
#define LIB_MODULED_ADAPTOR_HH
#include <mdcomp/bigendian_io.hh>
#include <limits>
#include <sstream>
#include <vector>
template <
typename Format, size_t DefaultModuleSize, size_t DefaultModulePadding>
class ModuledAdaptor {
public:
enum {
ModuleSize = DefaultModuleSize,
ModulePadding = DefaultModulePadding
};
static size_t PadMaskBits;
static bool moduled_decode(
std::istream& Src, std::iostream& Dst,
size_t ModulePadding = DefaultModulePadding);
static bool moduled_encode(
std::istream& Src, std::ostream& Dst,
size_t ModulePadding = DefaultModulePadding);
};
template <
typename Format, size_t DefaultModuleSize, size_t DefaultModulePadding>
bool ModuledAdaptor<Format, DefaultModuleSize, DefaultModulePadding>::
moduled_decode(
std::istream& Src, std::iostream& Dst,
size_t const ModulePadding) {
int64_t const FullSize = BigEndian::Read2(Src);
std::stringstream in(std::ios::in | std::ios::out | std::ios::binary);
in << Src.rdbuf();
// Pad to even length, for safety.
if ((in.tellp() % 2) != 0) {
in.put(0);
}
in.seekg(0);
size_t const PadMask = ModulePadding - 1;
while (true) {
Format::decode(in, Dst);
if (Dst.tellp() >= FullSize) {
break;
}
// Skip padding between modules
size_t const paddingEnd = (size_t(in.tellg()) + PadMask) & ~PadMask;
in.seekg(paddingEnd);
}
return true;
}
template <
typename Format, size_t DefaultModuleSize, size_t DefaultModulePadding>
bool ModuledAdaptor<Format, DefaultModuleSize, DefaultModulePadding>::
moduled_encode(
std::istream& Src, std::ostream& Dst,
size_t const ModulePadding) {
size_t Location = Src.tellg();
Src.ignore(std::numeric_limits<std::streamsize>::max());
size_t FullSize = Src.gcount();
Src.seekg(Location);
std::vector<uint8_t> data;
data.resize(FullSize);
auto ptr = data.cbegin();
Src.read(reinterpret_cast<char*>(&(data.front())), data.size());
size_t const PadMask = ModulePadding - 1;
BigEndian::Write2(Dst, FullSize);
std::stringstream sout(std::ios::in | std::ios::out | std::ios::binary);
while (FullSize > ModuleSize) {
// We want to manage internal padding for all modules but the last.
PadMaskBits = 8 * ModulePadding - 1U;
Format::encode(sout, &(*ptr), ModuleSize);
FullSize -= ModuleSize;
ptr += ModuleSize;
// Padding between modules
int64_t const paddingEnd = (size_t(sout.tellp()) + PadMask) & ~PadMask;
for (; sout.tellp() < paddingEnd; sout.put(0)) {
}
}
PadMaskBits = 7U;
Format::encode(sout, &(*ptr), FullSize);
// Pad to even size.
Dst << sout.rdbuf();
if ((Dst.tellp() % 2) != 0) {
Dst.put(0);
}
return true;
}
#endif // LIB_MODULED_ADAPTOR_HH