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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. struct output_file *output,
  185. union zinfo_record *zinfo ) {
  186. struct zinfo_payload *payload = &zinfo->payload;
  187. output->len = align ( output->len, payload->align );
  188. output->hdr_len = output->len;
  189. if ( DEBUG ) {
  190. fprintf ( stderr, "PAYL at %#zx\n", output->hdr_len );
  191. }
  192. }
  193. static int process_zinfo_add ( struct input_file *input,
  194. struct output_file *output,
  195. size_t len,
  196. struct zinfo_add *add,
  197. size_t datasize ) {
  198. size_t offset = add->offset;
  199. void *target;
  200. signed long addend;
  201. unsigned long size;
  202. signed long val;
  203. unsigned long mask;
  204. if ( ( offset + datasize ) > output->len ) {
  205. fprintf ( stderr, "Add at %#zx outside output buffer\n",
  206. offset );
  207. return -1;
  208. }
  209. target = ( output->buf + offset );
  210. size = ( align ( len, add->divisor ) / add->divisor );
  211. switch ( datasize ) {
  212. case 1:
  213. addend = *( ( int8_t * ) target );
  214. break;
  215. case 2:
  216. addend = *( ( int16_t * ) target );
  217. break;
  218. case 4:
  219. addend = *( ( int32_t * ) target );
  220. break;
  221. default:
  222. fprintf ( stderr, "Unsupported add datasize %zd\n",
  223. datasize );
  224. return -1;
  225. }
  226. val = size + addend;
  227. /* The result of 1UL << ( 8 * sizeof(unsigned long) ) is undefined */
  228. mask = ( ( datasize < sizeof ( mask ) ) ?
  229. ( ( 1UL << ( 8 * datasize ) ) - 1 ) : ~0UL );
  230. if ( val < 0 ) {
  231. fprintf ( stderr, "Add %s%#x+%#lx at %#zx %sflows field\n",
  232. ( ( addend < 0 ) ? "-" : "" ), abs ( addend ), size,
  233. offset, ( ( addend < 0 ) ? "under" : "over" ) );
  234. return -1;
  235. }
  236. if ( val & ~mask ) {
  237. fprintf ( stderr, "Add %s%#x+%#lx at %#zx overflows %zd-byte "
  238. "field (%d bytes too big)\n",
  239. ( ( addend < 0 ) ? "-" : "" ), abs ( addend ), size,
  240. offset, datasize,
  241. ( int )( ( val - mask - 1 ) * add->divisor ) );
  242. return -1;
  243. }
  244. switch ( datasize ) {
  245. case 1:
  246. *( ( uint8_t * ) target ) = val;
  247. break;
  248. case 2:
  249. *( ( uint16_t * ) target ) = val;
  250. break;
  251. case 4:
  252. *( ( uint32_t * ) target ) = val;
  253. break;
  254. }
  255. if ( DEBUG ) {
  256. fprintf ( stderr, "ADDx [%#zx,%#zx) (%s%#x+(%#zx/%#x)) = "
  257. "%#lx\n", offset, ( offset + datasize ),
  258. ( ( addend < 0 ) ? "-" : "" ), abs ( addend ),
  259. len, add->divisor, val );
  260. }
  261. return 0;
  262. }
  263. static int process_zinfo_addb ( struct input_file *input,
  264. struct output_file *output,
  265. union zinfo_record *zinfo ) {
  266. return process_zinfo_add ( input, output, output->len,
  267. &zinfo->add, 1 );
  268. }
  269. static int process_zinfo_addw ( struct input_file *input,
  270. struct output_file *output,
  271. union zinfo_record *zinfo ) {
  272. return process_zinfo_add ( input, output, output->len,
  273. &zinfo->add, 2 );
  274. }
  275. static int process_zinfo_addl ( struct input_file *input,
  276. struct output_file *output,
  277. union zinfo_record *zinfo ) {
  278. return process_zinfo_add ( input, output, output->len,
  279. &zinfo->add, 4 );
  280. }
  281. static int process_zinfo_adhb ( struct input_file *input,
  282. struct output_file *output,
  283. union zinfo_record *zinfo ) {
  284. return process_zinfo_add ( input, output, output->hdr_len,
  285. &zinfo->add, 1 );
  286. }
  287. static int process_zinfo_adhw ( struct input_file *input,
  288. struct output_file *output,
  289. union zinfo_record *zinfo ) {
  290. return process_zinfo_add ( input, output, output->hdr_len,
  291. &zinfo->add, 2 );
  292. }
  293. static int process_zinfo_adhl ( struct input_file *input,
  294. struct output_file *output,
  295. union zinfo_record *zinfo ) {
  296. return process_zinfo_add ( input, output, output->hdr_len,
  297. &zinfo->add, 4 );
  298. }
  299. struct zinfo_processor {
  300. char *type;
  301. int ( * process ) ( struct input_file *input,
  302. struct output_file *output,
  303. union zinfo_record *zinfo );
  304. };
  305. static struct zinfo_processor zinfo_processors[] = {
  306. { "COPY", process_zinfo_copy },
  307. { "PACK", process_zinfo_pack },
  308. { "PAYL", process_zinfo_payl },
  309. { "ADDB", process_zinfo_addb },
  310. { "ADDW", process_zinfo_addw },
  311. { "ADDL", process_zinfo_addl },
  312. { "ADHB", process_zinfo_adhb },
  313. { "ADHW", process_zinfo_adhw },
  314. { "ADHL", process_zinfo_adhl },
  315. };
  316. static int process_zinfo ( struct input_file *input,
  317. struct output_file *output,
  318. union zinfo_record *zinfo ) {
  319. struct zinfo_common *common = &zinfo->common;
  320. struct zinfo_processor *processor;
  321. char type[ sizeof ( common->type ) + 1 ] = "";
  322. unsigned int i;
  323. strncat ( type, common->type, sizeof ( type ) - 1 );
  324. for ( i = 0 ; i < ( sizeof ( zinfo_processors ) /
  325. sizeof ( zinfo_processors[0] ) ) ; i++ ) {
  326. processor = &zinfo_processors[i];
  327. if ( strcmp ( processor->type, type ) == 0 )
  328. return processor->process ( input, output, zinfo );
  329. }
  330. fprintf ( stderr, "Unknown zinfo record type \"%s\"\n", &type[0] );
  331. return -1;
  332. }
  333. static int write_output_file ( struct output_file *output ) {
  334. if ( fwrite ( output->buf, 1, output->len, stdout ) != output->len ) {
  335. fprintf ( stderr, "Could not write %zd bytes of output: %s\n",
  336. output->len, strerror ( errno ) );
  337. return -1;
  338. }
  339. return 0;
  340. }
  341. int main ( int argc, char **argv ) {
  342. struct input_file input;
  343. struct output_file output;
  344. struct zinfo_file zinfo;
  345. unsigned int i;
  346. if ( argc != 3 ) {
  347. fprintf ( stderr, "Syntax: %s file.bin file.zinfo "
  348. "> file.zbin\n", argv[0] );
  349. exit ( 1 );
  350. }
  351. if ( read_input_file ( argv[1], &input ) < 0 )
  352. exit ( 1 );
  353. if ( read_zinfo_file ( argv[2], &zinfo ) < 0 )
  354. exit ( 1 );
  355. if ( alloc_output_file ( ( input.len * 4 ), &output ) < 0 )
  356. exit ( 1 );
  357. for ( i = 0 ; i < zinfo.num_entries ; i++ ) {
  358. if ( process_zinfo ( &input, &output,
  359. &zinfo.zinfo[i] ) < 0 )
  360. exit ( 1 );
  361. }
  362. if ( write_output_file ( &output ) < 0 )
  363. exit ( 1 );
  364. return 0;
  365. }