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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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, max_len );
  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, size_t offset,
  200. size_t datasize ) {
  201. void *target;
  202. signed long addend;
  203. unsigned long size;
  204. signed long val;
  205. unsigned long mask;
  206. offset += add->offset;
  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, 0, 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, 0, 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, 0, 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, 0, 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, 0, 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, 0, 4 );
  301. }
  302. static int process_zinfo_adpb ( struct input_file *input,
  303. struct output_file *output,
  304. union zinfo_record *zinfo ) {
  305. return process_zinfo_add ( input, output,
  306. ( output->len - output->hdr_len ),
  307. &zinfo->add, 0, 1 );
  308. }
  309. static int process_zinfo_adpw ( struct input_file *input,
  310. struct output_file *output,
  311. union zinfo_record *zinfo ) {
  312. return process_zinfo_add ( input, output,
  313. ( output->len - output->hdr_len ),
  314. &zinfo->add, 0, 2 );
  315. }
  316. static int process_zinfo_adpl ( struct input_file *input,
  317. struct output_file *output,
  318. union zinfo_record *zinfo ) {
  319. return process_zinfo_add ( input, output,
  320. ( output->len - output->hdr_len ),
  321. &zinfo->add, 0, 4 );
  322. }
  323. static int process_zinfo_appb ( struct input_file *input,
  324. struct output_file *output,
  325. union zinfo_record *zinfo ) {
  326. return process_zinfo_add ( input, output,
  327. ( output->len - output->hdr_len ),
  328. &zinfo->add, output->hdr_len, 1 );
  329. }
  330. static int process_zinfo_appw ( struct input_file *input,
  331. struct output_file *output,
  332. union zinfo_record *zinfo ) {
  333. return process_zinfo_add ( input, output,
  334. ( output->len - output->hdr_len ),
  335. &zinfo->add, output->hdr_len, 2 );
  336. }
  337. static int process_zinfo_appl ( struct input_file *input,
  338. struct output_file *output,
  339. union zinfo_record *zinfo ) {
  340. return process_zinfo_add ( input, output,
  341. ( output->len - output->hdr_len ),
  342. &zinfo->add, output->hdr_len, 4 );
  343. }
  344. struct zinfo_processor {
  345. char *type;
  346. int ( * process ) ( struct input_file *input,
  347. struct output_file *output,
  348. union zinfo_record *zinfo );
  349. };
  350. static struct zinfo_processor zinfo_processors[] = {
  351. { "COPY", process_zinfo_copy },
  352. { "PACK", process_zinfo_pack },
  353. { "PAYL", process_zinfo_payl },
  354. { "ADDB", process_zinfo_addb },
  355. { "ADDW", process_zinfo_addw },
  356. { "ADDL", process_zinfo_addl },
  357. { "ADHB", process_zinfo_adhb },
  358. { "ADHW", process_zinfo_adhw },
  359. { "ADHL", process_zinfo_adhl },
  360. { "ADPB", process_zinfo_adpb },
  361. { "ADPW", process_zinfo_adpw },
  362. { "ADPL", process_zinfo_adpl },
  363. { "APPB", process_zinfo_appb },
  364. { "APPW", process_zinfo_appw },
  365. { "APPL", process_zinfo_appl },
  366. };
  367. static int process_zinfo ( struct input_file *input,
  368. struct output_file *output,
  369. union zinfo_record *zinfo ) {
  370. struct zinfo_common *common = &zinfo->common;
  371. struct zinfo_processor *processor;
  372. char type[ sizeof ( common->type ) + 1 ] = "";
  373. unsigned int i;
  374. strncat ( type, common->type, sizeof ( type ) - 1 );
  375. for ( i = 0 ; i < ( sizeof ( zinfo_processors ) /
  376. sizeof ( zinfo_processors[0] ) ) ; i++ ) {
  377. processor = &zinfo_processors[i];
  378. if ( strcmp ( processor->type, type ) == 0 )
  379. return processor->process ( input, output, zinfo );
  380. }
  381. fprintf ( stderr, "Unknown zinfo record type \"%s\"\n", &type[0] );
  382. return -1;
  383. }
  384. static int write_output_file ( struct output_file *output ) {
  385. if ( fwrite ( output->buf, 1, output->len, stdout ) != output->len ) {
  386. fprintf ( stderr, "Could not write %zd bytes of output: %s\n",
  387. output->len, strerror ( errno ) );
  388. return -1;
  389. }
  390. return 0;
  391. }
  392. int main ( int argc, char **argv ) {
  393. struct input_file input;
  394. struct output_file output;
  395. struct zinfo_file zinfo;
  396. unsigned int i;
  397. if ( argc != 3 ) {
  398. fprintf ( stderr, "Syntax: %s file.bin file.zinfo "
  399. "> file.zbin\n", argv[0] );
  400. exit ( 1 );
  401. }
  402. if ( read_input_file ( argv[1], &input ) < 0 )
  403. exit ( 1 );
  404. if ( read_zinfo_file ( argv[2], &zinfo ) < 0 )
  405. exit ( 1 );
  406. if ( alloc_output_file ( ( input.len * 4 ), &output ) < 0 )
  407. exit ( 1 );
  408. for ( i = 0 ; i < zinfo.num_entries ; i++ ) {
  409. if ( process_zinfo ( &input, &output,
  410. &zinfo.zinfo[i] ) < 0 )
  411. exit ( 1 );
  412. }
  413. if ( write_output_file ( &output ) < 0 )
  414. exit ( 1 );
  415. return 0;
  416. }