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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 hdr_len;
  16. size_t max_len;
  17. };
  18. struct zinfo_common {
  19. char type[4];
  20. char pad[12];
  21. };
  22. struct zinfo_copy {
  23. char type[4];
  24. uint32_t offset;
  25. uint32_t len;
  26. uint32_t align;
  27. };
  28. struct zinfo_pack {
  29. char type[4];
  30. uint32_t offset;
  31. uint32_t len;
  32. uint32_t align;
  33. };
  34. struct zinfo_payload {
  35. char type[4];
  36. uint32_t pad1;
  37. uint32_t pad2;
  38. uint32_t align;
  39. };
  40. struct zinfo_add {
  41. char type[4];
  42. uint32_t offset;
  43. uint32_t divisor;
  44. uint32_t pad;
  45. };
  46. union zinfo_record {
  47. struct zinfo_common common;
  48. struct zinfo_copy copy;
  49. struct zinfo_pack pack;
  50. struct zinfo_payload payload;
  51. struct zinfo_add add;
  52. };
  53. struct zinfo_file {
  54. union zinfo_record *zinfo;
  55. unsigned int num_entries;
  56. };
  57. static unsigned long align ( unsigned long value, unsigned long align ) {
  58. return ( ( value + align - 1 ) & ~( align - 1 ) );
  59. }
  60. static int read_file ( const char *filename, void **buf, size_t *len ) {
  61. FILE *file;
  62. struct stat stat;
  63. file = fopen ( filename, "r" );
  64. if ( ! file ) {
  65. fprintf ( stderr, "Could not open %s: %s\n", filename,
  66. strerror ( errno ) );
  67. goto err;
  68. }
  69. if ( fstat ( fileno ( file ), &stat ) < 0 ) {
  70. fprintf ( stderr, "Could not stat %s: %s\n", filename,
  71. strerror ( errno ) );
  72. goto err;
  73. }
  74. *len = stat.st_size;
  75. *buf = malloc ( *len );
  76. if ( ! *buf ) {
  77. fprintf ( stderr, "Could not malloc() %zd bytes for %s: %s\n",
  78. *len, filename, strerror ( errno ) );
  79. goto err;
  80. }
  81. if ( fread ( *buf, 1, *len, file ) != *len ) {
  82. fprintf ( stderr, "Could not read %zd bytes from %s: %s\n",
  83. *len, filename, strerror ( errno ) );
  84. goto err;
  85. }
  86. fclose ( file );
  87. return 0;
  88. err:
  89. if ( file )
  90. fclose ( file );
  91. return -1;
  92. }
  93. static int read_input_file ( const char *filename,
  94. struct input_file *input ) {
  95. return read_file ( filename, &input->buf, &input->len );
  96. }
  97. static int read_zinfo_file ( const char *filename,
  98. struct zinfo_file *zinfo ) {
  99. void *buf;
  100. size_t len;
  101. if ( read_file ( filename, &buf, &len ) < 0 )
  102. return -1;
  103. if ( ( len % sizeof ( *(zinfo->zinfo) ) ) != 0 ) {
  104. fprintf ( stderr, ".zinfo file %s has invalid length %zd\n",
  105. filename, len );
  106. return -1;
  107. }
  108. zinfo->zinfo = buf;
  109. zinfo->num_entries = ( len / sizeof ( *(zinfo->zinfo) ) );
  110. return 0;
  111. }
  112. static int alloc_output_file ( size_t max_len, struct output_file *output ) {
  113. output->len = 0;
  114. output->max_len = ( max_len );
  115. output->buf = malloc ( max_len );
  116. if ( ! output->buf ) {
  117. fprintf ( stderr, "Could not allocate %zd bytes for output\n",
  118. max_len );
  119. return -1;
  120. }
  121. memset ( output->buf, 0xff, sizeof ( output->buf ) );
  122. return 0;
  123. }
  124. static int process_zinfo_copy ( struct input_file *input,
  125. struct output_file *output,
  126. union zinfo_record *zinfo ) {
  127. struct zinfo_copy *copy = &zinfo->copy;
  128. size_t offset = copy->offset;
  129. size_t len = copy->len;
  130. if ( ( offset + len ) > input->len ) {
  131. fprintf ( stderr, "Input buffer overrun on copy\n" );
  132. return -1;
  133. }
  134. output->len = align ( output->len, copy->align );
  135. if ( ( output->len + len ) > output->max_len ) {
  136. fprintf ( stderr, "Output buffer overrun on copy\n" );
  137. return -1;
  138. }
  139. if ( DEBUG ) {
  140. fprintf ( stderr, "COPY [%#zx,%#zx) to [%#zx,%#zx)\n",
  141. offset, ( offset + len ), output->len,
  142. ( output->len + len ) );
  143. }
  144. memcpy ( ( output->buf + output->len ),
  145. ( input->buf + offset ), len );
  146. output->len += len;
  147. return 0;
  148. }
  149. static int process_zinfo_pack ( struct input_file *input,
  150. struct output_file *output,
  151. union zinfo_record *zinfo ) {
  152. struct zinfo_pack *pack = &zinfo->pack;
  153. size_t offset = pack->offset;
  154. size_t len = pack->len;
  155. unsigned long packed_len;
  156. if ( ( offset + len ) > input->len ) {
  157. fprintf ( stderr, "Input buffer overrun on pack\n" );
  158. return -1;
  159. }
  160. output->len = align ( output->len, pack->align );
  161. if ( output->len > output->max_len ) {
  162. fprintf ( stderr, "Output buffer overrun on pack\n" );
  163. return -1;
  164. }
  165. if ( ucl_nrv2b_99_compress ( ( input->buf + offset ), len,
  166. ( output->buf + output->len ),
  167. &packed_len, 0 ) != UCL_E_OK ) {
  168. fprintf ( stderr, "Compression failure\n" );
  169. return -1;
  170. }
  171. if ( DEBUG ) {
  172. fprintf ( stderr, "PACK [%#zx,%#zx) to [%#zx,%#zx)\n",
  173. offset, ( offset + len ), output->len,
  174. ( size_t )( output->len + packed_len ) );
  175. }
  176. output->len += packed_len;
  177. if ( output->len > output->max_len ) {
  178. fprintf ( stderr, "Output buffer overrun on pack\n" );
  179. return -1;
  180. }
  181. return 0;
  182. }
  183. static int process_zinfo_payl ( struct input_file *input
  184. __attribute__ (( unused )),
  185. struct output_file *output,
  186. union zinfo_record *zinfo ) {
  187. struct zinfo_payload *payload = &zinfo->payload;
  188. output->len = align ( output->len, payload->align );
  189. output->hdr_len = output->len;
  190. if ( DEBUG ) {
  191. fprintf ( stderr, "PAYL at %#zx\n", output->hdr_len );
  192. }
  193. return 0;
  194. }
  195. static int process_zinfo_add ( struct input_file *input
  196. __attribute__ (( unused )),
  197. struct output_file *output,
  198. size_t len,
  199. struct zinfo_add *add,
  200. size_t datasize ) {
  201. size_t offset = add->offset;
  202. void *target;
  203. signed long addend;
  204. unsigned long size;
  205. signed long val;
  206. unsigned long mask;
  207. if ( ( offset + datasize ) > output->len ) {
  208. fprintf ( stderr, "Add at %#zx outside output buffer\n",
  209. offset );
  210. return -1;
  211. }
  212. target = ( output->buf + offset );
  213. size = ( align ( len, add->divisor ) / add->divisor );
  214. switch ( datasize ) {
  215. case 1:
  216. addend = *( ( int8_t * ) target );
  217. break;
  218. case 2:
  219. addend = *( ( int16_t * ) target );
  220. break;
  221. case 4:
  222. addend = *( ( int32_t * ) target );
  223. break;
  224. default:
  225. fprintf ( stderr, "Unsupported add datasize %zd\n",
  226. datasize );
  227. return -1;
  228. }
  229. val = size + addend;
  230. /* The result of 1UL << ( 8 * sizeof(unsigned long) ) is undefined */
  231. mask = ( ( datasize < sizeof ( mask ) ) ?
  232. ( ( 1UL << ( 8 * datasize ) ) - 1 ) : ~0UL );
  233. if ( val < 0 ) {
  234. fprintf ( stderr, "Add %s%#x+%#lx at %#zx %sflows field\n",
  235. ( ( addend < 0 ) ? "-" : "" ), abs ( addend ), size,
  236. offset, ( ( addend < 0 ) ? "under" : "over" ) );
  237. return -1;
  238. }
  239. if ( val & ~mask ) {
  240. fprintf ( stderr, "Add %s%#x+%#lx at %#zx overflows %zd-byte "
  241. "field (%d bytes too big)\n",
  242. ( ( addend < 0 ) ? "-" : "" ), abs ( addend ), size,
  243. offset, datasize,
  244. ( int )( ( val - mask - 1 ) * add->divisor ) );
  245. return -1;
  246. }
  247. switch ( datasize ) {
  248. case 1:
  249. *( ( uint8_t * ) target ) = val;
  250. break;
  251. case 2:
  252. *( ( uint16_t * ) target ) = val;
  253. break;
  254. case 4:
  255. *( ( uint32_t * ) target ) = val;
  256. break;
  257. }
  258. if ( DEBUG ) {
  259. fprintf ( stderr, "ADDx [%#zx,%#zx) (%s%#x+(%#zx/%#x)) = "
  260. "%#lx\n", offset, ( offset + datasize ),
  261. ( ( addend < 0 ) ? "-" : "" ), abs ( addend ),
  262. len, add->divisor, val );
  263. }
  264. return 0;
  265. }
  266. static int process_zinfo_addb ( struct input_file *input,
  267. struct output_file *output,
  268. union zinfo_record *zinfo ) {
  269. return process_zinfo_add ( input, output, output->len,
  270. &zinfo->add, 1 );
  271. }
  272. static int process_zinfo_addw ( struct input_file *input,
  273. struct output_file *output,
  274. union zinfo_record *zinfo ) {
  275. return process_zinfo_add ( input, output, output->len,
  276. &zinfo->add, 2 );
  277. }
  278. static int process_zinfo_addl ( struct input_file *input,
  279. struct output_file *output,
  280. union zinfo_record *zinfo ) {
  281. return process_zinfo_add ( input, output, output->len,
  282. &zinfo->add, 4 );
  283. }
  284. static int process_zinfo_adhb ( struct input_file *input,
  285. struct output_file *output,
  286. union zinfo_record *zinfo ) {
  287. return process_zinfo_add ( input, output, output->hdr_len,
  288. &zinfo->add, 1 );
  289. }
  290. static int process_zinfo_adhw ( struct input_file *input,
  291. struct output_file *output,
  292. union zinfo_record *zinfo ) {
  293. return process_zinfo_add ( input, output, output->hdr_len,
  294. &zinfo->add, 2 );
  295. }
  296. static int process_zinfo_adhl ( struct input_file *input,
  297. struct output_file *output,
  298. union zinfo_record *zinfo ) {
  299. return process_zinfo_add ( input, output, output->hdr_len,
  300. &zinfo->add, 4 );
  301. }
  302. struct zinfo_processor {
  303. char *type;
  304. int ( * process ) ( struct input_file *input,
  305. struct output_file *output,
  306. union zinfo_record *zinfo );
  307. };
  308. static struct zinfo_processor zinfo_processors[] = {
  309. { "COPY", process_zinfo_copy },
  310. { "PACK", process_zinfo_pack },
  311. { "PAYL", process_zinfo_payl },
  312. { "ADDB", process_zinfo_addb },
  313. { "ADDW", process_zinfo_addw },
  314. { "ADDL", process_zinfo_addl },
  315. { "ADHB", process_zinfo_adhb },
  316. { "ADHW", process_zinfo_adhw },
  317. { "ADHL", process_zinfo_adhl },
  318. };
  319. static int process_zinfo ( struct input_file *input,
  320. struct output_file *output,
  321. union zinfo_record *zinfo ) {
  322. struct zinfo_common *common = &zinfo->common;
  323. struct zinfo_processor *processor;
  324. char type[ sizeof ( common->type ) + 1 ] = "";
  325. unsigned int i;
  326. strncat ( type, common->type, sizeof ( type ) - 1 );
  327. for ( i = 0 ; i < ( sizeof ( zinfo_processors ) /
  328. sizeof ( zinfo_processors[0] ) ) ; i++ ) {
  329. processor = &zinfo_processors[i];
  330. if ( strcmp ( processor->type, type ) == 0 )
  331. return processor->process ( input, output, zinfo );
  332. }
  333. fprintf ( stderr, "Unknown zinfo record type \"%s\"\n", &type[0] );
  334. return -1;
  335. }
  336. static int write_output_file ( struct output_file *output ) {
  337. if ( fwrite ( output->buf, 1, output->len, stdout ) != output->len ) {
  338. fprintf ( stderr, "Could not write %zd bytes of output: %s\n",
  339. output->len, strerror ( errno ) );
  340. return -1;
  341. }
  342. return 0;
  343. }
  344. int main ( int argc, char **argv ) {
  345. struct input_file input;
  346. struct output_file output;
  347. struct zinfo_file zinfo;
  348. unsigned int i;
  349. if ( argc != 3 ) {
  350. fprintf ( stderr, "Syntax: %s file.bin file.zinfo "
  351. "> file.zbin\n", argv[0] );
  352. exit ( 1 );
  353. }
  354. if ( read_input_file ( argv[1], &input ) < 0 )
  355. exit ( 1 );
  356. if ( read_zinfo_file ( argv[2], &zinfo ) < 0 )
  357. exit ( 1 );
  358. if ( alloc_output_file ( ( input.len * 4 ), &output ) < 0 )
  359. exit ( 1 );
  360. for ( i = 0 ; i < zinfo.num_entries ; i++ ) {
  361. if ( process_zinfo ( &input, &output,
  362. &zinfo.zinfo[i] ) < 0 )
  363. exit ( 1 );
  364. }
  365. if ( write_output_file ( &output ) < 0 )
  366. exit ( 1 );
  367. return 0;
  368. }