-
Notifications
You must be signed in to change notification settings - Fork 13
/
intelhexclass.cpp
1140 lines (950 loc) · 48.3 KB
/
intelhexclass.cpp
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
/*******************************************************************************
*
* INTEL HEX FILE CLASS MODULE
*
*******************************************************************************/
/******************************************************************************/
/*!
* \file intelhexclass.cpp
* \mainpage
* \image html intelhexclass.png
* \image latex intelhexclass.eps
* \section intro Introduction
* The Intel HEX File class module is designed to encode, decode and manipulate
* the content of Intel HEX format files commonly generated by most toolchains
* for embedded processors and microcontrollers.
*
* It uses standard C++ streams to decode files and store them in memory, and
* encode data stored in memory back into an Intel HEX format file. Once the file
* content is in memory, the content can then be manipulated using the available
* API.
*
* With this class it is possible to create tools that can compare Intel HEX
* files, fill empty space with desired values, splice two or more files
* together to name a few possibilities.
*
* \section contactInfo Contact Information
* For more information and the latest release, please visit this projects home
* page at http://codinghead.github.com/Intel-HEX-Class
* To participate in the project or for other enquiries, please contact Stuart
* Cording at codinghead@gmail.com
*
* \section license Licensing Information
* Copyright (c) 2012 Stuart Cording
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* \section imageInfo Image Information
* Image chosen for this project comes from 'Henkster'. Original image is from
* http://www.sxc.hu/photo/504350 on stock.xchng.
*
* \author Stuart Cording aka CODINGHEAD
*
********************************************************************************
* \note
* No notes to date (19th Jan 2012)
*******************************************************************************/
#include <iostream>
#include <string>
#include <vector>
#ifdef _MSC_FULL_VER
#include <stdio.h>
#else
#include <cstdio>
#endif
#include "intelhexclass.h"
using namespace std;
/******************************************************************************/
/*! Possible record types for Intel HEX file.
*
* List of all possible record types that can be found in an Intel HEX file.
*******************************************************************************/
enum intelhexRecordType {
DATA_RECORD,
END_OF_FILE_RECORD,
EXTENDED_SEGMENT_ADDRESS,
START_SEGMENT_ADDRESS,
EXTENDED_LINEAR_ADDRESS,
START_LINEAR_ADDRESS,
NO_OF_RECORD_TYPES
};
/*******************************************************************************
* Converts a 2 char string to its HEX value
*******************************************************************************/
unsigned char intelhex::stringToHex(string value)
{
unsigned char returnValue = 0;
string::iterator valueIterator;
if(value.length() == 2)
{
valueIterator = value.begin();
for (int x=0; x < 2; x++)
{
/* Shift result variable 4 bits to the left */
returnValue <<= 4;
if (*valueIterator >= '0' && *valueIterator <= '9')
{
returnValue +=
static_cast<unsigned char>(*valueIterator - '0');
}
else if (*valueIterator >= 'A' && *valueIterator <= 'F')
{
returnValue +=
static_cast<unsigned char>(*valueIterator - 'A' + 10);
}
else if (*valueIterator >= 'a' && *valueIterator <= 'f')
{
returnValue +=
static_cast<unsigned char>(*valueIterator - 'a' + 10);
}
else
{
/* Error occured - non-HEX value found */
string message;
message = "Can't convert byte 0x" + value + " @ 0x" +
ulToHexString(segmentBaseAddress) + " to hex.";
addError(message);
returnValue = 0;
}
/* Iterate to next char in the string */
++valueIterator;
}
}
else
{
/* Error occured - more or less than two nibbles in the string */
string message;
message = value + " @ 0x" + ulToHexString(segmentBaseAddress) +
" isn't an 8-bit value.";
addError(message);
}
return returnValue;
}
/*******************************************************************************
* Converts an unsigned long to a string in HEX format
*******************************************************************************/
string intelhex::ulToHexString(unsigned long value)
{
string returnString;
char localString[50];
returnString.erase();
#ifdef _MSC_FULL_VER
sprintf_s(localString, 49, "%08lX", value);
#else
snprintf(localString, 49, "%08lX", value);
#endif
returnString.insert(0, localString);
return returnString;
}
/*******************************************************************************
* Converts an unsigned long to a string in DEC format
*******************************************************************************/
string intelhex::ulToString(unsigned long value)
{
string returnString;
char localString[50];
returnString.erase();
#ifdef _MSC_FULL_VER
sprintf_s(localString, 49, "%lu", value);
#else
snprintf(localString, 49, "%lu", value);
#endif
returnString.insert(0, localString);
return returnString;
}
/*******************************************************************************
* Converts an unsigned char to a string in HEX format
*******************************************************************************/
string intelhex::ucToHexString(unsigned char value)
{
string returnString;
char localString[50];
returnString.erase();
#ifdef _MSC_FULL_VER
sprintf_s(localString, 49, "%02X", value);
#else
snprintf(localString, 49, "%02X", value);
#endif
returnString.insert(0, localString);
return returnString;
}
/*******************************************************************************
* Adds a warning to the list of warning messages
*******************************************************************************/
void intelhex::addWarning(string warningMessage)
{
string localMessage;
/* Build the message and push the warning message onto the list */
localMessage += ulToString(msgWarning.noOfWarnings + 1) + " Warning: "
+ warningMessage;
msgWarning.ihWarnings.push_back(localMessage);
/* Update the number of warning messages */
msgWarning.noOfWarnings = msgWarning.ihWarnings.size();
}
/*******************************************************************************
* Adds an error to the list of error messages
*******************************************************************************/
void intelhex::addError(string errorMessage)
{
string localMessage;
/* Build the message and push the error message onto the list */
localMessage += ulToString(msgError.noOfErrors + 1) + " Error: "
+ errorMessage;
msgError.ihErrors.push_back(localMessage);
/* Update the number of error messages */
msgError.noOfErrors = msgError.ihErrors.size();
}
/*******************************************************************************
* Decodes a data record read in from a file
*******************************************************************************/
void intelhex::decodeDataRecord(unsigned char recordLength,
unsigned long loadOffset,
string::const_iterator data)
{
/* Variable to store a byte of the record as a two char string */
string sByteRead;
/* Variable to store the byte of the record as an u.char */
unsigned char byteRead;
/* Calculate new SBA by clearing the low four bytes and then adding the */
/* current loadOffset for this line of Intel HEX data */
segmentBaseAddress &= ~(0xFFFFUL);
segmentBaseAddress += loadOffset;
for (unsigned char x = 0; x < recordLength; x ++)
{
sByteRead.erase();
sByteRead = *data;
data++;
sByteRead += *data;
data++;
byteRead = stringToHex(sByteRead);
ihReturn=ihContent.insert(
pair<int,unsigned char>(segmentBaseAddress, byteRead));
if (ihReturn.second==false)
{
/* If this address already contains the byte we are trying to */
/* write, this is only a warning */
if (ihReturn.first->second == byteRead)
{
string message;
message = "Location 0x" + ulToHexString(segmentBaseAddress) +
" already contains data 0x" + sByteRead;
addWarning(message);
}
/* Otherwise this is an error */
else
{
string message;
message = "Couldn't add 0x" + sByteRead + " @ 0x" +
ulToHexString(segmentBaseAddress) +
"; already contains 0x" +
ucToHexString(ihReturn.first->second);
addError(message);
}
}
/* Increment the segment base address */
++segmentBaseAddress;
}
}
/*******************************************************************************
* Input Stream for Intel HEX File Decoding (friend function)
*******************************************************************************/
istream& operator>>(istream& dataIn, intelhex& ihLocal)
{
// Create a string to store lines of Intel Hex info
string ihLine;
/* Create a string to store a single byte of Intel HEX info */
string ihByte;
// Create an iterator for this variable
string::iterator ihLineIterator;
// Create a line counter
unsigned long lineCounter = 0;
// Variable to hold a single byte (two chars) of data
unsigned char byteRead;
// Variable to calculate the checksum for each line
unsigned char intelHexChecksum;
// Variable to hold the record length
unsigned char recordLength;
// Variable to hold the load offset
unsigned long loadOffset;
// Variables to hold the record type
intelhexRecordType recordType;
do
{
/* Clear the string before this next round */
ihLine.erase();
/* Clear the checksum before processing this line */
intelHexChecksum = 0;
/* Get a line of data */
dataIn >> ihLine;
/* If the line contained some data, process it */
if (ihLine.length() > 0)
{
/* Increment line counter */
lineCounter++;
/* Set string iterator to start of string */
ihLineIterator = ihLine.begin();
/* Check that we have a ':' record mark at the beginning */
if (*ihLineIterator != ':')
{
/* Add some warning code here */
string message;
message = "Line without record mark ':' found @ line " +
ihLocal.ulToString(lineCounter);
ihLocal.addWarning(message);
/* If this is the first line, let's simply give up. Chances */
/* are this is not an Intel HEX file at all */
if (lineCounter == 1)
{
message = "Intel HEX File decode aborted; ':' missing in " \
"first line.";
ihLocal.addError(message);
/* Erase ihLine content and break out of do...while loop */
ihLine.erase();
break;
}
}
else
{
/* Remove the record mark from the string as we don't need it */
/* anymore */
ihLine.erase(ihLineIterator);
}
/* Run through the whole line to check the checksum */
for (ihLineIterator = ihLine.begin();
ihLineIterator != ihLine.end();
/* Nothing - really! */ )
{
/* Convert the line in pair of chars (making a single byte) */
/* into single bytes, and then add to the checksum variable. */
/* By adding all the bytes in a line together *including* the */
/* checksum byte, we should get a result of '0' at the end. */
/* If not, there is a checksum error */
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
/* Just in case there are an odd number of chars in the */
/* just check we didn't reach the end of the string early */
if (ihLineIterator != ihLine.end())
{
ihByte += *ihLineIterator;
++ihLineIterator;
byteRead = ihLocal.stringToHex(ihByte);
intelHexChecksum += byteRead;
}
else
{
string message;
message = "Odd number of characters in line " +
ihLocal.ulToString(lineCounter);
ihLocal.addError(message);
}
}
/* Make sure the checksum was ok */
if (intelHexChecksum == 0)
{
/* Reset iterator back to beginning of the line so we can now */
/* decode it */
ihLineIterator = ihLine.begin();
/* Clear all the variables associated with decoding a line of */
/* Intel HEX code. */
recordLength = 0;
loadOffset = 0;
/* Get the record length */
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
recordLength = ihLocal.stringToHex(ihByte);
/* Get the load offset (2 bytes) */
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
loadOffset =
static_cast<unsigned long>(ihLocal.stringToHex(ihByte));
loadOffset <<= 8;
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
loadOffset +=
static_cast<unsigned long>(ihLocal.stringToHex(ihByte));
/* Get the record type */
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
recordType =
static_cast<intelhexRecordType>(ihLocal.stringToHex(ihByte));
/* Decode the INFO or DATA portion of the record */
switch (recordType)
{
case DATA_RECORD:
ihLocal.decodeDataRecord(recordLength, loadOffset,
ihLineIterator);
if (ihLocal.verbose == true)
{
cout << "Data Record begining @ 0x" <<
ihLocal.ulToHexString(loadOffset) << endl;
}
break;
case END_OF_FILE_RECORD:
/* Check that the EOF record wasn't already found. If */
/* it was, generate appropriate error */
if (ihLocal.foundEof == false)
{
ihLocal.foundEof = true;
}
else
{
string message;
message = "Additional End Of File record @ line " +
ihLocal.ulToString(lineCounter) +
" found.";
ihLocal.addError(message);
}
/* Generate error if there were */
if (ihLocal.verbose == true)
{
cout << "End of File" << endl;
}
break;
case EXTENDED_SEGMENT_ADDRESS:
/* Make sure we have 2 bytes of data */
if (recordLength == 2)
{
/* Extract the two bytes of the ESA */
unsigned long extSegAddress = 0;
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
extSegAddress = static_cast<unsigned long>
(ihLocal.stringToHex(ihByte));
extSegAddress <<= 8;
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
extSegAddress += static_cast<unsigned long>
(ihLocal.stringToHex(ihByte));
/* ESA is bits 4-19 of the segment base address */
/* (SBA), so shift left 4 bits */
extSegAddress <<= 4;
/* Update the SBA */
ihLocal.segmentBaseAddress = extSegAddress;
}
else
{
/* Note the error */
string message;
message = "Extended Segment Address @ line " +
ihLocal.ulToString(lineCounter) +
" not 2 bytes as required.";
ihLocal.addError(message);
}
if (ihLocal.verbose == true)
{
cout << "Ext. Seg. Address found: 0x" <<
ihLocal.ulToHexString(ihLocal.segmentBaseAddress)
<< endl;
}
break;
case START_SEGMENT_ADDRESS:
/* Make sure we have 4 bytes of data, and that no */
/* Start Segment Address has been found to date */
if (recordLength == 4 &&
ihLocal.startSegmentAddress.exists == false)
{
/* Note that the Start Segment Address has been */
/* found. */
ihLocal.startSegmentAddress.exists = true;
/* Clear the two registers, just in case */
ihLocal.startSegmentAddress.csRegister = 0;
ihLocal.startSegmentAddress.ipRegister = 0;
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
ihLocal.startSegmentAddress.csRegister =
static_cast<unsigned long>
(ihLocal.stringToHex(ihByte));
ihLocal.startSegmentAddress.csRegister <<= 8;
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
ihLocal.startSegmentAddress.csRegister +=
static_cast<unsigned long>
(ihLocal.stringToHex(ihByte));
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
ihLocal.startSegmentAddress.ipRegister =
static_cast<unsigned long>
(ihLocal.stringToHex(ihByte));
ihLocal.startSegmentAddress.ipRegister <<= 8;
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
ihLocal.startSegmentAddress.ipRegister +=
static_cast<unsigned long>
(ihLocal.stringToHex(ihByte));
}
/* Note an error if the start seg. address already */
/* exists */
else if (ihLocal.startSegmentAddress.exists == true)
{
string message;
message = "Start Segment Address record appears again @ line " +
ihLocal.ulToString(lineCounter) +
"; repeated record ignored.";
ihLocal.addError(message);
}
/* Note an error if the start lin. address already */
/* exists as they should be mutually exclusive */
if (ihLocal.startLinearAddress.exists == true)
{
string message;
message = "Start Segment Address record found @ line " +
ihLocal.ulToString(lineCounter) +
" but Start Linear Address already exists.";
ihLocal.addError(message);
}
/* Note an error if the record lenght is not 4 as */
/* expected */
if (recordLength != 4)
{
string message;
message = "Start Segment Address @ line " +
ihLocal.ulToString(lineCounter) +
" not 4 bytes as required.";
ihLocal.addError(message);
}
if (ihLocal.verbose == true)
{
cout << "Start Seg. Address - CS 0x" <<
ihLocal.ulToHexString(ihLocal.startSegmentAddress.csRegister) <<
" IP 0x" <<
ihLocal.ulToHexString(ihLocal.startSegmentAddress.ipRegister)
<< endl;
}
break;
case EXTENDED_LINEAR_ADDRESS:
/* Make sure we have 2 bytes of data */
if (recordLength == 2)
{
/* Extract the two bytes of the ELA */
unsigned long extLinAddress = 0;
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
extLinAddress = static_cast<unsigned long>
(ihLocal.stringToHex(ihByte));
extLinAddress <<= 8;
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
extLinAddress += static_cast<unsigned long>
(ihLocal.stringToHex(ihByte));
/* ELA is bits 16-31 of the segment base address */
/* (SBA), so shift left 16 bits */
extLinAddress <<= 16;
/* Update the SBA */
ihLocal.segmentBaseAddress = extLinAddress;
}
else
{
/* Note the error */
//cout << "Error in Ext. Lin. Address" << endl;
string message;
message = "Extended Linear Address @ line " +
ihLocal.ulToString(lineCounter) +
" not 2 bytes as required.";
ihLocal.addError(message);
}
if (ihLocal.verbose == true)
{
cout << "Ext. Lin. Address 0x" <<
ihLocal.ulToHexString(ihLocal.segmentBaseAddress)
<< endl;
}
break;
case START_LINEAR_ADDRESS:
/* Make sure we have 4 bytes of data */
if (recordLength == 4 &&
ihLocal.startLinearAddress.exists == false)
{
/* Extract the four bytes of the SLA */
ihLocal.startLinearAddress.eipRegister = 0;
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
ihLocal.startLinearAddress.eipRegister =
static_cast<unsigned long>
(ihLocal.stringToHex(ihByte));
ihLocal.startLinearAddress.eipRegister <<= 8;
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
ihLocal.startLinearAddress.eipRegister +=
static_cast<unsigned long>
(ihLocal.stringToHex(ihByte));
ihLocal.startLinearAddress.eipRegister <<= 8;
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
ihLocal.startLinearAddress.eipRegister +=
static_cast<unsigned long>
(ihLocal.stringToHex(ihByte));
ihLocal.startLinearAddress.eipRegister <<= 8;
ihByte.erase();
ihByte = *ihLineIterator;
++ihLineIterator;
ihByte += *ihLineIterator;
++ihLineIterator;
ihLocal.startLinearAddress.eipRegister +=
static_cast<unsigned long>
(ihLocal.stringToHex(ihByte));
}
/* Note an error if the start seg. address already */
/* exists */
else if (ihLocal.startLinearAddress.exists == true)
{
string message;
message = "Start Linear Address record appears again @ line " +
ihLocal.ulToString(lineCounter) +
"; repeated record ignored.";
ihLocal.addError(message);
}
/* Note an error if the start seg. address already */
/* exists as they should be mutually exclusive */
if (ihLocal.startSegmentAddress.exists == true)
{
string message;
message = "Start Linear Address record found @ line " +
ihLocal.ulToString(lineCounter) +
" but Start Segment Address already exists.";
ihLocal.addError(message);
}
/* Note an error if the record lenght is not 4 as */
/* expected */
if (recordLength != 4)
{
string message;
message = "Start Linear Address @ line " +
ihLocal.ulToString(lineCounter) +
" not 4 bytes as required.";
ihLocal.addError(message);
}
if (ihLocal.verbose == true)
{
cout << "Start Lin. Address - EIP 0x" <<
ihLocal.ulToHexString(ihLocal.startLinearAddress.eipRegister)
<< endl;
}
break;
default:
/* Handle the error here */
if (ihLocal.verbose == true)
{
cout << "Unknown Record @ line " <<
ihLocal.ulToString(lineCounter) << endl;
}
string message;
message = "Unknown Intel HEX record @ line " +
ihLocal.ulToString(lineCounter);
ihLocal.addError(message);
break;
}
}
else
{
/* Note that the checksum contained an error */
string message;
message = "Checksum error @ line " +
ihLocal.ulToString(lineCounter) +
"; calculated 0x" +
ihLocal.ucToHexString(intelHexChecksum - byteRead) +
" expected 0x" +
ihLocal.ucToHexString(byteRead);
ihLocal.addError(message);
}
}
} while (ihLine.length() > 0);
if (ihLocal.verbose == true)
{
cout << "Decoded " << lineCounter << " lines from file." << endl;
}
return(dataIn);
}
/*******************************************************************************
* Output Stream for Intel HEX File Encoding (friend function)
*******************************************************************************/
ostream& operator<<(ostream& dataOut, intelhex& ihLocal)
{
/* Stores the address offset needed by the linear/segment address records */
unsigned long addressOffset;
/* Iterator into the ihContent - where the addresses & data are stored */
map<unsigned long, unsigned char>::iterator ihIterator;
/* Holds string that represents next record to be written */
string thisRecord;
/* Checksum calculation variable */
unsigned char checksum;
thisRecord.clear();
/* Check that there is some content to encode */
if (ihLocal.ihContent.size() > 0)
{
/* Calculate the Linear/Segment address */
ihIterator = ihLocal.ihContent.begin();
addressOffset = (*ihIterator).first;
checksum = 0;
/* Construct the first record to define the segment base address */
if (ihLocal.segmentAddressMode == false)
{
unsigned char dataByte;
addressOffset >>= 16;
thisRecord = ":02000004";
checksum = 0x02 + 0x04;
dataByte = static_cast<unsigned char>(addressOffset & 0xFF);
checksum += dataByte;
thisRecord += ihLocal.ucToHexString(dataByte);
dataByte = static_cast<unsigned char>((addressOffset >> 8) & 0xFF);
checksum += dataByte;
thisRecord += ihLocal.ucToHexString(dataByte);
thisRecord += ihLocal.ucToHexString(0x00 - (checksum & 0xFF));
}
else
{
unsigned char dataByte;
addressOffset >>= 4;
thisRecord = ":02000002";
checksum = 0x02 + 0x02;
dataByte = static_cast<unsigned char>(addressOffset & 0xFF);
checksum += dataByte;
thisRecord += ihLocal.ucToHexString(dataByte);
dataByte = static_cast<unsigned char>((addressOffset >> 8) & 0xFF);
checksum += dataByte;
thisRecord += ihLocal.ucToHexString(dataByte);
thisRecord += ihLocal.ucToHexString(0x00 - (checksum & 0xFF));
}
/* Output the record */
dataOut << thisRecord << endl;
/* Now loop through all the available data and insert into file */
/* with maximum 16 bytes per line, and making sure to keep the */
/* segment base address up to date */
vector<unsigned char> recordData;
unsigned long previousAddress;
unsigned long currentAddress;
unsigned long loadOffset;
while(ihIterator != ihLocal.ihContent.end())
{
/* Check to see if we need to start a new linear/segment section */
loadOffset = (*ihIterator).first;
/* If we are using the linear mode... */
if (ihLocal.segmentAddressMode == false)
{
if ((loadOffset >> 16) != addressOffset)
{
unsigned char dataByte;
thisRecord.clear();
checksum = 0;
addressOffset = loadOffset;
addressOffset >>= 16;
thisRecord = ":02000004";
checksum = 0x02 + 0x04;
dataByte = static_cast<unsigned char>(addressOffset & 0xFF);
checksum += dataByte;
thisRecord += ihLocal.ucToHexString(dataByte);
dataByte = static_cast<unsigned char>((addressOffset >> 8) & 0xFF);
checksum += dataByte;
thisRecord += ihLocal.ucToHexString(dataByte);
thisRecord += ihLocal.ucToHexString(0x00 - (checksum & 0xFF));
/* Output the record */
dataOut << thisRecord << endl;
}
}
/* ...otherwise assume segment mode */
else
{
if ((loadOffset >> 4) != addressOffset)
{
unsigned char dataByte;
thisRecord.clear();
checksum = 0;
addressOffset = loadOffset;
addressOffset >>= 4;
thisRecord = ":02000002";
checksum = 0x02 + 0x02;
dataByte = static_cast<unsigned char>(addressOffset & 0xFF);
checksum += dataByte;
thisRecord += ihLocal.ucToHexString(dataByte);
dataByte = static_cast<unsigned char>((addressOffset >> 8) & 0xFF);
checksum += dataByte;
thisRecord += ihLocal.ucToHexString(dataByte);
thisRecord += ihLocal.ucToHexString(0x00 - (checksum & 0xFF));
/* Output the record */
dataOut << thisRecord << endl;
}
}
/* Prepare for encoding next data record */
thisRecord.clear();
checksum = 0;
recordData.clear();
/* We need to check where the data actually starts, but only the */
/* bottom 16-bits; the other bits are in the segment/linear */
/* address record */
loadOffset = (*ihIterator).first & 0xFFFF;
/* Loop through and collect up to 16 bytes of data */
for (int x = 0; x < 16; x++)
{
currentAddress = (*ihIterator).first & 0xFFFF;
recordData.push_back((*ihIterator).second);
ihIterator++;
/* Check that we haven't run out of data */
if (ihIterator == ihLocal.ihContent.end())