Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 5 | #ifndef SHILL_BYTE_STRING_H_ |
| 6 | #define SHILL_BYTE_STRING_H_ |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 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) {} |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 21 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 22 | ByteString(const unsigned char *data, size_t length) |
| 23 | : data_(data, data + length) {} |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 24 | |
| 25 | ByteString(const char *data, size_t length) |
| 26 | : data_(data, data + length) {} |
| 27 | |
| 28 | ByteString(const signed char *data, size_t length) |
| 29 | : data_(data, data + length) {} |
| 30 | |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 31 | ByteString(const std::string &data, bool copy_terminator) |
| 32 | : data_(reinterpret_cast<const unsigned char *>(data.c_str()), |
| 33 | reinterpret_cast<const unsigned char *>(data.c_str() + |
| 34 | data.length() + |
| 35 | (copy_terminator ? |
| 36 | 1 : 0))) {} |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 37 | |
| 38 | ByteString &operator=(const ByteString &b) { |
| 39 | data_ = b.data_; |
| 40 | return *this; |
| 41 | } |
| 42 | |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 43 | unsigned char *GetData() { return data_.data(); } |
| 44 | const unsigned char *GetConstData() const { return data_.data(); } |
| 45 | size_t GetLength() const { return data_.size(); } |
| 46 | |
| 47 | // Returns a ByteString containing |length| bytes from the ByteString |
| 48 | // starting at |offset|. This function truncates the returned string |
| 49 | // if part (or all) of this requested data lies outside the bounds of |
| 50 | // this ByteString. |
| 51 | ByteString GetSubstring(size_t offset, size_t length) const; |
| 52 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 53 | // Inserts a uint32 into a ByteString in cpu-order |
| 54 | static ByteString CreateFromCPUUInt32(uint32 val); |
| 55 | // Inserts a uint32 into a ByteString in network-order |
| 56 | static ByteString CreateFromNetUInt32(uint32 val); |
| 57 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 58 | // Converts to a uint32 from a host-order value stored in the ByteString |
| 59 | // Returns true on success |
| 60 | bool ConvertToCPUUInt32(uint32 *val) const; |
| 61 | // Converts to a uint32 from a network-order value stored in the ByteString |
| 62 | // Returns true on success |
| 63 | bool ConvertToNetUInt32(uint32 *val) const; |
| 64 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 65 | bool IsEmpty() const { return GetLength() == 0; } |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 66 | |
| 67 | // Returns true if every element of |this| is zero, false otherwise. |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 68 | bool IsZero() const; |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 69 | |
| 70 | // Perform an AND operation between each element of |this| with the |
| 71 | // corresponding byte of |b|. Returns true if both |this| and |b| |
| 72 | // are the same length, and as such the operation succeeds; false |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame] | 73 | // if they are not. The result of the operation is stored in |this|. |
| 74 | bool BitwiseAnd(const ByteString &b); |
| 75 | |
| 76 | // Perform an OR operation between each element of |this| with the |
| 77 | // corresponding byte of |b|. Returns true if both |this| and |b| |
| 78 | // are the same length, and as such the operation succeeds; false |
| 79 | // if they are not. The result of the operation is stored in |this|. |
| 80 | bool BitwiseOr(const ByteString &b); |
| 81 | |
| 82 | // Perform an inversion operation on each of the bits this string. |
| 83 | void BitwiseInvert(); |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 84 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 85 | bool Equals(const ByteString &b) const; |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 86 | void Append(const ByteString &b); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 87 | void Clear() { data_.clear(); } |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 88 | void Resize(int size) { |
| 89 | data_.resize(size, 0); |
| 90 | } |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 91 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 92 | std::string HexEncode() const; |
| 93 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 94 | private: |
| 95 | std::vector<unsigned char> data_; |
| 96 | // NO DISALLOW_COPY_AND_ASSIGN -- we assign ByteStrings in STL hashes |
| 97 | }; |
| 98 | |
| 99 | } // namespace shill |
| 100 | |
| 101 | |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 102 | #endif // SHILL_BYTE_STRING_H_ |