ports

morpheus ports
git clone git://git.2f30.org/ports
Log | Files | Refs | LICENSE

bin2c.c (2631B)


      1 /*
      2  * This is bin2c program, which allows you to convert binary file to
      3  * C language array, for use as embedded resource, for instance you can
      4  * embed graphics or audio file directly into your program.
      5  * This is public domain software, use it on your own risk.
      6  * Contact Serge Fukanchik at fuxx@mail.ru  if you have any questions.
      7  *
      8  * Some modifications were made by Gwilym Kuiper (kuiper.gwilym@gmail.com)
      9  * He decided not to change the licence.
     10  *
     11  * Some modifications were made by Daniel Bainton (dpb@driftaway.org)
     12  * He decided not to change the licence.
     13  */
     14 
     15 #include <stdio.h>
     16 #include <string.h>
     17 #include <stdlib.h>
     18 #include <assert.h>
     19 #include <sys/stat.h>
     20 
     21 int
     22 main(int argc, char* argv[])
     23 {
     24     char *buf;
     25     char *ident;
     26     unsigned int i, file_size, need_comma, n, pad;
     27     struct stat st;
     28 
     29     FILE *f_input, *f_output;
     30 
     31     if (argc < 4) {
     32         fprintf(stderr, "Usage: %s binary_file output_file array_name [pad]\n", argv[0]);
     33         return -1;
     34     }
     35 
     36     f_input = fopen(argv[1], "rb");
     37     if (f_input == NULL) {
     38         fprintf(stderr, "%s: can't open %s for reading\n", argv[0], argv[1]);
     39         return -1;
     40     }
     41 
     42     // Get the file length
     43     fseek(f_input, 0, SEEK_END);
     44     file_size = ftell(f_input);
     45     fseek(f_input, 0, SEEK_SET);
     46 
     47     buf = (char *)malloc(file_size);
     48     assert(buf);   
     49 
     50     fread(buf, file_size, 1, f_input);
     51     fclose(f_input);
     52 
     53     f_output = fopen(argv[2], "w");
     54     if (f_output == NULL)
     55     {
     56         fprintf(stderr, "%s: can't open %s for writing\n", argv[0], argv[1]);
     57         return -1;
     58     }
     59 
     60     ident = argv[3];
     61     
     62     need_comma = 0;
     63 
     64     fprintf (f_output, "unsigned char %s[] = {", ident);
     65     for (i = 0; i < file_size; ++i)
     66     {
     67         if (need_comma) fprintf(f_output, ",");
     68         else need_comma = 1;
     69         if (( i % 8 ) == 0) fprintf(f_output, "\n\t");
     70         else fprintf(f_output, " ");
     71         fprintf(f_output, "0x%.2x", buf[i] & 0xff);
     72     }
     73 
     74     if (argc > 4)
     75         pad = atoi(argv[4]);
     76     else
     77         pad = 1;
     78     if (pad > 1) {
     79         n = file_size % pad;
     80         file_size += pad - n;
     81 
     82         for (; i < file_size; ++i)
     83         {
     84             if (need_comma) fprintf(f_output, ",");
     85             else need_comma = 1;
     86             if (( i % 8 ) == 0) fprintf(f_output, "\n\t");
     87             else fprintf(f_output, " ");
     88             fprintf(f_output, "0x00");
     89         }
     90     }
     91 
     92     fprintf(f_output, "\n};\n\n");
     93 
     94     fprintf(f_output, "const unsigned int %s_len = %i;\n\n", ident, file_size);
     95 
     96     stat(argv[1], &st);
     97 
     98     fprintf(f_output, "const int %s_mtime = %i;\n", ident, st.st_mtim);
     99 
    100     fclose(f_output);
    101 
    102     return 0;
    103 }