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.1KB

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