jabberd2  2.3.1
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  nad->nss[ns].iprefix = -1;
762 
763  return ns;
764 }
765 
767 int nad_append_namespace(nad_t nad, int elem, const char *uri, const char *prefix) {
768  int ns;
769 
770  _nad_ptr_check(__func__, nad);
771 
772  /* see if its already scoped on this element */
773  ns = nad_find_namespace(nad, elem, uri, NULL);
774  if(ns >= 0)
775  return ns;
776 
777  /* make some room */
778  NAD_SAFE(nad->nss, (nad->ncur + 1) * sizeof(struct nad_ns_st), nad->nlen);
779 
780  ns = nad->ncur;
781  nad->ncur++;
782  nad->nss[ns].next = nad->elems[elem].ns;
783  nad->elems[elem].ns = ns;
784 
785  nad->nss[ns].luri = strlen(uri);
786  nad->nss[ns].iuri = _nad_cdata(nad, uri, nad->nss[ns].luri);
787  if(prefix != NULL)
788  {
789  nad->nss[ns].lprefix = strlen(prefix);
790  nad->nss[ns].iprefix = _nad_cdata(nad, prefix, nad->nss[ns].lprefix);
791  }
792  else
793  nad->nss[ns].iprefix = -1;
794 
795  return ns;
796 }
797 
798 static void _nad_escape(nad_t nad, int data, int len, int flag)
799 {
800  char *c;
801  int ic;
802 
803  if(len <= 0) return;
804 
805  /* first, if told, find and escape " */
806  while(flag >= 4 && (c = memchr(nad->cdata + data,'"',len)) != NULL)
807  {
808  /* get offset */
809  ic = c - nad->cdata;
810 
811  /* cute, eh? handle other data before this normally */
812  _nad_escape(nad, data, ic - data, 3);
813 
814  /* ensure enough space, and add our escaped &quot; */
815  NAD_SAFE(nad->cdata, nad->ccur + 6, nad->clen);
816  memcpy(nad->cdata + nad->ccur, "&quot;", 6);
817  nad->ccur += 6;
818 
819  /* just update and loop for more */
820  len -= (ic+1) - data;
821  data = ic+1;
822  }
823 
824  /* next, find and escape ' */
825  while(flag >= 3 && (c = memchr(nad->cdata + data,'\'',len)) != NULL)
826  {
827  ic = c - nad->cdata;
828  _nad_escape(nad, data, ic - data, 2);
829 
830  /* ensure enough space, and add our escaped &apos; */
831  NAD_SAFE(nad->cdata, nad->ccur + 6, nad->clen);
832  memcpy(nad->cdata + nad->ccur, "&apos;", 6);
833  nad->ccur += 6;
834 
835  /* just update and loop for more */
836  len -= (ic+1) - data;
837  data = ic+1;
838  }
839 
840  /* next look for < */
841  while(flag >= 2 && (c = memchr(nad->cdata + data,'<',len)) != NULL)
842  {
843  ic = c - nad->cdata;
844  _nad_escape(nad, data, ic - data, 1);
845 
846  /* ensure enough space, and add our escaped &lt; */
847  NAD_SAFE(nad->cdata, nad->ccur + 4, nad->clen);
848  memcpy(nad->cdata + nad->ccur, "&lt;", 4);
849  nad->ccur += 4;
850 
851  /* just update and loop for more */
852  len -= (ic+1) - data;
853  data = ic+1;
854  }
855 
856  /* next look for > */
857  while(flag >= 1 && (c = memchr(nad->cdata + data, '>', len)) != NULL)
858  {
859  ic = c - nad->cdata;
860  _nad_escape(nad, data, ic - data, 0);
861 
862  /* ensure enough space, and add our escaped &gt; */
863  NAD_SAFE(nad->cdata, nad->ccur + 4, nad->clen);
864  memcpy(nad->cdata + nad->ccur, "&gt;", 4);
865  nad->ccur += 4;
866 
867  /* just update and loop for more */
868  len -= (ic+1) - data;
869  data = ic+1;
870  }
871 
872  /* if & is found, escape it */
873  while((c = memchr(nad->cdata + data,'&',len)) != NULL)
874  {
875  ic = c - nad->cdata;
876 
877  /* ensure enough space */
878  NAD_SAFE(nad->cdata, nad->ccur + 5 + (ic - data), nad->clen);
879 
880  /* handle normal data */
881  memcpy(nad->cdata + nad->ccur, nad->cdata + data, (ic - data));
882  nad->ccur += (ic - data);
883 
884  /* append escaped &amp; */
885  memcpy(nad->cdata + nad->ccur, "&amp;", 5);
886  nad->ccur += 5;
887 
888  /* just update and loop for more */
889  len -= (ic+1) - data;
890  data = ic+1;
891  }
892 
893  /* nothing exciting, just append normal cdata */
894  if(len > 0) {
895  NAD_SAFE(nad->cdata, nad->ccur + len, nad->clen);
896  memcpy(nad->cdata + nad->ccur, nad->cdata + data, len);
897  nad->ccur += len;
898  }
899 }
900 
902 static int _nad_lp0(nad_t nad, int elem)
903 {
904  int attr;
905  int ndepth;
906  int ns;
907  int elem_ns;
908 
909  /* 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 */
910 
911  /* this whole thing is in a big loop for processing siblings */
912  while(elem != nad->ecur)
913  {
914 
915  /* make enough space for the opening element */
916  ns = nad->elems[elem].my_ns;
917  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
918  {
919  NAD_SAFE(nad->cdata, nad->ccur + nad->elems[elem].lname + nad->nss[ns].lprefix + 2, nad->clen);
920  } else {
921  NAD_SAFE(nad->cdata, nad->ccur + nad->elems[elem].lname + 1, nad->clen);
922  }
923 
924  /* opening tag */
925  *(nad->cdata + nad->ccur++) = '<';
926 
927  /* add the prefix if necessary */
928  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
929  {
930  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iprefix, nad->nss[ns].lprefix);
931  nad->ccur += nad->nss[ns].lprefix;
932  *(nad->cdata + nad->ccur++) = ':';
933  }
934 
935  /* copy in the name */
936  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->elems[elem].iname, nad->elems[elem].lname);
937  nad->ccur += nad->elems[elem].lname;
938 
939  /* add element prefix namespace */
940  ns = nad->elems[elem].my_ns;
941  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
942  {
943  /* make space */
944  if(nad->nss[ns].iprefix >= 0)
945  {
946  NAD_SAFE(nad->cdata, nad->ccur + nad->nss[ns].luri + nad->nss[ns].lprefix + 10, nad->clen);
947  } else {
948  NAD_SAFE(nad->cdata, nad->ccur + nad->nss[ns].luri + 9, nad->clen);
949  }
950 
951  /* start */
952  memcpy(nad->cdata + nad->ccur, " xmlns", 6);
953  nad->ccur += 6;
954 
955  /* prefix if necessary */
956  if(nad->nss[ns].iprefix >= 0)
957  {
958  *(nad->cdata + nad->ccur++) = ':';
959  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iprefix, nad->nss[ns].lprefix);
960  nad->ccur += nad->nss[ns].lprefix;
961  }
962 
963  *(nad->cdata + nad->ccur++) = '=';
964  *(nad->cdata + nad->ccur++) = '\'';
965 
966  /* uri */
967  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iuri, nad->nss[ns].luri);
968  nad->ccur += nad->nss[ns].luri;
969 
970  *(nad->cdata + nad->ccur++) = '\'';
971 
972  elem_ns = ns;
973  }else{
974  elem_ns = -1;
975  }
976 
977  /* add the namespaces */
978  for(ns = nad->elems[elem].ns; ns >= 0; ns = nad->nss[ns].next)
979  {
980  /* never explicitly declare the implicit xml namespace */
981  if(nad->nss[ns].luri == strlen(uri_XML) && strncmp(uri_XML, nad->cdata + nad->nss[ns].iuri, nad->nss[ns].luri) == 0)
982  continue;
983 
984  /* do not redeclare element namespace */
985  if(ns == elem_ns)
986  continue;
987 
988  /* make space */
989  if(nad->nss[ns].iprefix >= 0)
990  {
991  NAD_SAFE(nad->cdata, nad->ccur + nad->nss[ns].luri + nad->nss[ns].lprefix + 10, nad->clen);
992  } else {
993  NAD_SAFE(nad->cdata, nad->ccur + nad->nss[ns].luri + 9, nad->clen);
994  }
995 
996  /* start */
997  memcpy(nad->cdata + nad->ccur, " xmlns", 6);
998  nad->ccur += 6;
999 
1000  /* prefix if necessary */
1001  if(nad->nss[ns].iprefix >= 0)
1002  {
1003  *(nad->cdata + nad->ccur++) = ':';
1004  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iprefix, nad->nss[ns].lprefix);
1005  nad->ccur += nad->nss[ns].lprefix;
1006  }
1007 
1008  *(nad->cdata + nad->ccur++) = '=';
1009  *(nad->cdata + nad->ccur++) = '\'';
1010 
1011  /* uri */
1012  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iuri, nad->nss[ns].luri);
1013  nad->ccur += nad->nss[ns].luri;
1014 
1015  *(nad->cdata + nad->ccur++) = '\'';
1016  }
1017 
1018  for(attr = nad->elems[elem].attr; attr >= 0; attr = nad->attrs[attr].next)
1019  {
1020  if(nad->attrs[attr].lname <= 0) continue;
1021 
1022  /* make enough space for the wrapper part */
1023  ns = nad->attrs[attr].my_ns;
1024  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
1025  {
1026  NAD_SAFE(nad->cdata, nad->ccur + nad->attrs[attr].lname + nad->nss[ns].lprefix + 4, nad->clen);
1027  } else {
1028  NAD_SAFE(nad->cdata, nad->ccur + nad->attrs[attr].lname + 3, nad->clen);
1029  }
1030 
1031  *(nad->cdata + nad->ccur++) = ' ';
1032 
1033  /* add the prefix if necessary */
1034  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
1035  {
1036  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iprefix, nad->nss[ns].lprefix);
1037  nad->ccur += nad->nss[ns].lprefix;
1038  *(nad->cdata + nad->ccur++) = ':';
1039  }
1040 
1041  /* copy in the name parts */
1042  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->attrs[attr].iname, nad->attrs[attr].lname);
1043  nad->ccur += nad->attrs[attr].lname;
1044  *(nad->cdata + nad->ccur++) = '=';
1045  *(nad->cdata + nad->ccur++) = '\'';
1046 
1047  /* copy in the escaped value */
1048  _nad_escape(nad, nad->attrs[attr].ival, nad->attrs[attr].lval, 4);
1049 
1050  /* make enough space for the closing quote and add it */
1051  NAD_SAFE(nad->cdata, nad->ccur + 1, nad->clen);
1052  *(nad->cdata + nad->ccur++) = '\'';
1053  }
1054 
1055  /* figure out what's next */
1056  if(elem+1 == nad->ecur)
1057  ndepth = -1;
1058  else
1059  ndepth = nad->elems[elem+1].depth;
1060 
1061  /* handle based on if there are children, update nelem after done */
1062  if(ndepth <= nad->elems[elem].depth)
1063  {
1064  /* make sure there's enough for what we could need */
1065  NAD_SAFE(nad->cdata, nad->ccur + 2, nad->clen);
1066  if(nad->elems[elem].lcdata == 0)
1067  {
1068  memcpy(nad->cdata + nad->ccur, "/>", 2);
1069  nad->ccur += 2;
1070  }else{
1071  *(nad->cdata + nad->ccur++) = '>';
1072 
1073  /* copy in escaped cdata */
1074  _nad_escape(nad, nad->elems[elem].icdata, nad->elems[elem].lcdata,4);
1075 
1076  /* make room */
1077  ns = nad->elems[elem].my_ns;
1078  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
1079  {
1080  NAD_SAFE(nad->cdata, nad->ccur + 4 + nad->elems[elem].lname + nad->nss[ns].lprefix, nad->clen);
1081  } else {
1082  NAD_SAFE(nad->cdata, nad->ccur + 3 + nad->elems[elem].lname, nad->clen);
1083  }
1084 
1085  /* close tag */
1086  memcpy(nad->cdata + nad->ccur, "</", 2);
1087  nad->ccur += 2;
1088 
1089  /* add the prefix if necessary */
1090  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
1091  {
1092  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iprefix, nad->nss[ns].lprefix);
1093  nad->ccur += nad->nss[ns].lprefix;
1094  *(nad->cdata + nad->ccur++) = ':';
1095  }
1096 
1097  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->elems[elem].iname, nad->elems[elem].lname);
1098  nad->ccur += nad->elems[elem].lname;
1099  *(nad->cdata + nad->ccur++) = '>';
1100  }
1101 
1102  /* always try to append the tail */
1103  _nad_escape(nad, nad->elems[elem].itail, nad->elems[elem].ltail,4);
1104 
1105  /* if no siblings either, bail */
1106  if(ndepth < nad->elems[elem].depth)
1107  return elem+1;
1108 
1109  /* next sibling */
1110  elem++;
1111  }else{
1112  int nelem;
1113  /* process any children */
1114 
1115  /* close ourself and append any cdata first */
1116  NAD_SAFE(nad->cdata, nad->ccur + 1, nad->clen);
1117  *(nad->cdata + nad->ccur++) = '>';
1118  _nad_escape(nad, nad->elems[elem].icdata, nad->elems[elem].lcdata, 4);
1119 
1120  /* process children */
1121  nelem = _nad_lp0(nad, elem+1);
1122 
1123  /* close and tail up */
1124  ns = nad->elems[elem].my_ns;
1125  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
1126  {
1127  NAD_SAFE(nad->cdata, nad->ccur + 4 + nad->elems[elem].lname + nad->nss[ns].lprefix, nad->clen);
1128  } else {
1129  NAD_SAFE(nad->cdata, nad->ccur + 3 + nad->elems[elem].lname, nad->clen);
1130  }
1131  memcpy(nad->cdata + nad->ccur, "</", 2);
1132  nad->ccur += 2;
1133  if(ns >= 0 && nad->nss[ns].iprefix >= 0)
1134  {
1135  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->nss[ns].iprefix, nad->nss[ns].lprefix);
1136  nad->ccur += nad->nss[ns].lprefix;
1137  *(nad->cdata + nad->ccur++) = ':';
1138  }
1139  memcpy(nad->cdata + nad->ccur, nad->cdata + nad->elems[elem].iname, nad->elems[elem].lname);
1140  nad->ccur += nad->elems[elem].lname;
1141  *(nad->cdata + nad->ccur++) = '>';
1142  _nad_escape(nad, nad->elems[elem].itail, nad->elems[elem].ltail,4);
1143 
1144  /* if the next element is not our sibling, we're done */
1145  if(nelem < nad->ecur && nad->elems[nelem].depth < nad->elems[elem].depth)
1146  return nelem;
1147 
1148  /* for next sibling in while loop */
1149  elem = nelem;
1150  }
1151 
1152  /* here's the end of that big while loop */
1153  }
1154 
1155  return elem;
1156 }
1157 
1158 void nad_print(nad_t nad, int elem, const char **xml, int *len)
1159 {
1160  int ixml = nad->ccur;
1161 
1162  _nad_ptr_check(__func__, nad);
1163 
1164  _nad_lp0(nad, elem);
1165  *len = nad->ccur - ixml;
1166  *xml = nad->cdata + ixml;
1167 }
1168 
1186 void nad_serialize(nad_t nad, char **buf, int *len) {
1187  char *pos;
1188 
1189  _nad_ptr_check(__func__, nad);
1190 
1191  *len = sizeof(int) * 5 + /* 4 ints in nad_t, plus one for len */
1192  sizeof(struct nad_elem_st) * nad->ecur +
1193  sizeof(struct nad_attr_st) * nad->acur +
1194  sizeof(struct nad_ns_st) * nad->ncur +
1195  sizeof(char) * nad->ccur;
1196 
1197  *buf = (char *) malloc(*len);
1198  pos = *buf;
1199 
1200  * (int *) pos = *len; pos += sizeof(int);
1201  * (int *) pos = nad->ecur; pos += sizeof(int);
1202  * (int *) pos = nad->acur; pos += sizeof(int);
1203  * (int *) pos = nad->ncur; pos += sizeof(int);
1204  * (int *) pos = nad->ccur; pos += sizeof(int);
1205 
1206  memcpy(pos, nad->elems, sizeof(struct nad_elem_st) * nad->ecur); pos += sizeof(struct nad_elem_st) * nad->ecur;
1207  memcpy(pos, nad->attrs, sizeof(struct nad_attr_st) * nad->acur); pos += sizeof(struct nad_attr_st) * nad->acur;
1208  memcpy(pos, nad->nss, sizeof(struct nad_ns_st) * nad->ncur); pos += sizeof(struct nad_ns_st) * nad->ncur;
1209  memcpy(pos, nad->cdata, sizeof(char) * nad->ccur);
1210 }
1211 
1212 nad_t nad_deserialize(const char *buf) {
1213  nad_t nad = nad_new();
1214  const char *pos = buf + sizeof(int); /* skip len */
1215 
1216  _nad_ptr_check(__func__, nad);
1217 
1218  nad->ecur = * (int *) pos; pos += sizeof(int);
1219  nad->acur = * (int *) pos; pos += sizeof(int);
1220  nad->ncur = * (int *) pos; pos += sizeof(int);
1221  nad->ccur = * (int *) pos; pos += sizeof(int);
1222  nad->elen = nad->ecur;
1223  nad->alen = nad->acur;
1224  nad->nlen = nad->ncur;
1225  nad->clen = nad->ccur;
1226 
1227  if(nad->ecur > 0)
1228  {
1229  nad->elems = (struct nad_elem_st *) malloc(sizeof(struct nad_elem_st) * nad->ecur);
1230  memcpy(nad->elems, pos, sizeof(struct nad_elem_st) * nad->ecur);
1231  pos += sizeof(struct nad_elem_st) * nad->ecur;
1232  }
1233 
1234  if(nad->acur > 0)
1235  {
1236  nad->attrs = (struct nad_attr_st *) malloc(sizeof(struct nad_attr_st) * nad->acur);
1237  memcpy(nad->attrs, pos, sizeof(struct nad_attr_st) * nad->acur);
1238  pos += sizeof(struct nad_attr_st) * nad->acur;
1239  }
1240 
1241  if(nad->ncur > 0)
1242  {
1243  nad->nss = (struct nad_ns_st *) malloc(sizeof(struct nad_ns_st) * nad->ncur);
1244  memcpy(nad->nss, pos, sizeof(struct nad_ns_st) * nad->ncur);
1245  pos += sizeof(struct nad_ns_st) * nad->ncur;
1246  }
1247 
1248  if(nad->ccur > 0)
1249  {
1250  nad->cdata = (char *) malloc(sizeof(char) * nad->ccur);
1251  memcpy(nad->cdata, pos, sizeof(char) * nad->ccur);
1252  }
1253 
1254  return nad;
1255 }
1256 
1257 
1260 struct build_data {
1261  nad_t nad;
1262  int depth;
1263  XML_Parser p;
1264 };
1265 
1266 static void _nad_parse_element_start(void *arg, const char *name, const char **atts) {
1267  struct build_data *bd = (struct build_data *) arg;
1268  char buf[1024];
1269  char *uri, *elem, *prefix;
1270  const char **attr;
1271  int el, ns;
1272 
1273  /* make a copy */
1274  strncpy(buf, name, 1024);
1275  buf[1023] = '\0';
1276 
1277  /* expat gives us:
1278  prefixed namespaced elem: uri|elem|prefix
1279  default namespaced elem: uri|elem
1280  un-namespaced elem: elem
1281  */
1282 
1283  /* extract all the bits */
1284  uri = buf;
1285  elem = strchr(uri, '|');
1286  if(elem != NULL) {
1287  *elem = '\0';
1288  elem++;
1289  prefix = strchr(elem, '|');
1290  if(prefix != NULL) {
1291  *prefix = '\0';
1292  prefix++;
1293  }
1294  ns = nad_add_namespace(bd->nad, uri, prefix);
1295  } else {
1296  /* un-namespaced, just take it as-is */
1297  uri = NULL;
1298  elem = buf;
1299  prefix = NULL;
1300  ns = -1;
1301  }
1302 
1303  /* add it */
1304  el = nad_append_elem(bd->nad, ns, elem, bd->depth);
1305 
1306  /* now the attributes, one at a time */
1307  attr = atts;
1308  while(attr[0] != NULL) {
1309 
1310  /* make a copy */
1311  strncpy(buf, attr[0], 1024);
1312  buf[1023] = '\0';
1313 
1314  /* extract all the bits */
1315  uri = buf;
1316  elem = strchr(uri, '|');
1317  if(elem != NULL) {
1318  *elem = '\0';
1319  elem++;
1320  prefix = strchr(elem, '|');
1321  if(prefix != NULL) {
1322  *prefix = '\0';
1323  prefix++;
1324  }
1325  ns = nad_append_namespace(bd->nad, el, uri, prefix);
1326  } else {
1327  /* un-namespaced, just take it as-is */
1328  uri = NULL;
1329  elem = buf;
1330  prefix = NULL;
1331  ns = -1;
1332  }
1333 
1334  /* add it */
1335  nad_append_attr(bd->nad, ns, elem, (char *) attr[1]);
1336 
1337  attr += 2;
1338  }
1339 
1340  bd->depth++;
1341 }
1342 
1343 static void _nad_parse_element_end(void *arg, const char *name) {
1344  struct build_data *bd = (struct build_data *) arg;
1345 
1346  bd->depth--;
1347 }
1348 
1349 static void _nad_parse_cdata(void *arg, const char *str, int len) {
1350  struct build_data *bd = (struct build_data *) arg;
1351 
1352  /* go */
1353  nad_append_cdata(bd->nad, (char *) str, len, bd->depth);
1354 }
1355 
1356 static void _nad_parse_namespace_start(void *arg, const char *prefix, const char *uri) {
1357  struct build_data *bd = (struct build_data *) arg;
1358  int ns;
1359 
1360  ns = nad_add_namespace(bd->nad, (char *) uri, (char *) prefix);
1361 
1362  /* Always set the namespace (to catch cases where nad_add_namespace doesn't add it) */
1363  bd->nad->scope = ns;
1364 }
1365 
1366 #ifdef HAVE_XML_STOPPARSER
1367 /* Stop the parser if an entity declaration is hit. */
1368 static void _nad_parse_entity_declaration(void *arg, const char *entityName,
1369  int is_parameter_entity, const char *value,
1370  int value_length, const char *base,
1371  const char *systemId, const char *publicId,
1372  const char *notationName)
1373 {
1374  struct build_data *bd = (struct build_data *) arg;
1375 
1376  XML_StopParser(bd->p, XML_FALSE);
1377 }
1378 #endif
1379 
1380 nad_t nad_parse(const char *buf, int len) {
1381  struct build_data bd;
1382  XML_Parser p;
1383 
1384  if(len == 0)
1385  len = strlen(buf);
1386 
1387  p = XML_ParserCreateNS(NULL, '|');
1388  if(p == NULL)
1389  return NULL;
1390  bd.p = p;
1391 
1392  XML_SetReturnNSTriplet(p, 1);
1393  /* Prevent the "billion laughs" attack against expat by disabling
1394  * internal entity expansion. With 2.x, forcibly stop the parser
1395  * if an entity is declared - this is safer and a more obvious
1396  * failure mode. With older versions, simply prevent expenansion
1397  * of such entities. */
1398 #ifdef HAVE_XML_STOPPARSER
1399  XML_SetEntityDeclHandler(p, (void *) _nad_parse_entity_declaration);
1400 #else
1401  XML_SetDefaultHandler(p, NULL);
1402 #endif
1403 
1404  bd.nad = nad_new();
1405  bd.depth = 0;
1406 
1407  XML_SetUserData(p, (void *) &bd);
1408  XML_SetElementHandler(p, _nad_parse_element_start, _nad_parse_element_end);
1409  XML_SetCharacterDataHandler(p, _nad_parse_cdata);
1410  XML_SetStartNamespaceDeclHandler(p, _nad_parse_namespace_start);
1411 
1412  if(!XML_Parse(p, buf, len, 1)) {
1413  XML_ParserFree(p);
1414  nad_free(bd.nad);
1415  return NULL;
1416  }
1417 
1418  XML_ParserFree(p);
1419 
1420  if(bd.depth != 0)
1421  return NULL;
1422 
1423  return bd.nad;
1424 }
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&#39;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:1343
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:1212
#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:1186
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:798
nad_t nad_parse(const char *buf, int len)
create a nad from raw xml
Definition: nad.c:1380
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:1266
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:1263
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:1349
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:767
int nad_find_elem_path(nad_t nad, int elem, int ns, const char *name)
find elem using XPath like query name – &quot;name&quot; for the child tag of that name &quot;name/name&quot; for a sub c...
Definition: nad.c:318
static void _nad_parse_namespace_start(void *arg, const char *prefix, const char *uri)
Definition: nad.c:1356
int itail
Definition: nad.h:73
static int _nad_lp0(nad_t nad, int elem)
internal recursive printing function
Definition: nad.c:902
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:1158
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