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 | |
| 9 | namespace shill { |
| 10 | |
| 11 | // static |
| 12 | ByteString ByteString::CreateFromCPUUInt32(uint32 val) { |
| 13 | return ByteString(reinterpret_cast<unsigned char *>(&val), sizeof(val)); |
| 14 | } |
| 15 | |
| 16 | // static |
| 17 | ByteString ByteString::CreateFromNetUInt32(uint32 val) { |
| 18 | return CreateFromCPUUInt32(htonl(val)); |
| 19 | } |
| 20 | |
| 21 | bool 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 | |
| 30 | bool ByteString::ConvertToNetUInt32(uint32 *val) const { |
| 31 | if (!ConvertToCPUUInt32(val)) { |
| 32 | return false; |
| 33 | } |
| 34 | *val = ntohl(*val); |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | bool 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 | |
| 48 | bool ByteString::Equals(const ByteString &b) const { |
| 49 | return data_ == b.data_; |
| 50 | } |
| 51 | |
Paul Stewart | dd7df79 | 2011-07-15 11:09:50 -0700 | [diff] [blame] | 52 | void ByteString::Append(const ByteString &b) { |
| 53 | data_.insert(data_.end(), b.data_.begin(), b.data_.end()); |
| 54 | } |
| 55 | |
Paul Stewart | 3ecfa2b | 2011-07-15 10:47:42 -0700 | [diff] [blame] | 56 | } // namespace shill |