HTP  0.3
bstr.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2009-2010, Open Information Security Foundation
3  * Copyright (c) 2009-2012, 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  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * * Neither the name of the Qualys, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  ***************************************************************************/
31 
37 #ifndef _BSTR_H
38 #define _BSTR_H
39 
40 typedef struct bstr_t bstr_t;
41 typedef bstr_t bstr;
42 
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <stdint.h>
46 #include <string.h>
47 
48 #include "bstr_builder.h"
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 // IMPORTANT This binary string library is used internally by the parser and you should
55 // not rely on it in your code. The interface and the implementation may change
56 // without warning.
57 
58 struct bstr_t {
60  size_t len;
61 
65  size_t size;
66 
72  char *ptr;
73 };
74 
75 
76 // Defines
77 
78 #define bstr_len(X) ((*(bstr_t *)(X)).len)
79 #define bstr_size(X) ((*(bstr_t *)(X)).size)
80 #define bstr_ptr(X) ( ((*(bstr_t *)(X)).ptr == NULL) ? ((char *)(X) + sizeof(bstr_t)) : (char *)(*(bstr_t *)(X)).ptr )
81 
82 
83 // Functions
84 
85 bstr *bstr_alloc(size_t newsize);
86  void bstr_free(bstr **s);
87 bstr *bstr_expand(bstr *s, size_t newsize);
88 
89 bstr *bstr_dup(const bstr *b);
90 bstr *bstr_dup_ex(const bstr *b, size_t offset, size_t len);
91 bstr *bstr_dup_c(const char *);
92 bstr *bstr_dup_mem(const char *data, size_t len);
93 
94 bstr *bstr_dup_lower(const bstr *);
95 
96  int bstr_chr(const bstr *, int);
97  int bstr_rchr(const bstr *, int);
98 
99  int bstr_cmp(const bstr *, const bstr *);
100  int bstr_cmp_nocase(const bstr *, const bstr *);
101  int bstr_cmp_c(const bstr *, const char *);
102  int bstr_cmp_c_nocase(const bstr *, const char *);
103  int bstr_cmp_ex(const char *, size_t, const char *, size_t);
104  int bstr_cmp_nocase_ex(const char *, size_t, const char *, size_t);
105 
106 
108 
109 bstr *bstr_add(bstr *, const bstr *);
110 bstr *bstr_add_c(bstr *, const char *);
111 bstr *bstr_add_mem(bstr *, const char *, size_t);
112 
113 bstr *bstr_add_noex(bstr *, const bstr *);
114 bstr *bstr_add_c_noex(bstr *, const char *);
115 bstr *bstr_add_mem_noex(bstr *, const char *, size_t);
116 
117  int bstr_index_of(const bstr *haystack, const bstr *needle);
118  int bstr_index_of_nocase(const bstr *haystack, const bstr *needle);
119  int bstr_index_of_c(const bstr *haystack, const char *needle);
120  int bstr_index_of_c_nocase(const bstr *haystack, const char *needle);
121  int bstr_index_of_mem(const bstr *haystack, const char *data, size_t len);
122  int bstr_index_of_mem_nocase(const bstr *haystack, const char *data, size_t len);
123 
124  int bstr_begins_with_mem(const bstr *haystack, const char *data, size_t len);
125  int bstr_begins_with_mem_nocase(const bstr *haystack, const char *data, size_t len);
126  int bstr_begins_with(const bstr *haystack, const bstr *needle);
127  int bstr_begins_with_c(const bstr *haystack, const char *needle);
128  int bstr_begins_with_nocase(const bstr *haystack, const bstr *needle);
129  int bstr_begins_withc_nocase(const bstr *haystack, const char *needle);
130 
131 unsigned char bstr_char_at(const bstr *s, size_t pos);
132 
133  void bstr_chop(bstr *b);
134  void bstr_util_adjust_len(bstr *s, size_t newlen);
135 int64_t bstr_util_mem_to_pint(const char *data, size_t len, int base, size_t *lastlen);
136  char *bstr_util_memdup_to_c(const char *data, size_t len);
137  char *bstr_util_strdup_to_c(const bstr *);
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 #endif /* _BSTR_H */