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: |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 18 | ByteString() : begin_(data_.begin()) {} |
| 19 | ByteString(const ByteString &b); |
| 20 | explicit ByteString(size_t length) : data_(length), begin_(data_.begin()) {} |
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) |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 23 | : data_(data, data + length), begin_(data_.begin()) {} |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 24 | |
| 25 | ByteString(const char *data, size_t length) |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 26 | : data_(data, data + length), begin_(data_.begin()) {} |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 27 | |
| 28 | ByteString(const signed char *data, size_t length) |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 29 | : data_(data, data + length), begin_(data_.begin()) {} |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 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 ? |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 36 | 1 : 0))), |
| 37 | begin_(data_.begin()) {} |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 38 | |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 39 | ByteString &operator=(const ByteString &b); |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 40 | |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 41 | unsigned char *GetData(); |
| 42 | const unsigned char *GetConstData() const; |
| 43 | size_t GetLength() const; |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 44 | |
| 45 | // Returns a ByteString containing |length| bytes from the ByteString |
| 46 | // starting at |offset|. This function truncates the returned string |
| 47 | // if part (or all) of this requested data lies outside the bounds of |
| 48 | // this ByteString. |
| 49 | ByteString GetSubstring(size_t offset, size_t length) const; |
| 50 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 51 | // Inserts a uint32 into a ByteString in cpu-order |
| 52 | static ByteString CreateFromCPUUInt32(uint32 val); |
| 53 | // Inserts a uint32 into a ByteString in network-order |
| 54 | static ByteString CreateFromNetUInt32(uint32 val); |
| 55 | |
Ben Chan | dc80b32 | 2013-04-08 13:46:13 -0700 | [diff] [blame] | 56 | // Creates a ByteString from a string of hexadecimal digits where |
| 57 | // a pair of hexadecimal digits corresponds to a byte. |
| 58 | // Returns a default-constructed ByteString if |hex_string| is empty |
| 59 | // or not a valid string of hexadecimal digits representing a sequence |
| 60 | // of bytes. |
| 61 | static ByteString CreateFromHexString(const std::string &hex_string); |
| 62 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 63 | // Converts to a uint32 from a host-order value stored in the ByteString |
| 64 | // Returns true on success |
| 65 | bool ConvertToCPUUInt32(uint32 *val) const; |
| 66 | // Converts to a uint32 from a network-order value stored in the ByteString |
| 67 | // Returns true on success |
| 68 | bool ConvertToNetUInt32(uint32 *val) const; |
| 69 | |
Ben Chan | dc80b32 | 2013-04-08 13:46:13 -0700 | [diff] [blame] | 70 | // Converts the string of bytes stored in the ByteString from network order |
| 71 | // to host order in 32-bit chunks. Returns true on success or false if the |
| 72 | // length of ByteString is not a multiple of 4. |
| 73 | bool ConvertFromNetToCPUUInt32Array(); |
| 74 | |
| 75 | // Converts the string of bytes stored in the ByteString from host order |
| 76 | // to network order in 32-bit chunks. Returns true on success or false if the |
| 77 | // length of ByteString is not a multiple of 4. |
| 78 | bool ConvertFromCPUToNetUInt32Array(); |
| 79 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 80 | bool IsEmpty() const { return GetLength() == 0; } |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 81 | |
| 82 | // Returns true if every element of |this| is zero, false otherwise. |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 83 | bool IsZero() const; |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 84 | |
| 85 | // Perform an AND operation between each element of |this| with the |
| 86 | // corresponding byte of |b|. Returns true if both |this| and |b| |
| 87 | // are the same length, and as such the operation succeeds; false |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame] | 88 | // if they are not. The result of the operation is stored in |this|. |
| 89 | bool BitwiseAnd(const ByteString &b); |
| 90 | |
| 91 | // Perform an OR operation between each element of |this| with the |
| 92 | // corresponding byte of |b|. Returns true if both |this| and |b| |
| 93 | // are the same length, and as such the operation succeeds; false |
| 94 | // if they are not. The result of the operation is stored in |this|. |
| 95 | bool BitwiseOr(const ByteString &b); |
| 96 | |
| 97 | // Perform an inversion operation on each of the bits this string. |
| 98 | void BitwiseInvert(); |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 99 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 100 | bool Equals(const ByteString &b) const; |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 101 | void Append(const ByteString &b); |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 102 | void Clear(); |
| 103 | void Resize(int size); |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 104 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 105 | std::string HexEncode() const; |
| 106 | |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 107 | // Discards |offset| bytes from the beginning of the ByteString (but does |
| 108 | // not cause a copy). |
| 109 | void RemovePrefix(size_t offset); |
| 110 | |
Wade Guthrie | b9c3feb | 2013-04-25 16:31:19 -0700 | [diff] [blame] | 111 | static bool IsLessThan(const ByteString &lhs, const ByteString &rhs); |
| 112 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 113 | private: |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 114 | typedef std::vector<unsigned char> Vector; |
Ben Chan | dc80b32 | 2013-04-08 13:46:13 -0700 | [diff] [blame] | 115 | |
| 116 | // Converts the string of bytes stored in the ByteString by treating it as |
| 117 | // an array of unsigned integer of type T and applying |converter| on each |
| 118 | // unsigned value of type T. Return true on success or false if the length |
| 119 | // ByteString is not a multiple of sizeof(T). |
| 120 | template <typename T> bool ConvertByteOrderAsUIntArray(T (*converter)(T)); |
| 121 | |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 122 | Vector data_; |
| 123 | |
| 124 | // Permit chopping-off the front part of the data without requiring a copy. |
| 125 | Vector::iterator begin_; |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 126 | // NO DISALLOW_COPY_AND_ASSIGN -- we assign ByteStrings in STL hashes |
| 127 | }; |
| 128 | |
| 129 | } // namespace shill |
| 130 | |
| 131 | |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 132 | #endif // SHILL_BYTE_STRING_H_ |