-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
138 lines (113 loc) · 3.86 KB
/
main.cpp
File metadata and controls
138 lines (113 loc) · 3.86 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
#define BPF_IMPLEMENT
#include <bpf.h>
#include <fstream>
#include <dirent.h>
#include <string>
#include <iostream>
#include <filesystem>
#include <string>
#include <streambuf>
#include <vector>
using std::ofstream;
using std::ifstream;
using std::cout;
using std::endl;
void print_file(bpf_file_t file){
printf("VALID: %s\n", (bpf_verify_file(file) == BPF_TRUE ? "TRUE" : "FALSE"));
printf("bpf_file_header: %s\n", file.header.bpf_file_header);
printf("bpf_file_version: %i\n", file.header.bpf_file_version);
printf("checksum: %i\n", file.header.checksum);
printf("file_blocks: %i\n", file.header.file_blocks);
printf("data_size: %i\n", file.header.data_size);
for(int i = 0; i < file.header.file_blocks; i++) {
printf("blocks[%i].file_name_hash: %i\n", i, file.blocks[i].file_name_hash);
printf("blocks[%i].pointer_location: %i\n", i, file.blocks[i].pointer_location);
printf("blocks[%i].pointer_size: %i\n", i, file.blocks[i].pointer_size);
}
}
int main(int argc, char* argv[]){
printf("-- BPFPACK --\n");
char* directory;
if(argc < 2){
printf("ERROR: NO INPUT DIRECTORY\n");
return -1;
} else{
directory = argv[1];
}
char* outfile;
if(argc < 3){
printf("ERROR: NO OUTPUT FILE; DEFAULTING TO DIRECTORY NAME!\n");
outfile = (char*)malloc(strlen(directory));
memcpy(outfile, directory, strlen(directory));
strcat(outfile, ".bpf");
} else{
outfile = argv[2];
}
printf("Input directory: %s\n", directory);
printf("Output file: %s\n", outfile);
char* files[2048];
int file_c = 0;
DIR *d;
struct dirent *dir;
d = opendir(directory);
if (d) {
while ((dir = readdir(d)) != NULL) {
if(dir->d_type==DT_REG){
files[file_c] = (char*)malloc(256);
memcpy(files[file_c], dir->d_name, 256);
file_c++;
}
}
closedir(d);
} else{
printf("ERROR: INVALID DIRECTORY\n");
exit(EXIT_FAILURE);
}
printf("\nLISTING FILES:\n");
for(int i = 0; i < file_c; i++){
printf("\tPACK FILE\t %s\n", files[i]);
}
printf("LIST END\n");
printf("Generating file...\n");
unsigned int sizes[file_c]; // The sizes of the 2 resources data
unsigned char* data[file_c]; // The complete data array
for(int i = 0; i < file_c; i++){
std::string path;
path.append(directory).append("/").append(files[i]);
printf("READING FILE\t");
printf("%s\t", files[i]);
ifstream in(path, std::ifstream::in | std::ifstream::binary);
if(in.is_open()){
std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(in), {});
sizes[i] = buffer.size();
data[i] = (unsigned char*)malloc(buffer.size());
memcpy(data[i], buffer.data(), buffer.size());
printf("[OK]\n");
} else{
printf("[FAILED]\n");
return 1;
}
}
printf("Generating bpf_file_t...\n");
bpf_file_t file = bpf_generate_file(files, file_c, sizes, data); // Generate the array
printf("Generating raw data...\n");
bpf_file_data_t filedata = bpf_serialize_file(file); // Serialize into a byte[];
printf("Writing file...\n");
// Write byte array to file
ofstream stream;
stream.open(outfile);
if( !stream )
cout << "Opening file failed" << endl;
// use operator<< for clarity
stream.write(filedata.data, filedata.size); // Filedata has the data segment and the size segment.
// test if write was succesful - not *really* necessary
if( !stream )
cout << "Write failed!" << endl;
printf("Task finished!\n");
printf("Cleaning up...\n");
for(int i = 0; i < file_c; i++){
free(data[i]);
free(files[i]);
}
return 0;
}