blob: 4600746d5277ef34cacc872b95b82e86e01b76d4 [file] [log] [blame]
Dave Chapmane8156012007-03-15 23:26:47 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
Dave Chapmane8156012007-03-15 23:26:47 +00008 *
9 * Copyright (C) 2007 Dave Chapman
10 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000011 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
Dave Chapmane8156012007-03-15 23:26:47 +000015 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20
21#include <stdio.h>
22#include <string.h>
23#include <sys/types.h>
24#include <sys/stat.h>
Dave Chapmane8156012007-03-15 23:26:47 +000025#include <fcntl.h>
26#include <stdlib.h>
Dominik Riebeling73f9bde2009-06-13 14:15:50 +000027#if !defined(_MSC_VER)
28#include <unistd.h>
29#else
30#include <io.h>
31#define snprintf _snprintf
32#define open _open
33#define close _close
34#define read _read
35#endif
Dominik Riebeling992b5732009-11-07 20:03:32 +000036#include <libgen.h>
Dave Chapmane8156012007-03-15 23:26:47 +000037
38#ifndef O_BINARY
39#define O_BINARY 0
40#endif
41
Dominik Riebelingb4424ca2012-04-28 12:02:48 +020042static void usage(void)
43{
44 fprintf(stderr, "bin2c [options] infile cfile\n");
45 fprintf(stderr, " -i ipod mode\n");
46}
47
48
Dave Chapmane8156012007-03-15 23:26:47 +000049static off_t filesize(int fd)
50{
51 struct stat buf;
52
53 fstat(fd,&buf);
54 return buf.st_size;
55}
56
Bertrik Sikkend4e38392008-07-14 16:43:47 +000057static int write_cfile(const unsigned char* buf, off_t len, const char* cname)
Dave Chapmane8156012007-03-15 23:26:47 +000058{
59 char filename[256];
Dominik Riebeling992b5732009-11-07 20:03:32 +000060 char filebase[256];
61 char* bn;
Dave Chapmane8156012007-03-15 23:26:47 +000062 FILE* fp;
63 int i;
64
65 snprintf(filename,256,"%s.c",cname);
Dominik Riebeling992b5732009-11-07 20:03:32 +000066 strncpy(filebase, cname, 256);
67 bn = basename(filebase);
Dave Chapmane8156012007-03-15 23:26:47 +000068
69 fp = fopen(filename,"w+");
70 if (fp == NULL) {
71 fprintf(stderr,"Couldn't open %s\n",filename);
72 return -1;
73 }
74
Dave Chapman2f1a2622007-06-12 14:13:57 +000075 fprintf(fp,"/* Generated by bin2c */\n\n");
Dominik Riebeling992b5732009-11-07 20:03:32 +000076 fprintf(fp,"unsigned char %s[] = {",bn);
Dave Chapmane8156012007-03-15 23:26:47 +000077
78 for (i=0;i<len;i++) {
79 if ((i % 16) == 0) {
80 fprintf(fp,"\n ");
81 }
82 if (i == (len-1)) {
83 fprintf(fp,"0x%02x",buf[i]);
84 } else {
85 fprintf(fp,"0x%02x, ",buf[i]);
86 }
87 }
88 fprintf(fp,"\n};\n");
89
90 fclose(fp);
91 return 0;
92}
93
Bertrik Sikkend4e38392008-07-14 16:43:47 +000094static int write_hfile(off_t len, const char* cname)
Dave Chapmane8156012007-03-15 23:26:47 +000095{
96 char filename[256];
Dominik Riebeling992b5732009-11-07 20:03:32 +000097 char filebase[256];
98 char* bn;
Dave Chapmane8156012007-03-15 23:26:47 +000099 FILE* fp;
100
101 snprintf(filename,256,"%s.h",cname);
Dominik Riebeling992b5732009-11-07 20:03:32 +0000102 strncpy(filebase, cname, 256);
103 bn = basename(filebase);
Dave Chapmane8156012007-03-15 23:26:47 +0000104 fp = fopen(filename,"w+");
105 if (fp == NULL) {
106 fprintf(stderr,"Couldn't open %s\n",filename);
107 return -1;
108 }
109
Dave Chapman2f1a2622007-06-12 14:13:57 +0000110 fprintf(fp,"/* Generated by bin2c */\n\n");
Dominik Riebeling992b5732009-11-07 20:03:32 +0000111 fprintf(fp,"#define LEN_%s %d\n",bn,(int)len);
112 fprintf(fp,"extern unsigned char %s[];\n",bn);
Dave Chapmane8156012007-03-15 23:26:47 +0000113 fclose(fp);
114 return 0;
115}
116
117int main (int argc, char* argv[])
118{
119 char* infile;
120 char* cname;
121 int fd;
122 unsigned char* buf;
123 int len;
124 int n;
Dominik Riebelingb4424ca2012-04-28 12:02:48 +0200125 int skip = 0;
126 int opts = 0;
Dave Chapmane8156012007-03-15 23:26:47 +0000127
Dominik Riebelingb4424ca2012-04-28 12:02:48 +0200128
129 if(argc < 2) {
130 usage();
131 return 0;
132 }
133 if(strcmp(argv[1], "-i") == 0) {
134 skip = 8;
135 opts++;
136 }
137 if (argc < opts + 3) {
138 usage();
Dave Chapmane8156012007-03-15 23:26:47 +0000139 return 0;
140 }
141
Dominik Riebelingb4424ca2012-04-28 12:02:48 +0200142 infile=argv[opts + 1];
143 cname=argv[opts + 2];
Dave Chapmane8156012007-03-15 23:26:47 +0000144
145 fd = open(infile,O_RDONLY|O_BINARY);
146 if (fd < 0) {
147 fprintf(stderr,"Can not open %s\n",infile);
148 return 0;
149 }
150
Dominik Riebelingb4424ca2012-04-28 12:02:48 +0200151 len = filesize(fd) - skip;
152 n = lseek(fd, skip, SEEK_SET);
153 if (n != skip) {
154 fprintf(stderr,"Seek failed\n");
155 return 0;
156 }
Dave Chapmane8156012007-03-15 23:26:47 +0000157
158 buf = malloc(len);
159 n = read(fd,buf,len);
160 if (n < len) {
161 fprintf(stderr,"Short read, aborting\n");
162 return 0;
163 }
164 close(fd);
165
166 if (write_cfile(buf,len,cname) < 0) {
167 return -1;
168 }
Barry Wardellec04e732007-08-02 12:38:52 +0000169 if (write_hfile(len,cname) < 0) {
Dave Chapmane8156012007-03-15 23:26:47 +0000170 return -1;
171 }
172
173 return 0;
174}