/src/nss/cpputil/databuffer.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
5 | | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef databuffer_h__ |
8 | | #define databuffer_h__ |
9 | | |
10 | | #include <algorithm> |
11 | | #include <cstdint> |
12 | | #include <cstring> |
13 | | #include <iomanip> |
14 | | #include <iostream> |
15 | | |
16 | | namespace nss_test { |
17 | | |
18 | | class DataBuffer { |
19 | | public: |
20 | 0 | DataBuffer() : data_(nullptr), len_(0) {} |
21 | 0 | DataBuffer(const uint8_t* d, size_t l) : data_(nullptr), len_(0) { |
22 | 0 | Assign(d, l); |
23 | 0 | } |
24 | 0 | DataBuffer(const DataBuffer& other) : data_(nullptr), len_(0) { |
25 | 0 | Assign(other); |
26 | 0 | } |
27 | 0 | explicit DataBuffer(size_t l) : data_(nullptr), len_(0) { Allocate(l); } |
28 | 0 | ~DataBuffer() { delete[] data_; } |
29 | | |
30 | 0 | DataBuffer& operator=(const DataBuffer& other) { |
31 | 0 | if (&other != this) { |
32 | 0 | Assign(other); |
33 | 0 | } |
34 | 0 | return *this; |
35 | 0 | } |
36 | 0 | DataBuffer& operator=(DataBuffer&& other) { |
37 | 0 | if (this != &other) { |
38 | 0 | delete[] data_; |
39 | 0 | data_ = other.data_; |
40 | 0 | len_ = other.len_; |
41 | 0 | other.data_ = nullptr; |
42 | 0 | other.len_ = 0; |
43 | 0 | } |
44 | 0 | return *this; |
45 | 0 | } |
46 | | |
47 | 0 | void Allocate(size_t l) { |
48 | 0 | delete[] data_; |
49 | 0 | data_ = new uint8_t[l ? l : 1](); // Don't depend on new [0]. |
50 | 0 | len_ = l; |
51 | 0 | } |
52 | | |
53 | 0 | void Truncate(size_t l) { len_ = (std::min)(len_, l); } |
54 | | |
55 | 0 | void Assign(const DataBuffer& other) { Assign(other.data(), other.len()); } |
56 | | |
57 | | void Assign(const uint8_t* d, size_t l); |
58 | | |
59 | | // Write will do a new allocation and expand the size of the buffer if needed. |
60 | | // Returns the offset of the end of the write. |
61 | | size_t Write(size_t index, const uint8_t* val, size_t count); |
62 | 0 | size_t Write(size_t index, const DataBuffer& buf) { |
63 | 0 | return Write(index, buf.data(), buf.len()); |
64 | 0 | } |
65 | | |
66 | | // Write an integer, also performing host-to-network order conversion. |
67 | | // Returns the offset of the end of the write. |
68 | | size_t Write(size_t index, uint32_t val, size_t count); |
69 | | |
70 | | // Starting at |index|, remove |remove| bytes and replace them with the |
71 | | // contents of |buf|. |
72 | 0 | void Splice(const DataBuffer& buf, size_t index, size_t remove = 0) { |
73 | 0 | Splice(buf.data(), buf.len(), index, remove); |
74 | 0 | } |
75 | | |
76 | | void Splice(const uint8_t* ins, size_t ins_len, size_t index, |
77 | | size_t remove = 0); |
78 | 0 | void Append(const DataBuffer& buf) { Splice(buf, len_); } |
79 | | |
80 | | bool Read(size_t index, size_t count, uint64_t* val) const; |
81 | | bool Read(size_t index, size_t count, uint32_t* val) const; |
82 | | |
83 | 0 | const uint8_t* data() const { return data_; } |
84 | 0 | uint8_t* data() { return data_; } |
85 | 0 | size_t len() const { return len_; } |
86 | 0 | bool empty() const { return len_ == 0; } |
87 | | |
88 | | static void SetLogLimit(size_t limit); |
89 | | friend std::ostream& operator<<(std::ostream& stream, const DataBuffer& buf); |
90 | | |
91 | | private: |
92 | | static size_t logging_limit; |
93 | | uint8_t* data_; |
94 | | size_t len_; |
95 | | }; |
96 | | |
97 | 0 | inline std::ostream& operator<<(std::ostream& stream, const DataBuffer& buf) { |
98 | 0 | stream << "[" << buf.len() << "] "; |
99 | 0 | for (size_t i = 0; i < buf.len(); ++i) { |
100 | 0 | if (i >= DataBuffer::logging_limit) { |
101 | 0 | stream << "..."; |
102 | 0 | break; |
103 | 0 | } |
104 | 0 | stream << std::hex << std::setfill('0') << std::setw(2) |
105 | 0 | << static_cast<unsigned>(buf.data()[i]); |
106 | 0 | } |
107 | 0 | stream << std::dec; |
108 | 0 | return stream; |
109 | 0 | } |
110 | | |
111 | 0 | inline bool operator==(const DataBuffer& a, const DataBuffer& b) { |
112 | 0 | return (a.empty() && b.empty()) || |
113 | 0 | (a.len() == b.len() && 0 == memcmp(a.data(), b.data(), a.len())); |
114 | 0 | } |
115 | | |
116 | 0 | inline bool operator!=(const DataBuffer& a, const DataBuffer& b) { |
117 | 0 | return !(a == b); |
118 | 0 | } |
119 | | |
120 | | } // namespace nss_test |
121 | | |
122 | | #endif |