HTP  0.5
bstr.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2009-2010 Open Information Security Foundation
3  * Copyright (c) 2010-2013 Qualys, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12 
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.
16 
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.
20  *
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  ***************************************************************************/
33 
39 #ifndef _BSTR_H
40 #define _BSTR_H
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 typedef struct bstr_t bstr;
47 
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <stdint.h>
51 #include <string.h>
52 
53 #include "bstr_builder.h"
54 
55 // Data structures
56 
57 struct bstr_t {
59  size_t len;
60 
64  size_t size;
65 
71  unsigned char *realptr;
72 };
73 
74 
75 // Defines
76 
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)
81 
82 
83 // Functions
84 
95 bstr *bstr_add(bstr *bdestination, const bstr *bsource);
96 
107 bstr *bstr_add_c(bstr *b, const char *cstr);
108 
118 bstr *bstr_add_c_noex(bstr *b, const char *cstr);
119 
131 bstr *bstr_add_mem(bstr *b, const void *data, size_t len);
132 
143 bstr *bstr_add_mem_noex(bstr *b, const void *data, size_t len);
144 
154 bstr *bstr_add_noex(bstr *bdestination, const bstr *bsource);
155 
164 void bstr_adjust_len(bstr *b, size_t newlen);
165 
173 void bstr_adjust_realptr(bstr *b, void *newrealptr);
174 
184 void bstr_adjust_size(bstr *b, size_t newsize);
185 
192 bstr *bstr_alloc(size_t size);
193 
201 int bstr_begins_with(const bstr *bhaystack, const bstr *bneedle);
202 
210 int bstr_begins_with_c(const bstr *bhaystack, const char *cneedle);
211 
219 int bstr_begins_with_c_nocase(const bstr *bhaystack, const char *cneedle);
220 
229 int bstr_begins_with_mem(const bstr *bhaystack, const void *data, size_t len);
230 
239 int bstr_begins_with_mem_nocase(const bstr *bhaystack, const void *data, size_t len);
240 
248 int bstr_begins_with_nocase(const bstr *bhaystack, const bstr *cneedle);
249 
257 int bstr_char_at(const bstr *b, size_t pos);
258 
267 int bstr_char_at_end(const bstr *b, size_t pos);
268 
276 void bstr_chop(bstr *b);
277 
285 int bstr_chr(const bstr *b, int c);
286 
295 int bstr_cmp(const bstr *b1, const bstr *b2);
296 
305 int bstr_cmp_c(const bstr *b, const char *cstr);
306 
314 int bstr_cmp_c_nocase(const bstr *b, const char *cstr);
315 
324 int bstr_cmp_mem(const bstr *b, const void *data, size_t len);
325 
334 int bstr_cmp_mem_nocase(const bstr *b, const void *data, size_t len);
335 
344 int bstr_cmp_nocase(const bstr *b1, const bstr *b2);
345 
352 bstr *bstr_dup(const bstr *b);
353 
360 bstr *bstr_dup_c(const char *cstr);
361 
370 bstr *bstr_dup_ex(const bstr *b, size_t offset, size_t len);
371 
378 bstr *bstr_dup_lower(const bstr *b);
379 
387 bstr *bstr_dup_mem(const void *data, size_t len);
388 
400 bstr *bstr_expand(bstr *b, size_t newsize);
401 
408 void bstr_free(bstr *b);
409 
417 int bstr_index_of(const bstr *bhaystack, const bstr *bneedle);
418 
426 int bstr_index_of_nocase(const bstr *bhaystack, const bstr *bneedle);
427 
436 int bstr_index_of_c(const bstr *bhaystack, const char *cneedle);
437 
446 int bstr_index_of_c_nocase(const bstr *bhaystack, const char *cneedle);
447 
456 int bstr_index_of_mem(const bstr *bhaystack, const void *data, size_t len);
457 
467 int bstr_index_of_mem_nocase(const bstr *bhaystack, const void *data, size_t len);
468 
476 int bstr_rchr(const bstr *b, int c);
477 
486 
497 int bstr_util_cmp_mem(const void *data1, size_t len1, const void *data2, size_t len2);
498 
509  int bstr_util_cmp_mem_nocase(const void *data1, size_t len1, const void *data2, size_t len2);
510 
523 int64_t bstr_util_mem_to_pint(const void *data, size_t len, int base, size_t *lastlen);
524 
533 int bstr_util_mem_index_of_c(const void *data, size_t len, const char *cstr);
534 
543 int bstr_util_mem_index_of_c_nocase(const void *data, size_t len, const char *cstr);
544 
554 int bstr_util_mem_index_of_mem(const void *data1, size_t len1, const void *data2, size_t len2);
555 
565 int bstr_util_mem_index_of_mem_nocase(const void *data1, size_t len1, const void *data2, size_t len2);
566 
574 void bstr_util_mem_trim(unsigned char **data, size_t *len);
575 
587 char *bstr_util_memdup_to_c(const void *data, size_t len);
588 
599 char *bstr_util_strdup_to_c(const bstr *b);
600 
609 bstr *bstr_wrap_c(const char *cstr);
610 
620 bstr *bstr_wrap_mem(const void *data, size_t len);
621 
622 #ifdef __cplusplus
623 }
624 #endif
625 
626 #endif /* _BSTR_H */
int bstr_char_at_end(const bstr *b, size_t pos)
Definition: bstr.c:180
void bstr_free(bstr *b)
Definition: bstr.c:281
int bstr_util_mem_index_of_c_nocase(const void *_data1, size_t len1, const char *cstr)
Definition: bstr.c:457
int bstr_begins_with_nocase(const bstr *haystack, const bstr *needle)
Definition: bstr.c:126
void bstr_util_mem_trim(unsigned char **data, size_t *len)
Definition: bstr.c:507
bstr * bstr_dup_ex(const bstr *b, size_t offset, size_t len)
Definition: bstr.c:242
int bstr_cmp_mem_nocase(const bstr *b, const void *data, size_t len)
Definition: bstr.c:226
size_t size
Definition: bstr.h:64
bstr * bstr_add_c(bstr *bdestination, const char *csource)
Definition: bstr.c:58
int64_t bstr_util_mem_to_pint(const void *_data, size_t len, int base, size_t *lastlen)
Definition: bstr.c:391
int bstr_chr(const bstr *b, int c)
Definition: bstr.c:194
int bstr_begins_with_c(const bstr *haystack, const char *needle)
Definition: bstr.c:118
bstr * bstr_dup_c(const char *cstr)
Definition: bstr.c:238
int bstr_begins_with_c_nocase(const bstr *haystack, const char *needle)
Definition: bstr.c:122
int bstr_util_mem_index_of_c(const void *_data1, size_t len1, const char *cstr)
Definition: bstr.c:453
bstr * bstr_add_noex(bstr *destination, const bstr *source)
Definition: bstr.c:98
bstr * bstr_wrap_mem(const void *data, size_t len)
Definition: bstr.c:567
int bstr_util_cmp_mem(const void *_data1, size_t len1, const void *_data2, size_t len2)
Definition: bstr.c:341
bstr * bstr_dup_lower(const bstr *b)
Definition: bstr.c:250
int bstr_index_of_mem_nocase(const bstr *haystack, const void *_data2, size_t len2)
Definition: bstr.c:302
bstr * bstr_expand(bstr *b, size_t newsize)
Definition: bstr.c:262
size_t len
Definition: bstr.h:59
int bstr_util_mem_index_of_mem_nocase(const void *_data1, size_t len1, const void *_data2, size_t len2)
Definition: bstr.c:484
Definition: bstr.h:57
int bstr_begins_with(const bstr *haystack, const bstr *needle)
Definition: bstr.c:114
bstr * bstr_add_mem_noex(bstr *destination, const void *data, size_t len)
Definition: bstr.c:81
int bstr_begins_with_mem_nocase(const bstr *haystack, const void *_data, size_t len)
Definition: bstr.c:151
int bstr_rchr(const bstr *b, int c)
Definition: bstr.c:310
char * bstr_util_memdup_to_c(const void *_data, size_t len)
Definition: bstr.c:526
void bstr_adjust_size(bstr *b, size_t newsize)
Definition: bstr.c:110
bstr * bstr_add(bstr *destination, const bstr *source)
Definition: bstr.c:54
int bstr_util_cmp_mem_nocase(const void *_data1, size_t len1, const void *_data2, size_t len2)
Definition: bstr.c:366
int bstr_begins_with_mem(const bstr *haystack, const void *_data, size_t len)
Definition: bstr.c:130
bstr * bstr_dup(const bstr *b)
Definition: bstr.c:234
void bstr_chop(bstr *b)
Definition: bstr.c:188
int bstr_index_of_mem(const bstr *haystack, const void *_data2, size_t len2)
Definition: bstr.c:298
char * bstr_util_strdup_to_c(const bstr *b)
Definition: bstr.c:558
int bstr_index_of_c(const bstr *haystack, const char *needle)
Definition: bstr.c:290
bstr * bstr_add_c_noex(bstr *destination, const char *source)
Definition: bstr.c:62
int bstr_index_of_nocase(const bstr *haystack, const bstr *needle)
Definition: bstr.c:306
bstr * bstr_add_mem(bstr *destination, const void *data, size_t len)
Definition: bstr.c:66
int bstr_cmp(const bstr *b1, const bstr *b2)
Definition: bstr.c:210
bstr * bstr_alloc(size_t len)
Definition: bstr.c:43
bstr * bstr_dup_mem(const void *data, size_t len)
Definition: bstr.c:254
bstr * bstr_to_lowercase(bstr *b)
Definition: bstr.c:326
int bstr_cmp_nocase(const bstr *b1, const bstr *b2)
Definition: bstr.c:230
int bstr_util_mem_index_of_mem(const void *_data1, size_t len1, const void *_data2, size_t len2)
Definition: bstr.c:461
unsigned char * realptr
Definition: bstr.h:71
int bstr_index_of_c_nocase(const bstr *haystack, const char *needle)
Definition: bstr.c:294
int bstr_index_of(const bstr *haystack, const bstr *needle)
Definition: bstr.c:286
void bstr_adjust_realptr(bstr *b, void *newrealptr)
Definition: bstr.c:106
int bstr_cmp_c(const bstr *b, const char *c)
Definition: bstr.c:214
void bstr_adjust_len(bstr *b, size_t newlen)
Definition: bstr.c:102
int bstr_char_at(const bstr *b, size_t pos)
Definition: bstr.c:172
bstr * bstr_wrap_c(const char *cstr)
Definition: bstr.c:563
int bstr_cmp_mem(const bstr *b, const void *data, size_t len)
Definition: bstr.c:222
int bstr_cmp_c_nocase(const bstr *b, const char *c)
Definition: bstr.c:218