• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #include "webm/buffer_reader.h"
9 
10 #include <algorithm>
11 #include <cassert>
12 #include <cstddef>
13 #include <cstdint>
14 #include <initializer_list>
15 #include <utility>
16 #include <vector>
17 
18 #include "webm/status.h"
19 
20 namespace webm {
21 
BufferReader(std::initializer_list<std::uint8_t> bytes)22 BufferReader::BufferReader(std::initializer_list<std::uint8_t> bytes)
23     : data_(bytes) {}
24 
BufferReader(const std::vector<std::uint8_t> & vector)25 BufferReader::BufferReader(const std::vector<std::uint8_t>& vector)
26     : data_(vector) {}
27 
BufferReader(std::vector<std::uint8_t> && vector)28 BufferReader::BufferReader(std::vector<std::uint8_t>&& vector)
29     : data_(std::move(vector)) {}
30 
BufferReader(BufferReader && other)31 BufferReader::BufferReader(BufferReader&& other)
32     : data_(std::move(other.data_)), pos_(other.pos_) {
33   other.pos_ = 0;
34 }
35 
operator =(BufferReader && other)36 BufferReader& BufferReader::operator=(BufferReader&& other) {
37   if (this != &other) {
38     data_ = std::move(other.data_);
39     pos_ = other.pos_;
40     other.pos_ = 0;
41   }
42   return *this;
43 }
44 
operator =(std::initializer_list<std::uint8_t> bytes)45 BufferReader& BufferReader::operator=(
46     std::initializer_list<std::uint8_t> bytes) {
47   data_ = std::vector<std::uint8_t>(bytes);
48   pos_ = 0;
49   return *this;
50 }
51 
Read(std::size_t num_to_read,std::uint8_t * buffer,std::uint64_t * num_actually_read)52 Status BufferReader::Read(std::size_t num_to_read, std::uint8_t* buffer,
53                           std::uint64_t* num_actually_read) {
54   assert(num_to_read > 0);
55   assert(buffer != nullptr);
56   assert(num_actually_read != nullptr);
57 
58   *num_actually_read = 0;
59   std::size_t expected = num_to_read;
60 
61   std::size_t num_remaining = data_.size() - pos_;
62   if (num_remaining == 0) {
63     return Status(Status::kEndOfFile);
64   }
65 
66   if (num_to_read > num_remaining) {
67     num_to_read = static_cast<std::size_t>(num_remaining);
68   }
69 
70   std::copy_n(data_.data() + pos_, num_to_read, buffer);
71   *num_actually_read = num_to_read;
72   pos_ += num_to_read;
73 
74   if (*num_actually_read != expected) {
75     return Status(Status::kOkPartial);
76   }
77 
78   return Status(Status::kOkCompleted);
79 }
80 
Skip(std::uint64_t num_to_skip,std::uint64_t * num_actually_skipped)81 Status BufferReader::Skip(std::uint64_t num_to_skip,
82                           std::uint64_t* num_actually_skipped) {
83   assert(num_to_skip > 0);
84   assert(num_actually_skipped != nullptr);
85 
86   *num_actually_skipped = 0;
87   std::uint64_t expected = num_to_skip;
88 
89   std::size_t num_remaining = data_.size() - pos_;
90   if (num_remaining == 0) {
91     return Status(Status::kEndOfFile);
92   }
93 
94   if (num_to_skip > num_remaining) {
95     num_to_skip = static_cast<std::uint64_t>(num_remaining);
96   }
97 
98   *num_actually_skipped = num_to_skip;
99   pos_ += num_to_skip;
100 
101   if (*num_actually_skipped != expected) {
102     return Status(Status::kOkPartial);
103   }
104 
105   return Status(Status::kOkCompleted);
106 }
107 
Position() const108 std::uint64_t BufferReader::Position() const { return pos_; }
109 
110 }  // namespace webm
111