|
| 1 | +/********************************************************************** |
| 2 | +* File_to_Hex_Data_Array.c |
| 3 | +* |
| 4 | +* Program to read a file and convert the contexts to C array of unsigned |
| 5 | +* characters. |
| 6 | +**********************************************************************/ |
| 7 | + |
| 8 | +#include <errno.h> |
| 9 | +#include <fcntl.h> |
| 10 | +#include <stdbool.h> |
| 11 | +#include <stdint.h> |
| 12 | +#include <stdio.h> |
| 13 | +#include <stdlib.h> |
| 14 | +#include <string.h> |
| 15 | +#include <unistd.h> |
| 16 | + |
| 17 | +//---------------------------------------- |
| 18 | +// Globals |
| 19 | +//---------------------------------------- |
| 20 | + |
| 21 | +int binFile; |
| 22 | +uint8_t buffer[64 * 1024]; |
| 23 | +size_t offset; |
| 24 | +ssize_t validData; |
| 25 | + |
| 26 | +//---------------------------------------- |
| 27 | +// Read data from the file and add it to the array as hexadecimal bytes |
| 28 | +//---------------------------------------- |
| 29 | +int writeArray(const char * fileName, off_t fileBytes) |
| 30 | +{ |
| 31 | + size_t bufferOffset; |
| 32 | + int bytesInLine; |
| 33 | + const int bytesPerLine = 16; |
| 34 | + ssize_t bytesRead; |
| 35 | + int exitStatus; |
| 36 | + int index; |
| 37 | + int remainingBytes; |
| 38 | + size_t startingOffset; |
| 39 | + |
| 40 | + exitStatus = 0; |
| 41 | + offset = 0; |
| 42 | + |
| 43 | + // Output the header |
| 44 | + printf("// %s\r\n", fileName); |
| 45 | + printf("// array size is %ld (0x%08lx) bytes\r\n", fileBytes, fileBytes); |
| 46 | + printf("static const uint8_t dataArray[] PROGMEM =\r\n"); |
| 47 | + printf("{\r\n"); |
| 48 | + while (1) |
| 49 | + { |
| 50 | + // Read some data from the file |
| 51 | + bytesRead = read(binFile, buffer, sizeof(buffer)); |
| 52 | + if (bytesRead == 0) |
| 53 | + break; |
| 54 | + if (bytesRead < 0) |
| 55 | + { |
| 56 | + exitStatus = errno; |
| 57 | + perror("ERROR: Failed to read from file!\n"); |
| 58 | + break; |
| 59 | + } |
| 60 | + validData = bytesRead; |
| 61 | + |
| 62 | + // Display the lines of the array |
| 63 | + bufferOffset = 0; |
| 64 | + while (validData) |
| 65 | + { |
| 66 | + // Allow the most of the data to be removed |
| 67 | + if (offset == 0x80) |
| 68 | + printf("#ifdef COMPILE_ALL_FIRMWARE\r\n"); |
| 69 | + |
| 70 | + // Determine the number of bytes in this line |
| 71 | + bytesInLine = validData; |
| 72 | + if (bytesInLine > bytesPerLine) |
| 73 | + bytesInLine = bytesPerLine; |
| 74 | + remainingBytes = bytesPerLine - bytesInLine; |
| 75 | + |
| 76 | + // Output the data |
| 77 | + printf(" "); |
| 78 | + startingOffset = offset; |
| 79 | + for (index = 0; index < bytesInLine; index++) |
| 80 | + { |
| 81 | + if (index) |
| 82 | + printf(" "); |
| 83 | + printf("0x%02x,", buffer[bufferOffset++]); |
| 84 | + } |
| 85 | + |
| 86 | + // Output space until the comment area |
| 87 | + for (index = 0; index < remainingBytes; index++) |
| 88 | + printf(" "); |
| 89 | + |
| 90 | + // Output the comment |
| 91 | + printf(" // 0x%08lx, %10ld\r\n", startingOffset, startingOffset); |
| 92 | + |
| 93 | + // Account for this data |
| 94 | + validData -= bytesInLine; |
| 95 | + offset += bytesInLine; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Finish the array |
| 100 | + if (exitStatus == 0) |
| 101 | + { |
| 102 | + if (offset > 0x80) |
| 103 | + printf("#endif // COMPILE_ALL_FIRMWARE\r\n"); |
| 104 | + printf("}; // 0x%08lx, %10ld\r\n", offset, offset); |
| 105 | + printf("// %s\r\n", fileName); |
| 106 | + } |
| 107 | + return exitStatus; |
| 108 | +} |
| 109 | + |
| 110 | +//---------------------------------------- |
| 111 | +// Application |
| 112 | +//---------------------------------------- |
| 113 | +int main(int argc, char ** argv) |
| 114 | +{ |
| 115 | + off_t fileBytes; |
| 116 | + char * fileName; |
| 117 | + int status; |
| 118 | + |
| 119 | + do |
| 120 | + { |
| 121 | + status = -1; |
| 122 | + |
| 123 | + // Display the help text |
| 124 | + if (argc != 2) |
| 125 | + { |
| 126 | + printf ("%s filename\n", argv[0]); |
| 127 | + return -1; |
| 128 | + } |
| 129 | + |
| 130 | + // Open the file |
| 131 | + fileName = argv[1]; |
| 132 | + binFile = open(fileName, O_RDONLY); |
| 133 | + if (binFile < 0) |
| 134 | + { |
| 135 | + status = errno; |
| 136 | + perror("ERROR: Unable to open the file\n"); |
| 137 | + } |
| 138 | + |
| 139 | + // Determine the file size |
| 140 | + fileBytes = lseek(binFile, 0, SEEK_END); |
| 141 | + lseek(binFile, 0, SEEK_SET); |
| 142 | + |
| 143 | + // Write "C" array |
| 144 | + status = writeArray(fileName, fileBytes); |
| 145 | + } while (0); |
| 146 | + |
| 147 | + // Close the file |
| 148 | + if (binFile >= 0) |
| 149 | + close(binFile); |
| 150 | + |
| 151 | + return status; |
| 152 | +} |
0 commit comments