-
Notifications
You must be signed in to change notification settings - Fork 4
/
rkkernel.cpp
149 lines (109 loc) · 2.78 KB
/
rkkernel.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <errno.h>
#include <string.h>
#include "rkcrc.h"
#define MAGIC_CODE "KRNL"
const char* progname;
struct KRNL_HEADER
{
char magic[4];
uint32_t length;
KRNL_HEADER()
{
memcpy( magic, MAGIC_CODE, 4 );
length = 0;
}
};
int pack_krnl( FILE* fp_in, FILE* fp_out )
{
char buf[1024];
KRNL_HEADER header;
uint32_t crc = 0;
fwrite( &header, sizeof(header), 1, fp_out );
unsigned readlen;
while( (readlen = fread( buf, 1, sizeof(buf), fp_in )) != 0 )
{
header.length += readlen;
fwrite( buf, 1, readlen, fp_out );
RKCRC( crc, buf, readlen );
}
fwrite( &crc, sizeof(crc), 1, fp_out );
fseek( fp_out, 0, SEEK_SET );
fwrite( &header, sizeof(header), 1, fp_out );
printf( "%04X\n", crc );
return 0;
}
int unpack_krnl( FILE* fp_in, FILE* fp_out )
{
KRNL_HEADER header;
char buf[1024*16];
fprintf( stderr, "unpacking..." );
fflush( stderr );
if( sizeof(header) != fread( &header, 1, sizeof(header), fp_in ) )
{
err( EXIT_FAILURE, "%s: cannot read header from input file", __func__ );
}
fseek( fp_in, header.length + sizeof(header), SEEK_SET );
uint32_t file_crc;
if( sizeof(file_crc) != fread( &file_crc, 1, sizeof(file_crc), fp_in ) )
{
err( EXIT_FAILURE, "%s: cannot read crc from input file", __func__ );
}
size_t length = header.length;
fseek( fp_in, sizeof(header), SEEK_SET );
uint32_t crc = 0;
unsigned readlen;
while( length && (readlen = fread( buf, 1, sizeof(buf), fp_in )) != 0 )
{
length -= readlen;
fwrite( buf, 1, readlen, fp_out );
RKCRC( crc, buf, readlen );
}
if( file_crc != crc )
fprintf( stderr, "WARNING: bad crc checksum\n" );
fprintf( stderr, "OK\n" );
return 0;
}
void help()
{
fprintf( stderr, "usage: %s [-pack|-unpack] <input> <output>\n", progname );
exit( EXIT_FAILURE );
}
int main( int argc, char** argv )
{
progname = strrchr( argv[0], '/' );
if( !progname )
progname = argv[0];
else
++progname;
int action = 0;
if( argc != 4 )
{
help();
}
FILE* fp_in = fopen( argv[2], "rb" );
if( !fp_in )
{
err( EXIT_FAILURE, "%s: can't open input file '%s'", __func__, argv[2] );
}
FILE* fp_out = fopen( argv[3], "wb" );
if( !fp_out )
{
err( EXIT_FAILURE, "%s: can't open output file '%s'", __func__, argv[3] );
}
if( strcmp( argv[1], "-pack" ) == 0 )
{
pack_krnl( fp_in, fp_out );
}
else if( strcmp( argv[1], "-unpack" ) == 0 )
{
unpack_krnl( fp_in, fp_out );
}
else
{
help();
}
return 0;
}