blob: 3037cdfc2010e1c9d9b45eacac7f203b09b19861 [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20
21#include "dtc.h"
22
23#if 0
24static struct data data_init_buf(char *buf, int len)
25{
26 struct data d;
27
28 d.asize = 0;
29 d.len = len;
30 d.val = buf;
31
32 return d;
33}
34
35struct data data_ref_string(char *str)
36{
37 return data_init_buf(str, strlen(str)+1);
38}
39#endif
40
David Gibson81f2e892005-06-16 17:04:00 +100041void fixup_free(struct fixup *f)
42{
43 free(f->ref);
44 free(f);
45}
46
David Gibsonfc14dad2005-06-08 17:18:34 +100047void data_free(struct data d)
48{
David Gibson81f2e892005-06-16 17:04:00 +100049 struct fixup *f;
50
51 f = d.refs;
52 while (f) {
53 struct fixup *nf;
54
55 nf = f->next;
56 fixup_free(f);
57 f = nf;
58 }
59
David Gibsonfc14dad2005-06-08 17:18:34 +100060 assert(!d.val || d.asize);
61
62 if (d.val)
63 free(d.val);
64}
65
66struct data data_grow_for(struct data d, int xlen)
67{
68 struct data nd;
69 int newsize;
70
71 /* we must start with an allocated datum */
72 assert(!d.val || d.asize);
73
74 if (xlen == 0)
75 return d;
76
77 newsize = xlen;
78
79 while ((d.len + xlen) > newsize)
80 newsize *= 2;
81
82 nd.asize = newsize;
83 nd.val = xrealloc(d.val, newsize);
84 nd.len = d.len;
David Gibson81f2e892005-06-16 17:04:00 +100085 nd.refs = d.refs;
David Gibsonfc14dad2005-06-08 17:18:34 +100086
87 assert(nd.asize >= (d.len + xlen));
88
89 return nd;
90}
91
92struct data data_copy_mem(char *mem, int len)
93{
94 struct data d;
95
96 d = data_grow_for(empty_data, len);
97
98 d.len = len;
99 memcpy(d.val, mem, len);
100
101 return d;
102}
103
David Gibson230f2532005-08-29 12:48:02 +1000104static char get_oct_char(char *s, int *i)
David Gibsonfc14dad2005-06-08 17:18:34 +1000105{
106 char x[4];
107 char *endx;
108 long val;
109
110 x[3] = '\0';
111 x[0] = s[(*i)];
112 if (x[0]) {
113 x[1] = s[(*i)+1];
114 if (x[1])
115 x[2] = s[(*i)+2];
116 }
117
118 val = strtol(x, &endx, 8);
119 if ((endx - x) == 0)
120 fprintf(stderr, "Empty \\nnn escape\n");
121
122 (*i) += endx - x;
123 return val;
124}
125
David Gibson230f2532005-08-29 12:48:02 +1000126static char get_hex_char(char *s, int *i)
David Gibsonfc14dad2005-06-08 17:18:34 +1000127{
128 char x[3];
129 char *endx;
130 long val;
131
132 x[2] = '\0';
133 x[0] = s[(*i)];
134 if (x[0])
135 x[1] = s[(*i)+1];
136
137 val = strtol(x, &endx, 16);
138 if ((endx - x) == 0)
139 fprintf(stderr, "Empty \\x escape\n");
140
141 (*i) += endx - x;
142 return val;
143}
144
145struct data data_copy_escape_string(char *s, int len)
146{
147 int i = 0;
148 struct data d;
149 char *q;
150
151 d = data_grow_for(empty_data, strlen(s)+1);
152
153 q = d.val;
154 while (i < len) {
155 char c = s[i++];
156
157 if (c != '\\') {
158 q[d.len++] = c;
159 continue;
160 }
161
162 c = s[i++];
163 assert(c);
164 switch (c) {
165 case 't':
166 q[d.len++] = '\t';
167 break;
168 case 'n':
169 q[d.len++] = '\n';
170 break;
171 case 'r':
172 q[d.len++] = '\r';
173 break;
David Gibsonb4ac0492005-10-17 10:27:45 +1000174 case '0':
175 case '1':
176 case '2':
177 case '3':
178 case '4':
179 case '5':
180 case '6':
181 case '7':
David Gibsonfc14dad2005-06-08 17:18:34 +1000182 i--; /* need to re-read the first digit as
183 * part of the octal value */
184 q[d.len++] = get_oct_char(s, &i);
185 break;
186 case 'x':
187 q[d.len++] = get_hex_char(s, &i);
188 break;
189 default:
190 q[d.len++] = c;
191 }
192 }
193
194 q[d.len++] = '\0';
195 return d;
196}
197
198struct data data_copy_file(FILE *f, size_t len)
199{
200 struct data d;
201
202 d = data_grow_for(empty_data, len);
203
204 d.len = len;
205 fread(d.val, len, 1, f);
206
207 return d;
208}
209
210struct data data_append_data(struct data d, void *p, int len)
211{
212 d = data_grow_for(d, len);
213 memcpy(d.val + d.len, p, len);
214 d.len += len;
215 return d;
216}
217
218struct data data_append_cell(struct data d, cell_t word)
219{
220 cell_t beword = cpu_to_be32(word);
221
222 return data_append_data(d, &beword, sizeof(beword));
223}
224
David Gibsonf0517db2005-07-15 17:14:24 +1000225struct data data_append_addr(struct data d, u64 addr)
226{
227 u64 beaddr = cpu_to_be64(addr);
228
229 return data_append_data(d, &beaddr, sizeof(beaddr));
230}
231
David Gibsonfc14dad2005-06-08 17:18:34 +1000232struct data data_append_byte(struct data d, uint8_t byte)
233{
234 return data_append_data(d, &byte, 1);
235}
236
237struct data data_append_zeroes(struct data d, int len)
238{
239 d = data_grow_for(d, len);
240
241 memset(d.val + d.len, 0, len);
242 d.len += len;
243 return d;
244}
245
246struct data data_append_align(struct data d, int align)
247{
248 int newlen = ALIGN(d.len, align);
249 return data_append_zeroes(d, newlen - d.len);
250}
251
David Gibson81f2e892005-06-16 17:04:00 +1000252struct data data_add_fixup(struct data d, char *ref)
253{
254 struct fixup *f;
255 struct data nd;
256
257 f = xmalloc(sizeof(*f));
258 f->offset = d.len;
259 f->ref = ref;
260 f->next = d.refs;
261
262 nd = d;
263 nd.refs = f;
264
265 return nd;
266}
267
David Gibsonfc14dad2005-06-08 17:18:34 +1000268int data_is_one_string(struct data d)
269{
270 int i;
271 int len = d.len;
272
273 if (len == 0)
274 return 0;
275
276 for (i = 0; i < len-1; i++)
277 if (d.val[i] == '\0')
278 return 0;
279
280 if (d.val[len-1] != '\0')
281 return 0;
282
283 return 1;
284}