-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
138 lines (108 loc) · 3.21 KB
/
Copy pathmain.cpp
File metadata and controls
138 lines (108 loc) · 3.21 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
#include <iostream>
#include <vector>
#include "HexFile.h"
#include <iomanip>
#include <string>
#include "memory.h"
#include "crc.h"
#include "windows.h"
#include "CMAC.h"
using namespace std;
#define SCRIPTNAME "script.txt"
#define SYSPAUSE (system("pause"));
int main(int argc, char ** argv)
{
unsigned int size = 0;
unsigned int offsetAddr = 0;
vector<HexBlock_t *> vecBlocks;
vector<unsigned char> headerData;
vector<Hexline_t> headerAscData;
size_t HexHeaderLines = 0;
// unsigned char test[16] = {0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,\
// 0x00,0x01,0x02,0x03,0x00,0x00,0x2F,0xDE};
// unsigned int out = crc32(test,16);
// cout<<out;
// //return 0;
if(argc <= 1)
{
cout<<"ERROR: no input file"<<endl;
SYSPAUSE
return -1;
}
const char * file = argv[1];
cout<<"Read File..."<<endl;
if(HexRead2(file, vecBlocks) == false)
{
cout<<"Read File Failed!"<<endl;
SYSPAUSE
return 0;
}
HexReadFirstOffset(file);
ReadScriptHeaderSize(SCRIPTNAME);
int k = 0;
for(auto i : vecBlocks)
{
std::cout<< "block_"<<dec<<i->counter<<" "<<"start: "<<hex<<"0x"<<i->startAddr<<" end: "<<"0x"<<i->endAddr<<" size: "<<dec<<i->size<<endl;
size += i->buffer.size();
k++;
}
cout<<"total size: "<<size<<endl;
if(k>1)
{
cout<<"ERROR: Hex Block bigger than 1, Please full fill Hex with 0xFF"<<endl;
SYSPAUSE
return -1;
}
if( (HeaderSize % 8)!=0 )
{
cout<<"ERROR: Header Data Size % 8 != 0"<<endl;
SYSPAUSE
return -1;
}
headerData.insert(headerData.begin(),vecBlocks[0]->buffer.begin(),vecBlocks[0]->buffer.begin() + HeaderSize);
HexHeaderLines = HeaderSize / vecBlocks[0]->hexLines[0].len;
for(int i = 0;i<HexHeaderLines;i++)
{
headerAscData.push_back(vecBlocks[0]->hexLines[i]);
}
// k = 0;
// for(auto byte: headerData)
// {
// printf("%02X",byte);
// if( (k+1)%32 == 0 )
// cout<<endl;
// k++;
// }
//process
if(DoScript(SCRIPTNAME, headerData, vecBlocks[0]->buffer) == false)
{
cout<<"ERROR TERMINATED"<<endl;
SYSPAUSE
return 0;
}
//process
string headerstr = HexBuffer2HexLineASC(headerData,headerAscData);
//cout<<"Header Modifyed: "<<endl<<headerstr<<endl ;
cout<<"Rebuilt Header..."<<endl;
string headerOffset = HexReadFirstOffset(file);
headerstr.insert(headerstr.begin(), headerOffset.begin(), headerOffset.end());
//cout<<headerstr<<endl;
vector<char> *pHexAll = HexReadALL(file);
//cout<<pHexAll->size();
cout<<"Cover Data..."<<endl;
memcpy(pHexAll->data(), headerstr.data(), headerstr.size());
cout<<"Write File..."<<endl;
string filename(file);
filename.erase(filename.begin() + filename.find("."), filename.end());
filename = filename + "_header_added.hex";
if(NewHex(filename.c_str(),pHexAll) == false)
{
cout<<"Write File Error"<<endl;
}else{
cout<<"Okay"<<endl;
}
cout<<"Modifyed Header: "<<endl;
HexShowHeader(filename.c_str());
SYSPAUSE;
return 0;
}