blob: 75b44d5df7f7db9ff936ab46246b71ba1130f00e [file] [log] [blame]
Dave Chapmane8156012007-03-15 23:26:47 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2007 Dave Chapman
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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.
Dave Chapmane8156012007-03-15 23:26:47 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include <stdio.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
Dave Chapmane8156012007-03-15 23:26:47 +000026#include <fcntl.h>
27#include <stdlib.h>
Dominik Riebeling73f9bde2009-06-13 14:15:50 +000028#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
Dave Chapmane8156012007-03-15 23:26:47 +000037
38#ifndef O_BINARY
39#define O_BINARY 0
40#endif
41
42static off_t filesize(int fd)
43{
44 struct stat buf;
45
46 fstat(fd,&buf);
47 return buf.st_size;
48}
49
Bertrik Sikkend4e38392008-07-14 16:43:47 +000050static int write_cfile(const unsigned char* buf, off_t len, const char* cname)
Dave Chapmane8156012007-03-15 23:26:47 +000051{
52 char filename[256];
53 FILE* fp;
54 int i;
55
56 snprintf(filename,256,"%s.c",cname);
57
58 fp = fopen(filename,"w+");
59 if (fp == NULL) {
60 fprintf(stderr,"Couldn't open %s\n",filename);
61 return -1;
62 }
63
Dave Chapman2f1a2622007-06-12 14:13:57 +000064 fprintf(fp,"/* Generated by bin2c */\n\n");
Dave Chapmane8156012007-03-15 23:26:47 +000065 fprintf(fp,"unsigned char %s[] = {",cname);
66
67 for (i=0;i<len;i++) {
68 if ((i % 16) == 0) {
69 fprintf(fp,"\n ");
70 }
71 if (i == (len-1)) {
72 fprintf(fp,"0x%02x",buf[i]);
73 } else {
74 fprintf(fp,"0x%02x, ",buf[i]);
75 }
76 }
77 fprintf(fp,"\n};\n");
78
79 fclose(fp);
80 return 0;
81}
82
Bertrik Sikkend4e38392008-07-14 16:43:47 +000083static int write_hfile(off_t len, const char* cname)
Dave Chapmane8156012007-03-15 23:26:47 +000084{
85 char filename[256];
86 FILE* fp;
87
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;
93 }
94
Dave Chapman2f1a2622007-06-12 14:13:57 +000095 fprintf(fp,"/* Generated by bin2c */\n\n");
Dave Chapmane8156012007-03-15 23:26:47 +000096 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;
100}
101
102int main (int argc, char* argv[])
103{
104 char* infile;
105 char* cname;
106 int fd;
107 unsigned char* buf;
108 int len;
109 int n;
110
111 if (argc != 3) {
112 fprintf(stderr,"Usage: bin2c file cname\n");
113 return 0;
114 }
115
116 infile=argv[1];
117 cname=argv[2];
118
119 fd = open(infile,O_RDONLY|O_BINARY);
120 if (fd < 0) {
121 fprintf(stderr,"Can not open %s\n",infile);
122 return 0;
123 }
124
125 len = filesize(fd);
126
127 buf = malloc(len);
128 n = read(fd,buf,len);
129 if (n < len) {
130 fprintf(stderr,"Short read, aborting\n");
131 return 0;
132 }
133 close(fd);
134
135 if (write_cfile(buf,len,cname) < 0) {
136 return -1;
137 }
Barry Wardellec04e732007-08-02 12:38:52 +0000138 if (write_hfile(len,cname) < 0) {
Dave Chapmane8156012007-03-15 23:26:47 +0000139 return -1;
140 }
141
142 return 0;
143}