blob: bd370e79d47a11305291f403c179ea2569e6f202 [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
Paul Stewartdd7df792011-07-15 11:09:50 -07008#include <string>
Paul Stewart3ecfa2b2011-07-15 10:47:42 -07009#include <vector>
10
11#include <base/basictypes.h>
12
13namespace shill {
14
15// Provides a holder of a string of bytes
16class ByteString {
17 public:
Paul Stewart1d18e8c2011-07-15 11:00:31 -070018 ByteString() {}
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070019 ByteString(const ByteString &b) : data_(b.data_) {}
20 explicit ByteString(size_t length) : data_(length) {}
21 ByteString(const unsigned char *data, size_t length)
22 : data_(data, data + length) {}
Paul Stewartdd7df792011-07-15 11:09:50 -070023 ByteString(const std::string &data, bool copy_terminator)
24 : data_(reinterpret_cast<const unsigned char *>(data.c_str()),
25 reinterpret_cast<const unsigned char *>(data.c_str() +
26 data.length() +
27 (copy_terminator ?
28 1 : 0))) {}
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070029
30 ByteString &operator=(const ByteString &b) {
31 data_ = b.data_;
32 return *this;
33 }
34
Paul Stewart91a5aac2012-07-20 11:55:40 -070035 unsigned char *GetData() { return data_.data(); }
36 const unsigned char *GetConstData() const { return data_.data(); }
37 size_t GetLength() const { return data_.size(); }
38
39 // Returns a ByteString containing |length| bytes from the ByteString
40 // starting at |offset|. This function truncates the returned string
41 // if part (or all) of this requested data lies outside the bounds of
42 // this ByteString.
43 ByteString GetSubstring(size_t offset, size_t length) const;
44
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070045 // Inserts a uint32 into a ByteString in cpu-order
46 static ByteString CreateFromCPUUInt32(uint32 val);
47 // Inserts a uint32 into a ByteString in network-order
48 static ByteString CreateFromNetUInt32(uint32 val);
49
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070050 // Converts to a uint32 from a host-order value stored in the ByteString
51 // Returns true on success
52 bool ConvertToCPUUInt32(uint32 *val) const;
53 // Converts to a uint32 from a network-order value stored in the ByteString
54 // Returns true on success
55 bool ConvertToNetUInt32(uint32 *val) const;
56
Darin Petkove3e1cfa2011-08-11 13:41:17 -070057 bool IsEmpty() const { return GetLength() == 0; }
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070058
59 // Returns true if every element of |this| is zero, false otherwise.
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070060 bool IsZero() const;
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070061
62 // Perform an AND operation between each element of |this| with the
63 // corresponding byte of |b|. Returns true if both |this| and |b|
64 // are the same length, and as such the operation succeeds; false
Paul Stewartfe1c0e12012-04-30 19:57:04 -070065 // if they are not. The result of the operation is stored in |this|.
66 bool BitwiseAnd(const ByteString &b);
67
68 // Perform an OR operation between each element of |this| with the
69 // corresponding byte of |b|. Returns true if both |this| and |b|
70 // are the same length, and as such the operation succeeds; false
71 // if they are not. The result of the operation is stored in |this|.
72 bool BitwiseOr(const ByteString &b);
73
74 // Perform an inversion operation on each of the bits this string.
75 void BitwiseInvert();
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070076
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070077 bool Equals(const ByteString &b) const;
Paul Stewartdd7df792011-07-15 11:09:50 -070078 void Append(const ByteString &b);
Paul Stewartf65320c2011-10-13 14:34:52 -070079 void Clear() { data_.clear(); }
Paul Stewartdd7df792011-07-15 11:09:50 -070080 void Resize(int size) {
81 data_.resize(size, 0);
82 }
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070083
Darin Petkove3e1cfa2011-08-11 13:41:17 -070084 std::string HexEncode() const;
85
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070086 private:
87 std::vector<unsigned char> data_;
88 // NO DISALLOW_COPY_AND_ASSIGN -- we assign ByteStrings in STL hashes
89};
90
91} // namespace shill
92
93
94#endif // SHILL_BYTE_STRING_