2
* LibHTP (http://www.libhtp.org)
3
* Copyright 2009,2010 Ivan Ristic <ivanr@webkreator.com>
5
* LibHTP is an open source product, released under terms of the General Public Licence
6
* version 2 (GPLv2). Please refer to the file LICENSE, which contains the complete text
9
* In addition, there is a special exception that allows LibHTP to be freely
10
* used with any OSI-approved open source licence. Please refer to the file
11
* LIBHTP_LICENSING_EXCEPTION for the full text of the exception.
1
/***************************************************************************
2
* Copyright (c) 2009-2010 Open Information Security Foundation
3
* Copyright (c) 2010-2013 Qualys, Inc.
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions are
10
* - Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
13
* - Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
17
* - Neither the name of the Qualys, Inc. nor the names of its
18
* contributors may be used to endorse or promote products derived from
19
* this software without specific prior written permission.
21
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
***************************************************************************/
36
* @author Ivan Ristic <ivanr@webkreator.com>
46
typedef struct bstr_t bstr;
19
49
#include <stdlib.h>
20
51
#include <string.h>
22
// IMPORTANT This binary string library is used internally by the parser and you should
23
// not rely on it in your code. The implementation may change.
26
// - Add a function that wraps an existing data
27
// - Support Unicode bstrings
31
bstr *bstr_alloc(size_t newsize);
32
void bstr_free(bstr *s);
33
bstr *bstr_expand(bstr *s, size_t newsize);
34
bstr *bstr_cstrdup(char *);
35
bstr *bstr_memdup(char *data, size_t len);
36
bstr *bstr_strdup(bstr *b);
37
bstr *bstr_strdup_ex(bstr *b, size_t offset, size_t len);
38
char *bstr_tocstr(bstr *);
40
int bstr_chr(bstr *, int);
41
int bstr_rchr(bstr *, int);
43
int bstr_cmpc(bstr *, char *);
44
int bstr_cmp(bstr *, bstr *);
45
int bstr_cmp_nocase(bstr *, bstr *);
47
bstr *bstr_dup_lower(bstr *);
48
bstr *bstr_tolowercase(bstr *);
50
bstr *bstr_add_mem(bstr *, char *, size_t);
51
bstr *bstr_add_str(bstr *, bstr *);
52
bstr *bstr_add_cstr(bstr *, char *);
54
bstr *bstr_add_mem_noex(bstr *, char *, size_t);
55
bstr *bstr_add_str_noex(bstr *, bstr *);
56
bstr *bstr_add_cstr_noex(bstr *, char *);
58
int bstr_util_memtoip(char *data, size_t len, int base, size_t *lastlen);
59
char *bstr_memtocstr(char *data, size_t len);
61
int bstr_indexof(bstr *haystack, bstr *needle);
62
int bstr_indexofc(bstr *haystack, char *needle);
63
int bstr_indexof_nocase(bstr *haystack, bstr *needle);
64
int bstr_indexofc_nocase(bstr *haystack, char *needle);
65
int bstr_indexofmem(bstr *haystack, char *data, size_t len);
66
int bstr_indexofmem_nocase(bstr *haystack, char *data, size_t len);
68
void bstr_chop(bstr *b);
69
void bstr_len_adjust(bstr *s, size_t newlen);
71
char bstr_char_at(bstr *s, size_t pos);
73
typedef struct bstr_t bstr_t;
53
#include "bstr_builder.h"
76
58
/** The length of the string stored in the buffer. */
79
/** The current size of the buffer. If the buffer is bigger than the
80
* string then it will be able to expand without having to reallocate.
61
/** The current size of the buffer. If there is extra room in the
62
* buffer the string will be able to expand without reallocation.
84
/** Optional buffer pointer. If this pointer is NUL (as it currently is
85
* in virtually all cases, the string buffer will immediatelly follow
86
* this structure. If the pointer is not NUL, it points to the actual
87
* buffer used, and there's no data following this structure.
66
/** Optional buffer pointer. If this pointer is NULL the string buffer
67
* will immediately follow this structure. If the pointer is not NUL,
68
* it points to the actual buffer used, and there's no data following
71
unsigned char *realptr;
92
#define bstr_len(X) ((*(bstr_t *)(X)).len)
93
#define bstr_size(X) ((*(bstr_t *)(X)).size)
94
#define bstr_ptr(X) ( ((*(bstr_t *)(X)).ptr == NULL) ? (char *)((char *)(X) + sizeof(bstr_t)) : (char *)(*(bstr_t *)(X)).ptr )
77
#define bstr_len(X) ((*(X)).len)
78
#define bstr_size(X) ((*(X)).size)
79
#define bstr_ptr(X) ( ((*(X)).realptr == NULL) ? ((unsigned char *)(X) + sizeof(bstr)) : (unsigned char *)(*(X)).realptr )
80
#define bstr_realptr(X) ((*(X)).realptr)
86
* Append source bstring to destination bstring, growing destination if
87
* necessary. If the destination bstring is expanded, the pointer will change.
88
* You must replace the original destination pointer with the returned one.
89
* Destination is not changed on memory allocation failure.
91
* @param[in] bdestination
93
* @return Updated bstring, or NULL on memory allocation failure.
95
bstr *bstr_add(bstr *bdestination, const bstr *bsource);
98
* Append a NUL-terminated source to destination, growing destination if
99
* necessary. If the string is expanded, the pointer will change. You must
100
* replace the original destination pointer with the returned one. Destination
101
* is not changed on memory allocation failure.
105
* @return Updated bstring, or NULL on memory allocation failure.
107
bstr *bstr_add_c(bstr *b, const char *cstr);
110
* Append as many bytes from the source to destination bstring. The
111
* destination storage will not be expanded if there is not enough space in it
112
* already to accommodate all of the data.
116
* @return The destination bstring.
118
bstr *bstr_add_c_noex(bstr *b, const char *cstr);
121
* Append a memory region to destination, growing destination if necessary. If
122
* the string is expanded, the pointer will change. You must replace the
123
* original destination pointer with the returned one. Destination is not
124
* changed on memory allocation failure.
129
* @return Updated bstring, or NULL on memory allocation failure.
131
bstr *bstr_add_mem(bstr *b, const void *data, size_t len);
134
* Append as many bytes from the source to destination bstring. The
135
* destination storage will not be expanded if there is not enough space in it
136
* already to accommodate all of the data.
141
* @return The destination bstring.
143
bstr *bstr_add_mem_noex(bstr *b, const void *data, size_t len);
146
* Append as many bytes from the source bstring to destination bstring. The
147
* destination storage will not be expanded if there is not enough space in it
148
* already to accommodate all of the data.
150
* @param[in] bdestination
152
* @return The destination bstring.
154
bstr *bstr_add_noex(bstr *bdestination, const bstr *bsource);
157
* Adjust bstring length. You will need to use this method whenever
158
* you work directly with the string contents, and end up changing
159
* its length by direct structure manipulation.
164
void bstr_adjust_len(bstr *b, size_t newlen);
167
* Change the external pointer used by bstring. You will need to use this
168
* function only if you're messing with bstr internals. Use with caution.
171
* @param[in] newrealptr
173
void bstr_adjust_realptr(bstr *b, void *newrealptr);
176
* Adjust bstring size. This does not change the size of the storage behind
177
* the bstring, just changes the field that keeps track of how many bytes
178
* there are in the storage. You will need to use this function only if
179
* you're messing with bstr internals. Use with caution.
184
void bstr_adjust_size(bstr *b, size_t newsize);
187
* Allocate a zero-length bstring, reserving space for at least size bytes.
190
* @return New string instance
192
bstr *bstr_alloc(size_t size);
195
* Checks whether bstring begins with another bstring. Case sensitive.
197
* @param[in] bhaystack
199
* @return 1 if true, otherwise 0.
201
int bstr_begins_with(const bstr *bhaystack, const bstr *bneedle);
204
* Checks whether bstring begins with NUL-terminated string. Case sensitive.
206
* @param[in] bhaystack
208
* @return 1 if true, otherwise 0.
210
int bstr_begins_with_c(const bstr *bhaystack, const char *cneedle);
213
* Checks whether bstring begins with NUL-terminated string. Case insensitive.
215
* @param[in] bhaystack
217
* @return 1 if true, otherwise 0.
219
int bstr_begins_with_c_nocase(const bstr *bhaystack, const char *cneedle);
222
* Checks whether the bstring begins with the given memory block. Case sensitive.
224
* @param[in] bhaystack
227
* @return 1 if true, otherwise 0.
229
int bstr_begins_with_mem(const bstr *bhaystack, const void *data, size_t len);
232
* Checks whether bstring begins with memory block. Case insensitive.
234
* @param[in] bhaystack
237
* @return 1 if true, otherwise 0.
239
int bstr_begins_with_mem_nocase(const bstr *bhaystack, const void *data, size_t len);
242
* Checks whether bstring begins with another bstring. Case insensitive.
244
* @param[in] bhaystack
246
* @return 1 if true, otherwise 0.
248
int bstr_begins_with_nocase(const bstr *bhaystack, const bstr *cneedle);
251
* Return the byte at the given position.
255
* @return The byte at the given location, or -1 if the position is out of range.
257
int bstr_char_at(const bstr *b, size_t pos);
260
* Return the byte at the given position, counting from the end of the string (e.g.,
261
* byte at position 0 is the last byte in the string.)
265
* @return The byte at the given location, or -1 if the position is out of range.
267
int bstr_char_at_end(const bstr *b, size_t pos);
270
* Remove the last byte from bstring, assuming it contains at least one byte. This
271
* function will not reduce the storage that backs the string, only the amount
276
void bstr_chop(bstr *b);
279
* Return the first position of the provided byte.
283
* @return The first position of the byte, or -1 if it could not be found
285
int bstr_chr(const bstr *b, int c);
288
* Case-sensitive comparison of two bstrings.
292
* @return Zero on string match, 1 if b1 is greater than b2, and -1 if b2 is
295
int bstr_cmp(const bstr *b1, const bstr *b2);
298
* Case-sensitive comparison of a bstring and a NUL-terminated string.
302
* @return Zero on string match, 1 if b is greater than cstr, and -1 if cstr is
305
int bstr_cmp_c(const bstr *b, const char *cstr);
308
* Case-insensitive comparison of a bstring with a NUL-terminated string.
312
* @return Zero on string match, 1 if b is greater than cstr, and -1 if cstr is greater than b.
314
int bstr_cmp_c_nocase(const bstr *b, const char *cstr);
317
* Performs a case-sensitive comparison of a bstring with a memory region.
322
* @return Zero ona match, 1 if b is greater than data, and -1 if data is greater than b.
324
int bstr_cmp_mem(const bstr *b, const void *data, size_t len);
327
* Performs a case-insensitive comparison of a bstring with a memory region.
332
* @return Zero ona match, 1 if b is greater than data, and -1 if data is greater than b.
334
int bstr_cmp_mem_nocase(const bstr *b, const void *data, size_t len);
337
* Case-insensitive comparison two bstrings.
341
* @return Zero on string match, 1 if b1 is greater than b2, and -1 if b2 is
344
int bstr_cmp_nocase(const bstr *b1, const bstr *b2);
347
* Create a new bstring by copying the provided bstring.
350
* @return New bstring, or NULL if memory allocation failed.
352
bstr *bstr_dup(const bstr *b);
355
* Create a new bstring by copying the provided NUL-terminated string.
358
* @return New bstring, or NULL if memory allocation failed.
360
bstr *bstr_dup_c(const char *cstr);
363
* Create a new bstring by copying a part of the provided bstring.
368
* @return New bstring, or NULL if memory allocation failed.
370
bstr *bstr_dup_ex(const bstr *b, size_t offset, size_t len);
373
* Create a copy of the provided bstring, then convert it to lowercase.
376
* @return New bstring, or NULL if memory allocation failed
378
bstr *bstr_dup_lower(const bstr *b);
381
* Create a new bstring by copying the provided memory region.
385
* @return New bstring, or NULL if memory allocation failed
387
bstr *bstr_dup_mem(const void *data, size_t len);
390
* Expand internal bstring storage to support at least newsize bytes. The storage
391
* is not expanded if the current size is equal or greater to newsize. Because
392
* realloc is used underneath, the old pointer to bstring may no longer be valid
393
* after this function completes successfully.
397
* @return Updated string instance, or NULL if memory allocation failed or if
398
* attempt was made to "expand" the bstring to a smaller size.
400
bstr *bstr_expand(bstr *b, size_t newsize);
403
* Deallocate the supplied bstring instance and set it to NULL. Allows NULL on
408
void bstr_free(bstr *b);
411
* Find the needle in the haystack.
413
* @param[in] bhaystack
415
* @return Position of the match, or -1 if the needle could not be found.
417
int bstr_index_of(const bstr *bhaystack, const bstr *bneedle);
420
* Find the needle in the haystack, ignoring case differences.
422
* @param[in] bhaystack
424
* @return Position of the match, or -1 if the needle could not be found.
426
int bstr_index_of_nocase(const bstr *bhaystack, const bstr *bneedle);
429
* Find the needle in the haystack, with the needle being a NUL-terminated
432
* @param[in] bhaystack
434
* @return Position of the match, or -1 if the needle could not be found.
436
int bstr_index_of_c(const bstr *bhaystack, const char *cneedle);
439
* Find the needle in the haystack, with the needle being a NUL-terminated
440
* string. Ignore case differences.
442
* @param[in] bhaystack
444
* @return Position of the match, or -1 if the needle could not be found.
446
int bstr_index_of_c_nocase(const bstr *bhaystack, const char *cneedle);
449
* Find the needle in the haystack, with the needle being a memory region.
451
* @param[in] bhaystack
454
* @return Position of the match, or -1 if the needle could not be found.
456
int bstr_index_of_mem(const bstr *bhaystack, const void *data, size_t len);
459
* Find the needle in the haystack, with the needle being a memory region.
460
* Ignore case differences.
462
* @param[in] bhaystack
465
* @return Position of the match, or -1 if the needle could not be found.
467
int bstr_index_of_mem_nocase(const bstr *bhaystack, const void *data, size_t len);
470
* Return the last position of a character (byte).
474
* @return The last position of the character, or -1 if it could not be found.
476
int bstr_rchr(const bstr *b, int c);
479
* Convert bstring to lowercase. This function converts the supplied string,
480
* it does not create a new string.
483
* @return The same bstring received on input
485
bstr *bstr_to_lowercase(bstr *b);
488
* Case-sensitive comparison of two memory regions.
494
* @return Zero if the memory regions are identical, 1 if data1 is greater than
495
* data2, and -1 if data2 is greater than data1.
497
int bstr_util_cmp_mem(const void *data1, size_t len1, const void *data2, size_t len2);
500
* Case-insensitive comparison of two memory regions.
506
* @return Zero if the memory regions are identical, 1 if data1 is greater than
507
* data2, and -1 if data2 is greater than data1.
509
int bstr_util_cmp_mem_nocase(const void *data1, size_t len1, const void *data2, size_t len2);
512
* Convert contents of a memory region to a positive integer.
516
* @param[in] base The desired number base.
517
* @param[in] lastlen Points to the first unused byte in the region
518
* @return If the conversion was successful, this function returns the
519
* number. When the conversion fails, -1 will be returned when not
520
* one valid digit was found, and -2 will be returned if an overflow
523
int64_t bstr_util_mem_to_pint(const void *data, size_t len, int base, size_t *lastlen);
526
* Searches a memory block for the given NUL-terminated string. Case sensitive.
531
* @return Index of the first location of the needle on success, or -1 if the needle was not found.
533
int bstr_util_mem_index_of_c(const void *data, size_t len, const char *cstr);
536
* Searches a memory block for the given NUL-terminated string. Case insensitive.
541
* @return Index of the first location of the needle on success, or -1 if the needle was not found.
543
int bstr_util_mem_index_of_c_nocase(const void *data, size_t len, const char *cstr);
546
* Searches the haystack memory block for the needle memory block. Case sensitive.
552
* @return Index of the first location of the needle on success, or -1 if the needle was not found.
554
int bstr_util_mem_index_of_mem(const void *data1, size_t len1, const void *data2, size_t len2);
557
* Searches the haystack memory block for the needle memory block. Case sensitive.
563
* @return Index of the first location of the needle on success, or -1 if the needle was not found.
565
int bstr_util_mem_index_of_mem_nocase(const void *data1, size_t len1, const void *data2, size_t len2);
568
* Removes whitespace from the beginning and the end of a memory region. The data
569
* itself is not modified; this function only adjusts the provided pointers.
571
* @param[in,out] data
574
void bstr_util_mem_trim(unsigned char **data, size_t *len);
577
* Take the provided memory region, allocate a new memory buffer, and construct
578
* a NUL-terminated string, replacing each NUL byte with "\0" (two bytes). The
579
* caller is responsible to keep track of the allocated memory area and free
580
* it once it is no longer needed.
584
* @return The newly created NUL-terminated string, or NULL in case of memory
585
* allocation failure.
587
char *bstr_util_memdup_to_c(const void *data, size_t len);
590
* Create a new NUL-terminated string out of the provided bstring. If NUL bytes
591
* are contained in the bstring, each will be replaced with "\0" (two characters).
592
* The caller is responsible to keep track of the allocated memory area and free
593
* it once it is no longer needed.
596
* @return The newly created NUL-terminated string, or NULL in case of memory
597
* allocation failure.
599
char *bstr_util_strdup_to_c(const bstr *b);
602
* Create a new bstring from the provided NUL-terminated string and without
603
* copying the data. The caller must ensure that the input string continues
604
* to point to a valid memory location for as long as the bstring is used.
607
* @return New bstring, or NULL on memory allocation failure.
609
bstr *bstr_wrap_c(const char *cstr);
612
* Create a new bstring from the provided memory buffer without
613
* copying the data. The caller must ensure that the buffer remains
614
* valid for as long as the bstring is used.
618
* @return New bstring, or NULL on memory allocation failure.
620
bstr *bstr_wrap_mem(const void *data, size_t len);
96
626
#endif /* _BSTR_H */