yumapro  21.10T-12
YumaPro SDK
xml_util.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 - 2012, Andy Bierman, All Rights Reserved.
3  * Copyright (c) 2012 - 2021, YumaWorks, Inc., All Rights Reserved.
4  *
5  * Unless required by applicable law or agreed to in writing,
6  * software distributed under the License is distributed on an
7  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations
10  * under the License.
11  */
12 #ifndef _H_xml_util
13 #define _H_xml_util
14 /* FILE: xml_util.h
15 *********************************************************************
16 * *
17 * P U R P O S E *
18 * *
19 *********************************************************************/
20 
26 /*********************************************************************
27 * *
28 * C H A N G E H I S T O R Y *
29 * *
30 *********************************************************************
31 
32 date init comment
33 ----------------------------------------------------------------------
34 14-oct-05 abb begun
35 2-jan-06 abb rewrite xml_consume_* API to use simpler
36  xml_node_t
37 */
38 
39 
40 /* From /usr/include/libxml2/libxml/ */
41 #include <xmlreader.h>
42 #include <xmlstring.h>
43 
44 #ifndef _H_ncxconst
45 #include "ncxconst.h"
46 #endif
47 
48 #ifndef _H_xmlns
49 #include "xmlns.h"
50 #endif
51 
52 #ifndef _H_log
53 #include "log.h"
54 #endif
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 /********************************************************************
61 * *
62 * C O N S T A N T S *
63 * *
64 *********************************************************************/
65 
66 #define MAX_CHAR_ENT 8
67 
68 #define XML_START_MSG ((const xmlChar *)\
69  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
70 
71 #define XML_START_MSG_SIZE 38
72 
73 
74 /* XML_PARSE_NOCDATA option merge CDATA as text nodes, used to
75  * parse Confd get-schema reply properly
76  */
77 #define XML_READER_OPTIONS \
78  (XML_PARSE_RECOVER+XML_PARSE_NOERROR+ \
79  XML_PARSE_NOWARNING+XML_PARSE_NOBLANKS+XML_PARSE_NONET+ \
80  XML_PARSE_XINCLUDE+XML_PARSE_NOCDATA)
81 
82 #define XML_SES_URL "netconf://pdu"
83 
84 /********************************************************************
85 * *
86 * T Y P E S *
87 * *
88 *********************************************************************/
89 
152 typedef dlq_hdr_t xml_attrs_t;
153 
154 
156 typedef struct xml_attr_t_ {
158  dlq_hdr_t attr_qhdr;
159 
162 
165 
167  const xmlChar *attr_qname;
168 
170  const xmlChar *attr_name;
171 
175  xmlChar *attr_dname;
176 
178  xmlChar *attr_val;
179 
185  struct xpath_pcb_t_ *attr_xpcb;
186 } xml_attr_t;
187 
188 
190 typedef enum xml_nodetyp_t_ {
196 } xml_nodetyp_t;
197 
198 
204 typedef struct xml_node_t_ {
206  xml_nodetyp_t nodetyp;
207 
210 
213 
215  const xmlChar *module;
216 
218  const xmlChar *badns;
219 
221  xmlChar *qname;
222 
224  const xmlChar *elname;
225 
227  const xmlChar *simval;
228 
230  uint32 simlen;
231 
233  xmlChar *simfree;
234 
236  int depth;
237 
239  xml_attrs_t attrs;
240 } xml_node_t;
241 
242 
243 /********************************************************************
244 * *
245 * F U N C T I O N S *
246 * *
247 *********************************************************************/
248 
249 
255 extern xml_node_t *
256  xml_new_node (void);
257 
258 
264 extern void
265  xml_init_node (xml_node_t *node);
266 
267 
273 extern void
274  xml_free_node (xml_node_t *node);
275 
276 
282 extern void
283  xml_clean_node (xml_node_t *node);
284 
285 
294 extern status_t
295  xml_get_reader_from_buff (const xmlChar *buff,
296  xmlTextReaderPtr *reader);
297 
298 
308 extern status_t
309  xml_get_reader_from_filespec (const char *filespec,
310  xmlTextReaderPtr *reader);
311 
312 
324 extern status_t
325  xml_get_reader_for_session (xmlInputReadCallback readfn,
326  xmlInputCloseCallback closefn,
327  void *context,
328  xmlTextReaderPtr *reader);
329 
330 
342 extern status_t
343  xml_reset_reader_for_session (xmlInputReadCallback readfn,
344  xmlInputCloseCallback closefn,
345  void *context,
346  xmlTextReaderPtr reader);
347 
348 
354 extern void
355  xml_free_reader (xmlTextReaderPtr reader);
356 
357 
365 extern const char *
366  xml_get_node_name (int nodeval);
367 
368 
376 extern boolean
377  xml_advance_reader (xmlTextReaderPtr reader);
378 
379 
389 extern status_t
390  xml_node_match (const xml_node_t *node,
391  xmlns_id_t nsid,
392  const xmlChar *elname,
393  xml_nodetyp_t nodetyp);
394 
395 
404 extern status_t
405  xml_endnode_match (const xml_node_t *startnode,
406  const xml_node_t *endnode);
407 
408 
417 extern boolean
418  xml_docdone (xmlTextReaderPtr reader);
419 
420 
427 extern void
428  xml_dump_node (const xml_node_t *node,
429  log_debug_t lvl);
430 
431 
437 extern void
438  xml_init_attrs (xml_attrs_t *attrs);
439 
440 
446 extern xml_attr_t *
447  xml_new_attr (void);
448 
449 
455 extern void
456  xml_free_attr (xml_attr_t *attr);
457 
458 
468 extern status_t
469  xml_add_attr (xml_attrs_t *attrs,
470  xmlns_id_t ns_id,
471  const xmlChar *attr_name,
472  const xmlChar *attr_val);
473 
474 
488 extern xml_attr_t *
489  xml_add_qattr (xml_attrs_t *attrs,
490  xmlns_id_t ns_id,
491  const xmlChar *attr_qname,
492  uint32 plen,
493  const xmlChar *attr_val,
494  status_t *res);
495 
496 
506 extern status_t
507  xml_add_xmlns_attr (xml_attrs_t *attrs,
508  xmlns_id_t ns_id,
509  const xmlChar *pfix);
510 
511 
521 extern status_t
522  xml_add_xmlns_attr_string (xml_attrs_t *attrs,
523  const xmlChar *ns,
524  const xmlChar *pfix);
525 
526 
539 extern status_t
540  xml_add_inv_xmlns_attr (xml_attrs_t *attrs,
541  xmlns_id_t ns_id,
542  const xmlChar *pfix,
543  const xmlChar *nsval);
544 
545 
552 extern xml_attr_t *
553  xml_first_attr (xml_attrs_t *attrs);
554 
555 
562 extern xml_attr_t *
563  xml_get_first_attr (const xml_node_t *node);
564 
565 
572 extern xml_attr_t *
573  xml_next_attr (xml_attr_t *attr);
574 
575 
581 extern void
582  xml_clean_attrs (xml_attrs_t *attrs);
583 
584 
591 extern void
592  xml_move_attrs (xml_attrs_t *src_attrs,
593  xml_attrs_t *dest_attrs);
594 
595 
596 
608 extern xml_attr_t *
609  xml_find_attr (xml_node_t *node,
610  xmlns_id_t nsid,
611  const xmlChar *attrname);
612 
613 
625 extern xml_attr_t *
626  xml_find_attr_q (xml_attrs_t *attrs,
627  xmlns_id_t nsid,
628  const xmlChar *attrname);
629 
630 
642 extern const xml_attr_t *
643  xml_find_ro_attr (const xml_node_t *node,
644  xmlns_id_t nsid,
645  const xmlChar *attrname);
646 
647 
656 extern uint32
657  xml_strlen (const xmlChar *str);
658 
659 
671 extern uint32
672  xml_strlen_sp (const xmlChar *str,
673  boolean *sp);
674 
675 
686 extern uint32
687  xml_strcpy (xmlChar *copyTo,
688  const xmlChar *copyFrom);
689 
690 
700 extern uint32
701  xml_strncpy (xmlChar *copyTo,
702  const xmlChar *copyFrom,
703  uint32 maxlen);
704 
705 
712 extern xmlChar *
713  xml_strdup (const xmlChar *copyFrom);
714 
715 
723 extern xmlChar *
724  xml_strcat (xmlChar *appendTo,
725  const xmlChar *appendFrom);
726 
727 
736 extern xmlChar *
737  xml_strncat (xmlChar *appendTo,
738  const xmlChar *appendFrom,
739  uint32 maxlen);
740 
741 
749 extern xmlChar *
750  xml_strndup (const xmlChar *copyFrom,
751  uint32 maxlen);
752 
753 
761 extern char *
762  xml_ch_strndup (const char *copyFrom,
763  uint32 maxlen);
764 
765 
779 extern int
780  xml_strcmp (const xmlChar *s1,
781  const xmlChar *s2);
782 
783 
797 extern int
798  xml_stricmp (const xmlChar *s1,
799  const xmlChar *s2);
800 
801 
815 extern int
816  xml_strncmp (const xmlChar *s1,
817  const xmlChar *s2,
818  uint32 maxlen);
819 
820 
835 extern int
836  xml_strnicmp (const xmlChar *s1,
837  const xmlChar *s2,
838  uint32 maxlen);
839 
840 
847 extern boolean
848  xml_isspace (uint32 ch);
849 
850 
857 extern boolean
858  xml_isspace_str (const xmlChar *str);
859 
860 
878 extern int
879  xml_strcmp_nosp (const xmlChar *s1,
880  const xmlChar *s2);
881 
882 
903 int
904  xml_strcmp_nosp2 (const xmlChar *s1,
905  const xmlChar *s2);
906 
907 
919 extern xmlChar *
920  xml_copy_clean_string (const xmlChar *str);
921 
922 
931 extern xmlChar
932  xml_convert_char_entity (const xmlChar *str,
933  uint32 *used);
934 
935 
955 extern status_t
956  xml_check_ns (xmlTextReaderPtr reader,
957  const xmlChar *elname,
958  xmlns_id_t *id,
959  uint32 *pfix_len,
960  const xmlChar **badns);
961 
962 
972 extern void
973  xml_check_qname_content (xmlTextReaderPtr reader,
974  xml_node_t *node);
975 
976 
992 extern status_t
993  xml_get_namespace_id (xmlTextReaderPtr reader,
994  const xmlChar *prefix,
995  uint32 prefixlen,
996  boolean register_missing,
997  xmlns_id_t *retnsid);
998 
999 
1014 extern status_t
1015  xml_consume_node (xmlTextReaderPtr reader,
1016  xml_node_t *xmlnode,
1017  boolean nserr,
1018  boolean adv);
1019 
1020 
1033 extern xmlChar * xml_trim_string (xmlChar *str);
1034 
1035 
1039 #ifdef __cplusplus
1040 } /* end extern 'C' */
1041 #endif
1042 
1043 #endif /* _H_xml_util */
boolean xml_advance_reader(xmlTextReaderPtr reader)
Advance to the next node in the specified reader.
Definition: xml_util.c:517
void xml_init_node(xml_node_t *node)
Init an xml_node_t struct.
Definition: xml_util.c:229
xml_attr_t * xml_find_attr_q(xml_attrs_t *attrs, xmlns_id_t nsid, const xmlChar *attrname)
Find an attribute in a Q.
Definition: xml_util.c:1324
void xml_dump_node(const xml_node_t *node, log_debug_t lvl)
Debug function to printf xml_node_t contents.
Definition: xml_util.c:661
gather node data into a simple struct.
Definition: xml_util.h:204
status_t xml_endnode_match(const xml_node_t *startnode, const xml_node_t *endnode)
check if a specific node is the proper endnode match for a given startnode
Definition: xml_util.c:592
status_t xml_get_reader_for_session(xmlInputReadCallback readfn, xmlInputCloseCallback closefn, void *context, xmlTextReaderPtr *reader)
Get a new xmlTextReader for parsing the input of a NETCONF session.
Definition: xml_util.c:378
uint32 xml_strcpy(xmlChar *copyTo, const xmlChar *copyFrom)
String copy for xmlChar.
Definition: xml_util.c:1486
log_debug_t
The debug level enumerations used in util/log.c.
Definition: log.h:386
xmlChar * xml_strcat(xmlChar *appendTo, const xmlChar *appendFrom)
String concatenate for xmlChar.
Definition: xml_util.c:1597
status_t xml_node_match(const xml_node_t *node, xmlns_id_t nsid, const xmlChar *elname, xml_nodetyp_t nodetyp)
check if a specific node is the proper owner, name, and type
Definition: xml_util.c:545
xmlChar * qname
qualified name of element
Definition: xml_util.h:221
end-tag of an element
Definition: xml_util.h:194
int xml_strcmp_nosp(const xmlChar *s1, const xmlChar *s2)
String compare for xmlChar for 2 strings, but ignoring whitespace differences.
Definition: xml_util.c:1964
string content node
Definition: xml_util.h:195
xml_nodetyp_t
only 4 types of nodes returned
Definition: xml_util.h:190
uint32 xml_strlen(const xmlChar *str)
Get the String len for xmlChar.
Definition: xml_util.c:1406
status_t xml_reset_reader_for_session(xmlInputReadCallback readfn, xmlInputCloseCallback closefn, void *context, xmlTextReaderPtr reader)
Reset the xmlTextReader for parsing the input of a NETCONF session.
Definition: xml_util.c:418
xml_attr_t * xml_get_first_attr(const xml_node_t *node)
get the first attribute in the attrs list, from an xml_node_t param
Definition: xml_util.c:1192
boolean xml_isspace_str(const xmlChar *str)
Check if an xmlChar string is all whitespace chars.
Definition: xml_util.c:1923
void xml_check_qname_content(xmlTextReaderPtr reader, xml_node_t *node)
Check if the string node content is a likely QName.
Definition: xml_util.c:2307
const xmlChar * attr_name
attribute name without any prefix
Definition: xml_util.h:170
status_t xml_get_reader_from_filespec(const char *filespec, xmlTextReaderPtr *reader)
Get a new xmlTextReader for parsing a debug test file.
Definition: xml_util.c:341
boolean xml_isspace(uint32 ch)
Check if an xmlChar is a space char.
Definition: xml_util.c:1901
status_t xml_get_namespace_id(xmlTextReaderPtr reader, const xmlChar *prefix, uint32 prefixlen, boolean register_missing, xmlns_id_t *retnsid)
Get the namespace for the specified prefix (may be NULL) Use the current XML reader context to resolv...
Definition: xml_util.c:2360
const xmlChar * badns
set if nsid == xmlns_inv_id()
Definition: xml_util.h:218
xml_attr_t * xml_find_attr(xml_node_t *node, xmlns_id_t nsid, const xmlChar *attrname)
Find an attribute.
Definition: xml_util.c:1295
xmlChar * xml_copy_clean_string(const xmlChar *str)
Get a malloced string contained the converted string from the input.
Definition: xml_util.c:2119
status_t xml_add_inv_xmlns_attr(xml_attrs_t *attrs, xmlns_id_t ns_id, const xmlChar *pfix, const xmlChar *nsval)
add an xmlns decl to the attribute Queue for an INVALID namespace.
Definition: xml_util.c:1090
uint16 xmlns_id_t
integer handle for registered namespaces
Definition: xmlns.h:89
Contains NCX constants.
int xml_strnicmp(const xmlChar *s1, const xmlChar *s2, uint32 maxlen)
Case insensitive string compare for xmlChar for at most &#39;maxlen&#39; xmlChars.
Definition: xml_util.c:1865
xmlns_id_t attr_ns
attribute namespace ID
Definition: xml_util.h:161
xmlns_id_t contentnsid
namespace of content
Definition: xml_util.h:212
xmlns_id_t attr_xmlns_ns
if xmlns, then namespace ID getting assigned
Definition: xml_util.h:164
status_t xml_check_ns(xmlTextReaderPtr reader, const xmlChar *elname, xmlns_id_t *id, uint32 *pfix_len, const xmlChar **badns)
Check a namespace during parsing.
Definition: xml_util.c:2248
xmlChar * xml_strncat(xmlChar *appendTo, const xmlChar *appendFrom, uint32 maxlen)
String concatenate for at most maxlen xmlChars.
Definition: xml_util.c:1630
void xml_clean_attrs(xml_attrs_t *attrs)
clean an xml_attrs_t variable
Definition: xml_util.c:1243
int xml_stricmp(const xmlChar *s1, const xmlChar *s2)
Case insensitive string compare for xmlChar.
Definition: xml_util.c:1785
xml_attr_t * xml_next_attr(xml_attr_t *attr)
get the next attribute in the list
Definition: xml_util.c:1218
status_t xml_get_reader_from_buff(const xmlChar *buff, xmlTextReaderPtr *reader)
Get a new xmlTextReader for parsing a debug test file.
Definition: xml_util.c:305
represents one attribute
Definition: xml_util.h:156
void xml_move_attrs(xml_attrs_t *src_attrs, xml_attrs_t *dest_attrs)
Move all the attrs from the src to the dest.
Definition: xml_util.c:1272
status_t xml_consume_node(xmlTextReaderPtr reader, xml_node_t *xmlnode, boolean nserr, boolean adv)
parse function for YIN input
Definition: xml_util.c:2447
standalone empty node
Definition: xml_util.h:192
not set
Definition: xml_util.h:191
const xml_attr_t * xml_find_ro_attr(const xml_node_t *node, xmlns_id_t nsid, const xmlChar *attrname)
Find a read-only attribute.
Definition: xml_util.c:1367
void xml_free_node(xml_node_t *node)
Free an xml_node_t struct.
Definition: xml_util.c:246
xml_nodetyp_t nodetyp
XML tag type.
Definition: xml_util.h:206
uint32 xml_strncpy(xmlChar *copyTo, const xmlChar *copyFrom, uint32 maxlen)
String copy for xmlChar – checks for buffer overflow.
Definition: xml_util.c:1518
status_t xml_add_xmlns_attr(xml_attrs_t *attrs, xmlns_id_t ns_id, const xmlChar *pfix)
add an xmlns decl to the attribute Queue
Definition: xml_util.c:928
xmlChar * simfree
non-NULL if simval is freed
Definition: xml_util.h:233
xmlChar * xml_strdup(const xmlChar *copyFrom)
String duplicate for xmlChar.
Definition: xml_util.c:1553
xml_attr_t * xml_new_attr(void)
malloc and init an attribute struct
Definition: xml_util.c:764
void xml_clean_node(xml_node_t *node)
Clean an xml_node_t struct.
Definition: xml_util.c:266
xmlChar xml_convert_char_entity(const xmlChar *str, uint32 *used)
Convert an XML character entity into a single xmlChar.
Definition: xml_util.c:2185
int xml_strcmp(const xmlChar *s1, const xmlChar *s2)
String compare for xmlChar.
Definition: xml_util.c:1746
status_t
global error return code
Definition: status_enum.h:186
int xml_strcmp_nosp2(const xmlChar *s1, const xmlChar *s2)
String compare for xmlChar for 2 strings, but completely ignoring whitespace differences.
Definition: xml_util.c:2043
const xmlChar * module
YANG module found for this node.
Definition: xml_util.h:215
xmlChar * attr_val
attribute value string
Definition: xml_util.h:178
status_t xml_add_xmlns_attr_string(xml_attrs_t *attrs, const xmlChar *ns, const xmlChar *pfix)
add an xmlns decl to the attribute Queue
Definition: xml_util.c:1011
xmlChar * xml_trim_string(xmlChar *str)
trim leading and trailing whitespace inline in the same buffer
Definition: xml_util.c:2646
struct xpath_pcb_t_ * attr_xpcb
XPath PCB in case XPath was parsed.
Definition: xml_util.h:185
xml_node_t * xml_new_node(void)
Malloc and init a new xml_node_t struct.
Definition: xml_util.c:206
boolean xml_docdone(xmlTextReaderPtr reader)
check if the input is completed for a given PDU
Definition: xml_util.c:636
int depth
XML node depth.
Definition: xml_util.h:236
xml_attrs_t attrs
attributes parsed for this node if XML_NT_EMPTY or XML_NT_START
Definition: xml_util.h:239
uint32 simlen
length of the simple value string
Definition: xml_util.h:230
start-tag of an element
Definition: xml_util.h:193
void xml_free_reader(xmlTextReaderPtr reader)
Free the previously allocated xmlTextReader.
Definition: xml_util.c:453
xml_attr_t * xml_add_qattr(xml_attrs_t *attrs, xmlns_id_t ns_id, const xmlChar *attr_qname, uint32 plen, const xmlChar *attr_val, status_t *res)
add a qualified attribute to an attribute list with a prefix
Definition: xml_util.c:872
const char * xml_get_node_name(int nodeval)
get the node type according to the xmlElementType enum list in /usr/include/libxml/libxml/tree.h
Definition: xml_util.c:475
status_t xml_add_attr(xml_attrs_t *attrs, xmlns_id_t ns_id, const xmlChar *attr_name, const xmlChar *attr_val)
add an attribute to an attribute list
Definition: xml_util.c:820
const xmlChar * attr_qname
attribute qname with prefix (if any)
Definition: xml_util.h:167
xml_attr_t * xml_first_attr(xml_attrs_t *attrs)
get the first attribute in the list
Definition: xml_util.c:1167
void xml_free_attr(xml_attr_t *attr)
free an attribute
Definition: xml_util.c:785
char * xml_ch_strndup(const char *copyFrom, uint32 maxlen)
String duplicate for max N chars.
Definition: xml_util.c:1703
xmlChar * attr_dname
malloced attribute name full qualified name if any
Definition: xml_util.h:175
const xmlChar * simval
simple value if this is an XML_NT_STRING node
Definition: xml_util.h:227
xmlChar * xml_strndup(const xmlChar *copyFrom, uint32 maxlen)
String duplicate for max N xmlChars.
Definition: xml_util.c:1663
void xml_init_attrs(xml_attrs_t *attrs)
initialize an xml_attrs_t variable
Definition: xml_util.c:745
const xmlChar * elname
element name without any prefix
Definition: xml_util.h:224
XML namespace support.
NCX System Logging Manager.
dlq_hdr_t xml_attrs_t
queue of xml_attr_t
Definition: xml_util.h:152
uint32 xml_strlen_sp(const xmlChar *str, boolean *sp)
get length and check if any whitespace at the same time
Definition: xml_util.c:1443
xmlns_id_t nsid
namespace ID
Definition: xml_util.h:209
int xml_strncmp(const xmlChar *s1, const xmlChar *s2, uint32 maxlen)
String compare for xmlChar for at most &#39;maxlen&#39; xmlChars.
Definition: xml_util.c:1824