You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zbin.c 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #define ENCODE
  4. #define VERBOSE
  5. #include "nrv2b.c"
  6. FILE *infile, *outfile;
  7. #define DEBUG 0
  8. struct input_file {
  9. void *buf;
  10. size_t len;
  11. };
  12. struct output_file {
  13. void *buf;
  14. size_t len;
  15. size_t max_len;
  16. };
  17. struct zinfo_common {
  18. char type[4];
  19. char pad[12];
  20. };
  21. struct zinfo_copy {
  22. char type[4];
  23. uint32_t offset;
  24. uint32_t len;
  25. uint32_t align;
  26. };
  27. struct zinfo_pack {
  28. char type[4];
  29. uint32_t offset;
  30. uint32_t len;
  31. uint32_t align;
  32. };
  33. struct zinfo_subtract {
  34. char type[4];
  35. uint32_t offset;
  36. uint32_t divisor;
  37. uint32_t pad;
  38. };
  39. union zinfo_record {
  40. struct zinfo_common common;
  41. struct zinfo_copy copy;
  42. struct zinfo_pack pack;
  43. struct zinfo_subtract subtract;
  44. };
  45. struct zinfo_file {
  46. union zinfo_record *zinfo;
  47. unsigned int num_entries;
  48. };
  49. static int read_file ( const char *filename, void **buf, size_t *len ) {
  50. FILE *file;
  51. struct stat stat;
  52. file = fopen ( filename, "r" );
  53. if ( ! file ) {
  54. fprintf ( stderr, "Could not open %s: %s\n", filename,
  55. strerror ( errno ) );
  56. goto err;
  57. }
  58. if ( fstat ( fileno ( file ), &stat ) < 0 ) {
  59. fprintf ( stderr, "Could not stat %s: %s\n", filename,
  60. strerror ( errno ) );
  61. goto err;
  62. }
  63. *len = stat.st_size;
  64. *buf = malloc ( *len );
  65. if ( ! *buf ) {
  66. fprintf ( stderr, "Could not malloc() %d bytes for %s: %s\n",
  67. *len, filename, strerror ( errno ) );
  68. goto err;
  69. }
  70. if ( fread ( *buf, 1, *len, file ) != *len ) {
  71. fprintf ( stderr, "Could not read %d bytes from %s: %s\n",
  72. *len, filename, strerror ( errno ) );
  73. goto err;
  74. }
  75. fclose ( file );
  76. return 0;
  77. err:
  78. if ( file )
  79. fclose ( file );
  80. return -1;
  81. }
  82. static int read_input_file ( const char *filename,
  83. struct input_file *input ) {
  84. return read_file ( filename, &input->buf, &input->len );
  85. }
  86. static int read_zinfo_file ( const char *filename,
  87. struct zinfo_file *zinfo ) {
  88. void *buf;
  89. size_t len;
  90. if ( read_file ( filename, &buf, &len ) < 0 )
  91. return -1;
  92. if ( ( len % sizeof ( *(zinfo->zinfo) ) ) != 0 ) {
  93. fprintf ( stderr, ".zinfo file %s has invalid length %d\n",
  94. filename, len );
  95. return -1;
  96. }
  97. zinfo->zinfo = buf;
  98. zinfo->num_entries = ( len / sizeof ( *(zinfo->zinfo) ) );
  99. return 0;
  100. }
  101. static int alloc_output_file ( size_t max_len, struct output_file *output ) {
  102. output->len = 0;
  103. output->max_len = ( max_len );
  104. output->buf = malloc ( max_len );
  105. if ( ! output->buf ) {
  106. fprintf ( stderr, "Could not allocate %d bytes for output\n",
  107. max_len );
  108. return -1;
  109. }
  110. memset ( output->buf, 0xff, sizeof ( output->buf ) );
  111. return 0;
  112. }
  113. static int process_zinfo_copy ( struct input_file *input,
  114. struct output_file *output,
  115. union zinfo_record *zinfo ) {
  116. struct zinfo_copy *copy = &zinfo->copy;
  117. size_t offset = copy->offset;
  118. size_t len = copy->len;
  119. unsigned int align = copy->align;
  120. if ( ( offset + len ) > input->len ) {
  121. fprintf ( stderr, "Input buffer overrun on copy\n" );
  122. return -1;
  123. }
  124. output->len = ( ( output->len + align - 1 ) & ~( align - 1 ) );
  125. if ( ( output->len + len ) > output->max_len ) {
  126. fprintf ( stderr, "Output buffer overrun on copy\n" );
  127. return -1;
  128. }
  129. if ( DEBUG ) {
  130. fprintf ( stderr, "COPY [%#zx,%#zx) to [%#zx,%#zx)\n", offset, ( offset + len ),
  131. output->len, ( output->len + len ) );
  132. }
  133. memcpy ( ( output->buf + output->len ),
  134. ( input->buf + offset ), len );
  135. output->len += len;
  136. return 0;
  137. }
  138. static int process_zinfo_pack ( struct input_file *input,
  139. struct output_file *output,
  140. union zinfo_record *zinfo ) {
  141. struct zinfo_pack *pack = &zinfo->pack;
  142. size_t offset = pack->offset;
  143. size_t len = pack->len;
  144. unsigned int align = pack->align;
  145. unsigned long packed_len;
  146. if ( ( offset + len ) > input->len ) {
  147. fprintf ( stderr, "Input buffer overrun on pack\n" );
  148. return -1;
  149. }
  150. output->len = ( ( output->len + align - 1 ) & ~( align - 1 ) );
  151. if ( output->len > output->max_len ) {
  152. fprintf ( stderr, "Output buffer overrun on pack\n" );
  153. return -1;
  154. }
  155. if ( ucl_nrv2b_99_compress ( ( input->buf + offset ), len,
  156. ( output->buf + output->len ),
  157. &packed_len, 0 ) != UCL_E_OK ) {
  158. fprintf ( stderr, "Compression failure\n" );
  159. return -1;
  160. }
  161. if ( DEBUG ) {
  162. fprintf ( stderr, "PACK [%#zx,%#zx) to [%#zx,%#zx)\n", offset, ( offset + len ),
  163. output->len, ( output->len + packed_len ) );
  164. }
  165. output->len += packed_len;
  166. if ( output->len > output->max_len ) {
  167. fprintf ( stderr, "Output buffer overrun on pack\n" );
  168. return -1;
  169. }
  170. return 0;
  171. }
  172. static int process_zinfo_subtract ( struct input_file *input,
  173. struct output_file *output,
  174. struct zinfo_subtract *subtract,
  175. size_t datasize ) {
  176. size_t offset = subtract->offset;
  177. void *target;
  178. long delta;
  179. unsigned long old;
  180. unsigned long new;
  181. if ( ( offset + datasize ) > output->len ) {
  182. fprintf ( stderr, "Subtract at %#zx outside output buffer\n",
  183. offset );
  184. return -1;
  185. }
  186. target = ( output->buf + offset );
  187. delta = ( ( output->len / subtract->divisor ) -
  188. ( input->len / subtract->divisor ) );
  189. switch ( datasize ) {
  190. case 1: {
  191. uint8_t *byte = target;
  192. old = *byte;
  193. *byte += delta;
  194. new = *byte;
  195. break; }
  196. case 2: {
  197. uint16_t *word = target;
  198. old = *word;
  199. *word += delta;
  200. new = *word;
  201. break; }
  202. case 4: {
  203. uint32_t *dword = target;
  204. old = *dword;
  205. *dword += delta;
  206. new = *dword;
  207. break; }
  208. default:
  209. fprintf ( stderr, "Unsupported subtract datasize %d\n",
  210. datasize );
  211. return -1;
  212. }
  213. if ( DEBUG ) {
  214. fprintf ( stderr, "SUBx [%#zx,%#zx) (%#lx+(%#lx/%#lx)-(%#lx/%#lx)) = %#lx\n",
  215. offset, ( offset + datasize ), old, output->len, subtract->divisor,
  216. input->len, subtract->divisor, new );
  217. }
  218. return 0;
  219. }
  220. static int process_zinfo_subb ( struct input_file *input,
  221. struct output_file *output,
  222. union zinfo_record *zinfo ) {
  223. return process_zinfo_subtract ( input, output, &zinfo->subtract, 1 );
  224. }
  225. static int process_zinfo_subw ( struct input_file *input,
  226. struct output_file *output,
  227. union zinfo_record *zinfo ) {
  228. return process_zinfo_subtract ( input, output, &zinfo->subtract, 2 );
  229. }
  230. static int process_zinfo_subl ( struct input_file *input,
  231. struct output_file *output,
  232. union zinfo_record *zinfo ) {
  233. return process_zinfo_subtract ( input, output, &zinfo->subtract, 4 );
  234. }
  235. struct zinfo_processor {
  236. char *type;
  237. int ( * process ) ( struct input_file *input,
  238. struct output_file *output,
  239. union zinfo_record *zinfo );
  240. };
  241. static struct zinfo_processor zinfo_processors[] = {
  242. { "COPY", process_zinfo_copy },
  243. { "PACK", process_zinfo_pack },
  244. { "SUBB", process_zinfo_subb },
  245. { "SUBW", process_zinfo_subw },
  246. { "SUBL", process_zinfo_subl },
  247. };
  248. static int process_zinfo ( struct input_file *input,
  249. struct output_file *output,
  250. union zinfo_record *zinfo ) {
  251. struct zinfo_common *common = &zinfo->common;
  252. struct zinfo_processor *processor;
  253. char type[ sizeof ( common->type ) + 1 ] = "";
  254. unsigned int i;
  255. strncat ( type, common->type, sizeof ( type ) - 1 );
  256. for ( i = 0 ; i < ( sizeof ( zinfo_processors ) /
  257. sizeof ( zinfo_processors[0] ) ) ; i++ ) {
  258. processor = &zinfo_processors[i];
  259. if ( strcmp ( processor->type, type ) == 0 )
  260. return processor->process ( input, output, zinfo );
  261. }
  262. fprintf ( stderr, "Unknown zinfo record type \"%s\"\n", &type[0] );
  263. return -1;
  264. }
  265. static int write_output_file ( struct output_file *output ) {
  266. if ( fwrite ( output->buf, 1, output->len, stdout ) != output->len ) {
  267. fprintf ( stderr, "Could not write %d bytes of output: %s\n",
  268. output->len, strerror ( errno ) );
  269. return -1;
  270. }
  271. return 0;
  272. }
  273. int main ( int argc, char **argv ) {
  274. struct input_file input;
  275. struct output_file output;
  276. struct zinfo_file zinfo;
  277. unsigned int i;
  278. if ( argc != 3 ) {
  279. fprintf ( stderr, "Syntax: %s file.bin file.zinfo "
  280. "> file.zbin\n", argv[0] );
  281. exit ( 1 );
  282. }
  283. if ( read_input_file ( argv[1], &input ) < 0 )
  284. exit ( 1 );
  285. if ( read_zinfo_file ( argv[2], &zinfo ) < 0 )
  286. exit ( 1 );
  287. if ( alloc_output_file ( ( input.len * 4 ), &output ) < 0 )
  288. exit ( 1 );
  289. for ( i = 0 ; i < zinfo.num_entries ; i++ ) {
  290. if ( process_zinfo ( &input, &output,
  291. &zinfo.zinfo[i] ) < 0 )
  292. exit ( 1 );
  293. }
  294. if ( write_output_file ( &output ) < 0 )
  295. exit ( 1 );
  296. return 0;
  297. }