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
|
// Scintilla source code edit control
/** @file LexHex.cxx
** Lexer for Motorola S-Record.
**
** Written by Markus Heidelberg
**/
// Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
/*
* Motorola S-Record file format
* ===============================
*
* Each record (line) is built as follows:
*
* field digits states
*
* +----------+
* | start | 1 ('S') SCE_HEX_RECSTART
* +----------+
* | type | 1 SCE_HEX_RECTYPE
* +----------+
* | count | 2 SCE_HEX_BYTECOUNT, SCE_HEX_BYTECOUNT_WRONG
* +----------+
* | address | 4/6/8 SCE_HEX_NOADDRESS, SCE_HEX_DATAADDRESS, SCE_HEX_RECCOUNT, SCE_HEX_STARTADDRESS, (SCE_HEX_ADDRESSFIELD_UNKNOWN)
* +----------+
* | data | 0..504/502/500 SCE_HEX_DATA_ODD, SCE_HEX_DATA_EVEN, (SCE_HEX_DATA_UNKNOWN)
* +----------+
* | checksum | 2 SCE_HEX_CHECKSUM, SCE_HEX_CHECKSUM_WRONG
* +----------+
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <ctype.h>
#include "ILexer.h"
#include "Scintilla.h"
#include "SciLexer.h"
#include "WordList.h"
#include "LexAccessor.h"
#include "Accessor.h"
#include "StyleContext.h"
#include "CharacterSet.h"
#include "LexerModule.h"
#ifdef SCI_NAMESPACE
using namespace Scintilla;
#endif
// prototypes for general helper functions
static inline bool IsNewline(const int ch);
static int GetHexaChar(char hd1, char hd2);
static int GetHexaChar(unsigned int pos, Accessor &styler);
static bool ForwardWithinLine(StyleContext &sc, int nb = 1);
static int CountByteCount(unsigned int startPos, int uncountedDigits, Accessor &styler);
static int CalcChecksum(unsigned int startPos, int cnt, bool twosCompl, Accessor &styler);
// prototypes for file format specific helper functions
static unsigned int GetSrecRecStartPosition(unsigned int pos, Accessor &styler);
static int GetSrecByteCount(unsigned int recStartPos, Accessor &styler);
static int CountSrecByteCount(unsigned int recStartPos, Accessor &styler);
static int GetSrecAddressFieldSize(unsigned int recStartPos, Accessor &styler);
static int GetSrecAddressFieldType(unsigned int recStartPos, Accessor &styler);
static int GetSrecChecksum(unsigned int recStartPos, Accessor &styler);
static int CalcSrecChecksum(unsigned int recStartPos, Accessor &styler);
static inline bool IsNewline(const int ch)
{
return (ch == '\n' || ch == '\r');
}
static int GetHexaChar(char hd1, char hd2)
{
int hexValue = 0;
if (hd1 >= '0' && hd1 <= '9') {
hexValue += 16 * (hd1 - '0');
} else if (hd1 >= 'A' && hd1 <= 'F') {
hexValue += 16 * (hd1 - 'A' + 10);
} else if (hd1 >= 'a' && hd1 <= 'f') {
hexValue += 16 * (hd1 - 'a' + 10);
} else {
return -1;
}
if (hd2 >= '0' && hd2 <= '9') {
hexValue += hd2 - '0';
} else if (hd2 >= 'A' && hd2 <= 'F') {
hexValue += hd2 - 'A' + 10;
} else if (hd2 >= 'a' && hd2 <= 'f') {
hexValue += hd2 - 'a' + 10;
} else {
return -1;
}
return hexValue;
}
static int GetHexaChar(unsigned int pos, Accessor &styler)
{
char highNibble, lowNibble;
highNibble = styler.SafeGetCharAt(pos);
lowNibble = styler.SafeGetCharAt(pos + 1);
return GetHexaChar(highNibble, lowNibble);
}
// Forward <nb> characters, but abort (and return false) if hitting the line
// end. Return true if forwarding within the line was possible.
// Avoids influence on highlighting of the subsequent line if the current line
// is malformed (too short).
static bool ForwardWithinLine(StyleContext &sc, int nb)
{
for (int i = 0; i < nb; i++) {
if (sc.atLineEnd) {
// line is too short
sc.SetState(SCE_HEX_DEFAULT);
sc.Forward();
return false;
} else {
sc.Forward();
}
}
return true;
}
// Count the number of digit pairs from <startPos> till end of record, ignoring
// <uncountedDigits> digits.
// If the record is too short, a negative count may be returned.
static int CountByteCount(unsigned int startPos, int uncountedDigits, Accessor &styler)
{
int cnt;
unsigned int pos;
pos = startPos;
while (!IsNewline(styler.SafeGetCharAt(pos, '\n'))) {
pos++;
}
// number of digits in this line minus number of digits of uncounted fields
cnt = static_cast<int>(pos - startPos) - uncountedDigits;
// Prepare round up if odd (digit pair incomplete), this way the byte
// count is considered to be valid if the checksum is incomplete.
if (cnt >= 0) {
cnt++;
}
// digit pairs
cnt /= 2;
return cnt;
}
// Calculate the checksum of the record.
// <startPos> is the position of the first character of the starting digit
// pair, <cnt> is the number of digit pairs.
static int CalcChecksum(unsigned int startPos, int cnt, bool twosCompl, Accessor &styler)
{
int cs = 0;
for (unsigned int pos = startPos; pos < startPos + cnt; pos += 2) {
int val = GetHexaChar(pos, styler);
if (val < 0) {
return val;
}
// overflow does not matter
cs += val;
}
if (twosCompl) {
// low byte of two's complement
return -cs & 0xFF;
} else {
// low byte of one's complement
return ~cs & 0xFF;
}
}
// Get the position of the record "start" field (first character in line) in
// the record around position <pos>.
static unsigned int GetSrecRecStartPosition(unsigned int pos, Accessor &styler)
{
while (styler.SafeGetCharAt(pos) != 'S') {
pos--;
}
return pos;
}
// Get the value of the "byte count" field, it counts the number of bytes in
// the subsequent fields ("address", "data" and "checksum" fields).
static int GetSrecByteCount(unsigned int recStartPos, Accessor &styler)
{
int val;
val = GetHexaChar(recStartPos + 2, styler);
if (val < 0) {
val = 0;
}
return val;
}
// Count the number of digit pairs for the "address", "data" and "checksum"
// fields in this record. Has to be equal to the "byte count" field value.
// If the record is too short, a negative count may be returned.
static int CountSrecByteCount(unsigned int recStartPos, Accessor &styler)
{
return CountByteCount(recStartPos, 4, styler);
}
// Get the size of the "address" field.
static int GetSrecAddressFieldSize(unsigned int recStartPos, Accessor &styler)
{
switch (styler.SafeGetCharAt(recStartPos + 1)) {
case '0':
case '1':
case '5':
case '9':
return 2; // 16 bit
case '2':
case '6':
case '8':
return 3; // 24 bit
case '3':
case '7':
return 4; // 32 bit
default:
return 0;
}
}
// Get the type of the "address" field content.
static int GetSrecAddressFieldType(unsigned int recStartPos, Accessor &styler)
{
switch (styler.SafeGetCharAt(recStartPos + 1)) {
case '0':
return SCE_HEX_NOADDRESS;
case '1':
case '2':
case '3':
return SCE_HEX_DATAADDRESS;
case '5':
case '6':
return SCE_HEX_RECCOUNT;
case '7':
case '8':
case '9':
return SCE_HEX_STARTADDRESS;
default: // handle possible format extension in the future
return SCE_HEX_ADDRESSFIELD_UNKNOWN;
}
}
// Get the value of the "checksum" field.
static int GetSrecChecksum(unsigned int recStartPos, Accessor &styler)
{
int byteCount;
byteCount = GetSrecByteCount(recStartPos, styler);
return GetHexaChar(recStartPos + 2 + byteCount * 2, styler);
}
// Calculate the checksum of the record.
static int CalcSrecChecksum(unsigned int recStartPos, Accessor &styler)
{
int byteCount;
byteCount = GetSrecByteCount(recStartPos, styler);
// sum over "byte count", "address" and "data" fields (6..510 digits)
return CalcChecksum(recStartPos + 2, byteCount * 2, false, styler);
}
static void ColouriseSrecDoc(unsigned int startPos, int length, int initStyle, WordList *[], Accessor &styler)
{
StyleContext sc(startPos, length, initStyle, styler);
while (sc.More()) {
unsigned int recStartPos;
int byteCount, addrFieldSize, addrFieldType, dataFieldSize;
int cs1, cs2;
switch (sc.state) {
case SCE_HEX_DEFAULT:
if (sc.atLineStart && sc.Match('S')) {
sc.SetState(SCE_HEX_RECSTART);
}
ForwardWithinLine(sc);
break;
case SCE_HEX_RECSTART:
sc.SetState(SCE_HEX_RECTYPE);
ForwardWithinLine(sc);
break;
case SCE_HEX_RECTYPE:
recStartPos = sc.currentPos - 2;
byteCount = GetSrecByteCount(recStartPos, styler);
if (byteCount == CountSrecByteCount(recStartPos, styler)) {
sc.SetState(SCE_HEX_BYTECOUNT);
} else {
sc.SetState(SCE_HEX_BYTECOUNT_WRONG);
}
ForwardWithinLine(sc, 2);
break;
case SCE_HEX_BYTECOUNT:
case SCE_HEX_BYTECOUNT_WRONG:
recStartPos = sc.currentPos - 4;
addrFieldSize = GetSrecAddressFieldSize(recStartPos, styler);
addrFieldType = GetSrecAddressFieldType(recStartPos, styler);
sc.SetState(addrFieldType);
ForwardWithinLine(sc, addrFieldSize * 2);
break;
case SCE_HEX_NOADDRESS:
case SCE_HEX_DATAADDRESS:
case SCE_HEX_RECCOUNT:
case SCE_HEX_STARTADDRESS:
case SCE_HEX_ADDRESSFIELD_UNKNOWN:
recStartPos = GetSrecRecStartPosition(sc.currentPos, styler);
byteCount = GetSrecByteCount(recStartPos, styler);
addrFieldSize = GetSrecAddressFieldSize(recStartPos, styler);
dataFieldSize = byteCount - addrFieldSize - 1; // -1 for checksum field
if (sc.state == SCE_HEX_ADDRESSFIELD_UNKNOWN) {
sc.SetState(SCE_HEX_DATA_UNKNOWN);
ForwardWithinLine(sc, dataFieldSize * 2);
break;
}
sc.SetState(SCE_HEX_DATA_ODD);
for (int i = 0; i < dataFieldSize * 2; i++) {
if ((i & 0x3) == 0) {
sc.SetState(SCE_HEX_DATA_ODD);
} else if ((i & 0x3) == 2) {
sc.SetState(SCE_HEX_DATA_EVEN);
}
if (!ForwardWithinLine(sc)) {
break;
}
}
break;
case SCE_HEX_DATA_ODD:
case SCE_HEX_DATA_EVEN:
case SCE_HEX_DATA_UNKNOWN:
recStartPos = GetSrecRecStartPosition(sc.currentPos, styler);
cs1 = CalcSrecChecksum(recStartPos, styler);
cs2 = GetSrecChecksum(recStartPos, styler);
if (cs1 != cs2 || cs1 < 0 || cs2 < 0) {
sc.SetState(SCE_HEX_CHECKSUM_WRONG);
} else {
sc.SetState(SCE_HEX_CHECKSUM);
}
ForwardWithinLine(sc, 2);
break;
case SCE_HEX_CHECKSUM:
case SCE_HEX_CHECKSUM_WRONG:
// record finished
sc.SetState(SCE_HEX_DEFAULT);
ForwardWithinLine(sc);
break;
}
}
sc.Complete();
}
LexerModule lmSrec(SCLEX_SREC, ColouriseSrecDoc, "srec", 0, NULL);
|