jabberd2  2.6.1
Data Structures | Macros | Functions
nad.c File Reference
#include "nad.h"
#include "util.h"

Go to the source code of this file.

Data Structures

struct  build_data
 parse a buffer into a nad More...
 

Macros

#define _nad_ptr_check(func, nad)
 !!! Things to do (after 2.0) More...
 
#define BLOCKSIZE   128
 
#define NAD_SAFE(blocks, size, len)   if((size) > len) len = _nad_realloc((void**)&(blocks),(size));
 this is the safety check used to make sure there's always enough mem More...
 

Functions

static int _nad_realloc (void **oblocks, int len)
 Reallocate the given buffer to make it larger. More...
 
static int _nad_cdata (nad_t nad, const char *cdata, int len)
 internal: append some cdata and return the index to it More...
 
static int _nad_attr (nad_t nad, int elem, int ns, const char *name, const char *val, int vallen)
 internal: create a new attr on any given elem More...
 
nad_t nad_new (void)
 create a new nad More...
 
nad_t nad_copy (nad_t nad)
 copy a nad More...
 
void nad_free (nad_t nad)
 free that nad More...
 
int nad_find_elem (nad_t nad, unsigned int elem, int ns, const char *name, int depth)
 locate the next elem at a given depth with an optional matching name More...
 
int nad_find_attr (nad_t nad, unsigned int elem, int ns, const char *name, const char *val)
 get a matching attr on this elem, both name and optional val More...
 
int nad_find_namespace (nad_t nad, unsigned int elem, const char *uri, const char *prefix)
 get a matching ns on this elem, both uri and optional prefix More...
 
int nad_find_scoped_namespace (nad_t nad, const char *uri, const char *prefix)
 find a namespace in scope More...
 
int nad_find_elem_path (nad_t nad, unsigned int elem, int ns, const char *name)
 find elem using XPath like query name – "name" for the child tag of that name "name/name" for a sub child (recurses) "?attrib" to match the first tag with that attrib defined "?attrib=value" to match the first tag with that attrib and value "!attrib" to match the first tag without that attrib defined "!attrib=value" to match the first tag without that attrib and value or any combination: "name/name/?attrib", etc More...
 
void nad_set_attr (nad_t nad, unsigned int elem, int ns, const char *name, const char *val, int vallen)
 create, update, or zap any matching attr on this elem More...
 
int nad_insert_elem (nad_t nad, unsigned int parent, int ns, const char *name, const char *cdata)
 shove in a new child elem after the given one More...
 
void nad_drop_elem (nad_t nad, unsigned int elem)
 remove an element (and its subelements) More...
 
void nad_wrap_elem (nad_t nad, unsigned int elem, int ns, const char *name)
 wrap an element with another element More...
 
int nad_insert_nad (nad_t dest, int delem, nad_t src, int selem)
 insert part of a nad into another nad More...
 
int nad_append_elem (nad_t nad, int ns, const char *name, int depth)
 create a new elem on the list More...
 
int nad_append_attr (nad_t nad, int ns, const char *name, const char *val)
 attach new attr to the last elem More...
 
void nad_append_cdata (nad_t nad, const char *cdata, int len, int depth)
 append new cdata to the last elem More...
 
int nad_add_namespace (nad_t nad, const char *uri, const char *prefix)
 bring a new namespace into scope More...
 
int nad_append_namespace (nad_t nad, unsigned int elem, const char *uri, const char *prefix)
 declare a namespace on an already-existing element More...
 
static void _nad_escape (nad_t nad, int data, int len, int flag)
 
static int _nad_lp0 (nad_t nad, unsigned int elem)
 internal recursive printing function More...
 
void nad_print (nad_t nad, unsigned int elem, const char **xml, int *len)
 create a string representation of the given element (and children), point references to it More...
 
void nad_serialize (nad_t nad, char **buf, int *len)
 nads serialize to a buffer of this form: More...
 
nad_t nad_deserialize (const char *buf)
 
static void _nad_parse_element_start (void *arg, const char *name, const char **atts)
 
static void _nad_parse_element_end (void *arg, const char *name)
 
static void _nad_parse_cdata (void *arg, const char *str, int len)
 
static void _nad_parse_namespace_start (void *arg, const char *prefix, const char *uri)
 
nad_t nad_parse (const char *buf, int len)
 create a nad from raw xml More...
 

Macro Definition Documentation

◆ _nad_ptr_check

#define _nad_ptr_check (   func,
  nad 
)

!!! Things to do (after 2.0)

  • make nad_find_scoped_namespace() take an element index, and only search the scope on that element (currently, it searchs all elements from end to start, which isn't really correct, though it works in most cases
  • new functions:
    • insert one nad (or part thereof) into another nad
    • clear a part of a nad (like xmlnode_hide)
  • audit use of depth array and parent (see j2 bug #792)

Definition at line 61 of file nad.c.

Referenced by nad_add_namespace(), nad_append_attr(), nad_append_cdata(), nad_append_elem(), nad_append_namespace(), nad_copy(), nad_deserialize(), nad_drop_elem(), nad_find_attr(), nad_find_elem(), nad_find_elem_path(), nad_find_namespace(), nad_find_scoped_namespace(), nad_free(), nad_insert_elem(), nad_insert_nad(), nad_new(), nad_print(), nad_serialize(), nad_set_attr(), and nad_wrap_elem().

◆ BLOCKSIZE

#define BLOCKSIZE   128

Definition at line 64 of file nad.c.

Referenced by _nad_realloc().

◆ NAD_SAFE

#define NAD_SAFE (   blocks,
  size,
  len 
)    if((size) > len) len = _nad_realloc((void**)&(blocks),(size));

this is the safety check used to make sure there's always enough mem

Definition at line 89 of file nad.c.

Referenced by _nad_attr(), _nad_cdata(), _nad_escape(), _nad_lp0(), nad_add_namespace(), nad_append_elem(), nad_append_namespace(), nad_copy(), nad_insert_elem(), nad_insert_nad(), and nad_wrap_elem().

Function Documentation

◆ _nad_realloc()

static int _nad_realloc ( void **  oblocks,
int  len 
)
static

Reallocate the given buffer to make it larger.

Parameters
oblocksA pointer to a buffer that will be made larger.
lenThe minimum size in bytes to make the buffer. The actual size of the buffer will be rounded up to the nearest block of 1024 bytes.
Returns
The new size of the buffer in bytes.

Definition at line 76 of file nad.c.

References BLOCKSIZE.

◆ _nad_cdata()

static int _nad_cdata ( nad_t  nad,
const char *  cdata,
int  len 
)
static

internal: append some cdata and return the index to it

Definition at line 92 of file nad.c.

References nad_st::ccur, nad_st::cdata, nad_st::clen, and NAD_SAFE.

Referenced by _nad_attr(), nad_add_namespace(), nad_append_cdata(), nad_append_elem(), nad_append_namespace(), nad_insert_elem(), nad_insert_nad(), nad_set_attr(), and nad_wrap_elem().

◆ _nad_attr()

static int _nad_attr ( nad_t  nad,
int  elem,
int  ns,
const char *  name,
const char *  val,
int  vallen 
)
static

◆ nad_new()

nad_t nad_new ( void  )

◆ nad_copy()

nad_t nad_copy ( nad_t  nad)

◆ nad_free()

void nad_free ( nad_t  nad)

◆ nad_find_elem()

int nad_find_elem ( nad_t  nad,
unsigned int  elem,
int  ns,
const char *  name,
int  depth 
)

◆ nad_find_attr()

int nad_find_attr ( nad_t  nad,
unsigned int  elem,
int  ns,
const char *  name,
const char *  val 
)

◆ nad_find_namespace()

int nad_find_namespace ( nad_t  nad,
unsigned int  elem,
const char *  uri,
const char *  prefix 
)

◆ nad_find_scoped_namespace()

int nad_find_scoped_namespace ( nad_t  nad,
const char *  uri,
const char *  prefix 
)

◆ nad_find_elem_path()

int nad_find_elem_path ( nad_t  nad,
unsigned int  elem,
int  ns,
const char *  name 
)

find elem using XPath like query name – "name" for the child tag of that name "name/name" for a sub child (recurses) "?attrib" to match the first tag with that attrib defined "?attrib=value" to match the first tag with that attrib and value "!attrib" to match the first tag without that attrib defined "!attrib=value" to match the first tag without that attrib and value or any combination: "name/name/?attrib", etc

find elem using XPath like query name – "name" for the child tag of that name "name/name" for a sub child (recurses) "?attrib" to match the first tag with that attrib defined "?attrib=value" to match the first tag with that attrib and value or any combination: "name/name/?attrib", etc

Definition at line 322 of file nad.c.

References _nad_ptr_check, nad_st::ecur, nad_find_attr(), nad_find_elem(), nad_find_elem_path(), and nad_find_namespace().

Referenced by filter_packet(), and nad_find_elem_path().

◆ nad_set_attr()

void nad_set_attr ( nad_t  nad,
unsigned int  elem,
int  ns,
const char *  name,
const char *  val,
int  vallen 
)

◆ nad_insert_elem()

int nad_insert_elem ( nad_t  nad,
unsigned int  parent,
int  ns,
const char *  name,
const char *  cdata 
)

◆ nad_drop_elem()

void nad_drop_elem ( nad_t  nad,
unsigned int  elem 
)

remove an element (and its subelements)

Definition at line 484 of file nad.c.

References _nad_ptr_check, nad_elem_st::depth, nad_st::ecur, nad_st::elems, and nad_elem_st::parent.

Referenced by _announce_pkt_sm(), _help_pkt_sm(), _iq_ping_reply(), _roster_in_sess(), and _session_in_router().

◆ nad_wrap_elem()

void nad_wrap_elem ( nad_t  nad,
unsigned int  elem,
int  ns,
const char *  name 
)

◆ nad_insert_nad()

int nad_insert_nad ( nad_t  dest,
int  delem,
nad_t  src,
int  selem 
)

◆ nad_append_elem()

int nad_append_elem ( nad_t  nad,
int  ns,
const char *  name,
int  depth 
)

create a new elem on the list

append and return a new element

Definition at line 711 of file nad.c.

References _nad_cdata(), _nad_ptr_check, nad_elem_st::attr, nad_elem_st::depth, nad_st::depths, nad_st::dlen, nad_st::ecur, nad_st::elems, nad_st::elen, nad_elem_st::icdata, nad_elem_st::iname, nad_elem_st::itail, nad_elem_st::lcdata, nad_elem_st::lname, nad_elem_st::ltail, nad_elem_st::my_ns, NAD_SAFE, nad_elem_st::ns, nad_elem_st::parent, and nad_st::scope.

Referenced by _address_features(), _amp_pkt_sm(), _authreg_auth_get(), _authreg_auth_set(), _authreg_register_get(), _authreg_register_set(), _bind_features(), _c2s_client_sx_callback(), _config_startElement(), _disco_agents_result(), _disco_info_result(), _disco_items_result(), _disco_pkt_router(), _disco_pkt_sm(), _disco_sessions_result(), _disco_user_result(), _help_disco_extend(), _in_result(), _iq_vcard_to_pkt(), _iq_version_disco_extend(), _nad_parse_element_start(), _offline_pkt_user(), _out_dialback(), _out_verify(), _pbx_presence_nad(), _pep_out_sess(), _roster_in_sess_s10n(), _roster_pkt_user(), _roster_set_item(), _roster_update_walker(), _router_advertise(), _router_advertise_reverse(), _s2s_db_features(), _sm_build_route(), _sx_ack_features(), _sx_compress_features(), _sx_element_start(), _sx_sasl_abort(), _sx_sasl_challenge(), _sx_sasl_failure(), _sx_sasl_features(), _sx_sasl_response(), _sx_sasl_success(), _sx_server_notify_header(), _sx_ssl_features(), amp_build_response_pkt(), c2s_router_sx_callback(), pkt_create(), s2s_router_sx_callback(), sm_c2s_action(), sm_sx_callback(), and sx_sasl_auth().

◆ nad_append_attr()

int nad_append_attr ( nad_t  nad,
int  ns,
const char *  name,
const char *  val 
)

◆ nad_append_cdata()

void nad_append_cdata ( nad_t  nad,
const char *  cdata,
int  len,
int  depth 
)

◆ nad_add_namespace()

int nad_add_namespace ( nad_t  nad,
const char *  uri,
const char *  prefix 
)

bring a new namespace into scope

add a namespace to the next element (ie, called when the namespace comes into scope)

Definition at line 778 of file nad.c.

References _nad_cdata(), _nad_ptr_check, nad_ns_st::iprefix, nad_ns_st::iuri, nad_ns_st::lprefix, nad_ns_st::luri, nad_find_scoped_namespace(), NAD_SAFE, nad_st::ncur, nad_ns_st::next, nad_st::nlen, nad_st::nss, and nad_st::scope.

Referenced by _address_features(), _amp_pkt_sm(), _authreg_auth_get(), _authreg_auth_set(), _authreg_register_get(), _authreg_register_set(), _bind_features(), _c2s_client_sx_callback(), _disco_agents_result(), _disco_info_result(), _disco_items_result(), _disco_pkt_router(), _disco_pkt_sm(), _disco_sessions_result(), _help_disco_extend(), _in_packet(), _in_result(), _iq_vcard_to_pkt(), _iq_version_disco_extend(), _nad_parse_element_start(), _nad_parse_namespace_start(), _offline_pkt_user(), _out_dialback(), _out_verify(), _pbx_presence_nad(), _privacy_in_sess(), _privacy_out_router(), _roster_in_sess_s10n(), _roster_insert_item(), _roster_pkt_user(), _roster_set_item(), _roster_update_walker(), _router_advertise(), _router_advertise_reverse(), _router_sx_callback(), _s2s_db_features(), _sm_build_route(), _sx_compress_features(), _sx_element_start(), _sx_namespace_start(), _sx_sasl_abort(), _sx_sasl_challenge(), _sx_sasl_failure(), _sx_sasl_features(), _sx_sasl_response(), _sx_sasl_success(), _sx_server_notify_header(), _sx_ssl_features(), amp_build_response_pkt(), c2s_router_sx_callback(), nad_insert_nad(), pkt_create(), pkt_delay(), s2s_router_sx_callback(), sm_c2s_action(), sm_packet(), sm_sx_callback(), stanza_error(), and sx_sasl_auth().

◆ nad_append_namespace()

int nad_append_namespace ( nad_t  nad,
unsigned int  elem,
const char *  uri,
const char *  prefix 
)

declare a namespace on an already-existing element

declare a namespace on an already existing element

Definition at line 814 of file nad.c.

References _nad_cdata(), _nad_ptr_check, nad_st::elems, nad_ns_st::iprefix, nad_ns_st::iuri, nad_ns_st::lprefix, nad_ns_st::luri, nad_find_namespace(), NAD_SAFE, nad_st::ncur, nad_ns_st::next, nad_st::nlen, nad_elem_st::ns, and nad_st::nss.

Referenced by _nad_parse_element_start(), _sx_element_start(), sess_route(), and sm_packet().

◆ _nad_escape()

static void _nad_escape ( nad_t  nad,
int  data,
int  len,
int  flag 
)
static

Definition at line 848 of file nad.c.

References nad_st::ccur, nad_st::cdata, nad_st::clen, and NAD_SAFE.

Referenced by _nad_lp0().

◆ _nad_lp0()

static int _nad_lp0 ( nad_t  nad,
unsigned int  elem 
)
static

◆ nad_print()

void nad_print ( nad_t  nad,
unsigned int  elem,
const char **  xml,
int *  len 
)

create a string representation of the given element (and children), point references to it

Definition at line 1208 of file nad.c.

References _nad_lp0(), _nad_ptr_check, nad_st::ccur, and nad_st::cdata.

Referenced by _help_pkt_sm(), _router_process_route(), _sx_nad_write(), _sx_process_read(), _sx_server_notify_header(), c2s_router_sx_callback(), and filter_packet().

◆ nad_serialize()

void nad_serialize ( nad_t  nad,
char **  buf,
int *  len 
)

nads serialize to a buffer of this form:

serialize and deserialize a nad

[buflen][ecur][acur][ncur][ccur][elems][attrs][nss][cdata]

nothing is done with endianness or word length, so the nad must be serialized and deserialized on the same platform

buflen is not actually used by deserialize(), but is provided as a convenience to the application so it knows how many bytes to read before passing them in to deserialize()

the depths array is not stored, so after deserialization nad_append_elem() and nad_append_cdata() will not work. this is rarely a problem

Definition at line 1236 of file nad.c.

References _nad_ptr_check, nad_st::acur, nad_st::attrs, nad_st::ccur, nad_st::cdata, nad_st::ecur, nad_st::elems, nad_st::ncur, and nad_st::nss.

◆ nad_deserialize()

nad_t nad_deserialize ( const char *  buf)

◆ _nad_parse_element_start()

static void _nad_parse_element_start ( void *  arg,
const char *  name,
const char **  atts 
)
static

◆ _nad_parse_element_end()

static void _nad_parse_element_end ( void *  arg,
const char *  name 
)
static

Definition at line 1393 of file nad.c.

References build_data::depth.

Referenced by nad_parse().

◆ _nad_parse_cdata()

static void _nad_parse_cdata ( void *  arg,
const char *  str,
int  len 
)
static

Definition at line 1399 of file nad.c.

References build_data::depth, build_data::nad, and nad_append_cdata().

Referenced by nad_parse().

◆ _nad_parse_namespace_start()

static void _nad_parse_namespace_start ( void *  arg,
const char *  prefix,
const char *  uri 
)
static

Definition at line 1406 of file nad.c.

References build_data::nad, nad_add_namespace(), build_data::p, and nad_st::scope.

Referenced by nad_parse().

◆ nad_parse()

nad_t nad_parse ( const char *  buf,
int  len 
)