File: codeplug.cc

package info (click to toggle)
qdmr 0.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 22,236 kB
  • sloc: cpp: 93,672; xml: 10,526; python: 1,108; makefile: 78; sh: 10
file content (797 lines) | stat: -rw-r--r-- 22,097 bytes parent folder | download | duplicates (2)
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
#include "codeplug.hh"
#include "config.hh"
#include <QtEndian>
#include "logger.hh"
#include "roamingchannel.hh"
#include "configcopyvisitor.hh"


/* ********************************************************************************************* *
 * Implementation of CodePlug::Flags
 * ********************************************************************************************* */
Codeplug::Flags::Flags()
  : TransferFlags(), _updateCodeplug(true), _autoEnableGPS(false), _autoEnableRoaming(false)
{
  // pass...
}


bool
Codeplug::Flags::updateCodeplug() const {
  return _updateCodeplug;
}

void
Codeplug::Flags::setUpdateCodeplug(bool enable) {
  _updateCodeplug = enable;
}


bool
Codeplug::Flags::autoEnableGPS() const {
  return _autoEnableGPS;
}

void
Codeplug::Flags::setAutoEnableGPS(bool enable) {
  _autoEnableGPS = enable;
}


bool
Codeplug::Flags::autoEnableRoaming() const {
  return _autoEnableRoaming;
}

void
Codeplug::Flags::setAutoEnableRoaming(bool enable) {
  _autoEnableRoaming = enable;
}



/* ********************************************************************************************* *
 * Implementation of CodePlug::Element
 * ********************************************************************************************* */
Codeplug::Element::Element(uint8_t *ptr, size_t size)
  : _data(ptr), _size(size)
{
  // pass...
}

Codeplug::Element::Element(const Element &other)
  : _data(other._data), _size(other._size)
{
  // pass...
}

Codeplug::Element::~Element() {
  // pass...
}

Codeplug::Element &
Codeplug::Element::operator =(const Element &other) {
  this->_data = other._data;
  this->_size = other._size;
  return *this;
}

bool
Codeplug::Element::isValid() const {
  return nullptr != _data;
}

void
Codeplug::Element::clear() {
  // pass...
}

bool
Codeplug::Element::fill(uint8_t value, unsigned offset, int size) {
  if (0 > size)
    size = _size-offset;
  if ((offset+size) > _size) {
    logFatal() << "Cannot fill codeplug element from " << QString::number(offset,16)
               << " size " << QString::number(size, 16) << ": overflow.";
    return false;
  }
  memset(_data+offset, value, size);
  return true;
}

bool
Codeplug::Element::getBit(const Offset::Bit &offset) const {
  return getBit(offset.byte, offset.bit);
}

bool
Codeplug::Element::getBit(unsigned offset, unsigned bit) const {
  if (offset >= _size) {
    logFatal() << "Cannot get bit at " << QString::number(offset, 16)
               << " bit " << bit << ": Overflow.";
    return false;
  }

  uint8_t *ptr = (_data+offset);
  return (1<<bit) & (*ptr);
}

void
Codeplug::Element::setBit(const Offset::Bit &offset, bool value) {
  setBit(offset.byte, offset.bit, value);
}
void
Codeplug::Element::setBit(unsigned offset, unsigned bit, bool value) {
  if (offset >= _size) {
    logFatal() << "Cannot set bit at " << QString::number(offset, 16)
               << " bit " << bit << ": Overflow.";
    return;
  }

  uint8_t *ptr = (_data+offset);
  if (value)
    (*ptr) |= (1<<bit);
  else
    (*ptr) &= ~(1<<bit);
}

void
Codeplug::Element::clearBit(const Offset::Bit &offset) {
  clearBit(offset.byte, offset.bit);
}
void
Codeplug::Element::clearBit(unsigned offset, unsigned bit) {
  if (offset >= _size) {
    logFatal() << "Cannot clear bit at " << QString::number(offset, 16)
               << " bit " << bit << ": Overflow.";
    return;
  }

  uint8_t *ptr = (_data+offset);
  (*ptr) &= ~(1<<bit);
}


uint8_t
Codeplug::Element::getUInt2(const Offset::Bit &offset) const {
  return getUInt2(offset.byte, offset.bit);
}
void
Codeplug::Element::setUInt2(const Offset::Bit &offset, uint8_t value) {
  setUInt2(offset.byte, offset.bit, value);
}

uint8_t
Codeplug::Element::getUInt2(unsigned offset, unsigned bit) const {
  if (offset >= _size) {
    logFatal() << "Cannot get uint2 at " << QString::number(offset, 16)
               << " bit " << bit << ": Overflow.";
    return 0;
  }

  return (((*(_data+offset)) >> bit) & 0b11);
}
void
Codeplug::Element::setUInt2(unsigned offset, unsigned bit, uint8_t value) {
  if (offset >= _size) {
    logFatal() << "Cannot set uint2 at " << QString::number(offset, 16)
               << " bit " << bit << ": Overflow.";
    return;
  }

  *(_data+offset) &= ~(0b11 << bit);
  *(_data+offset) |= ((value & 0b11)<<bit);
}

uint8_t
Codeplug::Element::getUInt3(const Offset::Bit &offset) const {
  return getUInt3(offset.byte, offset.bit);
}
void
Codeplug::Element::setUInt3(const Offset::Bit &offset, uint8_t value) {
  setUInt3(offset.byte, offset.bit, value);
}

uint8_t
Codeplug::Element::getUInt3(unsigned offset, unsigned bit) const {
  if (offset >= _size) {
    logFatal() << "Cannot get uint3 at " << QString::number(offset, 16)
               << " bit " << bit << ": Overflow.";
    return 0;
  }

  return (((*(_data+offset)) >> bit) & 0b111);
}
void
Codeplug::Element::setUInt3(unsigned offset, unsigned bit, uint8_t value) {
  if (offset >= _size) {
    logFatal() << "Cannot set uint3 at " << QString::number(offset, 16)
               << " bit " << bit << ": Overflow.";
    return;
  }

  *(_data+offset) &= ~(0b111 << bit);
  *(_data+offset) |= ((value & 0b111)<<bit);
}

uint8_t
Codeplug::Element::getUInt4(const Offset::Bit &offset) const {
  return getUInt4(offset.byte, offset.bit);
}
void
Codeplug::Element::setUInt4(const Offset::Bit &offset, uint8_t value) {
  setUInt4(offset.byte, offset.bit, value);
}


uint8_t
Codeplug::Element::getUInt4(unsigned offset, unsigned bit) const {
  if (offset >= _size) {
    logFatal() << "Cannot get uint4 at " << QString::number(offset, 16)
               << " bit " << bit << ": Overflow.";
    return 0;
  }

  return (((*(_data+offset)) >> bit) & 0b1111);
}
void
Codeplug::Element::setUInt4(unsigned offset, unsigned bit, uint8_t value) {
  if (offset >= _size) {
    logFatal() << "Cannot set uint4 at " << QString::number(offset, 16)
               << " bit " << bit << ": Overflow.";
    return;
  }

  *(_data+offset) &= ~(0b1111 << bit);
  *(_data+offset) |= ((value & 0b1111)<<bit);
}

uint8_t
Codeplug::Element::getUInt5(const Offset::Bit &offset) const {
  return getUInt5(offset.byte, offset.bit);
}
void
Codeplug::Element::setUInt5(const Offset::Bit &offset, uint8_t value) {
  setUInt5(offset.byte, offset.bit, value);
}

uint8_t
Codeplug::Element::getUInt5(unsigned offset, unsigned bit) const {
  if (offset >= _size) {
    logFatal() << "Cannot get uint5 at " << QString::number(offset, 16)
               << " bit " << bit << ": Overflow.";
    return 0;
  }

  return (((*(_data+offset)) >> bit) & 0b11111);
}
void
Codeplug::Element::setUInt5(unsigned offset, unsigned bit, uint8_t value) {
  if (offset >= _size) {
    logFatal() << "Cannot get uint5 at " << QString::number(offset, 16)
               << " bit " << bit << ": Overflow.";
    return;
  }

  *(_data+offset) &= ~(0b11111 << bit);
  *(_data+offset) |= ((value & 0b11111)<<bit);
}

uint8_t
Codeplug::Element::getUInt6(const Offset::Bit &offset) const {
  return getUInt6(offset.byte, offset.bit);
}
void
Codeplug::Element::setUInt6(const Offset::Bit &offset, uint8_t value) {
  setUInt6(offset.byte, offset.bit, value);
}

uint8_t
Codeplug::Element::getUInt6(unsigned offset, unsigned bit) const {
  if (offset >= _size) {
    logFatal() << "Cannot get uint6 at " << QString::number(offset, 16)
               << " bit " << bit << ": Overflow.";
    return 0;
  }

  return (((*(_data+offset)) >> bit) & 0b111111);
}
void
Codeplug::Element::setUInt6(unsigned offset, unsigned bit, uint8_t value) {
  if (offset >= _size) {
    logFatal() << "Cannot set uint6 at " << QString::number(offset, 16)
               << " bit " << bit << ": Overflow.";
    return;
  }

  *(_data+offset) &= ~(0b111111 << bit);
  *(_data+offset) |= ((value & 0b111111)<<bit);
}

uint8_t
Codeplug::Element::getUInt8(unsigned offset) const {
  if (offset >= _size) {
    logFatal() << "Cannot get uint8 at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  return _data[offset];
}
void
Codeplug::Element::setUInt8(unsigned offset, uint8_t value) {
  if (offset >= _size) {
    logFatal() << "Cannot set uint8 at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  _data[offset] = value;
}

int8_t
Codeplug::Element::getInt8(unsigned offset) const {
  if (offset >= _size) {
    logFatal() << "Cannot get int8 at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }
  return ((int8_t *)_data)[offset];
}
void
Codeplug::Element::setInt8(unsigned offset, int8_t value) {
  if (offset >= _size) {
    logFatal() << "Cannot set int8 at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  ((int8_t *)_data)[offset] = value;
}

uint16_t
Codeplug::Element::getUInt16_be(unsigned offset) const {
  if ((offset+2) > _size) {
    logFatal() << "Cannot get int16 (be) at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  uint16_t *ptr = (uint16_t *)(_data+offset);
  return qFromBigEndian(*ptr);
}

uint16_t
Codeplug::Element::getUInt16_le(unsigned offset) const {
  if ((offset+2) > _size) {
    logFatal() << "Cannot get int16 (le) at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  quint16 *ptr = (quint16 *)(_data+offset);
  return qFromLittleEndian(*ptr);
}

void
Codeplug::Element::setUInt16_be(unsigned offset, uint16_t value) {
  if ((offset+2) > _size) {
    logFatal() << "Cannot set int16 (be) at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  quint16 *ptr = (quint16 *)(_data+offset);
  (*ptr) = qToBigEndian((quint16)value);
}

void
Codeplug::Element::setUInt16_le(unsigned offset, uint16_t value) {
  if ((offset+2) > _size) {
    logFatal() << "Cannot set int16 (le) at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  quint16 *ptr = (quint16 *)(_data+offset);
  (*ptr) = qToLittleEndian(value);
}

uint32_t
Codeplug::Element::getUInt24_be(unsigned offset) const {
  if ((offset+3) > _size) {
    logFatal() << "Cannot get int24 (be) at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  uint8_t *ptr = _data+offset;
  return uint32_t(ptr[2]) + (uint32_t(ptr[1])<<8) + (uint32_t(ptr[0])<<16);
}
uint32_t
Codeplug::Element::getUInt24_le(unsigned offset) const {
  if ((offset+3) > _size) {
    logFatal() << "Cannot get int24 (le) at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  uint8_t *ptr = _data+offset;
  return uint32_t(ptr[0]) + (uint32_t(ptr[1])<<8) + (uint32_t(ptr[2])<<16);
}
void
Codeplug::Element::setUInt24_be(unsigned offset, uint32_t value) {
  if ((offset+3) > _size) {
    logFatal() << "Cannot set int24 (be) at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  uint8_t *ptr = _data+offset;
  ptr[0] = ((value >> 16) & 0xff);
  ptr[1] = ((value >> 8)  & 0xff);
  ptr[2] = ((value >> 0)  & 0xff);
}
void
Codeplug::Element::setUInt24_le(unsigned offset, uint32_t value) {
  if ((offset+3) > _size) {
    logFatal() << "Cannot set int24 (le) at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  uint8_t *ptr = _data+offset;
  ptr[0] = ((value >> 0)  & 0xff);
  ptr[1] = ((value >> 8)  & 0xff);
  ptr[2] = ((value >> 16) & 0xff);
}

uint32_t
Codeplug::Element::getUInt32_be(unsigned offset) const {
  if ((offset+4) > _size) {
    logFatal() << "Cannot get int32 (be) at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  uint32_t *ptr = (uint32_t *)(_data+offset);
  return qFromBigEndian(*ptr);
}
uint32_t
Codeplug::Element::getUInt32_le(unsigned offset) const {
  if ((offset+4) > _size) {
    logFatal() << "Cannot get int32 (le) at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  uint32_t *ptr = (uint32_t *)(_data+offset);
  return qFromLittleEndian(*ptr);
}
void
Codeplug::Element::setUInt32_be(unsigned offset, uint32_t value) {
  if ((offset+4) > _size) {
    logFatal() << "Cannot set int32 (be) at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  uint32_t *ptr = (uint32_t *)(_data+offset);
  (*ptr) = qToBigEndian(value);
}
void
Codeplug::Element::setUInt32_le(unsigned offset, uint32_t value) {
  if ((offset+4) > _size) {
    logFatal() << "Cannot set int32 (le) at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  uint32_t *ptr = (uint32_t *)(_data+offset);
  (*ptr) = qToLittleEndian(value);
}

uint64_t
Codeplug::Element::getUInt64_be(unsigned offset) const {
  if ((offset+8) > _size) {
    logFatal() << "Cannot get int64 (be) at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  quint64 *ptr = (quint64 *)(_data+offset);
  return qFromBigEndian<quint64>(*ptr);
}
uint64_t
Codeplug::Element::getUInt64_le(unsigned offset) const {
  if ((offset+8) > _size) {
    logFatal() << "Cannot get int64 (le) at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  quint64 *ptr = (quint64 *)(_data+offset);
  return qFromLittleEndian<quint64>(*ptr);
}
void
Codeplug::Element::setUInt64_be(unsigned offset, uint64_t value) {
  if ((offset+8) > _size) {
    logFatal() << "Cannot set int64 (be) at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  quint64 *ptr = (quint64 *)(_data+offset);
  (*ptr) = qToBigEndian<quint64>(value);
}
void
Codeplug::Element::setUInt64_le(unsigned offset, uint64_t value) {
  if ((offset+8) > _size) {
    logFatal() << "Cannot set int64 (le) at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  quint64 *ptr = (quint64 *)(_data+offset);
  (*ptr) = qToLittleEndian<quint64>(value);
}

uint8_t
Codeplug::Element::getBCD2(unsigned offset) const {
  if ((offset+1) > _size) {
    logFatal() << "Cannot get BCD2 at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  uint8_t val = getUInt8(offset);
  return (val & 0xf) + ((val>>4) & 0xf)*10;
}
void
Codeplug::Element::setBCD2(unsigned offset, uint8_t val) {
  if ((offset+1) > _size) {
    logFatal() << "Cannot get BCD2 at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  uint8_t a  = (val / 10) % 10;
  uint8_t b  = (val /  1) % 10;
  setUInt8(offset, (a << 4) + b);
}

uint16_t
Codeplug::Element::getBCD4_be(unsigned offset) const {
  if ((offset+2) > _size) {
    logFatal() << "Cannot get BCD4 (be) at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  uint32_t val = getUInt16_be(offset);
  return (val & 0xf) + ((val>>4) & 0xf)*10 + ((val>>8) & 0xf)*100 + ((val>>12) & 0xf)*1000;
}
void
Codeplug::Element::setBCD4_be(unsigned offset, uint16_t val) {
  if ((offset+2) > _size) {
    logFatal() << "Cannot set BCD4 (be) at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  uint32_t a  = (val / 1000) % 10;
  uint32_t b  = (val /  100) % 10;
  uint32_t c  = (val /   10) % 10;
  uint32_t d  = (val /    1) % 10;
  setUInt16_be(offset, (a << 12) + (b << 8) + (c << 4) + d);
}
uint16_t
Codeplug::Element::getBCD4_le(unsigned offset) const {
  if ((offset+2) > _size) {
    logFatal() << "Cannot get BCD4 (le) at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  uint32_t val = getUInt16_le(offset);
  return (val & 0xf) + ((val>>4) & 0xf)*10 + ((val>>8) & 0xf)*100 + ((val>>12) & 0xf)*1000;
}
void
Codeplug::Element::setBCD4_le(unsigned offset, uint16_t val) {
  if ((offset+2) > _size) {
    logFatal() << "Cannot set BCD4 (le) at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  uint32_t a  = (val / 1000) % 10;
  uint32_t b  = (val /  100) % 10;
  uint32_t c  = (val /   10) % 10;
  uint32_t d  = (val /    1) % 10;
  setUInt16_le(offset, (a << 12) + (b << 8) + (c << 4) + d);
}

uint32_t
Codeplug::Element::getBCD8_be(unsigned offset) const {
  if ((offset+4) > _size) {
    logFatal() << "Cannot get BCD8 (be) at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  uint32_t val = getUInt32_be(offset);
  return (val & 0xf) + ((val>>4) & 0xf)*10 + ((val>>8) & 0xf)*100 + ((val>>12) & 0xf)*1000 +
      ((val>>16) & 0xf)*10000 + ((val>>20) & 0xf)*100000 + ((val>>24) & 0xf)*1000000 +
      ((val>>28) & 0xf)*10000000;
}
void
Codeplug::Element::setBCD8_be(unsigned offset, uint32_t val) {
  if ((offset+4) > _size) {
    logFatal() << "Cannot set BCD8 (be) at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  uint32_t a  = (val / 10000000) % 10;
  uint32_t b  = (val /  1000000) % 10;
  uint32_t c  = (val /   100000) % 10;
  uint32_t d  = (val /    10000) % 10;
  uint32_t e  = (val /     1000) % 10;
  uint32_t f  = (val /      100) % 10;
  uint32_t g  = (val /       10) % 10;
  uint32_t h  = (val /        1) % 10;
  setUInt32_be(offset, (a << 28) + (b << 24) + (c << 20) + (d << 16) + (e << 12) + (f << 8) + (g << 4) + h);
}
uint32_t
Codeplug::Element::getBCD8_le(unsigned offset) const {
  if ((offset+4) > _size) {
    logFatal() << "Cannot get BCD8 (le) at " << QString::number(offset, 16) << ": Overflow.";
    return 0;
  }

  uint32_t val = getUInt32_le(offset);
  return (val & 0xf) + ((val>>4) & 0xf)*10 + ((val>>8) & 0xf)*100 + ((val>>12) & 0xf)*1000 +
      ((val>>16) & 0xf)*10000 + ((val>>20) & 0xf)*100000 + ((val>>24) & 0xf)*1000000 +
      ((val>>28) & 0xf)*10000000;
}
void
Codeplug::Element::setBCD8_le(unsigned offset, uint32_t val) {
  if ((offset+4) > _size) {
    logFatal() << "Cannot set BCD8 (le) at " << QString::number(offset, 16) << ": Overflow.";
    return;
  }

  uint32_t a  = (val / 10000000) % 10;
  uint32_t b  = (val /  1000000) % 10;
  uint32_t c  = (val /   100000) % 10;
  uint32_t d  = (val /    10000) % 10;
  uint32_t e  = (val /     1000) % 10;
  uint32_t f  = (val /      100) % 10;
  uint32_t g  = (val /       10) % 10;
  uint32_t h  = (val /        1) % 10;
  setUInt32_le(offset, (a << 28) + (b << 24) + (c << 20) + (d << 16) + (e << 12) + (f << 8) + (g << 4) + h);
}

QString
Codeplug::Element::readASCII(unsigned offset, unsigned maxlen, uint8_t eos) const {
  QString txt;
  uint8_t *ptr = (uint8_t *)(_data+offset);
  for (unsigned i=0; (i<maxlen)&&(ptr[i])&&(eos!=ptr[i]); i++) {
    txt.append(QChar::fromLatin1(ptr[i]));
  }
  return txt;
}
void
Codeplug::Element::writeASCII(unsigned offset, const QString &txt, unsigned maxlen, uint8_t eos) {
  uint8_t *ptr = (uint8_t *)(_data+offset);
  for (unsigned i=0; i<maxlen; i++) {
    if (i < unsigned(txt.length()))
      ptr[i] = txt.at(i).toLatin1();
    else
      ptr[i] = eos;
  }
}

QString
Codeplug::Element::readUnicode(unsigned offset, unsigned maxlen, uint16_t eos) const {
  QString txt;
  uint16_t *ptr = (uint16_t *)(_data+offset);
  for (unsigned i=0; (i<maxlen)&&(eos!=ptr[i]); i++) {
    txt.append(QChar(ptr[i]));
  }
  return txt;
}
void
Codeplug::Element::writeUnicode(unsigned offset, const QString &txt, unsigned maxlen, uint16_t eos) {
  uint16_t *ptr = (uint16_t *)(_data+offset);
  for (unsigned i=0; i<maxlen; i++) {
    if (i < unsigned(txt.length()))
      ptr[i] = txt.at(i).unicode();
    else
      ptr[i] = eos;
  }
}


/* ********************************************************************************************* *
 * Implementation of CodePlug::Context
 * ********************************************************************************************* */
Codeplug::Context::Context(Config *config)
  : _config(config), _satellites(nullptr), _tables()
{
  // Add tables for common elements
  addTable(&DMRRadioID::staticMetaObject);
  addTable(&DMRContact::staticMetaObject);
  addTable(&DTMFContact::staticMetaObject);
  addTable(&RXGroupList::staticMetaObject);
  addTable(&Channel::staticMetaObject);
  addTable(&Zone::staticMetaObject);
  addTable(&ScanList::staticMetaObject);
  addTable(&GPSSystem::staticMetaObject);
  addTable(&APRSSystem::staticMetaObject);
  addTable(&RoamingChannel::staticMetaObject);
  addTable(&RoamingZone::staticMetaObject);
  addTable(&SMSTemplate::staticMetaObject);
  addTable(&BasicEncryptionKey::staticMetaObject);
  addTable(&ARC4EncryptionKey::staticMetaObject);
  addTable(&AESEncryptionKey::staticMetaObject);
}

Config  *
Codeplug::Context::config() const {
  return _config;
}

SatelliteDatabase *
Codeplug::Context::satellites() const {
  return _satellites;
}

bool
Codeplug::Context::hasTable(const QMetaObject *obj) const {
  // Find a matching table
  if (_tables.contains(obj->className()))
    return true;
  else if (obj->superClass())
    return hasTable(obj->superClass());
  return false;
}

Codeplug::Context::Table &
Codeplug::Context::getTable(const QMetaObject *obj) {
  if (_tables.contains(obj->className()))
    return _tables[obj->className()];
  return getTable(obj->superClass());
}

bool
Codeplug::Context::addTable(const QMetaObject *obj) {
  if (hasTable(obj))
    return false;
  _tables.insert(obj->className(), Table());
  return true;
}

ConfigItem *
Codeplug::Context::obj(const QMetaObject *elementType, unsigned idx) {
  if (! hasTable(elementType))
    return nullptr;
  return getTable(elementType).objects.value(idx, nullptr);
}

int
Codeplug::Context::index(ConfigItem *obj) {
  if (nullptr == obj)
    return -1;
  if (! hasTable(obj->metaObject()))
    return -1;
  return getTable(obj->metaObject()).indices.value(obj, -1);
}

bool
Codeplug::Context::add(ConfigItem *obj, unsigned idx) {
  if (!hasTable(obj->metaObject()))
    return false;
  if (! getTable(obj->metaObject()).indices.contains(obj))
    getTable(obj->metaObject()).indices.insert(obj, idx);
  if (! getTable(obj->metaObject()).objects.contains(idx))
    getTable(obj->metaObject()).objects.insert(idx, obj);
  return true;
}


/* ********************************************************************************************* *
 * Implementation of CodePlug
 * ********************************************************************************************* */
Codeplug::Codeplug(QObject *parent)
  : DFUFile(parent)
{
	// pass...
}

Codeplug::~Codeplug() {
	// pass...
}

Config *
Codeplug::preprocess(Config *config, const ErrorStack &err) const {
  return ConfigCopy::copy(config, err)->as<Config>();
}

bool
Codeplug::postprocess(Config *config, const ErrorStack &err) const {
  Q_UNUSED(config); Q_UNUSED(err);
  return true;
}