aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/fstring.c
blob: ffe1304772c119bac57ccf3afb4e0c66637625ce (plain)
1
2
3
4
5
6
7
8
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
203
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*-
 * Copyright 2016 Vsevolod Stakhov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "fstring.h"
#include "str_util.h"
#include "contrib/fastutf8/fastutf8.h"
#include "contrib/mumhash/mum.h"


#ifdef WITH_JEMALLOC
#include <jemalloc/jemalloc.h>
#if (JEMALLOC_VERSION_MAJOR == 3 && JEMALLOC_VERSION_MINOR >= 6) || (JEMALLOC_VERSION_MAJOR > 3)
#define HAVE_MALLOC_SIZE 1
#define sys_alloc_size(sz) nallocx(sz, 0)
#endif
#elif defined(__APPLE__)
#include <malloc/malloc.h>
#define HAVE_MALLOC_SIZE 1
#define sys_alloc_size(sz) malloc_good_size(sz)
#endif

static const gsize default_initial_size = 16;

#define fstravail(s) ((s)->allocated - (s)->len)

rspamd_fstring_t *
rspamd_fstring_new(void)
{
	rspamd_fstring_t *s;

	if ((s = malloc(default_initial_size + sizeof(*s))) == NULL) {
		g_error("%s: failed to allocate %" G_GSIZE_FORMAT " bytes",
				G_STRLOC, default_initial_size + sizeof(*s));

		return NULL;
	}

	s->len = 0;
	s->allocated = default_initial_size;

	return s;
}

rspamd_fstring_t *
rspamd_fstring_sized_new(gsize initial_size)
{
	rspamd_fstring_t *s;
	gsize real_size = MAX(default_initial_size, initial_size);

	if ((s = malloc(real_size + sizeof(*s))) == NULL) {
		g_error("%s: failed to allocate %" G_GSIZE_FORMAT " bytes",
				G_STRLOC, real_size + sizeof(*s));

		return NULL;
	}
	s->len = 0;
	s->allocated = real_size;

	return s;
}

rspamd_fstring_t *
rspamd_fstring_new_init(const char *init, gsize len)
{
	rspamd_fstring_t *s;
	gsize real_size = MAX(default_initial_size, len);

	if ((s = malloc(real_size + sizeof(*s))) == NULL) {
		g_error("%s: failed to allocate %" G_GSIZE_FORMAT " bytes",
				G_STRLOC, real_size + sizeof(*s));

		abort();
	}

	s->len = len;
	s->allocated = real_size;
	memcpy(s->str, init, len);

	return s;
}

rspamd_fstring_t *
rspamd_fstring_assign(rspamd_fstring_t *str, const char *init, gsize len)
{
	gsize avail;

	if (str == NULL) {
		return rspamd_fstring_new_init(init, len);
	}

	avail = fstravail(str);

	if (avail < len) {
		str = rspamd_fstring_grow(str, len);
	}

	if (len > 0) {
		memcpy(str->str, init, len);
	}

	str->len = len;

	return str;
}

void rspamd_fstring_free(rspamd_fstring_t *str)
{
	free(str);
}

inline gsize
rspamd_fstring_suggest_size(gsize len, gsize allocated, gsize needed_len)
{
	gsize newlen, optlen = 0;

	if (allocated < 4096) {
		newlen = MAX(len + needed_len, allocated * 2);
	}
	else {
		newlen = MAX(len + needed_len, 1 + allocated * 3 / 2);
	}

#ifdef HAVE_MALLOC_SIZE
	optlen = sys_alloc_size(newlen + sizeof(rspamd_fstring_t));
#endif

	return MAX(newlen, optlen);
}

rspamd_fstring_t *
rspamd_fstring_grow(rspamd_fstring_t *str, gsize needed_len)
{
	gsize newlen;
	gpointer nptr;

	newlen = rspamd_fstring_suggest_size(str->len, str->allocated, needed_len);

	nptr = realloc(str, newlen + sizeof(*str));

	if (nptr == NULL) {
		/* Avoid memory leak */
		free(str);
		g_error("%s: failed to re-allocate %" G_GSIZE_FORMAT " bytes",
				G_STRLOC, newlen + sizeof(*str));
		abort();
	}

	str = nptr;
	str->allocated = newlen;

	return str;
}

rspamd_fstring_t *
rspamd_fstring_append(rspamd_fstring_t *str, const char *in, gsize len)
{
	if (str == NULL) {
		str = rspamd_fstring_new_init(in, len);
	}
	else {
		gsize avail = fstravail(str);

		if (avail < len) {
			str = rspamd_fstring_grow(str, len);
		}

		memcpy(str->str + str->len, in, len);
		str->len += len;
	}

	return str;
}

rspamd_fstring_t *
rspamd_fstring_append_chars(rspamd_fstring_t *str,
							char c, gsize len)
{
	if (str == NULL) {
		str = rspamd_fstring_sized_new(len);

		memset(str->str + str->len, c, len);
		str->len += len;
	}
	else {
		gsize avail = fstravail(str);

		if (avail < len) {
			str = rspamd_fstring_grow(str, len);
		}

		memset(str->str + str->len, c, len);
		str->len += len;
	}

	return str;
}

void rspamd_fstring_erase(rspamd_fstring_t *str, gsize pos, gsize len)
{
	if (pos < str->len) {
		if (pos + len > str->len) {
			len = str->len - pos;
		}

		if (len == str->len - pos) {
			/* Fast path */
			str->len = pos;
		}
		else {
			memmove(str->str + pos, str->str + pos + len, str->len - pos);
			str->len -= pos;
		}
	}
	else {
		/* Do nothing */
	}
}

/* Compat code */
static uint64_t
fstrhash_c(uint64_t c, uint64_t hval)
{
	return mum_hash_step(hval, c);
}


/*
 * Return hash value for a string
 */
uint32_t
rspamd_fstrhash_lc(const rspamd_ftok_t *str, gboolean is_utf)
{
	gsize i;
	uint64_t hval;
	const char *p, *end = NULL;
	gunichar uc;

	if (str == NULL) {
		return 0;
	}

	p = str->begin;
	hval = str->len;
	end = p + str->len;

	if (is_utf) {
		if (rspamd_fast_utf8_validate(p, str->len) != 0) {
			return rspamd_fstrhash_lc(str, FALSE);
		}
		while (p < end) {
			uc = g_unichar_tolower(g_utf8_get_char(p));
			hval = fstrhash_c(uc, hval);
			p = g_utf8_next_char(p);
		}
	}
	else {
		gsize large_steps = str->len / sizeof(uint64_t);
		for (i = 0; i < large_steps; i++, p += sizeof(uint64_t)) {
			/* Copy to the uint64 lowercasing each byte */
			union {
				char c[sizeof(uint64_t)];
				uint64_t iu64;
			} t;
			for (int j = 0; j < sizeof(uint64_t); j++) {
				t.c[j] = g_ascii_tolower(p[j]);
			}
			hval = fstrhash_c(t.iu64, hval);
		}

		gsize remain = str->len % sizeof(uint64_t);
		for (i = 0; i < remain; i++, p++) {
			hval = fstrhash_c(g_ascii_tolower(*p), hval);
		}
	}

	return hval;
}

gboolean
rspamd_fstring_equal(const rspamd_fstring_t *s1,
					 const rspamd_fstring_t *s2)
{
	g_assert(s1 != NULL && s2 != NULL);

	if (s1->len == s2->len) {
		return (memcmp(s1->str, s2->str, s1->len) == 0);
	}

	return FALSE;
}

int rspamd_fstring_casecmp(const rspamd_fstring_t *s1,
						   const rspamd_fstring_t *s2)
{
	int ret = 0;

	g_assert(s1 != NULL && s2 != NULL);

	if (s1->len == s2->len) {
		ret = rspamd_lc_cmp(s1->str, s2->str, s1->len);
	}
	else {
		ret = s1->len - s2->len;
	}

	return ret;
}

int rspamd_fstring_cmp(const rspamd_fstring_t *s1,
					   const rspamd_fstring_t *s2)
{
	g_assert(s1 != NULL && s2 != NULL);

	if (s1->len == s2->len) {
		return memcmp(s1->str, s2->str, s1->len);
	}

	return s1->len - s2->len;
}

int rspamd_ftok_casecmp(const rspamd_ftok_t *s1,
						const rspamd_ftok_t *s2)
{
	int ret = 0;

	g_assert(s1 != NULL && s2 != NULL);

	if (s1->len == s2->len) {
		ret = rspamd_lc_cmp(s1->begin, s2->begin, s1->len);
	}
	else {
		ret = s1->len - s2->len;
	}

	return ret;
}

int rspamd_ftok_cmp(const rspamd_ftok_t *s1,
					const rspamd_ftok_t *s2)
{
	g_assert(s1 != NULL && s2 != NULL);

	if (s1->len == s2->len) {
		return memcmp(s1->begin, s2->begin, s1->len);
	}

	return s1->len - s2->len;
}

gboolean
rspamd_ftok_starts_with(const rspamd_ftok_t *s1,
						const rspamd_ftok_t *s2)
{
	g_assert(s1 != NULL && s2 != NULL);

	if (s1->len >= s2->len) {
		return !!(memcmp(s1->begin, s2->begin, s2->len) == 0);
	}

	return FALSE;
}

void rspamd_fstring_mapped_ftok_free(gpointer p)
{
	rspamd_ftok_t *tok = p;
	rspamd_fstring_t *storage;

	storage = (rspamd_fstring_t *) (tok->begin - 2 * sizeof(gsize));
	rspamd_fstring_free(storage);
	g_free(tok);
}

rspamd_ftok_t *
rspamd_ftok_map(const rspamd_fstring_t *s)
{
	rspamd_ftok_t *tok;

	g_assert(s != NULL);

	tok = g_malloc(sizeof(*tok));
	tok->begin = s->str;
	tok->len = s->len;

	return tok;
}

char *
rspamd_fstring_cstr(const rspamd_fstring_t *s)
{
	char *result;

	if (s == NULL) {
		return NULL;
	}

	result = g_malloc(s->len + 1);
	memcpy(result, s->str, s->len);
	result[s->len] = '\0';

	return result;
}

char *
rspamd_ftok_cstr(const rspamd_ftok_t *s)
{
	char *result;

	if (s == NULL) {
		return NULL;
	}

	result = g_malloc(s->len + 1);
	memcpy(result, s->begin, s->len);
	result[s->len] = '\0';

	return result;
}

gboolean
rspamd_ftok_cstr_equal(const rspamd_ftok_t *s, const char *pat,
					   gboolean icase)
{
	gsize slen;
	rspamd_ftok_t srch;

	g_assert(s != NULL);
	g_assert(pat != NULL);

	slen = strlen(pat);
	srch.begin = pat;
	srch.len = slen;

	if (icase) {
		return (rspamd_ftok_casecmp(s, &srch) == 0);
	}

	return (rspamd_ftok_cmp(s, &srch) == 0);
}

char *
rspamd_ftokdup(const rspamd_ftok_t *src)
{
	char *newstr;

	if (src == NULL) {
		return NULL;
	}

	newstr = g_malloc(src->len + 1);
	memcpy(newstr, src->begin, src->len);
	newstr[src->len] = '\0';

	return newstr;
}

char *
rspamd_fstringdup(const rspamd_fstring_t *src)
{
	char *newstr;

	if (src == NULL) {
		return NULL;
	}

	newstr = g_malloc(src->len + 1);
	memcpy(newstr, src->str, src->len);
	newstr[src->len] = '\0';

	return newstr;
}