Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2007 Dave Chapman |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 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. |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 16 | * |
| 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 Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 26 | #include <fcntl.h> |
| 27 | #include <stdlib.h> |
Dominik Riebeling | 73f9bde | 2009-06-13 14:15:50 +0000 | [diff] [blame^] | 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 |
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 | |
| 42 | static off_t filesize(int fd) |
| 43 | { |
| 44 | struct stat buf; |
| 45 | |
| 46 | fstat(fd,&buf); |
| 47 | return buf.st_size; |
| 48 | } |
| 49 | |
Bertrik Sikken | d4e3839 | 2008-07-14 16:43:47 +0000 | [diff] [blame] | 50 | 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] | 51 | { |
| 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 Chapman | 2f1a262 | 2007-06-12 14:13:57 +0000 | [diff] [blame] | 64 | fprintf(fp,"/* Generated by bin2c */\n\n"); |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 65 | 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 Sikken | d4e3839 | 2008-07-14 16:43:47 +0000 | [diff] [blame] | 83 | static int write_hfile(off_t len, const char* cname) |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 84 | { |
| 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 Chapman | 2f1a262 | 2007-06-12 14:13:57 +0000 | [diff] [blame] | 95 | fprintf(fp,"/* Generated by bin2c */\n\n"); |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 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; |
| 100 | } |
| 101 | |
| 102 | int 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 Wardell | ec04e73 | 2007-08-02 12:38:52 +0000 | [diff] [blame] | 138 | if (write_hfile(len,cname) < 0) { |
Dave Chapman | e815601 | 2007-03-15 23:26:47 +0000 | [diff] [blame] | 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |