xml-utils.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Generic XML helper functions
  3. * Copyright (c) 2012-2013, Qualcomm Atheros, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "xml-utils.h"
  11. static xml_node_t * get_node_uri_iter(struct xml_node_ctx *ctx,
  12. xml_node_t *root, char *uri)
  13. {
  14. char *end;
  15. xml_node_t *node;
  16. const char *name;
  17. end = strchr(uri, '/');
  18. if (end)
  19. *end++ = '\0';
  20. node = root;
  21. xml_node_for_each_sibling(ctx, node) {
  22. xml_node_for_each_check(ctx, node);
  23. name = xml_node_get_localname(ctx, node);
  24. if (strcasecmp(name, uri) == 0)
  25. break;
  26. }
  27. if (node == NULL)
  28. return NULL;
  29. if (end) {
  30. return get_node_uri_iter(ctx, xml_node_first_child(ctx, node),
  31. end);
  32. }
  33. return node;
  34. }
  35. xml_node_t * get_node_uri(struct xml_node_ctx *ctx, xml_node_t *root,
  36. const char *uri)
  37. {
  38. char *search;
  39. xml_node_t *node;
  40. search = os_strdup(uri);
  41. if (search == NULL)
  42. return NULL;
  43. node = get_node_uri_iter(ctx, root, search);
  44. os_free(search);
  45. return node;
  46. }
  47. static xml_node_t * get_node_iter(struct xml_node_ctx *ctx,
  48. xml_node_t *root, const char *path)
  49. {
  50. char *end;
  51. xml_node_t *node;
  52. const char *name;
  53. end = os_strchr(path, '/');
  54. if (end)
  55. *end++ = '\0';
  56. xml_node_for_each_child(ctx, node, root) {
  57. xml_node_for_each_check(ctx, node);
  58. name = xml_node_get_localname(ctx, node);
  59. if (os_strcasecmp(name, path) == 0)
  60. break;
  61. }
  62. if (node == NULL)
  63. return NULL;
  64. if (end)
  65. return get_node_iter(ctx, node, end);
  66. return node;
  67. }
  68. xml_node_t * get_node(struct xml_node_ctx *ctx, xml_node_t *root,
  69. const char *path)
  70. {
  71. char *search;
  72. xml_node_t *node;
  73. search = os_strdup(path);
  74. if (search == NULL)
  75. return NULL;
  76. node = get_node_iter(ctx, root, search);
  77. os_free(search);
  78. return node;
  79. }
  80. xml_node_t * get_child_node(struct xml_node_ctx *ctx, xml_node_t *root,
  81. const char *path)
  82. {
  83. xml_node_t *node;
  84. xml_node_t *match;
  85. xml_node_for_each_child(ctx, node, root) {
  86. xml_node_for_each_check(ctx, node);
  87. match = get_node(ctx, node, path);
  88. if (match)
  89. return match;
  90. }
  91. return NULL;
  92. }
  93. xml_node_t * node_from_file(struct xml_node_ctx *ctx, const char *name)
  94. {
  95. xml_node_t *node;
  96. char *buf, *buf2, *start;
  97. size_t len;
  98. buf = os_readfile(name, &len);
  99. if (buf == NULL)
  100. return NULL;
  101. buf2 = os_realloc(buf, len + 1);
  102. if (buf2 == NULL) {
  103. os_free(buf);
  104. return NULL;
  105. }
  106. buf = buf2;
  107. buf[len] = '\0';
  108. start = os_strstr(buf, "<!DOCTYPE ");
  109. if (start) {
  110. char *pos = start + 1;
  111. int count = 1;
  112. while (*pos) {
  113. if (*pos == '<')
  114. count++;
  115. else if (*pos == '>') {
  116. count--;
  117. if (count == 0) {
  118. pos++;
  119. break;
  120. }
  121. }
  122. pos++;
  123. }
  124. if (count == 0) {
  125. /* Remove DOCTYPE to allow the file to be parsed */
  126. os_memset(start, ' ', pos - start);
  127. }
  128. }
  129. node = xml_node_from_buf(ctx, buf);
  130. os_free(buf);
  131. return node;
  132. }
  133. int node_to_file(struct xml_node_ctx *ctx, const char *fname, xml_node_t *node)
  134. {
  135. FILE *f;
  136. char *str;
  137. str = xml_node_to_str(ctx, node);
  138. if (str == NULL)
  139. return -1;
  140. f = fopen(fname, "w");
  141. if (!f) {
  142. os_free(str);
  143. return -1;
  144. }
  145. fprintf(f, "%s\n", str);
  146. os_free(str);
  147. fclose(f);
  148. return 0;
  149. }
  150. static char * get_val(struct xml_node_ctx *ctx, xml_node_t *node)
  151. {
  152. char *val, *pos;
  153. val = xml_node_get_text(ctx, node);
  154. if (val == NULL)
  155. return NULL;
  156. pos = val;
  157. while (*pos) {
  158. if (*pos != ' ' && *pos != '\t' && *pos != '\r' && *pos != '\n')
  159. return val;
  160. pos++;
  161. }
  162. return NULL;
  163. }
  164. static char * add_path(const char *prev, const char *leaf)
  165. {
  166. size_t len;
  167. char *new_uri;
  168. if (prev == NULL)
  169. return NULL;
  170. len = os_strlen(prev) + 1 + os_strlen(leaf) + 1;
  171. new_uri = os_malloc(len);
  172. if (new_uri)
  173. os_snprintf(new_uri, len, "%s/%s", prev, leaf);
  174. return new_uri;
  175. }
  176. static void node_to_tnds(struct xml_node_ctx *ctx, xml_node_t *out,
  177. xml_node_t *in, const char *uri)
  178. {
  179. xml_node_t *node;
  180. xml_node_t *tnds;
  181. const char *name;
  182. char *val;
  183. char *new_uri;
  184. xml_node_for_each_child(ctx, node, in) {
  185. xml_node_for_each_check(ctx, node);
  186. name = xml_node_get_localname(ctx, node);
  187. tnds = xml_node_create(ctx, out, NULL, "Node");
  188. if (tnds == NULL)
  189. return;
  190. xml_node_create_text(ctx, tnds, NULL, "NodeName", name);
  191. if (uri)
  192. xml_node_create_text(ctx, tnds, NULL, "Path", uri);
  193. val = get_val(ctx, node);
  194. xml_node_create_text(ctx, tnds, NULL, "Value", val ? val : "");
  195. xml_node_get_text_free(ctx, val);
  196. new_uri = add_path(uri, name);
  197. node_to_tnds(ctx, new_uri ? out : tnds, node, new_uri);
  198. os_free(new_uri);
  199. }
  200. }
  201. static int add_ddfname(struct xml_node_ctx *ctx, xml_node_t *parent,
  202. const char *urn)
  203. {
  204. xml_node_t *node;
  205. node = xml_node_create(ctx, parent, NULL, "RTProperties");
  206. if (node == NULL)
  207. return -1;
  208. node = xml_node_create(ctx, node, NULL, "Type");
  209. if (node == NULL)
  210. return -1;
  211. xml_node_create_text(ctx, node, NULL, "DDFName", urn);
  212. return 0;
  213. }
  214. xml_node_t * mo_to_tnds(struct xml_node_ctx *ctx, xml_node_t *mo,
  215. int use_path, const char *urn, const char *ns_uri)
  216. {
  217. xml_node_t *root;
  218. xml_node_t *node;
  219. const char *name;
  220. root = xml_node_create_root(ctx, ns_uri, NULL, NULL, "MgmtTree");
  221. if (root == NULL)
  222. return NULL;
  223. xml_node_create_text(ctx, root, NULL, "VerDTD", "1.2");
  224. name = xml_node_get_localname(ctx, mo);
  225. node = xml_node_create(ctx, root, NULL, "Node");
  226. if (node == NULL)
  227. goto fail;
  228. xml_node_create_text(ctx, node, NULL, "NodeName", name);
  229. if (urn)
  230. add_ddfname(ctx, node, urn);
  231. node_to_tnds(ctx, use_path ? root : node, mo, use_path ? name : NULL);
  232. return root;
  233. fail:
  234. xml_node_free(ctx, root);
  235. return NULL;
  236. }
  237. static xml_node_t * get_first_child_node(struct xml_node_ctx *ctx,
  238. xml_node_t *node,
  239. const char *name)
  240. {
  241. const char *lname;
  242. xml_node_t *child;
  243. xml_node_for_each_child(ctx, child, node) {
  244. xml_node_for_each_check(ctx, child);
  245. lname = xml_node_get_localname(ctx, child);
  246. if (os_strcasecmp(lname, name) == 0)
  247. return child;
  248. }
  249. return NULL;
  250. }
  251. static char * get_node_text(struct xml_node_ctx *ctx, xml_node_t *node,
  252. const char *node_name)
  253. {
  254. node = get_first_child_node(ctx, node, node_name);
  255. if (node == NULL)
  256. return NULL;
  257. return xml_node_get_text(ctx, node);
  258. }
  259. static xml_node_t * add_mo_node(struct xml_node_ctx *ctx, xml_node_t *root,
  260. xml_node_t *node, const char *uri)
  261. {
  262. char *nodename, *value, *path;
  263. xml_node_t *parent;
  264. nodename = get_node_text(ctx, node, "NodeName");
  265. if (nodename == NULL)
  266. return NULL;
  267. value = get_node_text(ctx, node, "Value");
  268. if (root == NULL) {
  269. root = xml_node_create_root(ctx, NULL, NULL, NULL,
  270. nodename);
  271. if (root && value)
  272. xml_node_set_text(ctx, root, value);
  273. } else {
  274. if (uri == NULL) {
  275. xml_node_get_text_free(ctx, nodename);
  276. xml_node_get_text_free(ctx, value);
  277. return NULL;
  278. }
  279. path = get_node_text(ctx, node, "Path");
  280. if (path)
  281. uri = path;
  282. parent = get_node_uri(ctx, root, uri);
  283. xml_node_get_text_free(ctx, path);
  284. if (parent == NULL) {
  285. printf("Could not find URI '%s'\n", uri);
  286. xml_node_get_text_free(ctx, nodename);
  287. xml_node_get_text_free(ctx, value);
  288. return NULL;
  289. }
  290. if (value)
  291. xml_node_create_text(ctx, parent, NULL, nodename,
  292. value);
  293. else
  294. xml_node_create(ctx, parent, NULL, nodename);
  295. }
  296. xml_node_get_text_free(ctx, nodename);
  297. xml_node_get_text_free(ctx, value);
  298. return root;
  299. }
  300. static xml_node_t * tnds_to_mo_iter(struct xml_node_ctx *ctx, xml_node_t *root,
  301. xml_node_t *node, const char *uri)
  302. {
  303. xml_node_t *child;
  304. const char *name;
  305. char *nodename;
  306. xml_node_for_each_sibling(ctx, node) {
  307. xml_node_for_each_check(ctx, node);
  308. nodename = get_node_text(ctx, node, "NodeName");
  309. if (nodename == NULL)
  310. return NULL;
  311. name = xml_node_get_localname(ctx, node);
  312. if (strcmp(name, "Node") == 0) {
  313. if (root && !uri) {
  314. printf("Invalid TNDS tree structure - "
  315. "multiple top level nodes\n");
  316. xml_node_get_text_free(ctx, nodename);
  317. return NULL;
  318. }
  319. root = add_mo_node(ctx, root, node, uri);
  320. }
  321. child = get_first_child_node(ctx, node, "Node");
  322. if (child) {
  323. if (uri == NULL)
  324. tnds_to_mo_iter(ctx, root, child, nodename);
  325. else {
  326. char *new_uri;
  327. new_uri = add_path(uri, nodename);
  328. tnds_to_mo_iter(ctx, root, child, new_uri);
  329. os_free(new_uri);
  330. }
  331. }
  332. xml_node_get_text_free(ctx, nodename);
  333. }
  334. return root;
  335. }
  336. xml_node_t * tnds_to_mo(struct xml_node_ctx *ctx, xml_node_t *tnds)
  337. {
  338. const char *name;
  339. xml_node_t *node;
  340. name = xml_node_get_localname(ctx, tnds);
  341. if (name == NULL || os_strcmp(name, "MgmtTree") != 0)
  342. return NULL;
  343. node = get_first_child_node(ctx, tnds, "Node");
  344. if (!node)
  345. return NULL;
  346. return tnds_to_mo_iter(ctx, NULL, node, NULL);
  347. }
  348. xml_node_t * soap_build_envelope(struct xml_node_ctx *ctx, xml_node_t *node)
  349. {
  350. xml_node_t *envelope, *body;
  351. xml_namespace_t *ns;
  352. envelope = xml_node_create_root(
  353. ctx, "http://www.w3.org/2003/05/soap-envelope", "soap12", &ns,
  354. "Envelope");
  355. if (envelope == NULL)
  356. return NULL;
  357. body = xml_node_create(ctx, envelope, ns, "Body");
  358. xml_node_add_child(ctx, body, node);
  359. return envelope;
  360. }
  361. xml_node_t * soap_get_body(struct xml_node_ctx *ctx, xml_node_t *soap)
  362. {
  363. xml_node_t *body, *child;
  364. body = get_node_uri(ctx, soap, "Envelope/Body");
  365. if (body == NULL)
  366. return NULL;
  367. xml_node_for_each_child(ctx, child, body) {
  368. xml_node_for_each_check(ctx, child);
  369. return child;
  370. }
  371. return NULL;
  372. }