LCOV - code coverage report
Current view: top level - src - BroString.h (source / functions) Hit Total Coverage
Test: app.info Lines: 6 8 75.0 %
Date: 2010-12-13 Functions: 5 7 71.4 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // $Id: BroString.h 6219 2008-10-01 05:39:07Z vern $
       2                 :            : //
       3                 :            : // See the file "COPYING" in the main distribution directory for copyright.
       4                 :            : 
       5                 :            : #ifndef brostring_h
       6                 :            : #define brostring_h
       7                 :            : 
       8                 :            : #include <vector>
       9                 :            : #include <string>
      10                 :            : #include <iostream>
      11                 :            : #include <stdlib.h>
      12                 :            : #include <sys/types.h>
      13                 :            : using namespace std;
      14                 :            : 
      15                 :            : #include "util.h"
      16                 :            : 
      17                 :            : typedef u_char* byte_vec;
      18                 :            : 
      19                 :            : // Forward declaration, for helper functions that convert (sub)string vectors
      20                 :            : // to and from policy-level representations.
      21                 :            : //
      22                 :            : class VectorVal;
      23                 :            : 
      24                 :            : class BroString {
      25                 :            : public:
      26                 :            :         typedef vector<BroString*> Vec;
      27                 :            :         typedef Vec::iterator VecIt;
      28                 :            :         typedef Vec::const_iterator VecCIt;
      29                 :            : 
      30                 :            :         typedef vector<const BroString*> CVec;
      31                 :            :         typedef Vec::iterator CVecIt;
      32                 :            :         typedef Vec::const_iterator CVecCIt;
      33                 :            : 
      34                 :            :         // IdxVecs are vectors of indices of characters in a string.
      35                 :            :         typedef vector<int> IdxVec;
      36                 :            :         typedef IdxVec::iterator IdxVecIt;
      37                 :            :         typedef IdxVec::const_iterator IdxVecCIt;
      38                 :            : 
      39                 :            :         BroString(int arg_final_NUL, byte_vec str, int arg_n);
      40                 :            :         BroString(const u_char* str, int arg_n, int add_NUL);
      41                 :            :         BroString(const char* str);
      42                 :            :         BroString(const string& str);
      43                 :            :         BroString(const BroString& bs);
      44                 :            :         BroString();
      45                 :     201625 :         ~BroString()    { Reset(); }
      46                 :            : 
      47                 :            :         const BroString& operator=(const BroString& bs);
      48                 :            :         bool operator==(const BroString& bs) const;
      49                 :            :         bool operator<(const BroString& bs) const;
      50                 :            : 
      51                 :     263183 :         byte_vec Bytes() const  { return b; }
      52                 :     434903 :         int Len() const { return n; }
      53                 :            : 
      54                 :            :         // Releases the string's current contents, if any, and
      55                 :            :         // adopts the byte vector of given length.  The string will
      56                 :            :         // manage the memory occupied by the string afterwards.
      57                 :            :         //
      58                 :            :         void Adopt(byte_vec bytes, int len);
      59                 :            : 
      60                 :            :         // Various flavors of methods that release the string's
      61                 :            :         // current contents, if any, and then set the string's
      62                 :            :         // contents to a copy of the string given by the arguments.
      63                 :            :         //
      64                 :            :         void Set(const u_char* str, int len, int add_NUL=1);
      65                 :            :         void Set(const char* str);
      66                 :            :         void Set(const string& str);
      67                 :            :         void Set(const BroString &str);
      68                 :            : 
      69                 :      71226 :         void SetUseFreeToDelete(int use_it)
      70                 :      71226 :                 { use_free_to_delete = use_it; }
      71                 :            : 
      72                 :            :         const char* CheckString() const;
      73                 :            : 
      74                 :            :         enum render_style {
      75                 :            :                 ESC_NONE = 0,
      76                 :            : 
      77                 :            :                 ESC_NULL = (1 << 0),      // 0 -> "\0"
      78                 :            :                 ESC_DEL  = (1 << 1),      // DEL -> "^?"
      79                 :            :                 ESC_LOW  = (1 << 2),      // values <= 26 mapped into "^[A-Z]"
      80                 :            :                 ESC_ESC  = (1 << 3),      // '\' -> "\\"
      81                 :            :                 ESC_QUOT = (1 << 4),      // '"' -> "\"", ''' -> "\'"
      82                 :            :                 ESC_HEX  = (1 << 5),      // Not in [32, 126]? -> "%XX"
      83                 :            :                 ESC_DOT  = (1 << 6),      // Not in [32, 126]? -> "."
      84                 :            : 
      85                 :            :                 // For serialization: '<string len> <string>'
      86                 :            :                 ESC_SER  = (1 << 7),
      87                 :            :         };
      88                 :            : 
      89                 :            :         static const int EXPANDED_STRING =      // the original style
      90                 :            :                 ESC_NULL | ESC_DEL | ESC_LOW | ESC_HEX;
      91                 :            : 
      92                 :            :         static const int BRO_STRING_LITERAL =   // as in a Bro string literal
      93                 :            :                 ESC_ESC | ESC_QUOT | ESC_HEX;
      94                 :            : 
      95                 :            :         // Renders a string into a newly allocated character array that
      96                 :            :         // you have to delete[].  You can combine the render styles given
      97                 :            :         // above to achieve the representation you desire.  If you pass a
      98                 :            :         // pointer to an integer as the final argument, you'll receive the
      99                 :            :         // entire length of the resulting char* in it.
     100                 :            :         //
     101                 :            :         // Note that you need to delete[] the resulting string.
     102                 :            :         //
     103                 :            :         char* Render(int format = EXPANDED_STRING, int* len = 0) const;
     104                 :            : 
     105                 :            :         // Similar to the above, but useful for output streams.
     106                 :            :         // Also more useful for debugging purposes since no deallocation
     107                 :            :         // is required on your part here.
     108                 :            :         //
     109                 :            :         ostream& Render(ostream& os, int format = ESC_SER) const;
     110                 :            : 
     111                 :            :         // Reads a string from an input stream.  Unless you use a render
     112                 :            :         // style combination that uses ESC_SER, note that the streams
     113                 :            :         // will consider whitespace as a field delimiter.
     114                 :            :         //
     115                 :            :         istream& Read(istream& is, int format = ESC_SER);
     116                 :            : 
     117                 :            :         // XXX Fix redundancy: strings.bif implements both to_lower
     118                 :            :         // XXX and to_upper; the latter doesn't use BroString::ToUpper().
     119                 :            :         void ToUpper();
     120                 :            : 
     121                 :          0 :         unsigned int MemoryAllocation() const
     122                 :          0 :                 { return padded_sizeof(*this) + pad_size(n + final_NUL); }
     123                 :            : 
     124                 :            :         // Returns new string containing the substring of this string,
     125                 :            :         // starting at @start >= 0 for going up to @length elements,
     126                 :            :         // A negative @length means "until end of string".  Other invalid
     127                 :            :         // values result in a return value of 0.
     128                 :            :         //
     129                 :            :         BroString* GetSubstring(int start, int length) const;
     130                 :            : 
     131                 :            :         // Returns the start index of s in this string, counting from 0.
     132                 :            :         // If s is not found, -1 is returned.
     133                 :            :         //
     134                 :            :         int FindSubstring(const BroString* s) const;
     135                 :            : 
     136                 :            :         // Splits the string into substrings, taking all the indices in
     137                 :            :         // the given vector as cutting points.  The vector does not need
     138                 :            :         // to be sorted, and can have multiple entries.  Out-of-bounds
     139                 :            :         // indices are ignored.  All returned strings are newly allocated.
     140                 :            :         //
     141                 :            :         Vec* Split(const IdxVec& indices) const;
     142                 :            : 
     143                 :            :         // Helper functions for vectors:
     144                 :            :         static VectorVal* VecToPolicy(Vec* vec);
     145                 :            :         static Vec* VecFromPolicy(VectorVal* vec);
     146                 :            :         static char* VecToString(const Vec* vec);
     147                 :            : 
     148                 :            : protected:
     149                 :            :         void Reset();
     150                 :            : 
     151                 :            :         byte_vec b;
     152                 :            :         int n;
     153                 :            :         unsigned int final_NUL:1;       // whether we have added a final NUL
     154                 :            :         unsigned int use_free_to_delete:1;      // free() vs. operator delete
     155                 :            : };
     156                 :            : 
     157                 :            : // A comparison class that sorts pointers to BroString's according to
     158                 :            : // the length of the pointed-to strings. Sort order can be specified
     159                 :            : // through the constructor.
     160                 :            : //
     161                 :            : class BroStringLenCmp {
     162                 :            : public:
     163                 :            :         BroStringLenCmp(bool increasing = true) { _increasing = increasing; }
     164                 :            :         bool operator()(BroString*const& bst1, BroString*const& bst2);
     165                 :            : 
     166                 :            :  private:
     167                 :            :         unsigned int _increasing;
     168                 :            : };
     169                 :            : 
     170                 :            : // Default output stream operator, using rendering mode EXPANDED_STRING.
     171                 :            : ostream& operator<<(ostream& os, const BroString& bs);
     172                 :            : 
     173                 :            : extern int Bstr_eq(const BroString* s1, const BroString* s2);
     174                 :            : extern int Bstr_cmp(const BroString* s1, const BroString* s2);
     175                 :            : 
     176                 :            : // A data_chunk_t specifies a length-delimited constant string. It is
     177                 :            : // often used for substrings of other BroString's to avoid memory copy,
     178                 :            : // which would be necessary if BroString were used. Unlike BroString,
     179                 :            : // the string should not be deallocated on destruction.
     180                 :            : //
     181                 :            : // "BroConstString" might be a better name here.
     182                 :            : 
     183                 :      19008 : struct data_chunk_t {
     184                 :            :         int length;
     185                 :            :         const char* data;
     186                 :            : };
     187                 :            : 
     188                 :            : extern BroString* concatenate(std::vector<data_chunk_t>& v);
     189                 :            : extern BroString* concatenate(BroString::Vec& v);
     190                 :            : extern BroString* concatenate(BroString::CVec& v);
     191                 :            : extern void delete_strings(std::vector<const BroString*>& v);
     192                 :            : 
     193                 :            : #endif

Generated by: LCOV version 1.8