|
| 1 | +/* |
| 2 | + * bin_to_c - convert binary data into c-compatible data tables |
| 3 | + * Written by Larry Bank |
| 4 | + * Copyright (c) 2009 BitBank Software, Inc. |
| 5 | + * |
| 6 | + * The MIT License (MIT) |
| 7 | + * |
| 8 | + * Copyright (c) 2015 bitbank2 |
| 9 | + * |
| 10 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | + * of this software and associated documentation files (the "Software"), to deal |
| 12 | + * in the Software without restriction, including without limitation the rights |
| 13 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | + * copies of the Software, and to permit persons to whom the Software is |
| 15 | + * furnished to do so, subject to the following conditions: |
| 16 | + * |
| 17 | + * The above copyright notice and this permission notice shall be included in |
| 18 | + * all copies or substantial portions of the Software. |
| 19 | + * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | + * SOFTWARE. |
| 27 | + */ |
| 28 | + |
| 29 | +#include <stdint.h> |
| 30 | +#include <stdio.h> |
| 31 | +#include <stdlib.h> |
| 32 | +#include <string.h> |
| 33 | +FILE *ihandle; |
| 34 | +void MakeC(unsigned char *, int, int); |
| 35 | +void GetLeafName(char *fname, char *leaf); |
| 36 | +void FixName(char *name); |
| 37 | + |
| 38 | +int main(int argc, char *argv[]) |
| 39 | +{ |
| 40 | + int iSize, iData; |
| 41 | + unsigned char *p; |
| 42 | + char szLeaf[256]; |
| 43 | + |
| 44 | + if (argc != 2) { |
| 45 | + printf("bin_to_c Copyright (c) 2013 BitBank Software, Inc.\n"); |
| 46 | + printf("Usage: bin_to_c <filename>\n"); |
| 47 | + printf("output is written to stdout\n"); |
| 48 | + return 0; // no filename passed |
| 49 | + } |
| 50 | + ihandle = fopen(argv[1], "rb"); // open input file |
| 51 | + if (ihandle == NULL) { |
| 52 | + printf("Unable to open file: %s\n", argv[1]); |
| 53 | + return -1; // bad filename passed |
| 54 | + } |
| 55 | + |
| 56 | + fseek(ihandle, 0L, SEEK_END); // get the file size |
| 57 | + iSize = (int) ftell(ihandle); |
| 58 | + fseek(ihandle, 0, SEEK_SET); |
| 59 | + p = (unsigned char *) malloc(0x10000); // allocate 64k to play with |
| 60 | + GetLeafName(argv[1], szLeaf); |
| 61 | + printf("//\n// %s\n//\n", szLeaf); // comment header with filename |
| 62 | + FixName(szLeaf); // remove unusable characters |
| 63 | + printf("const uint8_t %s[] = {", szLeaf); // start of data array |
| 64 | + while (iSize) { |
| 65 | + iData = fread(p, 1, 0x10000, ihandle); // try to read 64k |
| 66 | + MakeC(p, iData, iSize == iData); // create the output data |
| 67 | + iSize -= iData; |
| 68 | + } |
| 69 | + free(p); |
| 70 | + fclose(ihandle); |
| 71 | + printf("};\n"); // final closing brace |
| 72 | + return 0; |
| 73 | +} /* main() */ |
| 74 | +/* |
| 75 | + * Generate C hex characters from each byte of file data |
| 76 | + */ |
| 77 | +void MakeC(unsigned char *p, int iLen, int bLast) |
| 78 | +{ |
| 79 | + int i, j, iCount; |
| 80 | + char szTemp[256], szOut[256]; |
| 81 | + |
| 82 | + iCount = 0; |
| 83 | + for (i = 0; i < iLen >> 4; i++) // do lines of 16 bytes |
| 84 | + { |
| 85 | + strcpy(szOut, "\t"); |
| 86 | + for (j = 0; j < 16; j++) { |
| 87 | + if (iCount == iLen - 1 && bLast) // last one, skip the comma |
| 88 | + sprintf(szTemp, "0x%02x", p[(i * 16) + j]); |
| 89 | + else |
| 90 | + sprintf(szTemp, "0x%02x,", p[(i * 16) + j]); |
| 91 | + strcat(szOut, szTemp); |
| 92 | + iCount++; |
| 93 | + } |
| 94 | + if (!bLast || iCount != iLen) |
| 95 | + strcat(szOut, "\n"); |
| 96 | + printf("%s", szOut); |
| 97 | + } |
| 98 | + p += (iLen & 0xfff0); // point to last section |
| 99 | + if (iLen & 0xf) // any remaining characters? |
| 100 | + { |
| 101 | + strcpy(szOut, "\t"); |
| 102 | + for (j = 0; j < (iLen & 0xf); j++) { |
| 103 | + if (iCount == iLen - 1 && bLast) |
| 104 | + sprintf(szTemp, "0x%02x", p[j]); |
| 105 | + else |
| 106 | + sprintf(szTemp, "0x%02x,", p[j]); |
| 107 | + strcat(szOut, szTemp); |
| 108 | + iCount++; |
| 109 | + } |
| 110 | + if (!bLast) |
| 111 | + strcat(szOut, "\n"); |
| 112 | + printf("%s", szOut); |
| 113 | + } |
| 114 | +} /* MakeC() */ |
| 115 | +/* |
| 116 | + * Make sure the name can be used in C/C++ as a variable |
| 117 | + * replace invalid characters and make sure it starts with a letter |
| 118 | + */ |
| 119 | +void FixName(char *name) |
| 120 | +{ |
| 121 | + char c, *d, *s, szTemp[256]; |
| 122 | + int i, iLen; |
| 123 | + |
| 124 | + iLen = strlen(name); |
| 125 | + d = szTemp; |
| 126 | + s = name; |
| 127 | + if (s[0] >= '0' && s[0] <= '9') // starts with a digit |
| 128 | + *d++ = '_'; // Insert an underscore |
| 129 | + for (i = 0; i < iLen; i++) { |
| 130 | + c = *s++; |
| 131 | + // these characters can't be in a variable name |
| 132 | + if (c < ' ' || (c >= '!' && c < '0') || (c > 'Z' && c < 'a')) |
| 133 | + c = '_'; // convert all to an underscore |
| 134 | + *d++ = c; |
| 135 | + } |
| 136 | + *d++ = 0; |
| 137 | + strcpy(name, szTemp); |
| 138 | +} /* FixName() */ |
| 139 | +/* |
| 140 | + * Trim off the leaf name from a fully |
| 141 | + * formed file pathname |
| 142 | + */ |
| 143 | +void GetLeafName(char *fname, char *leaf) |
| 144 | +{ |
| 145 | + int i, iLen; |
| 146 | + |
| 147 | + iLen = strlen(fname); |
| 148 | + for (i = iLen - 1; i >= 0; i--) { |
| 149 | + if (fname[i] == '\\' || fname[i] == '/') // Windows or Linux |
| 150 | + break; |
| 151 | + } |
| 152 | + strcpy(leaf, &fname[i + 1]); |
| 153 | + // remove the filename extension |
| 154 | + iLen = strlen(leaf); |
| 155 | + for (i = iLen - 1; i >= 0; i--) { |
| 156 | + if (leaf[i] == '.') { |
| 157 | + leaf[i] = 0; |
| 158 | + break; |
| 159 | + } |
| 160 | + } |
| 161 | +} /* GetLeafName() */ |
0 commit comments