This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bin2dat.c
84 lines (69 loc) · 1.58 KB
/
bin2dat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Convert a binary file into dat ascii-hex format
*
* Date: Dec 26th 2018
* Author: Jorge "NewEraCracker" Oliveira
* License: Unlicense (Public Domain)
* URL: https://github.com/NewEraCracker/bin2dat
*
* No warranties or guarantees express or implied.
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
// Declare variables for C89
int read, count;
FILE *in, *out;
// Check number of arguments
if (argc != 3) {
printf("Usage: %s <microcode.bin> <microcode.dat>\n", argv[0]);
return 1;
}
// Open in for reading
in = fopen(argv[1], "rb");
if (in == NULL) {
printf("ERROR: Unable to open file for read: %s\n", argv[1]);
return 1;
}
// Open out for writing
out = fopen(argv[2], "wb");
if (out == NULL) {
fclose(in); // Don't leak descriptors
printf("ERROR: Unable to open file for write: %s\n", argv[2]);
return 1;
}
// Initialize counters
read = 0;
count = 0;
do {
// Read in four byte chunks
unsigned int buffer = 0;
read = fread(&buffer, 1, sizeof(buffer), in);
// Check for EOF and invalid reads
if (!read) {
break;
} else if(read != 4) {
printf("WARN: Invalid read from: %s\n", argv[1]);
}
// Output conversion result
fprintf(out, "0x%08x,", buffer);
// Whitespace
if(++count % 4) {
fwrite(" ", 1, 1, out);
} else {
fwrite("\n", 1, 1, out);
}
} while (read);
// Check for unaligned reads
if(count % 4) {
printf("WARN: Unaligned read from: %s\n", argv[1]);
fwrite("\n", 1, 1, out);
}
// Flush output
fflush(out);
// We're done, close descriptors
fclose(out);
fclose(in);
return 0;
}