-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHexFile.cpp
More file actions
1141 lines (924 loc) · 24.6 KB
/
Copy pathHexFile.cpp
File metadata and controls
1141 lines (924 loc) · 24.6 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "HexFile.h"
#include <string>
#include <assert.h>
#include <iostream>
#include "encrypt.h"
#include "stdlib.h"
using namespace std;
long long HexOffSetAddr = 0;
size_t HeaderSize = 0;
size_t HeaderStart = 0;
string EncryptStrs[] = {"CRC16","CRC32","ECC","SHA256","CMAC"};
void SetHeaderSize(size_t sz)
{
HeaderSize = sz;
}
void SetHexOffSetAddr(unsigned int addr)
{
if(HexOffSetAddr == 0)
{
HexOffSetAddr = addr;
cout<<" set HexStartAddr: "<<hex<<"0x"<<HexOffSetAddr<<endl;
}
}
long long ReadHexLine(unsigned char * hexLine)
{
if (*hexLine != COLON)
return -1;
switch(*(hexLine + DATATYPEPOS))
{
case DataRrecord:
return *(hexLine + LENGTHPOS);
break;
case EndofFileRecord:
return -10;
break;
case ExtendedSegmentAddressRecord:
return -9;
break;
case StartSegmentAddressRecord:
return -9;
break;
case ExtendedLinearAddressRecord:
return -8;
//unsigned int pos = *((unsigned int *)(hexLine + DATAPOS));
//return (pos << 16);
break;
case StartLinearAddressRecord:
return -8;
//return *((unsigned int *)(hexLine + DATAPOS));
break;
default:
return -1;
}
}
bool ReadOffSet(unsigned char *line,unsigned int *offset)
{
if (*(line) != COLON)
return false;
unsigned char * p = line + DATATYPEPOS;
unsigned char type = *(p);
p = line + LENGTHPOS;
unsigned char size = *p;
unsigned int pos = 0;
memcpy_s(&pos,size, (line + DATAPOS), size);
pos = ((unsigned int )(pos));
if (type == ExtendedLinearAddressRecord)
{
*offset = (pos << 16);
return true;
}
else if (type == StartLinearAddressRecord)
{
*offset = pos;
return true;
}
return false;
}
bool ReadData(unsigned char *line, unsigned int *len)
{
if (*(line) != COLON)
return false;
int type = *(line + DATATYPEPOS);
unsigned int len_ = *((unsigned int *)(line + DATAPOS));
if (type == DataRrecord)
{
*len = len_;
return true;
}
return false;
}
int ReadHexBlock( char *hex, HexBlock_t *block)
{
//unsigned int offset = 0;
//if ( ReadOffSet(hex, &offset) == false)
// return -1;
//block->offset = offset;
//unsigned int nextpos = FLAGLEN + *(hex + LENGTHPOS);
//while (ReadData(hex + nextpos + FLAGLEN, &nextpos))
//{
// block->size += nextpos;
//}
return 0;
}
void HexStrToByte(const char* source, unsigned char* dest, int sourceLen)
{
short i;
unsigned char highByte, lowByte;
for (i = 0; i < sourceLen; i += 2)
{
highByte = toupper(source[i]);
lowByte = toupper(source[i + 1]);
if (highByte > 0x39)
highByte -= 0x37;
else
highByte -= 0x30;
if (lowByte > 0x39)
lowByte -= 0x37;
else
lowByte -= 0x30;
dest[i / 2] = (highByte << 4) | lowByte;
}
return;
}
unsigned char CharToHexUchar(char * c)
{
unsigned char uc = 0;
HexStrToByte(c, &uc, 2);
return uc;
}
unsigned int CharToHexInt(char *c)
{
unsigned char uc[2] = { 0 };
HexStrToByte(c, uc, 4);
unsigned int us = uc[0]<<8 | uc[1];
return us;
}
bool HexRead2(const char *FileName, vector<HexBlock_t *> &blocks)
{
FILE* fp = NULL;
char RowData[256] = { 0 };
unsigned int Len = 0;
unsigned int preLen = 0;
unsigned int Addr = 0;
int Type = -1;
int preType = -1;
unsigned int OffsetAddress = 0;
unsigned char ucarr[32] = { 0 };
HexBlock_t * pBlock = NULL;
unsigned int blockCounter = 0;
unsigned int blockDataSize = 0;
unsigned int preAddr = 0;
Hexline_t line;
unsigned int tmpLen = 0;
unsigned char ucLine[256] = {0};
if ((fp = fopen(FileName, "a+")) == NULL)
{
return false;
}
while (fgets(RowData, 256, fp) != NULL)
{
if (RowData[0] == ':')
{
Type = CharToHexUchar(RowData + 7);
switch (Type)
{
case 0x00:
if(pBlock == NULL)
{
pBlock = new HexBlock;
pBlock->buffer.reserve(1*1024*1024);
blocks.push_back(pBlock);
pBlock->offset = OffsetAddress;
pBlock->startAddr = OffsetAddress;
pBlock->counter = blockCounter++;
preAddr = OffsetAddress;
preLen = 0;
Addr = CharToHexInt(RowData + 3) + OffsetAddress;
Len = CharToHexUchar(RowData + 1);
}else{
preType = Type;
preAddr = Addr ;
Addr = CharToHexInt(RowData + 3) + OffsetAddress;
preLen = Len;
Len = CharToHexUchar(RowData + 1);
}
HexStrToByte(RowData + 9, ucarr, Len * 2);
HexStrToByte(RowData + 1,ucLine,Len * 2 + 8 + 2);
pBlock->buffer.insert(pBlock->buffer.end(), ucarr, ucarr + Len);
line.bufferAsic.insert(line.bufferAsic.begin(), RowData, RowData + 9 + Len * 2 + 2);
line.buffers.insert(line.buffers.begin(), ucLine, ucLine + Len + 4 + 1 );
line.bufferAsic.push_back(0x0d);
line.bufferAsic.push_back(0x0a);
line.addr = Addr;
line.type = 0x00;
line.realAddr = (unsigned short)(Addr - OffsetAddress);
line.len = (unsigned char)Len;
line.checkSum = CharToHexUchar(RowData + 9 + Len*2);
pBlock->hexLines.push_back(std::move(line));
line.bufferAsic.clear();
line.buffers.clear();
if((preAddr + preLen != Addr))
{
pBlock->size = blockDataSize;
//pBlock->endAddr = pBlock->startAddr + blockDataSize - 1;
pBlock->endAddr = preAddr + preLen - 1;
// printf("block_%d\n", pBlock->counter);
// printf("OffsetAddress: 0x%02x ", pBlock->offset);
// printf("startAddress: 0x%02x ", pBlock->startAddr);
// printf("endAddress: 0x%02x ", pBlock->endAddr);
// printf("size: %d\n", pBlock->size);
// printf("\n");
blockDataSize = 0;
pBlock = new HexBlock;
blocks.push_back(pBlock);
pBlock->offset = OffsetAddress;
pBlock->counter = blockCounter++;
pBlock->startAddr = Addr;
}
blockDataSize += Len;
break;
case 0x02:
case 0x03:
break;
case 0x04:
OffsetAddress = CharToHexInt(RowData + 9) << 16;
break;
case 0x05:
OffsetAddress = CharToHexInt(RowData + 9);
break;
case 0x01:
//printf(RowData);
pBlock->size = blockDataSize;
pBlock->endAddr = pBlock->startAddr + blockDataSize - 1;
// printf("block_%d\n", pBlock->counter);
// printf("OffsetAddress: 0x%02x ", pBlock->offset);
// printf("startAddress: 0x%02x ", pBlock->startAddr);
// printf("endAddress: 0x%02x ", pBlock->endAddr);
// printf("size: %d\n", pBlock->size);
// printf("\n");
//printf("EOF");
break;
}
}
}
fclose(fp);
return true;
}
string HexReadFirstOffset(const char *FileName)
{
FILE* fp = NULL;
char RowData[256] = { 0 };
unsigned int Len = 0;
int Type = -1;
unsigned int blockDataSize = 0;
unsigned int OffsetAddress = 0;
if ((fp = fopen(FileName, "a+")) == NULL)
{
return string();
}
while (fgets(RowData, 256, fp) != NULL)
{
if (RowData[0] == ':')
{
Type = CharToHexUchar(RowData + 7);
switch (Type)
{
case 0x00:
blockDataSize += Len;
break;
case 0x02:
case 0x03:
break;
case 0x04:
OffsetAddress = CharToHexInt(RowData + 9) << 16;
SetHexOffSetAddr(OffsetAddress);
fclose(fp);
return string(RowData);
break;
case 0x05:
OffsetAddress = CharToHexInt(RowData + 9);
break;
case 0x01:
//printf(RowData);
fclose(fp);
return string(RowData);
break;
}
}
}
fclose(fp);
return string();
}
vector<char> * HexReadALL(const char *FileName)
{
FILE* fp = NULL;
unsigned int lSize = 0;
if ((fp = fopen(FileName, "a+")) == NULL)
{
cout<<"open failed";
return NULL;
}
fseek(fp, 0, SEEK_END);
lSize=ftell(fp);
rewind(fp);
char * pbuffer=(char *)malloc(sizeof(char)*lSize);
if(pbuffer==NULL)
{
cout<<"malloc failed";
return NULL;
}
size_t result=fread(pbuffer,1,lSize,fp);
if(result!=lSize)
{
// cout<<"result"<<result<<endl;
// cout<<"lSize"<<lSize<<endl;
// free(pbuffer);
// return NULL;
}
//cout<<"read: "<<result<<endl;
vector<char> * pbuf = new vector<char>;
pbuf->insert(pbuf->begin(),pbuffer,pbuffer + result);
free(pbuffer);
fclose(fp);
return pbuf;
}
void ScanHex(unsigned char *hex, unsigned int size, HexBlock_t *blocks)
{
if (NULL == hex) return;
unsigned int curPos = 0;
unsigned int pastPos = 0;
unsigned int len = 0;
if (*(hex + curPos) == COLON)
{
len = *(hex + curPos + LENGTHPOS);
}
}
// bool GetHeaderData(vector<unsigned char> & h)
// {
// }
string UcArray2String(vector<unsigned char> &buffers)
{
//assert(buffers)
string str;
for(auto i : buffers)
{
char tmp[2] = {0};
sprintf(tmp, "%02x",i);
str.append(tmp);
}
return str;
}
void HexToAscii(unsigned char * pHex, unsigned char * pAscii, int nLen)
{
unsigned char Nibble[2];
for (int i = 0; i < nLen; i++)
{
Nibble[0] = (pHex[i] & 0xF0) >> 4;
Nibble[1] = pHex[i] & 0x0F;
for (int j = 0; j < 2; j++)
{
if (Nibble[j] < 10)
Nibble[j] += 0x30;
else
{
if (Nibble[j] < 16)
Nibble[j] = Nibble[j] - 10 + 'A';
}
*pAscii++ = Nibble[j];
} // for (int j = ...)
} // for (int i = ...)
//*pAscii++ = 0x0d;
*pAscii++ = 0x0a;
}
unsigned char CheckSum(Hexline_t &hexLine)
{
unsigned int i = 0;
for(auto j : hexLine.buffers)
{
i +=j;
}
unsigned int addrl = hexLine.realAddr && 0xff;
unsigned int addrh = (hexLine.realAddr && 0xff00) >> 8;
return (0x100 - (i + addrl + addrh + hexLine.len + hexLine.type));
}
string HexBuffer2HexLineASC(const vector<unsigned char> &buffers, vector<Hexline_t> &hexLines)
{
assert(buffers.size() == HeaderSize);
assert(hexLines.size() == HeaderSize/(hexLines[0].len));
string header;
vector<unsigned char> tmp;
unsigned char asc[256] = {':'};
int j = 0;
unsigned int cur = 0;
unsigned char crc = 0;
for( auto i:hexLines )
{
//tmp.push_back(':');
tmp.insert(tmp.end(),i.buffers.begin(), i.buffers.begin() + 4);
//std::cout<<UcArray2String(tmp)<<std::endl;
tmp.insert(tmp.end(), buffers.begin() + cur , buffers.begin() + cur + i.len );
for(auto byte:tmp)
{
crc+=byte;
}
crc = 0x100 - crc;
tmp.push_back(crc);
HexToAscii(tmp.data(), asc + 1,tmp.size());
//cout<<asc;
header += std::string((char*)asc);
memset(asc+1,0,sizeof(asc) - 1);
crc = 0;
cur += i.len;
tmp.clear();
j++;
}
//cout<<header;
return header;
}
bool NewHex(const char * filename, vector<char> *pHexAll )
{
FILE * FPNEW = fopen(filename,"w+");
if(FPNEW == NULL)
{
return false;
}
if(fwrite(pHexAll->data(),1,pHexAll->size(),FPNEW) != pHexAll->size())
{
cout<<"write error";
return false;
}
return true;
}
vector<string> split(const string& str, const string& delim) {
vector<string> res;
if("" == str) return res;
//先将要切割的字符串从string类型转换为char*类型
char * strs = new char[str.length() + 1] ; //不要忘了
strcpy(strs, str.c_str());
char * d = new char[delim.length() + 1];
strcpy(d, delim.c_str());
char *p = strtok(strs, d);
while(p) {
string s = p; //分割得到的字符串转换为string类型
res.push_back(s); //存入结果数组
p = strtok(NULL, d);
}
return res;
}
string& trim(string &s)
{
if (s.empty())
{
return s;
}
s.erase(0,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
return s;
}
long long GetVal(string &cmd)
{
vector<string> cmds = split(cmd, "=");
cmds[1] = trim(cmds[1]);
long long val;
if(cmds[1].find("0x") != std::string::npos)
{
//cmds[1].erase(cmds[1].begin(), cmds[1].begin()+2);
val = strtoll(cmds[1].c_str(),NULL,16);
}else{
val = strtoll(cmds[1].c_str(),NULL,10);
}
// cout<<">>>string: "<<cmds[1]<<endl;
// printf(">>>out: %X\n",val);
return val;
}
bool CoverData(vector<unsigned char> & headerData, unsigned int offset, vector<unsigned char> & data)
{
if(headerData.size() == 0 || data.size() == 0)
{
return false;
}
headerData.erase(headerData.begin() + offset, headerData.begin() + offset + data.size());
headerData.insert(headerData.begin() + offset, data.begin(), data.begin() + data.size());
return true;
}
bool DoScript(const char* filename, vector<unsigned char> & headerData, const vector<unsigned char> &hexdata)
{
FILE* fp = NULL;
char RowData[256] = { 0 };
if((fp = fopen(filename, "r")) == NULL)
{
cout<<"Script not found!"<<endl;
return false;
}
cout<<"Do Script..."<<endl;
int step = 0;
while (fgets(RowData, 256, fp) != NULL)
{
string cmd(RowData);
if(cmd.find("//") != std::string::npos)
{
continue;
}
if(cmd.find("HexStartAddr") != std::string::npos)
{
//cout<<" SetHexOffSetAddr: "<<"0x"<<hex<<GetVal(cmd)<<endl;
SetHexOffSetAddr(GetVal(cmd));
continue;
}
if(cmd.find("HeaderSize") != std::string::npos)
{
cout<<"Step ";
printf("%d\n",step++);
cout<<" SetHeaderSize: "<<"0x"<<hex<<GetVal(cmd)<<endl;
SetHeaderSize(GetVal(cmd));
continue;
}
if(cmd.find("writedata") != std::string::npos || cmd.find("filldata") != std::string::npos)
{
cout<<"Step ";
printf("%d\n",step++);
//cout<<" writedata: "<<endl;
size_t addr = 0;
size_t lengthCommandLine = 0;
vector<unsigned char> data;
unsigned char buffer[256] = {0};
vector<string> cmds;
cmds = split(cmd,",");
for(auto i : cmds)
{
if(i.find("startaddr") != std::string::npos)
{
addr = GetVal(i);
if(addr < HexOffSetAddr)
{
cout<<"ERROR: startaddr less than Hex start addr!"<<endl;
}
cout<<" startaddr: "<<"0x"<<hex<<addr;
}
if(i.find("length") != std::string::npos)
{
lengthCommandLine = GetVal(i);
//cout<<" length: "<<"0x"<<hex<<lengthCommandLine;
}
if(i.find("writedata") != std::string::npos || i.find("filldata") != std::string::npos)
{
vector<string> cmds_ = split(i, "=");
size_t offset = 0;
size_t len = 0;
cmds_[1] = trim(cmds_[1]);
if(cmds_[1].find("0x") != std::string::npos)
{
offset = 2;
len = (cmds_[1].size()-2) / 2;
}else{
len = cmds_[1].size() / 2;
}
cout<<" length: "<<dec<<len<< " ";
//cout<<cmds_[1].c_str()+ offset;
HexStrToByte(cmds_[1].c_str() + offset, buffer, len * 2);
data.insert(data.begin(), buffer, buffer + len);
if(i.find("writedata") != std::string::npos)
{
cout<<" writedata: ";
for(auto j:data)
{
printf(" 0x%02X",j);
}
if(addr < HexOffSetAddr || addr > (HexOffSetAddr + HeaderSize))
{
cout<<"ERROR: write addr small than hex start addr or over hex header addr!"<<endl;
return false;
}
if(CoverData(headerData,addr - HexOffSetAddr, data) == false)
{
cout<<"ERROR: cover data error!"<<endl;
return false;
}
cout<<endl;
}else{
unsigned char uc = data[0];
cout<<" filldata: ";
printf("0x%02X", uc);
vector<unsigned char> ucdata(lengthCommandLine,uc);
cout<<" length: "<<lengthCommandLine<<endl;
if(addr < HexOffSetAddr || addr > (HexOffSetAddr + HeaderSize))
{
cout<<"ERROR: write addr small than hex start addr or over hex header addr!"<<endl;
return false;
}
if(CoverData(headerData,addr - HexOffSetAddr, ucdata) == false)
{
cout<<"ERROR: cover data error!"<<endl;
return false;
}
cout<<endl;
}
}
}
}//write data
if((cmd.find("CRC16")!=string::npos) || (cmd.find("CRC32")!=string::npos)|| \
(cmd.find("CMAC")!=string::npos) || (cmd.find("SHA256")!=string::npos) ||\
(cmd.find("ECC")!=string::npos))
{
cout<<"Step ";
printf("%d\n",step++);
//std::cout<<cmd<<endl;
vector<unsigned char> dataToEncrypt;
vector<string> cmds = split(cmd,",");
long long length = 0;
unsigned int addr = 0;
vector<unsigned int> vaddr;
vector<unsigned int> vlength;
bool encryptFlag = false;
vector<unsigned char> key;
for(auto ops : cmds)
{
if(ops.find("startaddr") != std::string::npos)
{
addr = GetVal(ops);
if(addr < HexOffSetAddr)
{
cout<<"ERROR: startaddr less than Hex start addr!"<<endl;
return false;
}
//cout<<" startaddr: "<<"0x"<<hex<<addr;
vaddr.push_back(addr);
}
if(ops.find("length") != std::string::npos)
{
length = GetVal(ops);
if(length == -1)
{
length = hexdata.size() - (vaddr.back() - HexOffSetAddr);
}
if(length > hexdata.size())
{
printf("length: %d",length);
cout<<"ERROR: length over hex data size!"<<endl;
return false;
}
vlength.push_back(length);
//cout<<" length: "<<dec<<length;
//cout<<endl;
}
if(ops.find("key") != std::string::npos)
{
vector<string> cmds_ = split(ops, "=");
size_t offset = 0;
size_t len = 0;
cmds_[1] = trim(cmds_[1]);
unsigned char buffer[256] = {0};
if(cmds_[1].find("0x") != std::string::npos )
{
offset = 2;
len = (cmds_[1].size()-2) / 2;
}else{
len = cmds_[1].size() / 2;
}
//cout<<cmds_[1].c_str()+ offset;
HexStrToByte(cmds_[1].c_str() + offset, buffer, len * 2);
key.insert(key.begin(), buffer, buffer + len);
}
for(auto encrypt : EncryptStrs)
{
if(ops.find(encrypt) != string::npos)
{
encryptFlag = true;
}
}
if(encryptFlag == true)
{
if(vaddr.size() != vlength.size())
{
cout<<"ERROR: addr and length not match!"<<endl;
return false;
}
int k = 0;
for(auto addr_ : vaddr)
{
if(addr_ + vlength[k] < HexOffSetAddr + HeaderSize)
{
dataToEncrypt.insert(dataToEncrypt.end(), headerData.begin() + (addr_ - HexOffSetAddr) , headerData.begin() + (addr_ - HexOffSetAddr)+ vlength[k]);
}
else if(addr_ + vlength[k] >= HexOffSetAddr + HeaderSize)
{
dataToEncrypt.insert(dataToEncrypt.end(), hexdata.begin() + (addr_ - HexOffSetAddr) , hexdata.begin() + (addr_ - HexOffSetAddr) + vlength[k]);
}
cout<<" startaddr: "<<"0x"<<hex<<addr_;
cout<<" length: "<<dec<<vlength[k];
if(vlength[k] >= (hexdata.size() - HeaderSize))
{
cout<<" EOF";
}
cout<<endl;
k++;
}
//cout<<" length: "<<dec<<len<< " ";
if(key.size())
{
cout<<" key=";
for(auto byte:key)
{
printf("%02X",byte);
}
cout<<" length="<<key.size();
cout<<endl;
}
if(dataToEncrypt.size() <= HeaderSize)
{
cout<<" encrypt Data add:"<<endl;
k = 0;
cout<<" ";
for(auto byte : dataToEncrypt)
{
printf("%02X",byte);
if((k+1)%16 == 0)
{
cout<<endl;
cout<<" ";
}
if((k+1)%4 == 0 && (k+1)%16 != 0)
{
cout<<" ";
}
k++;
}
cout<<endl;
}else{
cout<<" encrypt Data too long dont show"<<endl;
}
vector<string> tmp = split(ops, "=");
if(tmp.size() != 2)
{
cout<<"ERROR: encrypt algo no argement!"<<endl;
return false;
}
unsigned int encryptWriteAddr = GetVal(ops);
if(encryptWriteAddr > HexOffSetAddr + HeaderSize)
{
cout<<"ERROR: encrypt out data write to hex data!"<<endl;
// cout<<hex<<encryptWriteAddr <<endl;
// cout<<hex<<HexOffSetAddr << endl;
// cout<<hex<< HeaderSize<<endl;
return false;
}
if(encryptWriteAddr < HexOffSetAddr)
{
cout<<"ERROR: encrypt out write addr lower than HexOffSetAddr"<<endl;
return false;
}
cout<<" Do "<<tmp[0]<<" "<<"wrtie addr :"<<hex<<"0x"<<encryptWriteAddr<<endl;
vector<unsigned char> out = DoEncrypt(tmp[0], headerData, dataToEncrypt,key);
if(out.size() == 0)
{
cout<<"ERROR: "<<tmp[0]<< " encrypt error!"<<endl;
return false;
}
cout<<" out: ";
for(auto byte : out)
{
printf("%02X",byte);
}
cout<<endl;
//cout<<"write";
CoverData(headerData, encryptWriteAddr - HexOffSetAddr, out);
encryptFlag = false;
}//Get encrypt data
}//encrypt line loop
vaddr.clear();
vlength.clear();
dataToEncrypt.clear();
key.clear();
}//if ENCRYPT
// if(cmd.find("filldata") != std::string::npos)
// {
// size_t addr = 0;
// size_t len = 0;
// vector<unsigned char> data;
// unsigned char buffer[256] = {0};
// vector<string> cmds;
// cmds = split(cmd,",");
// for(auto op :cmds)
// {
// if(op.find("startaddr") != std::string::npos)
// {
// addr = GetVal(op);
// if(addr < HexOffSetAddr)
// {
// cout<<"ERROR: startaddr less than Hex start addr!"<<endl;
// }
// cout<<" startaddr: "<<"0x"<<hex<<addr;
// }
// if(op.find("length") != std::string::npos)
// {
// len = GetVal(op);
// cout<<" length: "<<"0x"<<hex<<len;
// }
// }
// }//filldata
}//main loop
return true;
}
int ReadScriptHeaderSize(const char* filename)
{
FILE* fp = NULL;
char RowData[256] = { 0 };