source: thomson/tools/f2k5.cpp@ 2557cd0

main
Last change on this file since 2557cd0 was 2557cd0, checked in by Adrien Destugues <pulkomandy@…>, 11 years ago

Simple tool to wrap files in a K5 image.

git-svn-id: svn://localhost/thomson@34 85ae3b6b-dc8f-4344-a89d-598714f2e4e5

  • Property mode set to 100644
File size: 1.3 KB
Line 
1#include <stdint.h>
2#include <stdio.h>
3#include <string.h>
4
5int main(int argc, char* argv[])
6{
7 const uint8_t sync[] = {
8 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0x3C,0x5A
9 };
10 while(--argc > 0)
11 {
12 uint8_t l;
13 char* path = argv[argc];
14 FILE *in, *out;
15 in = fopen(path, "rb");
16 out = fopen("FILE.K5", "wb");
17
18 // HEADER BLOCK
19 fwrite(sync, 1, sizeof(sync), out);
20
21 uint8_t head[17];
22 l = 0;
23 head[l++] = 0; head[l++] = 16;
24 for(int i = 0; l < 13;)
25 {
26 if(i >= strlen(path)) break;
27 if(path[i] == '.') i++;
28 if(path[i] == '/') {
29 l = 2;
30 i++;
31 continue;
32 }
33 head[l++] = path[i++];
34 }
35
36 while(l < 13) head[l++] = ' ';
37
38 head[l++] = 2;
39 head[l++] = 0;
40 head[l++] = 0;
41 head[l] = 0;
42 for(int i = 2; i < l; i++)
43 head[l] += head[i];
44 head[l++] ^= 255;
45 fwrite(head, 1, 17, out);
46
47 // DATA BLOCKS
48 for(;;)
49 {
50 uint8_t buf[254];
51 l = fread(buf, 1, 254, in);
52 if(l <= 0) break;
53
54 fwrite(sync, 1, sizeof(sync), out);
55 head[0] = 1;
56 head[1] = l + 2;
57 fwrite(head, 1, 2, out);
58 fwrite(buf, 1, l, out);
59 while(l > 0)
60 head[0] += buf[--l];
61 head[0] ^= 0xFF;
62 fwrite(head, 1, 1, out);
63 }
64
65 // FOOTER BLOCK
66 fwrite(sync, 1, sizeof(sync), out);
67 head[0] = 255;
68 head[1] = 2;
69 head[2] = 0;
70 fwrite(head, 1, 3, out);
71
72 fclose(in);
73 fclose(out);
74
75 }
76}
Note: See TracBrowser for help on using the repository browser.