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 | |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 8 | #include <string> |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
| 11 | #include <base/basictypes.h> |
| 12 | |
| 13 | namespace shill { |
| 14 | |
| 15 | // Provides a holder of a string of bytes |
| 16 | class ByteString { |
| 17 | public: |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 18 | ByteString() {} |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 19 | ByteString(const ByteString &b) : data_(b.data_) {} |
| 20 | explicit ByteString(size_t length) : data_(length) {} |
| 21 | ByteString(const unsigned char *data, size_t length) |
| 22 | : data_(data, data + length) {} |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 23 | ByteString(const std::string &data, bool copy_terminator) |
| 24 | : data_(reinterpret_cast<const unsigned char *>(data.c_str()), |
| 25 | reinterpret_cast<const unsigned char *>(data.c_str() + |
| 26 | data.length() + |
| 27 | (copy_terminator ? |
| 28 | 1 : 0))) {} |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 29 | |
| 30 | ByteString &operator=(const ByteString &b) { |
| 31 | data_ = b.data_; |
| 32 | return *this; |
| 33 | } |
| 34 | |
| 35 | // Inserts a uint32 into a ByteString in cpu-order |
| 36 | static ByteString CreateFromCPUUInt32(uint32 val); |
| 37 | // Inserts a uint32 into a ByteString in network-order |
| 38 | static ByteString CreateFromNetUInt32(uint32 val); |
| 39 | |
| 40 | unsigned char *GetData() { return data_.data(); } |
| 41 | const unsigned char *GetConstData() const { return data_.data(); } |
| 42 | size_t GetLength() const { return data_.size(); } |
| 43 | |
| 44 | // Converts to a uint32 from a host-order value stored in the ByteString |
| 45 | // Returns true on success |
| 46 | bool ConvertToCPUUInt32(uint32 *val) const; |
| 47 | // Converts to a uint32 from a network-order value stored in the ByteString |
| 48 | // Returns true on success |
| 49 | bool ConvertToNetUInt32(uint32 *val) const; |
| 50 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 51 | bool IsEmpty() const { return GetLength() == 0; } |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 52 | bool IsZero() const; |
| 53 | bool Equals(const ByteString &b) const; |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 54 | void Append(const ByteString &b); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 55 | void Clear() { data_.clear(); } |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 56 | void Resize(int size) { |
| 57 | data_.resize(size, 0); |
| 58 | } |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 59 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 60 | std::string HexEncode() const; |
| 61 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 62 | private: |
| 63 | std::vector<unsigned char> data_; |
| 64 | // NO DISALLOW_COPY_AND_ASSIGN -- we assign ByteStrings in STL hashes |
| 65 | }; |
| 66 | |
| 67 | } // namespace shill |
| 68 | |
| 69 | |
| 70 | #endif // SHILL_BYTE_STRING_ |