[POWERPC] Merge dtc upstream source
[linux-2.6/kmemtrace.git] / arch / powerpc / boot / dtc-src / data.c
bloba94718c731a95256d92e3ea1fbf63077aebd917a
1 /*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
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.
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.
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
21 #include "dtc.h"
23 void data_free(struct data d)
25 struct marker *m, *nm;
27 m = d.markers;
28 while (m) {
29 nm = m->next;
30 free(m->ref);
31 free(m);
32 m = nm;
35 assert(!d.val || d.asize);
37 if (d.val)
38 free(d.val);
41 struct data data_grow_for(struct data d, int xlen)
43 struct data nd;
44 int newsize;
46 /* we must start with an allocated datum */
47 assert(!d.val || d.asize);
49 if (xlen == 0)
50 return d;
52 nd = d;
54 newsize = xlen;
56 while ((d.len + xlen) > newsize)
57 newsize *= 2;
59 nd.asize = newsize;
60 nd.val = xrealloc(d.val, newsize);
62 assert(nd.asize >= (d.len + xlen));
64 return nd;
67 struct data data_copy_mem(const char *mem, int len)
69 struct data d;
71 d = data_grow_for(empty_data, len);
73 d.len = len;
74 memcpy(d.val, mem, len);
76 return d;
79 static char get_oct_char(const char *s, int *i)
81 char x[4];
82 char *endx;
83 long val;
85 x[3] = '\0';
86 x[0] = s[(*i)];
87 if (x[0]) {
88 x[1] = s[(*i)+1];
89 if (x[1])
90 x[2] = s[(*i)+2];
93 val = strtol(x, &endx, 8);
94 if ((endx - x) == 0)
95 fprintf(stderr, "Empty \\nnn escape\n");
97 (*i) += endx - x;
98 return val;
101 static char get_hex_char(const char *s, int *i)
103 char x[3];
104 char *endx;
105 long val;
107 x[2] = '\0';
108 x[0] = s[(*i)];
109 if (x[0])
110 x[1] = s[(*i)+1];
112 val = strtol(x, &endx, 16);
113 if ((endx - x) == 0)
114 fprintf(stderr, "Empty \\x escape\n");
116 (*i) += endx - x;
117 return val;
120 struct data data_copy_escape_string(const char *s, int len)
122 int i = 0;
123 struct data d;
124 char *q;
126 d = data_grow_for(empty_data, strlen(s)+1);
128 q = d.val;
129 while (i < len) {
130 char c = s[i++];
132 if (c != '\\') {
133 q[d.len++] = c;
134 continue;
137 c = s[i++];
138 assert(c);
139 switch (c) {
140 case 'a':
141 q[d.len++] = '\a';
142 break;
143 case 'b':
144 q[d.len++] = '\b';
145 break;
146 case 't':
147 q[d.len++] = '\t';
148 break;
149 case 'n':
150 q[d.len++] = '\n';
151 break;
152 case 'v':
153 q[d.len++] = '\v';
154 break;
155 case 'f':
156 q[d.len++] = '\f';
157 break;
158 case 'r':
159 q[d.len++] = '\r';
160 break;
161 case '0':
162 case '1':
163 case '2':
164 case '3':
165 case '4':
166 case '5':
167 case '6':
168 case '7':
169 i--; /* need to re-read the first digit as
170 * part of the octal value */
171 q[d.len++] = get_oct_char(s, &i);
172 break;
173 case 'x':
174 q[d.len++] = get_hex_char(s, &i);
175 break;
176 default:
177 q[d.len++] = c;
181 q[d.len++] = '\0';
182 return d;
185 struct data data_copy_file(FILE *f, size_t len)
187 struct data d;
189 d = data_grow_for(empty_data, len);
191 d.len = len;
192 fread(d.val, len, 1, f);
194 return d;
197 struct data data_append_data(struct data d, const void *p, int len)
199 d = data_grow_for(d, len);
200 memcpy(d.val + d.len, p, len);
201 d.len += len;
202 return d;
205 struct data data_insert_at_marker(struct data d, struct marker *m,
206 const void *p, int len)
208 d = data_grow_for(d, len);
209 memmove(d.val + m->offset + len, d.val + m->offset, d.len - m->offset);
210 memcpy(d.val + m->offset, p, len);
211 d.len += len;
213 /* Adjust all markers after the one we're inserting at */
214 m = m->next;
215 for_each_marker(m)
216 m->offset += len;
217 return d;
220 struct data data_append_markers(struct data d, struct marker *m)
222 struct marker **mp = &d.markers;
224 /* Find the end of the markerlist */
225 while (*mp)
226 mp = &((*mp)->next);
227 *mp = m;
228 return d;
231 struct data data_merge(struct data d1, struct data d2)
233 struct data d;
234 struct marker *m2 = d2.markers;
236 d = data_append_markers(data_append_data(d1, d2.val, d2.len), m2);
238 /* Adjust for the length of d1 */
239 for_each_marker(m2)
240 m2->offset += d1.len;
242 d2.markers = NULL; /* So data_free() doesn't clobber them */
243 data_free(d2);
245 return d;
248 struct data data_append_cell(struct data d, cell_t word)
250 cell_t beword = cpu_to_be32(word);
252 return data_append_data(d, &beword, sizeof(beword));
255 struct data data_append_re(struct data d, const struct fdt_reserve_entry *re)
257 struct fdt_reserve_entry bere;
259 bere.address = cpu_to_be64(re->address);
260 bere.size = cpu_to_be64(re->size);
262 return data_append_data(d, &bere, sizeof(bere));
265 struct data data_append_addr(struct data d, u64 addr)
267 u64 beaddr = cpu_to_be64(addr);
269 return data_append_data(d, &beaddr, sizeof(beaddr));
272 struct data data_append_byte(struct data d, uint8_t byte)
274 return data_append_data(d, &byte, 1);
277 struct data data_append_zeroes(struct data d, int len)
279 d = data_grow_for(d, len);
281 memset(d.val + d.len, 0, len);
282 d.len += len;
283 return d;
286 struct data data_append_align(struct data d, int align)
288 int newlen = ALIGN(d.len, align);
289 return data_append_zeroes(d, newlen - d.len);
292 struct data data_add_marker(struct data d, enum markertype type, char *ref)
294 struct marker *m;
296 m = xmalloc(sizeof(*m));
297 m->offset = d.len;
298 m->type = type;
299 m->ref = ref;
300 m->next = NULL;
302 return data_append_markers(d, m);
305 int data_is_one_string(struct data d)
307 int i;
308 int len = d.len;
310 if (len == 0)
311 return 0;
313 for (i = 0; i < len-1; i++)
314 if (d.val[i] == '\0')
315 return 0;
317 if (d.val[len-1] != '\0')
318 return 0;
320 return 1;