jabberd2  2.3.4
nad.c
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
4  * Ryan Eatmon, Robert Norris
5  *
6  * This program 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  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19  */
20 
35 #include "nad.h"
36 #include "util.h"
37 
38 /* define NAD_DEBUG to get pointer tracking - great for weird bugs that you can't reproduce */
39 #ifdef NAD_DEBUG
40 
41 static xht _nad_alloc_tracked = NULL;
42 static xht _nad_free_tracked = NULL;
43 
44 static void _nad_ptr_check(const char *func, nad_t nad) {
45  char loc[24];
46  snprintf(loc, sizeof(loc), "%x", (int) nad);
47 
48  if(xhash_get(_nad_alloc_tracked, loc) == NULL) {
49  fprintf(stderr, ">>> NAD OP %s: 0x%x not allocated!\n", func, (int) nad);
50  abort();
51  }
52 
53  if(xhash_get(_nad_free_tracked, loc) != NULL) {
54  fprintf(stderr, ">>> NAD OP %s: 0x%x previously freed!\n", func, (int) nad);
55  abort();
56  }
57 
58  fprintf(stderr, ">>> NAD OP %s: 0x%x\n", func, (int) nad);
59 }
60 #else
61 #define _nad_ptr_check(func,nad)
62 #endif
63 
64 #define BLOCKSIZE 128
65 
76 static int _nad_realloc(void **oblocks, int len)
77 {
78  int nlen;
79 
80  /* round up to standard block sizes */
81  nlen = (((len-1)/BLOCKSIZE)+1)*BLOCKSIZE;
82 
83  /* keep trying till we get it */
84  *oblocks = realloc(*oblocks, nlen);
85  return nlen;
86 }
87 
89 #define NAD_SAFE(blocks, size, len) if((size) > len) len = _nad_realloc((void**)&(blocks),(size));
90 
92 static int _nad_cdata(nad_t nad, const char *cdata, int len)
93 {
94  NAD_SAFE(nad->cdata, nad->ccur + len, nad->clen);
95 
96  memcpy(nad->cdata + nad->ccur, cdata, len);
97  nad->ccur += len;
98  return nad->ccur - len;
99 }
100 
102 static int _nad_attr(nad_t nad, int elem, int ns, const char *name, const char *val, int vallen)
103 {
104  int attr;
105 
106  /* make sure there's mem for us */
107  NAD_SAFE(nad->attrs, (nad->acur + 1) * sizeof(struct nad_attr_st), nad->alen);
108 
109  attr = nad->acur;
110  nad->acur++;
111  nad->attrs[attr].next = nad->elems[elem].attr;
112  nad->elems[elem].attr = attr;
113  nad->attrs[attr].lname = strlen(name);
114  nad->attrs[attr].iname = _nad_cdata(nad,name,nad->attrs[attr].lname);
115  if(vallen > 0)
116  nad->attrs[attr].lval = vallen;
117  else
118  nad->attrs[attr].lval = strlen(val);
119  nad->attrs[attr].ival = _nad_cdata(nad,val,nad->attrs[attr].lval);
120  nad->attrs[attr].my_ns = ns;
121 
122  return attr;
123 }
124 
126 {
127  nad_t nad;
128 
129  nad = calloc(1, sizeof(struct nad_st));
130 
131  nad->scope = -1;
132 
133 #ifdef NAD_DEBUG
134  {
135  char loc[24];
136  snprintf(loc, sizeof(loc), "%x", (int) nad);
137  xhash_put(_nad_alloc_tracked, pstrdup(xhash_pool(_nad_alloc_tracked), loc), (void *) 1);
138  }
139  _nad_ptr_check(__func__, nad);
140 #endif
141 
142  return nad;
143 }
144 
146 {
147  nad_t copy;
148 
149  _nad_ptr_check(__func__, nad);
150 
151  if(nad == NULL) return NULL;
152 
153  copy = nad_new();
154 
155  /* if it's not large enough, make bigger */
156  NAD_SAFE(copy->elems, nad->elen, copy->elen);
157  NAD_SAFE(copy->attrs, nad->alen, copy->alen);
158  NAD_SAFE(copy->nss, nad->nlen, copy->nlen);
159  NAD_SAFE(copy->cdata, nad->clen, copy->clen);
160 
161  /* copy all data */
162  memcpy(copy->elems, nad->elems, nad->elen);
163  memcpy(copy->attrs, nad->attrs, nad->alen);
164  memcpy(copy->nss, nad->nss, nad->nlen);
165  memcpy(copy->cdata, nad->cdata, nad->clen);
166 
167  /* sync data */
168  copy->ecur = nad->ecur;
169  copy->acur = nad->acur;
170  copy->ncur = nad->ncur;
171  copy->ccur = nad->ccur;
172 
173  copy->scope = nad->scope;
174 
175  return copy;
176 }
177 
178 void nad_free(nad_t nad)
179 {
180  if(nad == NULL) return;
181 
182 #ifdef NAD_DEBUG
183  _nad_ptr_check(__func__, nad);
184  {
185  char loc[24];
186  snprintf(loc, sizeof(loc), "%x", (int) nad);
187  xhash_zap(_nad_alloc_tracked, loc);
188  xhash_put(_nad_free_tracked, pstrdup(xhash_pool(_nad_free_tracked), loc), (void *) nad);
189  }
190 #endif
191 
192  /* Free nad */
193  free(nad->elems);
194  free(nad->attrs);
195  free(nad->cdata);
196  free(nad->nss);
197  free(nad->depths);
198 #ifndef NAD_DEBUG
199  free(nad);
200 #endif
201 }
202 
204 int nad_find_elem(nad_t nad, int elem, int ns, const char *name, int depth)
205 {
206  int my_ns;
207  int lname = 0;
208 
209  _nad_ptr_check(__func__, nad);
210 
211  /* make sure there are valid args */
212  if(elem >= nad->ecur) return -1;
213 
214  /* set up args for searching */
215  depth = nad->elems[elem].depth + depth;
216  if(name != NULL) lname = strlen(name);
217 
218  /* search */
219  for(elem++;elem < nad->ecur;elem++)
220  {
221  /* if we hit one with a depth less than ours, then we don't have the
222  * same parent anymore, bail */
223  if(nad->elems[elem].depth < depth)
224  return -1;
225 
226  if(nad->elems[elem].depth == depth && (lname <= 0 || (lname == nad->elems[elem].lname && strncmp(name,nad->cdata + nad->elems[elem].iname, lname) == 0)) &&
227  (ns < 0 || ((my_ns = nad->elems[elem].my_ns) >= 0 && NAD_NURI_L(nad, ns) == NAD_NURI_L(nad, my_ns) && strncmp(NAD_NURI(nad, ns), NAD_NURI(nad, my_ns), NAD_NURI_L(nad, ns)) == 0)))
228  return elem;
229  }
230 
231  return -1;
232 }
233 
235 int nad_find_attr(nad_t nad, int elem, int ns, const char *name, const char *val)
236 {
237  int attr, my_ns;
238  int lname, lval = 0;
239 
240  _nad_ptr_check(__func__, nad);
241 
242  /* make sure there are valid args */
243  if(elem >= nad->ecur || name == NULL) return -1;
244 
245  attr = nad->elems[elem].attr;
246  lname = strlen(name);
247  if(val != NULL) lval = strlen(val);
248 
249  while(attr >= 0)
250  {
251  /* hefty, match name and if a val, also match that */
252  if(lname == nad->attrs[attr].lname && strncmp(name,nad->cdata + nad->attrs[attr].iname, lname) == 0 &&
253  (lval <= 0 || (lval == nad->attrs[attr].lval && strncmp(val,nad->cdata + nad->attrs[attr].ival, lval) == 0)) &&
254  (ns < 0 || ((my_ns = nad->attrs[attr].my_ns) >= 0 && NAD_NURI_L(nad, ns) == NAD_NURI_L(nad, my_ns) && strncmp(NAD_NURI(nad, ns), NAD_NURI(nad, my_ns), NAD_NURI_L(nad, ns)) == 0)))
255  return attr;
256  attr = nad->attrs[attr].next;
257  }
258  return -1;
259 }
260 
262 int nad_find_namespace(nad_t nad, int elem, const char *uri, const char *prefix)
263 {
264  int check, ns;
265 
266  _nad_ptr_check(__func__, nad);
267 
268  /* make sure there are valid args */
269  if(elem >= nad->ecur || uri == NULL) return -1;
270 
271  /* work backwards through our parents, looking for our namespace on each one.
272  * if we find it, link it. if not, the namespace is undeclared - for now, just drop it */
273  check = elem;
274  while(check >= 0)
275  {
276  ns = nad->elems[check].ns;
277  while(ns >= 0)
278  {
279  if(strlen(uri) == NAD_NURI_L(nad, ns) && strncmp(uri, NAD_NURI(nad, ns), NAD_NURI_L(nad, ns)) == 0 && (prefix == NULL || (nad->nss[ns].iprefix >= 0 && strlen(prefix) == NAD_NPREFIX_L(nad, ns) && strncmp(prefix, NAD_NPREFIX(nad, ns), NAD_NPREFIX_L(nad, ns)) == 0)))
280  return ns;
281  ns = nad->nss[ns].next;
282  }
283  check = nad->elems[check].parent;
284  }
285 
286  return -1;
287 }
288 
290 int nad_find_scoped_namespace(nad_t nad, const char *uri, const char *prefix)
291 {
292  int ns;
293 
294  _nad_ptr_check(__func__, nad);
295 
296  if(uri == NULL)
297  return -1;
298 
299  for(ns = 0; ns < nad->ncur; ns++)
300  {
301  if(strlen(uri) == NAD_NURI_L(nad, ns) && strncmp(uri, NAD_NURI(nad, ns), NAD_NURI_L(nad, ns)) == 0 &&
302  (prefix == NULL ||
303  (nad->nss[ns].iprefix >= 0 &&
304  strlen(prefix) == NAD_NPREFIX_L(nad, ns) && strncmp(prefix, NAD_NPREFIX(nad, ns), NAD_NPREFIX_L(nad, ns)) == 0)))
305  return ns;
306  }
307 
308  return -1;
309 }
310 
318 int nad_find_elem_path(nad_t nad, int elem, int ns, const char *name) {
319  char *str, *slash, *qmark, *equals;
320 
321  _nad_ptr_check(__func__, nad);
322 
323  /* make sure there are valid args */
324  if(elem >= nad->ecur || name == NULL) return -1;
325 
326  /* if it's plain name just search children */
327  if(strstr(name, "/") == NULL && strstr(name,"?") == NULL)
328  return nad_find_elem(nad, elem, ns, name, 1);
329 
330  str = strdup(name);
331  slash = strstr(str, "/");
332  qmark = strstr(str, "?");
333  equals = strstr(str, "=");
334 
335  /* no / in element name part */
336  if(qmark != NULL && (slash == NULL || qmark < slash))
337  { /* of type ?attrib */
338 
339  *qmark = '\0';
340  qmark++;
341  if(equals != NULL)
342  {
343  *equals = '\0';
344  equals++;
345  }
346 
347  for(elem = nad_find_elem(nad, elem, ns, str, 1); ; elem = nad_find_elem(nad, elem, ns, str, 0)) {
348  if(elem < 0) break;
349  if(strcmp(qmark, "xmlns") == 0) {
350  if(nad_find_namespace(nad, elem, equals, NULL) >= 0) break;
351  }
352  else {
353  if(nad_find_attr(nad, elem, ns, qmark, equals) >= 0) break;
354  }
355  }
356 
357  free(str);
358  return elem;
359  }
360 
361  /* there is a / in element name part - need to recurse */
362  *slash = '\0';
363  ++slash;
364 
365  for(elem = nad_find_elem(nad, elem, ns, str, 1); ; elem = nad_find_elem(nad, elem, ns, str, 0)) {
366  if(elem < 0) break;
367  if((elem = nad_find_elem_path(nad, elem, ns, slash)) >= 0) break;
368  }
369 
370  free(str);
371  return elem;
372 }
373 
375 void nad_set_attr(nad_t nad, int elem, int ns, const char *name, const char *val, int vallen)
376 {
377  int attr;
378 
379  _nad_ptr_check(__func__, nad);
380 
381  /* find one to replace first */
382  if((attr = nad_find_attr(nad, elem, ns, name, NULL)) < 0)
383  {
384  /* only create new if there's a value to store */
385  if(val != NULL)
386  _nad_attr(nad, elem, ns, name, val, vallen);
387  return;
388  }
389 
390  /* got matching, update value or zap */
391  if(val == NULL)
392  {
393  nad->attrs[attr].lval = nad->attrs[attr].lname = 0;
394  }else{
395  if(vallen > 0)
396  nad->attrs[attr].lval = vallen;
397  else
398  nad->attrs[attr].lval = strlen(val);
399  nad->attrs[attr].ival = _nad_cdata(nad,val,nad->attrs[attr].lval);
400  }
401 
402 }
403 
405 int nad_insert_elem(nad_t nad, int parent, int ns, const char *name, const char *cdata)
406 {
407  int elem;
408 
409  if (parent >= nad->ecur) {
410  if (nad->ecur > 0)
411  parent = nad->ecur -1;
412  else
413  parent = 0;
414  }
415 
416  elem = parent + 1;
417 
418  _nad_ptr_check(__func__, nad);
419 
420  NAD_SAFE(nad->elems, (nad->ecur + 1) * sizeof(struct nad_elem_st), nad->elen);
421 
422  /* relocate all the rest of the elems (unless we're at the end already) */
423  if(nad->ecur != elem)
424  memmove(&nad->elems[elem + 1], &nad->elems[elem], (nad->ecur - elem) * sizeof(struct nad_elem_st));
425  nad->ecur++;
426 
427  /* set up req'd parts of new elem */
428  nad->elems[elem].parent = parent;
429  nad->elems[elem].lname = strlen(name);
430  nad->elems[elem].iname = _nad_cdata(nad,name,nad->elems[elem].lname);
431  nad->elems[elem].attr = -1;
432  nad->elems[elem].ns = nad->scope; nad->scope = -1;
433  nad->elems[elem].itail = nad->elems[elem].ltail = 0;
434  nad->elems[elem].my_ns = ns;
435 
436  /* add cdata if given */
437  if(cdata != NULL)
438  {
439  nad->elems[elem].lcdata = strlen(cdata);
440  nad->elems[elem].icdata = _nad_cdata(nad,cdata,nad->elems[elem].lcdata);
441  }else{
442  nad->elems[elem].icdata = nad->elems[elem].lcdata = 0;
443  }
444 
445  /* parent/child */
446  nad->elems[elem].depth = nad->elems[parent].depth + 1;
447 
448  return elem;
449 }
450 
452 void nad_drop_elem(nad_t nad, int elem) {
453  int next, cur;
454 
455  _nad_ptr_check(__func__, nad);
456 
457  if(elem >= nad->ecur) return;
458 
459  /* find the next elem at this depth to move into the space */
460  next = elem + 1;
461  while(next < nad->ecur && nad->elems[next].depth > nad->elems[elem].depth) next++;
462 
463  /* relocate */
464  if(next < nad->ecur)
465  memmove(&nad->elems[elem], &nad->elems[next], (nad->ecur - next) * sizeof(struct nad_elem_st));
466  nad->ecur -= next - elem;
467 
468  /* relink parents */
469  for(cur = elem; cur < nad->ecur; cur++)
470  if(nad->elems[cur].parent > next)
471  nad->elems[cur].parent -= (next - elem);
472 }
473 
475 void nad_wrap_elem(nad_t nad, int elem, int ns, const char *name)
476 {
477  int cur;
478 
479  _nad_ptr_check(__func__, nad);
480 
481  if(elem >= nad->ecur) return;
482 
483  NAD_SAFE(nad->elems, (nad->ecur + 1) * sizeof(struct nad_elem_st), nad->elen);
484 
485  /* relocate all the rest of the elems after us */
486  memmove(&nad->elems[elem + 1], &nad->elems[elem], (nad->ecur - elem) * sizeof(struct nad_elem_st));
487  nad->ecur++;
488 
489  /* relink parents on moved elements */
490  for(cur = elem + 1; cur < nad->ecur; cur++)
491  if(nad->elems[cur].parent > elem + 1)
492  nad->elems[cur].parent++;
493 
494  /* set up req'd parts of new elem */
495  nad->elems[elem].lname = strlen(name);
496  nad->elems[elem].iname = _nad_cdata(nad,name,nad->elems[elem].lname);
497  nad->elems[elem].attr = -1;
498  nad->elems[elem].ns = nad->scope; nad->scope = -1;
499  nad->elems[elem].itail = nad->elems[elem].ltail = 0;
500  nad->elems[elem].icdata = nad->elems[elem].lcdata = 0;
501  nad->elems[elem].my_ns = ns;
502 
503  /* raise the bar on all the children */
504  nad->elems[elem+1].depth++;
505  for(cur = elem + 2; cur < nad->ecur && nad->elems[cur].depth > nad->elems[elem].depth; cur++) nad->elems[cur].depth++;
506 
507  /* hook up the parent */
508  nad->elems[elem].parent = nad->elems[elem + 1].parent;
509 }
510 
512 int nad_insert_nad(nad_t dest, int delem, nad_t src, int selem) {
513  int nelem, first, i, j, ns, nattr, attr;
514  char buri[256], *uri = buri, bprefix[256], *prefix = bprefix;
515 
516  _nad_ptr_check(__func__, dest);
517  _nad_ptr_check(__func__, src);
518 
519  /* can't do anything if these aren't real elems */
520  if(src->ecur <= selem || dest->ecur <= delem)
521  return -1;
522 
523  /* figure out how many elements to copy */
524  nelem = 1;
525  while(selem + nelem < src->ecur && src->elems[selem + nelem].depth > src->elems[selem].depth) nelem++;
526 
527  /* make room */
528  NAD_SAFE(dest->elems, (dest->ecur + nelem) * sizeof(struct nad_elem_st), dest->elen);
529 
530  /* relocate all the elems after us */
531  memmove(&dest->elems[delem + nelem + 1], &dest->elems[delem + 1], (dest->ecur - delem - 1) * sizeof(struct nad_elem_st));
532  dest->ecur += nelem;
533 
534  /* relink parents on moved elements */
535  for(i = delem + nelem; i < dest->ecur; i++)
536  if(dest->elems[i].parent > delem)
537  dest->elems[i].parent += nelem;
538 
539  first = delem + 1;
540 
541  /* copy them in, one at a time */
542  for(i = 0; i < nelem; i++) {
543  /* link the parent */
544  dest->elems[first + i].parent = delem + (src->elems[selem + i].parent - src->elems[selem].parent);
545 
546  /* depth */
547  dest->elems[first + i].depth = dest->elems[delem].depth + (src->elems[selem + i].depth - src->elems[selem].depth) + 1;
548 
549  /* name */
550  dest->elems[first + i].lname = src->elems[selem + i].lname;
551  dest->elems[first + i].iname = _nad_cdata(dest, src->cdata + src->elems[selem + i].iname, src->elems[selem + i].lname);
552 
553  /* cdata */
554  dest->elems[first + i].lcdata = src->elems[selem + i].lcdata;
555  dest->elems[first + i].icdata = _nad_cdata(dest, src->cdata + src->elems[selem + i].icdata, src->elems[selem + i].lcdata);
556  dest->elems[first + i].ltail = src->elems[selem + i].ltail;
557  dest->elems[first + i].itail = _nad_cdata(dest, src->cdata + src->elems[selem + i].itail, src->elems[selem + i].ltail);
558 
559  /* namespaces */
560  dest->elems[first + i].my_ns = dest->elems[first + i].ns = dest->scope = -1;
561 
562  /* first, the element namespace */
563  ns = src->elems[selem + i].my_ns;
564  if(ns >= 0) {
565  for(j = 0; j < dest->ncur; j++)
566  if(NAD_NURI_L(src, ns) == NAD_NURI_L(dest, j) && strncmp(NAD_NURI(src, ns), NAD_NURI(dest, j), NAD_NURI_L(src, ns)) == 0) {
567  dest->elems[first + i].my_ns = j;
568  break;
569  }
570 
571  /* not found, gotta add it */
572  if(j == dest->ncur) {
573  /* make room */
574  /* !!! this can go once we have _ex() functions */
575  if(NAD_NURI_L(src, ns) > 255)
576  uri = (char *) malloc(sizeof(char) * (NAD_NURI_L(src, ns) + 1));
577  if(NAD_NPREFIX_L(src, ns) > 255)
578  prefix = (char *) malloc(sizeof(char) * (NAD_NURI_L(src, ns) + 1));
579 
580  sprintf(uri, "%.*s", NAD_NURI_L(src, ns), NAD_NURI(src, ns));
581 
582  if(NAD_NPREFIX_L(src, ns) > 0) {
583  sprintf(prefix, "%.*s", NAD_NPREFIX_L(src, ns), NAD_NPREFIX(src, ns));
584  dest->elems[first + i].my_ns = nad_add_namespace(dest, uri, prefix);
585  } else
586  dest->elems[first + i].my_ns = nad_add_namespace(dest, uri, NULL);
587 
588  if(uri != buri) free(uri);
589  if(prefix != bprefix) free(prefix);
590  }
591  }
592 
593  /* then, any declared namespaces */
594  for(ns = src->elems[selem + i].ns; ns >= 0; ns = src->nss[ns].next) {
595  for(j = 0; j < dest->ncur; j++)
596  if(NAD_NURI_L(src, ns) == NAD_NURI_L(dest, j) && strncmp(NAD_NURI(src, ns), NAD_NURI(dest, j), NAD_NURI_L(src, ns)) == 0)
597  break;
598 
599  /* not found, gotta add it */
600  if(j == dest->ncur) {
601  /* make room */
602  /* !!! this can go once we have _ex() functions */
603  if(NAD_NURI_L(src, ns) > 255)
604  uri = (char *) malloc(sizeof(char) * (NAD_NURI_L(src, ns) + 1));
605  if(NAD_NPREFIX_L(src, ns) > 255)
606  prefix = (char *) malloc(sizeof(char) * (NAD_NURI_L(src, ns) + 1));
607 
608  sprintf(uri, "%.*s", NAD_NURI_L(src, ns), NAD_NURI(src, ns));
609 
610  if(NAD_NPREFIX_L(src, ns) > 0) {
611  sprintf(prefix, "%.*s", NAD_NPREFIX_L(src, ns), NAD_NPREFIX(src, ns));
612  nad_add_namespace(dest, uri, prefix);
613  } else
614  nad_add_namespace(dest, uri, NULL);
615 
616  if(uri != buri) free(uri);
617  if(prefix != bprefix) free(prefix);
618  }
619  }
620 
621  /* scope any new namespaces onto this element */
622  dest->elems[first + i].ns = dest->scope; dest->scope = -1;
623 
624  /* attributes */
625  dest->elems[first + i].attr = -1;
626  if(src->acur > 0) {
627  nattr = 0;
628  for(attr = src->elems[selem + i].attr; attr >= 0; attr = src->attrs[attr].next) nattr++;
629 
630  /* make room */
631  NAD_SAFE(dest->attrs, (dest->acur + nattr) * sizeof(struct nad_attr_st), dest->alen);
632 
633  /* kopy ker-azy! */
634  for(attr = src->elems[selem + i].attr; attr >= 0; attr = src->attrs[attr].next) {
635  /* name */
636  dest->attrs[dest->acur].lname = src->attrs[attr].lname;
637  dest->attrs[dest->acur].iname = _nad_cdata(dest, src->cdata + src->attrs[attr].iname, src->attrs[attr].lname);
638 
639  /* val */
640  dest->attrs[dest->acur].lval = src->attrs[attr].lval;
641  dest->attrs[dest->acur].ival = _nad_cdata(dest, src->cdata + src->attrs[attr].ival, src->attrs[attr].lval);
642 
643  /* namespace */
644  dest->attrs[dest->acur].my_ns = -1;
645 
646  ns = src->attrs[attr].my_ns;
647  if(ns >= 0)
648  for(j = 0; j < dest->ncur; j++)
649  if(NAD_NURI_L(src, ns) == NAD_NURI_L(dest, j) && strncmp(NAD_NURI(src, ns), NAD_NURI(dest, j), NAD_NURI_L(src, ns)) == 0) {
650  dest->attrs[dest->acur].my_ns = j;
651  break;
652  }
653 
654  /* link it up */
655  dest->attrs[dest->acur].next = dest->elems[first + i].attr;
656  dest->elems[first + i].attr = dest->acur;
657 
658  dest->acur++;
659  }
660  }
661  }
662 
663  return first;
664 }
665 
667 int nad_append_elem(nad_t nad, int ns, const char *name, int depth)
668 {
669  int elem;
670 
671  _nad_ptr_check(__func__, nad);
672 
673  /* make sure there's mem for us */
674  NAD_SAFE(nad->elems, (nad->ecur + 1) * sizeof(struct nad_elem_st), nad->elen);
675 
676  elem = nad->ecur;
677  nad->ecur++;
678  nad->elems[elem].lname = strlen(name);
679  nad->elems[elem].iname = _nad_cdata(nad,name,nad->elems[elem].lname);
680  nad->elems[elem].icdata = nad->elems[elem].lcdata = 0;
681  nad->elems[elem].itail = nad->elems[elem].ltail = 0;
682  nad->elems[elem].attr = -1;
683  nad->elems[elem].ns = nad->scope; nad->scope = -1;
684  nad->elems[elem].depth = depth;
685  nad->elems[elem].my_ns = ns;
686 
687  /* make sure there's mem in the depth array, then track us */
688  NAD_SAFE(nad->depths, (depth + 1) * sizeof(int), nad->dlen);
689  nad->depths[depth] = elem;
690 
691  /* our parent is the previous guy in the depth array */
692  if(depth <= 0)
693  nad->elems[elem].parent = -1;
694  else
695  nad->elems[elem].parent = nad->depths[depth - 1];
696 
697  return elem;
698 }
699 
701 int nad_append_attr(nad_t nad, int ns, const char *name, const char *val)
702 {
703  _nad_ptr_check(__func__, nad);
704 
705  return _nad_attr(nad, nad->ecur - 1, ns, name, val, 0);
706 }
707 
709 void nad_append_cdata(nad_t nad, const char *cdata, int len, int depth)
710 {
711  int elem = nad->ecur - 1;
712 
713  _nad_ptr_check(__func__, nad);
714 
715  /* make sure this cdata is the child of the last elem to append */
716  if(nad->elems[elem].depth == depth - 1)
717  {
718  if(nad->elems[elem].icdata == 0)
719  nad->elems[elem].icdata = nad->ccur;
720  _nad_cdata(nad,cdata,len);
721  nad->elems[elem].lcdata += len;
722  return;
723  }
724 
725  /* otherwise, pin the cdata on the tail of the last element at this depth */
726  elem = nad->depths[depth];
727  if(nad->elems[elem].itail == 0)
728  nad->elems[elem].itail = nad->ccur;
729  _nad_cdata(nad,cdata,len);
730  nad->elems[elem].ltail += len;
731 }
732 
734 int nad_add_namespace(nad_t nad, const char *uri, const char *prefix)
735 {
736  int ns;
737 
738  _nad_ptr_check(__func__, nad);
739 
740  /* only add it if its not already in scope */
741  ns = nad_find_scoped_namespace(nad, uri, NULL);
742  if(ns >= 0)
743  return ns;
744 
745  /* make sure there's mem for us */
746  NAD_SAFE(nad->nss, (nad->ncur + 1) * sizeof(struct nad_ns_st), nad->nlen);
747 
748  ns = nad->ncur;
749  nad->ncur++;
750  nad->nss[ns].next = nad->scope;
751  nad->scope = ns;
752 
753  nad->nss[ns].luri = strlen(uri);
754  nad->nss[ns].iuri = _nad_cdata(nad, uri, nad->nss[ns].luri);
755  if(prefix != NULL)
756  {
757  nad->nss[ns].lprefix = strlen(prefix);
758  nad->nss[ns].iprefix = _nad_cdata(nad, prefix, nad->nss[ns].lprefix);
759  }
760  else
761  {
762  nad->nss[ns].lprefix = 0;
763  nad->nss[ns].iprefix = -1;
764  }
765 
766  return ns;
767 }
768 
770 int nad_append_namespace(nad_t nad, int elem, const char *uri, const char *prefix) {
771  int ns;
772 
773  _nad_ptr_check(__func__, nad);
774 
775  /* see if its already scoped on this element */
776  ns = nad_find_namespace(nad, elem, uri, NULL);
777  if(ns >= 0)
778  return ns;
779 
780  /* make some room */
781  NAD_SAFE(nad->nss, (nad->ncur + 1) * sizeof(struct nad_ns_st), nad->nlen);
782 
783  ns = nad->ncur;
784  nad->ncur++;
785  nad->nss[ns].next = nad->elems[elem].ns;
786  nad->elems[elem].ns = ns;
787 
788  nad->nss[ns].luri = strlen(uri);
789  nad->nss[ns].iuri = _nad_cdata(nad, uri, nad->nss[ns].luri);
790  if(prefix != NULL)
791  {
792  nad->nss[ns].lprefix = strlen(prefix);
793  nad->nss[ns].iprefix = _nad_cdata(nad, prefix, nad->nss[ns].lprefix);
794  }
795  else
796  {
797  nad->nss[ns].lprefix = 0;
798  nad->nss[ns].iprefix = -1;
799  }
800 
801  return ns;
802 }
803 
804 static void _nad_escape(nad_t nad, int data, int len, int flag)
805 {
806  char *c;
807  int ic;
808 
809  if(len <= 0) return;
810 
811  /* first, if told, find and escape " */
812  while(flag >= 4 && (c = memchr(nad->cdata + data,'"',len)) != NULL)
813  {
814  /* get offset */
815  ic = c - nad->cdata;
816 
817  /* cute, eh? handle other data before this normally */
818  _nad_escape(nad, data, ic - data, 3);
819 
820  /* ensure enough space, and add our escaped &quot; */
821  NAD_SAFE(nad->cdata, nad->ccur + 6, nad->clen);
822  memcpy(nad->cdata + nad->ccur, "&quot;", 6);
823  nad->ccur += 6;
824 
825  /* just update and loop for more */
826  len -= (ic+1) - data;
827  data = ic+1;
828  }
829 
830  /* next, find and escape ' */
831  while(flag >= 3 && (c = memchr(nad->cdata + data,'\'',len)) != NULL)
832  {
833  ic = c - nad->cdata;
834  _nad_escape(nad, data, ic - data, 2);
835 
836  /* ensure enough space, and add our escaped &apos; */
837  NAD_SAFE(nad->cdata, nad->ccur + 6, nad->clen);
838  memcpy(nad->cdata + nad->ccur, "&apos;", 6);
839  nad->ccur += 6;
840 
841  /* just update and loop for more */
842  len -= (ic+1) - data;
843  data = ic+1;
844  }
845 
846  /* next look for < */
847  while(flag >= 2 && (c = memchr(nad->cdata + data,'<',len)) != NULL)
848  {
849  ic = c - nad->cdata;
850  _nad_escape(nad, data, ic - data, 1);
851 
852  /* ensure enough space, and add our escaped &lt; */
853  NAD_SAFE(nad->cdata, nad->ccur + 4, nad->clen);
854  memcpy(nad->cdata + nad->ccur, "&lt;", 4);
855  nad->ccur += 4;
856 
857  /* just update and loop for more */
858  len -= (ic+1) - data;
859  data = ic+1;
860  }
861 
862  /* next look for > */
863  while(flag >= 1 && (c = memchr(nad->cdata + data, '>', len)) != NULL)
864  {
865  ic = c - nad->cdata;
866  _nad_escape(nad, data, ic - data, 0);
867 
868  /* ensure enough space, and add our escaped &gt; */
869  NAD_SAFE(nad->cdata, nad->ccur + 4, nad->clen);
870  memcpy(nad->cdata + nad->ccur, "&gt;", 4);
871  nad->ccur += 4;
872 
873  /* just update and loop for more */
874  len -= (ic+1) - data;
875  data = ic+1;
876  }
877 
878  /* if & is found, escape it */
879  while((c = memchr(nad->cdata + data,'&',len)) != NULL)
880  {
881  ic = c - nad->cdata;
882 
883  /* ensure enough space */
884  NAD_SAFE(nad->cdata, nad->ccur + 5 + (ic - data), nad->clen);
885 
886  /* handle normal data */
887  memcpy(nad->cdata + nad->ccur, nad->cdata + data, (ic - data));
888  nad->ccur += (ic - data);
889 
890  /* append escaped &amp; */
891  memcpy(nad->cdata + nad->ccur, "&amp;", 5);
892  nad->ccur += 5;
893 
894  /* just update and loop for more */
895  len -= (ic+1) - data;
896  data = ic+1;
897  }
898 
899  /* nothing exciting, just append normal cdata */
900  if(len > 0) {
901  NAD_SAFE(nad->cdata, nad->ccur + len, nad->clen);
902  memcpy(nad->cdata + nad->ccur, nad->cdata + data, len);
903  nad->ccur += len;
904  }
905 }
906 
908 static int _nad_lp0(nad_t nad, int elem)
909 {
910  int attr;
911  int ndepth;
912  int ns;
913  int elem_ns;
914 
915  /* there's a lot of code in here, but don't let that scare you, it's just duplication in order to be a bit more efficient cpu-wise */
916 
917  /* this whole thing is in a big loop for processing siblings */
918  while(elem != nad->ecur)
919  {
920 
921  /* make enough space for the opening element */
922  ns = nad->elems[elem].my_ns;
923  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
924  {
925  NAD_SAFE(nad->cdata, nad->ccur + nad->elems[elem].lname + nad->nss[ns].lprefix + 2, nad->clen);
926  } else {
927  NAD_SAFE(nad->cdata, nad->ccur + nad->elems[elem].lname + 1, nad->clen);
928  }
929 
930  /* opening tag */
931  *(nad->cdata + nad->ccur++) = '<';
932 
933  /* add the prefix if necessary */
934  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
935  {
936  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iprefix, nad->nss[ns].lprefix);
937  nad->ccur += nad->nss[ns].lprefix;
938  *(nad->cdata + nad->ccur++) = ':';
939  }
940 
941  /* copy in the name */
942  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->elems[elem].iname, nad->elems[elem].lname);
943  nad->ccur += nad->elems[elem].lname;
944 
945  /* add element prefix namespace */
946  ns = nad->elems[elem].my_ns;
947  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
948  {
949  /* make space */
950  if(nad->nss[ns].iprefix >= 0)
951  {
952  NAD_SAFE(nad->cdata, nad->ccur + nad->nss[ns].luri + nad->nss[ns].lprefix + 10, nad->clen);
953  } else {
954  NAD_SAFE(nad->cdata, nad->ccur + nad->nss[ns].luri + 9, nad->clen);
955  }
956 
957  /* start */
958  memcpy(nad->cdata + nad->ccur, " xmlns", 6);
959  nad->ccur += 6;
960 
961  /* prefix if necessary */
962  if(nad->nss[ns].iprefix >= 0)
963  {
964  *(nad->cdata + nad->ccur++) = ':';
965  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iprefix, nad->nss[ns].lprefix);
966  nad->ccur += nad->nss[ns].lprefix;
967  }
968 
969  *(nad->cdata + nad->ccur++) = '=';
970  *(nad->cdata + nad->ccur++) = '\'';
971 
972  /* uri */
973  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iuri, nad->nss[ns].luri);
974  nad->ccur += nad->nss[ns].luri;
975 
976  *(nad->cdata + nad->ccur++) = '\'';
977 
978  elem_ns = ns;
979  }else{
980  elem_ns = -1;
981  }
982 
983  /* add the namespaces */
984  for(ns = nad->elems[elem].ns; ns >= 0; ns = nad->nss[ns].next)
985  {
986  /* never explicitly declare the implicit xml namespace */
987  if(nad->nss[ns].luri == strlen(uri_XML) && strncmp(uri_XML, nad->cdata + nad->nss[ns].iuri, nad->nss[ns].luri) == 0)
988  continue;
989 
990  /* do not redeclare element namespace */
991  if(ns == elem_ns)
992  continue;
993 
994  /* make space */
995  if(nad->nss[ns].iprefix >= 0)
996  {
997  NAD_SAFE(nad->cdata, nad->ccur + nad->nss[ns].luri + nad->nss[ns].lprefix + 10, nad->clen);
998  } else {
999  NAD_SAFE(nad->cdata, nad->ccur + nad->nss[ns].luri + 9, nad->clen);
1000  }
1001 
1002  /* start */
1003  memcpy(nad->cdata + nad->ccur, " xmlns", 6);
1004  nad->ccur += 6;
1005 
1006  /* prefix if necessary */
1007  if(nad->nss[ns].iprefix >= 0)
1008  {
1009  *(nad->cdata + nad->ccur++) = ':';
1010  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iprefix, nad->nss[ns].lprefix);
1011  nad->ccur += nad->nss[ns].lprefix;
1012  }
1013 
1014  *(nad->cdata + nad->ccur++) = '=';
1015  *(nad->cdata + nad->ccur++) = '\'';
1016 
1017  /* uri */
1018  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iuri, nad->nss[ns].luri);
1019  nad->ccur += nad->nss[ns].luri;
1020 
1021  *(nad->cdata + nad->ccur++) = '\'';
1022  }
1023 
1024  for(attr = nad->elems[elem].attr; attr >= 0; attr = nad->attrs[attr].next)
1025  {
1026  if(nad->attrs[attr].lname <= 0) continue;
1027 
1028  /* make enough space for the wrapper part */
1029  ns = nad->attrs[attr].my_ns;
1030  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
1031  {
1032  NAD_SAFE(nad->cdata, nad->ccur + nad->attrs[attr].lname + nad->nss[ns].lprefix + 4, nad->clen);
1033  } else {
1034  NAD_SAFE(nad->cdata, nad->ccur + nad->attrs[attr].lname + 3, nad->clen);
1035  }
1036 
1037  *(nad->cdata + nad->ccur++) = ' ';
1038 
1039  /* add the prefix if necessary */
1040  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
1041  {
1042  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iprefix, nad->nss[ns].lprefix);
1043  nad->ccur += nad->nss[ns].lprefix;
1044  *(nad->cdata + nad->ccur++) = ':';
1045  }
1046 
1047  /* copy in the name parts */
1048  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->attrs[attr].iname, nad->attrs[attr].lname);
1049  nad->ccur += nad->attrs[attr].lname;
1050  *(nad->cdata + nad->ccur++) = '=';
1051  *(nad->cdata + nad->ccur++) = '\'';
1052 
1053  /* copy in the escaped value */
1054  _nad_escape(nad, nad->attrs[attr].ival, nad->attrs[attr].lval, 4);
1055 
1056  /* make enough space for the closing quote and add it */
1057  NAD_SAFE(nad->cdata, nad->ccur + 1, nad->clen);
1058  *(nad->cdata + nad->ccur++) = '\'';
1059  }
1060 
1061  /* figure out what's next */
1062  if(elem+1 == nad->ecur)
1063  ndepth = -1;
1064  else
1065  ndepth = nad->elems[elem+1].depth;
1066 
1067  /* handle based on if there are children, update nelem after done */
1068  if(ndepth <= nad->elems[elem].depth)
1069  {
1070  /* make sure there's enough for what we could need */
1071  NAD_SAFE(nad->cdata, nad->ccur + 2, nad->clen);
1072  if(nad->elems[elem].lcdata == 0)
1073  {
1074  memcpy(nad->cdata + nad->ccur, "/>", 2);
1075  nad->ccur += 2;
1076  }else{
1077  *(nad->cdata + nad->ccur++) = '>';
1078 
1079  /* copy in escaped cdata */
1080  _nad_escape(nad, nad->elems[elem].icdata, nad->elems[elem].lcdata,4);
1081 
1082  /* make room */
1083  ns = nad->elems[elem].my_ns;
1084  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
1085  {
1086  NAD_SAFE(nad->cdata, nad->ccur + 4 + nad->elems[elem].lname + nad->nss[ns].lprefix, nad->clen);
1087  } else {
1088  NAD_SAFE(nad->cdata, nad->ccur + 3 + nad->elems[elem].lname, nad->clen);
1089  }
1090 
1091  /* close tag */
1092  memcpy(nad->cdata + nad->ccur, "</", 2);
1093  nad->ccur += 2;
1094 
1095  /* add the prefix if necessary */
1096  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
1097  {
1098  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iprefix, nad->nss[ns].lprefix);
1099  nad->ccur += nad->nss[ns].lprefix;
1100  *(nad->cdata + nad->ccur++) = ':';
1101  }
1102 
1103  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->elems[elem].iname, nad->elems[elem].lname);
1104  nad->ccur += nad->elems[elem].lname;
1105  *(nad->cdata + nad->ccur++) = '>';
1106  }
1107 
1108  /* always try to append the tail */
1109  _nad_escape(nad, nad->elems[elem].itail, nad->elems[elem].ltail,4);
1110 
1111  /* if no siblings either, bail */
1112  if(ndepth < nad->elems[elem].depth)
1113  return elem+1;
1114 
1115  /* next sibling */
1116  elem++;
1117  }else{
1118  int nelem;
1119  /* process any children */
1120 
1121  /* close ourself and append any cdata first */
1122  NAD_SAFE(nad->cdata, nad->ccur + 1, nad->clen);
1123  *(nad->cdata + nad->ccur++) = '>';
1124  _nad_escape(nad, nad->elems[elem].icdata, nad->elems[elem].lcdata, 4);
1125 
1126  /* process children */
1127  nelem = _nad_lp0(nad, elem+1);
1128 
1129  /* close and tail up */
1130  ns = nad->elems[elem].my_ns;
1131  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
1132  {
1133  NAD_SAFE(nad->cdata, nad->ccur + 4 + nad->elems[elem].lname + nad->nss[ns].lprefix, nad->clen);
1134  } else {
1135  NAD_SAFE(nad->cdata, nad->ccur + 3 + nad->elems[elem].lname, nad->clen);
1136  }
1137  memcpy(nad->cdata + nad->ccur, "</", 2);
1138  nad->ccur += 2;
1139  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
1140  {
1141  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iprefix, nad->nss[ns].lprefix);
1142  nad->ccur += nad->nss[ns].lprefix;
1143  *(nad->cdata + nad->ccur++) = ':';
1144  }
1145  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->elems[elem].iname, nad->elems[elem].lname);
1146  nad->ccur += nad->elems[elem].lname;
1147  *(nad->cdata + nad->ccur++) = '>';
1148  _nad_escape(nad, nad->elems[elem].itail, nad->elems[elem].ltail,4);
1149 
1150  /* if the next element is not our sibling, we're done */
1151  if(nelem < nad->ecur && nad->elems[nelem].depth < nad->elems[elem].depth)
1152  return nelem;
1153 
1154  /* for next sibling in while loop */
1155  elem = nelem;
1156  }
1157 
1158  /* here's the end of that big while loop */
1159  }
1160 
1161  return elem;
1162 }
1163 
1164 void nad_print(nad_t nad, int elem, const char **xml, int *len)
1165 {
1166  int ixml = nad->ccur;
1167 
1168  _nad_ptr_check(__func__, nad);
1169 
1170  _nad_lp0(nad, elem);
1171  *len = nad->ccur - ixml;
1172  *xml = nad->cdata + ixml;
1173 }
1174 
1192 void nad_serialize(nad_t nad, char **buf, int *len) {
1193  char *pos;
1194 
1195  _nad_ptr_check(__func__, nad);
1196 
1197  *len = sizeof(int) * 5 + /* 4 ints in nad_t, plus one for len */
1198  sizeof(struct nad_elem_st) * nad->ecur +
1199  sizeof(struct nad_attr_st) * nad->acur +
1200  sizeof(struct nad_ns_st) * nad->ncur +
1201  sizeof(char) * nad->ccur;
1202 
1203  *buf = (char *) malloc(*len);
1204  pos = *buf;
1205 
1206  * (int *) pos = *len; pos += sizeof(int);
1207  * (int *) pos = nad->ecur; pos += sizeof(int);
1208  * (int *) pos = nad->acur; pos += sizeof(int);
1209  * (int *) pos = nad->ncur; pos += sizeof(int);
1210  * (int *) pos = nad->ccur; pos += sizeof(int);
1211 
1212  memcpy(pos, nad->elems, sizeof(struct nad_elem_st) * nad->ecur); pos += sizeof(struct nad_elem_st) * nad->ecur;
1213  memcpy(pos, nad->attrs, sizeof(struct nad_attr_st) * nad->acur); pos += sizeof(struct nad_attr_st) * nad->acur;
1214  memcpy(pos, nad->nss, sizeof(struct nad_ns_st) * nad->ncur); pos += sizeof(struct nad_ns_st) * nad->ncur;
1215  memcpy(pos, nad->cdata, sizeof(char) * nad->ccur);
1216 }
1217 
1218 nad_t nad_deserialize(const char *buf) {
1219  nad_t nad = nad_new();
1220  const char *pos = buf + sizeof(int); /* skip len */
1221 
1222  _nad_ptr_check(__func__, nad);
1223 
1224  nad->ecur = * (int *) pos; pos += sizeof(int);
1225  nad->acur = * (int *) pos; pos += sizeof(int);
1226  nad->ncur = * (int *) pos; pos += sizeof(int);
1227  nad->ccur = * (int *) pos; pos += sizeof(int);
1228  nad->elen = nad->ecur;
1229  nad->alen = nad->acur;
1230  nad->nlen = nad->ncur;
1231  nad->clen = nad->ccur;
1232 
1233  if(nad->ecur > 0)
1234  {
1235  nad->elems = (struct nad_elem_st *) malloc(sizeof(struct nad_elem_st) * nad->ecur);
1236  memcpy(nad->elems, pos, sizeof(struct nad_elem_st) * nad->ecur);
1237  pos += sizeof(struct nad_elem_st) * nad->ecur;
1238  }
1239 
1240  if(nad->acur > 0)
1241  {
1242  nad->attrs = (struct nad_attr_st *) malloc(sizeof(struct nad_attr_st) * nad->acur);
1243  memcpy(nad->attrs, pos, sizeof(struct nad_attr_st) * nad->acur);
1244  pos += sizeof(struct nad_attr_st) * nad->acur;
1245  }
1246 
1247  if(nad->ncur > 0)
1248  {
1249  nad->nss = (struct nad_ns_st *) malloc(sizeof(struct nad_ns_st) * nad->ncur);
1250  memcpy(nad->nss, pos, sizeof(struct nad_ns_st) * nad->ncur);
1251  pos += sizeof(struct nad_ns_st) * nad->ncur;
1252  }
1253 
1254  if(nad->ccur > 0)
1255  {
1256  nad->cdata = (char *) malloc(sizeof(char) * nad->ccur);
1257  memcpy(nad->cdata, pos, sizeof(char) * nad->ccur);
1258  }
1259 
1260  return nad;
1261 }
1262 
1263 
1266 struct build_data {
1267  nad_t nad;
1268  int depth;
1269  XML_Parser p;
1270 };
1271 
1272 static void _nad_parse_element_start(void *arg, const char *name, const char **atts) {
1273  struct build_data *bd = (struct build_data *) arg;
1274  char buf[1024];
1275  char *uri, *elem, *prefix;
1276  const char **attr;
1277  int el, ns;
1278 
1279  /* make a copy */
1280  strncpy(buf, name, 1024);
1281  buf[1023] = '\0';
1282 
1283  /* expat gives us:
1284  prefixed namespaced elem: uri|elem|prefix
1285  default namespaced elem: uri|elem
1286  un-namespaced elem: elem
1287  */
1288 
1289  /* extract all the bits */
1290  uri = buf;
1291  elem = strchr(uri, '|');
1292  if(elem != NULL) {
1293  *elem = '\0';
1294  elem++;
1295  prefix = strchr(elem, '|');
1296  if(prefix != NULL) {
1297  *prefix = '\0';
1298  prefix++;
1299  }
1300  ns = nad_add_namespace(bd->nad, uri, prefix);
1301  } else {
1302  /* un-namespaced, just take it as-is */
1303  uri = NULL;
1304  elem = buf;
1305  prefix = NULL;
1306  ns = -1;
1307  }
1308 
1309  /* add it */
1310  el = nad_append_elem(bd->nad, ns, elem, bd->depth);
1311 
1312  /* now the attributes, one at a time */
1313  attr = atts;
1314  while(attr[0] != NULL) {
1315 
1316  /* make a copy */
1317  strncpy(buf, attr[0], 1024);
1318  buf[1023] = '\0';
1319 
1320  /* extract all the bits */
1321  uri = buf;
1322  elem = strchr(uri, '|');
1323  if(elem != NULL) {
1324  *elem = '\0';
1325  elem++;
1326  prefix = strchr(elem, '|');
1327  if(prefix != NULL) {
1328  *prefix = '\0';
1329  prefix++;
1330  }
1331  ns = nad_append_namespace(bd->nad, el, uri, prefix);
1332  } else {
1333  /* un-namespaced, just take it as-is */
1334  uri = NULL;
1335  elem = buf;
1336  prefix = NULL;
1337  ns = -1;
1338  }
1339 
1340  /* add it */
1341  nad_append_attr(bd->nad, ns, elem, (char *) attr[1]);
1342 
1343  attr += 2;
1344  }
1345 
1346  bd->depth++;
1347 }
1348 
1349 static void _nad_parse_element_end(void *arg, const char *name) {
1350  struct build_data *bd = (struct build_data *) arg;
1351 
1352  bd->depth--;
1353 }
1354 
1355 static void _nad_parse_cdata(void *arg, const char *str, int len) {
1356  struct build_data *bd = (struct build_data *) arg;
1357 
1358  /* go */
1359  nad_append_cdata(bd->nad, (char *) str, len, bd->depth);
1360 }
1361 
1362 static void _nad_parse_namespace_start(void *arg, const char *prefix, const char *uri) {
1363  struct build_data *bd = (struct build_data *) arg;
1364  int ns;
1365 
1366  ns = nad_add_namespace(bd->nad, (char *) uri, (char *) prefix);
1367 
1368  /* Always set the namespace (to catch cases where nad_add_namespace doesn't add it) */
1369  bd->nad->scope = ns;
1370 }
1371 
1372 #ifdef HAVE_XML_STOPPARSER
1373 /* Stop the parser if an entity declaration is hit. */
1374 static void _nad_parse_entity_declaration(void *arg, const char *entityName,
1375  int is_parameter_entity, const char *value,
1376  int value_length, const char *base,
1377  const char *systemId, const char *publicId,
1378  const char *notationName)
1379 {
1380  struct build_data *bd = (struct build_data *) arg;
1381 
1382  XML_StopParser(bd->p, XML_FALSE);
1383 }
1384 #endif
1385 
1386 nad_t nad_parse(const char *buf, int len) {
1387  struct build_data bd;
1388  XML_Parser p;
1389 
1390  if(len == 0)
1391  len = strlen(buf);
1392 
1393  p = XML_ParserCreateNS(NULL, '|');
1394  if(p == NULL)
1395  return NULL;
1396  bd.p = p;
1397 
1398  XML_SetReturnNSTriplet(p, 1);
1399  /* Prevent the "billion laughs" attack against expat by disabling
1400  * internal entity expansion. With 2.x, forcibly stop the parser
1401  * if an entity is declared - this is safer and a more obvious
1402  * failure mode. With older versions, simply prevent expenansion
1403  * of such entities. */
1404 #ifdef HAVE_XML_STOPPARSER
1405  XML_SetEntityDeclHandler(p, (void *) _nad_parse_entity_declaration);
1406 #else
1407  XML_SetDefaultHandler(p, NULL);
1408 #endif
1409 
1410  bd.nad = nad_new();
1411  bd.depth = 0;
1412 
1413  XML_SetUserData(p, (void *) &bd);
1414  XML_SetElementHandler(p, _nad_parse_element_start, _nad_parse_element_end);
1415  XML_SetCharacterDataHandler(p, _nad_parse_cdata);
1416  XML_SetStartNamespaceDeclHandler(p, _nad_parse_namespace_start);
1417 
1418  if(!XML_Parse(p, buf, len, 1)) {
1419  XML_ParserFree(p);
1420  nad_free(bd.nad);
1421  return NULL;
1422  }
1423 
1424  XML_ParserFree(p);
1425 
1426  if(bd.depth != 0)
1427  return NULL;
1428 
1429  return bd.nad;
1430 }
int attr
Definition: nad.h:74
int iname
Definition: nad.h:81
struct nad_elem_st * elems
Definition: nad.h:95
Definition: nad.h:93
#define _nad_ptr_check(func, nad)
!!! Things to do (after 2.0)
Definition: nad.c:61
nad_t nad_new(void)
create a new nad
Definition: nad.c:125
int nad_append_attr(nad_t nad, int ns, const char *name, const char *val)
attach new attr to the last elem
Definition: nad.c:701
int nad_insert_elem(nad_t nad, int parent, int ns, const char *name, const char *cdata)
shove in a new child elem after the given one
Definition: nad.c:405
#define NAD_SAFE(blocks, size, len)
this is the safety check used to make sure there's always enough mem
Definition: nad.c:89
int ltail
Definition: nad.h:73
int depth
Definition: config.c:39
Not A DOM.
int nad_find_elem(nad_t nad, int elem, int ns, const char *name, int depth)
locate the next elem at a given depth with an optional matching name
Definition: nad.c:204
void nad_append_cdata(nad_t nad, const char *cdata, int len, int depth)
append new cdata to the last elem
Definition: nad.c:709
static void _nad_parse_element_end(void *arg, const char *name)
Definition: nad.c:1349
int next
Definition: nad.h:84
struct nad_attr_st * attrs
Definition: nad.h:96
int icdata
Definition: nad.h:72
int nad_add_namespace(nad_t nad, const char *uri, const char *prefix)
bring a new namespace into scope
Definition: nad.c:734
#define BLOCKSIZE
Definition: nad.c:64
static int _nad_realloc(void **oblocks, int len)
Reallocate the given buffer to make it larger.
Definition: nad.c:76
int elen
Definition: nad.h:102
int nad_append_elem(nad_t nad, int ns, const char *name, int depth)
create a new elem on the list
Definition: nad.c:667
void nad_free(nad_t nad)
free that nad
Definition: nad.c:178
int lname
Definition: nad.h:81
static int _nad_cdata(nad_t nad, const char *cdata, int len)
internal: append some cdata and return the index to it
Definition: nad.c:92
void nad_wrap_elem(nad_t nad, int elem, int ns, const char *name)
wrap an element with another element
Definition: nad.c:475
nad_t nad_copy(nad_t nad)
copy a nad
Definition: nad.c:145
int lname
Definition: nad.h:71
nad_t nad_deserialize(const char *buf)
Definition: nad.c:1218
#define NAD_NPREFIX_L(N, NS)
Definition: nad.h:194
void nad_serialize(nad_t nad, char **buf, int *len)
nads serialize to a buffer of this form:
Definition: nad.c:1192
void nad_set_attr(nad_t nad, int elem, int ns, const char *name, const char *val, int vallen)
create, update, or zap any matching attr on this elem
Definition: nad.c:375
#define uri_XML
Definition: uri.h:31
int parent
Definition: nad.h:70
static void _nad_escape(nad_t nad, int data, int len, int flag)
Definition: nad.c:804
nad_t nad_parse(const char *buf, int len)
create a nad from raw xml
Definition: nad.c:1386
int clen
Definition: nad.h:102
struct nad_ns_st * nss
Definition: nad.h:97
#define NAD_NURI_L(N, NS)
Definition: nad.h:192
void xhash_put(xht h, const char *key, void *val)
Definition: xhash.c:163
static void _nad_parse_element_start(void *arg, const char *name, const char **atts)
Definition: nad.c:1272
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
Definition: nad.c:102
XML_Parser p
Definition: nad.c:1269
int ecur
Definition: nad.h:105
int lval
Definition: nad.h:82
int acur
Definition: nad.h:105
void xhash_zap(xht h, const char *key)
Definition: xhash.c:235
int alen
Definition: nad.h:102
static void _nad_parse_cdata(void *arg, const char *str, int len)
Definition: nad.c:1355
int luri
Definition: nad.h:88
int dlen
Definition: nad.h:102
int ns
Definition: nad.h:75
nad_t nad
Definition: config.c:38
int lprefix
Definition: nad.h:89
int scope
Definition: nad.h:107
char * cdata
Definition: nad.h:98
int nad_insert_nad(nad_t dest, int delem, nad_t src, int selem)
insert part of a nad into another nad
Definition: nad.c:512
int iprefix
Definition: nad.h:89
int nad_find_namespace(nad_t nad, int elem, const char *uri, const char *prefix)
get a matching ns on this elem, both uri and optional prefix
Definition: nad.c:262
#define NAD_NPREFIX(N, NS)
Definition: nad.h:193
int * depths
Definition: nad.h:99
void nad_drop_elem(nad_t nad, int elem)
remove an element (and its subelements)
Definition: nad.c:452
int nlen
Definition: nad.h:102
int ccur
Definition: nad.h:105
pool_t xhash_pool(xht h)
get our pool
Definition: xhash.c:305
char * pstrdup(pool_t p, const char *src)
XXX efficient: move this to const char * and then loop throug the existing heaps to see if src is wit...
Definition: pool.c:191
void * xhash_get(xht h, const char *key)
Definition: xhash.c:184
#define NAD_NURI(N, NS)
Definition: nad.h:191
int ncur
Definition: nad.h:105
int next
Definition: nad.h:90
int nad_find_attr(nad_t nad, int elem, int ns, const char *name, const char *val)
get a matching attr on this elem, both name and optional val
Definition: nad.c:235
int nad_append_namespace(nad_t nad, int elem, const char *uri, const char *prefix)
declare a namespace on an already-existing element
Definition: nad.c:770
int nad_find_elem_path(nad_t nad, 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 ...
Definition: nad.c:318
static void _nad_parse_namespace_start(void *arg, const char *prefix, const char *uri)
Definition: nad.c:1362
int itail
Definition: nad.h:73
static int _nad_lp0(nad_t nad, int elem)
internal recursive printing function
Definition: nad.c:908
int ival
Definition: nad.h:82
int lcdata
Definition: nad.h:72
Definition: nad.h:87
int depth
Definition: nad.h:77
void nad_print(nad_t nad, int elem, const char **xml, int *len)
create a string representation of the given element (and children), point references to it ...
Definition: nad.c:1164
parse a buffer into a nad
Definition: config.c:36
int my_ns
Definition: nad.h:83
int my_ns
Definition: nad.h:76
int iname
Definition: nad.h:71
int nad_find_scoped_namespace(nad_t nad, const char *uri, const char *prefix)
find a namespace in scope
Definition: nad.c:290
int iuri
Definition: nad.h:88