shill: Add ByteString class

This can be used to store lumps of bytes.  This will be used in
IP Addresses, which will be used in routes.

BUG=chromium-os:17277
TEST=New unittest

Change-Id: Id9a635d9811997e1a734f7fd1c3d24bf37cfd807
Reviewed-on: http://gerrit.chromium.org/gerrit/4179
Reviewed-by: Darin Petkov <petkov@chromium.org>
Tested-by: Paul Stewart <pstew@chromium.org>
diff --git a/byte_string.cc b/byte_string.cc
new file mode 100644
index 0000000..3a3412f
--- /dev/null
+++ b/byte_string.cc
@@ -0,0 +1,52 @@
+// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "shill/byte_string.h"
+
+#include <netinet/in.h>
+
+namespace shill {
+
+// static
+ByteString ByteString::CreateFromCPUUInt32(uint32 val) {
+  return ByteString(reinterpret_cast<unsigned char *>(&val), sizeof(val));
+}
+
+// static
+ByteString ByteString::CreateFromNetUInt32(uint32 val) {
+  return CreateFromCPUUInt32(htonl(val));
+}
+
+bool ByteString::ConvertToCPUUInt32(uint32 *val) const {
+  if (val == NULL || data_.size() != sizeof(*val)) {
+    return false;
+  }
+  memcpy(val, GetConstData(), sizeof(*val));
+
+  return true;
+}
+
+bool ByteString::ConvertToNetUInt32(uint32 *val) const {
+  if (!ConvertToCPUUInt32(val)) {
+    return false;
+  }
+  *val = ntohl(*val);
+  return true;
+}
+
+bool ByteString::IsZero() const {
+  for (std::vector<unsigned char>::const_iterator i = data_.begin();
+       i != data_.end(); ++i) {
+    if (*i != 0) {
+      return false;
+    }
+  }
+  return true;
+}
+
+bool ByteString::Equals(const ByteString &b) const {
+  return data_ == b.data_;
+}
+
+}  // namespace shill