This file is indexed.

/usr/include/lime/MCU_File.h is in liblimesuite-dev 17.12.0+dfsg-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>

#if !(defined(max)) && _MSC_VER
	// VC fix
	#define max __max
#endif

using namespace std;

class MemBlock
{
public:
	unsigned long m_startAddress;
	vector<unsigned char> m_bytes;
};

class MCU_File
{
public:
	explicit MCU_File(const char *fileName, const char *mode)
	{
		m_file = fopen(fileName, mode);
		if (m_file != NULL)
		{
			return;
		}

		cout << "Error opening";
		//string errorStr = "Error opening ";
		//errorStr += fileName;
		//errorStr += "\n";
		//throw errorStr;
	}

	~MCU_File()
	{
        if (m_file)
		    fclose(m_file);
	}

    bool FileOpened()
    {
        return m_file != NULL;
    }

	// Read binary file
	void ReadBin(unsigned long limit)
	{
		m_top = 0;

		m_chunks.push_back(MemBlock());
		m_chunks.back().m_startAddress = 0;

		cout << "Reading binary file\n";

		int tmp = fgetc(m_file);

		while (!feof(m_file))
		{
			m_chunks.back().m_bytes.push_back(tmp);

			if (m_chunks.back().m_bytes.size() > limit + 1)
			{
				m_chunks.back().m_bytes.pop_back();
				m_top = m_chunks.back().m_bytes.size() - 1;
				cout << "Ignoring data above address space!\n";
				cout << " Limit: " << limit << "\n";
				return;
			}

			tmp = fgetc(m_file);
		}

		m_top = m_chunks.back().m_bytes.size() - 1;

		if (!m_chunks.back().m_bytes.size())
		{
			cout << "No data!\n";

			m_chunks.pop_back();
		}
	}

	// Read hex file
	void ReadHex(unsigned long limit)
	{
		char szLine[1024];
		bool formatDetected = false;
		bool intel = false;
		bool endSeen = false;
		bool linear = true;				// Only used for intel hex
		unsigned long addressBase = 0;	// Only used for intel hex
		unsigned long dataRecords = 0;	// Only used for s-record
		while (!feof(m_file))
		{
			if (fgets(szLine, 1024, m_file) == 0)
			{
				if (ferror(m_file))
				{
					throw "Error reading input!\n";
				}
				continue;
			}

			if (szLine[strlen(szLine) - 1] == 0xA || szLine[strlen(szLine) - 1] == 0xD)
			{
				szLine[strlen(szLine) - 1] = 0;
			}

			if (szLine[strlen(szLine) - 1] == 0xA || szLine[strlen(szLine) - 1] == 0xD)
			{
				szLine[strlen(szLine) - 1] = 0;
			}

			if (strlen(szLine) == 1023)
			{
				throw "Hex file lines to long!\n";
			}
			// Ignore blank lines
			if (szLine[0] == '\n')
			{
				continue;
			}
			// Detect format and warn if garbage lines are found
			if (!formatDetected)
			{
				if (szLine[0] != ':' && szLine[0] != 'S')
				{
					cout << "Ignoring garbage line!\n";
					continue;
				}
				if (szLine[0] == 'S')
				{
					intel = false;
					cout << "Detected S-Record\n";
				}
				else
				{
					intel = true;
					cout << "Detected intel hex file\n";
				}
				formatDetected = true;
			}
			else if ((intel && szLine[0] != ':') ||
					(!intel && szLine[0] != 'S'))
			{
				cout << "Ignoring garbage line!\n";
				continue;
			}

			if (endSeen)
			{
				throw "Hex line after end of file record!\n";
			}

			if (intel)
			{
				unsigned long	dataBytes;
				unsigned long	startAddress;
				unsigned long	type;
				if (sscanf(&szLine[1], "%2lx%4lx%2lx", &dataBytes, &startAddress, &type) != 3)
				{
					throw "Hex line beginning corrupt!\n";
				}
				// Check line length
				if (szLine[11 + dataBytes * 2] != '\n' && szLine[11 + dataBytes * 2] != 0)
				{
					throw "Hex line length incorrect!\n";
				}
				// Check line checksum
				unsigned char	checkSum = 0;
				unsigned long	tmp;
				for (unsigned int i = 0; i <= dataBytes + 4; ++i)
				{
					if (sscanf(&szLine[1 + i * 2], "%2lx", &tmp) != 1)
					{
						throw "Hex line data corrupt!\n";
					}
					checkSum += tmp;
				}
				if (checkSum != 0)
				{
					throw "Hex line checksum error!\n";
				}

				switch (type)
				{
				case 0:
					// Data record
					if (!linear)
					{
						// Segmented
						unsigned long test = startAddress;
						test += dataBytes;
						if (test > 0xffff)
						{
							throw "Can't handle wrapped segments!\n";
						}
					}
					if (!m_chunks.size() ||
						m_chunks.back().m_startAddress + m_chunks.back().m_bytes.size() !=
						addressBase + startAddress)
					{
						m_chunks.push_back(MemBlock());
						m_chunks.back().m_startAddress = addressBase + startAddress;
					}
					{
						unsigned char i = 0;
						for (i = 0; i < dataBytes; ++i)
						{
							sscanf(&szLine[9 + i * 2], "%2lx", &tmp);
							if (addressBase + startAddress + i > limit)
							{
								cout << "Ignoring data above address space!\n";
								cout << "Data address: " << addressBase + startAddress + i;
								cout << " Limit: " << limit << "\n";
								if (!m_chunks.back().m_bytes.size())
								{
									m_chunks.pop_back();
								}
								continue;
							}
							m_chunks.back().m_bytes.push_back(tmp);
						}
					}
					break;

				case 1:
					// End-of-file record
					if (dataBytes != 0)
					{
						cout << "Warning: End of file record not zero length!\n";
					}
					if (startAddress != 0)
					{
						cout << "Warning: End of file record address not zero!\n";
					}
					endSeen = true;
					break;

				case 2:
					// Extended segment address record
					if (dataBytes != 2)
					{
						throw "Length field must be 2 in extended segment address record!\n";
					}
					if (startAddress != 0)
					{
						throw "Address field must be zero in extended segment address record!\n";
					}
					sscanf(&szLine[9], "%4lx", &startAddress);
					addressBase = startAddress << 4;
					linear = false;
					break;

				case 3:
					// Start segment address record
					if (dataBytes != 4)
					{
						cout << "Warning: Length field must be 4 in start segment address record!\n";
					}
					if (startAddress != 0)
					{
						cout << "Warning: Address field must be zero in start segment address record!\n";
					}
					if (dataBytes == 4)
					{
						unsigned long ssa;
						char	ssaStr[16];
						sscanf(&szLine[9], "%8lx", &ssa);
						sprintf(ssaStr, "%08lX\n", ssa);
						cout << "Segment start address (CS/IP): ";
						cout << ssaStr;
					}
					break;

				case 4:
					// Extended linear address record
					if (dataBytes != 2)
					{
						throw "Length field must be 2 in extended linear address record!\n";
					}
					if (startAddress != 0)
					{
						throw "Address field must be zero in extended linear address record!\n";
					}
					sscanf(&szLine[9], "%4lx", &startAddress);
					addressBase = ((unsigned long)startAddress) << 16;
					linear = true;
					break;

				case 5:
					// Start linear address record
					if (dataBytes != 4)
					{
						cout << "Warning: Length field must be 4 in start linear address record!\n";
					}
					if (startAddress != 0)
					{
						cout << "Warning: Address field must be zero in start linear address record!\n";
					}
					if (dataBytes == 4)
					{
						unsigned long lsa;
						char	lsaStr[16];
						sscanf(&szLine[9], "%8lx", &lsa);
						sprintf(lsaStr, "%08lX\n", lsa);
						cout << "Linear start address: ";
						cout << lsaStr;
					}
					break;

				default:
					cout << "Waring: Unknown record found!\n";
				}
			}
			else
			{
				// S-record
				unsigned long count;
				char			type;
				if (sscanf(&szLine[1], "%c%2lx", &type, &count) != 2)
				{
					throw "Hex line beginning corrupt!\n";
				}
				// Check line length
				if (szLine[4 + count * 2] != '\n' && szLine[4 + count * 2] != 0)
				{
					throw "Hex line length incorrect!\n";
				}
				// Check line checksum
				unsigned char	checkSum = 0;
				unsigned long	tmp;
				for (unsigned int i = 0; i < count + 1; ++i)
				{
					if (sscanf(&szLine[2 + i * 2], "%2lx", &tmp) != 1)
					{
						throw "Hex line data corrupt!\n";
					}
					checkSum += tmp;
				}
				if (checkSum != 255)
				{
					throw "Hex line checksum error!\n";
				}

				switch (type)
				{
				case '0':
					// Header record
					{
						char header[256];
						uint8_t i = 0;
						for (i = 0; uint8_t(i + 3) < count; ++i)
						{
							sscanf(&szLine[8 + i * 2], "%2lx", &tmp);
							header[i] = tmp;
						}
						header[i] = 0;
						if (i > 0)
						{
							cout << "Module name: " << header << "\n";
						}
					}
					break;

				case '1':
				case '2':
				case '3':
					// Data record
					{
						dataRecords++;
						unsigned long	startAddress;
						if (type == '1')
						{
							sscanf(&szLine[4], "%4lx", &startAddress);
						}
						else if (type == '2')
						{
							sscanf(&szLine[4], "%6lx", &startAddress);
						}
						else
						{
							sscanf(&szLine[4], "%8lx", &startAddress);
						}

						if (!m_chunks.size() ||
							m_chunks.back().m_startAddress + m_chunks.back().m_bytes.size() !=
							startAddress)
						{
							m_chunks.push_back(MemBlock());
							m_chunks.back().m_startAddress = startAddress;
						}
						unsigned char i = 0;
						for (i = uint8_t(type - '1'); uint8_t(i + 3) < count; ++i)
						{
							sscanf(&szLine[8 + i * 2], "%2lx", &tmp);
							if (startAddress + i > limit)
							{
								cout << "Ignoring data above address space!\n";
								cout << "Data address: " << startAddress + i;
								cout << " Limit: " << limit << "\n";
								if (!m_chunks.back().m_bytes.size())
								{
									m_chunks.pop_back();
								}
								continue;
							}
							m_chunks.back().m_bytes.push_back(tmp);
						}
					}
					break;

				case '5':
					// Count record
					{
						unsigned long	address;
						sscanf(&szLine[4], "%4lx", &address);
						if (address != dataRecords)
						{
							throw "Wrong number of data records!\n";
						}
					}
					break;

				case '7':
				case '8':
				case '9':
					// Start address record
					cout << "Ignoring start address record!\n";
					break;

				default:
					cout << "Unknown record found!\n";
				}
			}
		}
		if (intel && !endSeen)
		{
			cout << "No end of file record!\n";
		}
		if (!m_chunks.size())
		{
			throw "No data in file!\n";
		}
		vector<MemBlock>::iterator	vi;
		m_top = 0;
		for (vi = m_chunks.begin(); vi < m_chunks.end(); vi++)
		{
			m_top = max(m_top, vi->m_startAddress + vi->m_bytes.size() - 1);
		}
	}

	// Rather inefficient this one, fix sometime
	bool GetByte(const unsigned long address, unsigned char &chr)
	{
		vector<MemBlock>::iterator	vi;

		for (vi = m_chunks.begin(); vi < m_chunks.end(); vi++)
		{
			if (vi->m_startAddress + vi->m_bytes.size() > address && vi->m_startAddress <= address)
			{
				break;
			}
		}
		if (vi == m_chunks.end())
		{
			return false;
		}
		chr = vi->m_bytes[address - vi->m_startAddress];
		return true;
	}

	bool BitString(const unsigned long address, const unsigned char bits, const bool lEndian, string &str)
	{
		bool			ok = false;
		long			i;
		unsigned char	chr;
		unsigned long	data = 0;
		unsigned long	tmp;

		if (lEndian)
		{
			for (i = 0; i < (bits + 7) / 8; ++i)
			{
				ok |= GetByte(address + i, chr);
				tmp = chr;
				data |= tmp << (8 * i);
			}
		}
		else
		{
			for (i = 0; i < (bits + 7) / 8; ++i)
			{
				ok |= GetByte(address + i, chr);
				tmp = chr;
				data |= tmp << (8 * ((bits + 7) / 8 - i - 1));
			}
		}

		if (!ok)
		{
			return false;
		}

		unsigned long mask = 1;

		str = "";
		for (i = 0; i < bits; i++)
		{
			if (data & mask)
			{
				str.insert(0,"1");
			}
			else
			{
				str.insert(0,"0");
			}
			mask <<= 1;
		}
		return true;
	}

	FILE *Handle() { return m_file; };
	vector<MemBlock>	m_chunks;
	unsigned long		m_top;
private:
	FILE				*m_file;
};