Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2004--2005, Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef TALK_BASE_BYTEBUFFER_H_
     29 #define TALK_BASE_BYTEBUFFER_H_
     30 
     31 #include <string>
     32 
     33 #include "talk/base/basictypes.h"
     34 #include "talk/base/constructormagic.h"
     35 
     36 namespace talk_base {
     37 
     38 class ByteBuffer {
     39  public:
     40 
     41   enum ByteOrder {
     42     ORDER_NETWORK = 0,  // Default, use network byte order (big endian).
     43     ORDER_HOST,         // Use the native order of the host.
     44   };
     45 
     46   // |byte_order| defines order of bytes in the buffer.
     47   ByteBuffer();
     48   explicit ByteBuffer(ByteOrder byte_order);
     49   ByteBuffer(const char* bytes, size_t len);
     50   ByteBuffer(const char* bytes, size_t len, ByteOrder byte_order);
     51 
     52   // Initializes buffer from a zero-terminated string.
     53   explicit ByteBuffer(const char* bytes);
     54 
     55   ~ByteBuffer();
     56 
     57   const char* Data() const { return bytes_ + start_; }
     58   size_t Length() const { return end_ - start_; }
     59   size_t Capacity() const { return size_ - start_; }
     60   ByteOrder Order() const { return byte_order_; }
     61 
     62   // Read a next value from the buffer. Return false if there isn't
     63   // enough data left for the specified type.
     64   bool ReadUInt8(uint8* val);
     65   bool ReadUInt16(uint16* val);
     66   bool ReadUInt24(uint32* val);
     67   bool ReadUInt32(uint32* val);
     68   bool ReadUInt64(uint64* val);
     69   bool ReadBytes(char* val, size_t len);
     70 
     71   // Appends next |len| bytes from the buffer to |val|. Returns false
     72   // if there is less than |len| bytes left.
     73   bool ReadString(std::string* val, size_t len);
     74 
     75   // Write value to the buffer. Resizes the buffer when it is
     76   // neccessary.
     77   void WriteUInt8(uint8 val);
     78   void WriteUInt16(uint16 val);
     79   void WriteUInt24(uint32 val);
     80   void WriteUInt32(uint32 val);
     81   void WriteUInt64(uint64 val);
     82   void WriteString(const std::string& val);
     83   void WriteBytes(const char* val, size_t len);
     84 
     85   // Reserves the given number of bytes and returns a char* that can be written
     86   // into. Useful for functions that require a char* buffer and not a
     87   // ByteBuffer.
     88   char* ReserveWriteBuffer(size_t len);
     89 
     90   // Resize the buffer to the specified |size|. This invalidates any remembered
     91   // seek positions.
     92   void Resize(size_t size);
     93 
     94   // Moves current position |size| bytes forward. Returns false if
     95   // there is less than |size| bytes left in the buffer. Consume doesn't
     96   // permanently remove data, so remembered read positions are still valid
     97   // after this call.
     98   bool Consume(size_t size);
     99 
    100   // Clears the contents of the buffer. After this, Length() will be 0.
    101   void Clear();
    102 
    103   // Used with GetReadPosition/SetReadPosition.
    104   class ReadPosition {
    105     friend class ByteBuffer;
    106     ReadPosition(size_t start, int version)
    107         : start_(start), version_(version) { }
    108     size_t start_;
    109     int version_;
    110   };
    111 
    112   // Remembers the current read position for a future SetReadPosition. Any
    113   // calls to Shift or Resize in the interim will invalidate the position.
    114   ReadPosition GetReadPosition() const;
    115 
    116   // If the given position is still valid, restores that read position.
    117   bool SetReadPosition(const ReadPosition &position);
    118 
    119  private:
    120   void Construct(const char* bytes, size_t size, ByteOrder byte_order);
    121 
    122   char* bytes_;
    123   size_t size_;
    124   size_t start_;
    125   size_t end_;
    126   int version_;
    127   ByteOrder byte_order_;
    128 
    129   // There are sensible ways to define these, but they aren't needed in our code
    130   // base.
    131   DISALLOW_COPY_AND_ASSIGN(ByteBuffer);
    132 };
    133 
    134 }  // namespace talk_base
    135 
    136 #endif  // TALK_BASE_BYTEBUFFER_H_
    137