Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 8 | * |
| 9 | * Copyright (C) 2007 Dave Chapman |
| 10 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 11 | * 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 Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 15 | * |
| 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 Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
| 26 | #include <stdlib.h> |
Dominik Riebeling | 73f9bde | 2009-06-13 14:15:50 +0000 | [diff] [blame] | 27 | #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 Riebeling | 992b573 | 2009-11-07 20:03:32 +0000 | [diff] [blame] | 36 | #include <libgen.h> |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 37 | |
| 38 | #ifndef O_BINARY |
| 39 | #define O_BINARY 0 |
| 40 | #endif |
| 41 | |
Dominik Riebeling | b4424ca | 2012-04-28 12:02:48 +0200 | [diff] [blame] | 42 | static void usage(void) |
| 43 | { |
| 44 | fprintf(stderr, "bin2c [options] infile cfile\n"); |
| 45 | fprintf(stderr, " -i ipod mode\n"); |
| 46 | } |
| 47 | |
| 48 | |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 49 | static off_t filesize(int fd) |
| 50 | { |
| 51 | struct stat buf; |
| 52 | |
| 53 | fstat(fd,&buf); |
| 54 | return buf.st_size; |
| 55 | } |
| 56 | |
Bertrik Sikken | d4e3839 | 2008-07-14 16:43:47 +0000 | [diff] [blame] | 57 | static int write_cfile(const unsigned char* buf, off_t len, const char* cname) |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 58 | { |
| 59 | char filename[256]; |
Dominik Riebeling | 992b573 | 2009-11-07 20:03:32 +0000 | [diff] [blame] | 60 | char filebase[256]; |
| 61 | char* bn; |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 62 | FILE* fp; |
| 63 | int i; |
| 64 | |
| 65 | snprintf(filename,256,"%s.c",cname); |
Dominik Riebeling | 992b573 | 2009-11-07 20:03:32 +0000 | [diff] [blame] | 66 | strncpy(filebase, cname, 256); |
| 67 | bn = basename(filebase); |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 68 | |
| 69 | fp = fopen(filename,"w+"); |
| 70 | if (fp == NULL) { |
| 71 | fprintf(stderr,"Couldn't open %s\n",filename); |
| 72 | return -1; |
| 73 | } |
| 74 | |
Dave Chapman | 2f1a262 | 2007-06-12 14:13:57 +0000 | [diff] [blame] | 75 | fprintf(fp,"/* Generated by bin2c */\n\n"); |
Dominik Riebeling | 992b573 | 2009-11-07 20:03:32 +0000 | [diff] [blame] | 76 | fprintf(fp,"unsigned char %s[] = {",bn); |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 77 | |
| 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 Sikken | d4e3839 | 2008-07-14 16:43:47 +0000 | [diff] [blame] | 94 | static int write_hfile(off_t len, const char* cname) |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 95 | { |
| 96 | char filename[256]; |
Dominik Riebeling | 992b573 | 2009-11-07 20:03:32 +0000 | [diff] [blame] | 97 | char filebase[256]; |
| 98 | char* bn; |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 99 | FILE* fp; |
| 100 | |
| 101 | snprintf(filename,256,"%s.h",cname); |
Dominik Riebeling | 992b573 | 2009-11-07 20:03:32 +0000 | [diff] [blame] | 102 | strncpy(filebase, cname, 256); |
| 103 | bn = basename(filebase); |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 104 | fp = fopen(filename,"w+"); |
| 105 | if (fp == NULL) { |
| 106 | fprintf(stderr,"Couldn't open %s\n",filename); |
| 107 | return -1; |
| 108 | } |
| 109 | |
Dave Chapman | 2f1a262 | 2007-06-12 14:13:57 +0000 | [diff] [blame] | 110 | fprintf(fp,"/* Generated by bin2c */\n\n"); |
Dominik Riebeling | 992b573 | 2009-11-07 20:03:32 +0000 | [diff] [blame] | 111 | fprintf(fp,"#define LEN_%s %d\n",bn,(int)len); |
| 112 | fprintf(fp,"extern unsigned char %s[];\n",bn); |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 113 | fclose(fp); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int 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 Riebeling | b4424ca | 2012-04-28 12:02:48 +0200 | [diff] [blame] | 125 | int skip = 0; |
| 126 | int opts = 0; |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 127 | |
Dominik Riebeling | b4424ca | 2012-04-28 12:02:48 +0200 | [diff] [blame] | 128 | |
| 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 Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Dominik Riebeling | b4424ca | 2012-04-28 12:02:48 +0200 | [diff] [blame] | 142 | infile=argv[opts + 1]; |
| 143 | cname=argv[opts + 2]; |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 144 | |
| 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 Riebeling | b4424ca | 2012-04-28 12:02:48 +0200 | [diff] [blame] | 151 | 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 Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 157 | |
| 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 Wardell | ec04e73 | 2007-08-02 12:38:52 +0000 | [diff] [blame] | 169 | if (write_hfile(len,cname) < 0) { |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |