blob: bfba54e65e4e2412cd7d0037dbd5f2ad10e19db5 [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#ifndef SHILL_BYTE_STRING_
6#define SHILL_BYTE_STRING_
7
8#include <vector>
9
10#include <base/basictypes.h>
11
12namespace shill {
13
14// Provides a holder of a string of bytes
15class ByteString {
16 public:
17 ByteString(const ByteString &b) : data_(b.data_) {}
18 explicit ByteString(size_t length) : data_(length) {}
19 ByteString(const unsigned char *data, size_t length)
20 : data_(data, data + length) {}
21
22 ByteString &operator=(const ByteString &b) {
23 data_ = b.data_;
24 return *this;
25 }
26
27 // Inserts a uint32 into a ByteString in cpu-order
28 static ByteString CreateFromCPUUInt32(uint32 val);
29 // Inserts a uint32 into a ByteString in network-order
30 static ByteString CreateFromNetUInt32(uint32 val);
31
32 unsigned char *GetData() { return data_.data(); }
33 const unsigned char *GetConstData() const { return data_.data(); }
34 size_t GetLength() const { return data_.size(); }
35
36 // Converts to a uint32 from a host-order value stored in the ByteString
37 // Returns true on success
38 bool ConvertToCPUUInt32(uint32 *val) const;
39 // Converts to a uint32 from a network-order value stored in the ByteString
40 // Returns true on success
41 bool ConvertToNetUInt32(uint32 *val) const;
42
43 bool IsZero() const;
44 bool Equals(const ByteString &b) const;
45
46 private:
47 std::vector<unsigned char> data_;
48 // NO DISALLOW_COPY_AND_ASSIGN -- we assign ByteStrings in STL hashes
49};
50
51} // namespace shill
52
53
54#endif // SHILL_BYTE_STRING_