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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**
  25. * Fixed strings library
  26. * These strings are NOT null-terminated for speed
  27. */
  28. typedef struct f_str_s {
  29. gsize len;
  30. gsize allocated;
  31. char str[];
  32. } rspamd_fstring_t;
  33. #define RSPAMD_FSTRING_DATA(s) ((s)->str)
  34. #define RSPAMD_FSTRING_LEN(s) ((s)->len)
  35. #define RSPAMD_FSTRING_LIT(lit) rspamd_fstring_new_init((lit), sizeof(lit) - 1)
  36. typedef struct f_str_tok {
  37. gsize len;
  38. const char *begin;
  39. } rspamd_ftok_t;
  40. typedef struct f_str_unicode_tok {
  41. gsize len; /* in UChar32 */
  42. const UChar32 *begin;
  43. } rspamd_ftok_unicode_t;
  44. /**
  45. * Create new fixed length string
  46. */
  47. rspamd_fstring_t *rspamd_fstring_new(void)
  48. G_GNUC_WARN_UNUSED_RESULT;
  49. /**
  50. * Create new fixed length string with preallocated size
  51. */
  52. rspamd_fstring_t *rspamd_fstring_sized_new(gsize initial_size)
  53. G_GNUC_WARN_UNUSED_RESULT;
  54. /**
  55. * Create new fixed length string and initialize it with the initial data
  56. */
  57. rspamd_fstring_t *rspamd_fstring_new_init(const char *init, gsize len)
  58. G_GNUC_WARN_UNUSED_RESULT;
  59. /**
  60. * Assign new value to fixed string
  61. */
  62. rspamd_fstring_t *rspamd_fstring_assign(rspamd_fstring_t *str,
  63. const char *init, gsize len) G_GNUC_WARN_UNUSED_RESULT;
  64. /**
  65. * Free fixed length string
  66. */
  67. void rspamd_fstring_free(rspamd_fstring_t *str);
  68. /**
  69. * Append data to a fixed length string
  70. */
  71. rspamd_fstring_t *rspamd_fstring_append(rspamd_fstring_t *str,
  72. const char *in, gsize len) G_GNUC_WARN_UNUSED_RESULT;
  73. /**
  74. * Append `len` repeated chars `c` to string `str`
  75. */
  76. rspamd_fstring_t *rspamd_fstring_append_chars(rspamd_fstring_t *str,
  77. char c, gsize len) G_GNUC_WARN_UNUSED_RESULT;
  78. /**
  79. * Erase `len` characters at position `pos`
  80. */
  81. void rspamd_fstring_erase(rspamd_fstring_t *str, gsize pos, gsize len);
  82. #define rspamd_fstring_clear(s) rspamd_fstring_erase(s, 0, s->len)
  83. /**
  84. * Convert fixed string to a zero terminated string. This string must be
  85. * freed by a caller
  86. */
  87. char *rspamd_fstring_cstr(const rspamd_fstring_t *str)
  88. G_GNUC_WARN_UNUSED_RESULT;
  89. /**
  90. * Convert fixed string usign ftok_t to a zero terminated string. This string must be
  91. * freed by a caller
  92. */
  93. char *rspamd_ftok_cstr(const rspamd_ftok_t *str)
  94. G_GNUC_WARN_UNUSED_RESULT;
  95. /*
  96. * Return fast hash value for fixed string converted to lowercase
  97. */
  98. uint32_t rspamd_fstrhash_lc(const rspamd_ftok_t *str, gboolean is_utf);
  99. /**
  100. * Return true if two strings are equal
  101. */
  102. gboolean rspamd_fstring_equal(const rspamd_fstring_t *s1,
  103. const rspamd_fstring_t *s2);
  104. /**
  105. * Compare two fixed strings ignoring case
  106. */
  107. int rspamd_fstring_casecmp(const rspamd_fstring_t *s1,
  108. const rspamd_fstring_t *s2);
  109. /**
  110. * Compare two fixed strings
  111. */
  112. int rspamd_fstring_cmp(const rspamd_fstring_t *s1,
  113. const rspamd_fstring_t *s2);
  114. /**
  115. * Compare two fixed tokens ignoring case
  116. */
  117. int rspamd_ftok_casecmp(const rspamd_ftok_t *s1,
  118. const rspamd_ftok_t *s2);
  119. /**
  120. * Compare two fixed tokens
  121. */
  122. int rspamd_ftok_cmp(const rspamd_ftok_t *s1,
  123. const rspamd_ftok_t *s2);
  124. /**
  125. * Returns true if `s1` starts with `s2`
  126. * @param s1
  127. * @param s2
  128. * @return
  129. */
  130. gboolean rspamd_ftok_starts_with(const rspamd_ftok_t *s1,
  131. const rspamd_ftok_t *s2);
  132. /**
  133. * Return TRUE if ftok is equal to specified C string
  134. */
  135. gboolean rspamd_ftok_cstr_equal(const rspamd_ftok_t *s,
  136. const char *pat, gboolean icase);
  137. /**
  138. * Free fstring_t that is mapped to ftok_t
  139. *
  140. * | len | allocated | <data> -- fstring_t
  141. * <begin> -- tok
  142. *
  143. * tok is expected to be allocated with g_malloc
  144. */
  145. void rspamd_fstring_mapped_ftok_free(gpointer p);
  146. /**
  147. * Map token to a specified string. Token must be freed using g_free
  148. */
  149. rspamd_ftok_t *rspamd_ftok_map(const rspamd_fstring_t *s);
  150. /**
  151. * Suggest suitable size to grow fstring
  152. * @param len
  153. * @param allocated
  154. * @param needed_len
  155. * @return
  156. */
  157. gsize rspamd_fstring_suggest_size(gsize len, gsize allocated, gsize needed_len);
  158. /**
  159. * Grow the specified fixed string
  160. * @param str
  161. * @param needed_len
  162. * @return
  163. */
  164. rspamd_fstring_t *rspamd_fstring_grow(rspamd_fstring_t *str,
  165. gsize needed_len) G_GNUC_WARN_UNUSED_RESULT;
  166. /**
  167. * Copies ftok to zero terminated string (must be freed using g_free)
  168. * @param src
  169. * @return
  170. */
  171. char *rspamd_ftokdup(const rspamd_ftok_t *src) G_GNUC_WARN_UNUSED_RESULT;
  172. /**
  173. * Copies fstring to zero terminated string (must be freed using g_free)
  174. * @param src
  175. * @return
  176. */
  177. char *rspamd_fstringdup(const rspamd_fstring_t *src) G_GNUC_WARN_UNUSED_RESULT;
  178. #define RSPAMD_FTOK_ASSIGN(t, lit) \
  179. do { \
  180. (t)->begin = (lit); \
  181. (t)->len = sizeof(lit) - 1; \
  182. } while (0)
  183. #define RSPAMD_FTOK_FROM_STR(t, str) \
  184. do { \
  185. if (G_LIKELY(str)) { \
  186. (t)->begin = (const char *) (str); \
  187. (t)->len = strlen(str); \
  188. } \
  189. else { \
  190. (t)->begin = NULL; \
  191. (t)->len = 0; \
  192. } \
  193. } while (0)
  194. #ifdef __cplusplus
  195. }
  196. #endif
  197. #endif