-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_file.c
602 lines (472 loc) · 12.2 KB
/
data_file.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
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
#include "data_file.h"
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "fletcher.h"
enum {
DF_IO_READ = 0x1000,
DF_IO_WRITE = 0x2000,
DF_CHKSUM = 0x8000
};
static int df_read_line(struct df*);
static int df_write_line(struct df*);
static int df_write_close(struct df*);
struct df* df_open(FILE* fp, int flags, const char* type_id,
size_t data_width)
{
if (!fp)
return 0;
struct df* df;
size_t idlen = strlen(type_id);
if (idlen > data_width)
{
debug("fail with type id string '%s' longer than data width %zd\n",
type_id, data_width);
return 0;
}
debug("opening file type id '%s'\n", type_id);
df = malloc(sizeof(*df));
if (!df)
{
debug("failed to allocate data for file '%s'\n", type_id);
fclose(fp);
return 0;
}
df->type_id = malloc(idlen + 1);
strcpy(df->type_id, type_id);
df->fp = fp;
df->flags = flags;
df->width = data_width;
df->cp = df->buf;
df->chksum = df->buf + df->width;
df->chkcp = df->chkdata;
if (flags & DF_READ)
{
size_t len = strlen(type_id);
if (!df_read_line(df))
return df_close(df);
if (strncmp(df->buf, type_id, len) != 0)
{
debug("file failed type id '%s' verification\n", type_id);
return df_close(df);
}
df->cp = df->buf + len;
}
else
{
if (!df_write_string(df, type_id, strlen(type_id)))
{
debug("failed to save type id data\n");
return df_close(df);
}
}
return df;
}
int df_read_v_chksum(struct df* df, uint8_t* chka, uint8_t* chkb)
{
uint8_t read_chka, read_chkb;
uint8_t calc_chka, calc_chkb;
if (!(df->flags & DF_READ))
{
debug("not for reading!\n");
return (df->flags = 0);
}
if (!df_read_hex_byte(df, &read_chka)
|| !df_read_hex_byte(df, &read_chkb))
{
debug("failed to read v checksum\n");
return (df->flags = 0);
}
fletcher16(&calc_chka, &calc_chkb, (uint8_t*)df->chkdata,
strlen(df->chkdata) - 4);
if (calc_chka != read_chka
|| calc_chkb != read_chkb)
{
debug("file failed v checksum match\n");
debug("chkdata:'%s'\n", df->chkdata);
return (df->flags = 0);
}
debug("v checksum ok\n");
if (chka && chkb)
{
*chka = calc_chka;
*chkb = calc_chkb;
}
return 1;
}
void* df_close(struct df* df)
{
if (!df)
return 0;
debug("closing file type id '%s'\n", df->type_id);
if (df->flags & DF_WRITE)
df_write_close(df);
free(df->type_id);
free(df);
return 0;
}
static int df_read_line(struct df* df)
{
uint8_t calc_chka = 0;
uint8_t calc_chkb = 0;
uint8_t read_chka = 0;
uint8_t read_chkb = 0;
size_t len;
if (!(df->flags & DF_READ))
{
debug("not for reading!\n");
return (df->flags = 0);
}
if (!fgets(df->buf, 80, df->fp))
{
debug("failed to read line in '%s'\n", df->type_id);
return (df->flags = 0);
}
df->cp = df->buf;
while(*df->cp >= ' ')
++df->cp;
*df->cp = '\0';
len = strlen(df->buf) - 4;
if (len != df->width)
{
debug("line length mismatch (desired:%zd actual:%zd)\n",
df->width, len);
return (df->flags = 0);
}
df->flags |= DF_CHKSUM;
df->cp = df->chksum;
if (!df_read_hex_byte(df, &read_chka)
|| !df_read_hex_byte(df, &read_chkb))
{
debug("failed to read line checksum in '%s'\n", df->type_id);
return (df->flags = 0);
}
fletcher16(&calc_chka, &calc_chkb, (uint8_t*)df->buf, df->width);
if (read_chka != calc_chka || read_chkb != calc_chkb)
{
debug("checksum mismatch in file '%s'\n", df->type_id);
return (df->flags = 0);
}
strncpy(df->chkcp, df->chksum, 4);
df->chkcp += 4;
*df->chkcp = '\0';
*df->chksum = '\0';
df->flags = DF_READ;
df->cp = df->buf;
return 1;
}
char* df_read_string(struct df* df, size_t length)
{
if (!(df->flags & DF_READ))
{
debug("not for reading!\n");
df->flags = 0;
return 0;
}
char* ret = malloc(length + 1);
char* r = ret;
unsigned int c = 0;
int trim = !(df->flags & DF_RSTR_NOTRIM);
if (!ret)
{
debug("failed to read string of %zd\n", length);
df->flags = 0;
return 0;
}
/* in case string only contains space characters */
*r = '\0';
while (c < length)
{
if (*df->cp == '\0')
{
if (df->cp != df->chksum /* misplaced termination */
|| !df_read_line(df)) /* short of file */
{
df->flags = 0;
return 0;
}
}
if (trim)
{
while (c < length && *df->cp == ' ')
{
++c;
++df->cp;
}
if (*df->cp)
trim = 0;
}
while (c < length && *df->cp)
{
++c;
*r++ = *df->cp++;
}
}
*r = '\0';
return ret;
}
int df_read_hex_byte(struct df* df, uint8_t* result)
{
if (!(df->flags & DF_READ))
{
debug("not for reading!\n");
return (df->flags = 0);
}
unsigned int i, c, r;
for (c = r = 0; c < 2; ++c)
{
if (*df->cp == '\0')
{
if ((df->flags & DF_CHKSUM) /* checksum cut short by line */
|| df->cp != df->chksum /* or misplaced termination */
|| !df_read_line(df)) /* hex byte short of file */
{
debug("line termination\n");
return (df->flags = 0);
}
}
if (*df->cp >= '0' && *df->cp <= '9')
i = *df->cp - '0';
else if (*df->cp >= 'a' && *df->cp <= 'f')
i = 10 + *df->cp - 'a';
else
{
debug("invalid hex character:'%c' (%d)\n", *df->cp, *df->cp);
return (df->flags = 0);
}
r = (r << 4) | i;
df->cp++;
}
*result = r;
return 1;
}
int df_read_hex_word(struct df* df, uint16_t* result)
{
if (!(df->flags & DF_READ))
{
debug("not for reading!\n");
return (df->flags = 0);
}
unsigned int i, c, r;
for (c = r = 0; c < 4; ++c)
{
if (*df->cp == '\0')
{
if ((df->flags & DF_CHKSUM) /* checksum cut short by line */
|| df->cp != df->chksum /* or misplaced termination */
|| !df_read_line(df)) /* or hex word short of file */
{
debug("line termination\n");
return (df->flags = 0);
}
}
if (*df->cp >= '0' && *df->cp <= '9')
i = *df->cp - '0';
else if (*df->cp >= 'a' && *df->cp <= 'f')
i = 10 + *df->cp - 'a';
else
{
debug("invalid hex character:'%c' (%d)\n", *df->cp, *df->cp);
return (df->flags = 0);
}
r = (r << 4) | i;
df->cp++;
}
*result = r;
return 1;
}
int df_read_hex_nibble_array(struct df* df, uint8_t* data, size_t count)
{
if (!(df->flags & DF_READ))
{
debug("not for reading!\n");
return (df->flags = 0);
}
while(count--)
{
if (*df->cp == '\0')
{
if (df->cp != df->chksum
|| !df_read_line(df))
{
debug("line termination\n");
return (df->flags = 0);
}
}
if (*df->cp >= '0' && *df->cp <= '9')
*data++ = *df->cp - '0';
else if (*df->cp >= 'a' && *df->cp <= 'f')
*data++ = 10 + *df->cp - 'a';
else
{
debug("invalid hex character:'%c' (%d)\n", *df->cp, *df->cp);
return (df->flags = 0);
}
++df->cp;
}
return 1;
}
static int df_write_close(struct df* df)
{
if (!(df->flags & DF_WRITE))
{
debug("not for writing!\n");
return (df->flags = 0);
}
debug("writing file type id '%s'\n", df->type_id);
char* chksum = df->buf + df->width;
uint8_t chka = 0;
uint8_t chkb = 0;
if (chksum - df->cp < 4)
if (!df_write_line(df))
return (df->flags = 0);
debug("chkdata: '%s'\n", df->chkdata);
fletcher16(&chka, &chkb, (uint8_t*)df->chkdata, strlen(df->chkdata));
df_write_hex_byte(df, chka);
df_write_hex_byte(df, chkb);
df_write_line(df);
return 1;
}
static int df_write_line(struct df* df)
{
if (!(df->flags & DF_WRITE))
{
debug("not for writing!\n");
return (df->flags = 0);
}
uint8_t chka = 0;
uint8_t chkb = 0;
char* chksum = df->buf + df->width;
*chksum = '\0';
while(df->cp < chksum)
*df->cp++ = '0';
if (df->cp > chksum)
{
debug("cp past chksum error\n");
return (df->flags = 0);
}
if (df->cp != chksum)
{
debug("cp not at end\n"); debug("'%s'\n",df->cp);
return (df->flags = 0);
}
fletcher16(&chka, &chkb, (uint8_t*)df->buf, df->width);
snprintf(chksum, 5, "%02x%02x", chka, chkb);
df->chkcp += snprintf(df->chkcp, 5, "%s", chksum);
fprintf(df->fp, "%s\n", df->buf);
df->cp = df->buf;
return 1;
}
int df_write_string(struct df* df, const char* str, size_t length)
{
if (!(df->flags & DF_WRITE))
{
debug("not for writing!\n");
return (df->flags = 0);
}
char* chksum = df->buf + df->width;
size_t slen = strlen(str);
if (df->flags & DF_WSTR_RPAD)
{
while (length)
{
if (df->cp == chksum)
if (!df_write_line(df))
return (df->flags = 0);
if (!slen && length)
*df->cp++ = ' ';
else
{
*df->cp++ = *str++;
--slen;
}
--length;
}
return 1;
}
else /* left pad (default) */
{
while (length)
{
if (df->cp == chksum)
if (!df_write_line(df))
return (df->flags = 0);
if (length > slen)
*df->cp++ = ' ';
else
{
*df->cp++ = *str++;
--slen;
}
--length;
}
}
return 1;
}
int df_write_hex_byte(struct df* df, uint8_t n)
{
if (!(df->flags & DF_WRITE))
{
debug("not for writing!\n");
return (df->flags = 0);
}
char* chksum = df->buf + df->width;
unsigned int c;
for (c = 0; c < 2; ++c)
{
if (df->cp == chksum)
if (!df_write_line(df))
return (df->flags = 0);
unsigned int d = (n & 0xf0) >> 4;
if (d < 10)
*df->cp++ = '0' + d;
else if (d <= 15)
*df->cp++ = 'a' + (d - 10);
n = n << 4;
}
return 1;
}
int df_write_hex_word(struct df* df, uint16_t n)
{
if (!(df->flags & DF_WRITE))
{
debug("not for writing!\n");
return (df->flags = 0);
}
char* chksum = df->buf + df->width;
unsigned int c;
for (c = 0; c < 4; ++c)
{
if (df->cp == chksum)
if (!df_write_line(df))
return (df->flags = 0);
unsigned int d = (n & 0xf000) >> 12;
if (d < 10)
*df->cp++ = '0' + d;
else if (d <= 15)
*df->cp++ = 'a' + (d - 10);
n = n << 4;
}
return 1;
}
int df_write_hex_nibble_array(struct df* df, uint8_t* data, size_t count)
{
if (!(df->flags & DF_WRITE))
{
debug("not for writing!\n");
return (df->flags = 0);
}
char* chksum = df->buf + df->width;
while(count--)
{
unsigned int n = *data++ & 0x0f;
if (df->cp == chksum)
if (!df_write_line(df))
return (df->flags = 0);
if (n < 10)
*df->cp++ = '0' + n;
else if (n <= 15)
*df->cp++ = 'a' + (n - 10);
}
return 1;
}