Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SHILL_BYTE_STRING_ |
| 6 | #define SHILL_BYTE_STRING_ |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <base/basictypes.h> |
| 11 | |
| 12 | namespace shill { |
| 13 | |
| 14 | // Provides a holder of a string of bytes |
| 15 | class ByteString { |
| 16 | public: |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame^] | 17 | ByteString() {} |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 18 | ByteString(const ByteString &b) : data_(b.data_) {} |
| 19 | explicit ByteString(size_t length) : data_(length) {} |
| 20 | ByteString(const unsigned char *data, size_t length) |
| 21 | : data_(data, data + length) {} |
| 22 | |
| 23 | ByteString &operator=(const ByteString &b) { |
| 24 | data_ = b.data_; |
| 25 | return *this; |
| 26 | } |
| 27 | |
| 28 | // Inserts a uint32 into a ByteString in cpu-order |
| 29 | static ByteString CreateFromCPUUInt32(uint32 val); |
| 30 | // Inserts a uint32 into a ByteString in network-order |
| 31 | static ByteString CreateFromNetUInt32(uint32 val); |
| 32 | |
| 33 | unsigned char *GetData() { return data_.data(); } |
| 34 | const unsigned char *GetConstData() const { return data_.data(); } |
| 35 | size_t GetLength() const { return data_.size(); } |
| 36 | |
| 37 | // Converts to a uint32 from a host-order value stored in the ByteString |
| 38 | // Returns true on success |
| 39 | bool ConvertToCPUUInt32(uint32 *val) const; |
| 40 | // Converts to a uint32 from a network-order value stored in the ByteString |
| 41 | // Returns true on success |
| 42 | bool ConvertToNetUInt32(uint32 *val) const; |
| 43 | |
| 44 | bool IsZero() const; |
| 45 | bool Equals(const ByteString &b) const; |
| 46 | |
| 47 | private: |
| 48 | std::vector<unsigned char> data_; |
| 49 | // NO DISALLOW_COPY_AND_ASSIGN -- we assign ByteStrings in STL hashes |
| 50 | }; |
| 51 | |
| 52 | } // namespace shill |
| 53 | |
| 54 | |
| 55 | #endif // SHILL_BYTE_STRING_ |