utility.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* iksemel (XML parser for Jabber)
  2. ** Copyright (C) 2000-2003 Gurer Ozen <madcat@e-kolay.net>
  3. ** This code is free software; you can redistribute it and/or
  4. ** modify it under the terms of GNU Lesser General Public License.
  5. */
  6. #include "common.h"
  7. #include "iksemel.h"
  8. /***** malloc wrapper *****/
  9. static void *(*my_malloc_func)(size_t size);
  10. static void (*my_free_func)(void *ptr);
  11. void *
  12. iks_malloc (size_t size)
  13. {
  14. if (my_malloc_func)
  15. return my_malloc_func (size);
  16. else
  17. return malloc (size);
  18. }
  19. void
  20. iks_real_free (void *ptr)
  21. {
  22. if (my_free_func)
  23. my_free_func (ptr);
  24. else
  25. free (ptr);
  26. }
  27. void
  28. iks_set_mem_funcs (void *(*malloc_func)(size_t size), void (*free_func)(void *ptr))
  29. {
  30. my_malloc_func = malloc_func;
  31. my_free_func = free_func;
  32. }
  33. /***** NULL-safe Functions *****/
  34. char *
  35. iks_strdup (const char *src)
  36. {
  37. if (src) return strdup(src);
  38. return NULL;
  39. }
  40. char *
  41. iks_strcat (char *dest, const char *src)
  42. {
  43. size_t len;
  44. if (!src) return dest;
  45. len = strlen (src);
  46. memcpy (dest, src, len);
  47. dest[len] = '\0';
  48. return dest + len;
  49. }
  50. int
  51. iks_strcmp (const char *a, const char *b)
  52. {
  53. if (!a || !b) return -1;
  54. return strcmp (a, b);
  55. }
  56. int
  57. iks_strcasecmp (const char *a, const char *b)
  58. {
  59. if (!a || !b) return -1;
  60. return strcasecmp (a, b);
  61. }
  62. int
  63. iks_strncmp (const char *a, const char *b, size_t n)
  64. {
  65. if (!a || !b) return -1;
  66. return strncmp (a, b, n);
  67. }
  68. int
  69. iks_strncasecmp (const char *a, const char *b, size_t n)
  70. {
  71. if (!a || !b) return -1;
  72. return strncasecmp (a, b, n);
  73. }
  74. size_t
  75. iks_strlen (const char *src)
  76. {
  77. if (!src) return 0;
  78. return strlen (src);
  79. }
  80. /***** XML Escaping *****/
  81. char *
  82. iks_escape (ikstack *s, char *src, size_t len)
  83. {
  84. char *ret;
  85. int i, j, nlen;
  86. if (!src || !s) return NULL;
  87. if (len == -1) len = strlen (src);
  88. nlen = len;
  89. for (i=0; i<len; i++) {
  90. switch (src[i]) {
  91. case '&': nlen += 4; break;
  92. case '<': nlen += 3; break;
  93. case '>': nlen += 3; break;
  94. case '\'': nlen += 5; break;
  95. case '"': nlen += 5; break;
  96. }
  97. }
  98. if (len == nlen) return src;
  99. ret = iks_stack_alloc (s, nlen + 1);
  100. if (!ret) return NULL;
  101. for (i=j=0; i<len; i++) {
  102. switch (src[i]) {
  103. case '&': memcpy (&ret[j], "&amp;", 5); j += 5; break;
  104. case '\'': memcpy (&ret[j], "&apos;", 6); j += 6; break;
  105. case '"': memcpy (&ret[j], "&quot;", 6); j += 6; break;
  106. case '<': memcpy (&ret[j], "&lt;", 4); j += 4; break;
  107. case '>': memcpy (&ret[j], "&gt;", 4); j += 4; break;
  108. default: ret[j++] = src[i];
  109. }
  110. }
  111. ret[j] = '\0';
  112. return ret;
  113. }
  114. char *
  115. iks_unescape (ikstack *s, char *src, size_t len)
  116. {
  117. int i,j;
  118. char *ret;
  119. if (!s || !src) return NULL;
  120. if (!strchr (src, '&')) return src;
  121. if (len == -1) len = strlen (src);
  122. ret = iks_stack_alloc (s, len + 1);
  123. if (!ret) return NULL;
  124. for (i=j=0; i<len; i++) {
  125. if (src[i] == '&') {
  126. i++;
  127. if (strncmp (&src[i], "amp;", 4) == 0) {
  128. ret[j] = '&';
  129. i += 3;
  130. } else if (strncmp (&src[i], "quot;", 5) == 0) {
  131. ret[j] = '"';
  132. i += 4;
  133. } else if (strncmp (&src[i], "apos;", 5) == 0) {
  134. ret[j] = '\'';
  135. i += 4;
  136. } else if (strncmp (&src[i], "lt;", 3) == 0) {
  137. ret[j] = '<';
  138. i += 2;
  139. } else if (strncmp (&src[i], "gt;", 3) == 0) {
  140. ret[j] = '>';
  141. i += 2;
  142. } else {
  143. ret[j] = src[--i];
  144. }
  145. } else {
  146. ret[j] = src[i];
  147. }
  148. j++;
  149. }
  150. ret[j] = '\0';
  151. return ret;
  152. }