data.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  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. #include "dtc.h"
  21. void data_free(struct data d)
  22. {
  23. struct marker *m, *nm;
  24. m = d.markers;
  25. while (m) {
  26. nm = m->next;
  27. free(m->ref);
  28. free(m);
  29. m = nm;
  30. }
  31. if (d.val)
  32. free(d.val);
  33. }
  34. struct data data_grow_for(struct data d, int xlen)
  35. {
  36. struct data nd;
  37. int newsize;
  38. if (xlen == 0)
  39. return d;
  40. nd = d;
  41. newsize = xlen;
  42. while ((d.len + xlen) > newsize)
  43. newsize *= 2;
  44. nd.val = xrealloc(d.val, newsize);
  45. return nd;
  46. }
  47. struct data data_copy_mem(const char *mem, int len)
  48. {
  49. struct data d;
  50. d = data_grow_for(empty_data, len);
  51. d.len = len;
  52. memcpy(d.val, mem, len);
  53. return d;
  54. }
  55. static char get_oct_char(const char *s, int *i)
  56. {
  57. char x[4];
  58. char *endx;
  59. long val;
  60. x[3] = '\0';
  61. strncpy(x, s + *i, 3);
  62. val = strtol(x, &endx, 8);
  63. assert(endx > x);
  64. (*i) += endx - x;
  65. return val;
  66. }
  67. static char get_hex_char(const char *s, int *i)
  68. {
  69. char x[3];
  70. char *endx;
  71. long val;
  72. x[2] = '\0';
  73. strncpy(x, s + *i, 2);
  74. val = strtol(x, &endx, 16);
  75. if (!(endx > x))
  76. die("\\x used with no following hex digits\n");
  77. (*i) += endx - x;
  78. return val;
  79. }
  80. struct data data_copy_escape_string(const char *s, int len)
  81. {
  82. int i = 0;
  83. struct data d;
  84. char *q;
  85. d = data_grow_for(empty_data, strlen(s)+1);
  86. q = d.val;
  87. while (i < len) {
  88. char c = s[i++];
  89. if (c != '\\') {
  90. q[d.len++] = c;
  91. continue;
  92. }
  93. c = s[i++];
  94. assert(c);
  95. switch (c) {
  96. case 'a':
  97. q[d.len++] = '\a';
  98. break;
  99. case 'b':
  100. q[d.len++] = '\b';
  101. break;
  102. case 't':
  103. q[d.len++] = '\t';
  104. break;
  105. case 'n':
  106. q[d.len++] = '\n';
  107. break;
  108. case 'v':
  109. q[d.len++] = '\v';
  110. break;
  111. case 'f':
  112. q[d.len++] = '\f';
  113. break;
  114. case 'r':
  115. q[d.len++] = '\r';
  116. break;
  117. case '0':
  118. case '1':
  119. case '2':
  120. case '3':
  121. case '4':
  122. case '5':
  123. case '6':
  124. case '7':
  125. i--; /* need to re-read the first digit as
  126. * part of the octal value */
  127. q[d.len++] = get_oct_char(s, &i);
  128. break;
  129. case 'x':
  130. q[d.len++] = get_hex_char(s, &i);
  131. break;
  132. default:
  133. q[d.len++] = c;
  134. }
  135. }
  136. q[d.len++] = '\0';
  137. return d;
  138. }
  139. struct data data_copy_file(FILE *f, size_t maxlen)
  140. {
  141. struct data d = empty_data;
  142. while (!feof(f) && (d.len < maxlen)) {
  143. size_t chunksize, ret;
  144. if (maxlen == -1)
  145. chunksize = 4096;
  146. else
  147. chunksize = maxlen - d.len;
  148. d = data_grow_for(d, chunksize);
  149. ret = fread(d.val + d.len, 1, chunksize, f);
  150. if (ferror(f))
  151. die("Error reading file into data: %s", strerror(errno));
  152. if (d.len + ret < d.len)
  153. die("Overflow reading file into data\n");
  154. d.len += ret;
  155. }
  156. return d;
  157. }
  158. struct data data_append_data(struct data d, const void *p, int len)
  159. {
  160. d = data_grow_for(d, len);
  161. memcpy(d.val + d.len, p, len);
  162. d.len += len;
  163. return d;
  164. }
  165. struct data data_insert_at_marker(struct data d, struct marker *m,
  166. const void *p, int len)
  167. {
  168. d = data_grow_for(d, len);
  169. memmove(d.val + m->offset + len, d.val + m->offset, d.len - m->offset);
  170. memcpy(d.val + m->offset, p, len);
  171. d.len += len;
  172. /* Adjust all markers after the one we're inserting at */
  173. m = m->next;
  174. for_each_marker(m)
  175. m->offset += len;
  176. return d;
  177. }
  178. static struct data data_append_markers(struct data d, struct marker *m)
  179. {
  180. struct marker **mp = &d.markers;
  181. /* Find the end of the markerlist */
  182. while (*mp)
  183. mp = &((*mp)->next);
  184. *mp = m;
  185. return d;
  186. }
  187. struct data data_merge(struct data d1, struct data d2)
  188. {
  189. struct data d;
  190. struct marker *m2 = d2.markers;
  191. d = data_append_markers(data_append_data(d1, d2.val, d2.len), m2);
  192. /* Adjust for the length of d1 */
  193. for_each_marker(m2)
  194. m2->offset += d1.len;
  195. d2.markers = NULL; /* So data_free() doesn't clobber them */
  196. data_free(d2);
  197. return d;
  198. }
  199. struct data data_append_cell(struct data d, cell_t word)
  200. {
  201. cell_t beword = cpu_to_fdt32(word);
  202. return data_append_data(d, &beword, sizeof(beword));
  203. }
  204. struct data data_append_re(struct data d, const struct fdt_reserve_entry *re)
  205. {
  206. struct fdt_reserve_entry bere;
  207. bere.address = cpu_to_fdt64(re->address);
  208. bere.size = cpu_to_fdt64(re->size);
  209. return data_append_data(d, &bere, sizeof(bere));
  210. }
  211. struct data data_append_addr(struct data d, uint64_t addr)
  212. {
  213. uint64_t beaddr = cpu_to_fdt64(addr);
  214. return data_append_data(d, &beaddr, sizeof(beaddr));
  215. }
  216. struct data data_append_byte(struct data d, uint8_t byte)
  217. {
  218. return data_append_data(d, &byte, 1);
  219. }
  220. struct data data_append_zeroes(struct data d, int len)
  221. {
  222. d = data_grow_for(d, len);
  223. memset(d.val + d.len, 0, len);
  224. d.len += len;
  225. return d;
  226. }
  227. struct data data_append_align(struct data d, int align)
  228. {
  229. int newlen = ALIGN(d.len, align);
  230. return data_append_zeroes(d, newlen - d.len);
  231. }
  232. struct data data_add_marker(struct data d, enum markertype type, char *ref)
  233. {
  234. struct marker *m;
  235. m = xmalloc(sizeof(*m));
  236. m->offset = d.len;
  237. m->type = type;
  238. m->ref = ref;
  239. m->next = NULL;
  240. return data_append_markers(d, m);
  241. }
  242. int data_is_one_string(struct data d)
  243. {
  244. int i;
  245. int len = d.len;
  246. if (len == 0)
  247. return 0;
  248. for (i = 0; i < len-1; i++)
  249. if (d.val[i] == '\0')
  250. return 0;
  251. if (d.val[len-1] != '\0')
  252. return 0;
  253. return 1;
  254. }