reSIProcate/rutil  9694
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Friends
resip::Data Class Reference

An alternative to std::string, encapsulates an arbitrary buffer of bytes. More...

#include <Data.hxx>

Collaboration diagram for resip::Data:
Collaboration graph
[legend]

List of all members.

Classes

class  PreallocateType

Public Types

enum  DoubleDigitPrecision {
  ZeroDigitPrecision = 0, OneDigitPrecision, TwoDigitPrecision, ThreeDigitPrecision,
  FourDigitPrecision, FiveDigitPrecision, SixDigitPrecision, SevenDigitPrecision,
  EightDigitPrecision, NineDigitPrecision, TenDigitPrecision, MaxDigitPrecision
}
enum  ShareEnum { Borrow = 0, Share = 1, Take = 2 }
 The various memory management behaviors. More...
enum  EncodingType { BINARY, BASE64, HEX }
typedef UInt32 size_type

Public Member Functions

 RESIP_HeapCount (Data)
 Data ()
 Data (size_type capacity, const PreallocateType &)
 Creates a data with a specified initial capacity.
 Data (const char *str)
 Creates a data with a copy of the contents of the null-terminated string.
 Data (const char *buffer, size_type length)
 Creates a data with the contents of the buffer.
 Data (const unsigned char *buffer, size_type length)
 Creates a data with the contents of the buffer.
 Data (const Data &data)
 Data (const std::string &str)
 Creates a data with the contents of the string.
 Data (int value)
 Converts the passed in value into ascii-decimal representation, and then creates a "Data" containing that value.
 Data (unsigned long value)
 Converts the passed in value into ascii-decimal representation, and then creates a "Data" containing that value.
 Data (unsigned int value)
 Converts the passed in value into ascii-decimal representation, and then creates a "Data" containing that value.
 Data (UInt64 value)
 Converts the passed in value into ascii-decimal representation, and then creates a "Data" containing that value.
 Data (double value, Data::DoubleDigitPrecision precision=FourDigitPrecision)
 Converts the passed in value into ascii-decimal representation, and then creates a "Data" containing that value.
 Data (bool value)
 Creates a buffer containing "true" or "false", depending on the value of "value".
 Data (char c)
 Creates a buffer containing a single character.
 Data (ShareEnum, const char *buffer, size_type length)
 Creates a Data from the passed-in buffer.
 Data (ShareEnum, const char *buffer)
 Takes a null-terminated string and creates a buffer.
 Data (ShareEnum, const Data &staticData)
 Lazily creates a Data from the passed-in Data.
 ~Data ()
DatasetBuf (ShareEnum se, const char *buf, size_type length)
 Set the Data to hold {buf} using share type {se}, which may be any of Share (read-only, no-free), Borrow (read-write, no-free) or Take (read-write, yes-free).
DatasetBuf (ShareEnum se, const char *str)
 Convience function to call setBuf() with a NULL-terminated string.
DatatakeBuf (Data &other)
 Take the data from {other}.
Datacopy (const char *buf, size_type length)
 Functional equivalent of: *this = Data(buf, length) but avoid the intermediate allocation and free.
char * getBuf (size_type length)
 Set size to be exactly {length}, extending buffer if needed.
Dataoperator= (const Data &data)
Dataoperator= (const char *str)
 Assigns a null-terminated string to the buffer.
Data operator+ (const Data &rhs) const
 Concatenates two Data objects.
Data operator+ (const char *str) const
 Concatenates a null-terminated string after the Data object.
Data operator+ (char c) const
 Concatenates a single byte after Data object.
Dataoperator+= (const Data &rhs)
 Appends a data object to this one.
Dataoperator+= (const char *str)
 Appends a null-terminated string to the end of the Data object.
Dataoperator+= (char c)
 Appends a single byte to the Data object.
Dataoperator^= (const Data &rhs)
 Performs an in-place exclusive-or of this buffer buffer with the specified buffer.
char & operator[] (size_type p)
 Returns the character at the specified position.
char operator[] (size_type p) const
 Returns the character at the specified position.
char & at (size_type p)
 Returns the character at the specified position.
void reserve (size_type capacity)
 Guarantees that the underlying buffer used by the Data is at least the number of bytes specified.
Dataappend (const char *str, size_type len)
 Appends the specified number of bytes to the end of this Data.
size_type truncate (size_type len)
 Shortens the size of this Data.
Datatruncate2 (size_type len)
 Shortens the size of this Data so length is at most of {len}.
bool empty () const
 Checks whether the Data is empty.
size_type size () const
 Returns the number of bytes in this Data.
const char * data () const
 Returns a pointer to the contents of this Data.
const char * c_str () const
 Returns a null-terminated string representing.
const char * begin () const
 Returns a pointer to the beginning of the buffer used by the Data.
const char * end () const
 Returns a pointer to the end of the buffer used by the Data.
Data md5 (EncodingType type=HEX) const
 Computes the MD5 hash of the current data.
Datalowercase ()
 Converts this Data to lowercase.
Datauppercase ()
 Converts this Data to uppercase.
DataschemeLowercase ()
 Converts this Data to lowercase, assuming this Data only consists of scheme characters.
Data hex () const
 Returns a hexadecimal representation of the contents of this Data.
Data escaped () const
 Returns a representation of the contents of the data with any non-printable characters escaped.
Data charEncoded () const
 Performs RFC 3261 escaping of SIP URIs.
Data charUnencoded () const
 Performs RFC 3261 un-escaping of SIP URIs.
Data urlEncoded () const
 Performs in-place HTTP URL escaping of a Data.
Data urlDecoded () const
 Performs in-place HTTP URL un-escaping of a Data.
EncodeStream & urlEncode (EncodeStream &s) const
 Escapes a Data to a stream according to HTTP URL encoding rules.
EncodeStream & urlDecode (EncodeStream &s) const
 Un-escapes a Data to a stream according to HTTP URL encoding rules.
Data xmlCharDataEncode () const
 Performs in-place XML Character Data escaping of a Data.
Data xmlCharDataDecode () const
 Performs in-place XML Character Data un-escaping of a Data.
EncodeStream & xmlCharDataEncode (EncodeStream &s) const
 Escapes a Data to a stream according to XML Character Data encoding rules.
EncodeStream & xmlCharDataDecode (EncodeStream &s) const
 Un-escapes a Data to a stream according to XML Character Data encoding rules.
Data trunc (size_type trunc) const
 Shortens the size of this Data.
Dataclear ()
 Clears the contents of this Data.
int convertInt () const
 Takes the contents of the Data and converts them to an integer.
unsigned long convertUnsignedLong () const
size_t convertSize () const
 Takes the contents of the Data and converts them to a size_t.
double convertDouble () const
 Takes the contents of the Data and converts them to a double precision floating point value.
UInt64 convertUInt64 () const
 Takes the contents of the Data and converts them to an unsigned 64-bit integer.
bool prefix (const Data &pre) const
 Returns true if this Data starts with the bytes indicated by the passed-in Data.
bool postfix (const Data &post) const
 Returns true if this Data ends with the bytes indicated by the passed-in Data.
Data substr (size_type first, size_type count=Data::npos) const
 Copies a portion of this Data into a new Data.
size_type find (const Data &match, size_type start=0) const
 Finds a specified sequence of bytes in this Data.
int replace (const Data &match, const Data &target, int max=INT_MAX)
 Replaces up to max occurrences of the bytes match with target.
Data base64decode () const
 Performs RFC 3548 Base 64 decoding of the contents of this data.
Data base64encode (bool useUrlSafe=false) const
 Performs RFC 3548 Base 64 encoding of the contents of this data.
size_t hash () const
 Creates a 32-bit hash based on the contents of this Data.
size_t caseInsensitivehash () const
 Creates a 32-bit hash based on the contents of this Data, after normalizing any alphabetic characters to lowercase.
size_t caseInsensitiveTokenHash () const
 A faster version of caseInsensitiveHash that has the same collision properties if this Data is made up of RFC 3261 token characters.
bool caseInsensitiveTokenCompare (const Data &rhs) const
bool sizeEqualCaseInsensitiveTokenCompare (const Data &rhs) const
template<class Predicate >
EncodeStream & escapeToStream (EncodeStream &str, Predicate shouldEscape) const
 Performs escaping of this Data according to the indicated Predicate.
std::ostream & escapeToStream (std::ostream &str, const std::bitset< 256 > &shouldEscape) const
 Performs escaping of this Data according to a bitset.

Static Public Member Functions

template<class T >
static Data from (const T &x)
 Converts from arbitrary other type to Data.
static bool init (DataLocalSize< RESIP_DATA_LOCAL_SIZE > arg)
 Initializes Data class.
static size_t rawHash (const unsigned char *c, size_t size)
 Creates a 32-bit hash based on the contents of the indicated buffer.
static size_t rawCaseInsensitiveHash (const unsigned char *c, size_t size)
 Creates a 32-bit hash based on the contents of the indicated buffer, after normalizing any alphabetic characters to lowercase.
static size_t rawCaseInsensitiveTokenHash (const unsigned char *c, size_t size)
 A faster version of rawCaseInsensitiveHash that has the same collision properties if this Data is made up of RFC 3261 token characters.
static std::bitset< 256 > toBitset (const resip::Data &chars)
 Creates a bitset reflecting the contents of this data (as a set) ie.

Static Public Attributes

static const PreallocateType Preallocate
 used only to disambiguate constructors
static const Data Empty
 Constant that represents a zero-length data.
static const size_type npos = UINT_MAX
 Represents an impossible position; returned to indicate failure to find.

Private Types

enum  { LocalAlloc = RESIP_DATA_LOCAL_SIZE }
 Trade off between in-object and heap allocation Larger LocalAlloc makes for larger objects that have Data members but bulk allocation/deallocation of Data members. More...

Private Member Functions

 Data (const char *buffer, size_type length, bool)
void own () const
 Copies the contents of this data to a new buffer if the Data does not own the current buffer.
void resize (size_type newSize, bool copy)

Static Private Member Functions

static bool isHex (unsigned char c)

Private Attributes

char * mBuf
size_type mSize
size_type mCapacity
char mPreBuffer [LocalAlloc]
size_type mShareEnum

Friends

class ParseBuffer
class DataBuffer
class DataStream
class oDataStream
class ::TestData
class MD5Buffer
bool operator== (const Data &lhs, const Data &rhs)
bool operator== (const Data &lhs, const char *rhs)
bool operator< (const Data &lhs, const Data &rhs)
bool operator< (const Data &lhs, const char *rhs)
bool operator< (const char *lhs, const Data &rhs)
std::ostream & operator<< (std::ostream &strm, const Data &d)
EncodeStream & operator<< (EncodeStream &strm, const Data &d)

Detailed Description

An alternative to std::string, encapsulates an arbitrary buffer of bytes.

It has a variety of memory management styles that can be established at contruction time and changed later via setBuf().

Three modes of allocation are currently available:

Additionally, Data has a small locally-allocated buffer (member buffer) that it will use to hold small amounts of data. By default, this buffer can contain 16 bytes, meaning that Data will not use the heap unless it needs more than 16 bytes of space. The tradeoff here, of course, is that instances of Data will be larger than instances of std::string. Generally speaking, if you expect to need more than 16 bytes of room, and you cannot make good use of the flexible memory management offered by Data, you may want to use a std::string instead.

See also:
RESIP_HeapCount
Todo:
It might be worthwhile examining the heap usage of this class in the context of using realloc everywhere appropriate. (realloc is defined in ANSI C, SVID, and the OpenGroup "Single Unix Specification").

Definition at line 86 of file Data.hxx.


Member Typedef Documentation

Definition at line 91 of file Data.hxx.


Member Enumeration Documentation

anonymous enum [private]

Trade off between in-object and heap allocation Larger LocalAlloc makes for larger objects that have Data members but bulk allocation/deallocation of Data members.

Enumerator:
LocalAlloc 

Definition at line 960 of file Data.hxx.

Enumerator:
ZeroDigitPrecision 
OneDigitPrecision 
TwoDigitPrecision 
ThreeDigitPrecision 
FourDigitPrecision 
FiveDigitPrecision 
SixDigitPrecision 
SevenDigitPrecision 
EightDigitPrecision 
NineDigitPrecision 
TenDigitPrecision 
MaxDigitPrecision 

Definition at line 217 of file Data.hxx.

Enumerator:
BINARY 
BASE64 
HEX 

Definition at line 595 of file Data.hxx.

The various memory management behaviors.

Enumerator:
Borrow 

The Data instance is borrowing the memory from the passed in buffer.

It will modify its contents as necessary, but will not deallocate it.

Share 

The Data instance will use the buffer in a read-only mode.

If any attempt is made to modify the contents of the Data, it will copy the buffer and modify it.

Take 

The Data instance takes complete ownership of the buffer.

The buffer must have been allocate using "new char[]" so that it can be freed with "delete char[]".

Definition at line 254 of file Data.hxx.

      {
        Borrow=0,

        Share=1,

        Take=2
      };

Constructor & Destructor Documentation

resip::Data::Data ( ) [inline]

Definition at line 93 of file Data.hxx.

References mBuf, and mSize.

Referenced by base64encode(), substr(), and trunc().

Data::Data ( size_type  capacity,
const PreallocateType  
)

Creates a data with a specified initial capacity.

Parameters:
capacityThe initial capacity of the buffer
fooThis parameter is ignored; it is merely used to disambuguate this constructor from the constructor that takes a single int. Always pass Data::Preallocate.

Definition at line 193 of file Data.cxx.

References mBuf, and mSize.

   : mBuf(capacity > LocalAlloc 
          ? new char[capacity + 1]
          : mPreBuffer),
     mSize(0),
     mCapacity(capacity > LocalAlloc
               ? capacity
               : LocalAlloc),
     mShareEnum(capacity > LocalAlloc ? Take : Borrow)
{
   assert( capacity >= 0 );
   mBuf[mSize] = 0;
}
Data::Data ( const char *  str)

Creates a data with a copy of the contents of the null-terminated string.

Warning:
Passing a non-null-terminated string to this method would be a Really Bad Thing.

Definition at line 304 of file Data.cxx.

References LocalAlloc, mBuf, mPreBuffer, and mSize.

   : mSize(str ? strlen(str) : 0),
     mCapacity(mSize > LocalAlloc
               ? mSize
               : LocalAlloc),
     mShareEnum(mSize > LocalAlloc ? Take : Borrow)
{
   if(mSize > LocalAlloc)
   {
      mBuf = new char[mSize+1];
   }
   else
   {
      mBuf = mPreBuffer;
   }

   if (str)
   {
      memcpy(mBuf, str, mSize+1);
   }
   else
   {
      mBuf[mSize] = 0;
   }
}
Data::Data ( const char *  buffer,
size_type  length 
)

Creates a data with the contents of the buffer.

Parameters:
lengthNumber of bytes in the buffer

Definition at line 225 of file Data.cxx.

References mBuf, and mSize.

   : mBuf(length > LocalAlloc 
          ? new char[length + 1]
          : mPreBuffer),
     mSize(length),
     mCapacity(mSize > LocalAlloc
               ? mSize
               : LocalAlloc),
     mShareEnum(mSize > LocalAlloc ? Take : Borrow)
{
   if (mSize > 0)
   {
      assert(str);
      memcpy(mBuf, str, mSize);
   }
   mBuf[mSize]=0;
}
Data::Data ( const unsigned char *  buffer,
size_type  length 
)

Creates a data with the contents of the buffer.

Parameters:
lengthNumber of bytes in the buffer

Definition at line 243 of file Data.cxx.

References mBuf, and mSize.

   : mBuf(length > LocalAlloc 
          ? new char[length + 1]
          : mPreBuffer),
     mSize(length),
     mCapacity(mSize > LocalAlloc
               ? mSize
               : LocalAlloc),
     mShareEnum(mSize > LocalAlloc ? Take : Borrow)
{
   if (mSize > 0)
   {
      assert(str);
      memcpy(mBuf, str, mSize);
   }
   mBuf[mSize]=0;
}
Data::Data ( const Data data)

Definition at line 343 of file Data.cxx.

References mBuf, and mSize.

   : mBuf(data.mSize > LocalAlloc
          ? new char[data.mSize + 1]
          : mPreBuffer),
     mSize(data.mSize),
     mCapacity(mSize > LocalAlloc
               ? mSize
               : LocalAlloc),
     mShareEnum(mSize > LocalAlloc ? Take : Borrow)
{
   if (mSize)
   {
      memcpy(mBuf, data.mBuf, mSize);
   }
   mBuf[mSize] = 0;
}
resip::Data::Data ( const std::string &  str) [explicit]

Creates a data with the contents of the string.

Data::Data ( int  value) [explicit]

Converts the passed in value into ascii-decimal representation, and then creates a "Data" containing that value.

(E.g. "Data(75)" will create a Data with length=2, and contents of 0x37 0x35).

Definition at line 371 of file Data.cxx.

References mBuf, and mSize.

   : mBuf(IntMaxSize > LocalAlloc 
          ? new char[IntMaxSize + 1]
          : mPreBuffer),
     mSize(0),
     mCapacity(IntMaxSize > LocalAlloc
               ? IntMaxSize
               : LocalAlloc),
     mShareEnum(IntMaxSize > LocalAlloc ? Take : Borrow)
{
   if (val == 0)
   {
      mBuf[0] = '0';
      mBuf[1] = 0;
      mSize = 1;
      return;
   }

   bool neg = false;
   
   int value = val;
   if (value < 0)
   {
      value = -value;
      neg = true;
   }

   int c = 0;
   int v = value;
   while (v /= 10)
   {
      ++c;
   }

   if (neg)
   {
      ++c;
   }

   mSize = c+1;
   mBuf[c+1] = 0;
   
   v = value;
   while (v)
   {
      mBuf[c--] = '0' + v%10;
      v /= 10;
   }

   if (neg)
   {
      mBuf[0] = '-';
   }
}
Data::Data ( unsigned long  value) [explicit]

Converts the passed in value into ascii-decimal representation, and then creates a "Data" containing that value.

(E.g. "Data(75)" will create a Data with length=2, and contents of 0x37 0x35).

Definition at line 427 of file Data.cxx.

References mBuf, and mSize.

   : mBuf(MaxLongSize > LocalAlloc 
          ? new char[MaxLongSize + 1]
          : mPreBuffer),
     mSize(0),
     mCapacity(MaxLongSize > LocalAlloc
               ? MaxLongSize
               : LocalAlloc),
     mShareEnum(MaxLongSize > LocalAlloc ? Take : Borrow)
{
   if (value == 0)
   {
      mBuf[0] = '0';
      mBuf[1] = 0;
      mSize = 1;
      return;
   }

   int c = 0;
   unsigned long v = value;
   while (v /= 10)
   {
      ++c;
   }

   mSize = c+1;
   mBuf[c+1] = 0;
   
   v = value;
   while (v)
   {
      unsigned int digit = v%10;
      unsigned char d = (char)digit;
      mBuf[c--] = '0' + d;
      v /= 10;
   }
}
Data::Data ( unsigned int  value) [explicit]

Converts the passed in value into ascii-decimal representation, and then creates a "Data" containing that value.

(E.g. "Data(75)" will create a Data with length=2, and contents of 0x37 0x35).

Definition at line 554 of file Data.cxx.

References mBuf, and mSize.

   : mBuf(IntMaxSize > LocalAlloc 
          ? new char[IntMaxSize + 1]
          : mPreBuffer),
     mSize(0),
     mCapacity(IntMaxSize > LocalAlloc
               ? IntMaxSize
               : LocalAlloc),
     mShareEnum(IntMaxSize > LocalAlloc ? Take : Borrow)
{
   if (value == 0)
   {
      mBuf[0] = '0';
      mBuf[1] = 0;
      mSize = 1;
      return;
   }

   int c = 0;
   unsigned long v = value;
   while (v /= 10)
   {
      ++c;
   }

   mSize = c+1;
   mBuf[c+1] = 0;
   
   v = value;
   while (v)
   {
      unsigned int digit = v%10;
      unsigned char d = (char)digit;
      mBuf[c--] = '0' + d;
      v /= 10;
   }
}
Data::Data ( UInt64  value) [explicit]

Converts the passed in value into ascii-decimal representation, and then creates a "Data" containing that value.

(E.g. "Data(75)" will create a Data with length=2, and contents of 0x37 0x35).

Definition at line 595 of file Data.cxx.

References mBuf, and mSize.

   : mBuf(UInt64MaxSize > LocalAlloc 
          ? new char[UInt64MaxSize + 1]
          : mPreBuffer),
     mSize(0),
     mCapacity(UInt64MaxSize > LocalAlloc
               ? UInt64MaxSize
               : LocalAlloc),
     mShareEnum(UInt64MaxSize > LocalAlloc ? Take : Borrow)
{
   if (value == 0)
   {
      mBuf[0] = '0';
      mBuf[1] = 0;
      mSize = 1;
      return;
   }

   int c = 0;
   UInt64 v = value;
   while (v /= 10)
   {
      ++c;
   }

   mSize = c+1;
   mBuf[c+1] = 0;
   
   v = value;
   while (v)
   {
      UInt64 digit = v%10;
      unsigned char d = (char)digit;
      mBuf[c--] = '0' + d;
      v /= 10;
   }
}
Data::Data ( double  value,
Data::DoubleDigitPrecision  precision = FourDigitPrecision 
) [explicit]

Converts the passed in value into ascii-decimal representation, and then creates a "Data" containing that value.

(E.g. "Data(75.4,2)" will create a Data with length=4, and contents of 0x37 0x35 0x2E 0x34).

Parameters:
precisionNumber of digits after the decimal point. Trailing zeros will be removed.

Definition at line 467 of file Data.cxx.

References MaxDigitPrecision, mBuf, mCapacity, mSize, Preallocate, resize(), and size().

   : mBuf(DoubleMaxSize + precision > LocalAlloc 
          ? new char[DoubleMaxSize + precision + 1]
          : mPreBuffer),
     mSize(0),
     mCapacity(DoubleMaxSize + precision > LocalAlloc
               ? DoubleMaxSize + precision
               : LocalAlloc),
     mShareEnum(DoubleMaxSize + precision > LocalAlloc ? Take : Borrow)
{
   assert(precision >= 0);
   assert(precision < MaxDigitPrecision);

   double v = value;
   bool neg = (value < 0.0);
   
   if (neg)
   {
      v = -v;
   }

   Data m((unsigned long)v);

   // remainder
   v = v - floor(v);

   int p = precision;
   while (p--)
   {
      v *= 10;
   }

   int dec = (int)floor(v+0.5);
   Data d(precision, Data::Preallocate);

   if (dec == 0)
   {
      d = "0";
   }
   else
   {
      d.mBuf[precision] = 0;
      p = precision;
      // neglect trailing zeros
      bool significant = false;
      while (p--)
      {
         if (dec % 10 || significant)
         {
            significant = true;
            ++d.mSize;
            d.mBuf[p] = '0' + (dec % 10);
         }
         else
         {
            d.mBuf[p] = 0;
         }
         
         dec /= 10;
      }
   }

   if (neg)
   {
      mBuf[0] = '-';
      memcpy(mBuf+1, m.mBuf, m.size());
      mBuf[1+m.size()] = '.';
      memcpy(mBuf+1+m.size()+1, d.mBuf, d.size()+1);
      mSize = m.size() + d.size() + 2;
   }
   else
   {
      if (mCapacity < m.size() + d.size() + 1)
      {
         resize(m.size() + d.size() + 1, false);
      }
      memcpy(mBuf, m.mBuf, m.size());
      mBuf[m.size()] = '.';
      memcpy(mBuf+m.size()+1, d.mBuf, d.size()+1);
      mSize = m.size() + d.size() + 1;
   }

   assert(mBuf[mSize] == 0);
}

Here is the call graph for this function:

Data::Data ( bool  value) [explicit]

Creates a buffer containing "true" or "false", depending on the value of "value".

Definition at line 648 of file Data.cxx.

   : mBuf(value ? const_cast<char*>("true") : const_cast<char*>("false")),
     mSize(value ? 4 : 5), 
     mCapacity(value ? 4 : 5),
     mShareEnum(Borrow)
{}
Data::Data ( char  c) [explicit]

Creates a buffer containing a single character.

Is this silly? Maybe. Perhaps it can be removed.

Definition at line 634 of file Data.cxx.

References mBuf.

Data::Data ( ShareEnum  se,
const char *  buffer,
size_type  length 
)

Creates a Data from the passed-in buffer.

See also:
ShareEnum

Definition at line 273 of file Data.cxx.

   : mBuf(const_cast<char*>(buffer)),
     mSize(length),
     mCapacity(mSize),
     mShareEnum(se)
{
   assert(buffer);
}
Data::Data ( ShareEnum  se,
const char *  buffer 
)

Takes a null-terminated string and creates a buffer.

See also:
ShareEnum
Warning:
Passing a non-null-terminated string to this method would be a Really Bad Thing.

Definition at line 282 of file Data.cxx.

   : mBuf(const_cast<char*>(buffer)),
     mSize(strlen(buffer)),
     mCapacity(mSize),
     mShareEnum(se)
{
   assert(buffer);
}
Data::Data ( ShareEnum  se,
const Data staticData 
)

Lazily creates a Data from the passed-in Data.

See also:
ShareEnum
Warning:
Calling this with "Take" or "Borrow" is invalid and will cause an assertion or crash.
Todo:
This implementation has some confusing and conflicting comments. (e.g. is Borrow actually okay? Can there be some way to use it with Take as long as you play with mShareEnum correctly?)

Definition at line 291 of file Data.cxx.

References Share.

   : mBuf(staticData.mBuf),
     mSize(staticData.mSize),
     mCapacity(mSize),
     mShareEnum(Share)
{
   // !dlb! maybe:
   // if you are trying to use Take, but make sure that you unset the mShareEnum on
   // the staticData
   assert(se == Share); // makes no sense to call this with 'Take'.
}
resip::Data::~Data ( ) [inline]

Definition at line 308 of file Data.hxx.

References mBuf, mShareEnum, and Take.

      {
         if (mShareEnum == Take)
         {
            delete[] mBuf;
         }
      }
Data::Data ( const char *  buffer,
size_type  length,
bool   
) [private]
Deprecated:
use Data(ShareEnum ...)

Definition at line 264 of file Data.cxx.

   : mBuf(const_cast<char*>(str)),
     mSize(length),
     mCapacity(mSize),
     mShareEnum(Share)
{
   assert(str);
}

Member Function Documentation

Data & Data::append ( const char *  str,
size_type  len 
)

Appends the specified number of bytes to the end of this Data.

Definition at line 1021 of file Data.cxx.

References mBuf, mCapacity, mShareEnum, mSize, resize(), and Share.

Referenced by resip::DnsUtil::inet_ntop(), TestData::main(), and operator+=().

{
   assert(str);
   if (mCapacity <= mSize + len)  // append null terminates, thus the equality
   {
      // .dlb. pad for future growth?
      resize(((mSize + len +16)*3)/2, true);
   }
   else
   {
      if (mShareEnum == Share)
      {
         resize(mSize + len, true);
      }
   }

   // could conceivably overlap
   memmove(mBuf + mSize, str, len);
   mSize += len;
   mBuf[mSize] = 0;

   return *this;
}

Here is the call graph for this function:

char & Data::at ( size_type  p)

Returns the character at the specified position.

Definition at line 951 of file Data.cxx.

References mBuf, mCapacity, mSize, own(), and resize().

Referenced by resip::ConfigParse::parseCommandLine(), and resip::Log::timestamp().

{
   if (p >= mCapacity)
   {
      resize(p+1, true);
   }
   else
   {
      own();
      if (p > mSize)
      {
         mSize = p + 1;
         mBuf[mSize] = 0;
      }
   }
   return mBuf[p];
}

Here is the call graph for this function:

Data Data::base64decode ( ) const

Performs RFC 3548 Base 64 decoding of the contents of this data.

Returns:
A new buffer containing the unencoded representation

Definition at line 2294 of file Data.cxx.

References base64Lookup, resip::Base64Coder::decode(), mBuf, reserve(), and size().

Referenced by resip::Base64Coder::decode(), and TestData::main().

{
   // see RFC 3548 
   // this will decode normal and URL safe alphabet 
#if 0
   return Base64Coder::decode( *this );
#else
   int wc=0;
   int val=0;
   Data bin;
   bin.reserve( size()*3/4 );
   
   for( unsigned int i=0; i<size(); i++ )
   {
      unsigned int x = mBuf[i] & 0x7F;
      char c1,c2,c3;
      
      int v =  base64Lookup[x];

      if ( v >= 0 )
      {
         val = val << 6;
         val |= v;
         wc++;
         
         if ( wc == 4 )
         {
            c3 = char( val & 0xFF ); val = val >> 8;
            c2 = char( val & 0xFF ); val = val >> 8;
            c1 = char( val & 0xFF ); val = val >> 8;

            bin += c1;
            bin += c2;
            bin += c3;
            
            wc=0;
            val=0;
         }
      }
      if ( base64Lookup[x] == -2 )
      {
         if (wc==2) val = val<<12;
         if (wc==3) val = val<<6;
         
         c3 = char( val & 0xFF ); val = val >> 8;
         c2 = char( val & 0xFF ); val = val >> 8;
         c1 = char( val & 0xFF ); val = val >> 8;
         
         unsigned int xNext = mBuf[i] & 0x7F;
         if ( (i+1<size() ) && ( base64Lookup[xNext] == -2 ))
         {
            bin += c1;
            i++;
         }
         else
         {
            bin += c1;
            bin += c2;
         }

         break;
      }
   }

   return bin;
#endif
}

Here is the call graph for this function:

Data Data::base64encode ( bool  useUrlSafe = false) const

Performs RFC 3548 Base 64 encoding of the contents of this data.

Returns:
A new buffer containing the base64 representation

Definition at line 2369 of file Data.cxx.

References codeCharSafe, codeCharUnsafe, Data(), data(), size(), and Take.

Referenced by resip::Base64Coder::encode(), TestData::main(), and md5().

{
   unsigned char* codeChar = useSafeSet ? codeCharSafe : codeCharUnsafe;
   
   int srcLength = (int)this->size();
   unsigned int dstLimitLength = srcLength*4/3 + 1 + 2; // +2 for the == chars
   unsigned char * dstData = new unsigned char[dstLimitLength];
   unsigned int dstIndex = 0;
   
   const char * p = static_cast<const char *>( this->data() );
   
   for(int index=0;index<srcLength;index+=3)
   {
      unsigned char codeBits = (p[index] & 0xfc)>>2;
      
      assert(codeBits < 64);
      dstData[dstIndex++] = codeChar[codeBits]; // c0 output
      assert(dstIndex <= dstLimitLength);
      
      // do second codeBits
      codeBits = ((p[index]&0x3)<<4);
      if (index+1 < srcLength)
      {
         codeBits |= ((p[index+1]&0xf0)>>4);
      }
      assert(codeBits < 64);
      dstData[dstIndex++] = codeChar[codeBits]; // c1 output
      assert(dstIndex <= dstLimitLength);
      
      if (index+1 >= srcLength) 
      {
         dstData[dstIndex++] = codeChar[64];
         assert(dstIndex <= dstLimitLength);
         dstData[dstIndex++] = codeChar[64];
         assert(dstIndex <= dstLimitLength);
         break; // encoded d0 only
      }
      
      // do third codeBits
      codeBits = ((p[index+1]&0xf)<<2);
      if (index+2 < srcLength)
      {
         codeBits |= ((p[index+2]&0xc0)>>6);
      }
      assert(codeBits < 64);
      dstData[dstIndex++] = codeChar[codeBits]; // c2 output
      assert(dstIndex <= dstLimitLength);
      
      if (index+2 >= srcLength) 
      {
         dstData[dstIndex++] = codeChar[64];   
         assert(dstIndex <= dstLimitLength);
         break; // encoded d0 d1 only
      }
      
      // do fourth codeBits
      codeBits = ((p[index+2]&0x3f));
      assert(codeBits < 64);
      dstData[dstIndex++] = codeChar[codeBits]; // c3 output
      assert(dstIndex <= dstLimitLength);
      // outputed all d0,d1, and d2
   }

   return Data(Data::Take, reinterpret_cast<char*>(dstData),
               dstIndex);
}

Here is the call graph for this function:

const char* resip::Data::begin ( ) const [inline]

Returns a pointer to the beginning of the buffer used by the Data.

Definition at line 582 of file Data.hxx.

References mBuf.

Referenced by resip::DnsUtil::getLocalIpAddress().

      {
         return mBuf;
      }
const char * Data::c_str ( ) const

Returns a null-terminated string representing.

Note:
Depending on the memory management scheme being used, this method often copies the contents of the Data; consequently, this method is rather expensive and should be avoided when possible.
Warning:
Calling this method is a pretty bad idea if the contents of Data are binary (i.e. may contain a null in the middle of the Data).

Definition at line 1060 of file Data.cxx.

References mBuf, mCapacity, mShareEnum, mSize, resize(), and Share.

Referenced by resip::ServerProcess::daemonize(), resip::getTransportNameFromType(), resip::getTransportNameFromTypeLower(), resip::DnsStub::Query::go(), resip::DnsUtil::inet_pton(), resip::Log::ThreadData::Instance(), resip::FileSystem::Directory::iterator::iterator(), resip::DnsUtil::lookupARecords(), resip::DnsStub::lookupRecords(), main(), TestData::main(), resip::Log::OutputToWin32DebugWindow(), and resip::ConfigParse::parseConfigFile().

{
   if (mShareEnum == Data::Share || mSize == mCapacity)
   {
      const_cast<Data*>(this)->resize(mSize+1,true);
   }
   // mostly is zero terminated, but not by DataStream
   mBuf[mSize] = 0;
   return mBuf;
}

Here is the call graph for this function:

size_t Data::caseInsensitivehash ( ) const

Creates a 32-bit hash based on the contents of this Data, after normalizing any alphabetic characters to lowercase.

Definition at line 2110 of file Data.cxx.

References data(), rawCaseInsensitiveHash(), and size().

Referenced by TestData::main().

{
   return rawCaseInsensitiveHash((const unsigned char*)(this->data()), this->size());
}

Here is the call graph for this function:

bool resip::Data::caseInsensitiveTokenCompare ( const Data rhs) const [inline]

Definition at line 891 of file Data.hxx.

References mSize, and sizeEqualCaseInsensitiveTokenCompare().

Referenced by resip::isTokenEqualNoCase(), and TestData::main().

      {
         if(mSize==rhs.mSize)
         {
            return sizeEqualCaseInsensitiveTokenCompare(rhs);
         }
         return false;
      }

Here is the call graph for this function:

size_t Data::caseInsensitiveTokenHash ( ) const

A faster version of caseInsensitiveHash that has the same collision properties if this Data is made up of RFC 3261 token characters.

Note:
This is not guaranteed to return the same value as rawCaseInsensitiveHash.

Definition at line 2116 of file Data.cxx.

References data(), rawCaseInsensitiveTokenHash(), and size().

Referenced by TestData::main().

{
   return rawCaseInsensitiveTokenHash((const unsigned char*)(this->data()), this->size());
}

Here is the call graph for this function:

Data Data::charEncoded ( ) const

Performs RFC 3261 escaping of SIP URIs.

Note:
This method is relatively inefficient
Deprecated:
Use escapeToStream instead
Todo:
This method should be removed
See also:
escapeToStream

Definition at line 1185 of file Data.cxx.

References data(), hexmap, Preallocate, and size().

Referenced by TestData::main().

{ 
   Data ret((int)((size()*11)/10), Data::Preallocate);

   const char* p = data();
   for (size_type i=0; i < size(); ++i)
   {
      unsigned char c = *p++;

      if ( c == 0x0d )
      {
         if ( i+1 < size() )
         {
            if ( *p == 0x0a )
            {
               // found a CRLF sequence
               ret += c;
               c = *p++; i++;
               ret += c;
               continue;
            }
         }
      }
      
      if ( !isprint(c) ||
           // rfc 3261 reserved + mark + space + tab
           strchr(" \";/?:@&=+%$,\t-_.!~*'()", c))
      {
         ret +='%';
         
         int hi = (c & 0xF0)>>4;
         int low = (c & 0x0F);
           
         ret += hexmap[hi];
         ret += hexmap[low];
      }
      else
      {
         ret += c;
      }
   }

   return ret;
}

Here is the call graph for this function:

Data Data::charUnencoded ( ) const

Performs RFC 3261 un-escaping of SIP URIs.

Note:
This method is relatively inefficient
Bug:
This method can assert if a "%00" comes in off the wire. That's really bad form.
Deprecated:
Use something more in the spirit of escapeToStream instead
Todo:
This method should be removed
See also:
escapeToStream

Definition at line 1231 of file Data.cxx.

References data(), hexmap, Preallocate, and size().

Referenced by TestData::main().

{
   Data ret(size(), Data::Preallocate);

   const char* p = data();
   for (size_type i = 0; i < size(); ++i)
   {
      unsigned char c = *p++;
      if (c == '%')
      {
         if ( i+2 < size())
         {
            const char* high = strchr(hexmap, tolower(*p++));
            const char* low = strchr(hexmap, tolower(*p++));

            // !rwm! changed from high==0 || low==0
            if (high == 0 && low == 0)
            {
               assert(0);
               // ugh
               return ret;
            }
            
            int highInt = int(high - hexmap);
            int lowInt = int(low - hexmap);
            ret += char(highInt<<4 | lowInt);
            i += 2;
         }
         else
         {
            break;
         }
      }
      else
      {
         ret += c;
      }
   }
   return ret;
}

Here is the call graph for this function:

Data& resip::Data::clear ( ) [inline]

Clears the contents of this Data.

This call does not modify the capacity of the Data. It does not write terminating NULL, and thus is safe to use with external buffers.

Definition at line 735 of file Data.hxx.

References truncate2().

Referenced by resip::XMLCursor::getAttributes(), resip::XMLCursor::getValue(), resip::DnsUtil::inet_ntop(), main(), TestData::main(), and resip::Log::timestamp().

{ return truncate2(0); };

Here is the call graph for this function:

double Data::convertDouble ( ) const

Takes the contents of the Data and converts them to a double precision floating point value.

Will strip leading whitespace. This method stops upon encountering the first non-decimal digit (with exceptions made for decimal points and leading negative signs).

Definition at line 1733 of file Data.cxx.

References end(), mBuf, and mSize.

Referenced by TestData::main().

{
   long val = 0;
   char* p = mBuf;
   const char* const end = p + mSize;
   int s = 1;

   for (; p != end; ++p)
   {
      if (!isspace(*p))
      {
         goto sign_char;
      }
   }
   return val;
sign_char:

   if (*p == '-')
   {
      s = -1;
      ++p;
   }
   else if (*p == '+')
   {
      ++p;
   }

   for(; p != end; ++p)
   {
      if (*p == '.')
      {
         goto decimals;
      }
      if (!isdigit(*p))
      {
         return s*val;
      }
      val *= 10;
      val += (*p) - '0';
   }
   return s*val;

decimals:
   ++p;
   long d = 0;
   double div = 1.0;
   for(; p != end; ++p)
   {
      if (!isdigit(*p)) 
      {
         break;
      }
      d *= 10;
      d += *p - '0';
      div *= 10;
   }
   return s*(val + d/div);
}

Here is the call graph for this function:

int Data::convertInt ( ) const

Takes the contents of the Data and converts them to an integer.

Will strip leading whitespace. This method stops upon encountering the first non-decimal digit (with exceptions made for leading negative signs).

Definition at line 1590 of file Data.cxx.

References end(), mBuf, and mSize.

Referenced by TestData::main().

{
   int val = 0;
   char* p = mBuf;
   const char* const end = mBuf + mSize;
   int s = 1;

   for (; p != end; ++p)
   {
      if (!isspace(*p))
      {
         goto sign_char;
      }
   }
   return val;
sign_char:

   if (*p == '-')
   {
      s = -1;
      ++p;
   }
   else if (*p == '+')
   {
      ++p;
   }

   for(; p != end; ++p)
   {
      if (!isdigit(*p))
      {
         break;
      }
      val *= 10;
      val += (*p) - '0';
   }
   return s*val;
}

Here is the call graph for this function:

size_t Data::convertSize ( ) const

Takes the contents of the Data and converts them to a size_t.

Will strip leading whitespace. This method stops upon encountering the first non-decimal digit.

Definition at line 1698 of file Data.cxx.

References end(), mBuf, and mSize.

Referenced by TestData::main().

{
   size_t val = 0;
   char* p = mBuf;
   const char* const end = mBuf + mSize;

   for (; p != end; ++p)
   {
      if (!isspace(*p))
      {
         goto sign_char;
      }
   }
   return val;
sign_char:

   if (*p == '+')
   {
      ++p;
   }

   for(; p != end; ++p)
   {
      if (!isdigit(*p))
      {
         break;
      }
      val *= 10;
      val += (*p) - '0';
   }
   return val;
}

Here is the call graph for this function:

UInt64 Data::convertUInt64 ( ) const

Takes the contents of the Data and converts them to an unsigned 64-bit integer.

Will strip leading whitespace. This method stops upon encountering the first non-decimal digit.

Definition at line 1664 of file Data.cxx.

References end(), mBuf, and mSize.

Referenced by TestData::main().

{
   UInt64 val = 0;
   char* p = mBuf;
   const char* const end = mBuf + mSize;

   for (; p != end; ++p)
   {
      if (!isspace(*p))
      {
         goto sign_char;
      }
   }
   return val;
sign_char:

   if (*p == '+')
   {
      ++p;
   }

   for(; p != end; ++p)
   {
      if (!isdigit(*p))
      {
         break;
      }
      val *= 10;
      val += (*p) - '0';
   }
   return val;
}

Here is the call graph for this function:

unsigned long Data::convertUnsignedLong ( ) const

Definition at line 1630 of file Data.cxx.

References end(), mBuf, and mSize.

Referenced by TestData::main().

{
   unsigned long val = 0;
   char* p = mBuf;
   const char* const end = mBuf + mSize;

   for (; p != end; ++p)
   {
      if (!isspace(*p))
      {
         goto sign_char;
      }
   }
   return val;
sign_char:

   if (*p == '+')
   {
      ++p;
   }

   for(; p != end; ++p)
   {
      if (!isdigit(*p))
      {
         break;
      }
      val *= 10;
      val += (*p) - '0';
   }
   return val;
}

Here is the call graph for this function:

Data & Data::copy ( const char *  buf,
size_type  length 
)

Functional equivalent of: *this = Data(buf, length) but avoid the intermediate allocation and free.

Also, will never decrease capacity. Safe to call even if {buf} is part of {this}.

Note:
The result is always NULL terminated. Unfortunately, this requires a buffer allocation even if capacity exactly equals length.

Definition at line 704 of file Data.cxx.

References mBuf, mCapacity, mShareEnum, mSize, resize(), and Share.

Referenced by operator=().

{
   if (mShareEnum == Data::Share || mCapacity < length+1)
   {
      // will alloc length+1, so the term NULL below is safe
      resize(length, false);
   }
   // {buf} might be part of ourselves already, in which case {length}
   // is smaller than our capacity, so resize above won't happen, so
   // just memmove (not memcpy) and everything good
   mSize = length;
   if (mSize>0)
   {
      memmove(mBuf, buf, mSize);
   }
   // DONT do term NULL until after copy, because may be shifting contents
   // down and don't want to put NULL in middle of it
   mBuf[mSize] = 0;
   return *this;
}

Here is the call graph for this function:

const char* resip::Data::data ( ) const [inline]
bool resip::Data::empty ( ) const [inline]
const char* resip::Data::end ( ) const [inline]

Returns a pointer to the end of the buffer used by the Data.

Definition at line 590 of file Data.hxx.

References mBuf, and mSize.

Referenced by convertDouble(), convertInt(), convertSize(), convertUInt64(), convertUnsignedLong(), operator^=(), rawCaseInsensitiveHash(), and rawHash().

      {
         return mBuf + mSize;
      }
Data Data::escaped ( ) const

Returns a representation of the contents of the data with any non-printable characters escaped.

Warning:
This is extremely slow, and should not be called except for debugging purposes.

Definition at line 1141 of file Data.cxx.

References data(), hexmap, Preallocate, and size().

{ 
   Data ret((int)((size()*11)/10), Data::Preallocate);

   const char* p = data();
   for (size_type i=0; i < size(); ++i)
   {
      unsigned char c = *p++;

      if ( c == 0x0d )
      {
         if ( i+1 < size() )
         {
            if ( *p == 0x0a )
            {
               // found a CRLF sequence
               ret += c;
               c = *p++; i++;
               ret += c;
               continue;
            }
         }
      }
      
      if ( !isprint(c) )
      {
         ret +='%';
         
         int hi = (c & 0xF0)>>4;
         int low = (c & 0x0F);
           
         ret += hexmap[hi];
         ret += hexmap[low];
      }
      else
      {
         ret += c;
      }
   }

   return ret;
}

Here is the call graph for this function:

template<class Predicate >
EncodeStream & resip::Data::escapeToStream ( EncodeStream &  str,
Predicate  shouldEscape 
) const

Performs escaping of this Data according to the indicated Predicate.

Parameters:
strA stream to which the escaped representation should be added.
shouldEscapeA functor which takes a single character as a parameter, and returns true if the character should be escaped, false if it should not.
Deprecated:
dlb -- pass a 256 array of bits rather than a function.

Definition at line 1031 of file Data.hxx.

References empty(), hex(), isHex(), mBuf, and mSize.

{
   static char hex[] = "0123456789ABCDEF";

   if (empty())
   {
      return str;
   }
   
   const unsigned char* p = (unsigned char*)mBuf;
   const unsigned char* e = (unsigned char*)mBuf + mSize;

   while (p < e)
   {
      // ?abr? Why is this special cased? Removing this code
      // does not change the behavior of this method.
      if (*p == '%' 
          && e - p > 2 
          && isHex(*(p+1)) 
          && isHex(*(p+2)))
      {
         str.write((char*)p, 3);
         p+=3;
      }
      else if (shouldEscape[*p])
      {
         int hi = (*p & 0xF0)>>4;
         int low = (*p & 0x0F);

         str << '%' << hex[hi] << hex[low];
         p++;
      }
      else
      {
         str.put(*p++);
      }
   }
   return str;
}

Here is the call graph for this function:

std::ostream & Data::escapeToStream ( std::ostream &  str,
const std::bitset< 256 > &  shouldEscape 
) const

Performs escaping of this Data according to a bitset.

Parameters:
strA stream to which the escaped representation should be added.
shouldEscapeA bitset representing which chars should be escaped.

Definition at line 2226 of file Data.cxx.

References empty(), hex(), isHex(), mBuf, and mSize.

{
   static char hex[] = "0123456789ABCDEF";

   if (empty())
   {
      return str;
   }
   
   const unsigned char* anchor = (unsigned char*)mBuf;
   const unsigned char* p = (unsigned char*)mBuf;
   const unsigned char* e = (unsigned char*)mBuf + mSize;

   while (p < e)
   {
      // ?abr? Why is this special cased? Removing this code
      // does not change the behavior of this method.
      if (*p == '%' 
          && e - p > 2 
          && isHex(*(p+1)) 
          && isHex(*(p+2)))
      {
         p+=3;
      }
      else if (shouldEscape[*p])
      {
         if(p > anchor)
         {
            str.write((char*)anchor, p-anchor);
         }
         int hi = (*p & 0xF0)>>4;
         int low = (*p & 0x0F);
           
         str << '%' << hex[hi] << hex[low];
         anchor=++p;
      }
      else
      {
         ++p;
      }
   }
   if(p > anchor)
   {
      str.write((char*)anchor, p-anchor);
   }
   return str;
}

Here is the call graph for this function:

Data::size_type Data::find ( const Data match,
size_type  start = 0 
) const

Finds a specified sequence of bytes in this Data.

Parameters:
matchThe bytes to be found
startOffset into this Data to start the search
Returns:
An index to the start of the found bytes.

Definition at line 1831 of file Data.cxx.

References resip::ParseBuffer::eof(), mBuf, mSize, npos, resip::ParseBuffer::position(), resip::ParseBuffer::skipToChars(), and resip::ParseBuffer::start().

Referenced by resip::ConfigParse::getConfigValue(), resip::DnsUtil::getLocalDomainName(), TestData::main(), and replace().

{
   if (start < mSize)
   {
      ParseBuffer pb(mBuf + start, mSize - start);
      pb.skipToChars(match);
      if (!pb.eof())
      {
         return pb.position() - pb.start() + start;
      }
   }

   return Data::npos;
}

Here is the call graph for this function:

template<class T >
static Data resip::Data::from ( const T &  x) [inline, static]

Converts from arbitrary other type to Data.

Requires the other type to have an operator<<.

Definition at line 382 of file Data.hxx.

      {
         Data d;
         {
            DataStream s(d);
            s << x;
         }
         return d;
      }
char * Data::getBuf ( size_type  length)

Set size to be exactly {length}, extending buffer if needed.

Also, reallocate buffer if needed so that it is writable. Buffer contents is NOT initialized, and existing contents may or may not be preserved.

Note:
Purpose of this function is to provide a working buffer of fixed size that the application fills in after this call.
If you want just the buffer without changing the size, use data() and cast-away the const-ness.
The result may or may not be NULL terminated. The buffer is NULL terminated only when safe to do so without extra reallocation.

Definition at line 726 of file Data.cxx.

References mBuf, mCapacity, mShareEnum, mSize, resize(), and Share.

{
   if (mShareEnum == Data::Share || mCapacity < length)
   {
      // will alloc length+1, so the term NULL below is safe
      resize(length, false);
      mBuf[length] = 0;
   }
   else if ( mCapacity != length )
   {
      mBuf[length] = 0;
   }
   // even if we don't NULL-term it, it may have NULL term from before.
   // But if external buffer (taken or borrow'd) then don't know if it
   // has a NULL or not.
   mSize = length;
   return mBuf;
}

Here is the call graph for this function:

size_t Data::hash ( ) const

Creates a 32-bit hash based on the contents of this Data.

Definition at line 2104 of file Data.cxx.

References data(), rawHash(), and size().

Referenced by resip::Random::getSimpleSeed(), TestData::main(), and rawCaseInsensitiveTokenHash().

{
   return rawHash((const unsigned char*)(this->data()), this->size());
}

Here is the call graph for this function:

Data Data::hex ( ) const

Returns a hexadecimal representation of the contents of this Data.

Definition at line 1519 of file Data.cxx.

References hexmap, mBuf, mSize, and Preallocate.

Referenced by escapeToStream(), resip::MD5Buffer::getHex(), resip::DigestBuffer::getHex(), resip::Random::getVersion4UuidUrn(), TestData::main(), main(), and md5().

{
   Data ret( 2*mSize, Data::Preallocate);

   const char* p = mBuf;
   char* r = ret.mBuf;
   for (size_type i=0; i < mSize; ++i)
   {
      unsigned char temp = *p++;

      int hi = (temp & 0xf0)>>4;
      int low = (temp & 0xf);

      *r++ = hexmap[hi];
      *r++ = hexmap[low];
   }
   *r = 0;
   ret.mSize = 2*mSize;
   return ret;
}
bool Data::init ( DataLocalSize< RESIP_DATA_LOCAL_SIZE >  arg) [static]

Initializes Data class.

Note:
This method is a link time constraint. Don't remove it.

Definition at line 188 of file Data.cxx.

{
   return true;
}
bool resip::Data::isHex ( unsigned char  c) [inline, static, private]

Definition at line 995 of file Data.hxx.

References resip::DataHelper::isCharHex.

Referenced by escapeToStream().

{
   return DataHelper::isCharHex[c];
}
Data & Data::lowercase ( )

Converts this Data to lowercase.

Note:
This is silly unless the contents are ASCII.

Definition at line 1541 of file Data.cxx.

References mBuf, mSize, and own().

Referenced by resip::ConfigParse::getConfigValue(), resip::ConfigParse::insertConfigValue(), and TestData::main().

{
   own();
   char* p = mBuf;
   for (size_type i=0; i < mSize; ++i)
   {
      *p = tolower(*p);
      ++p;
   }
   return *this;
}

Here is the call graph for this function:

Data Data::md5 ( EncodingType  type = HEX) const

Computes the MD5 hash of the current data.

Parameters:
typeThe encoding of the return (default is HEX)
Returns:
The MD5 hash, in the encoding specified by type.

Definition at line 1116 of file Data.cxx.

References BASE64, base64encode(), BINARY, HEX, hex(), mBuf, resip::MD5Final(), resip::MD5Init(), resip::MD5Update(), and mSize.

Referenced by main(), and TestData::main().

{
   MD5Context context;
   MD5Init(&context);
   MD5Update(&context, reinterpret_cast < unsigned const char* > (mBuf), (unsigned int)mSize);

   unsigned char digestBuf[16];
   MD5Final(digestBuf, &context);
   Data digest(digestBuf,16);

   switch(type)
   {
      case BINARY:
         return digest;
      case BASE64:
         return digest.base64encode(true);
      case HEX:
      default:
         return digest.hex();
   }
   assert(0);
   return digest.hex();
}

Here is the call graph for this function:

Data Data::operator+ ( const Data rhs) const

Concatenates two Data objects.

Definition at line 914 of file Data.cxx.

References mBuf, mCapacity, mSize, and Preallocate.

{
   Data tmp(mSize + (int)data.mSize, Data::Preallocate);
   tmp.mSize = mSize + data.mSize;
   tmp.mCapacity = tmp.mSize;
   memcpy(tmp.mBuf, mBuf, mSize);
   memcpy(tmp.mBuf + mSize, data.mBuf, data.mSize);
   tmp.mBuf[tmp.mSize] = 0;

   return tmp;
}
Data Data::operator+ ( const char *  str) const

Concatenates a null-terminated string after the Data object.

Warning:
Passing a non-null-terminated string to this method would be a Really Bad Thing.

Definition at line 998 of file Data.cxx.

References mBuf, mCapacity, mSize, and Preallocate.

{
   assert(str);
   size_t l = strlen(str);
   Data tmp(mSize + l, Data::Preallocate);
   tmp.mSize = mSize + l;
   tmp.mCapacity = tmp.mSize;
   memcpy(tmp.mBuf, mBuf, mSize);
   memcpy(tmp.mBuf + mSize, str, l+1);

   return tmp;
}
Data Data::operator+ ( char  c) const

Concatenates a single byte after Data object.

Definition at line 1047 of file Data.cxx.

References mBuf, mCapacity, mSize, and Preallocate.

{
   Data tmp(mSize + 1, Data::Preallocate);
   tmp.mSize = mSize + 1;
   tmp.mCapacity = tmp.mSize;
   memcpy(tmp.mBuf, mBuf, mSize);
   tmp.mBuf[mSize] = c;
   tmp.mBuf[mSize+1] = 0;

   return tmp;
}
Data& resip::Data::operator+= ( const Data rhs) [inline]

Appends a data object to this one.

Definition at line 444 of file Data.hxx.

References append(), data(), and size().

      {
         return append(rhs.data(), rhs.size());
      }

Here is the call graph for this function:

Data& resip::Data::operator+= ( const char *  str) [inline]

Appends a null-terminated string to the end of the Data object.

Warning:
Passing a non-null-terminated string to this method would be a Really Bad Thing.

Definition at line 456 of file Data.hxx.

References append().

      {
         assert(str);
         return append(str, (size_type)strlen(str));
      }

Here is the call graph for this function:

Data& resip::Data::operator+= ( char  c) [inline]

Appends a single byte to the Data object.

Definition at line 466 of file Data.hxx.

References append().

      {
         return append(&c, 1);
      }

Here is the call graph for this function:

Data& resip::Data::operator= ( const Data data) [inline]

Definition at line 399 of file Data.hxx.

References copy(), mBuf, and mSize.

      {
         if (&data==this)
             return *this;
         return copy(data.mBuf,data.mSize);
      }

Here is the call graph for this function:

Data& resip::Data::operator= ( const char *  str) [inline]

Assigns a null-terminated string to the buffer.

Warning:
Passing a non-null-terminated string to this method would be a Really Bad Thing. The strlen() inlined to take advantages of cases where the compiler knows the length statically.

Definition at line 418 of file Data.hxx.

References copy().

      {
         return copy(str, (size_type)strlen(str));
      }

Here is the call graph for this function:

char& resip::Data::operator[] ( size_type  p) [inline]

Returns the character at the specified position.

Ensures that ownership of the buffer is taken, since the character could be modified by the caller.

Definition at line 484 of file Data.hxx.

References mBuf, mSize, and own().

      {
         assert(p < mSize);
         own();
         return mBuf[p];
      }

Here is the call graph for this function:

char resip::Data::operator[] ( size_type  p) const [inline]

Returns the character at the specified position.

Definition at line 494 of file Data.hxx.

References mBuf, and mSize.

      {
         assert(p < mSize);
         return mBuf[p];
      }
Data & Data::operator^= ( const Data rhs)

Performs an in-place exclusive-or of this buffer buffer with the specified buffer.

If the specifed buffer is longer than this buffer, then this buffer will first be expanded and zero-padded.

Definition at line 927 of file Data.cxx.

References end(), mBuf, mCapacity, mSize, resip::resipMax(), and resize().

{
   if (mCapacity < rhs.mSize)
   {
      resize(rhs.mSize, true);
   }
   if (mSize < rhs.mSize)
   {
      memset(mBuf+mSize, 0, mCapacity - mSize);
   }

   char* c1 = mBuf;
   char* c2 = rhs.mBuf;
   char* end = c2 + rhs.mSize;
   while (c2 != end)
   {
      *c1++ ^= *c2++;
   }
   mSize = resipMax(mSize, rhs.mSize);
   
   return *this;
}

Here is the call graph for this function:

void Data::own ( ) const [private]

Copies the contents of this data to a new buffer if the Data does not own the current buffer.

Definition at line 1072 of file Data.cxx.

References mShareEnum, mSize, resize(), and Share.

Referenced by at(), lowercase(), operator[](), replace(), schemeLowercase(), and uppercase().

{
   if (mShareEnum == Share)
   {
      const_cast<Data*>(this)->resize(mSize, true);
   }
}

Here is the call graph for this function:

bool Data::postfix ( const Data post) const

Returns true if this Data ends with the bytes indicated by the passed-in Data.

For example, if this Data is "abc", then postfix(Data("bc")) would be true; however, postfix(Data("ab")) would be false.

Definition at line 1805 of file Data.cxx.

References data(), and size().

Referenced by TestData::main().

{
   if (post.size() > size())
   {
      return false;
   }

   return memcmp(data() + (size()-post.size()), post.data(), post.size()) == 0;
}

Here is the call graph for this function:

bool Data::prefix ( const Data pre) const

Returns true if this Data starts with the bytes indicated by the passed-in Data.

For example, if this Data is "abc", then prefix(Data("ab")) would be true; however, prefix(Data("abcd")) would be false.

Definition at line 1794 of file Data.cxx.

References data(), and size().

Referenced by TestData::main(), and resip::Log::toLevel().

{
   if (pre.size() > size())
   {
      return false;
   }

   return memcmp(data(), pre.data(), pre.size()) == 0;
}

Here is the call graph for this function:

size_t Data::rawCaseInsensitiveHash ( const unsigned char *  c,
size_t  size 
) [static]

Creates a 32-bit hash based on the contents of the indicated buffer, after normalizing any alphabetic characters to lowercase.

Parameters:
cPointer to the buffer to hash
sizeNumber of bytes to be hashed

Definition at line 1942 of file Data.cxx.

References end(), randomPermutation, and size().

Referenced by caseInsensitivehash().

{

   union 
   {
         size_t st;
         unsigned char bytes[4];
   };
   st = 0; // suppresses warnings about unused st
   bytes[0] = randomPermutation[0];
   bytes[1] = randomPermutation[1];
   bytes[2] = randomPermutation[2];
   bytes[3] = randomPermutation[3];

   const unsigned char* end = c + size;
   for ( ; c != end; ++c)
   {
      unsigned char cc = tolower(*c);
      bytes[0] = randomPermutation[cc ^ bytes[0]];
      bytes[1] = randomPermutation[cc ^ bytes[1]];
      bytes[2] = randomPermutation[cc ^ bytes[2]];
      bytes[3] = randomPermutation[cc ^ bytes[3]];
   }

   // convert from network to host byte order
   return ntohl((u_long)st);
}

Here is the call graph for this function:

size_t Data::rawCaseInsensitiveTokenHash ( const unsigned char *  c,
size_t  size 
) [static]

A faster version of rawCaseInsensitiveHash that has the same collision properties if this Data is made up of RFC 3261 token characters.

Parameters:
cPointer to the buffer to hash
sizeNumber of bytes to be hashed
Note:
This is not guaranteed to return the same value as rawCaseInsensitiveHash.

Definition at line 2000 of file Data.cxx.

References data(), get16bits, get32bits, and hash().

Referenced by caseInsensitiveTokenHash().

{
   // .bwc. Hsieh hash, with some bitmasking to get the case-insensitive 
   // property (this is what all the "0x2020 |" business is about.)
   UInt32 hash = len, tmp;
   int rem;

    if (len <= 0 || data == NULL) return 0;

    rem = len & 3;
    len >>= 2;

    union 
    {
          UInt32 masked;
          UInt16 ui16[2];
    };

    /* Main loop */
    for (;len > 0; len--) 
    {
        // mask out bit 6 for case-insensitivity
        masked = (0x20202020 | get32bits(data));
        hash  += ui16[0];
        tmp    = ((UInt32)ui16[1] << 11) ^ hash;
        hash   = (hash << 16) ^ tmp;
        data  += 2*sizeof (UInt16);
        hash  += hash >> 11;
    }

    /* Handle end cases */
    switch (rem) 
    {
        case 3: hash += (0x2020 | get16bits (data));
                hash ^= hash << 16;
                hash ^= (0x20 | data[sizeof (UInt16)]) << 18;
                hash += hash >> 11;
                break;
        case 2: hash += (0x2020 | get16bits (data));
                hash ^= hash << 11;
                hash += hash >> 17;
                break;
        case 1: hash += (0x20 | *data);
                hash ^= hash << 10;
                hash += hash >> 1;
    }

    /* Force "avalanching" of final 127 bits */
    hash ^= hash << 3;
    hash += hash >> 5;
    hash ^= hash << 4;
    hash += hash >> 17;
    hash ^= hash << 25;
    hash += hash >> 6;

    return hash;

//   union 
//   {
//         size_t st;
//         unsigned char bytes[4];
//   };
//   st = 0; // suppresses warnings about unused st
//   bytes[0] = randomPermutation[0];
//   bytes[1] = randomPermutation[1];
//   bytes[2] = randomPermutation[2];
//   bytes[3] = randomPermutation[3];
//
//   const unsigned char* end = c + size;
//   for ( ; c != end; ++c)
//   {
//       union
//       {
//         UInt32 temp;
//         UInt8 cccc[4];
//       };
//       // .bwc. Mask out bit 6, use a multiplication op to write the 
//       // resulting byte to each of the 4 bytes in temp, and xor the whole 
//       // thing with bytes/st.
//      temp = ( (*c | ' ') * 16843009UL) ^ st;
//      bytes[0] = randomPermutation[cccc[0]];
//      bytes[1] = randomPermutation[cccc[1]];
//      bytes[2] = randomPermutation[cccc[2]];
//      bytes[3] = randomPermutation[cccc[3]];
//   }
//
//   // convert from network to host byte order
//   return ntohl((u_long)st);
}

Here is the call graph for this function:

size_t Data::rawHash ( const unsigned char *  c,
size_t  size 
) [static]

Creates a 32-bit hash based on the contents of the indicated buffer.

Parameters:
cPointer to the buffer to hash
sizeNumber of bytes to be hashed

Definition at line 1911 of file Data.cxx.

References end(), randomPermutation, and size().

Referenced by hash().

{
   // 4 byte Pearson's hash
   // essentially random hashing

   union 
   {
         size_t st;
         unsigned char bytes[4];
   };
   st = 0; // suppresses warnings about unused st
   bytes[0] = randomPermutation[0];
   bytes[1] = randomPermutation[1];
   bytes[2] = randomPermutation[2];
   bytes[3] = randomPermutation[3];

   const unsigned char* end = c + size;
   for ( ; c != end; ++c)
   {
      bytes[0] = randomPermutation[*c ^ bytes[0]];
      bytes[1] = randomPermutation[*c ^ bytes[1]];
      bytes[2] = randomPermutation[*c ^ bytes[2]];
      bytes[3] = randomPermutation[*c ^ bytes[3]];
   }

   // convert from network to host byte order
   return ntohl((u_long)st);
}

Here is the call graph for this function:

int Data::replace ( const Data match,
const Data target,
int  max = INT_MAX 
)

Replaces up to max occurrences of the bytes match with target.

Returns the number of matches.

Definition at line 1848 of file Data.cxx.

References data(), empty(), find(), mBuf, mCapacity, mSize, npos, own(), resize(), and size().

Referenced by TestData::main().

{
   assert(!match.empty());

   int count = 0;

   const int incr = int(replaceWith.size() - match.size());
   for (size_type offset = find(match, 0); 
        count < max && offset != Data::npos; 
        offset = find(match, offset+replaceWith.size()))
   {
      if (mSize + incr >= mCapacity)
      {
         resize((mCapacity + incr) * 3 / 2, true);
      }
      else
      {
         own();
      }

      // move the memory forward (or backward)
      memmove(mBuf + offset + replaceWith.size(), mBuf + offset + match.size(), mSize - offset - match.size());
      memcpy(mBuf + offset, replaceWith.data(), replaceWith.size());
      mSize += incr;

      ++count;
   }

   return count;
}

Here is the call graph for this function:

void Data::reserve ( size_type  capacity)

Guarantees that the underlying buffer used by the Data is at least the number of bytes specified.

May cause reallocation of the buffer.

Definition at line 1012 of file Data.cxx.

References mCapacity, and resize().

Referenced by base64decode(), and resip::XMLCursor::XMLCursor().

{
   if (len > mCapacity)
   {
      resize(len, true);
   }
}

Here is the call graph for this function:

resip::Data::RESIP_HeapCount ( Data  )
void Data::resize ( size_type  newSize,
bool  copy 
) [private]
Note:
Always allocates a new buffer

Definition at line 1082 of file Data.cxx.

References Borrow, LocalAlloc, mBuf, mCapacity, mPreBuffer, mShareEnum, mSize, Share, and Take.

Referenced by append(), at(), c_str(), copy(), Data(), resip::ParseBuffer::dataUnescaped(), getBuf(), operator^=(), own(), replace(), and reserve().

{
   assert(newCapacity >= mCapacity || mShareEnum == Data::Share);

   char *oldBuf = mBuf;
   bool needToDelete=(mShareEnum==Take);

   if(newCapacity > LocalAlloc)
   {
      mBuf = new char[newCapacity+1];
      mShareEnum = Take;
   }
   else
   {
      mBuf = mPreBuffer;
      mShareEnum = Borrow;
   }

   if (copy)
   {
      memcpy(mBuf, oldBuf, mSize);
      mBuf[mSize] = 0;
   }

   if (needToDelete)
   {
      delete[] oldBuf;
   }

   mCapacity = newCapacity;
}
Data & Data::schemeLowercase ( )

Converts this Data to lowercase, assuming this Data only consists of scheme characters.

Note:
Assuming scheme contents allows the use of a bitmask instead of tolower(), which is faster. Why, you ask? A bitmask is sufficient to perform a lowercase operation on alphabetical data, since 'a' and 'A' only differ on bit 6; it is set for 'a', but not for 'A'. Digits always have bit 6 set, so setting it is a no-op. The last three characters in the scheme character set are '+', '-', and '.'; all of these have bit 6 set as well. Note that there is no corresponding efficient uppercase function; clearing bit 6 on either a digit or the the three remaining characters (+=.) will change them.

Definition at line 1567 of file Data.cxx.

References mBuf, mSize, and own().

{
   own();
   char* p = mBuf;
   for (size_type i=0; i < mSize; ++i)
   {
      *p |= ' ';
      ++p;
   }
   return *this;
}

Here is the call graph for this function:

Data & Data::setBuf ( ShareEnum  se,
const char *  buf,
size_type  length 
)

Set the Data to hold {buf} using share type {se}, which may be any of Share (read-only, no-free), Borrow (read-write, no-free) or Take (read-write, yes-free).

Both the capacity and current length are set to {length}; you can call truncate2() afterwords to shorten. The provided buffer (and its current contents) will be used going forward; any currently owned buffer will be released. NOTE: The {buf} param is declared const to support Share type; for Borrow and Take the buffer may be written (e.g., treated non-const).

Definition at line 656 of file Data.cxx.

References mBuf, mCapacity, mShareEnum, mSize, and Take.

Referenced by setBuf().

{
   assert(buffer);
   if (mShareEnum == Take)
   {
      delete[] mBuf;
   }
   mBuf = const_cast<char*>(buffer);
   mCapacity = mSize = length;
   mShareEnum = se;
   return *this;
}
Data& resip::Data::setBuf ( ShareEnum  se,
const char *  str 
) [inline]

Convience function to call setBuf() with a NULL-terminated string.

This is in-lined for case where compiler knows strlen statically.

Definition at line 333 of file Data.hxx.

References setBuf().

      {
         return setBuf(se, str, (size_type)strlen(str));
      };

Here is the call graph for this function:

size_type resip::Data::size ( ) const [inline]
bool Data::sizeEqualCaseInsensitiveTokenCompare ( const Data rhs) const

Definition at line 2153 of file Data.cxx.

References compUnalignedRemainder, get32bits, mBuf, and mSize.

Referenced by caseInsensitiveTokenCompare().

{
   assert(mSize==rhs.mSize);
   const char* d1(mBuf);
   const char* d2(rhs.mBuf);

   if(mSize < 4)
   {
      // No point in trying 32-bit ops.
      compUnalignedRemainder(d1, d2, mSize);
      return true;
   }

   int unalignedPrefix((ptrdiff_t)d1 & 3);

   compUnalignedRemainder(d1, d2, unalignedPrefix);

   // We are now on a 32-bit boundary with d1.
   UInt32* wd1((UInt32*)(d1));
   size_t wordLen=(mSize-unalignedPrefix)>>2;
   int rem=(mSize-unalignedPrefix)&3;

   if((ptrdiff_t)(d2)%4==0)
   {
      // d2 is aligned too. Happy day!
      UInt32* wd2((UInt32*)(d2));
      for (;wordLen > 0; wordLen--)
      {
         // bitwise xor is zero iff equal, but we only really care about bits 
         // other than bit 6, so we mask out bit 6 after the xor.
         if( (*wd1 ^ *wd2) & 0xDFDFDFDF)
         {
            return false;
         }
         wd1++;
         wd2++;
      }
      d2=(const char*)wd2;
   }
   else
   {
      for (;wordLen > 0; wordLen--)
      {
         // bitwise xor is zero iff equal, but we only really care about bits 
         // other than bit 6, so we mask out bit 6 after the xor.
         UInt32 test=get32bits(d2);
         if( (*wd1 ^ test) & 0xDFDFDFDF)
         {
            return false;
         }
         wd1++;
         d2+=4;
      }
   }
   d1=(const char*)wd1;

   compUnalignedRemainder(d1, d2, rem);
   return true;
}
Data Data::substr ( size_type  first,
size_type  count = Data::npos 
) const

Copies a portion of this Data into a new Data.

Parameters:
firstIndex of the first byte to copy
countNumber of bytes to copy

Definition at line 1816 of file Data.cxx.

References Data(), mBuf, mSize, and npos.

Referenced by resip::DnsUtil::getLocalDomainName(), TestData::main(), and resip::Log::toLevel().

{
   assert(first <= mSize);
   if ( count == Data::npos)
   {
      return Data(mBuf+first, mSize-first);
   }
   else
   {
      assert(first + count <= mSize);
      return Data(mBuf+first, count);
   }
}

Here is the call graph for this function:

Data & Data::takeBuf ( Data other)

Take the data from {other}.

Any current buffer is released. {this} will have the same storage mode as {other} and steal its buffer. All storage modes of {other} (Share,Borrow,Take) are legal. When done, {other} will be empty (it will ref its internal buffer).

Definition at line 670 of file Data.cxx.

References Borrow, LocalAlloc, mBuf, mCapacity, mPreBuffer, mShareEnum, mSize, and Take.

{
   if ( &other == this )
      return *this;

   if (mShareEnum == Data::Take)
      delete[] mBuf;

   if ( other.mBuf == other.mPreBuffer )
   {
      // plus one picks up the terminating safety NULL
      memcpy( mPreBuffer, other.mPreBuffer, other.mSize+1);
      mBuf = mPreBuffer;
   }
   else
   {
      mBuf = other.mBuf;
      other.mBuf = other.mPreBuffer;
   }
   mSize = other.mSize;
   mCapacity = other.mCapacity;
   mShareEnum = other.mShareEnum;

   // reset {other} to same state as the default Data() constructor
   // note that other.mBuf is set above
   other.mSize = 0;
   other.mCapacity = LocalAlloc;
   other.mShareEnum = Data::Borrow;
   other.mPreBuffer[0] = 0;

   return *this;
}
std::bitset< 256 > Data::toBitset ( const resip::Data chars) [static]

Creates a bitset reflecting the contents of this data (as a set) ie.

"15eo" would have the bits 49, 53, 101, and 111 set to true, and all others set to false

Definition at line 2214 of file Data.cxx.

References mBuf, and mSize.

Referenced by resip::ConfigParse::parseCommandLine().

{
   std::bitset<256> result;
   result.reset();
   for (unsigned int i=0; i!=chars.mSize;++i)
   {
      result.set(*(unsigned char*)(chars.mBuf+i));
   }
   return result;
}
Data Data::trunc ( size_type  trunc) const

Shortens the size of this Data.

If the contents are truncated, this method appends two dot ('.') characters to the end. Presumably, this is used for output purposes.

Definition at line 1506 of file Data.cxx.

References Data(), data(), and size().

{
   if (size() <= s)
   {
      return *this;
   }
   else
   {
      return Data(data(), s) + "..";
   }
}

Here is the call graph for this function:

Data::size_type Data::truncate ( size_type  len)

Shortens the size of this Data.

Does not impact the size of the allocated buffer. This owns() the buffer (undoes Share) so as to write terminating NULL. See truncate2() as alternative.

Deprecated:
dlb says that no one uses this and it should be removed.
Todo:
Remove this at some point.

Definition at line 891 of file Data.cxx.

References mSize.

{
   if (len < mSize)
   {
      (*this)[len] = 0;
      mSize = len;
   }

   return mSize;
}
Data & Data::truncate2 ( size_type  len)

Shortens the size of this Data so length is at most of {len}.

(If already shorter, doesn't increase length). Does not affect buffer allocation, and doesn't impact writing terminating NULL. Thus is safe to use with Share'd or external Take'n buffers.

Definition at line 903 of file Data.cxx.

References mSize.

Referenced by clear().

{
   if (len < mSize)
   {
      // NOTE: Do not write terminating NULL, to avoid un-doing Share
      mSize = len;
   }
   return *this;
}
Data & Data::uppercase ( )

Converts this Data to uppercase.

Note:
This is silly unless the contents are ASCII.

Definition at line 1554 of file Data.cxx.

References mBuf, mSize, and own().

Referenced by TestData::main().

{
   own();
   char* p = mBuf;
   for (size_type i=0; i < mSize; ++i)
   {
      *p = toupper(*p);
      ++p;
   }
   return *this;
}

Here is the call graph for this function:

EncodeStream & Data::urlDecode ( EncodeStream &  s) const

Un-escapes a Data to a stream according to HTTP URL encoding rules.

Definition at line 1293 of file Data.cxx.

References data(), hexpair2int(), and size().

Referenced by urlDecoded().

{
   unsigned int i = 0;
   for (const char* p = data(); p != data()+size(); ++p, ++i)
   {
      unsigned char c = *p;
      if (c == '%')
      {
         if (i+2 < size())
         {
            s << (char) hexpair2int( *(p+1), *(p+2));
            p += 2;
         }
         else
         {
            break;
         }
      }
      else if (c == '+')
      {
         s << ' ';
      }
      else
      {
         s << c;
      }
   }
   return s;
}

Here is the call graph for this function:

Data Data::urlDecoded ( ) const

Performs in-place HTTP URL un-escaping of a Data.

Definition at line 1283 of file Data.cxx.

References urlDecode().

Referenced by TestData::main().

{
   Data buffer;
   DataStream strm(buffer);
   urlDecode(strm);
   strm.flush();
   return buffer;
}

Here is the call graph for this function:

EncodeStream & Data::urlEncode ( EncodeStream &  s) const

Escapes a Data to a stream according to HTTP URL encoding rules.

Definition at line 1363 of file Data.cxx.

References data(), hexmap, size(), and urlNonEncodedChars.

Referenced by TestData::main(), and urlEncoded().

{
   for (const char* p = data(); p != data() + size(); ++p)
   {
      unsigned char c = *p;

      // pchar, slash, questionmark
      // pchar = unreserved, sub-delims, colon, at-sign
      // unreserved = alphanum, hyphen, underscore, period, tilde
      // subdelims = bang (!), dollar, ampersand (oops!), plus, single-quote, lparen, rparen, asterisk, comma, semicolon, equal
      //if (isalpha(c) || isdigit(c) || strchr("-_.~!$+'()*,;=:@/?", c)) //
      // strchr is inefficient and wrong
      if (urlNonEncodedChars[c])
      {
         s << c;
      }
      else
      {
         if (c == 0x20)
         {
            s << '+';
         }
         else
         {
            s << '%' << (hexmap[(c & 0xF0)>>4]) << (hexmap[(c & 0x0F)]);
         }
      }
   }

   return s;
}

Here is the call graph for this function:

Data Data::urlEncoded ( ) const

Performs in-place HTTP URL escaping of a Data.

Definition at line 1273 of file Data.cxx.

References urlEncode().

Referenced by TestData::main().

{
   Data buffer;
   DataStream strm(buffer);
   urlEncode(strm);
   strm.flush();
   return buffer;
}

Here is the call graph for this function:

Data Data::xmlCharDataDecode ( ) const

Performs in-place XML Character Data un-escaping of a Data.

Definition at line 1406 of file Data.cxx.

{
   Data buffer;
   DataStream strm(buffer);
   xmlCharDataDecode(strm);
   strm.flush();
   return buffer;
}
EncodeStream & Data::xmlCharDataDecode ( EncodeStream &  s) const

Un-escapes a Data to a stream according to XML Character Data encoding rules.

Definition at line 1449 of file Data.cxx.

References data(), and size().

{
   unsigned int i = 0;
   for (const char* p = data(); p != data()+size(); ++p, ++i)
   {
      unsigned char c = *p;
      if (c == '&')
      {
         // look for amp;
         if(i+4 < size() && 
            *(p+1) == 'a' && *(p+2) == 'm' && *(p+3) == 'p' && *(p+4) == ';')
         {
            s << '&';
            p += 4;
         }
         // look for lt;
         else if(i+3 < size() && 
            *(p+1) == 'l' && *(p+2) == 't' && *(p+3) == ';')
         {
            s << '<';
            p += 3;
         }
         // look for gt;
         else if(i+3 < size() &&
            *(p+1) == 'g' && *(p+2) == 't' && *(p+3) == ';')
         {
            s << '>';
            p += 3;
         }
         // look for apos;
         else if(i+5 < size() && 
            *(p+1) == 'a' && *(p+2) == 'p' && *(p+3) == 'o' && *(p+4) == 's' && *(p+5) == ';')
         {
            s << '\'';
            p += 5;
         }
         // look for quot;
         else if(i+5 < size() && 
            *(p+1) == 'q' && *(p+2) == 'u' && *(p+3) == 'o' && *(p+4) == 't' && *(p+5) == ';')
         {
            s << '\"';
            p += 5;
         }
         else // if no conversion found - just leave characters in data
         {
            s << c;
         }
      }
      else
      {
         s << c;
      }
   }
   return s;
}

Here is the call graph for this function:

Data Data::xmlCharDataEncode ( ) const

Performs in-place XML Character Data escaping of a Data.

Definition at line 1396 of file Data.cxx.

{
   Data buffer;
   DataStream strm(buffer);
   xmlCharDataEncode(strm);
   strm.flush();
   return buffer;
}
EncodeStream & Data::xmlCharDataEncode ( EncodeStream &  s) const

Escapes a Data to a stream according to XML Character Data encoding rules.

Definition at line 1417 of file Data.cxx.

References data(), and size().

{
   for (const char* p = data(); p != data() + size(); ++p)
   {
      unsigned char c = *p;

      switch(c)
      {
      case '&':
         s << "&amp;";
         break;
      case '<':
         s << "&lt;";
         break;
      case '>':
         s << "&gt;";
         break;
      case '\'':
         s << "&apos;";
         break;
      case '\"':
         s << "&quot;";
         break;
      default:
         s << c;
      }
   }

   return s;
}

Here is the call graph for this function:


Friends And Related Function Documentation

friend class ::TestData [friend]

Definition at line 981 of file Data.hxx.

friend class DataBuffer [friend]

Definition at line 978 of file Data.hxx.

friend class DataStream [friend]

Definition at line 979 of file Data.hxx.

friend class MD5Buffer [friend]

Definition at line 982 of file Data.hxx.

friend class oDataStream [friend]

Definition at line 980 of file Data.hxx.

bool operator< ( const Data lhs,
const Data rhs 
) [friend]
bool operator< ( const Data lhs,
const char *  rhs 
) [friend]
bool operator< ( const char *  lhs,
const Data rhs 
) [friend]
std::ostream& operator<< ( std::ostream &  strm,
const Data d 
) [friend]

Definition at line 1087 of file Data.hxx.

{
   return strm.write(d.mBuf, d.mSize);
}
EncodeStream& operator<< ( EncodeStream &  strm,
const Data d 
) [friend]
bool operator== ( const Data lhs,
const Data rhs 
) [friend]
bool operator== ( const Data lhs,
const char *  rhs 
) [friend]
friend class ParseBuffer [friend]

Definition at line 977 of file Data.hxx.


Member Data Documentation

const Data Data::Empty [static]
char* resip::Data::mBuf [private]
char resip::Data::mPreBuffer[LocalAlloc] [private]

Definition at line 965 of file Data.hxx.

Referenced by Data(), resize(), and takeBuf().

const Data::size_type Data::npos = UINT_MAX [static]

Represents an impossible position; returned to indicate failure to find.

Definition at line 820 of file Data.hxx.

Referenced by find(), resip::DnsUtil::getLocalDomainName(), TestData::main(), replace(), and substr().

used only to disambiguate constructors

Definition at line 113 of file Data.hxx.

Referenced by charEncoded(), charUnencoded(), Data(), escapeAndAnnotate(), escaped(), hex(), main(), TestData::main(), operator+(), and spaces().


The documentation for this class was generated from the following files: