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