7 FILE *infile, *outfile;
41 struct zinfo_subtract {
49 struct zinfo_common common;
50 struct zinfo_copy copy;
51 struct zinfo_pack pack;
52 struct zinfo_subtract subtract;
56 union zinfo_record *zinfo;
57 unsigned int num_entries;
60 static unsigned long align ( unsigned long value, unsigned long align ) {
61 return ( ( value + align - 1 ) & ~( align - 1 ) );
64 static int read_file ( const char *filename, void **buf, size_t *len ) {
68 file = fopen ( filename, "r" );
70 fprintf ( stderr, "Could not open %s: %s\n", filename,
75 if ( fstat ( fileno ( file ), &stat ) < 0 ) {
76 fprintf ( stderr, "Could not stat %s: %s\n", filename,
82 *buf = malloc ( *len );
84 fprintf ( stderr, "Could not malloc() %d bytes for %s: %s\n",
85 *len, filename, strerror ( errno ) );
89 if ( fread ( *buf, 1, *len, file ) != *len ) {
90 fprintf ( stderr, "Could not read %d bytes from %s: %s\n",
91 *len, filename, strerror ( errno ) );
104 static int read_input_file ( const char *filename,
105 struct input_file *input ) {
106 return read_file ( filename, &input->buf, &input->len );
109 static int read_zinfo_file ( const char *filename,
110 struct zinfo_file *zinfo ) {
114 if ( read_file ( filename, &buf, &len ) < 0 )
117 if ( ( len % sizeof ( *(zinfo->zinfo) ) ) != 0 ) {
118 fprintf ( stderr, ".zinfo file %s has invalid length %d\n",
124 zinfo->num_entries = ( len / sizeof ( *(zinfo->zinfo) ) );
128 static int alloc_output_file ( size_t max_len, struct output_file *output ) {
130 output->max_len = ( max_len );
131 output->buf = malloc ( max_len );
132 if ( ! output->buf ) {
133 fprintf ( stderr, "Could not allocate %d bytes for output\n",
137 memset ( output->buf, 0xff, sizeof ( output->buf ) );
141 static int process_zinfo_copy ( struct input_file *input,
142 struct output_file *output,
143 union zinfo_record *zinfo ) {
144 struct zinfo_copy *copy = &zinfo->copy;
145 size_t offset = copy->offset;
146 size_t len = copy->len;
148 if ( ( offset + len ) > input->len ) {
149 fprintf ( stderr, "Input buffer overrun on copy\n" );
153 output->len = align ( output->len, copy->align );
154 if ( ( output->len + len ) > output->max_len ) {
155 fprintf ( stderr, "Output buffer overrun on copy\n" );
160 fprintf ( stderr, "COPY [%#zx,%#zx) to [%#zx,%#zx)\n", offset, ( offset + len ),
161 output->len, ( output->len + len ) );
164 memcpy ( ( output->buf + output->len ),
165 ( input->buf + offset ), len );
170 static int process_zinfo_pack ( struct input_file *input,
171 struct output_file *output,
172 union zinfo_record *zinfo ) {
173 struct zinfo_pack *pack = &zinfo->pack;
174 size_t offset = pack->offset;
175 size_t len = pack->len;
176 unsigned long packed_len;
178 if ( ( offset + len ) > input->len ) {
179 fprintf ( stderr, "Input buffer overrun on pack\n" );
183 output->len = align ( output->len, pack->align );
184 if ( output->len > output->max_len ) {
185 fprintf ( stderr, "Output buffer overrun on pack\n" );
189 if ( ucl_nrv2b_99_compress ( ( input->buf + offset ), len,
190 ( output->buf + output->len ),
191 &packed_len, 0 ) != UCL_E_OK ) {
192 fprintf ( stderr, "Compression failure\n" );
197 fprintf ( stderr, "PACK [%#zx,%#zx) to [%#zx,%#zx)\n", offset, ( offset + len ),
198 output->len, ( output->len + packed_len ) );
201 output->len += packed_len;
202 if ( output->len > output->max_len ) {
203 fprintf ( stderr, "Output buffer overrun on pack\n" );
210 static int process_zinfo_subtract ( struct input_file *input,
211 struct output_file *output,
212 struct zinfo_subtract *subtract,
214 size_t offset = subtract->offset;
220 if ( ( offset + datasize ) > output->len ) {
221 fprintf ( stderr, "Subtract at %#zx outside output buffer\n",
226 target = ( output->buf + offset );
227 delta = ( ( align ( output->len, subtract->divisor ) -
228 align ( input->len, subtract->divisor ) )
229 / subtract->divisor );
231 switch ( datasize ) {
233 uint8_t *byte = target;
239 uint16_t *word = target;
245 uint32_t *dword = target;
251 fprintf ( stderr, "Unsupported subtract datasize %d\n",
257 fprintf ( stderr, "SUBx [%#zx,%#zx) (%#lx+(%#lx/%#x)-(%#lx/%#x)) = %#lx\n",
258 offset, ( offset + datasize ), old, output->len, subtract->divisor,
259 input->len, subtract->divisor, new );
265 static int process_zinfo_subb ( struct input_file *input,
266 struct output_file *output,
267 union zinfo_record *zinfo ) {
268 return process_zinfo_subtract ( input, output, &zinfo->subtract, 1 );
271 static int process_zinfo_subw ( struct input_file *input,
272 struct output_file *output,
273 union zinfo_record *zinfo ) {
274 return process_zinfo_subtract ( input, output, &zinfo->subtract, 2 );
277 static int process_zinfo_subl ( struct input_file *input,
278 struct output_file *output,
279 union zinfo_record *zinfo ) {
280 return process_zinfo_subtract ( input, output, &zinfo->subtract, 4 );
283 struct zinfo_processor {
285 int ( * process ) ( struct input_file *input,
286 struct output_file *output,
287 union zinfo_record *zinfo );
290 static struct zinfo_processor zinfo_processors[] = {
291 { "COPY", process_zinfo_copy },
292 { "PACK", process_zinfo_pack },
293 { "SUBB", process_zinfo_subb },
294 { "SUBW", process_zinfo_subw },
295 { "SUBL", process_zinfo_subl },
298 static int process_zinfo ( struct input_file *input,
299 struct output_file *output,
300 union zinfo_record *zinfo ) {
301 struct zinfo_common *common = &zinfo->common;
302 struct zinfo_processor *processor;
303 char type[ sizeof ( common->type ) + 1 ] = "";
306 strncat ( type, common->type, sizeof ( type ) - 1 );
307 for ( i = 0 ; i < ( sizeof ( zinfo_processors ) /
308 sizeof ( zinfo_processors[0] ) ) ; i++ ) {
309 processor = &zinfo_processors[i];
310 if ( strcmp ( processor->type, type ) == 0 )
311 return processor->process ( input, output, zinfo );
314 fprintf ( stderr, "Unknown zinfo record type \"%s\"\n", &type[0] );
318 static int write_output_file ( struct output_file *output ) {
319 if ( fwrite ( output->buf, 1, output->len, stdout ) != output->len ) {
320 fprintf ( stderr, "Could not write %d bytes of output: %s\n",
321 output->len, strerror ( errno ) );
327 int main ( int argc, char **argv ) {
328 struct input_file input;
329 struct output_file output;
330 struct zinfo_file zinfo;
334 fprintf ( stderr, "Syntax: %s file.bin file.zinfo "
335 "> file.zbin\n", argv[0] );
339 if ( read_input_file ( argv[1], &input ) < 0 )
341 if ( read_zinfo_file ( argv[2], &zinfo ) < 0 )
343 if ( alloc_output_file ( ( input.len * 4 ), &output ) < 0 )
346 for ( i = 0 ; i < zinfo.num_entries ; i++ ) {
347 if ( process_zinfo ( &input, &output,
348 &zinfo.zinfo[i] ) < 0 )
352 if ( write_output_file ( &output ) < 0 )