Dave Chapman | bdc27ff | 2007-02-08 18:05:50 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id: ipodio-win32.c 12205 2007-02-05 01:20:20Z dave $ |
| 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 | bdc27ff | 2007-02-08 18:05:50 +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> |
| 26 | #include <unistd.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <stdlib.h> |
| 29 | |
| 30 | #ifndef O_BINARY |
| 31 | #define O_BINARY 0 |
| 32 | #endif |
| 33 | |
| 34 | static off_t filesize(int fd) |
| 35 | { |
| 36 | struct stat buf; |
| 37 | |
| 38 | fstat(fd,&buf); |
| 39 | return buf.st_size; |
| 40 | } |
| 41 | |
| 42 | static int write_cfile(unsigned char* buf, off_t len, char* cname) |
| 43 | { |
| 44 | char filename[256]; |
| 45 | FILE* fp; |
| 46 | int i; |
| 47 | |
| 48 | snprintf(filename,256,"%s.c",cname); |
| 49 | |
| 50 | fp = fopen(filename,"w+"); |
| 51 | if (fp == NULL) { |
| 52 | fprintf(stderr,"Couldn't open %s\n",filename); |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | fprintf(fp,"/* Generated by ipod2c */\n\n"); |
| 57 | fprintf(fp,"unsigned char %s[] = {",cname); |
| 58 | |
| 59 | for (i=0;i<len;i++) { |
| 60 | if ((i % 16) == 0) { |
| 61 | fprintf(fp,"\n "); |
| 62 | } |
| 63 | if (i == (len-1)) { |
| 64 | fprintf(fp,"0x%02x",buf[i]); |
| 65 | } else { |
| 66 | fprintf(fp,"0x%02x, ",buf[i]); |
| 67 | } |
| 68 | } |
| 69 | fprintf(fp,"\n};\n"); |
| 70 | |
| 71 | fclose(fp); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int write_hfile(unsigned char* buf, off_t len, char* cname) |
| 76 | { |
| 77 | char filename[256]; |
| 78 | FILE* fp; |
| 79 | |
| 80 | snprintf(filename,256,"%s.h",cname); |
| 81 | fp = fopen(filename,"w+"); |
| 82 | if (fp == NULL) { |
| 83 | fprintf(stderr,"Couldn't open %s\n",filename); |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | fprintf(fp,"/* Generated by ipod2c */\n\n"); |
| 88 | fprintf(fp,"#define LEN_%s %d\n",cname,(int)len); |
| 89 | fprintf(fp,"extern unsigned char %s[];\n",cname); |
| 90 | fclose(fp); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int main (int argc, char* argv[]) |
| 95 | { |
| 96 | char* infile; |
| 97 | char* cname; |
| 98 | int fd; |
| 99 | unsigned char* buf; |
| 100 | int len; |
| 101 | int n; |
| 102 | |
| 103 | if (argc != 3) { |
Dave Chapman | 1ccc653 | 2007-03-02 00:20:42 +0000 | [diff] [blame] | 104 | fprintf(stderr,"Usage: ipod2c file.ipod cname\n"); |
Dave Chapman | bdc27ff | 2007-02-08 18:05:50 +0000 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | infile=argv[1]; |
| 109 | cname=argv[2]; |
| 110 | |
Dave Chapman | 8ae8351 | 2007-03-03 10:58:42 +0000 | [diff] [blame] | 111 | fd = open(infile,O_RDONLY|O_BINARY); |
Dave Chapman | bdc27ff | 2007-02-08 18:05:50 +0000 | [diff] [blame] | 112 | if (fd < 0) { |
| 113 | fprintf(stderr,"Can not open %s\n",infile); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | len = filesize(fd) - 8; |
| 118 | |
| 119 | n = lseek(fd,8,SEEK_SET); |
| 120 | if (n != 8) { |
| 121 | fprintf(stderr,"Seek failed\n"); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | buf = malloc(len); |
| 126 | n = read(fd,buf,len); |
| 127 | if (n < len) { |
| 128 | fprintf(stderr,"Short read, aborting\n"); |
| 129 | return 0; |
| 130 | } |
| 131 | close(fd); |
| 132 | |
| 133 | if (write_cfile(buf,len,cname) < 0) { |
| 134 | return -1; |
| 135 | } |
| 136 | if (write_hfile(buf,len,cname) < 0) { |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |