Debian Codegraph @

converter/other/srf.c

raw
1 /*
2 * Funcs for working with SRF (Garmin vehicle) files
3 *
4 * Written by Mike Frysinger <[email protected]>
5 * Released into the public domain
6 */
7
8 #include <stdio.h>
9
10 #include "pm_c_util.h"
11 #include "mallocvar.h"
12 #include "nstring.h"
13 #include "srf.h"
14
15
16 static unsigned char
17 csumRaw(void * const p,
18 size_t const len) {
19
20 unsigned char retval;
21
22 unsigned int i;
23 unsigned char * c;
24
25 for (i = 0, retval = 0, c = p; i < len; ++i)
26 retval += *c++;
27
28 return retval;
29 }
30
31
32
33 static unsigned char
34 csumPstring(struct srf_pstring * const pstringP) {
35
36 return
37 csumRaw(&pstringP->len, 4) + csumRaw(pstringP->val, pstringP->len);
38 }
39
40
41
42 static bool
43 readPstring(FILE * const ifP,
44 struct srf_pstring * const pstringP) {
45
46 size_t bytesRead;
47
48 pm_readlittlelong2u(ifP, &pstringP->len);
49
50 MALLOCARRAY(pstringP->val, pstringP->len + 1);
51
52 if (!pstringP->val)
53 pm_error("Failed to allocate buffer to read %u-byte pstring",
54 pstringP->len);
55
56 bytesRead = fread(pstringP->val, 1, pstringP->len, ifP);
57 if (bytesRead != pstringP->len)
58 pm_error("Failed to read pstring. Requested %u bytes, got %u",
59 (unsigned)pstringP->len, (unsigned)bytesRead);
60
61 pstringP->val[pstringP->len] = '\0';
62
63 return true;
64 }
65
66
67
68 static bool
69 writePstring(FILE * const ofP,
70 struct srf_pstring * const pstringP) {
71
72 bool retval;
73 size_t bytesWritten;
74
75 pm_writelittlelongu(ofP, pstringP->len);
76
77 bytesWritten = fwrite(pstringP->val, 1, pstringP->len, ofP);
78
79 if (bytesWritten == pstringP->len)
80 retval = true;
81 else
82 retval = false;
83
84 return retval;
85 }
86
87
88
89 static size_t
90 lenHeader(struct srf_header * const headerP) {
91
92 return 16 + (4 * 4) + 4 + headerP->s578.len
93 + 4 + 4 + headerP->ver.len
94 + 4 + 4 + headerP->prod.len;
95 }
96
97
98
99 static unsigned char
100 csumHeader(struct srf_header * const headerP) {
101
102 return
103 csumRaw(headerP->magic, 16) +
104 csumRaw(&headerP->_int4, 2 * 4) +
105 csumRaw(&headerP->img_cnt, 4) +
106 csumRaw(&headerP->_int5, 4) +
107 csumPstring(&headerP->s578) +
108 csumRaw(&headerP->_int6, 4) +
109 csumPstring(&headerP->ver) +
110 csumRaw(&headerP->_int7, 4) +
111 csumPstring(&headerP->prod);
112 }
113
114
115
116 static bool
117 readHeader(FILE * const ifP,
118 struct srf_header * const headerP) {
119
120 bool const retval =
121 fread(headerP->magic, 1, 16, ifP) == 16 &&
122 pm_readlittlelong2u(ifP, &headerP->_int4[0]) == 0 &&
123 pm_readlittlelong2u(ifP, &headerP->_int4[1]) == 0 &&
124 pm_readlittlelong2u(ifP, &headerP->img_cnt) == 0 &&
125 pm_readlittlelong2u(ifP, &headerP->_int5) == 0 &&
126 readPstring(ifP, &headerP->s578) &&
127 pm_readlittlelong2u(ifP, &headerP->_int6) == 0 &&
128 readPstring(ifP, &headerP->ver) &&
129 pm_readlittlelong2u(ifP, &headerP->_int7) == 0 &&
130 readPstring(ifP, &headerP->prod);
131
132 headerP->magic[16] = '\0';
133
134 return retval;
135 }
136
137
138
139 static bool
140 writeHeader(FILE * const ofP,
141 struct srf_header * const headerP) {
142
143 return
144 fwrite(headerP->magic, 1, 16, ofP) == 16 &&
145 pm_writelittlelongu(ofP, headerP->_int4[0]) == 0 &&
146 pm_writelittlelongu(ofP, headerP->_int4[1]) == 0 &&
147 pm_writelittlelongu(ofP, headerP->img_cnt) == 0 &&
148 pm_writelittlelongu(ofP, headerP->_int5) == 0 &&
149 writePstring(ofP, &headerP->s578) &&
150 pm_writelittlelongu(ofP, headerP->_int6) == 0 &&
151 writePstring(ofP, &headerP->ver) &&
152 pm_writelittlelongu(ofP, headerP->_int7) == 0 &&
153 writePstring(ofP, &headerP->prod);
154 }
155
156
157
158 static bool
159 checkHeader(struct srf_header * const headerP) {
160
161 return
162 streq(headerP->magic, SRF_MAGIC) &&
163 headerP->_int4[0] == 4 &&
164 headerP->_int4[1] == 4 &&
165 /* Should we require img_cnt to be multiple of 2 ? */
166 headerP->img_cnt > 0 &&
167 headerP->_int5 == 5 &&
168 headerP->s578.len == 3 &&
169 strcmp(headerP->s578.val, "578") == 0 &&
170 headerP->_int6 == 6 &&
171 headerP->ver.len == 4 &&
172 /* Allow any headerP->ver value */
173 headerP->_int7 == 7 &&
174 headerP->prod.len == 12;
175 /* Allow any headerP->prod value */
176 }
177
178
179
180 static size_t
181 lenImg(struct srf_img * const imgP) {
182
183 return
184 (4 * 3) + (2 * 2) + (1 * 2) + 2 + 4 +
185 4 + 4 + imgP->alpha.data_len +
186 4 + 4 + imgP->data.data_len;
187 }
188
189
190
191 static unsigned char
192 csumImg(struct srf_img * const imgP) {
193
194 struct srf_img_header * const headerP = &imgP->header;
195 struct srf_img_alpha * const alphaP = &imgP->alpha;
196 struct srf_img_data * const dataP = &imgP->data;
197
198 return
199 csumRaw(&headerP->_ints, 4 * 3) +
200 csumRaw(&headerP->height, 2) +
201 csumRaw(&headerP->width, 2) +
202 csumRaw(headerP->_bytes, 2) +
203 csumRaw(&headerP->line_len, 2) +
204 csumRaw(&headerP->zeros, 4) +
205 csumRaw(&alphaP->type, 4) +
206 csumRaw(&alphaP->data_len, 4) +
207 csumRaw(alphaP->data, alphaP->data_len) +
208 csumRaw(&dataP->type, 4) +
209 csumRaw(&dataP->data_len, 4) +
210 csumRaw(dataP->data, dataP->data_len);
211 }
212
213
214
215 static bool
216 readImgHeader(FILE * const ifP,
217 struct srf_img_header * const headerP) {
218 return
219 pm_readlittlelong2u(ifP, &headerP->_ints[0]) == 0 &&
220 pm_readlittlelong2u(ifP, &headerP->_ints[1]) == 0 &&
221 pm_readlittlelong2u(ifP, &headerP->_ints[2]) == 0 &&
222 pm_readlittleshortu(ifP, &headerP->height) == 0 &&
223 pm_readlittleshortu(ifP, &headerP->width) == 0 &&
224 fread(&headerP->_bytes[0], 1, 1, ifP) == 1 &&
225 fread(&headerP->_bytes[1], 1, 1, ifP) == 1 &&
226 pm_readlittleshortu(ifP, &headerP->line_len) == 0 &&
227 pm_readlittlelong2u(ifP, &headerP->zeros) == 0;
228 }
229
230
231
232 static bool
233 writeImgHeader(FILE * const ofP,
234 struct srf_img_header * const headerP) {
235 return
236 pm_writelittlelongu(ofP, headerP->_ints[0]) == 0 &&
237 pm_writelittlelongu(ofP, headerP->_ints[1]) == 0 &&
238 pm_writelittlelongu(ofP, headerP->_ints[2]) == 0 &&
239 pm_writelittleshortu(ofP, headerP->height) == 0 &&
240 pm_writelittleshortu(ofP, headerP->width) == 0 &&
241 fwrite(&headerP->_bytes[0], 1, 1, ofP) == 1 &&
242 fwrite(&headerP->_bytes[1], 1, 1, ofP) == 1 &&
243 pm_writelittleshortu(ofP, headerP->line_len) == 0 &&
244 pm_writelittlelongu(ofP, headerP->zeros) == 0;
245 }
246
247
248
249 static bool
250 checkImgHeader(struct srf_img_header * const headerP) {
251
252 return
253 headerP->_ints[0] == 0 &&
254 headerP->_ints[1] == 16 &&
255 headerP->_ints[2] == 0 &&
256 headerP->_bytes[0] == 16 &&
257 headerP->_bytes[1] == 8 &&
258 headerP->line_len == headerP->width * 2 &&
259 headerP->zeros == 0;
260 }
261
262
263
264 static bool
265 readImgAlpha(FILE * const ifP,
266 struct srf_img_alpha * const alphaP) {
267
268 bool retval;
269
270 pm_readlittlelong2u(ifP, &alphaP->type);
271 pm_readlittlelong2u(ifP, &alphaP->data_len);
272
273 MALLOCARRAY(alphaP->data, alphaP->data_len);
274
275 if (!alphaP->data)
276 retval = false;
277 else {
278 size_t bytesRead;
279
280 bytesRead = fread(alphaP->data, 1, alphaP->data_len, ifP);
281 retval = (bytesRead ==alphaP->data_len);
282 }
283 return retval;
284 }
285
286
287
288 static bool
289 writeImageAlpha(FILE * const ofP,
290 struct srf_img_alpha * const alphaP) {
291
292 return
293 pm_writelittlelongu(ofP, alphaP->type) == 0 &&
294 pm_writelittlelongu(ofP, alphaP->data_len) == 0 &&
295 fwrite(alphaP->data, 1, alphaP->data_len, ofP) == alphaP->data_len;
296 }
297
298
299
300 static bool
301 checkImgAlpha(struct srf_img_alpha * const alphaP) {
302
303 return (alphaP->type == 11);
304 }
305
306
307
308 static bool
309 readImgData(FILE * const ifP,
310 struct srf_img_data * const dataP) {
311
312 bool retval;
313
314 pm_readlittlelong2u(ifP, &dataP->type);
315 pm_readlittlelong2u(ifP, &dataP->data_len);
316
317 MALLOCARRAY(dataP->data, dataP->data_len / 2);
318
319 if (!dataP->data)
320 retval = false;
321 else {
322 size_t bytesRead;
323 bytesRead = fread(dataP->data, 2, dataP->data_len / 2, ifP);
324
325 retval = (bytesRead == dataP->data_len / 2);
326 }
327 return retval;
328 }
329
330
331
332 static bool
333 writeImgData(FILE * const ofP,
334 struct srf_img_data * const dataP) {
335
336 return
337 pm_writelittlelongu(ofP, dataP->type) == 0 &&
338 pm_writelittlelongu(ofP, dataP->data_len) == 0 &&
339 fwrite(dataP->data, 2, dataP->data_len / 2, ofP)
340 == dataP->data_len / 2;
341 }
342
343
344
345 static bool
346 checkImgData(struct srf_img_data * const dataP) {
347 return dataP->type == 1;
348 }
349
350
351
352 static bool
353 readImg(FILE * const ifP,
354 bool const verbose,
355 uint32_t const i,
356 struct srf_img * const imgP) {
357
358 if (!readImgHeader(ifP, &imgP->header))
359 pm_error("short srf image %u header", i);
360 if (!checkImgHeader(&imgP->header))
361 pm_error("invalid srf image %u header", i);
362
363 if (verbose)
364 pm_message("reading srf 16-bit RGB %ux%u image %u",
365 imgP->header.width, imgP->header.height, i);
366
367 if (!readImgAlpha(ifP, &imgP->alpha))
368 pm_error("short srf image %u alpha mask", i);
369 if (!checkImgAlpha(&imgP->alpha))
370 pm_error("invalid srf image %u alpha mask", i);
371
372 if (!readImgData(ifP, &imgP->data))
373 pm_error("short srf image %u data", i);
374 if (!checkImgData(&imgP->data))
375 pm_error("invalid srf image %u data", i);
376
377 return true;
378 }
379
380
381
382 static bool
383 writeImg(FILE * const ofP,
384 uint32_t const i,
385 struct srf_img * const imgP) {
386
387 if (!checkImgHeader(&imgP->header))
388 pm_error("invalid srf image %u header", i);
389 if (!writeImgHeader(ofP, &imgP->header))
390 pm_error("short srf image %u header", i);
391
392 if (!checkImgAlpha(&imgP->alpha))
393 pm_error("invalid srf image %u alpha mask", i);
394 if (!writeImageAlpha(ofP, &imgP->alpha))
395 pm_error("short srf image %u alpha mask", i);
396
397 if (!checkImgData(&imgP->data))
398 pm_error("invalid srf image %u data", i);
399 if (!writeImgData(ofP, &imgP->data))
400 pm_error("short srf image %u data", i);
401
402 return true;
403 }
404
405
406
407 static uint8_t
408 csum(struct srf * const srfP,
409 size_t const padLen) {
410 /*----------------------------------------------------------------------------
411 The sum of everything in the SRF image except the checksum byte. The
412 checksum byte is supposed to be the arithmetic opposite of this so that the
413 sum of everything is zero.
414 -----------------------------------------------------------------------------*/
415 unsigned char retval;
416 unsigned int i;
417
418 retval = csumHeader(&srfP->header);
419
420 for (i = 0; i < srfP->header.img_cnt; ++i)
421 retval += csumImg(&srfP->imgs[i]);
422
423 for (i = 0; i < padLen; ++i)
424 retval += 0xff;
425
426 return retval;
427 }
428
429
430
431 void
432 srf_read(FILE * const ifP,
433 bool const verbose,
434 struct srf * const srfP) {
435
436 uint8_t trialCsum;
437 size_t padLen;
438 unsigned char pad[256];
439 unsigned int i;
440
441 if (!readHeader(ifP, &srfP->header))
442 pm_error("short srf header");
443 if (!checkHeader(&srfP->header))
444 pm_error("invalid srf header");
445
446 if (verbose)
447 pm_message("reading srf ver %s with prod code %s and %u images",
448 srfP->header.ver.val, srfP->header.prod.val,
449 srfP->header.img_cnt);
450
451 MALLOCARRAY(srfP->imgs, srfP->header.img_cnt);
452
453 if (!srfP->imgs)
454 pm_error("Could not allocate memory for %u images",
455 srfP->header.img_cnt);
456
457 for (i = 0; i < srfP->header.img_cnt; ++i)
458 if (!readImg(ifP, verbose, i, &srfP->imgs[i]))
459 pm_error("invalid srf image %u", i);
460
461 padLen = fread(pad, 1, sizeof(pad), ifP);
462 if (!feof(ifP)) {
463 pm_errormsg("excess data at end of file");
464 return;
465 }
466
467 trialCsum = csum(srfP, 0); /* initial value */
468 for (i = 0; i < padLen; ++i)
469 trialCsum += pad[i];
470 if (trialCsum != 0)
471 pm_errormsg("checksum does not match");
472 }
473
474
475
476 void
477 srf_write(FILE * const ofP,
478 struct srf * const srfP) {
479
480 uint8_t srfCsum; /* checksum value in SRF image */
481 size_t padLen;
482 unsigned int i;
483 size_t bytesWritten;
484
485 padLen = 1; /* initial value */
486
487 if (!checkHeader(&srfP->header))
488 pm_error("invalid srf header");
489 if (!writeHeader(ofP, &srfP->header))
490 pm_error("write srf header");
491 padLen += lenHeader(&srfP->header);
492
493 for (i = 0; i < srfP->header.img_cnt; ++i) {
494 if (!writeImg(ofP, i, &srfP->imgs[i]))
495 pm_error("invalid srf image %u", i);
496 padLen += lenImg(&srfP->imgs[i]);
497 }
498
499 /* Pad to 256 bytes */
500 padLen = 256 - (padLen % 256);
501 if (padLen) {
502 char * d;
503 size_t bytesWritten;
504
505 MALLOCARRAY(d, padLen);
506
507 if (!d)
508 pm_error("Could not allocate memory for %u bytes of padding",
509 (unsigned)padLen);
510
511 memset(d, 0xff, padLen);
512
513 bytesWritten = fwrite(d, 1, padLen, ofP);
514
515 if (bytesWritten != padLen)
516 pm_error("unable to 0xff pad file");
517
518 free(d);
519 }
520
521 /* Write out checksum byte */
522 srfCsum = 0xff - csum(srfP, padLen) + 1;
523 bytesWritten = fwrite(&srfCsum, 1, 1, ofP);
524 if (bytesWritten != 1)
525 pm_error("unable to write checksum");
526 }
527
528
529
530 static void
531 freeImg(struct srf_img * const imgP) {
532
533 free(imgP->alpha.data);
534 free(imgP->data.data);
535 }
536
537
538
539 void
540 srf_term(struct srf * const srfP) {
541
542 unsigned int i;
543
544 free(srfP->header.s578.val);
545 free(srfP->header.ver.val);
546 free(srfP->header.prod.val);
547
548 for (i = 0; i < srfP->header.img_cnt; ++i)
549 freeImg(&srfP->imgs[i]);
550
551 free(srfP->imgs);
552 }
553
554
555
556 static void
557 srf_img_init(struct srf_img * const imgP,
558 uint16_t const width,
559 uint16_t const height) {
560
561 struct srf_img_header * const headerP = &imgP->header;
562 struct srf_img_alpha * const alphaP = &imgP->alpha;
563 struct srf_img_data * const dataP = &imgP->data;
564
565 headerP->_ints[0] = 0;
566 headerP->_ints[1] = 16;
567 headerP->_ints[2] = 0;
568 headerP->height = height;
569 headerP->width = width;
570 headerP->_bytes[0] = 16;
571 headerP->_bytes[1] = 8;
572 headerP->line_len = width * 2;
573 headerP->zeros = 0;
574
575 alphaP->type = 11;
576 alphaP->data_len = height * width;
577 MALLOCARRAY(alphaP->data, alphaP->data_len);
578
579 if (!alphaP->data)
580 pm_error("Could not allocate buffer for %u bytes of alpha",
581 alphaP->data_len);
582
583 dataP->type = 1;
584 dataP->data_len = height * width * 2;
585 MALLOCARRAY(dataP->data, dataP->data_len / 2);
586
587 if (!dataP->data)
588 pm_error("Could not allocation buffer for %u units of data",
589 dataP->data_len);
590 }
591
592
593
594 static void
595 initPstring(struct srf_pstring * const pstringP,
596 const char * const s) {
597
598 pstringP->len = strlen(s);
599
600 MALLOCARRAY(pstringP->val, pstringP->len + 1);
601
602 if (!pstringP->val)
603 pm_error("Could not allocate memory for string of length %u",
604 pstringP->len);
605
606 memcpy(pstringP->val, s, pstringP->len + 1);
607 }
608
609
610
611 void
612 srf_init(struct srf * const srfP) {
613
614 struct srf_header * const headerP = &srfP->header;
615
616 strcpy(headerP->magic, SRF_MAGIC);
617 headerP->_int4[0] = 4;
618 headerP->_int4[1] = 4;
619 headerP->img_cnt = 0;
620 headerP->_int5 = 5;
621 initPstring(&headerP->s578, "578");
622 headerP->_int6 = 6;
623 initPstring(&headerP->ver, "1.00");
624 headerP->_int7 = 7;
625 initPstring(&headerP->prod, "006-D0578-XX");
626
627 srfP->imgs = NULL;
628 }
629
630
631
632 void
633 srf_create_img(struct srf * const srfP,
634 uint16_t const width,
635 uint16_t const height) {
636 /*----------------------------------------------------------------------------
637 Add an "image" to the SRF. An image is a horizontal series of 36
638 square frames, each showing a different angle view of an object, 10
639 degrees about. At least that's what it's supposed to be. We don't
640 really care -- it's just an arbitrary rectangular raster image to us.
641 -----------------------------------------------------------------------------*/
642
643 ++srfP->header.img_cnt;
644
645 REALLOCARRAY(srfP->imgs, srfP->header.img_cnt);
646
647 if (!srfP->imgs)
648 pm_error("Could not allocate memory for %u images",
649 srfP->header.img_cnt);
650
651 srf_img_init(&srfP->imgs[srfP->header.img_cnt-1], width, height);
652 }
653
654
655