Debian Codegraph @

runtime/stringbuf.c

raw
1 /* This is the byte-counted string class for rsyslog.
2 * This object has a lot of legacy. Among others, it was started to
3 * support embedded \0 bytes, which looked like they were needed to
4 * be supported by RFC developments at that time. Later, this was
5 * no longer a requirement, and we refactored the class in 2016
6 * to some simpler internals which make use of the fact that no
7 * NUL can ever occur in rsyslog strings (they are escaped at the
8 * input side of rsyslog).
9 * It now serves primarily to a) dynamic string creation, b) keep
10 * old interfaces supported, and c) some special functionality,
11 * e.g. search. Further refactoring and simplificytin may make
12 * sense.
13 *
14 * Copyright (C) 2005-2025 Adiscon GmbH
15 *
16 * This file is part of the rsyslog runtime library.
17 *
18 * Licensed under the Apache License, Version 2.0 (the "License");
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 * -or-
24 * see COPYING.ASL20 in the source distribution
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32 #include "config.h"
33
34 #include <stdlib.h>
35 #include <assert.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <stdarg.h>
39 #include <sys/types.h>
40 #include <libestr.h>
41 #include "rsyslog.h"
42 #include "stringbuf.h"
43 #include "srUtils.h"
44 #include "regexp.h"
45 #include "errmsg.h"
46 #include "unicode-helper.h"
47
48 #define DEV_DEBUG 0 /* set to 1 to enable very verbose developer debugging messages */
49
50
51 /* ################################################################# *
52 * private members *
53 * ################################################################# */
54
55 /* static data */
56 DEFobjCurrIf(obj) DEFobjCurrIf(regexp)
57
58 /* ################################################################# *
59 * public members *
60 * ################################################################# */
61
62
63 rsRetVal cstrConstruct(cstr_t **const ppThis) {
64 DEFiRet;
65 cstr_t *pThis;
66
67 CHKmalloc(pThis = (cstr_t *)malloc(sizeof(cstr_t)));
68 rsSETOBJTYPE(pThis, OIDrsCStr);
69 #ifndef NDEBUG
70 pThis->isFinalized = 0;
71 #endif
72 pThis->pBuf = NULL;
73 pThis->iBufSize = 0;
74 pThis->iStrLen = 0;
75 *ppThis = pThis;
76
77 finalize_it:
78 RETiRet;
79 }
80
81
82 /* construct from sz string
83 * rgerhards 2005-09-15
84 */
85 rsRetVal rsCStrConstructFromszStr(cstr_t **const ppThis, const uchar *const sz) {
86 DEFiRet;
87 cstr_t *pThis;
88
89 CHKiRet(rsCStrConstruct(&pThis));
90
91 pThis->iStrLen = strlen((char *)sz);
92 pThis->iBufSize = strlen((char *)sz) + 1;
93 if ((pThis->pBuf = (uchar *)malloc(pThis->iBufSize)) == NULL) {
94 RSFREEOBJ(pThis);
95 ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
96 }
97
98 /* we do NOT need to copy the \0! */
99 memcpy(pThis->pBuf, sz, pThis->iStrLen);
100
101 *ppThis = pThis;
102
103 finalize_it:
104 RETiRet;
105 }
106
107
108 /* a helper function for rsCStr*Strf()
109 */
110 static rsRetVal rsCStrConstructFromszStrv(cstr_t **ppThis, const char *fmt, va_list ap)
111 __attribute__((format(printf, 2, 0)));
112 static rsRetVal rsCStrConstructFromszStrv(cstr_t **const ppThis, const char *const fmt, va_list ap) {
113 DEFiRet;
114 cstr_t *pThis;
115 va_list ap2;
116 int len;
117
118 va_copy(ap2, ap);
119 len = vsnprintf(NULL, 0, (char *)fmt, ap2);
120 va_end(ap2);
121
122 if (len < 0) ABORT_FINALIZE(RS_RET_ERR);
123
124 CHKiRet(rsCStrConstruct(&pThis));
125
126 pThis->iStrLen = len;
127 pThis->iBufSize = len + 1;
128 len++; /* account for the \0 written by vsnprintf */
129 if ((pThis->pBuf = (uchar *)malloc(pThis->iBufSize)) == NULL) {
130 RSFREEOBJ(pThis);
131 ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
132 }
133
134 vsnprintf((char *)pThis->pBuf, len, (char *)fmt, ap);
135 *ppThis = pThis;
136 finalize_it:
137 RETiRet;
138 }
139
140
141 /* construct from a printf-style formated string
142 */
143 rsRetVal rsCStrConstructFromszStrf(cstr_t **ppThis, const char *fmt, ...) {
144 DEFiRet;
145 va_list ap;
146
147 va_start(ap, fmt);
148 iRet = rsCStrConstructFromszStrv(ppThis, fmt, ap);
149 va_end(ap);
150
151 RETiRet;
152 }
153
154
155 /* construct from es_str_t string
156 * rgerhards 2010-12-03
157 */
158 rsRetVal cstrConstructFromESStr(cstr_t **const ppThis, es_str_t *const str) {
159 DEFiRet;
160 cstr_t *pThis;
161
162 CHKiRet(rsCStrConstruct(&pThis));
163
164 pThis->iStrLen = es_strlen(str);
165 pThis->iBufSize = pThis->iStrLen + 1;
166 if ((pThis->pBuf = (uchar *)malloc(pThis->iBufSize)) == NULL) {
167 RSFREEOBJ(pThis);
168 ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
169 }
170
171 /* we do NOT need to copy the \0! */
172 memcpy(pThis->pBuf, es_getBufAddr(str), pThis->iStrLen);
173
174 *ppThis = pThis;
175
176 finalize_it:
177 RETiRet;
178 }
179
180 /* construct from CStr object.
181 * rgerhards 2005-10-18
182 */
183 rsRetVal ATTR_NONNULL() rsCStrConstructFromCStr(cstr_t **const ppThis, const cstr_t *const pFrom) {
184 DEFiRet;
185 cstr_t *pThis;
186
187 rsCHECKVALIDOBJECT(pFrom, OIDrsCStr);
188
189 CHKiRet(rsCStrConstruct(&pThis));
190 if (pFrom->iStrLen > 0) {
191 pThis->iStrLen = pFrom->iStrLen;
192 pThis->iBufSize = pFrom->iStrLen + 1;
193 if ((pThis->pBuf = (uchar *)malloc(pThis->iBufSize)) == NULL) {
194 RSFREEOBJ(pThis);
195 ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
196 }
197 memcpy(pThis->pBuf, pFrom->pBuf, pThis->iStrLen);
198 }
199
200 *ppThis = pThis;
201 finalize_it:
202 RETiRet;
203 }
204
205
206 void rsCStrDestruct(cstr_t **const ppThis) {
207 free((*ppThis)->pBuf);
208 RSFREEOBJ(*ppThis);
209 *ppThis = NULL;
210 }
211
212
213 /* extend the string buffer if its size is insufficient.
214 * Param iMinNeeded is the minumum free space needed. If it is larger
215 * than the default alloc increment, space for at least this amount is
216 * allocated. In practice, a bit more is allocated because we envision that
217 * some more characters may be added after these.
218 * rgerhards, 2008-01-07
219 * changed to utilized realloc() -- rgerhards, 2009-06-16
220 */
221 static rsRetVal rsCStrExtendBuf(cstr_t *const __restrict__ pThis, const size_t iMinNeeded) {
222 uchar *pNewBuf;
223 size_t iNewSize;
224 DEFiRet;
225
226 /* first compute the new size needed */
227 if (iMinNeeded > RS_STRINGBUF_ALLOC_INCREMENT) {
228 /* we allocate "n" ALLOC_INCREMENTs. Usually, that should
229 * leave some room after the absolutely needed one. It also
230 * reduces memory fragmentation. Note that all of this are
231 * integer operations (very important to understand what is
232 * going on)! Parenthesis are for better readibility.
233 */
234 iNewSize = (iMinNeeded / RS_STRINGBUF_ALLOC_INCREMENT + 1) * RS_STRINGBUF_ALLOC_INCREMENT;
235 } else {
236 iNewSize = pThis->iBufSize + RS_STRINGBUF_ALLOC_INCREMENT;
237 }
238 iNewSize += pThis->iBufSize; /* add current size */
239
240 #if DEV_DEBUG == 1
241 dbgprintf("extending string buffer, old %d, new %d\n", pThis->iBufSize, iNewSize);
242 #endif
243 CHKmalloc(pNewBuf = (uchar *)realloc(pThis->pBuf, iNewSize));
244 pThis->iBufSize = iNewSize;
245 pThis->pBuf = pNewBuf;
246
247 finalize_it:
248 RETiRet;
249 }
250
251 /* Append a character to the current string object. This may only be done until
252 * cstrFinalize() is called.
253 * rgerhards, 2009-06-16
254 */
255 rsRetVal cstrAppendChar(cstr_t *const __restrict__ pThis, const uchar c) {
256 rsRetVal iRet = RS_RET_OK;
257
258 if (pThis->iStrLen + 1 >= pThis->iBufSize) {
259 CHKiRet(rsCStrExtendBuf(pThis, 1)); /* need more memory! */
260 }
261
262 /* ok, when we reach this, we have sufficient memory */
263 *(pThis->pBuf + pThis->iStrLen++) = c;
264
265 finalize_it:
266 return iRet;
267 }
268
269 /* append a string of known length. In this case, we make sure we do at most
270 * one additional memory allocation.
271 */
272 rsRetVal rsCStrAppendStrWithLen(cstr_t *const pThis, const uchar *const psz, const size_t iStrLen) {
273 DEFiRet;
274
275 rsCHECKVALIDOBJECT(pThis, OIDrsCStr);
276 assert(psz != NULL);
277
278 /* Keep one spare byte for cstrFinalize(), which appends the trailing NUL. */
279 if (pThis->iStrLen + iStrLen + 1 > pThis->iBufSize) {
280 CHKiRet(rsCStrExtendBuf(pThis, iStrLen + 1)); /* need more memory! */
281 }
282
283 /* ok, now we always have sufficient continues memory to do a memcpy() */
284 memcpy(pThis->pBuf + pThis->iStrLen, psz, iStrLen);
285 pThis->iStrLen += iStrLen;
286
287 finalize_it:
288 RETiRet;
289 }
290
291
292 /* changed to be a wrapper to rsCStrAppendStrWithLen() so that
293 * we can save some time when we have the length but do not
294 * need to change existing code.
295 * rgerhards, 2007-07-03
296 */
297 rsRetVal rsCStrAppendStr(cstr_t *const pThis, const uchar *const psz) {
298 return rsCStrAppendStrWithLen(pThis, psz, strlen((char *)psz));
299 }
300
301
302 /* append the contents of one cstr_t object to another
303 * rgerhards, 2008-02-25
304 */
305 rsRetVal cstrAppendCStr(cstr_t *pThis, cstr_t *pstrAppend) {
306 return rsCStrAppendStrWithLen(pThis, pstrAppend->pBuf, pstrAppend->iStrLen);
307 }
308
309
310 /* append a printf-style formated string
311 */
312 rsRetVal rsCStrAppendStrf(cstr_t *pThis, const char *fmt, ...) {
313 DEFiRet;
314 va_list ap;
315 cstr_t *pStr = NULL;
316
317 va_start(ap, fmt);
318 iRet = rsCStrConstructFromszStrv(&pStr, (char *)fmt, ap);
319 va_end(ap);
320
321 if (iRet != RS_RET_OK) goto finalize_it;
322
323 iRet = cstrAppendCStr(pThis, pStr);
324 rsCStrDestruct(&pStr);
325 finalize_it:
326 RETiRet;
327 }
328
329
330 rsRetVal rsCStrAppendInt(cstr_t *pThis, long i) {
331 DEFiRet;
332 uchar szBuf[32];
333
334 rsCHECKVALIDOBJECT(pThis, OIDrsCStr);
335
336 CHKiRet(srUtilItoA((char *)szBuf, sizeof(szBuf), (number_t)i));
337
338 iRet = rsCStrAppendStr(pThis, szBuf);
339 finalize_it:
340 RETiRet;
341 }
342
343
344 /* Sets the string object to the classigal sz-string provided.
345 * Any previously stored vlaue is discarded. If a NULL pointer
346 * the the new value (pszNew) is provided, an empty string is
347 * created (this is NOT an error!).
348 * rgerhards, 2005-10-18
349 */
350 rsRetVal rsCStrSetSzStr(cstr_t *const __restrict__ pThis, uchar *const __restrict__ pszNew) {
351 rsCHECKVALIDOBJECT(pThis, OIDrsCStr);
352
353 if (pszNew == NULL) {
354 free(pThis->pBuf);
355 pThis->pBuf = NULL;
356 pThis->iStrLen = 0;
357 pThis->iBufSize = 0;
358 } else {
359 const size_t newlen = strlen((char *)pszNew);
360 if (newlen > pThis->iBufSize) {
361 uchar *const newbuf = (uchar *)realloc(pThis->pBuf, newlen + 1);
362 if (newbuf == NULL) {
363 /* we keep the old value, best we can do */
364 return RS_RET_OUT_OF_MEMORY;
365 }
366 pThis->pBuf = newbuf;
367 pThis->iBufSize = newlen + 1;
368 }
369 pThis->iStrLen = newlen;
370 memcpy(pThis->pBuf, pszNew, pThis->iStrLen);
371 }
372
373 return RS_RET_OK;
374 }
375
376 /* Converts the CStr object to a classical zero-terminated C string
377 * and returns that string. The caller must not free it and must not
378 * destroy the CStr object as long as the ascii string is used.
379 */
380 uchar *cstrGetSzStrNoNULL(cstr_t *const __restrict__ pThis) {
381 assert(pThis->isFinalized);
382 return (pThis->pBuf == NULL) ? (uchar *)"" : pThis->pBuf;
383 }
384
385
386 /* Converts the CStr object to a classical zero-terminated C string,
387 * returns that string and destroys the CStr object. The returned string
388 * MUST be freed by the caller. The function might return NULL if
389 * no memory can be allocated.
390 *
391 * This is the NEW replacement for rsCStrConvSzStrAndDestruct which does
392 * no longer utilize a special buffer but soley works on pBuf (and also
393 * assumes that cstrFinalize had been called).
394 *
395 * Parameters are as follows:
396 * pointer to the object, pointer to string-pointer to receive string and
397 * bRetNULL: 0 - must not return NULL on empty string, return "" in that
398 * case, 1 - return NULL instead of an empty string.
399 * PLEASE NOTE: the caller must free the memory returned in ppSz in any case
400 * (except, of course, if it is NULL).
401 */
402 rsRetVal cstrConvSzStrAndDestruct(cstr_t **ppThis, uchar **ppSz, int bRetNULL) {
403 DEFiRet;
404 uchar *pRetBuf;
405 cstr_t *pThis;
406
407 assert(ppThis != NULL);
408 pThis = *ppThis;
409 assert(pThis->isFinalized);
410 rsCHECKVALIDOBJECT(pThis, OIDrsCStr);
411 assert(ppSz != NULL);
412 assert(bRetNULL == 0 || bRetNULL == 1);
413
414 if (pThis->pBuf == NULL) {
415 if (bRetNULL == 0) {
416 CHKmalloc(pRetBuf = malloc(1));
417 *pRetBuf = '\0';
418 } else {
419 pRetBuf = NULL;
420 }
421 } else {
422 pThis->pBuf[pThis->iStrLen] = '\0'; /* space for this is reserved */
423 pRetBuf = pThis->pBuf;
424 }
425
426 *ppSz = pRetBuf;
427
428 finalize_it:
429 /* We got it, now free the object ourselfs. Please note
430 * that we can NOT use the rsCStrDestruct function as it would
431 * also free the sz String buffer, which we pass on to the user.
432 */
433 RSFREEOBJ(pThis);
434 *ppThis = NULL;
435
436 RETiRet;
437 }
438
439
440 /* return the length of the current string
441 * 2005-09-09 rgerhards
442 * Please note: this is only a function in a debug build.
443 * For release builds, it is a macro defined in stringbuf.h.
444 * This is due to performance reasons.
445 */
446 #ifndef NDEBUG
447 size_t cstrLen(cstr_t *pThis) {
448 rsCHECKVALIDOBJECT(pThis, OIDrsCStr);
449 return (pThis->iStrLen);
450 }
451 #endif
452
453 /* Truncate characters from the end of the string.
454 * rgerhards 2005-09-15
455 */
456 rsRetVal rsCStrTruncate(cstr_t *pThis, size_t nTrunc) {
457 rsCHECKVALIDOBJECT(pThis, OIDrsCStr);
458
459 if (pThis->iStrLen < nTrunc) return RS_TRUNCAT_TOO_LARGE;
460
461 pThis->iStrLen -= nTrunc;
462
463 return RS_RET_OK;
464 }
465
466 /* Trim trailing whitespace from a given string
467 */
468 void cstrTrimTrailingWhiteSpace(cstr_t *const __restrict__ pThis) {
469 register int i;
470 register uchar *pC;
471 rsCHECKVALIDOBJECT(pThis, OIDrsCStr);
472
473 if (pThis->iStrLen == 0) goto done; /* empty string -> nothing to trim ;) */
474 i = pThis->iStrLen;
475 pC = pThis->pBuf + i - 1;
476 while (i > 0 && isspace((int)*pC)) {
477 --pC;
478 --i;
479 }
480 /* i now is the new string length! */
481 if (i != (int)pThis->iStrLen) {
482 pThis->iStrLen = i;
483 pThis->pBuf[pThis->iStrLen] = '\0'; /* we always have this space */ // TODO: can we remove this?
484 }
485
486 done:
487 return;
488 }
489
490 /* compare two string objects - works like strcmp(), but operates
491 * on CStr objects. Please note that this version here is
492 * faster in the majority of cases, simply because it can
493 * rely on StrLen.
494 * rgerhards 2005-09-19
495 * fixed bug, in which only the last byte was actually compared
496 * in equal-size strings.
497 * rgerhards, 2005-09-26
498 */
499 int rsCStrCStrCmp(cstr_t *const __restrict__ pCS1, cstr_t *const __restrict__ pCS2) {
500 rsCHECKVALIDOBJECT(pCS1, OIDrsCStr);
501 rsCHECKVALIDOBJECT(pCS2, OIDrsCStr);
502 if (pCS1->iStrLen == pCS2->iStrLen)
503 if (pCS1->iStrLen == 0)
504 return 0; /* zero-sized string are equal ;) */
505 else
506 return memcmp(pCS1->pBuf, pCS2->pBuf, pCS1->iStrLen);
507 else
508 return pCS1->iStrLen - pCS2->iStrLen;
509 }
510
511
512 /* check if a sz-type string starts with a CStr object. This function
513 * is initially written to support the "startswith" property-filter
514 * comparison operation. Maybe it also has other needs.
515 * This functions is modelled after the strcmp() series, thus a
516 * return value of 0 indicates that the string starts with the
517 * sequence while -1 indicates it does not!
518 * rgerhards 2005-10-19
519 */
520 int rsCStrSzStrStartsWithCStr(cstr_t *const __restrict__ pCS1, uchar *const __restrict__ psz, const size_t iLenSz) {
521 rsCHECKVALIDOBJECT(pCS1, OIDrsCStr);
522 assert(psz != NULL);
523 assert(iLenSz == strlen((char *)psz)); /* just make sure during debugging! */
524 if (iLenSz >= pCS1->iStrLen) {
525 if (pCS1->iStrLen == 0)
526 return 0; /* yes, it starts with a zero-sized string ;) */
527 else
528 return memcmp(psz, pCS1->pBuf, pCS1->iStrLen);
529 } else {
530 return -1; /* pCS1 is less then psz */
531 }
532 }
533
534 /* check if a sz-type string ends with a CStr object. This helper mirrors
535 * rsCStrSzStrStartsWithCStr and is primarily intended for the new
536 * "endswith" property-filter operation.
537 * returns 0 if the string ends with the pattern, -1 otherwise.
538 */
539 int rsCStrSzStrEndsWithCStr(cstr_t *const __restrict__ pCS1, uchar *const __restrict__ psz, const size_t iLenSz) {
540 rsCHECKVALIDOBJECT(pCS1, OIDrsCStr);
541 assert(psz != NULL);
542 assert(iLenSz == strlen((char *)psz));
543 if (iLenSz >= pCS1->iStrLen) {
544 if (pCS1->iStrLen == 0) {
545 return 0; /* yes, it ends with a zero-sized string ;) */
546 } else {
547 return memcmp(psz + iLenSz - pCS1->iStrLen, pCS1->pBuf, pCS1->iStrLen);
548 }
549 } else {
550 return -1;
551 }
552 }
553
554
555 /* check if a CStr object matches a regex.
556 * [email protected] 2007-07-12
557 * @return returns 0 if matched
558 * bug: doesn't work for CStr containing \0
559 * rgerhards, 2007-07-16: bug is no real bug, because rsyslogd ensures there
560 * never is a \0 *inside* a property string.
561 * Note that the function returns -1 if regexp functionality is not available.
562 * rgerhards: 2009-03-04: ERE support added, via parameter iType: 0 - BRE, 1 - ERE
563 * Arnaud Cornet/rgerhards: 2009-04-02: performance improvement by caching compiled regex
564 * If a caller does not need the cached version, it must still provide memory for it
565 * and must call rsCStrRegexDestruct() afterwards.
566 */
567 rsRetVal rsCStrSzStrMatchRegex(cstr_t *pCS1, uchar *psz, int iType, void *rc) {
568 regex_t **cache = (regex_t **)rc;
569 int ret;
570 DEFiRet;
571
572 assert(pCS1 != NULL);
573 assert(psz != NULL);
574 assert(cache != NULL);
575
576 if (objUse(regexp, LM_REGEXP_FILENAME) == RS_RET_OK) {
577 if (*cache == NULL) {
578 *cache = calloc(1, sizeof(regex_t));
579 int errcode;
580 if ((errcode = regexp.regcomp(*cache, (char *)rsCStrGetSzStrNoNULL(pCS1),
581 (iType == 1 ? REG_EXTENDED : 0) | REG_NOSUB))) {
582 char errbuff[512];
583 regexp.regerror(errcode, *cache, errbuff, sizeof(errbuff));
584 LogError(0, NO_ERRCODE, "Error in regular expression: %s\n", errbuff);
585 ABORT_FINALIZE(RS_RET_NOT_FOUND);
586 }
587 }
588 ret = regexp.regexec(*cache, (char *)psz, 0, NULL, 0);
589 if (ret != 0) ABORT_FINALIZE(RS_RET_NOT_FOUND);
590 } else {
591 ABORT_FINALIZE(RS_RET_NOT_FOUND);
592 }
593
594 finalize_it:
595 RETiRet;
596 }
597
598
599 /* free a cached compiled regex
600 * Caller must provide a pointer to a buffer that was created by
601 * rsCStrSzStrMatchRegexCache()
602 */
603 void rsCStrRegexDestruct(void *rc) {
604 regex_t **cache = rc;
605
606 assert(cache != NULL);
607 assert(*cache != NULL);
608
609 if (objUse(regexp, LM_REGEXP_FILENAME) == RS_RET_OK) {
610 regexp.regfree(*cache);
611 free(*cache);
612 *cache = NULL;
613 }
614 }
615
616
617 /* compare a rsCStr object with a classical sz string. This function
618 * is almost identical to rsCStrZsStrCmp(), but it also takes an offset
619 * to the CStr object from where the comparison is to start.
620 * I have thought quite a while if it really makes sense to more or
621 * less duplicate the code. After all, if you call it with an offset of
622 * zero, the functionality is exactly the same. So it looks natural to
623 * just have a single function. However, supporting the offset requires
624 * some (few) additional integer operations. While they are few, they
625 * happen at places in the code that is run very frequently. All in all,
626 * I have opted for performance and thus duplicated the code. I hope
627 * this is a good, or at least acceptable, compromise.
628 * rgerhards, 2005-09-26
629 * This function also has an offset-pointer which allows one to
630 * specify *where* the compare operation should begin in
631 * the CStr. If everything is to be compared, it must be set
632 * to 0. If some leading bytes are to be skipped, it must be set
633 * to the first index that is to be compared. It must not be
634 * set higher than the string length (this is considered a
635 * program bug and will lead to unpredictable results and program aborts).
636 * rgerhards 2005-09-26
637 */
638 int rsCStrOffsetSzStrCmp(cstr_t *pCS1, size_t iOffset, uchar *psz, size_t iLenSz) {
639 rsCHECKVALIDOBJECT(pCS1, OIDrsCStr);
640 assert(iOffset < pCS1->iStrLen);
641 assert(iLenSz == strlen((char *)psz)); /* just make sure during debugging! */
642 if ((pCS1->iStrLen - iOffset) == iLenSz) {
643 /* we are using iLenSz below, because the lengths
644 * are equal and iLenSz is faster to access
645 */
646 if (iLenSz == 0) {
647 return 0; /* zero-sized strings are equal ;) */
648 } else { /* we now have two non-empty strings of equal
649 * length, so we need to actually check if they
650 * are equal.
651 */
652 return memcmp(pCS1->pBuf + iOffset, psz, iLenSz);
653 }
654 } else {
655 return pCS1->iStrLen - iOffset - iLenSz;
656 }
657 }
658
659
660 /* compare a rsCStr object with a classical sz string.
661 * Just like rsCStrCStrCmp, just for a different data type.
662 * There must not only the sz string but also its length be
663 * provided. If the caller does not know the length he can
664 * call with
665 * rsCstrSzStrCmp(pCS, psz, strlen((char*)psz));
666 * we are not doing the strlen((char*)) ourselfs as the caller might
667 * already know the length and in such cases we can save the
668 * overhead of doing it one more time (strelen() is costly!).
669 * The bottom line is that the provided length MUST be correct!
670 * The to sz string pointer must not be NULL!
671 * rgerhards 2005-09-26
672 */
673 int rsCStrSzStrCmp(cstr_t *pCS1, uchar *psz, size_t iLenSz) {
674 rsCHECKVALIDOBJECT(pCS1, OIDrsCStr);
675 assert(psz != NULL);
676 assert(iLenSz == strlen((char *)psz)); /* just make sure during debugging! */
677 if (pCS1->iStrLen == iLenSz)
678 if (iLenSz == 0)
679 return 0; /* zero-sized strings are equal ;) */
680 else
681 return strncmp((char *)pCS1->pBuf, (char *)psz, iLenSz);
682 else
683 return (ssize_t)pCS1->iStrLen - (ssize_t)iLenSz;
684 }
685
686
687 /* Locate the first occurrence of this rsCStr object inside a standard sz string.
688 * Returns the offset (0-bound) of this first occurrence. If not found, -1 is
689 * returned. Both parameters MUST be given (NULL is not allowed).
690 * rgerhards 2005-09-19
691 */
692 int ATTR_NONNULL(1, 2) rsCStrLocateInSzStr(cstr_t *const pThis, uchar *const sz) {
693 size_t i;
694 size_t iMax;
695 size_t len_sz = ustrlen(sz);
696 int bFound;
697 rsCHECKVALIDOBJECT(pThis, OIDrsCStr);
698 assert(sz != NULL);
699
700 if (pThis->iStrLen == 0) return 0;
701 if (pThis->iStrLen > len_sz) return -1;
702
703 /* compute the largest index where a match could occur - after all,
704 * the to-be-located string must be able to be present in the
705 * searched string (it needs its size ;)).
706 */
707 iMax = len_sz - pThis->iStrLen;
708
709 bFound = 0;
710 i = 0;
711 while (i <= iMax && !bFound) {
712 size_t iCheck;
713 uchar *pComp = sz + i;
714 for (iCheck = 0; iCheck < pThis->iStrLen; ++iCheck)
715 if (*(pComp + iCheck) != *(pThis->pBuf + iCheck)) break;
716 if (iCheck == pThis->iStrLen)
717 bFound = 1; /* found! - else it wouldn't be equal */
718 else
719 ++i; /* on to the next try */
720 }
721
722 return (bFound ? (int)i : -1);
723 }
724
725
726 /* our exit function. TODO: remove once converted to a class
727 * rgerhards, 2008-03-11
728 */
729 rsRetVal strExit(void) {
730 DEFiRet;
731 objRelease(regexp, LM_REGEXP_FILENAME);
732 RETiRet;
733 }
734
735
736 /* our init function. TODO: remove once converted to a class
737 */
738 rsRetVal strInit(void) {
739 DEFiRet;
740 CHKiRet(objGetObjInterface(&obj));
741
742 finalize_it:
743 RETiRet;
744 }