2 * Copyright 2011 The Chromium Authors, All Rights Reserved.
3 * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
5 * util_is_printable_string contributed by
6 * Pantelis Antoniou <pantelis.antoniou AT gmail.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
38 char *xstrdup(const char *s
)
40 int len
= strlen(s
) + 1;
41 char *dup
= xmalloc(len
);
48 char *join_path(const char *path
, const char *name
)
50 int lenp
= strlen(path
);
51 int lenn
= strlen(name
);
56 len
= lenp
+ lenn
+ 2;
57 if ((lenp
> 0) && (path
[lenp
-1] == '/')) {
63 memcpy(str
, path
, lenp
);
68 memcpy(str
+lenp
, name
, lenn
+1);
72 int util_is_printable_string(const void *data
, int len
)
77 /* zero length is not */
81 /* must terminate with zero */
82 if (s
[len
- 1] != '\0')
86 while (*s
&& isprint(*s
))
89 /* not zero, or not done yet */
90 if (*s
!= '\0' || (s
+ 1 - ss
) < len
)
97 * Parse a octal encoded character starting at index i in string s. The
98 * resulting character will be returned and the index i will be updated to
99 * point at the character directly after the end of the encoding, this may be
100 * the '\0' terminator of the string.
102 static char get_oct_char(const char *s
, int *i
)
109 strncpy(x
, s
+ *i
, 3);
111 val
= strtol(x
, &endx
, 8);
120 * Parse a hexadecimal encoded character starting at index i in string s. The
121 * resulting character will be returned and the index i will be updated to
122 * point at the character directly after the end of the encoding, this may be
123 * the '\0' terminator of the string.
125 static char get_hex_char(const char *s
, int *i
)
132 strncpy(x
, s
+ *i
, 2);
134 val
= strtol(x
, &endx
, 16);
136 die("\\x used with no following hex digits\n");
142 char get_escape_char(const char *s
, int *i
)
179 j
--; /* need to re-read the first digit as
180 * part of the octal value */
181 val
= get_oct_char(s
, &j
);
184 val
= get_hex_char(s
, &j
);
194 int utilfdt_read_err(const char *filename
, char **buffp
)
196 int fd
= 0; /* assume stdin */
198 off_t bufsize
= 1024, offset
= 0;
202 if (strcmp(filename
, "-") != 0) {
203 fd
= open(filename
, O_RDONLY
);
208 /* Loop until we have read everything */
209 buf
= malloc(bufsize
);
211 /* Expand the buffer to hold the next chunk */
212 if (offset
== bufsize
) {
214 buf
= realloc(buf
, bufsize
);
221 ret
= read(fd
, &buf
[offset
], bufsize
- offset
);
229 /* Clean up, including closing stdin; return errno on error */
238 char *utilfdt_read(const char *filename
)
241 int ret
= utilfdt_read_err(filename
, &buff
);
244 fprintf(stderr
, "Couldn't open blob from '%s': %s\n", filename
,
248 /* Successful read */
252 int utilfdt_write_err(const char *filename
, const void *blob
)
254 int fd
= 1; /* assume stdout */
258 const char *ptr
= blob
;
260 if (strcmp(filename
, "-") != 0) {
261 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
266 totalsize
= fdt_totalsize(blob
);
269 while (offset
< totalsize
) {
270 ret
= write(fd
, ptr
+ offset
, totalsize
- offset
);
277 /* Close the file/stdin; return errno on error */
280 return ret
< 0 ? -ret
: 0;
284 int utilfdt_write(const char *filename
, const void *blob
)
286 int ret
= utilfdt_write_err(filename
, blob
);
289 fprintf(stderr
, "Couldn't write blob to '%s': %s\n", filename
,
295 int utilfdt_decode_type(const char *fmt
, int *type
, int *size
)
302 /* get the conversion qualifier */
304 if (strchr("hlLb", *fmt
)) {
306 if (qualifier
== *fmt
) {
308 /* TODO: case 'l': qualifier = 'L'; break;*/
316 /* we should now have a type */
317 if ((*fmt
== '\0') || !strchr("iuxs", *fmt
))
320 /* convert qualifier (bhL) to byte size */
322 *size
= qualifier
== 'b' ? 1 :
323 qualifier
== 'h' ? 2 :
324 qualifier
== 'l' ? 4 : -1;
327 /* that should be it! */