url_tag.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@CFILE url_tag.c URL Tag classes
  25. *
  26. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  27. *
  28. * @date Created: Wed Feb 21 10:15:20 2001 ppessi
  29. */
  30. #include "config.h"
  31. #define TAG_NAMESPACE "url"
  32. #include <sofia-sip/su.h>
  33. #include <sofia-sip/url_tag.h>
  34. #include <sofia-sip/url_tag_class.h>
  35. #include <sofia-sip/su_tag_class.h>
  36. #include <sofia-sip/url.h>
  37. #include <string.h>
  38. tag_typedef_t urltag_any = NSTAG_TYPEDEF(*);
  39. tag_typedef_t urltag_url = URLTAG_TYPEDEF(url);
  40. int urltag_snprintf(tagi_t const *t, char b[], size_t size)
  41. {
  42. url_string_t const *us = (url_string_t const *)t->t_value;
  43. if (us == NULL)
  44. return snprintf(b, size, "<null>");
  45. if (URL_STRING_P(us))
  46. return snprintf(b, size, "<%s>", us->us_str);
  47. else
  48. return snprintf(b, size, "<" URL_PRINT_FORMAT ">",
  49. URL_PRINT_ARGS(us->us_url));
  50. }
  51. size_t urltag_xtra(tagi_t const *t, size_t offset)
  52. {
  53. url_t const *url = (url_t const *)t->t_value;
  54. if (url == NULL || url == (url_t *)-1)
  55. return 0;
  56. else if (URL_STRING_P(url))
  57. return t_str_xtra(t, offset);
  58. else
  59. return SU_ALIGN(offset) + sizeof(*url) + url_xtra(url);
  60. }
  61. tagi_t *urltag_dup(tagi_t *dst, tagi_t const *src, void **bb)
  62. {
  63. url_t const *url = (url_t const *)src->t_value;
  64. if (url == NULL || url == (url_t *)-1) {
  65. dst->t_tag = src->t_tag;
  66. dst->t_value = src->t_value;
  67. }
  68. else if (URL_STRING_P(url)) {
  69. return t_str_dup(dst, src, bb);
  70. } else {
  71. size_t xtra = url_xtra(url);
  72. char *b = *bb;
  73. url_t *d;
  74. b += SU_ALIGN(b);
  75. d = (url_t *)b;
  76. url_dup(b + sizeof(*d), xtra, d, url);
  77. dst->t_tag = src->t_tag;
  78. dst->t_value = (tag_value_t)d;
  79. *bb = b + sizeof(*d) + xtra;
  80. }
  81. return dst + 1;
  82. }
  83. #define IS_EXCLUDED(u) \
  84. (u <= ' ' \
  85. || u >= '\177' \
  86. || (u < 64 ? (0xb400000aU & (1 << (63 - u))) \
  87. : (u < 96 ? (0x0000001eU & (1 << (95 - u))) \
  88. : /*u < 128*/ (0x8000001dU & (1 << (127 - u))))) != 0)
  89. /** Tag function used to convert a string to a URL tag value.
  90. *
  91. * @param tt tag type
  92. * @param home memory home used to allocate new #url_t structure
  93. * @param str string to convert
  94. * @param return_value return-value parameter for converted url
  95. *
  96. * @retval 0 when successful
  97. * @retval -1 upon an error
  98. *
  99. * @since New in @VERSION_1_12_2.
  100. */
  101. int urltag_scan(tag_type_t tt, su_home_t *home,
  102. char const *str,
  103. tag_value_t *return_value)
  104. {
  105. size_t len;
  106. url_t *url;
  107. char *s;
  108. (void)tt;
  109. for (len = 0; !IS_EXCLUDED(str[len]); len++)
  110. ;
  111. url = su_alloc(home, (sizeof *url) + len + 1);
  112. if (!url)
  113. return -1;
  114. s = memcpy((char *)(url + 1), str, len);
  115. s[len] = 0;
  116. if (url_d(url, s) < 0)
  117. return (void)su_free(home, url), -1;
  118. *return_value = (tag_value_t)url;
  119. return 0;
  120. }
  121. tag_class_t url_tag_class[1] =
  122. {{
  123. sizeof(url_tag_class),
  124. /* tc_next */ NULL,
  125. /* tc_len */ NULL,
  126. /* tc_move */ NULL,
  127. /* tc_xtra */ urltag_xtra,
  128. /* tc_dup */ urltag_dup,
  129. /* tc_free */ NULL,
  130. /* tc_find */ NULL,
  131. /* tc_snprintf */ urltag_snprintf,
  132. /* tc_filter */ NULL,
  133. /* tc_ref_set */ t_ptr_ref_set,
  134. /* tc_scan */ urltag_scan,
  135. }};