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 | #include "shill/byte_string.h" |
| 6 | |
| 7 | #include <netinet/in.h> |
| 8 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 9 | #include <base/string_number_conversions.h> |
| 10 | |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 11 | using std::distance; |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 12 | using std::string; |
| 13 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 14 | namespace shill { |
| 15 | |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 16 | ByteString::ByteString(const ByteString &b) { |
| 17 | data_.assign(Vector::const_iterator(b.begin_), b.data_.end()); |
| 18 | begin_ = data_.begin(); |
| 19 | } |
| 20 | |
| 21 | ByteString &ByteString::operator=(const ByteString &b) { |
| 22 | data_.assign(Vector::const_iterator(b.begin_), b.data_.end()); |
| 23 | begin_ = data_.begin(); |
| 24 | return *this; |
| 25 | } |
| 26 | |
| 27 | unsigned char *ByteString::GetData() { |
| 28 | return (GetLength() == 0) ? NULL : &*begin_; |
| 29 | } |
| 30 | |
| 31 | const unsigned char *ByteString::GetConstData() const { |
| 32 | return (GetLength() == 0) ? NULL : &*begin_; |
| 33 | } |
| 34 | |
| 35 | size_t ByteString::GetLength() const { |
| 36 | return distance(Vector::const_iterator(begin_), data_.end()); |
| 37 | } |
| 38 | |
Paul Stewart | 91a5aac | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 39 | ByteString ByteString::GetSubstring(size_t offset, size_t length) const { |
| 40 | if (offset > GetLength()) { |
| 41 | offset = GetLength(); |
| 42 | } |
| 43 | if (length > GetLength() - offset) { |
| 44 | length = GetLength() - offset; |
| 45 | } |
| 46 | return ByteString(GetConstData() + offset, length); |
| 47 | } |
| 48 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 49 | // static |
| 50 | ByteString ByteString::CreateFromCPUUInt32(uint32 val) { |
| 51 | return ByteString(reinterpret_cast<unsigned char *>(&val), sizeof(val)); |
| 52 | } |
| 53 | |
| 54 | // static |
| 55 | ByteString ByteString::CreateFromNetUInt32(uint32 val) { |
| 56 | return CreateFromCPUUInt32(htonl(val)); |
| 57 | } |
| 58 | |
| 59 | bool ByteString::ConvertToCPUUInt32(uint32 *val) const { |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 60 | if (val == NULL || GetLength() != sizeof(*val)) { |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | memcpy(val, GetConstData(), sizeof(*val)); |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | bool ByteString::ConvertToNetUInt32(uint32 *val) const { |
| 69 | if (!ConvertToCPUUInt32(val)) { |
| 70 | return false; |
| 71 | } |
| 72 | *val = ntohl(*val); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | bool ByteString::IsZero() const { |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 77 | for (Vector::const_iterator i = begin_; i != data_.end(); ++i) { |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 78 | if (*i != 0) { |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame] | 85 | bool ByteString::BitwiseAnd(const ByteString &b) { |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 86 | if (GetLength() != b.GetLength()) { |
| 87 | return false; |
| 88 | } |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 89 | Vector::iterator lhs(begin_); |
| 90 | Vector::const_iterator rhs(b.begin_); |
| 91 | while (lhs != data_.end()) { |
| 92 | *lhs++ &= *rhs++; |
Paul Stewart | f7bf9bf | 2012-04-17 17:30:14 -0700 | [diff] [blame] | 93 | } |
| 94 | return true; |
| 95 | } |
| 96 | |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame] | 97 | bool ByteString::BitwiseOr(const ByteString &b) { |
| 98 | if (GetLength() != b.GetLength()) { |
| 99 | return false; |
| 100 | } |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 101 | Vector::iterator lhs(begin_); |
| 102 | Vector::const_iterator rhs(b.begin_); |
| 103 | while (lhs != data_.end()) { |
| 104 | *lhs++ |= *rhs++; |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame] | 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | void ByteString::BitwiseInvert() { |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 110 | for (Vector::iterator i = begin_; i != data_.end(); ++i) { |
| 111 | *i = ~*i; |
Paul Stewart | fe1c0e1 | 2012-04-30 19:57:04 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 115 | bool ByteString::Equals(const ByteString &b) const { |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 116 | if (GetLength() != b.GetLength()) { |
| 117 | return false; |
| 118 | } |
| 119 | Vector::const_iterator lhs(begin_); |
| 120 | Vector::const_iterator rhs(b.begin_); |
| 121 | while(lhs != data_.end()) { |
| 122 | if (*lhs++ != *rhs++) { |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | return true; |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 129 | void ByteString::Append(const ByteString &b) { |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 130 | // Save and reapply offset since |insert| may reallocate the memory and |
| 131 | // invalidate the iterator. |
| 132 | size_t offset = distance(data_.begin(), begin_); |
| 133 | data_.insert(data_.end(), Vector::const_iterator(b.begin_), b.data_.end()); |
| 134 | begin_ = data_.begin() + offset; |
| 135 | } |
| 136 | |
| 137 | void ByteString::Clear() { |
| 138 | data_.clear(); |
| 139 | begin_ = data_.begin(); |
| 140 | } |
| 141 | |
| 142 | void ByteString::Resize(int size) { |
| 143 | // Save and reapply offset since |resize| may reallocate the memory and |
| 144 | // invalidate the iterator. |
| 145 | size_t offset = distance(data_.begin(), begin_); |
| 146 | data_.resize(size + offset, 0); |
| 147 | begin_ = data_.begin() + offset; |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Darin Petkov | e3e1cfa | 2011-08-11 13:41:17 -0700 | [diff] [blame] | 150 | string ByteString::HexEncode() const { |
| 151 | return base::HexEncode(GetConstData(), GetLength()); |
| 152 | } |
| 153 | |
Wade Guthrie | c8f5892 | 2013-01-29 16:18:27 -0800 | [diff] [blame] | 154 | void ByteString::RemovePrefix(size_t offset) { |
| 155 | if (offset >= GetLength()) { |
| 156 | begin_ = data_.end(); |
| 157 | } else { |
| 158 | begin_ += offset; |
| 159 | } |
| 160 | } |
| 161 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 162 | } // namespace shill |