00001 // $Id: BroString.h,v 1.1 2004/07/14 20:15:40 jason Exp $ 00002 // 00003 // Copyright (c) 1997, 1998, 1999, 2002, 2003 00004 // The Regents of the University of California. All rights reserved. 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that: (1) source code distributions 00008 // retain the above copyright notice and this paragraph in its entirety, (2) 00009 // distributions including binary code include the above copyright notice and 00010 // this paragraph in its entirety in the documentation or other materials 00011 // provided with the distribution, and (3) all advertising materials mentioning 00012 // features or use of this software display the following acknowledgement: 00013 // ``This product includes software developed by the University of California, 00014 // Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 00015 // the University nor the names of its contributors may be used to endorse 00016 // or promote products derived from this software without specific prior 00017 // written permission. 00018 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 00019 // WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 00020 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00021 00022 #ifndef brostring_h 00023 #define brostring_h 00024 00025 #include <vector> 00026 #include <stdlib.h> 00027 #include <sys/types.h> 00028 using namespace std; 00029 00030 #include "util.h" 00031 00032 typedef u_char* byte_vec; 00033 00034 class BroString { 00035 public: 00036 BroString(int arg_final_NUL, byte_vec str, int arg_n); 00037 BroString(const u_char* str, int arg_n, int add_NUL); 00038 BroString(const char* str); 00039 ~BroString() 00040 { 00041 if ( use_free_to_delete ) 00042 free(b); 00043 else 00044 delete [] b; 00045 } 00046 00047 byte_vec Bytes() const { return b; } 00048 int Len() const { return n; } 00049 00050 void SetUseFreeToDelete(int use_it) 00051 { use_free_to_delete = use_it; } 00052 00053 const char* CheckString() const; 00054 00055 enum expansion_style { 00056 EXPANDED_STRING, // the original style 00057 BRO_STRING_LITERAL, // as in a Bro string literal 00058 }; 00059 char* ExpandedString(int format = EXPANDED_STRING) const; 00060 00061 void ToUpper(); 00062 00063 unsigned int MemoryAllocation() const 00064 { return padded_sizeof(*this) + pad_size(n + final_NUL); } 00065 00066 protected: 00067 byte_vec b; 00068 int n; 00069 unsigned int final_NUL:1; // whether we have added a final NUL 00070 unsigned int use_free_to_delete:1; // free() vs. operator delete 00071 }; 00072 00073 extern int Bstr_eq(const BroString* s1, const BroString* s2); 00074 extern int Bstr_cmp(const BroString* s1, const BroString* s2); 00075 00076 // A data_chunk_t specifies a length-delimited constant string. It is 00077 // often used for substrings of other BroString's to avoid memory copy, 00078 // which would be necessary if BroString were used. Unlike BroString, 00079 // the string should not be deallocated on destruction. 00080 // 00081 // "BroConstString" might be a better name here. 00082 00083 struct data_chunk_t { 00084 int length; 00085 const char* data; 00086 }; 00087 00088 extern BroString* concatenate(std::vector<data_chunk_t>& v); 00089 extern BroString* concatenate(std::vector<const BroString*>& v); 00090 extern void delete_strings(std::vector<const BroString*>& v); 00091 00092 #endif
1.3.5