-
Notifications
You must be signed in to change notification settings - Fork 24
/
Archive.hpp
201 lines (165 loc) · 4.78 KB
/
Archive.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* MCM file compressor
Copyright (C) 2014, 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 ARCHIVE_HPP_
#define ARCHIVE_HPP_
#include <thread>
#include "CM.hpp"
#include "Compressor.hpp"
#include "File.hpp"
#include "Stream.hpp"
#include "Util.hpp"
// Force filter
enum FilterType {
kFilterTypeNone,
kFilterTypeDict,
kFilterTypeX86,
kFilterTypeAuto,
kFilterTypeCount,
};
enum CompLevel {
kCompLevelStore,
kCompLevelTurbo,
kCompLevelFast,
kCompLevelMid,
kCompLevelHigh,
kCompLevelMax,
kCompLevelSimple,
};
std::ostream& operator<<(std::ostream& os, CompLevel comp_level);
enum LZPType {
kLZPTypeAuto,
kLZPTypeEnable,
kLZPTypeDisable,
};
class CompressionOptions {
public:
static const size_t kDefaultMemUsage = 6;
static const CompLevel kDefaultLevel = kCompLevelMid;
static const FilterType kDefaultFilter = kFilterTypeAuto;
static const LZPType kDefaultLZPType = kLZPTypeAuto;
public:
size_t mem_usage_ = kDefaultMemUsage;
CompLevel comp_level_ = kDefaultLevel;
FilterType filter_type_ = kDefaultFilter;
LZPType lzp_type_ = kDefaultLZPType;
std::string dict_file_;
std::string out_dict_file_;
};
// File headers are stored in a list of blocks spread out through data.
class Archive {
public:
class Header {
public:
static const size_t kCurMajorVersion = 0;
static const size_t kCurMinorVersion = 84;
static const size_t kMagicStringLength = 10;
static const char* getMagic() {
return "MCMARCHIVE";
}
Header();
void read(Stream* stream);
void write(Stream* stream);
bool isArchive() const;
bool isSameVersion() const;
uint16_t majorVersion() const {
return major_version_;
}
uint16_t minorVersion() const {
return minor_version_;
}
private:
char magic_[10]; // MCMARCHIVE
uint16_t major_version_ = kCurMajorVersion;
uint16_t minor_version_ = kCurMinorVersion;
};
class Algorithm {
public:
Algorithm() {}
Algorithm(const CompressionOptions& options, Detector::Profile profile);
Algorithm(Stream* stream);
// Freq is the approximate distribution of input frequencies for the compressor.
Compressor* CreateCompressor(const FrequencyCounter<256>& freq);
void read(Stream* stream);
void write(Stream* stream);
Filter* createFilter(Stream* stream, Analyzer* analyzer, Archive& archive, size_t opt_var = 0);
Detector::Profile profile() const {
return profile_;
}
private:
uint8_t mem_usage_;
Compressor::Type algorithm_;
bool lzp_enabled_;
FilterType filter_;
Detector::Profile profile_;
};
class SolidBlock {
public:
Algorithm algorithm_;
std::vector<FileSegmentStream::FileSegments> segments_;
// Not stored, obtianed from segments.
uint64_t total_size_ = 0u;
SolidBlock() = default;
SolidBlock(const Algorithm& algorithm) : algorithm_(algorithm) {}
void write(Stream* stream);
void read(Stream* stream);
};
class Blocks : public std::vector<std::unique_ptr<SolidBlock>> {
public:
void write(Stream* stream);
void read(Stream* stream);
};
// Compression.
Archive(Stream* stream, const CompressionOptions& options);
// Decompression.
Archive(Stream* stream);
// Construct blocks from analyzer.
void constructBlocks(Analyzer::Blocks* blocks_for_file);
const Header& getHeader() const {
return header_;
}
CompressionOptions& Options() {
return options_;
}
bool setOpt(size_t var) {
opt_var_ = var;
return true;
}
bool setOpts(size_t* vars) {
opt_vars_ = vars;
setOpt(vars[0]);
return true;
}
void writeBlocks();
void readBlocks();
// Analyze and compress. Returns how many bytes wre compressed.
uint64_t compress(const std::vector<FileInfo>& in_files);
// Decompress.
void decompress(const std::string& out_dir, bool verify = false);
// List files and info.
void list();
size_t* opt_vars_ = nullptr;
private:
Stream* stream_;
Header header_;
CompressionOptions options_;
size_t opt_var_;
FileList files_; // File list.
Blocks blocks_; // Solid blocks.
void init();
Compressor* createMetaDataCompressor();
};
#endif