xml-utils.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. if (val) {
  195. xml_node_create_text(ctx, tnds, NULL, "Value", val);
  196. xml_node_get_text_free(ctx, val);
  197. }
  198. new_uri = add_path(uri, name);
  199. node_to_tnds(ctx, new_uri ? out : tnds, node, new_uri);
  200. os_free(new_uri);
  201. }
  202. }
  203. static int add_ddfname(struct xml_node_ctx *ctx, xml_node_t *parent,
  204. const char *urn)
  205. {
  206. xml_node_t *node;
  207. node = xml_node_create(ctx, parent, NULL, "RTProperties");
  208. if (node == NULL)
  209. return -1;
  210. node = xml_node_create(ctx, node, NULL, "Type");
  211. if (node == NULL)
  212. return -1;
  213. xml_node_create_text(ctx, node, NULL, "DDFName", urn);
  214. return 0;
  215. }
  216. xml_node_t * mo_to_tnds(struct xml_node_ctx *ctx, xml_node_t *mo,
  217. int use_path, const char *urn, const char *ns_uri)
  218. {
  219. xml_node_t *root;
  220. xml_node_t *node;
  221. const char *name;
  222. root = xml_node_create_root(ctx, ns_uri, NULL, NULL, "MgmtTree");
  223. if (root == NULL)
  224. return NULL;
  225. xml_node_create_text(ctx, root, NULL, "VerDTD", "1.2");
  226. name = xml_node_get_localname(ctx, mo);
  227. node = xml_node_create(ctx, root, NULL, "Node");
  228. if (node == NULL)
  229. goto fail;
  230. xml_node_create_text(ctx, node, NULL, "NodeName", name);
  231. if (urn)
  232. add_ddfname(ctx, node, urn);
  233. node_to_tnds(ctx, use_path ? root : node, mo, use_path ? name : NULL);
  234. return root;
  235. fail:
  236. xml_node_free(ctx, root);
  237. return NULL;
  238. }
  239. static xml_node_t * get_first_child_node(struct xml_node_ctx *ctx,
  240. xml_node_t *node,
  241. const char *name)
  242. {
  243. const char *lname;
  244. xml_node_t *child;
  245. xml_node_for_each_child(ctx, child, node) {
  246. xml_node_for_each_check(ctx, child);
  247. lname = xml_node_get_localname(ctx, child);
  248. if (os_strcasecmp(lname, name) == 0)
  249. return child;
  250. }
  251. return NULL;
  252. }
  253. static char * get_node_text(struct xml_node_ctx *ctx, xml_node_t *node,
  254. const char *node_name)
  255. {
  256. node = get_first_child_node(ctx, node, node_name);
  257. if (node == NULL)
  258. return NULL;
  259. return xml_node_get_text(ctx, node);
  260. }
  261. static xml_node_t * add_mo_node(struct xml_node_ctx *ctx, xml_node_t *root,
  262. xml_node_t *node, const char *uri)
  263. {
  264. char *nodename, *value, *path;
  265. xml_node_t *parent;
  266. nodename = get_node_text(ctx, node, "NodeName");
  267. if (nodename == NULL)
  268. return NULL;
  269. value = get_node_text(ctx, node, "Value");
  270. if (root == NULL) {
  271. root = xml_node_create_root(ctx, NULL, NULL, NULL,
  272. nodename);
  273. if (root && value)
  274. xml_node_set_text(ctx, root, value);
  275. } else {
  276. if (uri == NULL) {
  277. xml_node_get_text_free(ctx, nodename);
  278. xml_node_get_text_free(ctx, value);
  279. return NULL;
  280. }
  281. path = get_node_text(ctx, node, "Path");
  282. if (path)
  283. uri = path;
  284. parent = get_node_uri(ctx, root, uri);
  285. xml_node_get_text_free(ctx, path);
  286. if (parent == NULL) {
  287. printf("Could not find URI '%s'\n", uri);
  288. xml_node_get_text_free(ctx, nodename);
  289. xml_node_get_text_free(ctx, value);
  290. return NULL;
  291. }
  292. if (value)
  293. xml_node_create_text(ctx, parent, NULL, nodename,
  294. value);
  295. else
  296. xml_node_create(ctx, parent, NULL, nodename);
  297. }
  298. xml_node_get_text_free(ctx, nodename);
  299. xml_node_get_text_free(ctx, value);
  300. return root;
  301. }
  302. static xml_node_t * tnds_to_mo_iter(struct xml_node_ctx *ctx, xml_node_t *root,
  303. xml_node_t *node, const char *uri)
  304. {
  305. xml_node_t *child;
  306. const char *name;
  307. char *nodename;
  308. xml_node_for_each_sibling(ctx, node) {
  309. xml_node_for_each_check(ctx, node);
  310. nodename = get_node_text(ctx, node, "NodeName");
  311. if (nodename == NULL)
  312. return NULL;
  313. name = xml_node_get_localname(ctx, node);
  314. if (strcmp(name, "Node") == 0) {
  315. if (root && !uri) {
  316. printf("Invalid TNDS tree structure - "
  317. "multiple top level nodes\n");
  318. xml_node_get_text_free(ctx, nodename);
  319. return NULL;
  320. }
  321. root = add_mo_node(ctx, root, node, uri);
  322. }
  323. child = get_first_child_node(ctx, node, "Node");
  324. if (child) {
  325. if (uri == NULL)
  326. tnds_to_mo_iter(ctx, root, child, nodename);
  327. else {
  328. char *new_uri;
  329. new_uri = add_path(uri, nodename);
  330. tnds_to_mo_iter(ctx, root, child, new_uri);
  331. os_free(new_uri);
  332. }
  333. }
  334. xml_node_get_text_free(ctx, nodename);
  335. }
  336. return root;
  337. }
  338. xml_node_t * tnds_to_mo(struct xml_node_ctx *ctx, xml_node_t *tnds)
  339. {
  340. const char *name;
  341. xml_node_t *node;
  342. name = xml_node_get_localname(ctx, tnds);
  343. if (name == NULL || os_strcmp(name, "MgmtTree") != 0)
  344. return NULL;
  345. node = get_first_child_node(ctx, tnds, "Node");
  346. if (!node)
  347. return NULL;
  348. return tnds_to_mo_iter(ctx, NULL, node, NULL);
  349. }
  350. xml_node_t * soap_build_envelope(struct xml_node_ctx *ctx, xml_node_t *node)
  351. {
  352. xml_node_t *envelope, *body;
  353. xml_namespace_t *ns;
  354. envelope = xml_node_create_root(
  355. ctx, "http://www.w3.org/2003/05/soap-envelope", "soap12", &ns,
  356. "Envelope");
  357. if (envelope == NULL)
  358. return NULL;
  359. body = xml_node_create(ctx, envelope, ns, "Body");
  360. xml_node_add_child(ctx, body, node);
  361. return envelope;
  362. }
  363. xml_node_t * soap_get_body(struct xml_node_ctx *ctx, xml_node_t *soap)
  364. {
  365. xml_node_t *body, *child;
  366. body = get_node_uri(ctx, soap, "Envelope/Body");
  367. if (body == NULL)
  368. return NULL;
  369. xml_node_for_each_child(ctx, child, body) {
  370. xml_node_for_each_check(ctx, child);
  371. return child;
  372. }
  373. return NULL;
  374. }