su_taglist.c 35.9 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@SU_TAG
 * 
 * @CFILE su_taglist.c  
 *
 * Implementation of tag items and lists.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Tue Feb 20 20:03:38 2001 ppessi
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
42
#include <limits.h>
Pekka Pessi's avatar
Pekka Pessi committed
43 44 45 46 47 48 49 50 51 52 53

#if defined(va_copy)
/* Xyzzy */
#elif defined(__va_copy)
#define va_copy(dst, src) __va_copy((dst), (src))
#else
#define va_copy(dst, src) (memcpy(&(dst), &(src), sizeof (va_list)))
#endif

#include <assert.h>

54
#include <sofia-sip/su_config.h>
Pekka Pessi's avatar
Pekka Pessi committed
55

56 57 58 59
#include <sofia-sip/su_tag.h>
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tag_inline.h>
#include <sofia-sip/su_tagarg.h>
Pekka Pessi's avatar
Pekka Pessi committed
60

61 62 63 64
#ifndef HAVE_STRTOULL
unsigned longlong strtoull(const char *, char **, int);
#endif

Pekka Pessi's avatar
Pekka Pessi committed
65 66 67 68
/**@defgroup su_tag Tag Item Lists
 *
 * Object-oriented tag routines for Sofia utility library.
 *
Pekka Pessi's avatar
Pekka Pessi committed
69
 * The <sofia-sip/su_tag.h> defines a interface to object-oriented tag list routines. 
Pekka Pessi's avatar
Pekka Pessi committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
 * A tag list is a linear list (array) of tag items, tagi_t structures,
 * terminated by a TAG_END() item. Each tag item has a label, tag, (@c
 * t_tag) and a value (@c t_value). The tag is a pointer (tag_type_t) to a
 * structure defining how the value should be interpreted, in other words,
 * the name and the type of the value. The value or pointer to the actual
 * value is stored as opaque data (tag_value_t). The tag item structure is
 * defined as follows:
 *
 * @code
 * typedef struct {
 *   tag_type_t   t_tag;
 *   tag_value_t  t_value;
 * } tagi_t;
 * @endcode
 *
 * The tag lists are central concept in the Sofia APIs. The tags lists can
 * be used to a list of named arguments to a @ref tagarg "@em tagarg"
 * function, to store variable amount of data in a memory area, and pass
 * data between processes and threads.
 * 
 * The tagged argument lists can be used like named arguments in
 * higher-level languages. The argument list consists of tag-value pairs;
 * tags specify the name and type of the value. All the tag items are not
 * necessarily interpreted by the called function, but it can pass the list
 * to other functions. This feature is used also by the Sofia APIs, the
 * lower-layer settings and options are frequently passed through the
 * upper-layer API in the tag lists.
 * 
 * The tagged argument lists are constructed using special macros that
 * expand to two function arguments, tag and value. Each tag item macro
 * checks its arguments type so the tagged argument lists are typesafe if
 * the list is correctly constructed.
 * 
 * Each function documents the tags it accepts and also the tags it may pass
 * to the lower layers (at least in theory).
 * 
 * @par Special Tags
 *
 * There are a new special tags that are used to control and modify the tag
 * list processing itself. These special tags are as follows:
 * - TAG_NULL() or TAG_END() - indicates the end of tag list
 * - TAG_SKIP() - indicates an empty (overwritten) tag item 
 * - TAG_NEXT() - contains a pointer to the next tag list.
 *
 * The tag type structures are declared as tag_typedef_t. They can be
Pekka Pessi's avatar
Pekka Pessi committed
115
 * defined by the macros found in <sofia-sip/su_tag_class.h>. See nta_tag.c or
Pekka Pessi's avatar
Pekka Pessi committed
116 117 118 119
 * su_tag_test.c for an example.
 *
 */

120 121 122 123 124 125 126 127 128
/**@class tag_class_s sofia-sip/su_tag_class.h <sofia-sip/su_tag_class.h>
 *
 * @brief Virtual function table for @ref su_tag "tags".
 *
 * The struct tag_class_s contains virtual function table for tags,
 * specifying non-default behaviour of different tags. It provides functions
 * for copying, matching, printing and converting the tagged values.
 */

Pekka Pessi's avatar
Pekka Pessi committed
129 130
#ifdef longlong
typedef longlong unsigned llu;
Pekka Pessi's avatar
Pekka Pessi committed
131 132 133 134 135 136 137 138
#else
typedef long unsigned llu;
#endif

/** Print a tag. */
int t_snprintf(tagi_t const *t, char b[], size_t size)
{
  tag_type_t tt = TAG_TYPE_OF(t);
139
  int n, m;
Pekka Pessi's avatar
Pekka Pessi committed
140 141 142 143
  
  n = snprintf(b, size, "%s::%s: ", 
               tt->tt_ns ? tt->tt_ns : "",
	       tt->tt_name ? tt->tt_name : "null");
144 145
  if (n < 0)
    return n;
Pekka Pessi's avatar
Pekka Pessi committed
146

147
  if ((size_t)n > size)
Pekka Pessi's avatar
Pekka Pessi committed
148 149 150 151 152 153 154
    size = n;

  if (tt->tt_snprintf)
    m = tt->tt_snprintf(t, b + n, size - n);
  else
    m = snprintf(b + n, size - n, "%llx", (llu)t->t_value);

155 156
  if (m < 0)
    return m;
Pekka Pessi's avatar
Pekka Pessi committed
157

158
  if (m == 0 && 0 < n && (size_t)n < size)
Pekka Pessi's avatar
Pekka Pessi committed
159 160 161 162 163 164 165
    b[--n] = '\0';

  return n + m;
}

/** Get next tag item from list.
 */
166
tagi_t *tl_next(tagi_t const *t)
Pekka Pessi's avatar
Pekka Pessi committed
167
{
168 169 170 171 172 173 174 175 176
  tag_type_t tt;

  t = t_next(t);

  for (tt = TAG_TYPE_OF(t); t && tt->tt_next; tt = TAG_TYPE_OF(t)) {
    t = tt->tt_next(t);
  }

  return (tagi_t *)t;
Pekka Pessi's avatar
Pekka Pessi committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
}

/**Move a tag list.
 * 
 * The function tl_tmove() moves the tag list arguments to @a dst.  The @a
 * dst must have big enough for all arguments.
 * 
 * @param dst   pointer to the destination buffer
 * @param size  sizeof @a dst
 * @param t_tag,t_value,... tag list
 *
 * @return
 * The function tl_tmove() returns number of tag list items initialized.
 */
size_t tl_tmove(tagi_t *dst, size_t size, 
		tag_type_t t_tag, tag_value_t t_value, ...)
{
  size_t n = 0, N = size / sizeof(tagi_t);
  tagi_t tagi[1];
  va_list ap;
  
  va_start(ap, t_value); 

  tagi->t_tag = t_tag, tagi->t_value = t_value;

  for (;;) {
203
    assert((size_t)((char *)&dst[n] - (char *)dst) < size);
Pekka Pessi's avatar
Pekka Pessi committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    if (n < N)
      dst[n] = *tagi;
    n++;
    if (t_end(tagi)) 
      break;

    tagi->t_tag = va_arg(ap, tag_type_t);
    tagi->t_value = va_arg(ap, tag_value_t);
  }

  va_end(ap);

  return n;
}

/**Move a tag list.
 * 
 * The function tl_move() copies the tag list @a src to the buffer @a
 * dst. The size of the @a dst list must be at least @c tl_len(src) bytes.
 * 
 * @param dst pointer to the destination buffer
 * @param src tag list to be moved
 *
 * @return
 * The function tl_move() returns a pointer to the @a dst list after last
 * moved element.  
 */
tagi_t *tl_move(tagi_t *dst, tagi_t const src[])
{
  do {
    dst = t_move(dst, src);
  }
  while ((src = t_next(src)));

  return dst;
}

241
/** Calculate effective length of a tag list as bytes. */
Pekka Pessi's avatar
Pekka Pessi committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
size_t tl_len(tagi_t const lst[])
{
  size_t len = 0;
  
  do {
    len += t_len(lst);
  } 
  while ((lst = t_next(lst)));

  return len;
}

/** Calculate the size of extra memory areas associated with tag list. */
size_t tl_xtra(tagi_t const lst[], size_t offset)
{
  size_t xtra = offset;

  for (; lst; lst = t_next(lst))
    xtra += t_xtra(lst, xtra);

  return xtra - offset;
}

/** Duplicate a tag list.
266 267 268 269 270 271
 *
 * Deep copy the tag list @a src to the buffer @a dst. Memory areas
 * associated with @a src are copied to buffer at @a **bb.
 *
 * This is a rather low-level function. See tl_adup() for a more convenient
 * functionality.
Pekka Pessi's avatar
Pekka Pessi committed
272 273 274 275
 *
 * The size of the @a dst buffer must be at least @c tl_len(src) bytes.  The
 * size of buffer @a **bb must be at least @c tl_dup_xtra(src) bytes. 
 * 
276 277 278
 * @param[out] dst pointer to the destination buffer
 * @param[in] src tag list to be duplicated
 * @param[in,out] bb  pointer to pointer to buffer
Pekka Pessi's avatar
Pekka Pessi committed
279 280
 *
 * @return
281
 * A pointer to the @a dst list after last
Pekka Pessi's avatar
Pekka Pessi committed
282 283
 * duplicated taglist element.  
 *
284 285
 * The pointer at @a *bb is updated to the byte after last duplicated memory
 * area.
Pekka Pessi's avatar
Pekka Pessi committed
286 287 288 289 290 291 292 293 294 295 296
 */
tagi_t *tl_dup(tagi_t dst[], tagi_t const src[], void **bb)
{
  do {
    dst = t_dup(dst, src, bb);
  } while ((src = t_next(src)));

  return dst;
}
	       

297 298 299 300 301 302 303 304 305 306 307 308 309
/** Free a tag list.
 *
 * The function tl_free() frees resources associated with a tag list.
 * In other words, it calls t_free on each tag item on the list. 
 *
 */
void tl_free(tagi_t list[])
{
  while (list)
    list = t_free(list);
}

/** Allocate and duplicate a tag list using memory home. */
Pekka Pessi's avatar
Pekka Pessi committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323
tagi_t *tl_adup(su_home_t *home, tagi_t const lst[])
{
  size_t len = tl_len(lst);
  size_t xtra = tl_xtra(lst, 0);
  void *b = su_alloc(home, len + xtra);
  tagi_t *d, *newlst = b;

  void *end = (char *)b + len + xtra;
  tagi_t *tend = (tagi_t*)((char *)b + len);

  b = (char *)b + len;

  d = tl_dup(newlst, lst, &b);

324
  assert(b == end); assert(tend == d); (void)end; (void)tend;
Pekka Pessi's avatar
Pekka Pessi committed
325 326 327 328

  return newlst;
}

329
/** Allocate and duplicate tagged arguments as a tag list using memory home. */
Pekka Pessi's avatar
Pekka Pessi committed
330 331 332 333 334 335 336 337 338 339 340 341
tagi_t *tl_tlist(su_home_t *home, tag_type_t tag, tag_value_t value, ...)
{
  tagi_t *tl;
  ta_list ta;

  ta_start(ta, tag, value);
  tl = tl_adup(home, ta_args(ta));
  ta_end(ta);

  return tl;
}

342 343 344 345 346
/** Find first tag item with type @a tt from list. */
tagi_t *tl_find(tagi_t const lst[], tag_type_t tt)
{
  return (tagi_t *)t_find(tt, lst);
}
Pekka Pessi's avatar
Pekka Pessi committed
347

348 349
/** Find last tag item with type @a tt from list. */
tagi_t *tl_find_last(tagi_t const lst[], tag_type_t tt)
Pekka Pessi's avatar
Pekka Pessi committed
350
{
351 352
  tagi_t const *last, *next;
  
353
  for (next = last = t_find(tt, lst); next; next = t_find(tt, t_next(last)))
354
    last = next;
Pekka Pessi's avatar
Pekka Pessi committed
355

356
  return (tagi_t *)last;
Pekka Pessi's avatar
Pekka Pessi committed
357 358
}

359
su_inline
Pekka Pessi's avatar
Pekka Pessi committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
int t_ref_set(tag_type_t tt, void *ref, tagi_t const value[])
{
  if (value == NULL)
    return 0;

  if (tt->tt_class->tc_ref_set)
    return tt->tt_class->tc_ref_set(tt, ref, value);

  *(tag_value_t *)ref = value->t_value;

  return 1;
}

static int tl_get(tag_type_t tt, void *p, tagi_t const lst[])
{
  tagi_t const *t, *latest = NULL;
  
  assert(tt);
  
  if (tt == NULL || p == NULL)
    return 0;

  if (tt->tt_class == ref_tag_class)
    tt = (tag_type_t)tt->tt_magic;

  for (t = t_find(tt, lst); t; t = t_find(tt, t_next(t)))
    latest = t;

  return t_ref_set(tt, p, latest);
}

/** Find tags from given list. */
392
int tl_gets(tagi_t const lst[], tag_type_t tag, tag_value_t value, ...)
Pekka Pessi's avatar
Pekka Pessi committed
393
{
394
  int n = 0;
Pekka Pessi's avatar
Pekka Pessi committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
  tagi_t *t;
  ta_list ta;

  ta_start(ta, tag, value);

  for (t = ta_args(ta); t; t = (tagi_t *)t_next(t)) {
    tag_type_t tt = t->t_tag;

    if (!tt)
      continue;

    if (tt->tt_class == ref_tag_class) {
      assert(((tag_type_t)tt->tt_magic)->tt_class->tc_ref_set);
      n += tl_get(tt, (void *)t->t_value, lst);
    }
#if !defined(NDEBUG)
411
    else if (tt->tt_class && tt->tt_class->tc_ref_set) {
Pekka Pessi's avatar
Pekka Pessi committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425
      fprintf(stderr, "WARNING: tag %s::%s directly used by tl_gets()\n",
	      tt->tt_ns, tt->tt_name);
      assert(tt->tt_class == ref_tag_class);
    }
#endif
  } 

  ta_end(ta);

  return n;
}

/** Find tags from given list. 
 *
426
 * Copies values of argument tag list into the reference tags in the tag
Pekka Pessi's avatar
Pekka Pessi committed
427 428 429 430
 * list @a lst.
 *
 * @sa tl_gets()
 */
431
int tl_tgets(tagi_t lst[], tag_type_t tag, tag_value_t value, ...)
Pekka Pessi's avatar
Pekka Pessi committed
432
{
433
  int n = 0;
Pekka Pessi's avatar
Pekka Pessi committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  tagi_t *t;

  ta_list ta;
  ta_start(ta, tag, value);

  for (t = lst; t; t = (tagi_t *)t_next(t)) {
    tag_type_t tt = t->t_tag;

    if (!tt)
      continue;

    if (tt->tt_class == ref_tag_class) {
      assert(((tag_type_t)tt->tt_magic)->tt_class->tc_ref_set);
      n += tl_get(tt, (void *)t->t_value, ta_args(ta));
    }
#if !defined(NDEBUG)
    else if (tt->tt_class->tc_ref_set) {
      fprintf(stderr, "WARNING: tag %s::%s used in tl_tgets(lst)\n",
	      tt->tt_ns, tt->tt_name);
      assert(tt->tt_class == ref_tag_class);
    }
#endif
  } 

  ta_end(ta);

  return n;
}


/** Filter an element in tag list */
465 466 467 468
tagi_t *t_filter(tagi_t *dst, 
		 tagi_t const filter[], 
		 tagi_t const *src, 
		 void **bb)
Pekka Pessi's avatar
Pekka Pessi committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
{
  tag_type_t tt = TAG_TYPE_OF(src);
  tagi_t const *f;

  if (dst) {
    for (f = filter; f; f = t_next(f)) {
      if (TAG_TYPE_OF(f)->tt_filter)
	dst = TAG_TYPE_OF(f)->tt_filter(dst, f, src, bb);
      else if (f->t_tag == tt)
	dst = t_dup(dst, src, bb); 
    }
  }
  else {
    size_t d = 0;

    for (f = filter; f; f = t_next(f)) {
      if (TAG_TYPE_OF(f)->tt_filter)
	d += (size_t)TAG_TYPE_OF(f)->tt_filter(NULL, f, src, bb);
      else if (tt == f->t_tag) {
	d += t_len(src);
	*bb = (char *)*bb + t_xtra(src, (size_t)*bb);
      }
    }

    dst = (tagi_t *)d;
  }

  return dst;
}

499 500 501 502 503 504 505 506 507 508 509 510
/** Make filtered copy of a tag list @a src with @a filter to @a dst.
 *
 * Each tag in @a src is checked against tags in list @a filter. If the tag
 * is in the @a filter list, or there is a special filter tag in the list
 * which matches with the tag in @a src, the tag is duplicated to @a dst using 
 * memory buffer in @a b.
 *
 * When @a dst is NULL, this function calculates the size of the filtered list.
 *
 * @sa tl_afilter(), tl_tfilter(), tl_filtered_tlist(),
 * TAG_FILTER(), TAG_ANY(), #ns_tag_class
 */
Pekka Pessi's avatar
Pekka Pessi committed
511
tagi_t *tl_filter(tagi_t dst[], 
512 513 514
		  tagi_t const filter[], 
		  tagi_t const src[], 
		  void **b)
Pekka Pessi's avatar
Pekka Pessi committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
{
  tagi_t const *s;
  tagi_t *d;

  if (dst) {
    for (s = src, d = dst; s; s = t_next(s))
      d = t_filter(d, filter, s, b);
  }
  else {
    size_t rv = 0;

    for (s = src, d = dst; s; s = t_next(s)) {
      d = t_filter(NULL, filter, s, b);
      rv += (char *)d - (char *)NULL;
    }

    d = (tagi_t *)rv;
  }

  return d;
}



/**Filter a tag list. 
 * 
 * The function tl_afilter() will build a tag list containing tags specified
 * in @a filter and extracted from @a src.  It will allocate the memory used by
 * tag list via the specified memory @a home, which may be also @c NULL.
544 545 546
 *
 * @sa tl_afilter(), tl_tfilter(), tl_filtered_tlist(), 
 * TAG_FILTER(), TAG_ANY(), #ns_tag_class
Pekka Pessi's avatar
Pekka Pessi committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
 */
tagi_t *tl_afilter(su_home_t *home, tagi_t const filter[], tagi_t const src[])
{
  tagi_t *dst, *d, *t_end = NULL;
  void *b, *end = NULL;
  size_t len;

  /* Calculate length of the result */
  t_end = tl_filter(NULL, filter, src, &end);
  len = ((char *)t_end - (char *)NULL) + ((char *)end - (char*)NULL);

  if (len == 0)
    return NULL;

  /* Allocate the result */
  if (!(dst = su_alloc(home, len)))
    return NULL;

  /* Build the result */
  b = (dst + (t_end - (tagi_t *)NULL));
  d = tl_filter(dst, filter, src, (void **)&b);

  /* Ensure that everything is consistent */
  assert(d == dst + (t_end - (tagi_t *)NULL));
  assert(b == (char *)dst + len);

  return dst;
}

576 577 578 579
/** Filter tag list @a src with given tags.
 * 
 * @sa tl_afilter(), tl_filtered_tlist(), TAG_FILTER(), TAG_ANY(), #ns_tag_class
 */
Pekka Pessi's avatar
Pekka Pessi committed
580 581 582 583 584 585 586 587 588 589 590
tagi_t *tl_tfilter(su_home_t *home, tagi_t const src[], 
		   tag_type_t tag, tag_value_t value, ...)
{
  tagi_t *tl;
  ta_list ta;
  ta_start(ta, tag, value);
  tl = tl_afilter(home, ta_args(ta), src);
  ta_end(ta);
  return tl;
}

Pekka Pessi's avatar
Pekka Pessi committed
591
/** Create a filtered tag list.
592 593
 *
 * @sa tl_afilter(), tl_tfilter(), TAG_FILTER(), TAG_ANY(), #ns_tag_class
Pekka Pessi's avatar
Pekka Pessi committed
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
 */
tagi_t *tl_filtered_tlist(su_home_t *home, tagi_t const filter[], 
			  tag_type_t tag, tag_value_t value, ...)
{
  tagi_t *tl;
  ta_list ta;

  ta_start(ta, tag, value);
  tl = tl_afilter(home, filter, ta_args(ta));
  ta_end(ta);

  return tl;
}


Pekka Pessi's avatar
Pekka Pessi committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
/** Remove listed tags from the list @a lst. */
int tl_tremove(tagi_t lst[], tag_type_t tag, tag_value_t value, ...)
{
  tagi_t *l, *l_next;
  int retval = 0;
  ta_list ta;

  ta_start(ta, tag, value);

  for (l = lst; l; l = l_next) {
    if ((l_next = (tagi_t *)t_next(l))) {
      if (tl_find(ta_args(ta), l->t_tag))
	l->t_tag = tag_skip;
      else
	retval++;
    }
  }

  ta_end(ta);

  return retval;
}

/** Calculate length of a tag list with a @c va_list. */
size_t tl_vlen(va_list ap)
{
  size_t len = 0;
  tagi_t tagi[2] = {{ NULL }};

  do {
    tagi->t_tag = va_arg(ap, tag_type_t );
    tagi->t_value = va_arg(ap, tag_value_t);
    len += sizeof(tagi_t);
  } while (!t_end(tagi));

  return len;
}

/** Convert va_list to tag list */
tagi_t *tl_vlist(va_list ap)
{
  tagi_t *t, *rv;
  va_list aq;

  va_copy(aq, ap);
  rv = malloc(tl_vlen(aq));
  va_end(aq);

  for (t = rv; t; t++) {
    t->t_tag = va_arg(ap, tag_type_t);
    t->t_value = va_arg(ap, tag_value_t);

    if (t_end(t))
      break;
  }

  return rv;
}

tagi_t *tl_vlist2(tag_type_t tag, tag_value_t value, va_list ap)
{
  tagi_t *t, *rv;
  tagi_t tagi[1];
672
  size_t size;
Pekka Pessi's avatar
Pekka Pessi committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701

  tagi->t_tag = tag, tagi->t_value = value;

  if (!t_end(tagi)) {
    va_list aq;
    va_copy(aq, ap);
    size = sizeof(tagi) + tl_vlen(aq);
    va_end(aq);
  }
  else
    size = sizeof(tagi);

  t = rv = malloc(size);

  for (;t;) {
    *t++ = *tagi;

    if (t_end(tagi)) 
      break;

    tagi->t_tag = va_arg(ap, tag_type_t);
    tagi->t_value = va_arg(ap, tag_value_t);
  }
  
  assert((char *)rv + size == (char *)t);

  return rv;
}

702
/** Make a tag list until TAG_NEXT() or TAG_END() */
Pekka Pessi's avatar
Pekka Pessi committed
703 704 705 706 707 708 709 710 711 712 713 714
tagi_t *tl_list(tag_type_t tag, tag_value_t value, ...)
{
  va_list ap;  
  tagi_t *t;

  va_start(ap, value);
  t = tl_vlist2(tag, value, ap);
  va_end(ap);

  return t;
}

715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
/** Calculate length of a linear tag list. */
size_t tl_vllen(tag_type_t tag, tag_value_t value, va_list ap)
{
  size_t len = sizeof(tagi_t);
  tagi_t const *next;
  tagi_t tagi[3];

  tagi[0].t_tag = tag;
  tagi[0].t_value = value;
  tagi[1].t_tag = tag_any;
  tagi[1].t_value = 0;

  for (;;) {
    next = tl_next(tagi);
    if (next != tagi + 1)
      break;
    
    if (tagi->t_tag != tag_skip)
      len += sizeof(tagi_t);
    tagi->t_tag = va_arg(ap, tag_type_t);
    tagi->t_value = va_arg(ap, tag_value_t);
  }

  for (; next; next = tl_next(next))
    len += sizeof(tagi_t);

  return len;
}

/** Make a linear tag list. */
tagi_t *tl_vllist(tag_type_t tag, tag_value_t value, va_list ap)
{
  va_list aq;
  tagi_t *t, *rv;
  tagi_t const *next;
  tagi_t tagi[2];

752
  size_t size;
753 754 755 756 757 758

  va_copy(aq, ap);
  size = tl_vllen(tag, value, aq);
  va_end(aq);

  t = rv = malloc(size);
759 760
  if (rv == NULL)
    return rv;
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787

  tagi[0].t_tag = tag;
  tagi[0].t_value = value;
  tagi[1].t_tag = tag_any;
  tagi[1].t_value = 0;

  for (;;) {
    next = tl_next(tagi);
    if (next != tagi + 1)
      break;

    if (tagi->t_tag != tag_skip)
      *t++ = *tagi;

    tagi->t_tag = va_arg(ap, tag_type_t);
    tagi->t_value = va_arg(ap, tag_value_t);
  }

  for (; next; next = tl_next(next))
    *t++ = *next;

  t->t_tag = NULL; t->t_value = 0; t++;

  assert((char *)rv + size == (char *)t);

  return rv;
}
788

789 790 791
/** Make a linear tag list until TAG_END().
 *
 */
792 793 794 795 796 797 798 799 800 801 802 803 804
tagi_t *tl_llist(tag_type_t tag, tag_value_t value, ...)
{
  va_list ap;  
  tagi_t *t;

  va_start(ap, value);
  t = tl_vllist(tag, value, ap);
  va_end(ap);

  return t;
}

/** Free a tag list allocated by tl_list(), tl_llist() or tl_vlist(). */
Pekka Pessi's avatar
Pekka Pessi committed
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
void tl_vfree(tagi_t *t)
{
  if (t)
    free(t);
}

/** Convert a string to the a value of a tag. */
int t_scan(tag_type_t tt, su_home_t *home, char const *s, 
	   tag_value_t *return_value)
{
  if (tt == NULL || s == NULL || return_value == NULL)
    return -1;

  if (tt->tt_class->tc_scan) {
    return tt->tt_class->tc_scan(tt, home, s, return_value);
820 821
  }
  else {			/* Not implemented */
Pekka Pessi's avatar
Pekka Pessi committed
822
    *return_value = (tag_value_t)0;
823
    return -2;
Pekka Pessi's avatar
Pekka Pessi committed
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
  }
}


/* ====================================================================== */
/* null tag */

tagi_t const *t_null_next(tagi_t const *t)
{
  return NULL;
}

tagi_t *t_null_move(tagi_t *dst, tagi_t const *src)
{
  memset(dst, 0, sizeof(*dst));
  return dst + 1;
}

tagi_t *t_null_dup(tagi_t *dst, tagi_t const *src, void **bb)
{
  memset(dst, 0, sizeof(*dst));
  return dst + 1;
}

tagi_t *t_null_copy(tagi_t *dst, tagi_t const *src, void **bb)
{
  memset(dst, 0, sizeof(*dst));
  return dst + 1;
}

tagi_t const * t_null_find(tag_type_t tt, tagi_t const lst[])
{
  return NULL;
}

Pekka Pessi's avatar
Pekka Pessi committed
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
tagi_t *t_null_filter(tagi_t *dst, 
		      tagi_t const filter[], 
		      tagi_t const *src, 
		      void **bb)
{
  if (TAG_TYPE_OF(src) == tag_null) {
    if (dst) {
      dst->t_tag = NULL;
      dst->t_value = 0;
    }
    return dst + 1;
  }
  return dst;
}

Pekka Pessi's avatar
Pekka Pessi committed
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
tag_class_t null_tag_class[1] = 
  {{ 
    sizeof(null_tag_class), 
    /* tc_next */     t_null_next,
    /* tc_len */      NULL,
    /* tc_move */     t_null_move,
    /* tc_xtra */     NULL,
    /* tc_dup */      t_null_dup,
    /* tc_free */     NULL,
    /* tc_find */     t_null_find,
    /* tc_snprintf */ NULL,
    /* tc_filter */   t_null_filter,
    /* tc_ref_set */  NULL,
    /* tc_scan */     NULL,
  }};

890 891
tag_typedef_t tag_null = TAG_TYPEDEF(tag_null, null);

Pekka Pessi's avatar
Pekka Pessi committed
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
/* ====================================================================== */
/* end tag */

tagi_t *t_end_filter(tagi_t *dst,
		     tagi_t const filter[],
		     tagi_t const *src,
		     void **bb)
{
  return dst;
}

tag_class_t end_tag_class[1] =
  {{
    sizeof(end_tag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     NULL,
    /* tc_dup */      NULL,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ NULL,
    /* tc_filter */   t_end_filter,
    /* tc_ref_set */  NULL,
    /* tc_scan */     NULL,
  }};

/* ====================================================================== */
/* skip tag - placeholder in tag list */

922 923 924 925 926
tagi_t const *t_skip_next(tagi_t const *t)
{
  return t + 1;
}

Pekka Pessi's avatar
Pekka Pessi committed
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
tagi_t *t_skip_move(tagi_t *dst, tagi_t const *src)
{
  return dst;
}

size_t t_skip_len(tagi_t const *t)
{
  return 0;
}

tagi_t *t_skip_dup(tagi_t *dst, tagi_t const *src, void **bb)
{
  return dst;
}

Pekka Pessi's avatar
Pekka Pessi committed
942 943 944 945 946 947 948 949
tagi_t *t_skip_filter(tagi_t *dst, 
		    tagi_t const filter[], 
		    tagi_t const *src, 
		    void **bb)
{
  return dst;
}

Pekka Pessi's avatar
Pekka Pessi committed
950 951 952
tag_class_t skip_tag_class[1] = 
  {{
    sizeof(skip_tag_class),
953
    /* tc_next */     t_skip_next,
Pekka Pessi's avatar
Pekka Pessi committed
954 955 956 957 958 959 960 961 962 963 964 965
    /* tc_len */      t_skip_len,
    /* tc_move */     t_skip_move,
    /* tc_xtra */     NULL,
    /* tc_dup */      t_skip_dup,
    /* tc_free */     NULL,
    /* tc_find */     t_null_find,
    /* tc_snprintf */ NULL,
    /* tc_filter */   t_skip_filter,
    /* tc_ref_set */  NULL,
    /* tc_scan */     NULL,
  }};

966 967
tag_typedef_t tag_skip = TAG_TYPEDEF(tag_skip, skip);

Pekka Pessi's avatar
Pekka Pessi committed
968 969 970
/* ====================================================================== */
/* next tag - jump to next tag list */

Pekka Pessi's avatar
Pekka Pessi committed
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
tagi_t const *t_next_next(tagi_t const *t)
{
  return (tagi_t *)(t->t_value);
}

tagi_t *t_next_move(tagi_t *dst, tagi_t const *src)
{
  if (!src->t_value)
    return t_null_move(dst, src);
  return dst;
}

size_t t_next_len(tagi_t const *t)
{
  if (!t->t_value)
    return sizeof(*t);
  return 0;
}

tagi_t *t_next_dup(tagi_t *dst, tagi_t const *src, void **bb)
{
  if (!src->t_value)
    return t_null_dup(dst, src, bb);
  return dst;
}

Pekka Pessi's avatar
Pekka Pessi committed
997 998 999 1000 1001 1002 1003 1004
tagi_t *t_next_filter(tagi_t *dst, 
		    tagi_t const filter[], 
		    tagi_t const *src, 
		    void **bb)
{
  return dst;
}

Pekka Pessi's avatar
Pekka Pessi committed
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
tag_class_t next_tag_class[1] = 
  {{
    sizeof(next_tag_class),
    /* tc_next */     t_next_next,
    /* tc_len */      t_next_len,
    /* tc_move */     t_next_move,
    /* tc_xtra */     NULL,
    /* tc_dup */      t_next_dup,
    /* tc_free */     NULL,
    /* tc_find */     t_null_find,
    /* tc_snprintf */ NULL,
    /* tc_filter */   t_next_filter,
    /* tc_ref_set */  NULL,
    /* tc_scan */     NULL,
  }};

1021 1022
tag_typedef_t tag_next = TAG_TYPEDEF(tag_next, next);

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
/* ====================================================================== */
/* filter tag  - use function to filter tag */

tagi_t *t_filter_with(tagi_t *dst,
		      tagi_t const *t, 
		      tagi_t const *src, 
		      void **bb)
{
  tag_filter_f *function;

  if (!src || !t)
    return dst;

  function = (tag_filter_f *)t->t_value;

  if (!function || !function(t, src))
    return dst;

  if (dst) {
    return t_dup(dst, src, bb); 
  }
  else {
    dst = (tagi_t *)((char *)dst + t_len(src));
    *bb = (char *)*bb + t_xtra(src, (size_t)*bb);
    return dst;
  }
}
		   
tag_class_t filter_tag_class[1] = 
  {{
    sizeof(filter_tag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     NULL,
    /* tc_dup */      NULL,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ NULL,
    /* tc_filter */   t_filter_with,
    /* tc_ref_set */  NULL,
    /* tc_scan */     NULL,
  }};

/** Filter tag - apply function in order to filter tag. */
tag_typedef_t tag_filter = TAG_TYPEDEF(tag_filter, filter);

Pekka Pessi's avatar
Pekka Pessi committed
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
/* ====================================================================== */
/* any tag - match to any tag when filtering */

tagi_t *t_any_filter(tagi_t *dst,
		     tagi_t const filter[], 
		     tagi_t const *src, 
		     void **bb)
{
  if (!src)
    return dst;
  else if (dst) {
    return t_dup(dst, src, bb); 
  }
  else {
    dst = (tagi_t *)((char *)dst + t_len(src));
    *bb = (char *)*bb + t_xtra(src, (size_t)*bb);
    return dst;
  }
}
		   
Pekka Pessi's avatar
Pekka Pessi committed
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
tag_class_t any_tag_class[1] = 
  {{
    sizeof(any_tag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     NULL,
    /* tc_dup */      NULL,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ NULL,
    /* tc_filter */   t_any_filter,
    /* tc_ref_set */  NULL,
    /* tc_scan */     NULL,
  }};

1106
/** Any tag - match any tag when filtering. */
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
tag_typedef_t tag_any = TAG_TYPEDEF(tag_any, any);

/* ====================================================================== */
/* ns tag - match to any tag with same namespace when filtering */

static
tagi_t *t_ns_filter(tagi_t *dst,
		    tagi_t const filter[], 
		    tagi_t const *src, 
		    void **bb)
{
  char const *match, *ns;

  if (!src)
    return dst;

  assert(filter);

  match = TAG_TYPE_OF(filter)->tt_ns;
  ns = TAG_TYPE_OF(src)->tt_ns;

  if (match == NULL)
    /* everything matches with this */;
  else if (match == ns)
    /* namespaces matche */;
  else if (ns == NULL)
    /* no match */
    return dst;
  else if (strcmp(match, ns))
    /* no match */
    return dst;

  if (dst) {
    return t_dup(dst, src, bb); 
  }
  else {
    dst = (tagi_t *)((char *)dst + t_len(src));
    *bb = (char *)*bb + t_xtra(src, (size_t)*bb);
    return dst;
  }
}
1148 1149

/** Namespace filtering class */		   
1150 1151
tag_class_t ns_tag_class[1] = 
  {{
1152
    sizeof(ns_tag_class),
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     NULL,
    /* tc_dup */      NULL,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ NULL,
    /* tc_filter */   t_ns_filter,
    /* tc_ref_set */  NULL,
    /* tc_scan */     NULL,
  }};

/* ====================================================================== */
/* int tag - pass integer value */

Pekka Pessi's avatar
Pekka Pessi committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
int t_int_snprintf(tagi_t const *t, char b[], size_t size)
{
  return snprintf(b, size, "%i", (int)t->t_value);
}

int t_int_ref_set(tag_type_t tt, void *ref, tagi_t const value[])
{
  *(int *)ref = (int)value->t_value;

  return 1;
}

int t_int_scan(tag_type_t tt, su_home_t *home, 
	       char const *s, 
	       tag_value_t *return_value)
{
  int value;
  char *rest;

  value = strtol(s, &rest, 0);
  
  if (s != rest) {
    *return_value = (tag_value_t)value;
    return 1;
  }
  else {
    *return_value = (tag_value_t)0;
    return -1;
  }
}

tag_class_t int_tag_class[1] = 
  {{
    sizeof(int_tag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     NULL,
    /* tc_dup */      NULL,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ t_int_snprintf,
    /* tc_filter */   NULL,
    /* tc_ref_set */  t_int_ref_set,
    /* tc_scan */     t_int_scan,
  }};

1216 1217
/* ====================================================================== */
/* uint tag - pass unsigned integer value */
Pekka Pessi's avatar
Pekka Pessi committed
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266

int t_uint_snprintf(tagi_t const *t, char b[], size_t size)
{
  return snprintf(b, size, "%u", (unsigned)t->t_value);
}

int t_uint_ref_set(tag_type_t tt, void *ref, tagi_t const value[])
{
  *(unsigned *)ref = (unsigned)value->t_value;

  return 1;
}

int t_uint_scan(tag_type_t tt, su_home_t *home, 
	       char const *s, 
	       tag_value_t *return_value)
{
  unsigned value;
  char *rest;

  value = strtoul(s, &rest, 0);
  
  if (s != rest) {
    *return_value = (tag_value_t)value;
    return 1;
  }
  else {
    *return_value = (tag_value_t)0;
    return -1;
  }
}

tag_class_t uint_tag_class[1] = 
  {{
    sizeof(int_tag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     NULL,
    /* tc_dup */      NULL,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ t_uint_snprintf,
    /* tc_filter */   NULL,
    /* tc_ref_set */  t_uint_ref_set,
    /* tc_scan */     t_uint_scan,
  }};


1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
/* ====================================================================== */
/* size tag - pass size_t value @NEW_1_12_5 */

static
int t_size_snprintf(tagi_t const *t, char b[], size_t size)
{
  return snprintf(b, size, MOD_ZU, (size_t)t->t_value);
}

static
int t_size_ref_set(tag_type_t tt, void *ref, tagi_t const value[])
{
  *(size_t *)ref = (size_t)value->t_value;

  return 1;
}

static
int t_size_scan(tag_type_t tt, su_home_t *home, 
		 char const *s, 
		 tag_value_t *return_value)
{
  unsigned longlong value;
  char *rest;

  value = strtoull(s, &rest, 0);
  
  if (s != rest && value <= SIZE_MAX) {
    *return_value = (tag_value_t)value;
    return 1;
  }
  else {
    *return_value = (tag_value_t)0;
    return -1;
  }
}

Pekka Pessi's avatar
Pekka Pessi committed
1304
/** Tag class for tags with size_t value. @NEW_1_12_5. */
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
tag_class_t size_tag_class[1] = 
  {{
    sizeof(int_tag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     NULL,
    /* tc_dup */      NULL,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ t_size_snprintf,
    /* tc_filter */   NULL,
    /* tc_ref_set */  t_size_ref_set,
    /* tc_scan */     t_size_scan,
  }};

/* ====================================================================== */
/* usize tag - pass usize_t value */

static
int t_usize_ref_set(tag_type_t tt, void *ref, tagi_t const value[])
{
  *(usize_t *)ref = (usize_t)value->t_value;

  return 1;
}

static
int t_usize_scan(tag_type_t tt, su_home_t *home, 
		 char const *s, 
		 tag_value_t *return_value)
{
  unsigned longlong value;
  char *rest;

  value = strtoull(s, &rest, 0);
  
  if (s != rest && value <= USIZE_MAX) {
    *return_value = (tag_value_t)value;
    return 1;
  }
  else {
    *return_value = (tag_value_t)0;
    return -1;
  }
}

Pekka Pessi's avatar
Pekka Pessi committed
1352
/** Tag class for tags with usize_t value. @NEW_1_12_5. */
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
tag_class_t usize_tag_class[1] = 
  {{
    sizeof(int_tag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     NULL,
    /* tc_dup */      NULL,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ t_size_snprintf,
    /* tc_filter */   NULL,
    /* tc_ref_set */  t_usize_ref_set,
    /* tc_scan */     t_usize_scan,
  }};


1370 1371 1372
/* ====================================================================== */
/* bool tag - pass boolean value */

Pekka Pessi's avatar
Pekka Pessi committed
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
int t_bool_snprintf(tagi_t const *t, char b[], size_t size)
{
  return snprintf(b, size, "%s", t->t_value ? "true" : "false");
}

int t_bool_ref_set(tag_type_t tt, void *ref, tagi_t const value[])
{
  *(int *)ref = (value->t_value != 0);

  return 1;
}

int t_bool_scan(tag_type_t tt, su_home_t *home, 
	       char const *s, 
	       tag_value_t *return_value)
{
  int retval;
  int value = 0;

  if (strncasecmp(s, "true", 4) == 0 
      && strlen(s + 4) == strspn(s + 4, " \t\r\n")) {
    value = 1, retval = 1;
  } else if (strncasecmp(s, "false", 5) == 0 
	     && strlen(s + 5) == strspn(s + 5, " \t\r\n")) {
    value = 0, retval = 1;
  } else {
    retval = t_int_scan(tt, home, s, return_value);
    value = *return_value != 0;
  }

  if (retval == 1)
    *return_value = (tag_value_t)value;
  else
    *return_value = (tag_value_t)0;
  
  return retval;
}

tag_class_t bool_tag_class[1] = 
  {{
    sizeof(bool_tag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     NULL,
    /* tc_dup */      NULL,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ t_bool_snprintf,
    /* tc_filter */   NULL,
    /* tc_ref_set */  t_bool_ref_set,
    /* tc_scan */     t_bool_scan,
  }};

1427 1428 1429
/* ====================================================================== */
/* ptr tag - pass pointer value */

Pekka Pessi's avatar
Pekka Pessi committed
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
int t_ptr_snprintf(tagi_t const *t, char b[], size_t size)
{
  return snprintf(b, size, "%p", (void *)t->t_value);
}

int t_ptr_ref_set(tag_type_t tt, void *ref, tagi_t const value[])
{
  *(void **)ref = (void *)value->t_value;

  return 1;
}

/* This is not usually very safe, so it is not used */
int t_ptr_scan(tag_type_t tt, su_home_t *home, 
	       char const *s, 
	       tag_value_t *return_value)
{
  int retval;
  void *ptr;

  retval = sscanf(s, "%p", &ptr);

  if (retval == 1)
    *return_value = (tag_value_t)ptr;
  else
    *return_value = (tag_value_t)NULL;
  
  return retval;
}

tag_class_t ptr_tag_class[1] = 
  {{
    sizeof(ptr_tag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     NULL,
    /* tc_dup */      NULL,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ t_ptr_snprintf,
    /* tc_filter */   NULL,
    /* tc_ref_set */  t_ptr_ref_set,
    /* tc_scan */     NULL,
  }};

1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
/* ====================================================================== */
/* socket tag - pass socket */

#include <sofia-sip/su.h>

int t_socket_snprintf(tagi_t const *t, char b[], size_t size)
{
  /* socket can be int or DWORD (or QWORD on win64?) */
  return snprintf(b, size, LLI, (longlong)t->t_value);
}

int t_socket_ref_set(tag_type_t tt, void *ref, tagi_t const value[])
{
  *(su_socket_t *)ref = (su_socket_t)value->t_value;

  return 1;
}

tag_class_t socket_tag_class[1] = 
  {{
    sizeof(socket_tag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     NULL,
    /* tc_dup */      NULL,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ t_socket_snprintf,
    /* tc_filter */   NULL,
    /* tc_ref_set */  t_socket_ref_set,
    /* tc_scan */     NULL,
  }};

1510 1511 1512
/* ====================================================================== */
/* str tag - pass string value */

Pekka Pessi's avatar
Pekka Pessi committed
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
int t_str_snprintf(tagi_t const *t, char b[], size_t size)
{
  if (t->t_value) 
    return snprintf(b, size, "\"%s\"", (char const *)t->t_value);
  else
    return snprintf(b, size, "<null>");
}

int t_str_scan(tag_type_t tt, su_home_t *home, 
	       char const *s, 
	       tag_value_t *return_value)
{
  int retval;

  s = su_strdup(home, s);
    
  if (s)
    *return_value = (tag_value_t)s, retval = 1;
  else
    *return_value = (tag_value_t)NULL, retval = -1;
  
  return retval;
}

tagi_t *t_str_dup(tagi_t *dst, tagi_t const *src, void **bb)
{
  dst->t_tag = src->t_tag;
  if (src->t_value) {
    char const *s = (char const *)src->t_value;
1542
    size_t len = strlen(s) + 1;
Pekka Pessi's avatar
Pekka Pessi committed
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
    dst->t_value = (tag_value_t)strcpy(*bb, s); 
    *bb = (char *)*bb + len;
  }
  else 
    dst->t_value = (tag_value_t)0;

  return dst + 1;
}

size_t t_str_xtra(tagi_t const *t, size_t offset)
{
  return t->t_value ? strlen((char *)t->t_value) + 1 : 0;
}

tag_class_t str_tag_class[1] = 
  {{
    sizeof(str_tag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     t_str_xtra,
    /* tc_dup */      t_str_dup,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ t_str_snprintf,
    /* tc_filter */   NULL,
    /* tc_ref_set */  t_ptr_ref_set,
    /* tc_scan */     t_str_scan,
  }};

1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
/* ====================================================================== */
/* cstr tag - pass constant string value (no need to dup) */

/** Tag class for constant strings */
tag_class_t cstr_tag_class[1] = 
  {{
    sizeof(cstr_tag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     NULL,
    /* tc_dup */      NULL,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ t_str_snprintf,
    /* tc_filter */   NULL,
    /* tc_ref_set */  t_ptr_ref_set,
    /* tc_scan */     t_str_scan,
  }};

/* ====================================================================== */
/* ref tag - pass reference */

Pekka Pessi's avatar
Pekka Pessi committed
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
tag_class_t ref_tag_class[1] = 
  {{
    sizeof(ref_tag_class),
    /* tc_next */     NULL,
    /* tc_len */      NULL,
    /* tc_move */     NULL,
    /* tc_xtra */     NULL,
    /* tc_dup */      NULL,
    /* tc_free */     NULL,
    /* tc_find */     NULL,
    /* tc_snprintf */ t_ptr_snprintf,
    /* tc_filter */   NULL,
    /* tc_ref_set */  t_ptr_ref_set,
    /* tc_scan */     NULL,
  }};
1611 1612