blob: 36e245133e07731a4931037e42e7d3f63e854554 [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
Dominik Riebeling992b5732009-11-07 20:03:32 +000037#include <libgen.h>
Dave Chapmane8156012007-03-15 23:26:47 +000038
39#ifndef O_BINARY
40#define O_BINARY 0
41#endif
42
43static off_t filesize(int fd)
44{
45 struct stat buf;
46
47 fstat(fd,&buf);
48 return buf.st_size;
49}
50
Bertrik Sikkend4e38392008-07-14 16:43:47 +000051static int write_cfile(const unsigned char* buf, off_t len, const char* cname)
Dave Chapmane8156012007-03-15 23:26:47 +000052{
53 char filename[256];
Dominik Riebeling992b5732009-11-07 20:03:32 +000054 char filebase[256];
55 char* bn;
Dave Chapmane8156012007-03-15 23:26:47 +000056 FILE* fp;
57 int i;
58
59 snprintf(filename,256,"%s.c",cname);
Dominik Riebeling992b5732009-11-07 20:03:32 +000060 strncpy(filebase, cname, 256);
61 bn = basename(filebase);
Dave Chapmane8156012007-03-15 23:26:47 +000062
63 fp = fopen(filename,"w+");
64 if (fp == NULL) {
65 fprintf(stderr,"Couldn't open %s\n",filename);
66 return -1;
67 }
68
Dave Chapman2f1a2622007-06-12 14:13:57 +000069 fprintf(fp,"/* Generated by bin2c */\n\n");
Dominik Riebeling992b5732009-11-07 20:03:32 +000070 fprintf(fp,"unsigned char %s[] = {",bn);
Dave Chapmane8156012007-03-15 23:26:47 +000071
72 for (i=0;i<len;i++) {
73 if ((i % 16) == 0) {
74 fprintf(fp,"\n ");
75 }
76 if (i == (len-1)) {
77 fprintf(fp,"0x%02x",buf[i]);
78 } else {
79 fprintf(fp,"0x%02x, ",buf[i]);
80 }
81 }
82 fprintf(fp,"\n};\n");
83
84 fclose(fp);
85 return 0;
86}
87
Bertrik Sikkend4e38392008-07-14 16:43:47 +000088static int write_hfile(off_t len, const char* cname)
Dave Chapmane8156012007-03-15 23:26:47 +000089{
90 char filename[256];
Dominik Riebeling992b5732009-11-07 20:03:32 +000091 char filebase[256];
92 char* bn;
Dave Chapmane8156012007-03-15 23:26:47 +000093 FILE* fp;
94
95 snprintf(filename,256,"%s.h",cname);
Dominik Riebeling992b5732009-11-07 20:03:32 +000096 strncpy(filebase, cname, 256);
97 bn = basename(filebase);
Dave Chapmane8156012007-03-15 23:26:47 +000098 fp = fopen(filename,"w+");
99 if (fp == NULL) {
100 fprintf(stderr,"Couldn't open %s\n",filename);
101 return -1;
102 }
103
Dave Chapman2f1a2622007-06-12 14:13:57 +0000104 fprintf(fp,"/* Generated by bin2c */\n\n");
Dominik Riebeling992b5732009-11-07 20:03:32 +0000105 fprintf(fp,"#define LEN_%s %d\n",bn,(int)len);
106 fprintf(fp,"extern unsigned char %s[];\n",bn);
Dave Chapmane8156012007-03-15 23:26:47 +0000107 fclose(fp);
108 return 0;
109}
110
111int main (int argc, char* argv[])
112{
113 char* infile;
114 char* cname;
115 int fd;
116 unsigned char* buf;
117 int len;
118 int n;
119
120 if (argc != 3) {
121 fprintf(stderr,"Usage: bin2c file cname\n");
122 return 0;
123 }
124
125 infile=argv[1];
126 cname=argv[2];
127
128 fd = open(infile,O_RDONLY|O_BINARY);
129 if (fd < 0) {
130 fprintf(stderr,"Can not open %s\n",infile);
131 return 0;
132 }
133
134 len = filesize(fd);
135
136 buf = malloc(len);
137 n = read(fd,buf,len);
138 if (n < len) {
139 fprintf(stderr,"Short read, aborting\n");
140 return 0;
141 }
142 close(fd);
143
144 if (write_cfile(buf,len,cname) < 0) {
145 return -1;
146 }
Barry Wardellec04e732007-08-02 12:38:52 +0000147 if (write_hfile(len,cname) < 0) {
Dave Chapmane8156012007-03-15 23:26:47 +0000148 return -1;
149 }
150
151 return 0;
152}