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 "src/skip_parser.h"
9 
10 #include <cassert>
11 #include <cstdint>
12 
13 #include "webm/element.h"
14 #include "webm/reader.h"
15 #include "webm/status.h"
16 
17 namespace webm {
18 
Init(const ElementMetadata & metadata,std::uint64_t max_size)19 Status SkipParser::Init(const ElementMetadata& metadata,
20                         std::uint64_t max_size) {
21   assert(metadata.size == kUnknownElementSize || metadata.size <= max_size);
22 
23   if (metadata.size == kUnknownElementSize) {
24     return Status(Status::kInvalidElementSize);
25   }
26 
27   num_bytes_remaining_ = metadata.size;
28 
29   return Status(Status::kOkCompleted);
30 }
31 
Feed(Callback * callback,Reader * reader,std::uint64_t * num_bytes_read)32 Status SkipParser::Feed(Callback* callback, Reader* reader,
33                         std::uint64_t* num_bytes_read) {
34   assert(callback != nullptr);
35   assert(reader != nullptr);
36   assert(num_bytes_read != nullptr);
37 
38   *num_bytes_read = 0;
39 
40   if (num_bytes_remaining_ == 0) {
41     return Status(Status::kOkCompleted);
42   }
43 
44   Status status;
45   do {
46     std::uint64_t local_num_bytes_read = 0;
47     status = reader->Skip(num_bytes_remaining_, &local_num_bytes_read);
48     assert((status.completed_ok() &&
49             local_num_bytes_read == num_bytes_remaining_) ||
50            (status.ok() && local_num_bytes_read < num_bytes_remaining_) ||
51            (!status.ok() && local_num_bytes_read == 0));
52     *num_bytes_read += local_num_bytes_read;
53     num_bytes_remaining_ -= local_num_bytes_read;
54   } while (status.code == Status::kOkPartial);
55 
56   return status;
57 }
58 
59 }  // namespace webm
60