databuffer.h 3.26 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef databuffer_h__
#define databuffer_h__

10 11 12 13
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
14

15 16
namespace nss_test {

17 18 19
class DataBuffer {
 public:
  DataBuffer() : data_(nullptr), len_(0) {}
20 21
  DataBuffer(const uint8_t* d, size_t l) : data_(nullptr), len_(0) {
    Assign(d, l);
22
  }
23
  DataBuffer(const DataBuffer& other) : data_(nullptr), len_(0) {
24
    Assign(other);
25
  }
26
  explicit DataBuffer(size_t l) : data_(nullptr), len_(0) { Allocate(l); }
27 28
  ~DataBuffer() { delete[] data_; }

29 30
  DataBuffer& operator=(const DataBuffer& other) {
    if (&other != this) {
31
      Assign(other);
32 33
    }
    return *this;
34 35
  }

36
  void Allocate(size_t l) {
37
    delete[] data_;
38
    data_ = new uint8_t[l ? l : 1]();  // Don't depend on new [0].
39
    len_ = l;
40 41
  }

42
  void Truncate(size_t l) { len_ = (std::min)(len_, l); }
43

44
  void Assign(const DataBuffer& other) { Assign(other.data(), other.len()); }
45

46
  void Assign(const uint8_t* d, size_t l);
47 48

  // Write will do a new allocation and expand the size of the buffer if needed.
49
  // Returns the offset of the end of the write.
50
  size_t Write(size_t index, const uint8_t* val, size_t count);
51 52
  size_t Write(size_t index, const DataBuffer& buf) {
    return Write(index, buf.data(), buf.len());
53 54 55
  }

  // Write an integer, also performing host-to-network order conversion.
56
  // Returns the offset of the end of the write.
57
  size_t Write(size_t index, uint32_t val, size_t count);
EKR's avatar
EKR committed
58

59 60 61 62 63 64
  // Starting at |index|, remove |remove| bytes and replace them with the
  // contents of |buf|.
  void Splice(const DataBuffer& buf, size_t index, size_t remove = 0) {
    Splice(buf.data(), buf.len(), index, remove);
  }

65
  void Splice(const uint8_t* ins, size_t ins_len, size_t index,
66
              size_t remove = 0);
67 68
  void Append(const DataBuffer& buf) { Splice(buf, len_); }

69 70 71
  bool Read(size_t index, size_t count, uint64_t* val) const;
  bool Read(size_t index, size_t count, uint32_t* val) const;

72
  const uint8_t* data() const { return data_; }
73
  uint8_t* data() { return data_; }
74
  size_t len() const { return len_; }
75
  bool empty() const { return len_ == 0; }
76

77 78 79
  static void SetLogLimit(size_t limit);
  friend std::ostream& operator<<(std::ostream& stream, const DataBuffer& buf);

80
 private:
81
  static size_t logging_limit;
82
  uint8_t* data_;
83 84 85
  size_t len_;
};

86 87 88
inline std::ostream& operator<<(std::ostream& stream, const DataBuffer& buf) {
  stream << "[" << buf.len() << "] ";
  for (size_t i = 0; i < buf.len(); ++i) {
89
    if (i >= DataBuffer::logging_limit) {
90 91 92 93 94 95 96 97 98 99
      stream << "...";
      break;
    }
    stream << std::hex << std::setfill('0') << std::setw(2)
           << static_cast<unsigned>(buf.data()[i]);
  }
  stream << std::dec;
  return stream;
}

100 101
inline bool operator==(const DataBuffer& a, const DataBuffer& b) {
  return (a.empty() && b.empty()) ||
102
         (a.len() == b.len() && 0 == memcmp(a.data(), b.data(), a.len()));
103 104 105 106 107 108
}

inline bool operator!=(const DataBuffer& a, const DataBuffer& b) {
  return !(a == b);
}

109
}  // namespace nss_test
110

111
#endif