summaryrefslogtreecommitdiffstats
path: root/net/der/input.cc
blob: 7c875a44e70cb0ab4c8ca0b91a9ff8832a25d117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/logging.h"
#include "net/der/input.h"

namespace net {

namespace der {

Input::Input() : data_(nullptr), len_(0) {
}
Input::Input(const uint8_t* data, size_t len) : data_(data), len_(len) {
}
Input::Input(const std::string& s)
    : data_(reinterpret_cast<const uint8_t*>(s.data())), len_(s.size()) {
}

ByteReader::ByteReader(const Input& in)
    : data_(in.UnsafeData()), len_(in.Length()) {
}

bool ByteReader::ReadByte(uint8_t* byte_p) {
  if (!HasMore())
    return false;
  *byte_p = *data_;
  Advance(1);
  return true;
}

bool ByteReader::ReadBytes(size_t len, Input* out) {
  if (len > len_)
    return false;
  *out = Input(data_, len);
  Advance(len);
  return true;
}

// Returns whether there is any more data to be read.
bool ByteReader::HasMore() {
  return len_ > 0;
}

Mark ByteReader::NewMark() {
  return Mark(data_);
}

bool ByteReader::AdvanceToMark(Mark mark) {
  if (mark.ptr_ < data_)
    return false;
  // mark.ptr_ >= data_, so no concern of integer underflow here.
  size_t advance_len = mark.ptr_ - data_;
  if (advance_len > len_)
    return false;
  Advance(advance_len);
  return true;
}

bool ByteReader::ReadToMark(Mark mark, Input* out) {
  if (mark.ptr_ < data_)
    return false;
  // mark.ptr_ >= data_, so no concern of integer underflow here.
  size_t len = mark.ptr_ - data_;
  return ReadBytes(len, out);
}

void ByteReader::Advance(size_t len) {
  CHECK_LE(len, len_);
  data_ += len;
  len_ -= len;
}

Mark Mark::NullMark() {
  return Mark();
}

bool Mark::IsEmpty() {
  return ptr_ == nullptr;
}

Mark::Mark(const uint8_t* ptr) : ptr_(ptr) {
}

Mark::Mark() : ptr_(nullptr) {
}

}  // namespace der

}  // namespace net