xref: /aosp_15_r20/external/libwebm/webm_parser/src/var_int_parser.cc (revision 103e46e4cd4b6efcf6001f23fa8665fb110abf8d)
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/var_int_parser.h"
9 
10 #include <cassert>
11 #include <cstdint>
12 #include <limits>
13 
14 #include "src/bit_utils.h"
15 #include "src/parser_utils.h"
16 #include "webm/reader.h"
17 #include "webm/status.h"
18 
19 namespace webm {
20 
21 // Spec references:
22 // http://matroska.org/technical/specs/index.html#EBML_ex
23 // https://github.com/Matroska-Org/ebml-specification/blob/master/specification.markdown#variable-size-integer
Feed(Callback * callback,Reader * reader,std::uint64_t * num_bytes_read)24 Status VarIntParser::Feed(Callback* callback, Reader* reader,
25                           std::uint64_t* num_bytes_read) {
26   assert(callback != nullptr);
27   assert(reader != nullptr);
28   assert(num_bytes_read != nullptr);
29   assert(num_bytes_remaining_ != 0);
30 
31   *num_bytes_read = 0;
32 
33   if (num_bytes_remaining_ == -1) {
34     std::uint8_t first_byte;
35     const Status status = ReadByte(reader, &first_byte);
36     if (!status.completed_ok()) {
37       return status;
38     }
39     ++*num_bytes_read;
40 
41     // The first byte must have a marker bit set to indicate how many octets are
42     // used.
43     if (first_byte == 0) {
44       return Status(Status::kInvalidElementValue);
45     }
46 
47     total_data_bytes_ = CountLeadingZeros(first_byte);
48     num_bytes_remaining_ = total_data_bytes_;
49 
50     value_ = first_byte;
51   }
52 
53   std::uint64_t local_num_bytes_read;
54   const Status status = AccumulateIntegerBytes(num_bytes_remaining_, reader,
55                                                &value_, &local_num_bytes_read);
56   *num_bytes_read += local_num_bytes_read;
57   num_bytes_remaining_ -= static_cast<int>(local_num_bytes_read);
58 
59   if (!status.completed_ok()) {
60     return status;
61   }
62 
63   // Clear the marker bit.
64   constexpr std::uint64_t all_bits = std::numeric_limits<std::uint64_t>::max();
65   const std::uint64_t data_bits = all_bits >> (57 - 7 * total_data_bytes_);
66   value_ &= data_bits;
67 
68   return Status(Status::kOkCompleted);
69 }
70 
71 }  // namespace webm
72