File: buffer_reader.cc

package info (click to toggle)
libwebm 1.0.0.32-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,132 kB
  • sloc: cpp: 32,181; sh: 508; python: 128; makefile: 32
file content (110 lines) | stat: -rw-r--r-- 2,904 bytes parent folder | download | duplicates (26)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) 2016 The WebM project authors. All Rights Reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree. An additional intellectual property rights grant can be found
// in the file PATENTS.  All contributing project authors may
// be found in the AUTHORS file in the root of the source tree.
#include "webm/buffer_reader.h"

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <utility>
#include <vector>

#include "webm/status.h"

namespace webm {

BufferReader::BufferReader(std::initializer_list<std::uint8_t> bytes)
    : data_(bytes) {}

BufferReader::BufferReader(const std::vector<std::uint8_t>& vector)
    : data_(vector) {}

BufferReader::BufferReader(std::vector<std::uint8_t>&& vector)
    : data_(std::move(vector)) {}

BufferReader::BufferReader(BufferReader&& other)
    : data_(std::move(other.data_)), pos_(other.pos_) {
  other.pos_ = 0;
}

BufferReader& BufferReader::operator=(BufferReader&& other) {
  if (this != &other) {
    data_ = std::move(other.data_);
    pos_ = other.pos_;
    other.pos_ = 0;
  }
  return *this;
}

BufferReader& BufferReader::operator=(
    std::initializer_list<std::uint8_t> bytes) {
  data_ = std::vector<std::uint8_t>(bytes);
  pos_ = 0;
  return *this;
}

Status BufferReader::Read(std::size_t num_to_read, std::uint8_t* buffer,
                          std::uint64_t* num_actually_read) {
  assert(num_to_read > 0);
  assert(buffer != nullptr);
  assert(num_actually_read != nullptr);

  *num_actually_read = 0;
  std::size_t expected = num_to_read;

  std::size_t num_remaining = data_.size() - pos_;
  if (num_remaining == 0) {
    return Status(Status::kEndOfFile);
  }

  if (num_to_read > num_remaining) {
    num_to_read = static_cast<std::size_t>(num_remaining);
  }

  std::copy_n(data_.data() + pos_, num_to_read, buffer);
  *num_actually_read = num_to_read;
  pos_ += num_to_read;

  if (*num_actually_read != expected) {
    return Status(Status::kOkPartial);
  }

  return Status(Status::kOkCompleted);
}

Status BufferReader::Skip(std::uint64_t num_to_skip,
                          std::uint64_t* num_actually_skipped) {
  assert(num_to_skip > 0);
  assert(num_actually_skipped != nullptr);

  *num_actually_skipped = 0;
  std::uint64_t expected = num_to_skip;

  std::size_t num_remaining = data_.size() - pos_;
  if (num_remaining == 0) {
    return Status(Status::kEndOfFile);
  }

  if (num_to_skip > num_remaining) {
    num_to_skip = static_cast<std::uint64_t>(num_remaining);
  }

  *num_actually_skipped = num_to_skip;
  pos_ += num_to_skip;

  if (*num_actually_skipped != expected) {
    return Status(Status::kOkPartial);
  }

  return Status(Status::kOkCompleted);
}

std::uint64_t BufferReader::Position() const { return pos_; }

}  // namespace webm