New committer!
[kugel-rb.git] / rbutil / tools / bin2c.c
blob75b44d5df7f7db9ff936ab46246b71ba1130f00e
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Dave Chapman
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #if !defined(_MSC_VER)
29 #include <unistd.h>
30 #else
31 #include <io.h>
32 #define snprintf _snprintf
33 #define open _open
34 #define close _close
35 #define read _read
36 #endif
38 #ifndef O_BINARY
39 #define O_BINARY 0
40 #endif
42 static off_t filesize(int fd)
44 struct stat buf;
46 fstat(fd,&buf);
47 return buf.st_size;
50 static int write_cfile(const unsigned char* buf, off_t len, const char* cname)
52 char filename[256];
53 FILE* fp;
54 int i;
56 snprintf(filename,256,"%s.c",cname);
58 fp = fopen(filename,"w+");
59 if (fp == NULL) {
60 fprintf(stderr,"Couldn't open %s\n",filename);
61 return -1;
64 fprintf(fp,"/* Generated by bin2c */\n\n");
65 fprintf(fp,"unsigned char %s[] = {",cname);
67 for (i=0;i<len;i++) {
68 if ((i % 16) == 0) {
69 fprintf(fp,"\n ");
71 if (i == (len-1)) {
72 fprintf(fp,"0x%02x",buf[i]);
73 } else {
74 fprintf(fp,"0x%02x, ",buf[i]);
77 fprintf(fp,"\n};\n");
79 fclose(fp);
80 return 0;
83 static int write_hfile(off_t len, const char* cname)
85 char filename[256];
86 FILE* fp;
88 snprintf(filename,256,"%s.h",cname);
89 fp = fopen(filename,"w+");
90 if (fp == NULL) {
91 fprintf(stderr,"Couldn't open %s\n",filename);
92 return -1;
95 fprintf(fp,"/* Generated by bin2c */\n\n");
96 fprintf(fp,"#define LEN_%s %d\n",cname,(int)len);
97 fprintf(fp,"extern unsigned char %s[];\n",cname);
98 fclose(fp);
99 return 0;
102 int main (int argc, char* argv[])
104 char* infile;
105 char* cname;
106 int fd;
107 unsigned char* buf;
108 int len;
109 int n;
111 if (argc != 3) {
112 fprintf(stderr,"Usage: bin2c file cname\n");
113 return 0;
116 infile=argv[1];
117 cname=argv[2];
119 fd = open(infile,O_RDONLY|O_BINARY);
120 if (fd < 0) {
121 fprintf(stderr,"Can not open %s\n",infile);
122 return 0;
125 len = filesize(fd);
127 buf = malloc(len);
128 n = read(fd,buf,len);
129 if (n < len) {
130 fprintf(stderr,"Short read, aborting\n");
131 return 0;
133 close(fd);
135 if (write_cfile(buf,len,cname) < 0) {
136 return -1;
138 if (write_hfile(len,cname) < 0) {
139 return -1;
142 return 0;