-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeck.cpp
365 lines (299 loc) · 11.5 KB
/
speck.cpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "include/speck.hpp"
namespace speck {
void write_speckage_to_stream(const speckage& speckage, std::ostream& stream)
{
uint64_t header_size = sizeof(uint64_t); // starting size for the size itself
uint8_t name_size = (uint8_t)speckage.name.size();
header_size += sizeof(name_size);
header_size += name_size;
for (const auto& info : speckage.file_info)
{
// two uint64_t for each file;
header_size += 16;
header_size += info.first.size() + 1;
}
stream.write(SPECK_STRING, SPECK_STRING_LENGTH);
stream.write(SPECK_VERSION, SPECK_VERSION_LENGTH);
stream.write(reinterpret_cast<char*>(&header_size), sizeof(header_size));
stream.write((char*)&name_size, sizeof(name_size));
stream.write(speckage.name.data(), name_size);
for (const auto& info : speckage.file_info)
{
for (const auto& character : info.first)
{
stream << character;
}
stream << '\0';
// position
stream.write(reinterpret_cast<const char*>(&info.second.first), 8);
// size
stream.write(reinterpret_cast<const char*>(&info.second.second), 8);
}
stream.write(reinterpret_cast<char*>(speckage.data), speckage.first_empty_location);
}
bool read_header_from_stream(std::istream& stream, std::string& speckage_name, uint64_t& data_size, std::unordered_map<std::string, std::pair<uint64_t, uint64_t>>& file_info)
{
size_t start_position = stream.tellg();
// check header prefix
char name_string[SPECK_STRING_LENGTH];
char version_string[SPECK_VERSION_LENGTH];
stream.read(name_string, SPECK_STRING_LENGTH);
stream.read(version_string, SPECK_VERSION_LENGTH);
if (std::strcmp(name_string, SPECK_STRING) != 0 || std::strcmp(version_string, SPECK_VERSION) != 0)
{
return false;
}
// read header
auto header_size = get_data_from_stream<uint64_t>(stream) + SPECK_STRING_LENGTH + SPECK_VERSION_LENGTH;
auto name_size = get_data_from_stream<uint8_t>(stream);
speckage_name.resize(name_size);
stream.read(speckage_name.data(), name_size);
while ((size_t)stream.tellg() < start_position + (size_t)header_size)
{
std::vector<char> name;
char temp;
do
{
stream.get(temp);
name.push_back(temp);
}
while (temp);
std::string final_name = name.data();
auto begin_offset = get_data_from_stream<uint64_t>(stream);
auto length = get_data_from_stream<uint64_t>(stream);
data_size += length;
file_info[final_name] = std::pair(begin_offset, length);
}
assert((size_t)stream.tellg() == start_position + header_size);
return true;
}
speckage read_speckage_from_stream(std::istream& stream)
{
speckage out;
if (!read_header_from_stream(stream, out.name, out.data_size, out.file_info))
{
return out;
}
out.first_empty_location = out.data_size + 1;
out.data = (char*)malloc(out.data_size);
stream.read(reinterpret_cast<char*>(out.data), out.data_size);
return out;
}
appendedSpeckageInfos discover_appended_speckages(const std::string& filepath)
{
auto file = std::ifstream(filepath, std::ios::binary | std::ios::in);
appendedSpeckageInfos out;
if (file.bad())
return out;
out = discover_appended_speckages(file);
out.filepath = filepath;
file.close();
return out;
}
appendedSpeckageInfos discover_appended_speckages(std::istream& stream)
{
appendedSpeckageInfos out = {};
out.filepath = "";
stream.seekg(0, std::ios_base::end);
size_t position = stream.tellg();
while (!stream.bad() && !stream.fail())
{
// check post-header postfix
char name_string[SPECK_FOOTER_STRING_LENGTH];
char version_string[SPECK_VERSION_LENGTH];
position -= SPECK_FOOTER_STRING_LENGTH;
stream.seekg(position, std::ios_base::beg);
stream.read(name_string, SPECK_FOOTER_STRING_LENGTH);
position -= SPECK_VERSION_LENGTH;
stream.seekg(position, std::ios_base::beg);
stream.read(version_string, SPECK_VERSION_LENGTH);
// if post-header is invalid, all appended speckages have been found
if (std::strcmp(name_string, SPECK_FOOTER_STRING) != 0 || std::strcmp(version_string, SPECK_VERSION) != 0)
{
return out;
}
// speckage name
position -= sizeof(uint8_t);
stream.seekg(position, std::ios_base::beg);
uint8_t name_length = 0;
stream.read((char*)&name_length, sizeof(name_length));
std::string name;
name.resize(name_length);
position -= name_length;
stream.seekg(position, std::ios_base::beg);
stream.read(name.data(), name_length);
// read distance to header
uint64_t size = 0;
position -= sizeof(size);
stream.seekg(position, std::ios_base::beg);
stream.read((char*)&size, sizeof(size));
position -= size;
out.speckage_offset_map[name] = position;
stream.seekg(position, std::ios_base::beg);
std::unordered_map<std::string, std::pair<uint64_t, uint64_t>> file_info;
uint64_t data_size;
std::string temp;
read_header_from_stream(stream, temp, data_size, file_info);
for (auto const& [file_name, val] : file_info)
{
out.filename_speckage_map[file_name] = name;
}
}
return out;
}
speckage read_appended_speckage_from_file(const std::string& speckage_name, const appendedSpeckageInfos& appended_speckage_infos)
{
auto file = std::ifstream(appended_speckage_infos.filepath, std::ios::binary | std::ios::in);
speckage out;
if (file.bad())
return out;
out = read_appended_speckage_from_stream(speckage_name, appended_speckage_infos, file);
file.close();
return out;
}
std::vector<speckage> read_appended_speckages_from_file(const std::vector<std::string>& speckage_names, const appendedSpeckageInfos& appended_speckage_infos)
{
auto file = std::ifstream(appended_speckage_infos.filepath, std::ios::binary | std::ios::in);
std::vector<speckage> out;
if (file.bad())
return out;
out = read_appended_speckages_from_stream(speckage_names, appended_speckage_infos, file);
file.close();
return out;
}
speckage read_appended_speckage_from_stream(const std::string& speckage_name, const appendedSpeckageInfos& appended_speckage_infos, std::istream& stream)
{
stream.seekg(appended_speckage_infos.speckage_offset_map.at(speckage_name), std::ios_base::beg);
return read_speckage_from_stream(stream);
}
std::vector<speckage> read_appended_speckages_from_stream(const std::vector<std::string>& speckage_names, const appendedSpeckageInfos& appended_speckage_infos, std::istream& stream)
{
std::vector<speckage> out;
for (const auto& name : speckage_names)
{
stream.seekg(appended_speckage_infos.speckage_offset_map.at(name), std::ios_base::beg);
out.push_back(read_speckage_from_stream(stream));
}
return out;
}
bool append_speckages_to_file(const std::vector<speckage>& speckages, const std::string& filepath)
{
std::ofstream file(filepath, std::ios::out | std::ios::binary | std::ios::ate | std::ios::app);
append_speckages_to_stream(speckages, file);
if (file.fail())
{
file.close();
return false;
}
file.close();
return true;
}
bool append_speckages_to_stream(const std::vector<speckage>& speckages, std::ostream& stream)
{
stream.seekp(0, std::ios_base::end);
for (int i = 0; i < SPECK_FOOTER_STRING_LENGTH + SPECK_VERSION_LENGTH; ++i)
{
stream.put(0);
}
for (const auto& speckage : speckages)
{
size_t position = stream.tellp();
write_speckage_to_stream(speckage, stream);
uint64_t size = (size_t)stream.tellp() - position;
// now write footer
stream.write((char*)&size, sizeof(size)); // size of speckage
uint8_t name_size = (uint8_t)speckage.name.size();
stream.write(speckage.name.data(), name_size); // name
stream.write((char*)&name_size, sizeof(name_size)); // name size
stream.write(SPECK_VERSION, SPECK_VERSION_LENGTH); // version
stream.write(SPECK_FOOTER_STRING, SPECK_FOOTER_STRING_LENGTH); // footer postfix
}
return true;
}
bool add_file_to_speckage(speckage& speckage, const std::string& filepath)
{
std::ifstream file(filepath, std::ios::in | std::ios::binary | std::ios::ate);
if (!file)
{
// file failed to open
return false;
}
auto file_size = file.tellg();
file.seekg(0, std::ios::beg);
if (speckage.data_size - speckage.first_empty_location < (uint64_t)file_size)
{
uint64_t next_size = (uint64_t)file_size > speckage.min_memory_on_expand ? (uint64_t)file_size : speckage.min_memory_on_expand;
char* next_data = (char*)malloc(speckage.data_size + next_size);
std::memcpy(next_data, speckage.data, speckage.data_size);
if (speckage.data_size > 0)
{
free(speckage.data);
}
speckage.data = next_data;
speckage.data_size += next_size;
}
file.read(reinterpret_cast<char*>(speckage.data) + speckage.first_empty_location, file_size);
if (file.fail())
{
// io error
file.close();
return false;
}
speckage.file_info[std::string(filepath)] = std::make_pair(speckage.first_empty_location, file_size);
file.close();
speckage.first_empty_location += file_size;
return true;
}
bool save_speckage_to_file(const speckage& toSave, const std::string& filepath)
{
std::ofstream file(filepath, std::ios::out | std::ios::binary | std::ios::trunc);
write_speckage_to_stream(toSave, file);
if (file.fail())
{
file.close();
return false;
}
file.close();
return true;
}
speckage read_speckage_from_file(std::string filepath)
{
auto file = std::ifstream(filepath, std::ios::binary | std::ios::in);
if (file.bad())
return {};
speckage out = read_speckage_from_stream(file);
file.close();
return out;
}
void unload_speckage(speckage& speckage_to_unload)
{
free(speckage_to_unload.data);
speckage_to_unload.file_info.clear();
speckage_to_unload.data_size = 0;
speckage_to_unload.first_empty_location = 0;
speckage_to_unload.data = nullptr;
}
std::vector<std::string> discover_files_in_speckage(const std::string& filepath)
{
auto file = std::ifstream(filepath, std::ios::binary | std::ios::in);
std::vector<std::string> out;
if (file.bad())
return out;
std::string speckage_name;
uint64_t data_size;
std::unordered_map<std::string, std::pair<uint64_t, uint64_t>> file_info;
read_header_from_stream(file, speckage_name, data_size, file_info);
for(const auto& [name, val] : file_info){
out.push_back(name);
}
return out;
}
char* read_file_from_speckage(const speckage& speckage, std::string filepath, uint64_t& size)
{
if (!(speckage.file_info.count(filepath)))
return nullptr;
std::pair<uint64_t, uint64_t> info = speckage.file_info.at(filepath);
size = info.second;
return speckage.data + info.first;
}
} // namespace speck