blob: 459ede7a4d507a9d8d140de65e0d644721cad25b [file] [log] [blame]
Paul Stewart3ecfa2b2011-07-15 10:47:42 -07001// 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 Petkove3e1cfa2011-08-11 13:41:17 -07009#include <base/string_number_conversions.h>
10
Wade Guthriec8f58922013-01-29 16:18:27 -080011using std::distance;
Darin Petkove3e1cfa2011-08-11 13:41:17 -070012using std::string;
13
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070014namespace shill {
15
Wade Guthriec8f58922013-01-29 16:18:27 -080016ByteString::ByteString(const ByteString &b) {
17 data_.assign(Vector::const_iterator(b.begin_), b.data_.end());
18 begin_ = data_.begin();
19}
20
21ByteString &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
27unsigned char *ByteString::GetData() {
28 return (GetLength() == 0) ? NULL : &*begin_;
29}
30
31const unsigned char *ByteString::GetConstData() const {
32 return (GetLength() == 0) ? NULL : &*begin_;
33}
34
35size_t ByteString::GetLength() const {
36 return distance(Vector::const_iterator(begin_), data_.end());
37}
38
Paul Stewart91a5aac2012-07-20 11:55:40 -070039ByteString 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 Stewart3ecfa2b2011-07-15 10:47:42 -070049// static
50ByteString ByteString::CreateFromCPUUInt32(uint32 val) {
51 return ByteString(reinterpret_cast<unsigned char *>(&val), sizeof(val));
52}
53
54// static
55ByteString ByteString::CreateFromNetUInt32(uint32 val) {
56 return CreateFromCPUUInt32(htonl(val));
57}
58
59bool ByteString::ConvertToCPUUInt32(uint32 *val) const {
Wade Guthriec8f58922013-01-29 16:18:27 -080060 if (val == NULL || GetLength() != sizeof(*val)) {
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070061 return false;
62 }
63 memcpy(val, GetConstData(), sizeof(*val));
64
65 return true;
66}
67
68bool ByteString::ConvertToNetUInt32(uint32 *val) const {
69 if (!ConvertToCPUUInt32(val)) {
70 return false;
71 }
72 *val = ntohl(*val);
73 return true;
74}
75
76bool ByteString::IsZero() const {
Wade Guthriec8f58922013-01-29 16:18:27 -080077 for (Vector::const_iterator i = begin_; i != data_.end(); ++i) {
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070078 if (*i != 0) {
79 return false;
80 }
81 }
82 return true;
83}
84
Paul Stewartfe1c0e12012-04-30 19:57:04 -070085bool ByteString::BitwiseAnd(const ByteString &b) {
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070086 if (GetLength() != b.GetLength()) {
87 return false;
88 }
Wade Guthriec8f58922013-01-29 16:18:27 -080089 Vector::iterator lhs(begin_);
90 Vector::const_iterator rhs(b.begin_);
91 while (lhs != data_.end()) {
92 *lhs++ &= *rhs++;
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070093 }
94 return true;
95}
96
Paul Stewartfe1c0e12012-04-30 19:57:04 -070097bool ByteString::BitwiseOr(const ByteString &b) {
98 if (GetLength() != b.GetLength()) {
99 return false;
100 }
Wade Guthriec8f58922013-01-29 16:18:27 -0800101 Vector::iterator lhs(begin_);
102 Vector::const_iterator rhs(b.begin_);
103 while (lhs != data_.end()) {
104 *lhs++ |= *rhs++;
Paul Stewartfe1c0e12012-04-30 19:57:04 -0700105 }
106 return true;
107}
108
109void ByteString::BitwiseInvert() {
Wade Guthriec8f58922013-01-29 16:18:27 -0800110 for (Vector::iterator i = begin_; i != data_.end(); ++i) {
111 *i = ~*i;
Paul Stewartfe1c0e12012-04-30 19:57:04 -0700112 }
113}
114
Paul Stewart3ecfa2b2011-07-15 10:47:42 -0700115bool ByteString::Equals(const ByteString &b) const {
Wade Guthriec8f58922013-01-29 16:18:27 -0800116 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 Stewart3ecfa2b2011-07-15 10:47:42 -0700127}
128
Paul Stewartdd7df792011-07-15 11:09:50 -0700129void ByteString::Append(const ByteString &b) {
Wade Guthriec8f58922013-01-29 16:18:27 -0800130 // 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
137void ByteString::Clear() {
138 data_.clear();
139 begin_ = data_.begin();
140}
141
142void 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 Stewartdd7df792011-07-15 11:09:50 -0700148}
149
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700150string ByteString::HexEncode() const {
151 return base::HexEncode(GetConstData(), GetLength());
152}
153
Wade Guthriec8f58922013-01-29 16:18:27 -0800154void ByteString::RemovePrefix(size_t offset) {
155 if (offset >= GetLength()) {
156 begin_ = data_.end();
157 } else {
158 begin_ += offset;
159 }
160}
161
Paul Stewart3ecfa2b2011-07-15 10:47:42 -0700162} // namespace shill