-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.cpp
More file actions
60 lines (53 loc) · 1.69 KB
/
Copy pathobjects.cpp
File metadata and controls
60 lines (53 loc) · 1.69 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
#include <filesystem>
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdint>
#include <vector>
#include <zlib.h>
std::vector<uint8_t> wrap(const std::string &type, const std::vector<uint8_t> &content){
std:: size_t size = content.size();
std::string header = type + " " + std::to_string(size);
size_t totalSize = header.size() + 1 + size;
std::vector<uint8_t> finalBuffer(totalSize);
std::memcpy(finalBuffer.data(), header.data(), header.size());
finalBuffer[header.size()] = '\0';
std::memcpy(finalBuffer.data() + header.size() + 1, content.data(), size);
return finalBuffer;
}
std::vector<uint8_t> decompress(const std::vector<uint8_t>& compressed){
z_stream ztr{};
ztr.zalloc = Z_NULL;
ztr.zfree = Z_NULL;
ztr.opaque = Z_NULL;
ztr.next_in = (Bytef *) compressed.data();
ztr.avail_in = compressed.size();
inflateInit(&ztr);
std::vector<uint8_t> result;
uint8_t chunk[8192];
int ret;
do{
ztr.next_out = chunk;
ztr.avail_out = sizeof(chunk);
ret = inflate(&ztr,Z_NO_FLUSH);
size_t filled = sizeof(chunk) - ztr.avail_out;
result.insert(result.end(),chunk, chunk + filled);
}while(ret == Z_OK);
inflateEnd(&ztr);
if(ret != Z_STREAM_END) return {};
return result;
}
std::vector<uint8_t> read_bytes(const std::filesystem::path &filePath){
std::ifstream fileInputStream(filePath, std::ios::binary | std::ios::ate);
if(!fileInputStream){
std::cerr<<"read_bytes: could open the file: "<<filePath<<"\n";
return {};
}
std::streamsize size = fileInputStream.tellg();
fileInputStream.seekg(0, std::ios::beg);
std::vector<uint8_t> buffer(size);
if (size > 0){
if (!fileInputStream.read(reinterpret_cast<char *>(buffer.data()), size)) return {};
}
return buffer;
}