util.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright 2011 The Chromium Authors, All Rights Reserved.
  3. * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
  4. *
  5. * util_is_printable_string contributed by
  6. * Pantelis Antoniou <pantelis.antoniou AT gmail.com>
  7. *
  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.
  12. *
  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.
  17. *
  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
  21. * USA
  22. */
  23. #include <ctype.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #include <errno.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include "libfdt.h"
  33. #include "util.h"
  34. char *xstrdup(const char *s)
  35. {
  36. int len = strlen(s) + 1;
  37. char *dup = xmalloc(len);
  38. memcpy(dup, s, len);
  39. return dup;
  40. }
  41. char *join_path(const char *path, const char *name)
  42. {
  43. int lenp = strlen(path);
  44. int lenn = strlen(name);
  45. int len;
  46. int needslash = 1;
  47. char *str;
  48. len = lenp + lenn + 2;
  49. if ((lenp > 0) && (path[lenp-1] == '/')) {
  50. needslash = 0;
  51. len--;
  52. }
  53. str = xmalloc(len);
  54. memcpy(str, path, lenp);
  55. if (needslash) {
  56. str[lenp] = '/';
  57. lenp++;
  58. }
  59. memcpy(str+lenp, name, lenn+1);
  60. return str;
  61. }
  62. int util_is_printable_string(const void *data, int len)
  63. {
  64. const char *s = data;
  65. const char *ss;
  66. /* zero length is not */
  67. if (len == 0)
  68. return 0;
  69. /* must terminate with zero */
  70. if (s[len - 1] != '\0')
  71. return 0;
  72. ss = s;
  73. while (*s && isprint(*s))
  74. s++;
  75. /* not zero, or not done yet */
  76. if (*s != '\0' || (s + 1 - ss) < len)
  77. return 0;
  78. return 1;
  79. }
  80. /*
  81. * Parse a octal encoded character starting at index i in string s. The
  82. * resulting character will be returned and the index i will be updated to
  83. * point at the character directly after the end of the encoding, this may be
  84. * the '\0' terminator of the string.
  85. */
  86. static char get_oct_char(const char *s, int *i)
  87. {
  88. char x[4];
  89. char *endx;
  90. long val;
  91. x[3] = '\0';
  92. strncpy(x, s + *i, 3);
  93. val = strtol(x, &endx, 8);
  94. assert(endx > x);
  95. (*i) += endx - x;
  96. return val;
  97. }
  98. /*
  99. * Parse a hexadecimal encoded character starting at index i in string s. The
  100. * resulting character will be returned and the index i will be updated to
  101. * point at the character directly after the end of the encoding, this may be
  102. * the '\0' terminator of the string.
  103. */
  104. static char get_hex_char(const char *s, int *i)
  105. {
  106. char x[3];
  107. char *endx;
  108. long val;
  109. x[2] = '\0';
  110. strncpy(x, s + *i, 2);
  111. val = strtol(x, &endx, 16);
  112. if (!(endx > x))
  113. die("\\x used with no following hex digits\n");
  114. (*i) += endx - x;
  115. return val;
  116. }
  117. char get_escape_char(const char *s, int *i)
  118. {
  119. char c = s[*i];
  120. int j = *i + 1;
  121. char val;
  122. assert(c);
  123. switch (c) {
  124. case 'a':
  125. val = '\a';
  126. break;
  127. case 'b':
  128. val = '\b';
  129. break;
  130. case 't':
  131. val = '\t';
  132. break;
  133. case 'n':
  134. val = '\n';
  135. break;
  136. case 'v':
  137. val = '\v';
  138. break;
  139. case 'f':
  140. val = '\f';
  141. break;
  142. case 'r':
  143. val = '\r';
  144. break;
  145. case '0':
  146. case '1':
  147. case '2':
  148. case '3':
  149. case '4':
  150. case '5':
  151. case '6':
  152. case '7':
  153. j--; /* need to re-read the first digit as
  154. * part of the octal value */
  155. val = get_oct_char(s, &j);
  156. break;
  157. case 'x':
  158. val = get_hex_char(s, &j);
  159. break;
  160. default:
  161. val = c;
  162. }
  163. (*i) = j;
  164. return val;
  165. }
  166. int utilfdt_read_err(const char *filename, char **buffp)
  167. {
  168. int fd = 0; /* assume stdin */
  169. char *buf = NULL;
  170. off_t bufsize = 1024, offset = 0;
  171. int ret = 0;
  172. *buffp = NULL;
  173. if (strcmp(filename, "-") != 0) {
  174. fd = open(filename, O_RDONLY);
  175. if (fd < 0)
  176. return errno;
  177. }
  178. /* Loop until we have read everything */
  179. buf = malloc(bufsize);
  180. do {
  181. /* Expand the buffer to hold the next chunk */
  182. if (offset == bufsize) {
  183. bufsize *= 2;
  184. buf = realloc(buf, bufsize);
  185. if (!buf) {
  186. ret = ENOMEM;
  187. break;
  188. }
  189. }
  190. ret = read(fd, &buf[offset], bufsize - offset);
  191. if (ret < 0) {
  192. ret = errno;
  193. break;
  194. }
  195. offset += ret;
  196. } while (ret != 0);
  197. /* Clean up, including closing stdin; return errno on error */
  198. close(fd);
  199. if (ret)
  200. free(buf);
  201. else
  202. *buffp = buf;
  203. return ret;
  204. }
  205. char *utilfdt_read(const char *filename)
  206. {
  207. char *buff;
  208. int ret = utilfdt_read_err(filename, &buff);
  209. if (ret) {
  210. fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
  211. strerror(ret));
  212. return NULL;
  213. }
  214. /* Successful read */
  215. return buff;
  216. }
  217. int utilfdt_write_err(const char *filename, const void *blob)
  218. {
  219. int fd = 1; /* assume stdout */
  220. int totalsize;
  221. int offset;
  222. int ret = 0;
  223. const char *ptr = blob;
  224. if (strcmp(filename, "-") != 0) {
  225. fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  226. if (fd < 0)
  227. return errno;
  228. }
  229. totalsize = fdt_totalsize(blob);
  230. offset = 0;
  231. while (offset < totalsize) {
  232. ret = write(fd, ptr + offset, totalsize - offset);
  233. if (ret < 0) {
  234. ret = -errno;
  235. break;
  236. }
  237. offset += ret;
  238. }
  239. /* Close the file/stdin; return errno on error */
  240. if (fd != 1)
  241. close(fd);
  242. return ret < 0 ? -ret : 0;
  243. }
  244. int utilfdt_write(const char *filename, const void *blob)
  245. {
  246. int ret = utilfdt_write_err(filename, blob);
  247. if (ret) {
  248. fprintf(stderr, "Couldn't write blob to '%s': %s\n", filename,
  249. strerror(ret));
  250. }
  251. return ret ? -1 : 0;
  252. }
  253. int utilfdt_decode_type(const char *fmt, int *type, int *size)
  254. {
  255. int qualifier = 0;
  256. if (!*fmt)
  257. return -1;
  258. /* get the conversion qualifier */
  259. *size = -1;
  260. if (strchr("hlLb", *fmt)) {
  261. qualifier = *fmt++;
  262. if (qualifier == *fmt) {
  263. switch (*fmt++) {
  264. /* TODO: case 'l': qualifier = 'L'; break;*/
  265. case 'h':
  266. qualifier = 'b';
  267. break;
  268. }
  269. }
  270. }
  271. /* we should now have a type */
  272. if ((*fmt == '\0') || !strchr("iuxs", *fmt))
  273. return -1;
  274. /* convert qualifier (bhL) to byte size */
  275. if (*fmt != 's')
  276. *size = qualifier == 'b' ? 1 :
  277. qualifier == 'h' ? 2 :
  278. qualifier == 'l' ? 4 : -1;
  279. *type = *fmt++;
  280. /* that should be it! */
  281. if (*fmt)
  282. return -1;
  283. return 0;
  284. }