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 int read_file ( const char *filename, void **buf, size_t *len ) {
64 file = fopen ( filename, "r" );
66 fprintf ( stderr, "Could not open %s: %s\n", filename,
71 if ( fstat ( fileno ( file ), &stat ) < 0 ) {
72 fprintf ( stderr, "Could not stat %s: %s\n", filename,
78 *buf = malloc ( *len );
80 fprintf ( stderr, "Could not malloc() %d bytes for %s: %s\n",
81 *len, filename, strerror ( errno ) );
85 if ( fread ( *buf, 1, *len, file ) != *len ) {
86 fprintf ( stderr, "Could not read %d bytes from %s: %s\n",
87 *len, filename, strerror ( errno ) );
100 static int read_input_file ( const char *filename,
101 struct input_file *input ) {
102 return read_file ( filename, &input->buf, &input->len );
105 static int read_zinfo_file ( const char *filename,
106 struct zinfo_file *zinfo ) {
110 if ( read_file ( filename, &buf, &len ) < 0 )
113 if ( ( len % sizeof ( *(zinfo->zinfo) ) ) != 0 ) {
114 fprintf ( stderr, ".zinfo file %s has invalid length %d\n",
120 zinfo->num_entries = ( len / sizeof ( *(zinfo->zinfo) ) );
124 static int alloc_output_file ( size_t max_len, struct output_file *output ) {
126 output->max_len = ( max_len );
127 output->buf = malloc ( max_len );
128 if ( ! output->buf ) {
129 fprintf ( stderr, "Could not allocate %d bytes for output\n",
133 memset ( output->buf, 0xff, sizeof ( output->buf ) );
137 static int process_zinfo_copy ( struct input_file *input,
138 struct output_file *output,
139 union zinfo_record *zinfo ) {
140 struct zinfo_copy *copy = &zinfo->copy;
141 size_t offset = copy->offset;
142 size_t len = copy->len;
143 unsigned int align = copy->align;
145 if ( ( offset + len ) > input->len ) {
146 fprintf ( stderr, "Input buffer overrun on copy\n" );
150 output->len = ( ( output->len + align - 1 ) & ~( align - 1 ) );
151 if ( ( output->len + len ) > output->max_len ) {
152 fprintf ( stderr, "Output buffer overrun on copy\n" );
157 fprintf ( stderr, "COPY [%#zx,%#zx) to [%#zx,%#zx)\n", offset, ( offset + len ),
158 output->len, ( output->len + len ) );
161 memcpy ( ( output->buf + output->len ),
162 ( input->buf + offset ), len );
167 static int process_zinfo_pack ( struct input_file *input,
168 struct output_file *output,
169 union zinfo_record *zinfo ) {
170 struct zinfo_pack *pack = &zinfo->pack;
171 size_t offset = pack->offset;
172 size_t len = pack->len;
173 unsigned int align = pack->align;
174 unsigned long packed_len;
176 if ( ( offset + len ) > input->len ) {
177 fprintf ( stderr, "Input buffer overrun on pack\n" );
181 output->len = ( ( output->len + align - 1 ) & ~( align - 1 ) );
182 if ( output->len > output->max_len ) {
183 fprintf ( stderr, "Output buffer overrun on pack\n" );
187 if ( ucl_nrv2b_99_compress ( ( input->buf + offset ), len,
188 ( output->buf + output->len ),
189 &packed_len, 0 ) != UCL_E_OK ) {
190 fprintf ( stderr, "Compression failure\n" );
195 fprintf ( stderr, "PACK [%#zx,%#zx) to [%#zx,%#zx)\n", offset, ( offset + len ),
196 output->len, ( output->len + packed_len ) );
199 output->len += packed_len;
200 if ( output->len > output->max_len ) {
201 fprintf ( stderr, "Output buffer overrun on pack\n" );
208 static int process_zinfo_subtract ( struct input_file *input,
209 struct output_file *output,
210 struct zinfo_subtract *subtract,
212 size_t offset = subtract->offset;
218 if ( ( offset + datasize ) > output->len ) {
219 fprintf ( stderr, "Subtract at %#zx outside output buffer\n",
224 target = ( output->buf + offset );
225 delta = ( ( output->len / subtract->divisor ) -
226 ( input->len / subtract->divisor ) );
228 switch ( datasize ) {
230 uint8_t *byte = target;
236 uint16_t *word = target;
242 uint32_t *dword = target;
248 fprintf ( stderr, "Unsupported subtract datasize %d\n",
254 fprintf ( stderr, "SUBx [%#zx,%#zx) (%#lx+(%#lx/%#lx)-(%#lx/%#lx)) = %#lx\n",
255 offset, ( offset + datasize ), old, output->len, subtract->divisor,
256 input->len, subtract->divisor, new );
262 static int process_zinfo_subb ( struct input_file *input,
263 struct output_file *output,
264 union zinfo_record *zinfo ) {
265 return process_zinfo_subtract ( input, output, &zinfo->subtract, 1 );
268 static int process_zinfo_subw ( struct input_file *input,
269 struct output_file *output,
270 union zinfo_record *zinfo ) {
271 return process_zinfo_subtract ( input, output, &zinfo->subtract, 2 );
274 static int process_zinfo_subl ( struct input_file *input,
275 struct output_file *output,
276 union zinfo_record *zinfo ) {
277 return process_zinfo_subtract ( input, output, &zinfo->subtract, 4 );
280 struct zinfo_processor {
282 int ( * process ) ( struct input_file *input,
283 struct output_file *output,
284 union zinfo_record *zinfo );
287 static struct zinfo_processor zinfo_processors[] = {
288 { "COPY", process_zinfo_copy },
289 { "PACK", process_zinfo_pack },
290 { "SUBB", process_zinfo_subb },
291 { "SUBW", process_zinfo_subw },
292 { "SUBL", process_zinfo_subl },
295 static int process_zinfo ( struct input_file *input,
296 struct output_file *output,
297 union zinfo_record *zinfo ) {
298 struct zinfo_common *common = &zinfo->common;
299 struct zinfo_processor *processor;
300 char type[ sizeof ( common->type ) + 1 ] = "";
303 strncat ( type, common->type, sizeof ( type ) - 1 );
304 for ( i = 0 ; i < ( sizeof ( zinfo_processors ) /
305 sizeof ( zinfo_processors[0] ) ) ; i++ ) {
306 processor = &zinfo_processors[i];
307 if ( strcmp ( processor->type, type ) == 0 )
308 return processor->process ( input, output, zinfo );
311 fprintf ( stderr, "Unknown zinfo record type \"%s\"\n", &type[0] );
315 static int write_output_file ( struct output_file *output ) {
316 if ( fwrite ( output->buf, 1, output->len, stdout ) != output->len ) {
317 fprintf ( stderr, "Could not write %d bytes of output: %s\n",
318 output->len, strerror ( errno ) );
324 int main ( int argc, char **argv ) {
325 struct input_file input;
326 struct output_file output;
327 struct zinfo_file zinfo;
331 fprintf ( stderr, "Syntax: %s file.bin file.zinfo "
332 "> file.zbin\n", argv[0] );
336 if ( read_input_file ( argv[1], &input ) < 0 )
338 if ( read_zinfo_file ( argv[2], &zinfo ) < 0 )
340 if ( alloc_output_file ( ( input.len * 4 ), &output ) < 0 )
343 for ( i = 0 ; i < zinfo.num_entries ; i++ ) {
344 if ( process_zinfo ( &input, &output,
345 &zinfo.zinfo[i] ) < 0 )
349 if ( write_output_file ( &output ) < 0 )