xref: /netbsd/external/bsd/wpa/dist/src/utils/xml-utils.h (revision 36d97821)
1*36d97821Schristos /*
2*36d97821Schristos  * Generic XML helper functions
3*36d97821Schristos  * Copyright (c) 2012-2013, Qualcomm Atheros, Inc.
4*36d97821Schristos  *
5*36d97821Schristos  * This software may be distributed under the terms of the BSD license.
6*36d97821Schristos  * See README for more details.
7*36d97821Schristos  */
8*36d97821Schristos 
9*36d97821Schristos #ifndef XML_UTILS_H
10*36d97821Schristos #define XML_UTILS_H
11*36d97821Schristos 
12*36d97821Schristos struct xml_node_ctx;
13*36d97821Schristos typedef struct xml_node xml_node_t;
14*36d97821Schristos typedef struct xml_namespace_foo xml_namespace_t;
15*36d97821Schristos 
16*36d97821Schristos /* XML library wrappers */
17*36d97821Schristos 
18*36d97821Schristos int xml_validate(struct xml_node_ctx *ctx, xml_node_t *node,
19*36d97821Schristos 		 const char *xml_schema_fname, char **ret_err);
20*36d97821Schristos int xml_validate_dtd(struct xml_node_ctx *ctx, xml_node_t *node,
21*36d97821Schristos 		     const char *dtd_fname, char **ret_err);
22*36d97821Schristos void xml_node_free(struct xml_node_ctx *ctx, xml_node_t *node);
23*36d97821Schristos xml_node_t * xml_node_get_parent(struct xml_node_ctx *ctx, xml_node_t *node);
24*36d97821Schristos xml_node_t * xml_node_from_buf(struct xml_node_ctx *ctx, const char *buf);
25*36d97821Schristos const char * xml_node_get_localname(struct xml_node_ctx *ctx,
26*36d97821Schristos 				    xml_node_t *node);
27*36d97821Schristos char * xml_node_to_str(struct xml_node_ctx *ctx, xml_node_t *node);
28*36d97821Schristos void xml_node_detach(struct xml_node_ctx *ctx, xml_node_t *node);
29*36d97821Schristos void xml_node_add_child(struct xml_node_ctx *ctx, xml_node_t *parent,
30*36d97821Schristos 			xml_node_t *child);
31*36d97821Schristos xml_node_t * xml_node_create_root(struct xml_node_ctx *ctx, const char *ns_uri,
32*36d97821Schristos 				  const char *ns_prefix,
33*36d97821Schristos 				  xml_namespace_t **ret_ns, const char *name);
34*36d97821Schristos xml_node_t * xml_node_create(struct xml_node_ctx *ctx, xml_node_t *parent,
35*36d97821Schristos 			     xml_namespace_t *ns, const char *name);
36*36d97821Schristos xml_node_t * xml_node_create_text(struct xml_node_ctx *ctx,
37*36d97821Schristos 				  xml_node_t *parent, xml_namespace_t *ns,
38*36d97821Schristos 				  const char *name, const char *value);
39*36d97821Schristos xml_node_t * xml_node_create_text_ns(struct xml_node_ctx *ctx,
40*36d97821Schristos 				     xml_node_t *parent, const char *ns_uri,
41*36d97821Schristos 				     const char *name, const char *value);
42*36d97821Schristos void xml_node_set_text(struct xml_node_ctx *ctx, xml_node_t *node,
43*36d97821Schristos 		       const char *value);
44*36d97821Schristos int xml_node_add_attr(struct xml_node_ctx *ctx, xml_node_t *node,
45*36d97821Schristos 		      xml_namespace_t *ns, const char *name, const char *value);
46*36d97821Schristos char * xml_node_get_attr_value(struct xml_node_ctx *ctx, xml_node_t *node,
47*36d97821Schristos 			       char *name);
48*36d97821Schristos char * xml_node_get_attr_value_ns(struct xml_node_ctx *ctx, xml_node_t *node,
49*36d97821Schristos 				  const char *ns_uri, char *name);
50*36d97821Schristos void xml_node_get_attr_value_free(struct xml_node_ctx *ctx, char *val);
51*36d97821Schristos xml_node_t * xml_node_first_child(struct xml_node_ctx *ctx,
52*36d97821Schristos 				  xml_node_t *parent);
53*36d97821Schristos xml_node_t * xml_node_next_sibling(struct xml_node_ctx *ctx,
54*36d97821Schristos 				   xml_node_t *node);
55*36d97821Schristos int xml_node_is_element(struct xml_node_ctx *ctx, xml_node_t *node);
56*36d97821Schristos char * xml_node_get_text(struct xml_node_ctx *ctx, xml_node_t *node);
57*36d97821Schristos void xml_node_get_text_free(struct xml_node_ctx *ctx, char *val);
58*36d97821Schristos char * xml_node_get_base64_text(struct xml_node_ctx *ctx, xml_node_t *node,
59*36d97821Schristos 				int *ret_len);
60*36d97821Schristos xml_node_t * xml_node_copy(struct xml_node_ctx *ctx, xml_node_t *node);
61*36d97821Schristos 
62*36d97821Schristos #define xml_node_for_each_child(ctx, child, parent) \
63*36d97821Schristos for (child = xml_node_first_child(ctx, parent); \
64*36d97821Schristos      child; \
65*36d97821Schristos      child = xml_node_next_sibling(ctx, child))
66*36d97821Schristos 
67*36d97821Schristos #define xml_node_for_each_sibling(ctx, node) \
68*36d97821Schristos for (; \
69*36d97821Schristos      node; \
70*36d97821Schristos      node = xml_node_next_sibling(ctx, node))
71*36d97821Schristos 
72*36d97821Schristos #define xml_node_for_each_check(ctx, child) \
73*36d97821Schristos if (!xml_node_is_element(ctx, child)) \
74*36d97821Schristos 	continue
75*36d97821Schristos 
76*36d97821Schristos 
77*36d97821Schristos struct xml_node_ctx * xml_node_init_ctx(void *upper_ctx,
78*36d97821Schristos 					const void *env);
79*36d97821Schristos void xml_node_deinit_ctx(struct xml_node_ctx *ctx);
80*36d97821Schristos 
81*36d97821Schristos 
82*36d97821Schristos xml_node_t * get_node_uri(struct xml_node_ctx *ctx, xml_node_t *root,
83*36d97821Schristos 			  const char *uri);
84*36d97821Schristos xml_node_t * get_node(struct xml_node_ctx *ctx, xml_node_t *root,
85*36d97821Schristos 		      const char *path);
86*36d97821Schristos xml_node_t * get_child_node(struct xml_node_ctx *ctx, xml_node_t *root,
87*36d97821Schristos 			    const char *path);
88*36d97821Schristos xml_node_t * node_from_file(struct xml_node_ctx *ctx, const char *name);
89*36d97821Schristos int node_to_file(struct xml_node_ctx *ctx, const char *fname, xml_node_t *node);
90*36d97821Schristos xml_node_t * mo_to_tnds(struct xml_node_ctx *ctx, xml_node_t *mo,
91*36d97821Schristos 			int use_path, const char *urn, const char *ns_uri);
92*36d97821Schristos xml_node_t * tnds_to_mo(struct xml_node_ctx *ctx, xml_node_t *tnds);
93*36d97821Schristos 
94*36d97821Schristos xml_node_t * soap_build_envelope(struct xml_node_ctx *ctx, xml_node_t *node);
95*36d97821Schristos xml_node_t * soap_get_body(struct xml_node_ctx *ctx, xml_node_t *soap);
96*36d97821Schristos 
97*36d97821Schristos #endif /* XML_UTILS_H */
98