Advertisement
xerpi

C bin2c v2 by xerpi

Jun 3rd, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. /* bin2c by xerpi (c) 2013 */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. unsigned char buffer[512];
  8.  
  9. #define BUF_SIZE 512
  10. #define ROW_DATA 10
  11. unsigned char BUFFER[BUF_SIZE];
  12.  
  13. void strreplace(char *str, char from, char to);
  14. void print_usage();
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.     if(argc < 2) {
  19.         print_usage();
  20.     return -1;
  21.     }
  22.  
  23.     FILE *in_fp, *out_fp;
  24.     int in_size, steps, i, j, n_read, row_data_ctr = 0;
  25.  
  26.  
  27.     if(!(in_fp = fopen(argv[1], "rb"))) {
  28.         printf("Error opening input file.\n");
  29.         return -1;
  30.     }
  31.  
  32.     char *slash_pointer, *file_name, *h_name, *dot_ocurrence, *slash_ocurrence;
  33.     int name_size;
  34.     slash_ocurrence = strrchr(argv[1], '/');
  35.     if(slash_ocurrence) {
  36.         slash_pointer = slash_ocurrence + 1;
  37.     } else {
  38.     slash_pointer = argv[1];
  39.     }      
  40.  
  41.     dot_ocurrence = strrchr(argv[1], '.');
  42.  
  43.     if(dot_ocurrence){
  44.         name_size = (long)dot_ocurrence - (long)slash_pointer - 1;    
  45.     } else {
  46.         name_size = strlen(argv[1]);
  47.     }
  48.  
  49.     file_name = (char*)malloc(name_size + 1);
  50.     memcpy(file_name, slash_pointer, name_size + 1);
  51.  
  52.     strreplace(file_name, ' ', '_');
  53.  
  54.     h_name = (char*)malloc(name_size + 3);
  55.     sprintf(h_name, "%s.h", file_name);
  56.  
  57.     if(!(out_fp = fopen(h_name, "wb")))
  58.     {
  59.         printf("Error creating output file. (not enough permissions?)\n");
  60.         fclose(in_fp);
  61.         free(file_name);
  62.         return -1;
  63.     }
  64.  
  65.  
  66.     fseek(in_fp, 0x0, SEEK_END);
  67.     in_size = ftell(in_fp);
  68.     fseek(in_fp, 0x0, SEEK_SET);
  69.  
  70.     steps = (int)(in_size/BUF_SIZE);
  71.     if(in_size%BUF_SIZE) steps++;
  72.  
  73.     //
  74.     fprintf(out_fp, "int %s_size = %i;\n", file_name, in_size);
  75.     fprintf(out_fp, "unsigned char %s_data[] =\n{\n\t", file_name);
  76.  
  77.     for(i = 1; i <= steps; i++) {
  78.         n_read = fread(BUFFER, 1, BUF_SIZE, in_fp);
  79.         for(j = 1; j <= n_read; j++) {
  80.  
  81.             if((i == steps) && (j == n_read)) //last byte
  82.                 fprintf(out_fp, "0x%02X\n};", BUFFER[j]);
  83.             else
  84.                 fprintf(out_fp, "0x%02X, ", BUFFER[j]);
  85.  
  86.             if(++row_data_ctr % ROW_DATA == 0) {
  87.                 fprintf(out_fp, "\n\t");
  88.             }
  89.         }
  90.     }
  91.  
  92.     fflush(out_fp);
  93.     fclose(out_fp);
  94.     fclose(in_fp);
  95.     free(file_name);
  96.     return 0;
  97. }
  98.  
  99. void print_usage()
  100. {
  101.     printf("\nUsage: bin2c [file]\n");
  102. }
  103.  
  104. void strreplace(char *str, char from, char to)
  105. {
  106.     char *_str = str;
  107.     while(*_str++) {
  108.         if(*_str == from) *_str = to;
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement