Brian Silverman | 395d625 | 2013-09-13 20:58:14 -0700 | [diff] [blame^] | 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // A buffer for dealing with data. Some special support for PTP is |
| 4 | // available. |
| 5 | |
| 6 | #ifndef _GLIBUSB_GBUFFER_H_ |
| 7 | #define _GLIBUSB_GBUFFER_H_ |
| 8 | |
| 9 | #include <stdint.h> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | namespace glibusb { |
| 14 | |
| 15 | // Buffer of bytes. |
| 16 | class Buffer { |
| 17 | public: |
| 18 | // Underlying byte store type. |
| 19 | typedef std::vector<uint8_t> ByteBuffer; |
| 20 | typedef ByteBuffer::size_type size_type; |
| 21 | |
| 22 | // Destructor. |
| 23 | ~Buffer(); |
| 24 | |
| 25 | // Constructs an empty buffer. |
| 26 | Buffer(); |
| 27 | |
| 28 | // Constructs a buffer from memory of given length. Data is copied. |
| 29 | // (This is an interface to C functions, chiefly, libusb, and should |
| 30 | // only be used for such purposes.) |
| 31 | Buffer(const void *src, size_type length); |
| 32 | |
| 33 | // Returns true iff the buffers contain the same data. |
| 34 | bool operator==(const Buffer &other) const; |
| 35 | |
| 36 | // Returns true iff the buffer does not contain the same data. |
| 37 | bool operator!=(const Buffer &other) const; |
| 38 | |
| 39 | // Returns a new allocated slice of the buffer. |
| 40 | Buffer *MakeSlice(size_type offset, size_type length) const; |
| 41 | |
| 42 | // Returns the length. |
| 43 | size_type Length() const { return buffer_.size(); } |
| 44 | |
| 45 | // Clears (as in std::vector) the buffer. |
| 46 | void Clear(); |
| 47 | |
| 48 | // Resizes (as in std::vector) the buffer. |
| 49 | void Resize(size_type length); |
| 50 | |
| 51 | // Returns a pointer to the underlying store that of a guaranteed |
| 52 | // length (or CHECK), possibly beginning at a given offset. (These |
| 53 | // are interfaces to C functions, chiefly, libusb, and should only |
| 54 | // be used for such purposes.) |
| 55 | void *GetBufferPointer(size_type length); |
| 56 | const void *GetBufferPointer(size_type length) const; |
| 57 | void *GetBufferPointer(size_type offset, size_type length); |
| 58 | const void *GetBufferPointer(size_type offset, size_type length) const; |
| 59 | |
| 60 | // Gets the value of integral type T from the buffer at the offset |
| 61 | // byte_offset, places it in value_out, and returns the length of |
| 62 | // the marshalled data. The integer is expected to be in little |
| 63 | // endian format. This template is specialized for |
| 64 | // {int,uint}{8,16,32,64}_t. |
| 65 | template <class T> |
| 66 | size_type Get(size_type byte_offset, T *value_out) const; |
| 67 | |
| 68 | // Gets an ASCII string from the buffer at the offset byte_offset, |
| 69 | // places it in value_out, and returns the length of the marshalled |
| 70 | // data. The string data in the buffer is expected to be |
| 71 | // null-terminated. |
| 72 | size_type Get(size_type byte_offset, std::string *value_out) const; |
| 73 | |
| 74 | // Puts the value of integral type T into the buffer offset |
| 75 | // byte_offset and returns the length of the marshalled data. Data |
| 76 | // are put in little endian format. This template is specialized for |
| 77 | // {int,uint}{8,16,32,64}_t. |
| 78 | template <class T> |
| 79 | size_type Put(size_type byte_offset, T value); |
| 80 | |
| 81 | // Appends the value of type T into the buffer offset byte_offset |
| 82 | // and returns the length of the marshalled data. Data are appended |
| 83 | // in little endian format. This template is available for |
| 84 | // {int,uint}{8,16,32,64}_t. |
| 85 | template <class T> void Append(const T value); |
| 86 | |
| 87 | // Append a buffer. |
| 88 | void Append(const Buffer &buffer); |
| 89 | |
| 90 | // Inserts length bytes of (uninitialized) space at the beginning of |
| 91 | // the buffer. |
| 92 | void AddHeader(size_type length); |
| 93 | |
| 94 | // Removes length bytes of space from the beginning of the buffer. |
| 95 | void RemoveHeader(size_type length); |
| 96 | |
| 97 | // Copies the source buffer. |
| 98 | void Copy(const Buffer &source); |
| 99 | |
| 100 | #if 0 |
| 101 | // Writes the contents of the buffer to the file, or dies. |
| 102 | void WriteOrDie(File *fp) const; |
| 103 | |
| 104 | // Writes the contents of the buffer to the path, or dies. |
| 105 | void WriteToPathOrDie(const char *name) const; |
| 106 | #endif |
| 107 | |
| 108 | // Returns a hex dump of the buffer. |
| 109 | std::string Dump() const; |
| 110 | |
| 111 | private: |
| 112 | // The underlying byte store. |
| 113 | ByteBuffer buffer_; |
| 114 | |
| 115 | Buffer(const Buffer &) = delete; |
| 116 | void operator=(const Buffer &) = delete; |
| 117 | }; |
| 118 | |
| 119 | |
| 120 | // Template for Buffer::Append for integral values. |
| 121 | template <typename T> |
| 122 | void Buffer::Append(const T value) { |
| 123 | size_type offset = Length(); |
| 124 | Resize(offset + sizeof(T)); |
| 125 | Put(offset, value); |
| 126 | } |
| 127 | |
| 128 | } // namespace glibusb |
| 129 | |
| 130 | #endif // _GLIBUSB_GBUFFER_H_ |