1 // 2 // Copyright (C) 2012 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef SHILL_NET_BYTE_STRING_H_ 18 #define SHILL_NET_BYTE_STRING_H_ 19 20 #include <string> 21 #include <vector> 22 23 #include <base/macros.h> 24 25 #include "shill/net/shill_export.h" 26 27 namespace shill { 28 29 // Provides a holder of a string of bytes 30 class SHILL_EXPORT ByteString { 31 public: ByteString()32 ByteString() {} 33 ByteString(const ByteString& b); 34 ByteString(const std::vector<unsigned char> & data)35 explicit ByteString(const std::vector<unsigned char>& data) : data_(data) {} 36 ByteString(size_t length)37 explicit ByteString(size_t length) : data_(length) {} 38 ByteString(const unsigned char * data,size_t length)39 ByteString(const unsigned char* data, size_t length) 40 : data_(data, data + length) {} 41 ByteString(const char * data,size_t length)42 ByteString(const char* data, size_t length) 43 : data_(data, data + length) {} 44 ByteString(const signed char * data,size_t length)45 ByteString(const signed char* data, size_t length) 46 : data_(data, data + length) {} 47 ByteString(const std::string & data,bool copy_terminator)48 ByteString(const std::string& data, bool copy_terminator) 49 : data_(reinterpret_cast<const unsigned char*>(data.c_str()), 50 reinterpret_cast<const unsigned char*>(data.c_str() + 51 data.length() + 52 (copy_terminator ? 53 1 : 0))) {} 54 55 ByteString& operator=(const ByteString& b); 56 57 unsigned char* GetData(); 58 const unsigned char* GetConstData() const; 59 size_t GetLength() const; 60 61 // Returns a ByteString containing |length| bytes from the ByteString 62 // starting at |offset|. This function truncates the returned string 63 // if part (or all) of this requested data lies outside the bounds of 64 // this ByteString. 65 ByteString GetSubstring(size_t offset, size_t length) const; 66 67 // Inserts a uint32_t into a ByteString in cpu-order 68 static ByteString CreateFromCPUUInt32(uint32_t val); 69 // Inserts a uint32_t into a ByteString in network-order 70 static ByteString CreateFromNetUInt32(uint32_t val); 71 72 // Creates a ByteString from a string of hexadecimal digits where 73 // a pair of hexadecimal digits corresponds to a byte. 74 // Returns a default-constructed ByteString if |hex_string| is empty 75 // or not a valid string of hexadecimal digits representing a sequence 76 // of bytes. 77 static ByteString CreateFromHexString(const std::string& hex_string); 78 79 // Converts to a uint32_t from a host-order value stored in the ByteString 80 // Returns true on success 81 bool ConvertToCPUUInt32(uint32_t* val) const; 82 // Converts to a uint32_t from a network-order value stored in the ByteString 83 // Returns true on success 84 bool ConvertToNetUInt32(uint32_t* val) const; 85 86 // Converts the string of bytes stored in the ByteString from network order 87 // to host order in 32-bit chunks. Returns true on success or false if the 88 // length of ByteString is not a multiple of 4. 89 bool ConvertFromNetToCPUUInt32Array(); 90 91 // Converts the string of bytes stored in the ByteString from host order 92 // to network order in 32-bit chunks. Returns true on success or false if the 93 // length of ByteString is not a multiple of 4. 94 bool ConvertFromCPUToNetUInt32Array(); 95 IsEmpty()96 bool IsEmpty() const { return GetLength() == 0; } 97 98 // Returns true if every element of |this| is zero, false otherwise. 99 bool IsZero() const; 100 101 // Perform an AND operation between each element of |this| with the 102 // corresponding byte of |b|. Returns true if both |this| and |b| 103 // are the same length, and as such the operation succeeds; false 104 // if they are not. The result of the operation is stored in |this|. 105 bool BitwiseAnd(const ByteString& b); 106 107 // Perform an OR operation between each element of |this| with the 108 // corresponding byte of |b|. Returns true if both |this| and |b| 109 // are the same length, and as such the operation succeeds; false 110 // if they are not. The result of the operation is stored in |this|. 111 bool BitwiseOr(const ByteString& b); 112 113 // Perform an inversion operation on each of the bits this string. 114 void BitwiseInvert(); 115 116 bool Equals(const ByteString& b) const; 117 void Append(const ByteString& b); 118 void Clear(); 119 void Resize(int size); 120 121 std::string HexEncode() const; 122 123 // Ensures that |size| bytes are available in the payload, then copies 124 // these bytes to |output|. Returns false if |this| does not contain enough 125 // data. 126 bool CopyData(size_t size, void* output) const; 127 128 static bool IsLessThan(const ByteString& lhs, const ByteString& rhs); 129 130 private: 131 typedef std::vector<unsigned char> Vector; 132 133 // Converts the string of bytes stored in the ByteString by treating it as 134 // an array of unsigned integer of type T and applying |converter| on each 135 // unsigned value of type T. Return true on success or false if the length 136 // ByteString is not a multiple of sizeof(T). 137 template <typename T> bool ConvertByteOrderAsUIntArray(T (*converter)(T)); 138 139 Vector data_; 140 141 // NO DISALLOW_COPY_AND_ASSIGN -- we assign ByteStrings in STL hashes 142 }; 143 144 } // namespace shill 145 146 147 #endif // SHILL_NET_BYTE_STRING_H_ 148