You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fstring.h 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef FSTRING_H
  17. #define FSTRING_H
  18. #include "config.h"
  19. #include "mem_pool.h"
  20. #include <unicode/uchar.h>
  21. /**
  22. * Fixed strings library
  23. * These strings are NOT null-terminated for speed
  24. */
  25. typedef struct f_str_s {
  26. gsize len;
  27. gsize allocated;
  28. gchar str[];
  29. } rspamd_fstring_t;
  30. #define RSPAMD_FSTRING_DATA(s) ((s)->str)
  31. #define RSPAMD_FSTRING_LEN(s) ((s)->len)
  32. typedef struct f_str_tok {
  33. gsize len;
  34. const gchar *begin;
  35. } rspamd_ftok_t;
  36. typedef struct f_str_unicode_tok {
  37. gsize len; /* in UChar32 */
  38. const UChar32 *begin;
  39. } rspamd_ftok_unicode_t;
  40. /**
  41. * Create new fixed length string
  42. */
  43. rspamd_fstring_t* rspamd_fstring_new (void)
  44. G_GNUC_WARN_UNUSED_RESULT;
  45. /**
  46. * Create new fixed length string with preallocated size
  47. */
  48. rspamd_fstring_t *rspamd_fstring_sized_new (gsize initial_size)
  49. G_GNUC_WARN_UNUSED_RESULT;
  50. /**
  51. * Create new fixed length string and initialize it with the initial data
  52. */
  53. rspamd_fstring_t *rspamd_fstring_new_init (const gchar *init, gsize len)
  54. G_GNUC_WARN_UNUSED_RESULT;
  55. /**
  56. * Assign new value to fixed string
  57. */
  58. rspamd_fstring_t *rspamd_fstring_assign (rspamd_fstring_t *str,
  59. const gchar *init, gsize len) G_GNUC_WARN_UNUSED_RESULT;
  60. /**
  61. * Free fixed length string
  62. */
  63. void rspamd_fstring_free (rspamd_fstring_t *str);
  64. /**
  65. * Append data to a fixed length string
  66. */
  67. rspamd_fstring_t* rspamd_fstring_append (rspamd_fstring_t *str,
  68. const char *in, gsize len) G_GNUC_WARN_UNUSED_RESULT;
  69. /**
  70. * Append `len` repeated chars `c` to string `str`
  71. */
  72. rspamd_fstring_t *rspamd_fstring_append_chars (rspamd_fstring_t *str,
  73. char c, gsize len) G_GNUC_WARN_UNUSED_RESULT;
  74. /**
  75. * Erase `len` characters at position `pos`
  76. */
  77. void rspamd_fstring_erase (rspamd_fstring_t *str, gsize pos, gsize len);
  78. #define rspamd_fstring_clear(s) rspamd_fstring_erase(s, 0, s->len)
  79. /**
  80. * Convert fixed string to a zero terminated string. This string must be
  81. * freed by a caller
  82. */
  83. char * rspamd_fstring_cstr (const rspamd_fstring_t *str)
  84. G_GNUC_WARN_UNUSED_RESULT;
  85. /**
  86. * Convert fixed string usign ftok_t to a zero terminated string. This string must be
  87. * freed by a caller
  88. */
  89. char * rspamd_ftok_cstr (const rspamd_ftok_t *str)
  90. G_GNUC_WARN_UNUSED_RESULT;
  91. /*
  92. * Return fast hash value for fixed string converted to lowercase
  93. */
  94. guint32 rspamd_fstrhash_lc (const rspamd_ftok_t *str, gboolean is_utf);
  95. /**
  96. * Return true if two strings are equal
  97. */
  98. gboolean rspamd_fstring_equal (const rspamd_fstring_t *s1,
  99. const rspamd_fstring_t *s2);
  100. /**
  101. * Compare two fixed strings ignoring case
  102. */
  103. gint rspamd_fstring_casecmp (const rspamd_fstring_t *s1,
  104. const rspamd_fstring_t *s2);
  105. /**
  106. * Compare two fixed strings
  107. */
  108. gint rspamd_fstring_cmp (const rspamd_fstring_t *s1,
  109. const rspamd_fstring_t *s2);
  110. /**
  111. * Compare two fixed tokens ignoring case
  112. */
  113. gint rspamd_ftok_casecmp (const rspamd_ftok_t *s1,
  114. const rspamd_ftok_t *s2);
  115. /**
  116. * Compare two fixed tokens
  117. */
  118. gint rspamd_ftok_cmp (const rspamd_ftok_t *s1,
  119. const rspamd_ftok_t *s2);
  120. /**
  121. * Returns true if `s1` starts with `s2`
  122. * @param s1
  123. * @param s2
  124. * @return
  125. */
  126. gboolean rspamd_ftok_starts_with (const rspamd_ftok_t *s1,
  127. const rspamd_ftok_t *s2);
  128. /**
  129. * Return TRUE if ftok is equal to specified C string
  130. */
  131. gboolean rspamd_ftok_cstr_equal (const rspamd_ftok_t *s,
  132. const gchar *pat, gboolean icase);
  133. /**
  134. * Free fstring_t that is mapped to ftok_t
  135. *
  136. * | len | allocated | <data> -- fstring_t
  137. * <begin> -- tok
  138. *
  139. * tok is expected to be allocated with g_malloc
  140. */
  141. void rspamd_fstring_mapped_ftok_free (gpointer p);
  142. /**
  143. * Map token to a specified string. Token must be freed using g_free
  144. */
  145. rspamd_ftok_t *rspamd_ftok_map (const rspamd_fstring_t *s);
  146. /**
  147. * Suggest suitable size to grow fstring
  148. * @param len
  149. * @param allocated
  150. * @param needed_len
  151. * @return
  152. */
  153. gsize rspamd_fstring_suggest_size (gsize len, gsize allocated, gsize needed_len);
  154. /**
  155. * Grow the specified fixed string
  156. * @param str
  157. * @param needed_len
  158. * @return
  159. */
  160. rspamd_fstring_t * rspamd_fstring_grow (rspamd_fstring_t *str,
  161. gsize needed_len) G_GNUC_WARN_UNUSED_RESULT;
  162. /**
  163. * Copies ftok to zero terminated string (must be freed using g_free)
  164. * @param src
  165. * @return
  166. */
  167. gchar *rspamd_ftokdup (const rspamd_ftok_t *src) G_GNUC_WARN_UNUSED_RESULT;
  168. /**
  169. * Copies fstring to zero terminated string (must be freed using g_free)
  170. * @param src
  171. * @return
  172. */
  173. gchar *rspamd_fstringdup (const rspamd_fstring_t *src) G_GNUC_WARN_UNUSED_RESULT;
  174. #define RSPAMD_FTOK_ASSIGN(t, lit) do { (t)->begin = (lit); (t)->len = sizeof(lit) - 1; } while (0)
  175. #define RSPAMD_FTOK_FROM_STR(t, str) do { \
  176. if (G_LIKELY(str)) { \
  177. (t)->begin = (const char*)(str); \
  178. (t)->len = strlen (str); \
  179. } \
  180. else { \
  181. (t)->begin = NULL; \
  182. (t)->len = 0; \
  183. } \
  184. } while (0)
  185. #endif