resource_lists_parser.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Copyright (C) 2005 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <time.h>
  28. #include <xcap/resource_lists_parser.h>
  29. #include <xcap/xml_utils.h>
  30. #include <cds/logger.h>
  31. #include <libxml/parser.h>
  32. #include <libxml/tree.h>
  33. #include <cds/sstr.h>
  34. static char rl_namespace[] = "urn:ietf:params:xml:ns:resource-lists";
  35. static int read_entry_ref(xmlNode *entry_node, entry_ref_t **dst)
  36. {
  37. xmlAttr *a;
  38. const char *a_val;
  39. /* allocate memory and prepare empty node */
  40. if (!dst) return -1;
  41. *dst = (entry_ref_t*)cds_malloc(sizeof(entry_ref_t));
  42. if (!(*dst)) return -2;
  43. memset(*dst, 0, sizeof(entry_ref_t));
  44. /* get attributes */
  45. a = find_attr(entry_node->properties, "ref");
  46. if (a) {
  47. a_val = get_attr_value(a);
  48. if (a_val) (*dst)->ref = zt_strdup(a_val);
  49. }
  50. return 0;
  51. }
  52. static int read_name(xmlNode *name_node, display_name_t **dst)
  53. {
  54. xmlAttr *a;
  55. const char *a_val;
  56. /* allocate memory and prepare empty node */
  57. if (!dst) return -1;
  58. *dst = (display_name_t*)cds_malloc(sizeof(display_name_t));
  59. if (!(*dst)) return -2;
  60. memset(*dst, 0, sizeof(display_name_t));
  61. /* get attributes */
  62. a = find_attr(name_node->properties, "lang");
  63. if (a) {
  64. a_val = get_attr_value(a);
  65. if (a_val) (*dst)->lang = zt_strdup(a_val);
  66. }
  67. a_val = get_node_value(name_node);
  68. if (a_val) (*dst)->name = zt_strdup(a_val);
  69. return 0;
  70. }
  71. static int read_names(xmlNode *entry_node, display_name_t **dst)
  72. {
  73. xmlNode *n;
  74. display_name_t *name, *last;
  75. int res = 0;
  76. last = NULL;
  77. *dst = NULL;
  78. n = entry_node->children;
  79. while (n) {
  80. if (n->type == XML_ELEMENT_NODE) {
  81. if (cmp_node(n, "display-name", rl_namespace) >= 0) {
  82. res = read_name(n, &name);
  83. if (res == 0) {
  84. if (name) {
  85. SEQUENCE_ADD((*dst), last, name);
  86. name = NULL;
  87. }
  88. }
  89. else break;
  90. }
  91. }
  92. n = n->next;
  93. }
  94. return res;
  95. }
  96. static int read_entry(xmlNode *entry_node, entry_t **dst)
  97. {
  98. xmlAttr *a;
  99. const char *a_val;
  100. /* allocate memory and prepare empty node */
  101. if (!dst) return -1;
  102. *dst = (entry_t*)cds_malloc(sizeof(entry_t));
  103. if (!(*dst)) return -2;
  104. memset(*dst, 0, sizeof(entry_t));
  105. /* get attributes */
  106. a = find_attr(entry_node->properties, "uri");
  107. if (a) {
  108. a_val = get_attr_value(a);
  109. if (a_val) (*dst)->uri = zt_strdup(a_val);
  110. }
  111. return read_names(entry_node, &((*dst)->display_names));
  112. }
  113. static int read_external(xmlNode *entry_node, external_t **dst)
  114. {
  115. xmlAttr *a;
  116. const char *a_val;
  117. /* allocate memory and prepare empty node */
  118. if (!dst) return -1;
  119. *dst = (external_t*)cds_malloc(sizeof(external_t));
  120. if (!(*dst)) return -2;
  121. memset(*dst, 0, sizeof(external_t));
  122. /* get attributes */
  123. a = find_attr(entry_node->properties, "anchor");
  124. if (a) {
  125. a_val = get_attr_value(a);
  126. if (a_val) (*dst)->anchor = zt_strdup(a_val);
  127. }
  128. return 0;
  129. }
  130. int read_list(xmlNode *list_node, list_t **dst, int read_content_only)
  131. {
  132. int res = 0;
  133. xmlAttr *a;
  134. const char *a_val;
  135. xmlNode *n;
  136. list_content_t *l, *last_l;
  137. /* allocate memory and prepare empty node */
  138. if (!dst) return -1;
  139. *dst = (list_t*)cds_malloc(sizeof(list_t));
  140. if (!(*dst)) return -2;
  141. memset(*dst, 0, sizeof(list_t));
  142. /* get attributes */
  143. if (!read_content_only) {
  144. a = find_attr(list_node->properties, "name");
  145. if (a) {
  146. a_val = get_attr_value(a);
  147. if (a_val) (*dst)->name = zt_strdup(a_val);
  148. }
  149. }
  150. /* read entries */
  151. last_l = NULL;
  152. n = list_node->children;
  153. while (n) {
  154. if (n->type == XML_ELEMENT_NODE) {
  155. l = (list_content_t*) cds_malloc(sizeof(list_content_t));
  156. if (!l) return -1;
  157. memset(l, 0, sizeof(*l));
  158. if (cmp_node(n, "list", rl_namespace) >= 0) {
  159. res = read_list(n, &l->u.list, 0);
  160. if (res == 0) {
  161. if (l->u.list) {
  162. l->type = lct_list;
  163. SEQUENCE_ADD((*dst)->content, last_l, l);
  164. l = NULL;
  165. }
  166. }
  167. else break;
  168. }
  169. if (cmp_node(n, "entry", rl_namespace) >= 0) {
  170. res = read_entry(n, &l->u.entry);
  171. if (res == 0) {
  172. if (l->u.entry) {
  173. l->type = lct_entry;
  174. SEQUENCE_ADD((*dst)->content, last_l, l);
  175. l = NULL;
  176. }
  177. }
  178. else break;
  179. }
  180. if (cmp_node(n, "entry-ref", rl_namespace) >= 0) {
  181. res = read_entry_ref(n, &l->u.entry_ref);
  182. if (res == 0) {
  183. if (l->u.entry_ref) {
  184. l->type = lct_entry_ref;
  185. SEQUENCE_ADD((*dst)->content, last_l, l);
  186. l = NULL;
  187. }
  188. }
  189. else break;
  190. }
  191. if (cmp_node(n, "external", rl_namespace) >= 0) {
  192. res = read_external(n, &l->u.external);
  193. if (res == 0) {
  194. if (l->u.external) {
  195. l->type = lct_external;
  196. SEQUENCE_ADD((*dst)->content, last_l, l);
  197. l = NULL;
  198. }
  199. }
  200. else break;
  201. }
  202. if (l) {
  203. cds_free(l);
  204. l = NULL;
  205. }
  206. }
  207. n = n->next;
  208. }
  209. return 0;
  210. }
  211. static int read_resource_lists(xmlNode *root, resource_lists_t **dst)
  212. {
  213. resource_lists_t *rl;
  214. /* xmlAttr *a; */
  215. xmlNode *n;
  216. list_t *l, *last_l;
  217. int res = 0;
  218. if (!dst) return -1;
  219. else *dst = NULL;
  220. if (!root) return -1;
  221. if (cmp_node(root, "resource-lists", rl_namespace) < 0) {
  222. ERROR_LOG("document is not a resource-lists\n");
  223. return -1;
  224. }
  225. rl = (resource_lists_t*)cds_malloc(sizeof(resource_lists_t));
  226. if (!rl) return -2;
  227. *dst = rl;
  228. rl->lists = NULL;
  229. last_l = NULL;
  230. n = root->children;
  231. while (n) {
  232. if (n->type == XML_ELEMENT_NODE) {
  233. if (cmp_node(n, "list", rl_namespace) >= 0) {
  234. res = read_list(n, &l, 0);
  235. if (res == 0) {
  236. if (l) SEQUENCE_ADD(rl->lists, last_l, l);
  237. }
  238. else break;
  239. }
  240. }
  241. n = n->next;
  242. }
  243. return res;
  244. }
  245. int parse_resource_lists_xml(const char *data, int data_len, resource_lists_t **dst)
  246. {
  247. int res = 0;
  248. xmlDocPtr doc; /* the resulting document tree */
  249. if (dst) *dst = NULL;
  250. doc = xmlReadMemory(data, data_len, NULL, NULL, xml_parser_flags);
  251. if (doc == NULL) {
  252. ERROR_LOG("can't parse document\n");
  253. return -1;
  254. }
  255. res = read_resource_lists(xmlDocGetRootElement(doc), dst);
  256. xmlFreeDoc(doc);
  257. return res;
  258. }
  259. int parse_list_xml(const char *data, int data_len, list_t **dst)
  260. {
  261. int res = 0;
  262. xmlDocPtr doc; /* the resulting document tree */
  263. if (dst) *dst = NULL;
  264. doc = xmlReadMemory(data, data_len, NULL, NULL, xml_parser_flags);
  265. if (doc == NULL) {
  266. ERROR_LOG("can't parse document\n");
  267. return -1;
  268. }
  269. res = read_list(xmlDocGetRootElement(doc), dst, 0);
  270. xmlFreeDoc(doc);
  271. return res;
  272. }
  273. int parse_as_list_content_xml(const char *data, int data_len, list_t **dst)
  274. {
  275. int res = 0;
  276. xmlDocPtr doc; /* the resulting document tree */
  277. if (dst) *dst = NULL;
  278. doc = xmlReadMemory(data, data_len, NULL, NULL, xml_parser_flags);
  279. if (doc == NULL) {
  280. ERROR_LOG("can't parse document\n");
  281. return -1;
  282. }
  283. res = read_list(xmlDocGetRootElement(doc), dst, 1);
  284. xmlFreeDoc(doc);
  285. return res;
  286. }
  287. int parse_entry_xml(const char *data, int data_len, entry_t **dst)
  288. {
  289. int res = 0;
  290. xmlDocPtr doc; /* the resulting document tree */
  291. if (dst) *dst = NULL;
  292. doc = xmlReadMemory(data, data_len, NULL, NULL, xml_parser_flags);
  293. if (doc == NULL) {
  294. ERROR_LOG("can't parse document\n");
  295. return -1;
  296. }
  297. res = read_entry(xmlDocGetRootElement(doc), dst);
  298. xmlFreeDoc(doc);
  299. return res;
  300. }
  301. void free_display_name(display_name_t *n)
  302. {
  303. if (!n) return;
  304. if (n->name) cds_free(n->name);
  305. if (n->lang) cds_free(n->lang);
  306. cds_free(n);
  307. }
  308. void free_display_names(display_name_t *sequence_first)
  309. {
  310. display_name_t *d, *n;
  311. if (!sequence_first) return;
  312. d = SEQUENCE_FIRST(sequence_first);
  313. while (d) {
  314. n = SEQUENCE_NEXT(d);
  315. free_display_name(d);
  316. d = n;
  317. }
  318. }
  319. void free_entry(entry_t *e)
  320. {
  321. if (!e) return;
  322. if (e->uri) cds_free(e->uri);
  323. free_display_names(e->display_names);
  324. cds_free(e);
  325. }
  326. void free_entry_ref(entry_ref_t *e)
  327. {
  328. if (!e) return;
  329. if (e->ref) cds_free(e->ref);
  330. cds_free(e);
  331. }
  332. void free_external(external_t *e)
  333. {
  334. if (!e) return;
  335. if (e->anchor) cds_free(e->anchor);
  336. cds_free(e);
  337. }
  338. void free_list(list_t *l)
  339. {
  340. list_content_t *e, *f;
  341. if (!l) return;
  342. if (l->name) cds_free(l->name);
  343. e = SEQUENCE_FIRST(l->content);
  344. while (e) {
  345. switch (e->type) {
  346. case lct_list: free_list(e->u.list); break;
  347. case lct_entry: free_entry(e->u.entry); break;
  348. case lct_entry_ref: free_entry_ref(e->u.entry_ref); break;
  349. case lct_external: free_external(e->u.external); break;
  350. }
  351. f = e;
  352. e = SEQUENCE_NEXT(e);
  353. /* TRACE_LOG("freeing %p\n", f); */
  354. cds_free(f);
  355. }
  356. cds_free(l);
  357. }
  358. void free_resource_lists(resource_lists_t *rl)
  359. {
  360. list_t *e, *f;
  361. if (!rl) return;
  362. e = SEQUENCE_FIRST(rl->lists);
  363. while (e) {
  364. f = SEQUENCE_NEXT(e);
  365. free_list(e);
  366. e = f;
  367. }
  368. cds_free(rl);
  369. }