-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tyfe.hpp
258 lines (201 loc) · 5.79 KB
/
Tyfe.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
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
/* MIT License
#
# Copyright (c) 2021 Ferhat Geçdoğan All Rights Reserved.
# Distributed under the terms of the MIT License.
#
# */
#ifndef TYFE_HPP
#define TYFE_hPP
#if __cplusplus < 201703L
#error Tyfe requires compiler that must supports C++17 or higher standard.
#endif
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
// Required filesystem flag (-lstdc++fs) &
// C++17-supported compiler
#include <filesystem>
//#include <functional>
typedef unsigned short UINT16;
typedef unsigned char* UCHARP;
// Supported types
enum TYFES {
NOTHING = -1,
JPEG = 0,
PNG,
GIF,
BMP,
WEBP,
PDF,
ICO,
FLASCRIPT,
BASH,
SH
};
class Tyfe {
std::string extension,
filename;
/*std::vector<std::string> shebang_ext = {
"sh",
"bash",
"fla"
};*/
std::vector<std::string> binary_ext = {
".jpg",
".jpeg",
".png",
".gif",
".bmp",
".webp",
".pdf",
".ico"
};
std::vector<std::string>::iterator binary_ext_iter;
public:
/* Some markers & magics for detect type of file */
enum MARKERS : const UINT16 {
JPEG_SOI = 0xD8,
JPEG_START = 0xFF,
PNG_SOI = 0x89,
PNG_START_2 = 0x50,
PNG_START_3 = 0x4E,
PNG_START_4 = 0x47,
GIF_SOI = 0x47,
GIF_START_2 = 0x49,
GIF_START_3 = 0x46,
BMP_SOI = 0x42,
BMP_START_2 = 0x4D,
WEBP_SOI = 0x57,
WEBP_START_2 = 0x45,
WEBP_START_3 = 0x42,
WEBP_START_4 = 0x50,
PDF_SOI = 0x25,
PDF_START_2 = 0x50,
PDF_START_3 = 0x44,
PDF_START_4 = 0x46,
ICO_SOI = 0x00,
ICO_START_3 = 0x01
};
/*
template<typename T>
struct check_type {
bool type() {
return(std::is_same<T, std::string>::value
|| std::is_same<T, char*>::value
|| std::is_same<T, const char*>::value);
}
};
*/
bool ext(std::string const ext2,
std::string const ext3) {
return ((extension == ext2) || (extension == ext3));
}
// TODO: Create TYFES to std::string function.
// template<typename T>
TYFES check(std::string file/* T type */) {
// check_type<T> __check;
/*if(__check.type() != true) {
return NOTHING;
}*/
filename = file;
extension = std::filesystem::path(filename).extension();
binary_ext_iter = std::find(binary_ext.begin(),
binary_ext.end(), extension);
if(binary_ext_iter != binary_ext.end()) {
return what_is_this();
} else {
return is_shebang();
}
}
TYFES what_is_this() {
std::ifstream file(filename, std::ios::in | std::ifstream::binary);
/* check file is opened */
if(!file.is_open()) {
throw std::runtime_error("tyfe: Can't open file: " + filename);
return NOTHING;
}
std::string data((std::istreambuf_iterator<char>(file)),
(std::istreambuf_iterator<char>()));
UCHARP marker = reinterpret_cast<UCHARP>(&data[0]);
if(marker[0] == JPEG_START
&& marker[1] == JPEG_SOI
&& marker[2] == JPEG_START) {
return JPEG;
}
if(marker[0] == PNG_SOI
&& marker[1] == PNG_START_2
&& marker[2] == PNG_START_3
&& marker[3] == PNG_START_4) {
return PNG;
}
if(marker[0] == GIF_SOI
&& marker[1] == GIF_START_2
&& marker[2] == GIF_START_3) {
return GIF;
}
if(marker[0] == BMP_SOI
&& marker[1] == BMP_START_2) {
return BMP;
}
if(marker[8] == WEBP_SOI
&& marker[9] == WEBP_START_2
&& marker[10]== WEBP_START_3
&& marker[11]== WEBP_START_4) {
return WEBP;
}
if(marker[0] == PDF_SOI
&& marker[1] == PDF_START_2
&& marker[2] == PDF_START_3
&& marker[3] == PDF_START_4) {
return PDF;
}
if(marker[0] == ICO_SOI
&& marker[1] == ICO_SOI
&& marker[2] == ICO_START_3
&& marker[3] == ICO_SOI) {
return ICO;
}
return NOTHING;
}
TYFES is_shebang() {
std::ifstream file(filename, std::ios::in | std::ios::app);
std::string __file;
do {
if(__file[0] == '#' && __file[1] == '!') {
__file = __file.erase(0, (std::filesystem::path(__file).parent_path().string()).length());
if(__file.find("fla") != std::string::npos) return FLASCRIPT;
if(__file.find("sh") != std::string::npos
&& __file.find("bash") == std::string::npos) return SH;
else
return BASH;
}
} while(std::getline(file, __file));
return NOTHING;
}
std::string type(std::string type) {
switch(check(type)) {
case JPEG:
return "JPEG";
case FLASCRIPT:
return "FlaScript";
case BASH:
return "BASH";
case SH:
return "SH";
case PNG:
return "PNG";
case GIF:
return "GIF";
case BMP:
return "BMP";
case WEBP:
return "WEBP";
case PDF:
return "PDF";
case ICO:
return "ICO";
}
return "";
}
};
#endif // TYFE_HPP