~ubuntu-branches/ubuntu/wily/libhtp/wily

« back to all changes in this revision

Viewing changes to htp/bstr.h

  • Committer: Package Import Robot
  • Author(s): Pierre Chifflier
  • Date: 2014-04-02 19:02:35 UTC
  • mfrom: (10.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20140402190235-28hezclrzqg40iqi
* Upload to unstable
* Imported Upstream version 0.5.10
* Use Debian source format 3.0 (quilt)
* Use dh-autoreconf during build
* Add patch to fix missing script, causing error during autoreconf
* Update override file for so name change
* Update symbols file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * LibHTP (http://www.libhtp.org)
3
 
 * Copyright 2009,2010 Ivan Ristic <ivanr@webkreator.com>
4
 
 *
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
7
 
 * of the license.
8
 
 *
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.
12
 
 *
 
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
 
 
34
/**
 
35
 * @file
 
36
 * @author Ivan Ristic <ivanr@webkreator.com>
13
37
 */
14
38
 
15
39
#ifndef _BSTR_H
16
40
#define _BSTR_H
17
41
 
 
42
#ifdef __cplusplus
 
43
extern "C" {
 
44
#endif
 
45
 
 
46
typedef struct bstr_t bstr;
 
47
 
18
48
#include <stdio.h>
19
49
#include <stdlib.h>
 
50
#include <stdint.h>
20
51
#include <string.h>
21
52
 
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.
24
 
//
25
 
// TODO
26
 
//           - Add a function that wraps an existing data
27
 
//           - Support Unicode bstrings
28
 
 
29
 
typedef void * bstr;
30
 
 
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 *);
39
 
 
40
 
int bstr_chr(bstr *, int);
41
 
int bstr_rchr(bstr *, int);
42
 
 
43
 
int bstr_cmpc(bstr *, char *);
44
 
int bstr_cmp(bstr *, bstr *);
45
 
int bstr_cmp_nocase(bstr *, bstr *);
46
 
 
47
 
bstr *bstr_dup_lower(bstr *);
48
 
bstr *bstr_tolowercase(bstr *);
49
 
 
50
 
bstr *bstr_add_mem(bstr *, char *, size_t);
51
 
bstr *bstr_add_str(bstr *, bstr *);
52
 
bstr *bstr_add_cstr(bstr *, char *);
53
 
 
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 *);
57
 
 
58
 
int bstr_util_memtoip(char *data, size_t len, int base, size_t *lastlen);
59
 
char *bstr_memtocstr(char *data, size_t len);
60
 
 
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);
67
 
 
68
 
void bstr_chop(bstr *b);
69
 
void bstr_len_adjust(bstr *s, size_t newlen);
70
 
 
71
 
char bstr_char_at(bstr *s, size_t pos);
72
 
 
73
 
typedef struct bstr_t bstr_t;
74
 
 
 
53
#include "bstr_builder.h"
 
54
 
 
55
// Data structures
 
56
    
75
57
struct bstr_t {
76
58
    /** The length of the string stored in the buffer. */
77
59
    size_t len;
78
 
      
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.
 
60
 
 
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.
81
63
     */
82
64
    size_t size;
83
65
 
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
 
69
     *  this structure.
88
70
     */
89
 
    char *ptr;
 
71
    unsigned char *realptr;
90
72
};
91
73
 
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 )
 
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
 
 
85
/**
 
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.
 
90
 *
 
91
 * @param[in] bdestination
 
92
 * @param[in] bsource
 
93
 * @return Updated bstring, or NULL on memory allocation failure.
 
94
 */
 
95
bstr *bstr_add(bstr *bdestination, const bstr *bsource);
 
96
 
 
97
/**
 
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.
 
102
 *
 
103
 * @param[in] b
 
104
 * @param[in] cstr
 
105
 * @return Updated bstring, or NULL on memory allocation failure.
 
106
 */
 
107
bstr *bstr_add_c(bstr *b, const char *cstr);
 
108
 
 
109
/**
 
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.
 
113
 *
 
114
 * @param[in] b
 
115
 * @param[in] cstr
 
116
 * @return The destination bstring.
 
117
 */
 
118
bstr *bstr_add_c_noex(bstr *b, const char *cstr);
 
119
 
 
120
/**
 
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.
 
125
 *
 
126
 * @param[in] b
 
127
 * @param[in] data
 
128
 * @param[in] len
 
129
 * @return Updated bstring, or NULL on memory allocation failure.
 
130
 */
 
131
bstr *bstr_add_mem(bstr *b, const void *data, size_t len);
 
132
 
 
133
/**
 
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.
 
137
 *
 
138
 * @param[in] b
 
139
 * @param[in] data
 
140
 * @param[in] len
 
141
 * @return The destination bstring.
 
142
 */
 
143
bstr *bstr_add_mem_noex(bstr *b, const void *data, size_t len);
 
144
 
 
145
/**
 
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.
 
149
 *
 
150
 * @param[in] bdestination
 
151
 * @param[in] bsource
 
152
 * @return The destination bstring.
 
153
 */
 
154
bstr *bstr_add_noex(bstr *bdestination, const bstr *bsource);
 
155
 
 
156
/**
 
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.
 
160
 *
 
161
 * @param[in] b
 
162
 * @param[in] newlen
 
163
 */
 
164
void bstr_adjust_len(bstr *b, size_t newlen);
 
165
 
 
166
/**
 
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.
 
169
 *
 
170
 * @param[in] b
 
171
 * @param[in] newrealptr
 
172
 */
 
173
void bstr_adjust_realptr(bstr *b, void *newrealptr);
 
174
 
 
175
/**
 
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.
 
180
 *
 
181
 * @param[in] b
 
182
 * @param[in] newsize
 
183
 */
 
184
void bstr_adjust_size(bstr *b, size_t newsize);
 
185
 
 
186
/**
 
187
 * Allocate a zero-length bstring, reserving space for at least size bytes.
 
188
 *
 
189
 * @param[in] size
 
190
 * @return New string instance
 
191
 */
 
192
bstr *bstr_alloc(size_t size);
 
193
 
 
194
/**
 
195
 * Checks whether bstring begins with another bstring. Case sensitive.
 
196
 * 
 
197
 * @param[in] bhaystack
 
198
 * @param[in] bneedle
 
199
 * @return 1 if true, otherwise 0.
 
200
 */
 
201
int bstr_begins_with(const bstr *bhaystack, const bstr *bneedle);
 
202
 
 
203
/**
 
204
 * Checks whether bstring begins with NUL-terminated string. Case sensitive.
 
205
 *
 
206
 * @param[in] bhaystack
 
207
 * @param[in] cneedle
 
208
 * @return 1 if true, otherwise 0.
 
209
 */
 
210
int bstr_begins_with_c(const bstr *bhaystack, const char *cneedle);
 
211
 
 
212
/**
 
213
 * Checks whether bstring begins with NUL-terminated string. Case insensitive.
 
214
 *
 
215
 * @param[in] bhaystack
 
216
 * @param[in] cneedle
 
217
 * @return 1 if true, otherwise 0.
 
218
 */
 
219
int bstr_begins_with_c_nocase(const bstr *bhaystack, const char *cneedle);
 
220
 
 
221
/**
 
222
 * Checks whether the bstring begins with the given memory block. Case sensitive.
 
223
 *
 
224
 * @param[in] bhaystack
 
225
 * @param[in] data
 
226
 * @param[in] len
 
227
 * @return 1 if true, otherwise 0.
 
228
 */
 
229
int bstr_begins_with_mem(const bstr *bhaystack, const void *data, size_t len);
 
230
 
 
231
/**
 
232
 * Checks whether bstring begins with memory block. Case insensitive.
 
233
 *
 
234
 * @param[in] bhaystack
 
235
 * @param[in] data
 
236
 * @param[in] len
 
237
 * @return 1 if true, otherwise 0.
 
238
 */
 
239
int bstr_begins_with_mem_nocase(const bstr *bhaystack, const void *data, size_t len);
 
240
 
 
241
/**
 
242
 * Checks whether bstring begins with another bstring. Case insensitive.
 
243
 *
 
244
 * @param[in] bhaystack
 
245
 * @param[in] cneedle
 
246
 * @return 1 if true, otherwise 0.
 
247
 */
 
248
int bstr_begins_with_nocase(const bstr *bhaystack, const bstr *cneedle);
 
249
 
 
250
/**
 
251
 * Return the byte at the given position.
 
252
 *
 
253
 * @param[in] b
 
254
 * @param[in] pos
 
255
 * @return The byte at the given location, or -1 if the position is out of range.
 
256
 */
 
257
int bstr_char_at(const bstr *b, size_t pos);
 
258
 
 
259
/**
 
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.)
 
262
 *
 
263
 * @param[in] b
 
264
 * @param[in] pos
 
265
 * @return The byte at the given location, or -1 if the position is out of range.
 
266
 */
 
267
int bstr_char_at_end(const bstr *b, size_t pos);
 
268
 
 
269
/**
 
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
 
272
 * of data used.
 
273
 *
 
274
 * @param[in] b
 
275
 */
 
276
void bstr_chop(bstr *b);
 
277
 
 
278
/**
 
279
 * Return the first position of the provided byte.
 
280
 *
 
281
 * @param[in] b
 
282
 * @param[in] c
 
283
 * @return The first position of the byte, or -1 if it could not be found
 
284
 */
 
285
int bstr_chr(const bstr *b, int c);
 
286
 
 
287
/**
 
288
 * Case-sensitive comparison of two bstrings.
 
289
 *
 
290
 * @param[in] b1
 
291
 * @param[in] b2
 
292
 * @return Zero on string match, 1 if b1 is greater than b2, and -1 if b2 is
 
293
 *         greater than b1.
 
294
 */
 
295
int bstr_cmp(const bstr *b1, const bstr *b2);
 
296
  
 
297
/**
 
298
 * Case-sensitive comparison of a bstring and a NUL-terminated string.
 
299
 *
 
300
 * @param[in] b
 
301
 * @param[in] cstr
 
302
 * @return Zero on string match, 1 if b is greater than cstr, and -1 if cstr is
 
303
 *         greater than b.
 
304
 */
 
305
int bstr_cmp_c(const bstr *b, const char *cstr);
 
306
 
 
307
/**
 
308
 * Case-insensitive comparison of a bstring with a NUL-terminated string.
 
309
 *
 
310
 * @param[in] b
 
311
 * @param[in] cstr
 
312
 * @return Zero on string match, 1 if b is greater than cstr, and -1 if cstr is greater than b.
 
313
 */
 
314
int bstr_cmp_c_nocase(const bstr *b, const char *cstr);
 
315
 
 
316
/**
 
317
 * Performs a case-sensitive comparison of a bstring with a memory region.
 
318
 *
 
319
 * @param[in] b
 
320
 * @param[in] data
 
321
 * @param[in] len
 
322
 * @return Zero ona match, 1 if b is greater than data, and -1 if data is greater than b.
 
323
 */
 
324
int bstr_cmp_mem(const bstr *b, const void *data, size_t len);
 
325
 
 
326
/**
 
327
 * Performs a case-insensitive comparison of a bstring with a memory region.
 
328
 *
 
329
 * @param[in] b
 
330
 * @param[in] data
 
331
 * @param[in] len
 
332
 * @return Zero ona match, 1 if b is greater than data, and -1 if data is greater than b.
 
333
 */
 
334
int bstr_cmp_mem_nocase(const bstr *b, const void *data, size_t len);
 
335
 
 
336
/**
 
337
 * Case-insensitive comparison two bstrings.
 
338
 *
 
339
 * @param[in] b1
 
340
 * @param[in] b2
 
341
 * @return Zero on string match, 1 if b1 is greater than b2, and -1 if b2 is
 
342
 *         greater than b1.
 
343
 */
 
344
int bstr_cmp_nocase(const bstr *b1, const bstr *b2);
 
345
 
 
346
/**
 
347
 * Create a new bstring by copying the provided bstring.
 
348
 *
 
349
 * @param[in] b
 
350
 * @return New bstring, or NULL if memory allocation failed.
 
351
 */
 
352
bstr *bstr_dup(const bstr *b);
 
353
 
 
354
/**
 
355
 * Create a new bstring by copying the provided NUL-terminated string.
 
356
 *
 
357
 * @param[in] cstr
 
358
 * @return New bstring, or NULL if memory allocation failed.
 
359
 */
 
360
bstr *bstr_dup_c(const char *cstr);
 
361
 
 
362
/**
 
363
 * Create a new bstring by copying a part of the provided bstring.
 
364
 *
 
365
 * @param[in] b
 
366
 * @param[in] offset
 
367
 * @param[in] len
 
368
 * @return New bstring, or NULL if memory allocation failed.
 
369
 */
 
370
bstr *bstr_dup_ex(const bstr *b, size_t offset, size_t len);
 
371
 
 
372
/**
 
373
 * Create a copy of the provided bstring, then convert it to lowercase.
 
374
 *
 
375
 * @param[in] b
 
376
 * @return New bstring, or NULL if memory allocation failed
 
377
 */
 
378
bstr *bstr_dup_lower(const bstr *b);
 
379
 
 
380
/**
 
381
 * Create a new bstring by copying the provided memory region.
 
382
 *
 
383
 * @param[in] data
 
384
 * @param[in] len
 
385
 * @return New bstring, or NULL if memory allocation failed
 
386
 */
 
387
bstr *bstr_dup_mem(const void *data, size_t len);
 
388
 
 
389
/**
 
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.
 
394
 *
 
395
 * @param[in] b
 
396
 * @param[in] newsize
 
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.
 
399
 */
 
400
bstr *bstr_expand(bstr *b, size_t newsize);
 
401
 
 
402
/**
 
403
 * Deallocate the supplied bstring instance and set it to NULL. Allows NULL on
 
404
 * input.
 
405
 *
 
406
 * @param[in] b
 
407
 */
 
408
void bstr_free(bstr *b);
 
409
 
 
410
/**
 
411
 * Find the needle in the haystack.
 
412
 *
 
413
 * @param[in] bhaystack
 
414
 * @param[in] bneedle
 
415
 * @return Position of the match, or -1 if the needle could not be found.
 
416
 */
 
417
int bstr_index_of(const bstr *bhaystack, const bstr *bneedle);
 
418
 
 
419
/**
 
420
 * Find the needle in the haystack, ignoring case differences.
 
421
 *
 
422
 * @param[in] bhaystack
 
423
 * @param[in] bneedle
 
424
 * @return Position of the match, or -1 if the needle could not be found.
 
425
 */
 
426
int bstr_index_of_nocase(const bstr *bhaystack, const bstr *bneedle);
 
427
 
 
428
/**
 
429
 * Find the needle in the haystack, with the needle being a NUL-terminated
 
430
 * string.
 
431
 *
 
432
 * @param[in] bhaystack
 
433
 * @param[in] cneedle
 
434
 * @return Position of the match, or -1 if the needle could not be found.
 
435
 */
 
436
int bstr_index_of_c(const bstr *bhaystack, const char *cneedle);
 
437
 
 
438
/**
 
439
 * Find the needle in the haystack, with the needle being a NUL-terminated
 
440
 * string. Ignore case differences.
 
441
 *
 
442
 * @param[in] bhaystack
 
443
 * @param[in] cneedle
 
444
 * @return Position of the match, or -1 if the needle could not be found.
 
445
 */
 
446
int bstr_index_of_c_nocase(const bstr *bhaystack, const char *cneedle);
 
447
 
 
448
/**
 
449
 * Find the needle in the haystack, with the needle being a memory region.
 
450
 *
 
451
 * @param[in] bhaystack
 
452
 * @param[in] data
 
453
 * @param[in] len
 
454
 * @return Position of the match, or -1 if the needle could not be found.
 
455
 */
 
456
int bstr_index_of_mem(const bstr *bhaystack, const void *data, size_t len);
 
457
 
 
458
/**
 
459
 * Find the needle in the haystack, with the needle being a memory region.
 
460
 * Ignore case differences.
 
461
 *
 
462
 * @param[in] bhaystack
 
463
 * @param[in] data
 
464
 * @param[in] len
 
465
 * @return Position of the match, or -1 if the needle could not be found.
 
466
 */
 
467
int bstr_index_of_mem_nocase(const bstr *bhaystack, const void *data, size_t len);
 
468
 
 
469
/**
 
470
 * Return the last position of a character (byte).
 
471
 *
 
472
 * @param[in] b
 
473
 * @param[in] c
 
474
 * @return The last position of the character, or -1 if it could not be found.
 
475
 */
 
476
int bstr_rchr(const bstr *b, int c);
 
477
 
 
478
/**
 
479
 * Convert bstring to lowercase. This function converts the supplied string,
 
480
 * it does not create a new string.
 
481
 *
 
482
 * @param[in] b
 
483
 * @return The same bstring received on input
 
484
 */
 
485
bstr *bstr_to_lowercase(bstr *b);
 
486
 
 
487
/**
 
488
 * Case-sensitive comparison of two memory regions.
 
489
 *
 
490
 * @param[in] data1
 
491
 * @param[in] len1
 
492
 * @param[in] data2
 
493
 * @param[in] len2
 
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.
 
496
 */
 
497
int bstr_util_cmp_mem(const void *data1, size_t len1, const void *data2, size_t len2);
 
498
 
 
499
/**
 
500
 * Case-insensitive comparison of two memory regions.
 
501
 *
 
502
 * @param[in] data1
 
503
 * @param[in] len1
 
504
 * @param[in] data2
 
505
 * @param[in] len2
 
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.
 
508
 */
 
509
 int bstr_util_cmp_mem_nocase(const void *data1, size_t len1, const void *data2, size_t len2);
 
510
 
 
511
/**
 
512
 * Convert contents of a memory region to a positive integer.
 
513
 *
 
514
 * @param[in] data
 
515
 * @param[in] len
 
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
 
521
 *         occurred.
 
522
 */   
 
523
int64_t bstr_util_mem_to_pint(const void *data, size_t len, int base, size_t *lastlen);
 
524
 
 
525
/**
 
526
 * Searches a memory block for the given NUL-terminated string. Case sensitive.
 
527
 *
 
528
 * @param[in] data
 
529
 * @param[in] len
 
530
 * @param[in] cstr
 
531
 * @return Index of the first location of the needle on success, or -1 if the needle was not found.
 
532
 */
 
533
int bstr_util_mem_index_of_c(const void *data, size_t len, const char *cstr);
 
534
 
 
535
/**
 
536
 * Searches a memory block for the given NUL-terminated string. Case insensitive.
 
537
 *
 
538
 * @param[in] data
 
539
 * @param[in] len
 
540
 * @param[in] cstr
 
541
 * @return Index of the first location of the needle on success, or -1 if the needle was not found.
 
542
 */
 
543
int bstr_util_mem_index_of_c_nocase(const void *data, size_t len, const char *cstr);
 
544
 
 
545
/**
 
546
 * Searches the haystack memory block for the needle memory block. Case sensitive.
 
547
 *
 
548
 * @param data1
 
549
 * @param len1
 
550
 * @param data2
 
551
 * @param len2
 
552
 * @return Index of the first location of the needle on success, or -1 if the needle was not found.
 
553
 */
 
554
int bstr_util_mem_index_of_mem(const void *data1, size_t len1, const void *data2, size_t len2);
 
555
 
 
556
/**
 
557
 * Searches the haystack memory block for the needle memory block. Case sensitive.
 
558
 *
 
559
 * @param data1
 
560
 * @param len1
 
561
 * @param data2
 
562
 * @param len2
 
563
 * @return Index of the first location of the needle on success, or -1 if the needle was not found.
 
564
 */
 
565
int bstr_util_mem_index_of_mem_nocase(const void *data1, size_t len1, const void *data2, size_t len2);
 
566
 
 
567
/**
 
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.
 
570
 *
 
571
 * @param[in,out] data
 
572
 * @param[in,out] len
 
573
 */
 
574
void bstr_util_mem_trim(unsigned char **data, size_t *len);
 
575
 
 
576
/**
 
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.
 
581
 *
 
582
 * @param[in] data
 
583
 * @param[in] len
 
584
 * @return The newly created NUL-terminated string, or NULL in case of memory
 
585
 *         allocation failure.
 
586
 */
 
587
char *bstr_util_memdup_to_c(const void *data, size_t len);
 
588
 
 
589
/**
 
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.
 
594
 *
 
595
 * @param[in] b
 
596
 * @return The newly created NUL-terminated string, or NULL in case of memory
 
597
 *         allocation failure.
 
598
 */
 
599
char *bstr_util_strdup_to_c(const bstr *b);
 
600
  
 
601
/**
 
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.
 
605
 * 
 
606
 * @param[in] cstr
 
607
 * @return New bstring, or NULL on memory allocation failure.
 
608
 */
 
609
bstr *bstr_wrap_c(const char *cstr);
 
610
 
 
611
/**
 
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.
 
615
 *
 
616
 * @param[in] data
 
617
 * @param[in] len
 
618
 * @return New bstring, or NULL on memory allocation failure.
 
619
 */
 
620
bstr *bstr_wrap_mem(const void *data, size_t len);
 
621
 
 
622
#ifdef __cplusplus
 
623
}
 
624
#endif
95
625
 
96
626
#endif  /* _BSTR_H */
97