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

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