forked from rusefi/rusefi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogicdata_reader.cpp
More file actions
1156 lines (1037 loc) · 29.8 KB
/
Copy pathlogicdata_reader.cpp
File metadata and controls
1156 lines (1037 loc) · 29.8 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
/*
* @file logicdata_reader.cpp
*
* Inverse of logicdata.cpp - parses .logicdata binary files written by
* writeLogicDataFile() back into CompositeEvent stream.
*
* The .logicdata writer uses a variable-length integer scheme:
* byte N (0, 1..4, or 8) specifies how many following little-endian bytes
* encode the value. N=0 means value zero (no bytes follow).
*
* This reader mirrors the writer step-by-step.
*/
#include "logicdata.h"
#include "logicdata_reader.h"
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <algorithm>
#include <stdexcept>
namespace {
#define BLOCK 0x15
#define CHANNEL_BLOCK 0x16
#define SUB 0x54
#define FLAG_NOTEMPTY 2
#define FLAG_NOTEMPTY_LONG 3
#define FLAG_EMPTY 5
#define SIGN_FLAG 0x80000000L
static const int kNumChannels = 6;
static FILE* g_fp = nullptr;
static uint8_t readByte() {
int c = fgetc(g_fp);
if (c == EOF) throw std::runtime_error("unexpected EOF");
return (uint8_t)c;
}
// Inverse of write(): read length tag and that many little-endian bytes.
static uint64_t readVar() {
uint8_t n = readByte();
if (n == 0) return 0;
if (n > 8) throw std::runtime_error("bad var length");
uint64_t v = 0;
for (int i = 0; i < n; i++) {
v |= ((uint64_t)readByte()) << (i * 8);
}
return v;
}
// Skip one variable-length value.
static void skipVar() { (void)readVar(); }
static void skipVars(int count) {
for (int i = 0; i < count; i++) skipVar();
}
// Inverse of writeId(): three vars.
static void skipId() { skipVar(); skipVar(); skipVar(); }
// Inverse of writeString().
static void skipString() {
uint64_t len = readVar();
for (uint64_t i = 0; i < len; i++) (void)readByte();
}
// Inverse of writeDouble(). Written as length=8 + 8 raw bytes, but some files
// may emit shorter encodings; accept any 0..8.
static void skipDouble() {
uint8_t n = readByte();
if (n == 0) return;
if (n > 8) throw std::runtime_error("bad double length");
for (int i = 0; i < n; i++) (void)readByte();
}
static void readHeader() {
uint8_t magic = readByte();
if (magic != 0x7f) throw std::runtime_error("bad magic");
skipVar(); // duplicate strlen(title), see writeHeader
skipString(); // title
skipVar(); // BLOCK
skipVar(); // SUB
skipVar(); // frequency
skipVar(); // 0
skipVar(); // reservedDurationInSamples
skipVar(); // frequency/frequencyDiv
skipVars(2);
uint64_t nch = readVar(); // numChannels
if ((int)nch != kNumChannels) {
// still attempt to proceed, but we only support kNumChannels
}
skipVar(); // BLOCK
skipVar(); // 0
skipVar(); // BLOCK
for (int i = 0; i < kNumChannels; i++) {
skipId();
}
skipVar(); // 0
skipVar(); // BLOCK
skipId();
skipVar();
skipVar();
}
// Inverse of writeChannelHeader.
static void readChannelHeader(int ch) {
skipVar(); // 0xff
skipVar(); // ch
skipString(); // name
skipVars(2);
skipDouble();
skipVar();
skipDouble();
skipVar();
skipDouble();
if (ch == kNumChannels - 1) {
skipVar(); // 0
} else {
skipId();
for (int i = 0; i < 3; i++) skipVar();
}
}
static void readChannelDataHeader() {
skipVar(); // BLOCK
skipVar(); // scaledDurationInSamples
skipVars(5);
skipVar(); // numChannels
skipVars(3);
skipId();
skipVar();
skipVar(); // BLOCK
skipVars(3);
for (int i = 0; i < kNumChannels; i++) {
readChannelHeader(i);
}
skipVar(); // BLOCK
skipVars(6); // SUB_ARRAY
skipVars(6);
skipVar(); // BLOCK
skipVars(2);
skipVar(); // realDurationInSamples
skipVar();
skipVar(); // SUB
skipVar(); // reservedDurationInSamples
skipVar(); // freq/div
skipVars(2);
skipVar(); // SUB
skipVars(2);
skipVar(); // 1
skipVars(3);
skipId();
skipVar(); // BLOCK
skipVars(3); // SAM_ARRAY
skipVar();
skipVar(); // SUB
skipVar();
skipVar(); // BLOCK
skipVar();
skipVar(); // BLOCK
skipVars(4); // SUB four times
skipVar();
skipVar(); // BLOCK
skipVar(); // frequency
skipVars(3);
skipVar(); // 1
skipVars(3);
skipId();
skipVars(6); // ARR_6
skipVar(); // SUB
skipVar(); // BLOCK
skipVar(); // 0
skipVar(); // realDurationInSamples
skipVars(2);
skipVar(); // numChannels
skipVars(3); // ARR_3
}
struct ChannelEdges {
std::vector<uint32_t> timestamps; // absolute, after delta accumulation
std::vector<int> states; // state AFTER the edge (the new state)
int initialState = 0; // chPrevState before first edge (we infer)
int lastState = 0; // chLastState as recorded in file
};
static void readEdges(std::vector<uint32_t>& deltas, std::vector<int>& states,
bool useLongDeltas, int numEdges) {
// Edges are written as raw little-endian bytes (2 or 4 per edge), followed
// by a single 0x00 terminator byte.
for (int e = 0; e < numEdges; e++) {
uint8_t b0 = readByte();
uint8_t b1 = readByte();
uint32_t raw;
if (useLongDeltas) {
uint8_t b2 = readByte();
uint8_t b3 = readByte();
raw = (uint32_t)b0 | ((uint32_t)b1 << 8) | ((uint32_t)b2 << 16) | ((uint32_t)b3 << 24);
} else {
raw = (uint32_t)b0 | ((uint32_t)b1 << 8);
}
// Decode sign flag and recover delta and new state.
// Writer: if state went to 0 -> delta |= SIGN_FLAG. Then for short
// deltas, SIGN_FLAG (bit 31) was relocated to bit 15.
int newState = 1;
uint32_t delta;
if (useLongDeltas) {
if (raw & SIGN_FLAG) {
newState = 0;
delta = raw & ~SIGN_FLAG;
} else {
newState = 1;
delta = raw;
}
} else {
if (raw & 0x8000) {
newState = 0;
delta = raw & 0x7fff;
} else {
newState = 1;
delta = raw;
}
}
deltas.push_back(delta);
states.push_back(newState);
}
}
static void readChannelData(int ch, ChannelEdges& out) {
skipVar(); // CHANNEL_BLOCK
if (ch == 0) {
skipVar(); // SUB
skipVar(); // BLOCK
}
skipVar(); // ch+1
skipVar(); // 0
skipVar(); // realDurationInSamples
skipVar(); // 1
skipVar(); // lastRecord
skipVar(); // numSamplesLeft
out.lastState = (int)readVar(); // chLastState
uint64_t chFlag = readVar();
bool empty = (chFlag == FLAG_EMPTY);
bool useLongDeltas = (chFlag == FLAG_NOTEMPTY_LONG);
if (ch == 0) {
skipVar(); // 0
skipVar(); // BLOCK
skipVars(11);
if (useLongDeltas) {
skipVar(); // BLOCK
skipVars(6);
}
skipVar(); // BLOCK
} else {
skipVars(10);
if (useLongDeltas) {
skipVars(5);
}
}
uint64_t numEdges1 = readVar();
skipVar(); // 0
skipVar(); // numEdges
skipVar(); // 0
skipVar(); // numEdges
std::vector<uint32_t> deltas;
std::vector<int> newStates;
// Writer always calls writeEdges regardless of empty/non-empty, which
// loops numEdges times then writes a single 0x00 terminator byte. For
// empty channels numEdges is 0 so we just consume the terminator.
readEdges(deltas, newStates, useLongDeltas, (int)numEdges1);
(void)readByte(); // 0x00 terminator
if (ch == 0) {
skipVar(); // BLOCK
skipVars(6);
if (!useLongDeltas) {
skipVar(); // BLOCK
skipVars(6);
}
skipVar(); // BLOCK
} else {
skipVars(4);
if (!useLongDeltas) {
skipVars(5);
}
}
if (empty) {
skipVars(5);
out.initialState = out.lastState;
return;
}
skipVar(); // 1
skipVar(); // 0
skipVar(); // 1
skipVar(); // 0
skipVar(); // 1
skipVars(16);
for (int i = 0; i < 8; i++) (void)readByte(); // 0xFF * 8
(void)readByte(); // chFlag raw
for (int i = 0; i < 7; i++) (void)readByte(); // 0x00 * 7
// Convert deltas to absolute timestamps.
out.timestamps.reserve(deltas.size());
uint32_t ts = 0;
for (size_t i = 0; i < deltas.size(); i++) {
ts += deltas[i];
out.timestamps.push_back(ts);
out.states.push_back(newStates[i]);
}
// Initial state = opposite of first edge's new state (since edges are
// state changes), or equal to lastState if no edges.
if (!out.states.empty()) {
out.initialState = out.states[0] ? 0 : 1;
} else {
out.initialState = out.lastState;
}
(void)numEdges1;
}
// Read a length-prefixed ASCII string into `out`.
static void readString(std::string& out) {
uint64_t len = readVar();
out.clear();
out.reserve((size_t)len);
for (uint64_t i = 0; i < len; i++) {
out.push_back((char)readByte());
}
}
// Inspect just the header of a .logicdata file. Auto-detects between
// our writer's variant (no version byte, BLOCK=0x15, numChannels hard-coded
// to 6) and the real Saleae / Java LogicdataStreamFile variant (leading
// version=0x13 byte, BLOCK=0x18, numChannels declared in header).
static LogicDataHeaderInfo inspectHeaderImpl() {
LogicDataHeaderInfo info;
uint8_t magic = readByte();
if (magic != 0x7f) throw std::runtime_error("bad magic");
// After magic both variants emit a single var. For the real Saleae /
// Java writer this is `version=0x13`; for our C++ writer it's an extra
// `write(strlen(title))` that is otherwise unused (writeString below
// writes the length again). Distinguish by value: 0x13 = real format.
uint64_t firstVar = readVar();
info.hasVersionByte = (firstVar == 0x13);
// Title: length (var) followed by raw bytes. NOTE: for our writer this is
// `writeString(title)` (var-length-prefixed bytes); for the real format
// the layout is also `<var:titleLen> <raw bytes>`. Identical on the wire.
uint64_t titleLen = readVar();
if (titleLen > 256) throw std::runtime_error("implausible title length");
info.title.resize((size_t)titleLen);
for (uint64_t k = 0; k < titleLen; k++) {
info.title[(size_t)k] = (char)readByte();
}
info.blockMarker = (int)readVar(); // BLOCK (0x15 ours / 0x18 real)
skipVar(); // SUB
info.frequency = readVar();
skipVar(); // 0
skipVar(); // reservedDurationInSamples
skipVar(); // frequency / frequencyDiv
skipVars(2);
info.numChannels = (int)readVar();
if (info.numChannels <= 0 || info.numChannels > 64) {
throw std::runtime_error("implausible numChannels");
}
skipVar(); // BLOCK
skipVar(); // 0
skipVar(); // BLOCK
for (int i = 0; i < info.numChannels; i++) {
skipVar(); skipVar(); skipVar(); // writeId(i, 1)
}
skipVar(); // 0
skipVar(); // BLOCK
skipVar(); skipVar(); skipVar(); // writeId(0, 0)
skipVar();
skipVar();
// channelDataHeader prefix.
skipVar(); // BLOCK
skipVar(); // scaledDurationInSamples
skipVars(5);
skipVar(); // numChannels
skipVars(3);
skipVar(); skipVar(); skipVar(); // writeId(0, 1)
skipVar();
skipVar(); // BLOCK
skipVars(3);
// Per-channel headers: 0xff var, ch var, name string, then variant-
// specific padding/doubles + optional id+flags. We only need the name,
// then follow the writer's per-channel header skip recipe.
for (int ch = 0; ch < info.numChannels; ch++) {
skipVar(); // 0xff
skipVar(); // ch
uint64_t nameLen = readVar();
std::string name;
name.resize((size_t)nameLen);
for (uint64_t k = 0; k < nameLen; k++) {
name[(size_t)k] = (char)readByte();
}
info.channelNames.push_back(name);
skipVars(2); // padding 0, 0
skipDouble(); // 1.0
skipVar(); // 0
skipDouble(); // 0.0
skipVar(); // 1 or 2
skipDouble(); // 0.0 or 1.0
if (ch == info.numChannels - 1) {
skipVar(); // 0
} else {
skipVar(); skipVar(); skipVar(); // writeId(1+ch, 1)
skipVar(); skipVar(); skipVar(); // CHANNEL_FLAGS bytes (3 vars)
}
}
return info;
}
} // namespace
std::vector<CompositeEvent> readLogicDataFile(const char* fileName) {
g_fp = fopen(fileName, "rb");
if (!g_fp) {
throw std::runtime_error(std::string("cannot open ") + fileName);
}
std::vector<CompositeEvent> result;
try {
readHeader();
readChannelDataHeader();
ChannelEdges channels[kNumChannels];
for (int ch = 0; ch < kNumChannels; ch++) {
readChannelData(ch, channels[ch]);
}
// Merge per-channel edges into composite events sorted by timestamp.
// At each unique timestamp, snapshot all channel states.
struct Edge { uint32_t ts; int ch; int newState; };
std::vector<Edge> all;
for (int ch = 0; ch < kNumChannels; ch++) {
for (size_t i = 0; i < channels[ch].timestamps.size(); i++) {
all.push_back({channels[ch].timestamps[i], ch, channels[ch].states[i]});
}
}
std::stable_sort(all.begin(), all.end(),
[](const Edge& a, const Edge& b){ return a.ts < b.ts; });
int curState[kNumChannels];
for (int ch = 0; ch < kNumChannels; ch++) {
curState[ch] = channels[ch].initialState;
}
// Emit one CompositeEvent per unique timestamp.
size_t i = 0;
while (i < all.size()) {
uint32_t ts = all[i].ts;
while (i < all.size() && all[i].ts == ts) {
curState[all[i].ch] = all[i].newState;
i++;
}
CompositeEvent ev{};
ev.timestamp = ts;
ev.primaryTrigger = curState[0];
ev.secondaryTrigger = curState[1];
ev.isTDC = curState[2];
ev.sync = curState[3];
ev.coil = curState[4];
ev.injector = curState[5];
result.push_back(ev);
}
} catch (const std::exception& e) {
fclose(g_fp);
g_fp = nullptr;
throw;
}
fclose(g_fp);
g_fp = nullptr;
return result;
}
namespace {
// Generalized inverse of writeChannelData(): identical in structure to the
// 6-channel C++ writer AND the Java LogicdataStreamFile writer (modulo the
// BLOCK marker numeric value, which is not validated by the reader -- both
// 0x15 and 0x18 round-trip through skipVar()).
static void readChannelDataN(int ch, int nch, ChannelEdges& out) {
skipVar(); // CHANNEL_BLOCK
if (ch == 0) {
skipVar(); // SUB
skipVar(); // BLOCK
}
skipVar(); // ch+1
skipVar(); // 0
skipVar(); // realDurationInSamples
skipVar(); // 1
skipVar(); // lastRecord
skipVar(); // numSamplesLeft
out.lastState = (int)readVar(); // chLastState
uint64_t chFlag = readVar();
bool empty = (chFlag == FLAG_EMPTY);
bool useLongDeltas = (chFlag == FLAG_NOTEMPTY_LONG);
if (ch == 0) {
skipVar(); // 0
skipVar(); // BLOCK
skipVars(11);
if (useLongDeltas) {
skipVar(); // BLOCK
skipVars(6);
}
skipVar(); // BLOCK
} else {
skipVars(10);
if (useLongDeltas) {
skipVars(5);
}
}
uint64_t numEdges1 = readVar();
skipVar(); // 0
skipVar(); // numEdges
skipVar(); // 0
skipVar(); // numEdges
std::vector<uint32_t> deltas;
std::vector<int> newStates;
readEdges(deltas, newStates, useLongDeltas, (int)numEdges1);
(void)readByte(); // 0x00 terminator
if (ch == 0) {
skipVar(); // BLOCK
skipVars(6);
if (!useLongDeltas) {
skipVar(); // BLOCK
skipVars(6);
}
skipVar(); // BLOCK
} else {
skipVars(4);
if (!useLongDeltas) {
skipVars(5);
}
}
if (empty) {
skipVars(5);
out.initialState = out.lastState;
(void)nch;
return;
}
skipVar(); // 1
skipVar(); // 0
skipVar(); // 1
skipVar(); // 0
skipVar(); // 1
skipVars(16);
for (int i = 0; i < 8; i++) (void)readByte(); // 0xFF * 8
(void)readByte(); // chFlag raw
for (int i = 0; i < 7; i++) (void)readByte(); // 0x00 * 7
out.timestamps.reserve(deltas.size());
uint32_t ts = 0;
for (size_t i = 0; i < deltas.size(); i++) {
ts += deltas[i];
out.timestamps.push_back(ts);
out.states.push_back(newStates[i]);
}
if (!out.states.empty()) {
out.initialState = out.states[0] ? 0 : 1;
} else {
out.initialState = out.lastState;
}
}
// Read the full header + per-channel data block header for the generic case
// (any numChannels, any BLOCK marker). The file position is left right at
// the start of the first channel's CHANNEL_BLOCK record.
[[maybe_unused]] static void readChannelDataHeaderN(int nch) {
skipVar(); // BLOCK (scaledDurationInSamples block)
skipVar(); // scaledDurationInSamples
skipVars(5);
skipVar(); // numChannels
skipVars(3);
skipId();
skipVar();
skipVar(); // BLOCK
skipVars(3);
for (int i = 0; i < nch; i++) {
// readChannelHeader-equivalent: works for both writers since the
// "next pointer" tail is structurally identical (writeId + 3 vars).
skipVar(); // 0xff
skipVar(); // ch
skipString(); // name
skipVars(2);
skipDouble();
skipVar();
skipDouble();
skipVar();
skipDouble();
if (i == nch - 1) {
skipVar();
} else {
skipId();
for (int k = 0; k < 3; k++) skipVar();
}
}
skipVar(); // BLOCK
skipVars(6); // SUB_ARRAY
skipVars(6);
skipVar(); // BLOCK
skipVars(2);
skipVar(); // realDurationInSamples
skipVar();
skipVar(); // SUB
skipVar(); // reservedDurationInSamples
skipVar(); // freq/div
skipVars(2);
skipVar(); // SUB
skipVars(2);
skipVar(); // 1
skipVars(3);
skipId();
skipVar(); // BLOCK
skipVars(3); // SAM_ARRAY
skipVar();
skipVar(); // SUB
skipVar();
skipVar(); // BLOCK
skipVar();
skipVar(); // BLOCK
skipVars(4); // SUB four times
skipVar();
skipVar(); // BLOCK
skipVar(); // frequency
skipVars(3);
skipVar(); // 1
skipVars(3);
skipId();
skipVars(6); // ARR_6
skipVar(); // SUB
skipVar(); // BLOCK
skipVar(); // 0
skipVar(); // realDurationInSamples
skipVars(2);
skipVar(); // numChannels
skipVars(3); // ARR_3
}
// Read+skip the variant-agnostic file header up to (but not including) the
// channelDataHeader. Returns numChannels and stores channel names.
static int readHeaderN(LogicDataHeaderInfo& info) {
uint8_t magic = readByte();
if (magic != 0x7f) throw std::runtime_error("bad magic");
uint64_t firstVar = readVar();
info.hasVersionByte = (firstVar == 0x13);
uint64_t titleLen = readVar();
if (titleLen > 256) throw std::runtime_error("implausible title length");
info.title.resize((size_t)titleLen);
for (uint64_t k = 0; k < titleLen; k++) {
info.title[(size_t)k] = (char)readByte();
}
info.blockMarker = (int)readVar(); // BLOCK
skipVar(); // SUB
info.frequency = readVar();
skipVar(); // 0
skipVar(); // reservedDurationInSamples
skipVar(); // freq/div
skipVars(2);
info.numChannels = (int)readVar();
if (info.numChannels <= 0 || info.numChannels > 64) {
throw std::runtime_error("implausible numChannels");
}
skipVar(); // BLOCK
skipVar(); // 0
skipVar(); // BLOCK
for (int i = 0; i < info.numChannels; i++) {
skipId();
}
skipVar(); // 0
skipVar(); // BLOCK
skipId();
skipVar();
skipVar();
return info.numChannels;
}
// Decode one channel's pre-edge prologue (everything between CHANNEL_BLOCK
// and the numEdges triplet) and return numEdges + useLongDeltas + empty flag.
// Layout has subtle differences between our C++ writer (BLOCK=0x15) and real
// Saleae files (BLOCK=0x18). The known divergences for the real format:
// * ch == 0, useLongDeltas: 0 + BLOCK + 11 zeros + BLOCK (no extra
// BLOCK + 6 zeros block that our writer emits).
// * ch != 0, useLongDeltas: write(0, 13) (vs our writer's 10 + 5 = 15).
// The short-deltas paths and ch == 0 short-deltas match both writers.
static void readChannelPrologueGeneric(int ch, bool isReal,
uint64_t& numEdges, bool& useLongDeltas, bool& empty) {
skipVar(); // CHANNEL_BLOCK (0x16)
if (ch == 0) {
skipVar(); // SUB
skipVar(); // BLOCK
}
skipVar(); // ch + 1
skipVar(); // 0
skipVar(); // realDurationInSamples
skipVar(); // 1
skipVar(); // lastRecord
skipVar(); // numSamplesLeft
skipVar(); // chLastState
uint64_t chFlag = readVar();
empty = (chFlag == FLAG_EMPTY);
useLongDeltas = (chFlag == FLAG_NOTEMPTY_LONG);
if (ch == 0 && !isReal) {
// Our C++ writer's path for ch == 0.
skipVar(); // 0
skipVar(); // BLOCK
skipVars(11);
if (useLongDeltas) {
skipVar(); // BLOCK
skipVars(6);
}
skipVar(); // BLOCK
} else if (ch == 0 && isReal) {
// Real-Saleae ch == 0 has a structure of:
// 0, BLOCK, <variable run of zeros>, [optional BLOCK + zeros for
// useLongDeltas in some files], numEdges-triplet.
// Skip the leading "0, BLOCK" pair (two vars), then dynamically
// scan zero bytes until a non-zero length-prefix byte. If we hit
// another BLOCK marker (0x18 var = bytes "01 18") in the middle,
// consume it and keep scanning.
skipVar(); // 0
skipVar(); // BLOCK
if (empty) {
// Empty ch == 0 case never observed; fall back to writer recipe.
skipVars(useLongDeltas ? 18 : 11);
skipVar(); // BLOCK
} else {
while (true) {
int c = fgetc(g_fp);
if (c == EOF) throw std::runtime_error("EOF in ch0 prologue");
if (c == 0x00) continue;
if (c == 0x01) {
// Could be a BLOCK marker (01 18) or numEdges length=1.
int c2 = fgetc(g_fp);
if (c2 == EOF) throw std::runtime_error("EOF in ch0 prologue");
if (c2 == 0x18) {
// Inline BLOCK marker -- skip and keep scanning.
continue;
}
// numEdges length-prefix=1, value byte = c2. Push both
// back so readVar() below sees them.
ungetc(c2, g_fp);
ungetc(c, g_fp);
break;
}
// Any other non-zero byte starts the numEdges var length.
ungetc(c, g_fp);
break;
}
}
} else if (isReal) {
// In real-Saleae files the number of zero-vars between chFlag and
// the numEdges triplet varies for non-empty channels (observed: 5,
// 10, 13 zeros). For non-empty channels we robustly scan over any
// run of zero bytes, stopping at the first non-zero byte (which is
// the length prefix of numEdges -- always >= 1 since the channel
// has edges). For empty channels (chFlag == FLAG_EMPTY) numEdges
// itself is 0, so the writer emits a zero byte as the length tag
// for numEdges -- the dynamic scan cannot tell that zero apart
// from a padding zero. Use a fixed skip of 10 vars in that case
// (matches every observed empty real-Saleae channel so far).
if (empty) {
skipVars(10);
} else {
while (true) {
int c = fgetc(g_fp);
if (c == EOF) throw std::runtime_error("EOF in channel prologue");
if (c != 0x00) {
ungetc(c, g_fp);
break;
}
}
}
} else {
if (useLongDeltas) {
skipVars(15);
} else {
skipVars(10);
}
}
numEdges = readVar();
skipVar(); // 0
skipVar(); // numEdges
skipVar(); // 0
skipVar(); // numEdges
}
// Skip raw edge bytes: numEdges * (useLongDeltas ? 4 : 2) + 1 terminator.
static void skipEdgeBytesAndCollect(uint64_t numEdges, bool useLongDeltas,
std::vector<uint32_t>& deltas, std::vector<int>& newStates) {
for (uint64_t e = 0; e < numEdges; e++) {
uint8_t b0 = readByte();
uint8_t b1 = readByte();
uint32_t raw;
if (useLongDeltas) {
uint8_t b2 = readByte();
uint8_t b3 = readByte();
raw = (uint32_t)b0 | ((uint32_t)b1 << 8)
| ((uint32_t)b2 << 16) | ((uint32_t)b3 << 24);
} else {
raw = (uint32_t)b0 | ((uint32_t)b1 << 8);
}
int newState = 1;
uint32_t delta;
if (useLongDeltas) {
if (raw & SIGN_FLAG) { newState = 0; delta = raw & ~SIGN_FLAG; }
else { newState = 1; delta = raw; }
} else {
if (raw & 0x8000) { newState = 0; delta = raw & 0x7fff; }
else { newState = 1; delta = raw; }
}
deltas.push_back(delta);
newStates.push_back(newState);
}
(void)readByte(); // 0x00 terminator
}
// Scan the file forward until we find the next channel-block marker (the
// var-encoded value 0x16, which on the wire is the two bytes 0x01 0x16).
// To avoid matching false positives inside post-edge record qwords, we
// additionally require the bytes that follow the marker to look like a
// plausible channel header: `01 <expectedCh+1> 00 03 ...` (ch+1 var-encoded
// as a 1-byte value, then a 0 var, then a 3-byte length prefix introducing
// realDurationInSamples).
//
// Returns true if found; leaves g_fp positioned right before the matched
// 0x01 0x16 pair. If we hit EOF first, returns false.
static bool scanForNextChannelBlock(int expectedChPlus1) {
int prev = -1;
while (true) {
long here = ftell(g_fp);
int c = fgetc(g_fp);
if (c == EOF) return false;
if (prev == 0x01 && c == 0x16) {
// Peek next bytes; require: 01 <expectedChPlus1> 00 03
unsigned char peek[4];
size_t got = fread(peek, 1, 4, g_fp);
fseek(g_fp, here + 1 - (long)got, SEEK_CUR);
// Actually simpler: just fseek back to (here + 1) which is the
// byte right after the 0x16 byte we just consumed.
fseek(g_fp, here + 1, SEEK_SET);
if (got == 4
&& peek[0] == 0x01
&& peek[1] == (unsigned char)expectedChPlus1
&& peek[2] == 0x00
&& (peek[3] == 0x03 || peek[3] == 0x04)) {
// Rewind to just before 0x01 0x16 so caller's readVar() sees it.
fseek(g_fp, here - 1, SEEK_SET);
return true;
}
// Not a real header; keep scanning from after the 0x16.
prev = c;
continue;
}
prev = c;
}
}
} // namespace
LogicDataFull readLogicDataFull(const char* fileName) {
g_fp = fopen(fileName, "rb");
if (!g_fp) {
throw std::runtime_error(std::string("cannot open ") + fileName);
}
LogicDataFull result;
try {
int nch = readHeaderN(result.header);
// Now re-read the per-channel-header block to capture channel names.
// readChannelDataHeaderN above already skips them; we need a variant
// that captures names. Do it inline here (replacing the per-channel
// header loop from readChannelDataHeaderN).
skipVar(); // BLOCK (scaledDurationInSamples)
skipVar(); // scaledDurationInSamples
skipVars(5);
skipVar(); // numChannels
skipVars(3);
skipId();
skipVar();
skipVar(); // BLOCK
skipVars(3);
result.channels.resize(nch);
for (int i = 0; i < nch; i++) {
skipVar(); // 0xff
skipVar(); // ch
uint64_t nameLen = readVar();
std::string name((size_t)nameLen, '\0');
for (uint64_t k = 0; k < nameLen; k++) {
name[(size_t)k] = (char)readByte();
}
result.channels[i].name = name;
skipVars(2);
skipDouble();
skipVar();
skipDouble();
skipVar();
skipDouble();
if (i == nch - 1) {
skipVar();
} else {
skipId();
for (int k = 0; k < 3; k++) skipVar();
}
}
skipVar(); // BLOCK
skipVars(6); // SUB_ARRAY
skipVars(6);
skipVar(); // BLOCK
skipVars(2);
skipVar(); // realDurationInSamples
skipVar();
skipVar(); // SUB
skipVar(); // reservedDurationInSamples
skipVar(); // freq/div
skipVars(2);
skipVar(); // SUB
skipVars(2);
skipVar(); // 1
skipVars(3);
skipId();
skipVar(); // BLOCK
skipVars(3); // SAM_ARRAY
skipVar();
skipVar(); // SUB
skipVar();
skipVar(); // BLOCK
skipVar();
skipVar(); // BLOCK
skipVars(4);