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
|
/*
* Funcs for working with SRF (Garmin vehicle) files
*
* Written by Mike Frysinger <vapier@gentoo.org>
* Released into the public domain
*/
#include <stdio.h>
#include "pm_c_util.h"
#include "mallocvar.h"
#include "nstring.h"
#include "srf.h"
static unsigned char
csumRaw(void * const p,
size_t const len) {
unsigned char retval;
unsigned int i;
unsigned char * c;
for (i = 0, retval = 0, c = p; i < len; ++i)
retval += *c++;
return retval;
}
static unsigned char
csumPstring(struct srf_pstring * const pstringP) {
return
csumRaw(&pstringP->len, 4) + csumRaw(pstringP->val, pstringP->len);
}
static bool
readPstring(FILE * const ifP,
struct srf_pstring * const pstringP) {
size_t bytesRead;
pm_readlittlelong2u(ifP, &pstringP->len);
MALLOCARRAY(pstringP->val, pstringP->len + 1);
if (!pstringP->val)
pm_error("Failed to allocate buffer to read %u-byte pstring",
pstringP->len);
bytesRead = fread(pstringP->val, 1, pstringP->len, ifP);
if (bytesRead != pstringP->len)
pm_error("Failed to read pstring. Requested %u bytes, got %u",
(unsigned)pstringP->len, (unsigned)bytesRead);
pstringP->val[pstringP->len] = '\0';
return true;
}
static bool
writePstring(FILE * const ofP,
struct srf_pstring * const pstringP) {
bool retval;
size_t bytesWritten;
pm_writelittlelongu(ofP, pstringP->len);
bytesWritten = fwrite(pstringP->val, 1, pstringP->len, ofP);
if (bytesWritten == pstringP->len)
retval = true;
else
retval = false;
return retval;
}
static size_t
lenHeader(struct srf_header * const headerP) {
return 16 + (4 * 4) + 4 + headerP->s578.len
+ 4 + 4 + headerP->ver.len
+ 4 + 4 + headerP->prod.len;
}
static unsigned char
csumHeader(struct srf_header * const headerP) {
return
csumRaw(headerP->magic, 16) +
csumRaw(&headerP->_int4, 2 * 4) +
csumRaw(&headerP->img_cnt, 4) +
csumRaw(&headerP->_int5, 4) +
csumPstring(&headerP->s578) +
csumRaw(&headerP->_int6, 4) +
csumPstring(&headerP->ver) +
csumRaw(&headerP->_int7, 4) +
csumPstring(&headerP->prod);
}
static bool
readHeader(FILE * const ifP,
struct srf_header * const headerP) {
bool const retval =
fread(headerP->magic, 1, 16, ifP) == 16 &&
pm_readlittlelong2u(ifP, &headerP->_int4[0]) == 0 &&
pm_readlittlelong2u(ifP, &headerP->_int4[1]) == 0 &&
pm_readlittlelong2u(ifP, &headerP->img_cnt) == 0 &&
pm_readlittlelong2u(ifP, &headerP->_int5) == 0 &&
readPstring(ifP, &headerP->s578) &&
pm_readlittlelong2u(ifP, &headerP->_int6) == 0 &&
readPstring(ifP, &headerP->ver) &&
pm_readlittlelong2u(ifP, &headerP->_int7) == 0 &&
readPstring(ifP, &headerP->prod);
headerP->magic[16] = '\0';
return retval;
}
static bool
writeHeader(FILE * const ofP,
struct srf_header * const headerP) {
return
fwrite(headerP->magic, 1, 16, ofP) == 16 &&
pm_writelittlelongu(ofP, headerP->_int4[0]) == 0 &&
pm_writelittlelongu(ofP, headerP->_int4[1]) == 0 &&
pm_writelittlelongu(ofP, headerP->img_cnt) == 0 &&
pm_writelittlelongu(ofP, headerP->_int5) == 0 &&
writePstring(ofP, &headerP->s578) &&
pm_writelittlelongu(ofP, headerP->_int6) == 0 &&
writePstring(ofP, &headerP->ver) &&
pm_writelittlelongu(ofP, headerP->_int7) == 0 &&
writePstring(ofP, &headerP->prod);
}
static bool
checkHeader(struct srf_header * const headerP) {
return
streq(headerP->magic, SRF_MAGIC) &&
headerP->_int4[0] == 4 &&
headerP->_int4[1] == 4 &&
/* Should we require img_cnt to be multiple of 2 ? */
headerP->img_cnt > 0 &&
headerP->_int5 == 5 &&
headerP->s578.len == 3 &&
strcmp(headerP->s578.val, "578") == 0 &&
headerP->_int6 == 6 &&
headerP->ver.len == 4 &&
/* Allow any headerP->ver value */
headerP->_int7 == 7 &&
headerP->prod.len == 12;
/* Allow any headerP->prod value */
}
static size_t
lenImg(struct srf_img * const imgP) {
return
(4 * 3) + (2 * 2) + (1 * 2) + 2 + 4 +
4 + 4 + imgP->alpha.data_len +
4 + 4 + imgP->data.data_len;
}
static unsigned char
csumImg(struct srf_img * const imgP) {
struct srf_img_header * const headerP = &imgP->header;
struct srf_img_alpha * const alphaP = &imgP->alpha;
struct srf_img_data * const dataP = &imgP->data;
return
csumRaw(&headerP->_ints, 4 * 3) +
csumRaw(&headerP->height, 2) +
csumRaw(&headerP->width, 2) +
csumRaw(headerP->_bytes, 2) +
csumRaw(&headerP->line_len, 2) +
csumRaw(&headerP->zeros, 4) +
csumRaw(&alphaP->type, 4) +
csumRaw(&alphaP->data_len, 4) +
csumRaw(alphaP->data, alphaP->data_len) +
csumRaw(&dataP->type, 4) +
csumRaw(&dataP->data_len, 4) +
csumRaw(dataP->data, dataP->data_len);
}
static bool
readImgHeader(FILE * const ifP,
struct srf_img_header * const headerP) {
return
pm_readlittlelong2u(ifP, &headerP->_ints[0]) == 0 &&
pm_readlittlelong2u(ifP, &headerP->_ints[1]) == 0 &&
pm_readlittlelong2u(ifP, &headerP->_ints[2]) == 0 &&
pm_readlittleshortu(ifP, &headerP->height) == 0 &&
pm_readlittleshortu(ifP, &headerP->width) == 0 &&
fread(&headerP->_bytes[0], 1, 1, ifP) == 1 &&
fread(&headerP->_bytes[1], 1, 1, ifP) == 1 &&
pm_readlittleshortu(ifP, &headerP->line_len) == 0 &&
pm_readlittlelong2u(ifP, &headerP->zeros) == 0;
}
static bool
writeImgHeader(FILE * const ofP,
struct srf_img_header * const headerP) {
return
pm_writelittlelongu(ofP, headerP->_ints[0]) == 0 &&
pm_writelittlelongu(ofP, headerP->_ints[1]) == 0 &&
pm_writelittlelongu(ofP, headerP->_ints[2]) == 0 &&
pm_writelittleshortu(ofP, headerP->height) == 0 &&
pm_writelittleshortu(ofP, headerP->width) == 0 &&
fwrite(&headerP->_bytes[0], 1, 1, ofP) == 1 &&
fwrite(&headerP->_bytes[1], 1, 1, ofP) == 1 &&
pm_writelittleshortu(ofP, headerP->line_len) == 0 &&
pm_writelittlelongu(ofP, headerP->zeros) == 0;
}
static bool
checkImgHeader(struct srf_img_header * const headerP) {
return
headerP->_ints[0] == 0 &&
headerP->_ints[1] == 16 &&
headerP->_ints[2] == 0 &&
headerP->_bytes[0] == 16 &&
headerP->_bytes[1] == 8 &&
headerP->line_len == headerP->width * 2 &&
headerP->zeros == 0;
}
static bool
readImgAlpha(FILE * const ifP,
struct srf_img_alpha * const alphaP) {
bool retval;
pm_readlittlelong2u(ifP, &alphaP->type);
pm_readlittlelong2u(ifP, &alphaP->data_len);
MALLOCARRAY(alphaP->data, alphaP->data_len);
if (!alphaP->data)
retval = false;
else {
size_t bytesRead;
bytesRead = fread(alphaP->data, 1, alphaP->data_len, ifP);
retval = (bytesRead ==alphaP->data_len);
}
return retval;
}
static bool
writeImageAlpha(FILE * const ofP,
struct srf_img_alpha * const alphaP) {
return
pm_writelittlelongu(ofP, alphaP->type) == 0 &&
pm_writelittlelongu(ofP, alphaP->data_len) == 0 &&
fwrite(alphaP->data, 1, alphaP->data_len, ofP) == alphaP->data_len;
}
static bool
checkImgAlpha(struct srf_img_alpha * const alphaP) {
return (alphaP->type == 11);
}
static bool
readImgData(FILE * const ifP,
struct srf_img_data * const dataP) {
bool retval;
pm_readlittlelong2u(ifP, &dataP->type);
pm_readlittlelong2u(ifP, &dataP->data_len);
MALLOCARRAY(dataP->data, dataP->data_len / 2);
if (!dataP->data)
retval = false;
else {
size_t bytesRead;
bytesRead = fread(dataP->data, 2, dataP->data_len / 2, ifP);
retval = (bytesRead == dataP->data_len / 2);
}
return retval;
}
static bool
writeImgData(FILE * const ofP,
struct srf_img_data * const dataP) {
return
pm_writelittlelongu(ofP, dataP->type) == 0 &&
pm_writelittlelongu(ofP, dataP->data_len) == 0 &&
fwrite(dataP->data, 2, dataP->data_len / 2, ofP)
== dataP->data_len / 2;
}
static bool
checkImgData(struct srf_img_data * const dataP) {
return dataP->type == 1;
}
static bool
readImg(FILE * const ifP,
bool const verbose,
uint32_t const i,
struct srf_img * const imgP) {
if (!readImgHeader(ifP, &imgP->header))
pm_error("short srf image %u header", i);
if (!checkImgHeader(&imgP->header))
pm_error("invalid srf image %u header", i);
if (verbose)
pm_message("reading srf 16-bit RGB %ux%u image %u",
imgP->header.width, imgP->header.height, i);
if (!readImgAlpha(ifP, &imgP->alpha))
pm_error("short srf image %u alpha mask", i);
if (!checkImgAlpha(&imgP->alpha))
pm_error("invalid srf image %u alpha mask", i);
if (!readImgData(ifP, &imgP->data))
pm_error("short srf image %u data", i);
if (!checkImgData(&imgP->data))
pm_error("invalid srf image %u data", i);
return true;
}
static bool
writeImg(FILE * const ofP,
uint32_t const i,
struct srf_img * const imgP) {
if (!checkImgHeader(&imgP->header))
pm_error("invalid srf image %u header", i);
if (!writeImgHeader(ofP, &imgP->header))
pm_error("short srf image %u header", i);
if (!checkImgAlpha(&imgP->alpha))
pm_error("invalid srf image %u alpha mask", i);
if (!writeImageAlpha(ofP, &imgP->alpha))
pm_error("short srf image %u alpha mask", i);
if (!checkImgData(&imgP->data))
pm_error("invalid srf image %u data", i);
if (!writeImgData(ofP, &imgP->data))
pm_error("short srf image %u data", i);
return true;
}
static uint8_t
csum(struct srf * const srfP,
size_t const padLen) {
/*----------------------------------------------------------------------------
The sum of everything in the SRF image except the checksum byte. The
checksum byte is supposed to be the arithmetic opposite of this so that the
sum of everything is zero.
-----------------------------------------------------------------------------*/
unsigned char retval;
unsigned int i;
retval = csumHeader(&srfP->header);
for (i = 0; i < srfP->header.img_cnt; ++i)
retval += csumImg(&srfP->imgs[i]);
for (i = 0; i < padLen; ++i)
retval += 0xff;
return retval;
}
void
srf_read(FILE * const ifP,
bool const verbose,
struct srf * const srfP) {
uint8_t trialCsum;
size_t padLen;
unsigned char pad[256];
unsigned int i;
if (!readHeader(ifP, &srfP->header))
pm_error("short srf header");
if (!checkHeader(&srfP->header))
pm_error("invalid srf header");
if (verbose)
pm_message("reading srf ver %s with prod code %s and %u images",
srfP->header.ver.val, srfP->header.prod.val,
srfP->header.img_cnt);
MALLOCARRAY(srfP->imgs, srfP->header.img_cnt);
if (!srfP->imgs)
pm_error("Could not allocate memory for %u images",
srfP->header.img_cnt);
for (i = 0; i < srfP->header.img_cnt; ++i)
if (!readImg(ifP, verbose, i, &srfP->imgs[i]))
pm_error("invalid srf image %u", i);
padLen = fread(pad, 1, sizeof(pad), ifP);
if (!feof(ifP)) {
pm_errormsg("excess data at end of file");
return;
}
trialCsum = csum(srfP, 0); /* initial value */
for (i = 0; i < padLen; ++i)
trialCsum += pad[i];
if (trialCsum != 0)
pm_errormsg("checksum does not match");
}
void
srf_write(FILE * const ofP,
struct srf * const srfP) {
uint8_t srfCsum; /* checksum value in SRF image */
size_t padLen;
unsigned int i;
size_t bytesWritten;
padLen = 1; /* initial value */
if (!checkHeader(&srfP->header))
pm_error("invalid srf header");
if (!writeHeader(ofP, &srfP->header))
pm_error("write srf header");
padLen += lenHeader(&srfP->header);
for (i = 0; i < srfP->header.img_cnt; ++i) {
if (!writeImg(ofP, i, &srfP->imgs[i]))
pm_error("invalid srf image %u", i);
padLen += lenImg(&srfP->imgs[i]);
}
/* Pad to 256 bytes */
padLen = 256 - (padLen % 256);
if (padLen) {
char * d;
size_t bytesWritten;
MALLOCARRAY(d, padLen);
if (!d)
pm_error("Could not allocate memory for %u bytes of padding",
(unsigned)padLen);
memset(d, 0xff, padLen);
bytesWritten = fwrite(d, 1, padLen, ofP);
if (bytesWritten != padLen)
pm_error("unable to 0xff pad file");
free(d);
}
/* Write out checksum byte */
srfCsum = 0xff - csum(srfP, padLen) + 1;
bytesWritten = fwrite(&srfCsum, 1, 1, ofP);
if (bytesWritten != 1)
pm_error("unable to write checksum");
}
static void
freeImg(struct srf_img * const imgP) {
free(imgP->alpha.data);
free(imgP->data.data);
}
void
srf_term(struct srf * const srfP) {
unsigned int i;
free(srfP->header.s578.val);
free(srfP->header.ver.val);
free(srfP->header.prod.val);
for (i = 0; i < srfP->header.img_cnt; ++i)
freeImg(&srfP->imgs[i]);
free(srfP->imgs);
}
static void
srf_img_init(struct srf_img * const imgP,
uint16_t const width,
uint16_t const height) {
struct srf_img_header * const headerP = &imgP->header;
struct srf_img_alpha * const alphaP = &imgP->alpha;
struct srf_img_data * const dataP = &imgP->data;
headerP->_ints[0] = 0;
headerP->_ints[1] = 16;
headerP->_ints[2] = 0;
headerP->height = height;
headerP->width = width;
headerP->_bytes[0] = 16;
headerP->_bytes[1] = 8;
headerP->line_len = width * 2;
headerP->zeros = 0;
alphaP->type = 11;
alphaP->data_len = height * width;
MALLOCARRAY(alphaP->data, alphaP->data_len);
if (!alphaP->data)
pm_error("Could not allocate buffer for %u bytes of alpha",
alphaP->data_len);
dataP->type = 1;
dataP->data_len = height * width * 2;
MALLOCARRAY(dataP->data, dataP->data_len / 2);
if (!dataP->data)
pm_error("Could not allocation buffer for %u units of data",
dataP->data_len);
}
static void
initPstring(struct srf_pstring * const pstringP,
const char * const s) {
pstringP->len = strlen(s);
MALLOCARRAY(pstringP->val, pstringP->len + 1);
if (!pstringP->val)
pm_error("Could not allocate memory for string of length %u",
pstringP->len);
memcpy(pstringP->val, s, pstringP->len + 1);
}
void
srf_init(struct srf * const srfP) {
struct srf_header * const headerP = &srfP->header;
strcpy(headerP->magic, SRF_MAGIC);
headerP->_int4[0] = 4;
headerP->_int4[1] = 4;
headerP->img_cnt = 0;
headerP->_int5 = 5;
initPstring(&headerP->s578, "578");
headerP->_int6 = 6;
initPstring(&headerP->ver, "1.00");
headerP->_int7 = 7;
initPstring(&headerP->prod, "006-D0578-XX");
srfP->imgs = NULL;
}
void
srf_create_img(struct srf * const srfP,
uint16_t const width,
uint16_t const height) {
/*----------------------------------------------------------------------------
Add an "image" to the SRF. An image is a horizontal series of 36
square frames, each showing a different angle view of an object, 10
degrees about. At least that's what it's supposed to be. We don't
really care -- it's just an arbitrary rectangular raster image to us.
-----------------------------------------------------------------------------*/
++srfP->header.img_cnt;
REALLOCARRAY(srfP->imgs, srfP->header.img_cnt);
if (!srfP->imgs)
pm_error("Could not allocate memory for %u images",
srfP->header.img_cnt);
srf_img_init(&srfP->imgs[srfP->header.img_cnt-1], width, height);
}
|