-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtitext.c
157 lines (131 loc) · 2.95 KB
/
titext.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009, 2010 Daniel Beer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ctype.h>
#include <stdlib.h>
#include "titext.h"
#include "util.h"
#include "output.h"
static int is_address_line(const char *text)
{
if (*text != '@')
return 0;
text++;
if (!*text || isspace(*text))
return 0;
while (*text && !isspace(*text)) {
if (!ishex(*text))
return 0;
text++;
}
while (*text) {
if (!isspace(*text))
return 0;
text++;
}
return 1;
}
static int is_data_line(const char *text)
{
while (*text) {
if (!(ishex(*text) || isspace(*text)))
return 0;
text++;
}
return 1;
}
int titext_check(FILE *in)
{
char buf[64];
rewind(in);
if (!fgets(buf, sizeof(buf), in))
return 0;
return is_address_line(buf);
}
static int process_data_line(address_t address, const char *buf,
binfile_imgcb_t cb, void *user_data)
{
uint8_t data[64];
int data_len = 0;
int value = 0;
int vc = 0;
while (*buf) {
int c = *(buf++);
int x;
if (isspace(c)) {
if (vc) {
if (data_len >= sizeof(data))
goto too_long;
data[data_len++] = value;
}
vc = 0;
} else {
if (isdigit(c)) {
x = c - '0';
} else if (c >= 'A' && c <= 'F') {
x = c - 'A' + 10;
} else if (c >= 'a' && c <= 'f') {
x = c - 'a' + 10;
} else {
printc_err("titext: unexpected "
"character: %c\n", c);
return -1;
}
if (vc >= 2) {
printc_err("titext: too many digits "
"in hex value\n");
return -1;
}
value = (value << 4) | x;
vc++;
}
}
if (vc) {
if (data_len >= sizeof(data))
goto too_long;
data[data_len++] = value;
}
if (cb(user_data, address, data, data_len) < 0)
return -1;
return data_len;
too_long:
printc_err("titext: too many data bytes\n");
return -1;
}
int titext_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
{
address_t address = 0;
int lno = 0;
char buf[128];
rewind(in);
while (fgets(buf, sizeof(buf), in)) {
lno++;
if (is_address_line(buf)) {
address = strtoul(buf + 1, NULL, 16);
} else if (is_data_line(buf)) {
int count = process_data_line(address, buf,
cb, user_data);
if (count < 0) {
printc_err("titext: data error on line "
"%d\n", lno);
return -1;
}
address += count;
}
}
return 0;
}