HTP  0.3
bstr.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  * Copyright (c) 2009-2010, Open Information Security Foundation
00003  * Copyright (c) 2009-2012, Qualys, Inc.
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are
00008  * met:
00009  * 
00010  * * Redistributions of source code must retain the above copyright
00011  * notice, this list of conditions and the following disclaimer.
00012  * * Redistributions in binary form must reproduce the above copyright
00013  * notice, this list of conditions and the following disclaimer in the
00014  * documentation and/or other materials provided with the distribution.
00015  * * Neither the name of the Qualys, Inc. nor the names of its
00016  * contributors may be used to endorse or promote products derived from
00017  * this software without specific prior written permission.
00018  * 
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  ***************************************************************************/
00031 
00037 #ifndef _BSTR_H
00038 #define _BSTR_H
00039 
00040 typedef struct bstr_t bstr_t;
00041 typedef bstr_t bstr;
00042 
00043 #include <stdio.h>
00044 #include <stdlib.h>
00045 #include <stdint.h>
00046 #include <string.h>
00047 
00048 #include "bstr_builder.h"
00049 
00050 #ifdef __cplusplus
00051 extern "C" {
00052 #endif
00053 
00054 // IMPORTANT This binary string library is used internally by the parser and you should
00055 //           not rely on it in your code. The interface and the implementation may change
00056 //           without warning.
00057 
00058 struct bstr_t {
00060     size_t len;
00061 
00065     size_t size;
00066 
00072     char *ptr;
00073 };
00074 
00075 
00076 // Defines
00077 
00078 #define bstr_len(X) ((*(bstr_t *)(X)).len)
00079 #define bstr_size(X) ((*(bstr_t *)(X)).size)
00080 #define bstr_ptr(X) ( ((*(bstr_t *)(X)).ptr == NULL) ? ((char *)(X) + sizeof(bstr_t)) : (char *)(*(bstr_t *)(X)).ptr )
00081 
00082 
00083 // Functions
00084 
00085 bstr *bstr_alloc(size_t newsize);
00086  void bstr_free(bstr **s);
00087 bstr *bstr_expand(bstr *s, size_t newsize);
00088 
00089 bstr *bstr_dup(const bstr *b);
00090 bstr *bstr_dup_ex(const bstr *b, size_t offset, size_t len);
00091 bstr *bstr_dup_c(const char *);
00092 bstr *bstr_dup_mem(const char *data, size_t len);
00093 
00094 bstr *bstr_dup_lower(const bstr *);
00095 
00096   int bstr_chr(const bstr *, int);
00097   int bstr_rchr(const bstr *, int);
00098 
00099   int bstr_cmp(const bstr *, const bstr *);
00100   int bstr_cmp_nocase(const bstr *, const bstr *);
00101   int bstr_cmp_c(const bstr *, const char *);
00102   int bstr_cmp_c_nocase(const bstr *, const char *);
00103   int bstr_cmp_ex(const char *, size_t, const char *, size_t);
00104   int bstr_cmp_nocase_ex(const char *, size_t, const char *, size_t);
00105 
00106 
00107 bstr *bstr_to_lowercase(bstr *);
00108 
00109 bstr *bstr_add(bstr *, const bstr *);
00110 bstr *bstr_add_c(bstr *, const char *);
00111 bstr *bstr_add_mem(bstr *, const char *, size_t);
00112 
00113 bstr *bstr_add_noex(bstr *, const bstr *);
00114 bstr *bstr_add_c_noex(bstr *, const char *);
00115 bstr *bstr_add_mem_noex(bstr *, const char *, size_t);
00116 
00117   int bstr_index_of(const bstr *haystack, const bstr *needle);
00118   int bstr_index_of_nocase(const bstr *haystack, const bstr *needle);
00119   int bstr_index_of_c(const bstr *haystack, const char *needle);
00120   int bstr_index_of_c_nocase(const bstr *haystack, const char *needle);
00121   int bstr_index_of_mem(const bstr *haystack, const char *data, size_t len);
00122   int bstr_index_of_mem_nocase(const bstr *haystack, const char *data, size_t len);
00123 
00124   int bstr_begins_with_mem(const bstr *haystack, const char *data, size_t len);
00125   int bstr_begins_with_mem_nocase(const bstr *haystack, const char *data, size_t len);
00126   int bstr_begins_with(const bstr *haystack, const bstr *needle);
00127   int bstr_begins_with_c(const bstr *haystack, const char *needle);
00128   int bstr_begins_with_nocase(const bstr *haystack, const bstr *needle);
00129   int bstr_begins_withc_nocase(const bstr *haystack, const char *needle);
00130 
00131 unsigned char bstr_char_at(const bstr *s, size_t pos);
00132 
00133    void bstr_chop(bstr *b);
00134    void bstr_util_adjust_len(bstr *s, size_t newlen);
00135 int64_t bstr_util_mem_to_pint(const char *data, size_t len, int base, size_t *lastlen);
00136   char *bstr_util_memdup_to_c(const char *data, size_t len);
00137   char *bstr_util_strdup_to_c(const bstr *);
00138 
00139 #ifdef __cplusplus
00140 }
00141 #endif
00142 
00143 #endif  /* _BSTR_H */