forked from datajerk/x49gp
-
Notifications
You must be signed in to change notification settings - Fork 7
/
le32hex2bin.c
117 lines (104 loc) · 2.32 KB
/
le32hex2bin.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* $Id: le32hex2bin.c,v 1.3 2008/12/11 12:18:17 ecd Exp $
*
* $Log: le32hex2bin.c,v $
* Revision 1.3 2008/12/11 12:18:17 ecd
* major rework to qemu
*
* Revision 1.1 2006-08-20 17:26:07 ecd
* Modularize most stuff, IO_PORT, INTC, ARM, and MMU still missing.
*
* Revision 1.1 2002/06/19 08:19:39 ecd
* Add Lattice/Xilinx tools
*
* Revision 1.1 2002/01/04 10:46:48 ecd
* initial import
*
* Revision 1.1 1999/12/09 10:53:33 ecd
* add Xilinx conversion tool
*
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <netinet/in.h>
int
main(int argc, char **argv)
{
unsigned char *input, *p;
unsigned char *memory = NULL;
size_t size;
int in, out;
int i;
if (argc < 3) {
fprintf(stderr, "usage: %s <infile> <outfile>\n", argv[0]);
exit(1);
}
if (!strcmp(argv[1], "-"))
in = 0;
else {
in = open(argv[1], O_RDONLY);
if (in < 0) {
perror(argv[1]);
exit(1);
}
}
if (!strcmp(argv[2], "-"))
out = 1;
else {
out = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0666);
if (out < 0) {
perror(argv[2]);
exit(1);
}
}
size = lseek(in, 0, SEEK_END);
lseek(in, 0, SEEK_SET);
input = (unsigned char *)malloc(size);
if (!input) {
fprintf(stderr, "%s: out of memory\n", argv[0]);
exit(1);
}
if (read(in, input, size) != size) {
perror("read");
exit(1);
}
close(in);
memory = malloc(size >> 1);
if (!memory) {
fprintf(stderr, "%s: out of memory\n", argv[0]);
exit(1);
}
p = input;
for (i = 0; i < (size >> 1); i++) {
if ('0' <= *p && *p <= '9')
memory[(i & ~3) + 3 - (i & 3)] = (*p - '0') << 0;
else if ('a' <= *p && *p <= 'f')
memory[(i & ~3) + 3 - (i & 3)] = (*p - 'a' + 10) << 0;
else if ('A' <= *p && *p <= 'F')
memory[(i & ~3) + 3 - (i & 3)] = (*p - 'A' + 10) << 0;
else {
fprintf(stderr, "%s: parse error at byte %d\n",
argv[0], i);
exit(1);
}
p++;
if ('0' <= *p && *p <= '9')
memory[(i & ~3) + 3 - (i & 3)] |= (*p - '0') << 4;
else if ('a' <= *p && *p <= 'f')
memory[(i & ~3) + 3 - (i & 3)] |= (*p - 'a' + 10) << 4;
else if ('A' <= *p && *p <= 'F')
memory[(i & ~3) + 3 - (i & 3)] |= (*p - 'A' + 10) << 4;
else {
fprintf(stderr, "%s: parse error at byte %d\n",
argv[0], i);
exit(1);
}
p++;
}
write(out, memory, size >> 1);
close(out);
return 0;
}