blob: 73c83d2166368623dfac17b7eaa5da59acfd9f57 [file] [log] [blame]
Gary Morain41780232012-07-31 15:08:31 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart3ecfa2b2011-07-15 10:47:42 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gary Morain41780232012-07-31 15:08:31 -07005#ifndef SHILL_BYTE_STRING_H_
6#define SHILL_BYTE_STRING_H_
Paul Stewart3ecfa2b2011-07-15 10:47:42 -07007
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:
Wade Guthriec8f58922013-01-29 16:18:27 -080018 ByteString() : begin_(data_.begin()) {}
19 ByteString(const ByteString &b);
20 explicit ByteString(size_t length) : data_(length), begin_(data_.begin()) {}
Gary Morain41780232012-07-31 15:08:31 -070021
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070022 ByteString(const unsigned char *data, size_t length)
Wade Guthriec8f58922013-01-29 16:18:27 -080023 : data_(data, data + length), begin_(data_.begin()) {}
Gary Morain41780232012-07-31 15:08:31 -070024
25 ByteString(const char *data, size_t length)
Wade Guthriec8f58922013-01-29 16:18:27 -080026 : data_(data, data + length), begin_(data_.begin()) {}
Gary Morain41780232012-07-31 15:08:31 -070027
28 ByteString(const signed char *data, size_t length)
Wade Guthriec8f58922013-01-29 16:18:27 -080029 : data_(data, data + length), begin_(data_.begin()) {}
Gary Morain41780232012-07-31 15:08:31 -070030
Paul Stewartdd7df792011-07-15 11:09:50 -070031 ByteString(const std::string &data, bool copy_terminator)
32 : data_(reinterpret_cast<const unsigned char *>(data.c_str()),
33 reinterpret_cast<const unsigned char *>(data.c_str() +
34 data.length() +
35 (copy_terminator ?
Wade Guthriec8f58922013-01-29 16:18:27 -080036 1 : 0))),
37 begin_(data_.begin()) {}
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070038
Wade Guthriec8f58922013-01-29 16:18:27 -080039 ByteString &operator=(const ByteString &b);
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070040
Wade Guthriec8f58922013-01-29 16:18:27 -080041 unsigned char *GetData();
42 const unsigned char *GetConstData() const;
43 size_t GetLength() const;
Paul Stewart91a5aac2012-07-20 11:55:40 -070044
45 // Returns a ByteString containing |length| bytes from the ByteString
46 // starting at |offset|. This function truncates the returned string
47 // if part (or all) of this requested data lies outside the bounds of
48 // this ByteString.
49 ByteString GetSubstring(size_t offset, size_t length) const;
50
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070051 // Inserts a uint32 into a ByteString in cpu-order
52 static ByteString CreateFromCPUUInt32(uint32 val);
53 // Inserts a uint32 into a ByteString in network-order
54 static ByteString CreateFromNetUInt32(uint32 val);
55
Ben Chandc80b322013-04-08 13:46:13 -070056 // Creates a ByteString from a string of hexadecimal digits where
57 // a pair of hexadecimal digits corresponds to a byte.
58 // Returns a default-constructed ByteString if |hex_string| is empty
59 // or not a valid string of hexadecimal digits representing a sequence
60 // of bytes.
61 static ByteString CreateFromHexString(const std::string &hex_string);
62
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070063 // Converts to a uint32 from a host-order value stored in the ByteString
64 // Returns true on success
65 bool ConvertToCPUUInt32(uint32 *val) const;
66 // Converts to a uint32 from a network-order value stored in the ByteString
67 // Returns true on success
68 bool ConvertToNetUInt32(uint32 *val) const;
69
Ben Chandc80b322013-04-08 13:46:13 -070070 // Converts the string of bytes stored in the ByteString from network order
71 // to host order in 32-bit chunks. Returns true on success or false if the
72 // length of ByteString is not a multiple of 4.
73 bool ConvertFromNetToCPUUInt32Array();
74
75 // Converts the string of bytes stored in the ByteString from host order
76 // to network order in 32-bit chunks. Returns true on success or false if the
77 // length of ByteString is not a multiple of 4.
78 bool ConvertFromCPUToNetUInt32Array();
79
Darin Petkove3e1cfa2011-08-11 13:41:17 -070080 bool IsEmpty() const { return GetLength() == 0; }
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070081
82 // Returns true if every element of |this| is zero, false otherwise.
Paul Stewart3ecfa2b2011-07-15 10:47:42 -070083 bool IsZero() const;
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070084
85 // Perform an AND operation between each element of |this| with the
86 // corresponding byte of |b|. Returns true if both |this| and |b|
87 // are the same length, and as such the operation succeeds; false
Paul Stewartfe1c0e12012-04-30 19:57:04 -070088 // if they are not. The result of the operation is stored in |this|.
89 bool BitwiseAnd(const ByteString &b);
90
91 // Perform an OR operation between each element of |this| with the
92 // corresponding byte of |b|. Returns true if both |this| and |b|
93 // are the same length, and as such the operation succeeds; false
94 // if they are not. The result of the operation is stored in |this|.
95 bool BitwiseOr(const ByteString &b);
96
97 // Perform an inversion operation on each of the bits this string.
98 void BitwiseInvert();
Paul Stewartf7bf9bf2012-04-17 17:30:14 -070099
Paul Stewart3ecfa2b2011-07-15 10:47:42 -0700100 bool Equals(const ByteString &b) const;
Paul Stewartdd7df792011-07-15 11:09:50 -0700101 void Append(const ByteString &b);
Wade Guthriec8f58922013-01-29 16:18:27 -0800102 void Clear();
103 void Resize(int size);
Paul Stewart3ecfa2b2011-07-15 10:47:42 -0700104
Darin Petkove3e1cfa2011-08-11 13:41:17 -0700105 std::string HexEncode() const;
106
Wade Guthriec8f58922013-01-29 16:18:27 -0800107 // Discards |offset| bytes from the beginning of the ByteString (but does
108 // not cause a copy).
109 void RemovePrefix(size_t offset);
110
Wade Guthrieb9c3feb2013-04-25 16:31:19 -0700111 static bool IsLessThan(const ByteString &lhs, const ByteString &rhs);
112
Paul Stewart3ecfa2b2011-07-15 10:47:42 -0700113 private:
Wade Guthriec8f58922013-01-29 16:18:27 -0800114 typedef std::vector<unsigned char> Vector;
Ben Chandc80b322013-04-08 13:46:13 -0700115
116 // Converts the string of bytes stored in the ByteString by treating it as
117 // an array of unsigned integer of type T and applying |converter| on each
118 // unsigned value of type T. Return true on success or false if the length
119 // ByteString is not a multiple of sizeof(T).
120 template <typename T> bool ConvertByteOrderAsUIntArray(T (*converter)(T));
121
Wade Guthriec8f58922013-01-29 16:18:27 -0800122 Vector data_;
123
124 // Permit chopping-off the front part of the data without requiring a copy.
125 Vector::iterator begin_;
Paul Stewart3ecfa2b2011-07-15 10:47:42 -0700126 // NO DISALLOW_COPY_AND_ASSIGN -- we assign ByteStrings in STL hashes
127};
128
129} // namespace shill
130
131
Gary Morain41780232012-07-31 15:08:31 -0700132#endif // SHILL_BYTE_STRING_H_