Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* bin2c by xerpi (c) 2013 */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- unsigned char buffer[512];
- #define BUF_SIZE 512
- #define ROW_DATA 10
- unsigned char BUFFER[BUF_SIZE];
- void strreplace(char *str, char from, char to);
- void print_usage();
- int main(int argc, char *argv[])
- {
- if(argc < 2) {
- print_usage();
- return -1;
- }
- FILE *in_fp, *out_fp;
- int in_size, steps, i, j, n_read, row_data_ctr = 0;
- if(!(in_fp = fopen(argv[1], "rb"))) {
- printf("Error opening input file.\n");
- return -1;
- }
- char *slash_pointer, *file_name, *h_name, *dot_ocurrence, *slash_ocurrence;
- int name_size;
- slash_ocurrence = strrchr(argv[1], '/');
- if(slash_ocurrence) {
- slash_pointer = slash_ocurrence + 1;
- } else {
- slash_pointer = argv[1];
- }
- dot_ocurrence = strrchr(argv[1], '.');
- if(dot_ocurrence){
- name_size = (long)dot_ocurrence - (long)slash_pointer - 1;
- } else {
- name_size = strlen(argv[1]);
- }
- file_name = (char*)malloc(name_size + 1);
- memcpy(file_name, slash_pointer, name_size + 1);
- strreplace(file_name, ' ', '_');
- h_name = (char*)malloc(name_size + 3);
- sprintf(h_name, "%s.h", file_name);
- if(!(out_fp = fopen(h_name, "wb")))
- {
- printf("Error creating output file. (not enough permissions?)\n");
- fclose(in_fp);
- free(file_name);
- return -1;
- }
- fseek(in_fp, 0x0, SEEK_END);
- in_size = ftell(in_fp);
- fseek(in_fp, 0x0, SEEK_SET);
- steps = (int)(in_size/BUF_SIZE);
- if(in_size%BUF_SIZE) steps++;
- //
- fprintf(out_fp, "int %s_size = %i;\n", file_name, in_size);
- fprintf(out_fp, "unsigned char %s_data[] =\n{\n\t", file_name);
- for(i = 1; i <= steps; i++) {
- n_read = fread(BUFFER, 1, BUF_SIZE, in_fp);
- for(j = 1; j <= n_read; j++) {
- if((i == steps) && (j == n_read)) //last byte
- fprintf(out_fp, "0x%02X\n};", BUFFER[j]);
- else
- fprintf(out_fp, "0x%02X, ", BUFFER[j]);
- if(++row_data_ctr % ROW_DATA == 0) {
- fprintf(out_fp, "\n\t");
- }
- }
- }
- fflush(out_fp);
- fclose(out_fp);
- fclose(in_fp);
- free(file_name);
- return 0;
- }
- void print_usage()
- {
- printf("\nUsage: bin2c [file]\n");
- }
- void strreplace(char *str, char from, char to)
- {
- char *_str = str;
- while(*_str++) {
- if(*_str == from) *_str = to;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement