|
| 1 | +// |
| 2 | +// bin_to_c - convert binary data into c-compatible data tables |
| 3 | +// |
| 4 | +// Written by Larry Bank |
| 5 | +// Copyright (c) 2009 BitBank Software, Inc. |
| 6 | +// Change history |
| 7 | +// 2/1/09 - Started the project |
| 8 | +// 11/6/15 - converted to Linux |
| 9 | +// 7/29/20 - tailored to Arduino FLASH data output |
| 10 | +// |
| 11 | +#include <stdint.h> |
| 12 | +#include <string.h> |
| 13 | +#include <stdio.h> |
| 14 | +#include <stdlib.h> |
| 15 | +FILE * ihandle; |
| 16 | +void MakeC(unsigned char *, int, int); |
| 17 | +void GetLeafName(char *fname, char *leaf); |
| 18 | +void FixName(char *name); |
| 19 | + |
| 20 | +int main(int argc, char *argv[]) |
| 21 | +{ |
| 22 | +int iSize, iData; |
| 23 | +unsigned char *p; |
| 24 | +char szLeaf[256]; |
| 25 | + |
| 26 | + if (argc != 2) |
| 27 | + { |
| 28 | + printf("bin_to_c Copyright (c) 2013 BitBank Software, Inc.\n"); |
| 29 | + printf("Usage: bin_to_c <filename>\n"); |
| 30 | + printf("output is written to stdout\n"); |
| 31 | + return 0; // no filename passed |
| 32 | + } |
| 33 | + ihandle = fopen(argv[1],"rb"); // open input file |
| 34 | + if (ihandle == NULL) |
| 35 | + { |
| 36 | + printf("Unable to open file: %s\n", argv[1]); |
| 37 | + return -1; // bad filename passed |
| 38 | + } |
| 39 | + |
| 40 | + fseek(ihandle, 0L, SEEK_END); // get the file size |
| 41 | + iSize = (int)ftell(ihandle); |
| 42 | + fseek(ihandle, 0, SEEK_SET); |
| 43 | + p = (unsigned char *)malloc(0x10000); // allocate 64k to play with |
| 44 | + GetLeafName(argv[1], szLeaf); |
| 45 | + printf("//\n// %s\n//\n", szLeaf); // comment header with filename |
| 46 | + FixName(szLeaf); // remove unusable characters |
| 47 | + printf("const uint8_t %s[] = {", szLeaf); // start of data array |
| 48 | + while (iSize) |
| 49 | + { |
| 50 | + iData = fread(p, 1, 0x10000, ihandle); // try to read 64k |
| 51 | + MakeC(p, iData, iSize == iData); // create the output data |
| 52 | + iSize -= iData; |
| 53 | + } |
| 54 | + free(p); |
| 55 | + fclose(ihandle); |
| 56 | + printf("};\n"); // final closing brace |
| 57 | + return 0; |
| 58 | +} /* main() */ |
| 59 | +// |
| 60 | +// Generate C hex characters from each byte of file data |
| 61 | +// |
| 62 | +void MakeC(unsigned char *p, int iLen, int bLast) |
| 63 | +{ |
| 64 | +int i, j, iCount; |
| 65 | +char szTemp[256], szOut[256]; |
| 66 | + |
| 67 | + iCount = 0; |
| 68 | + for (i=0; i<iLen>>4; i++) // do lines of 16 bytes |
| 69 | + { |
| 70 | + strcpy(szOut, "\t"); |
| 71 | + for (j=0; j<16; j++) |
| 72 | + { |
| 73 | + if (iCount == iLen-1 && bLast) // last one, skip the comma |
| 74 | + sprintf(szTemp, "0x%02x", p[(i*16)+j]); |
| 75 | + else |
| 76 | + sprintf(szTemp, "0x%02x,", p[(i*16)+j]); |
| 77 | + strcat(szOut, szTemp); |
| 78 | + iCount++; |
| 79 | + } |
| 80 | + if (!bLast || iCount != iLen) |
| 81 | + strcat(szOut, "\n"); |
| 82 | + printf("%s",szOut); |
| 83 | + } |
| 84 | + p += (iLen & 0xfff0); // point to last section |
| 85 | + if (iLen & 0xf) // any remaining characters? |
| 86 | + { |
| 87 | + strcpy(szOut, "\t"); |
| 88 | + for (j=0; j<(iLen & 0xf); j++) |
| 89 | + { |
| 90 | + if (iCount == iLen-1 && bLast) |
| 91 | + sprintf(szTemp, "0x%02x", p[j]); |
| 92 | + else |
| 93 | + sprintf(szTemp, "0x%02x,", p[j]); |
| 94 | + strcat(szOut, szTemp); |
| 95 | + iCount++; |
| 96 | + } |
| 97 | + if (!bLast) |
| 98 | + strcat(szOut, "\n"); |
| 99 | + printf("%s",szOut); |
| 100 | + } |
| 101 | +} /* MakeC() */ |
| 102 | +// |
| 103 | +// Make sure the name can be used in C/C++ as a variable |
| 104 | +// replace invalid characters and make sure it starts with a letter |
| 105 | +// |
| 106 | +void FixName(char *name) |
| 107 | +{ |
| 108 | +char c, *d, *s, szTemp[256]; |
| 109 | +int i, iLen; |
| 110 | + |
| 111 | + iLen = strlen(name); |
| 112 | + d = szTemp; |
| 113 | + s = name; |
| 114 | + if (s[0] >= '0' && s[0] <= '9') // starts with a digit |
| 115 | + *d++ = '_'; // Insert an underscore |
| 116 | + for (i=0; i<iLen; i++) |
| 117 | + { |
| 118 | + c = *s++; |
| 119 | + // these characters can't be in a variable name |
| 120 | + if (c < ' ' || (c >= '!' && c < '0') || (c > 'Z' && c < 'a')) |
| 121 | + c = '_'; // convert all to an underscore |
| 122 | + *d++ = c; |
| 123 | + } |
| 124 | + *d++ = 0; |
| 125 | + strcpy(name, szTemp); |
| 126 | +} /* FixName() */ |
| 127 | +// |
| 128 | +// Trim off the leaf name from a fully |
| 129 | +// formed file pathname |
| 130 | +// |
| 131 | +void GetLeafName(char *fname, char *leaf) |
| 132 | +{ |
| 133 | +int i, iLen; |
| 134 | + |
| 135 | + iLen = strlen(fname); |
| 136 | + for (i=iLen-1; i>=0; i--) |
| 137 | + { |
| 138 | + if (fname[i] == '\\' || fname[i] == '/') // Windows or Linux |
| 139 | + break; |
| 140 | + } |
| 141 | + strcpy(leaf, &fname[i+1]); |
| 142 | + // remove the filename extension |
| 143 | + iLen = strlen(leaf); |
| 144 | + for (i=iLen-1; i>=0; i--) |
| 145 | + { |
| 146 | + if (leaf[i] == '.') |
| 147 | + { |
| 148 | + leaf[i] = 0; |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | +} /* GetLeafName() */ |
0 commit comments