Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / net / der / input.cc
blob75d1c9c1847e37b383c23d53a08cf1d17f2948c1
1 // Copyright 2015 The Chromium 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.
5 #include <string.h>
7 #include "base/logging.h"
8 #include "net/der/input.h"
10 namespace net {
12 namespace der {
14 Input::Input() : data_(nullptr), len_(0) {
17 Input::Input(const uint8_t* data, size_t len) : data_(data), len_(len) {
20 bool Input::Equals(const Input& other) const {
21 if (len_ != other.len_)
22 return false;
23 return memcmp(data_, other.data_, len_) == 0;
26 std::string Input::AsString() const {
27 return std::string(reinterpret_cast<const char*>(data_), len_);
30 ByteReader::ByteReader(const Input& in)
31 : data_(in.UnsafeData()), len_(in.Length()) {
34 bool ByteReader::ReadByte(uint8_t* byte_p) {
35 if (!HasMore())
36 return false;
37 *byte_p = *data_;
38 Advance(1);
39 return true;
42 bool ByteReader::ReadBytes(size_t len, Input* out) {
43 if (len > len_)
44 return false;
45 *out = Input(data_, len);
46 Advance(len);
47 return true;
50 // Returns whether there is any more data to be read.
51 bool ByteReader::HasMore() {
52 return len_ > 0;
55 Mark ByteReader::NewMark() {
56 return Mark(data_);
59 bool ByteReader::AdvanceToMark(Mark mark) {
60 if (mark.ptr_ < data_)
61 return false;
62 // mark.ptr_ >= data_, so no concern of integer underflow here.
63 size_t advance_len = mark.ptr_ - data_;
64 if (advance_len > len_)
65 return false;
66 Advance(advance_len);
67 return true;
70 bool ByteReader::ReadToMark(Mark mark, Input* out) {
71 if (mark.ptr_ < data_)
72 return false;
73 // mark.ptr_ >= data_, so no concern of integer underflow here.
74 size_t len = mark.ptr_ - data_;
75 return ReadBytes(len, out);
78 void ByteReader::Advance(size_t len) {
79 CHECK_LE(len, len_);
80 data_ += len;
81 len_ -= len;
84 Mark Mark::NullMark() {
85 return Mark();
88 bool Mark::IsEmpty() {
89 return ptr_ == nullptr;
92 Mark::Mark(const uint8_t* ptr) : ptr_(ptr) {
95 Mark::Mark() : ptr_(nullptr) {
98 } // namespace der
100 } // namespace net