source: thomson/tools/f2k5.c@ 6a5131e

main
Last change on this file since 6a5131e was 6a5131e, checked in by Adrien Destugues <pulkomandy@…>, 9 years ago

Add f2k5 sourcecode.

This is a simple tool to wrap a file with a K5 header.

git-svn-id: svn://localhost/thomson@62 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
43 for(int i = 2; i < l; i++)
44 head[l] += head[i];
45 head[l] = -head[l];
46 l++;
47 fwrite(head, 1, 17, out);
48
49 // DATA BLOCKS
50 for(;;)
51 {
52 uint8_t buf[254];
53 l = fread(buf, 1, 254, in);
54 if(l <= 0) break;
55
56 fwrite(sync, 1, sizeof(sync), out);
57 head[0] = 1;
58 head[1] = l + 2;
59 fwrite(head, 1, 2, out);
60 fwrite(buf, 1, l, out);
61
62 head[0] = 0;
63 while(l > 0)
64 head[0] -= buf[--l];
65 fwrite(head, 1, 1, out);
66 }
67
68 // FOOTER BLOCK
69 fwrite(sync, 1, sizeof(sync), out);
70 head[0] = 255;
71 head[1] = 2;
72 head[2] = 0;
73 fwrite(head, 1, 3, out);
74
75 fclose(in);
76 fclose(out);
77
78 }
79}
Note: See TracBrowser for help on using the repository browser.