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

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