-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
382 lines (316 loc) · 10.2 KB
/
main.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
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
/***************************************************************************
* Copyright (C) 2011-2014 by Petr Voytsik *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
*
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#define _GNU_SOURCE
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <gd.h>
#include <gdfontg.h>
const char program[] = "rdf_plot";
const char author[] = "Petr Voytsik";
const char version[] = "1.1";
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define FRAME_SIZE 40000UL
static const uint64_t syn64 = 0xFA463E11034CC457;
static const long shift = 60;
struct rdf_info{
char sig[5];
char date[19];
char station[18];
char source[18];
char exper[18];
time_t time0;
char name[128];
};
#define GET_FIELD(dest, src, n) \
do { \
strncpy(dest, src, n); \
dest[n] = 0; \
} while(0)
static void fill_rdf_info(const char *data, struct rdf_info *info)
{
struct tm date;
puts("********************");
GET_FIELD(info->sig, data, 4);
puts(info->sig);
GET_FIELD(info->date, &data[6], 18);
puts(info->date);
if(info->sig[3] == '1'){
GET_FIELD(info->station, &data[24], 11);
GET_FIELD(info->source, &data[35], 11);
GET_FIELD(info->exper, &data[46], 11);
}else if(info->sig[3] == '2'){
GET_FIELD(info->station, &data[0x18], 17);
GET_FIELD(info->source, &data[0x29], 17);
GET_FIELD(info->exper, &data[0x3A], 17);
}else{
return;
}
puts(info->station);
puts(info->source);
puts(info->exper);
puts("********************");
memset(&date, 0, sizeof(struct tm));
strptime(info->date, "%Y %j-%T", &date);
info->time0 = mktime(&date);
}
static void save_png(gdImagePtr im, const char *filename)
{
FILE *fp;
fp = fopen(filename, "wb");
if (!fp) {
fprintf(stderr, "Can't save png image %s\n", filename);
return;
}
gdImagePng(im, fp);
fclose(fp);
}
static void write_row(gdImagePtr im, int row, const char *data, unsigned len)
{
unsigned i, j;
int col = 0;
for(i = 0; i < len; i++)
for(j = 0; j < 8; j++){
if(data[i] & (1 << j))
gdImageSetPixel(im, col, row, 0x000FF00);
col++;
}
}
static void print_string(gdImagePtr im, int x, int y, const char *str)
{
gdFontPtr font = gdFontGetGiant();
x -= (10 + strlen(str)*font->w);
gdImageFilledRectangle(im, x-1, y,
x+strlen(str)*font->w,
y+font->h, 0x0);
gdImageString(im, font, x, y, (unsigned char *)str, 0x00FFFFFF);
}
static int print_800frames(const char *frame, size_t offset, const char *end,
const struct rdf_info *file_info)
{
static unsigned n = 0;
unsigned sec, i;
int ret = 0;
const unsigned w = 1200, h = 800;
time_t time = 0;
char time_str[64], sec_str[16];
char img_fname[256];
gdImagePtr im;
im = gdImageCreateTrueColor(w, h);
gdImageFilledRectangle(im, 0, 0, w-1, h-1, 0x0);
sec = offset / (40000*400);
printf("Save picture #%u: begin at %lu (%lu frame, %u sec)\n",
n++, offset, offset / 40000, sec);
for(i = 0; i < h; i++){
write_row(im, i, frame, w/8);
frame += FRAME_SIZE;
if(frame > end){
printf("End of file\n");
ret++;
break;
}
}
snprintf(img_fname, sizeof(img_fname), "%s_%04d.png", file_info->name, sec);
print_string(im, w, 10, file_info->exper);
print_string(im, w, 30, file_info->station);
time = file_info->time0 + sec;
ctime_r(&time, time_str);
time_str[strlen(time_str)-1] = 0;
print_string(im, w, 50, time_str);
sprintf(sec_str, "%02d:%02d", sec/60, sec%60);
print_string(im, w, 70, sec_str);
save_png(im, img_fname);
gdImageDestroy(im);
return ret;
}
static unsigned int bit_count64(uint64_t qword)
{
unsigned int c = 0;
for (c = 0; qword; c++)
qword &= qword - 1; // clear the least significant bit set
return c;
}
static unsigned int sync_cmp(const char *data)
{
return bit_count64( *(uint64_t *)data ^ syn64 );
}
static const char * find_sync(const char *data)
{
const char *p = NULL;
unsigned int i, c;
data += shift;
for(i = 0; i < 100*FRAME_SIZE; i++){
c = sync_cmp(&data[i]);
if(c <= 10){
p = &data[i];
printf("Found sync with %u wrong bits at 0x%lX\n", c, p-data);
p -= shift;
break;
}
}
if(!p)
printf("Could not find sync!\n");
return p;
}
void work(const char *data, size_t len, unsigned start,
unsigned interv, unsigned num, const char *file_name, int z)
{
unsigned i, n = 0, sec = 0;
const char *data_end = data + len;
const char *frame, *new_frame = NULL;
char *p;
char nulls2[2] = {0, 0};
struct rdf_info file_info;
/* At least 100 frames */
if(len < 100*FRAME_SIZE + 0x100){
fprintf(stderr, "File too short, exiting.\n");
return;
}
if(start * FRAME_SIZE * 400 > len){
fprintf(stderr, "Start after end of file\n");
return;
}
memset(&file_info, 0, sizeof(struct rdf_info));
/* Get information about file */
if(strncmp(data, "RDF", 3) == 0)
fill_rdf_info(data, &file_info);
strncpy(file_info.name, file_name, 128);
p = strrchr(file_info.name, '.');
if(p)
*p = 0;
frame = find_sync(data + start * FRAME_SIZE * 400);
if(!frame){
fprintf(stderr, "Can not find first frame\n");
return;
}
printf("First frame offset = 0x%lX\n", frame-data);
/*frame += start * FRAME_SIZE * 400;*/
do{
if(print_800frames(frame, frame-data, data_end, &file_info))
break;
frame += FRAME_SIZE * 400 * interv;
if(++n >= num){
printf("Stop!\n");
break;
}
if(frame > data_end){
printf("End of file\n");
break;
}
if(sync_cmp(frame + shift) >= 10 && z){
new_frame = find_sync(frame+shift);
if(!new_frame){
printf("Try to find something else...\n");
new_frame = frame;
for(i = 0; i < 50; i++){
new_frame = memmem(new_frame, 2*FRAME_SIZE,
nulls2, 2);
if(!new_frame)
break;
if(!memcmp(new_frame, new_frame+FRAME_SIZE, 2) &&
!memcmp(new_frame, new_frame+2*FRAME_SIZE, 2)){
puts("+");
break;
}
new_frame += 2;
}
if(!new_frame){
sec = (frame-data) / (40000*400);
printf("Can not find valid frame near second %u ", sec);
printf("Plot as is\n");
continue;
}
frame = new_frame - (shift+20);
}else
frame = new_frame;
}else{
printf("Ok!\n");
}
}while(frame < data_end);
}
static void usage()
{
printf("Usage: %s [-i interval] [-n number_of_im] [-s start_from] "
"[-z] <FILE>\n", program);
printf(" -i \t interal between images in seconds (default = 60s)\n");
printf(" -n \t maximum number of images (default = 30)\n");
printf(" -s \t start from second (default = 0)\n");
printf(" -z \t do not try to find next syncro-code\n");
}
int main(int argc, char *argv[])
{
char *ptr;
int fd;
struct stat sb;
size_t length;
/* Program options */
int opt;
unsigned interval = 60; /* Interval between images (sec) */
unsigned max_number = 30; /* Maximum number of images */
unsigned start_sec = 0;
char file_name[128];
int z = 1;
while ((opt = getopt(argc, argv, "hi:n:s:z")) != -1){
switch (opt) {
case 'i':
interval = atoi(optarg);
break;
case 'n':
max_number = atoi(optarg);
break;
case 's':
start_sec = atoi(optarg);
break;
case 'z':
z = 0;
break;
case 'h':
usage();
return EXIT_SUCCESS;
break;
default:
usage();
return EXIT_FAILURE;
}
}
if(optind >= argc){
usage();
return EXIT_FAILURE;
}
strncpy(file_name, argv[optind], 128);
fd = open(file_name, O_RDONLY);
if(fd < 0)
handle_error("open");
if (fstat(fd, &sb) < 0)
handle_error("fstat");
length = sb.st_size;
/*fprintf(stderr, "File \"%s\" size: %lu\n", file_name, length);*/
ptr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0L);
if (ptr == MAP_FAILED)
handle_error("mmap");
work(ptr, length, start_sec, interval, max_number, basename(file_name), z);
munmap(ptr, length);
close(fd);
return 0;
}