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