forked from JayXon/Leanify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleanify.cpp
More file actions
174 lines (160 loc) · 6.51 KB
/
leanify.cpp
File metadata and controls
174 lines (160 loc) · 6.51 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
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
#include "leanify.h"
#include <algorithm>
#include <iostream>
#include <memory>
#include <zopfli/zlib_container.h>
#include <zopflipng/lodepng/lodepng.h>
#include "formats/data_uri.h"
#include "formats/dwf.h"
#include "formats/format.h"
#include "formats/gft.h"
#include "formats/gz.h"
#include "formats/ico.h"
#include "formats/jpeg.h"
#include "formats/lua.h"
#include "formats/mime.h"
#include "formats/pe.h"
#include "formats/png.h"
#include "formats/rdb.h"
#include "formats/swf.h"
#include "formats/tar.h"
#include "formats/vcf.h"
#include "formats/xml.h"
#include "formats/zip.h"
#include "utils.h"
using std::cerr;
using std::endl;
using std::string;
std::unique_ptr<Format> GetType(void* file_pointer, size_t file_size, const string& filename, int depth) {
if (depth > max_depth) {
VerbosePrint("Max depth ", max_depth, " reached, skipping.");
return std::make_unique<Format>(file_pointer, file_size, depth);
}
if (!filename.empty()) {
size_t dot = filename.find_last_of('.');
if (dot != string::npos) {
string ext = filename.substr(dot + 1);
// toupper
for (auto& c : ext)
c &= ~0x20;
if (ext == "HTML" || ext == "HTM" || ext == "JS" || ext == "CSS") {
VerbosePrint(ext, " detected.");
return std::make_unique<DataURI>(file_pointer, file_size, depth);
}
if (ext == "VCF" || ext == "VCARD") {
VerbosePrint(ext, " detected.");
return std::make_unique<Vcf>(file_pointer, file_size, depth);
}
if (ext == "MHT" || ext == "MHTML" || ext == "MIM" || ext == "MIME" || ext == "EML") {
VerbosePrint(ext, " detected.");
return std::make_unique<Mime>(file_pointer, file_size, depth);
}
}
}
if (memcmp(file_pointer, Png::header_magic, sizeof(Png::header_magic)) == 0) {
VerbosePrint("PNG detected.");
return std::make_unique<Png>(file_pointer, file_size, depth);
} else if (memcmp(file_pointer, Jpeg::header_magic, sizeof(Jpeg::header_magic)) == 0) {
VerbosePrint("JPEG detected.");
return std::make_unique<Jpeg>(file_pointer, file_size, depth);
} else if (memcmp(file_pointer, Lua::header_magic, sizeof(Lua::header_magic)) == 0) {
VerbosePrint("Lua detected.");
return std::make_unique<Lua>(file_pointer, file_size, depth);
} else if (memcmp(file_pointer, Zip::header_magic, sizeof(Zip::header_magic)) == 0) {
VerbosePrint("ZIP detected.");
return std::make_unique<Zip>(file_pointer, file_size, depth);
} else if (memcmp(file_pointer, Pe::header_magic, sizeof(Pe::header_magic)) == 0) {
VerbosePrint("PE detected.");
return std::make_unique<Pe>(file_pointer, file_size, depth);
} else if (memcmp(file_pointer, Gz::header_magic, sizeof(Gz::header_magic)) == 0) {
VerbosePrint("GZ detected.");
return std::make_unique<Gz>(file_pointer, file_size, depth);
} else if (memcmp(file_pointer, Ico::header_magic, sizeof(Ico::header_magic)) == 0) {
VerbosePrint("ICO detected.");
return std::make_unique<Ico>(file_pointer, file_size, depth);
} else if (memcmp(file_pointer, Dwf::header_magic, sizeof(Dwf::header_magic)) == 0) {
VerbosePrint("DWF detected.");
return std::make_unique<Dwf>(file_pointer, file_size, depth);
} else if (memcmp(file_pointer, Gft::header_magic, sizeof(Gft::header_magic)) == 0) {
VerbosePrint("GFT detected.");
return std::make_unique<Gft>(file_pointer, file_size, depth);
} else if (memcmp(file_pointer, Rdb::header_magic, sizeof(Rdb::header_magic)) == 0) {
VerbosePrint("RDB detected.");
return std::make_unique<Rdb>(file_pointer, file_size, depth);
} else if (memcmp(file_pointer, Swf::header_magic, sizeof(Swf::header_magic)) == 0 ||
memcmp(file_pointer, Swf::header_magic_deflate, sizeof(Swf::header_magic_deflate)) == 0 ||
memcmp(file_pointer, Swf::header_magic_lzma, sizeof(Swf::header_magic_lzma)) == 0) {
VerbosePrint("SWF detected.");
return std::make_unique<Swf>(file_pointer, file_size, depth);
} else {
// Search for vcard magic which might not be at the very beginning.
const string vcard_magic = "BEGIN:VCARD";
const char* fp = static_cast<char*>(file_pointer);
const char* search_end = fp + std::min(static_cast<size_t>(1024), file_size);
if (std::search(fp, search_end, vcard_magic.begin(), vcard_magic.end()) < search_end) {
VerbosePrint("VCF detected.");
return std::make_unique<Vcf>(file_pointer, file_size, depth);
}
// tar file does not have header magic
// ustar is optional
{
auto t = std::make_unique<Tar>(file_pointer, file_size, depth);
// checking first record checksum
if (t->IsValid()) {
VerbosePrint("tar detected.");
return t;
}
}
// XML file does not have header magic
// have to parse and see if there are any errors.
{
auto x = std::make_unique<Xml>(file_pointer, file_size, depth);
if (x->IsValid()) {
VerbosePrint("XML detected.");
return x;
}
}
}
VerbosePrint("Format not supported!");
// for unsupported format, just memmove it.
return std::make_unique<Format>(file_pointer, file_size, depth);
}
// Leanify the file
// and move the file ahead size_leanified bytes
// the new location of the file will be file_pointer - size_leanified
// it's designed this way to avoid extra memmove or memcpy
// return new size
size_t LeanifyFile(void* file_pointer, size_t file_size, size_t size_leanified /*= 0*/,
const string& filename /*= ""*/, int depth /*= 1*/) {
if (file_pointer == nullptr || file_size == 0) {
return file_size;
}
auto f = GetType(file_pointer, file_size, filename, depth);
return f->Leanify(size_leanified);
}
size_t ZlibRecompress(uint8_t* src, size_t src_len, size_t size_leanified /*= 0*/) {
if (!is_fast) {
size_t uncompressed_size = 0;
uint8_t* buffer = nullptr;
if (lodepng_zlib_decompress(&buffer, &uncompressed_size, src, src_len, &lodepng_default_decompress_settings) ||
!buffer) {
cerr << "Decompress Zlib data failed." << endl;
} else {
ZopfliOptions zopfli_options;
ZopfliInitOptions(&zopfli_options);
zopfli_options.numiterations = iterations;
size_t new_size = 0;
uint8_t* out_buffer = nullptr;
ZopfliZlibCompress(&zopfli_options, buffer, uncompressed_size, &out_buffer, &new_size);
free(buffer);
if (new_size < src_len) {
memcpy(src - size_leanified, out_buffer, new_size);
free(out_buffer);
return new_size;
}
free(out_buffer);
}
}
memmove(src - size_leanified, src, src_len);
return src_len;
}