blob: 3c9db105ad33adcdb863d66296710c5c608d9a3f [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
9namespace shill {
10
11// static
12ByteString ByteString::CreateFromCPUUInt32(uint32 val) {
13 return ByteString(reinterpret_cast<unsigned char *>(&val), sizeof(val));
14}
15
16// static
17ByteString ByteString::CreateFromNetUInt32(uint32 val) {
18 return CreateFromCPUUInt32(htonl(val));
19}
20
21bool ByteString::ConvertToCPUUInt32(uint32 *val) const {
22 if (val == NULL || data_.size() != sizeof(*val)) {
23 return false;
24 }
25 memcpy(val, GetConstData(), sizeof(*val));
26
27 return true;
28}
29
30bool ByteString::ConvertToNetUInt32(uint32 *val) const {
31 if (!ConvertToCPUUInt32(val)) {
32 return false;
33 }
34 *val = ntohl(*val);
35 return true;
36}
37
38bool ByteString::IsZero() const {
39 for (std::vector<unsigned char>::const_iterator i = data_.begin();
40 i != data_.end(); ++i) {
41 if (*i != 0) {
42 return false;
43 }
44 }
45 return true;
46}
47
48bool ByteString::Equals(const ByteString &b) const {
49 return data_ == b.data_;
50}
51
Paul Stewartdd7df792011-07-15 11:09:50 -070052void ByteString::Append(const ByteString &b) {
53 data_.insert(data_.end(), b.data_.begin(), b.data_.end());
54}
55
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070056} // namespace shill