-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdumpv2.c
241 lines (208 loc) · 6.31 KB
/
dumpv2.c
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
/* Dump Taurus v2 packets to figure out store format
G. Helffrich/ELSI/Tokyo Tech.
8 Feb. 2023
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
char *prog, cmap[256];
short wo; /* word order: 0 - le, 1 - be */
int odmp;
void err(char *msg){
fprintf(stderr, "%s: %s\n", prog, msg); fflush(stderr);
exit(1);
}
void erroff(size_t off, char *msg){
fprintf(stderr, "%s: at offset 0x%zx, %s\n", prog, off, msg); fflush(stderr);
exit(1);
}
int hw(unsigned char *p){
return (p[0] << 8) | p[1];
}
int fw(unsigned char *p){
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
}
/* Check if end of data in buffer */
int ckend(unsigned char buf[]){
if (buf[0] != 'E') return 0;
if (buf[1] != 'N') return 0;
if (buf[2] != 'D') return 0;
if (buf[3] != 'O') return 0;
if (buf[4] != 'D') return 0;
if (buf[5] != 'A') return 0;
if (buf[6] != 'T') return 0;
if (buf[7] != 'A') return 0;
return 1;
}
/* Check for store header */
int dfhd(off_t off, unsigned char buf[]){
off_t siz;
if (
(buf[0] != 'N') ||
(buf[1] != 'M') ||
(buf[2] != 'X') ||
(buf[3] != 'V') ||
(buf[32] != 'V') ||
(buf[33] != 'O') ||
(buf[34] != 'L') ||
(buf[35] != 'F')
) return 0;
siz = fw(buf+16);
printf("NMX store header at %llx, size (hex) %llx\n", off, siz);
return 1;
}
/* Dump packet header and text */
int dhdr(off_t off, unsigned char buf[]){
/* Payload types: (v2)
c3 - alert information
c0 - configuration information (zipped data? contains "PK" and "zip")
a5 - log data, trigger info ?
a3 - telemetry log data ?
a1 - ARM log data ?
9f - Java log data, extension 00; size is length of text?;
9 bytes of control info, then text.
9b - ? something with its own internal sequence number
99 - ? something with a lat and lon attached; temperature/mass pos?
time sequence is about every 10 minutes; payload length is 58,
59 or 60. name is always 0xab, addition is always 00.
89 - 1000 1001 stream 1
8b - 1000 1011 stream 2
8d - 1000 1101 stream 3
*/
struct timeval tv;
struct tm *tm;
union {
uint64_t nsec;
uint32_t w[2];
} u;
int seq = fw(buf+4);
int siz = hw(buf+2);
int i, eop, next;
printf("%zx %02d %04d ", (size_t)off, buf[31], hw(buf+32));
u.w[1-wo] = fw(buf+12); u.w[wo] = fw(buf+16);
tv.tv_sec = u.nsec/1000000000l;
tv.tv_usec = (u.nsec%1000000000l)/1000;
tm = gmtime(&tv.tv_sec);
printf("%4d/%02d/%02d %02d:%02d:%02d.%03d ",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec/1000);
for(i=0; i<8; i++) printf("%02x",buf[8+i]); /* Time in hex */
printf(" %04d %04d\n", seq, siz);
printf(" %8.3f %9.3f %4dm ",
((float)(fw(buf+20)))*1e-6,
((float)(fw(buf+24)))*1e-6,
hw(buf+28));
i = hw(buf+30) & 0x0f;
printf("%s %04d ",
(i == 11) ? "Taurus" : ((i == 13) ? "Trident" : "(unknown)"),
hw(buf+32));
printf("band %02x seq %d\n", buf[34], fw(buf+8));
eop = 37;
next = hw(buf+35);
if (next && odmp) {
char str[16];
printf("Extension (%d bytes):", next);
for(i=0;i<next;i++) {
if (i%16 == 0) {
if (i) printf(" *%16.16s*",str);
printf("\n ");
}
printf(" %02x", buf[eop+1+i]); str[i%16] = cmap[buf[eop+1+i]];
}
if (i%16) {
int j, k=i%16, l=16-k;
for(j=1; j<=l; j++) printf(" "); printf(" *%*.*s*", k, k, str);
}
}
eop += next;
if (!odmp) return siz;
printf("\nContent (%d bytes):", siz-eop);
{ char str[16];
for (i=0; i<siz-eop && i<64; i++) {
if (i%16 == 0) {
if (i) printf(" *%16.16s*",str);
printf("\n ");
}
printf(" %02x", buf[eop+1+i]); str[i%16] = cmap[buf[eop+1+i]];
}
if (i != siz-eop) printf(" ... "); else printf(" ");
if (i%16 || i==64) {
int j, k=i%16, l=16-k;
if (i!=64) for(j=1; j<=l; j++) printf(" "); else k=16;
printf("*%*.*s*", k, k, str);
}
}
printf("\n");
return siz;
}
int main(int argc, char *argv[]){
FILE *fd;
off_t off = 0;
size_t siz;
char ok = 1;
char *cbuf, *store = NULL, *ptr;
int i;
unsigned char buf[0x100000];
prog = argv[0];
i = 1; wo = ((char *)&i) == 0;
/* Set up character map */
for(i=0;i<256;i++) cmap[i] = '.';
cbuf = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"!@#$%^&*()_+-={}[]:|;'\\\"~`<>?,./ ";
/* "§±!@#$%^&*()_+-={}[]:|;'\\\"~`<>?,./ "; */
for(i=0; i<strlen(cbuf); i++) cmap[(unsigned char)(cbuf[i])] = cbuf[i];
for(i=1; i<argc; i++) {
if (argv[i][0] == '-') { /* Check for option */
switch (argv[i][1]) {
case 'd': /* -d */
odmp = 1;
break;
case 'o': /* -o <offset> */
i += 1; off = strtoull(argv[i], &ptr, 16);
if ((ptr-argv[i] < strlen(argv[i])) || off == 0) goto bad;
break;
default:
goto bad;
}
continue;
bad:
fprintf(stderr, "%s: **Bad arg (ignored): %s\n", prog, argv[i]);
} else {
store = argv[i];
}
}
/* Open file */
if (store == NULL) err("no packet file given");
fd = fopen(store, "r");
if (fd == NULL) err("bad packet file name");
if (off == 0) {
siz = fread(buf, sizeof(char), 68, fd);
if (68 != siz || !dfhd(off, buf)) err("not a store file");
off += 68;
} else
if (0 != fseeko(fd, off, SEEK_SET)) erroff(off, "bad initial offset");
do {
int psiz;
siz = fread(buf, sizeof(char), 37, fd);
if (siz <= 0) break;
if (ckend(buf)) {
off += 0x100000; off &= ~0xfffff; /* Keep going until EOF */
off += 0x44;
} else {
if (buf[0] != 'N' || buf[1] != 'P') erroff(off, "bad packet header");
psiz = hw(buf+2);
if (psiz-37 != fread(buf+37, sizeof(char), psiz-37, fd))
erroff(off, "end of file");
siz = dhdr(off, buf);
off += (siz+3) & ~0x03; /* Round to word multiple */
}
if (0 != fseeko(fd, off, SEEK_SET)) erroff(off, "bad offset");
} while(ok);
fclose(fd);
return 0;
}