xref: /aosp_15_r20/external/libwebm/webm_parser/src/size_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/size_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/element.h"
17 #include "webm/reader.h"
18 #include "webm/status.h"
19 
20 namespace webm {
21 
22 // Spec references:
23 // http://matroska.org/technical/specs/index.html#EBML_ex
24 // https://github.com/Matroska-Org/ebml-specification/blob/master/specification.markdown#element-data-size
Feed(Callback * callback,Reader * reader,std::uint64_t * num_bytes_read)25 Status SizeParser::Feed(Callback* callback, Reader* reader,
26                         std::uint64_t* num_bytes_read) {
27   assert(callback != nullptr);
28   assert(reader != nullptr);
29   assert(num_bytes_read != nullptr);
30 
31   // Within the EBML header, the size can be encoded with 1-4 octets. After
32   // the EBML header, the size can be encoded with 1-8 octets (though not more
33   // than EBMLMaxSizeLength).
34 
35   Status status = uint_parser_.Feed(callback, reader, num_bytes_read);
36 
37   if (status.code == Status::kInvalidElementValue) {
38     status.code = Status::kInvalidElementSize;
39   }
40 
41   return status;
42 }
43 
size() const44 std::uint64_t SizeParser::size() const {
45   // If all data bits are set, then it represents an unknown element size.
46   const std::uint64_t data_bits =
47       std::numeric_limits<std::uint64_t>::max() >>
48       (57 - 7 * (uint_parser_.encoded_length() - 1));
49   if (uint_parser_.value() == data_bits) {
50     return kUnknownElementSize;
51   }
52 
53   return uint_parser_.value();
54 }
55 
56 }  // namespace webm
57