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/id_parser.h"
9
10 #include <cassert>
11 #include <cstdint>
12
13 #include "src/bit_utils.h"
14 #include "src/parser_utils.h"
15 #include "webm/id.h"
16 #include "webm/reader.h"
17 #include "webm/status.h"
18
19 namespace webm {
20
Feed(Callback * callback,Reader * reader,std::uint64_t * num_bytes_read)21 Status IdParser::Feed(Callback* callback, Reader* reader,
22 std::uint64_t* num_bytes_read) {
23 assert(callback != nullptr);
24 assert(reader != nullptr);
25 assert(num_bytes_read != nullptr);
26 assert(num_bytes_remaining_ != 0);
27
28 *num_bytes_read = 0;
29
30 // Spec references:
31 // http://matroska.org/technical/specs/index.html#EBML_ex
32 // https://github.com/Matroska-Org/ebml-specification/blob/master/specification.markdown#variable-size-integer
33 // https://github.com/Matroska-Org/ebml-specification/blob/master/specification.markdown#element-id
34
35 // IDs are encoded like so (big-endian):
36 // 0b1xxx xxxx
37 // 0b01xx xxxx xxxx xxxx
38 // 0b001x xxxx xxxx xxxx xxxx xxxx
39 // 0b0001 xxxx xxxx xxxx xxxx xxxx xxxx xxxx
40
41 if (num_bytes_remaining_ == -1) {
42 std::uint8_t first_byte;
43 const Status status = ReadByte(reader, &first_byte);
44 if (!status.completed_ok()) {
45 return status;
46 }
47 ++*num_bytes_read;
48
49 // The marker bit is the first 1-bit. It indicates the length of the ID.
50 // If there is no marker bit in the first half-octet, this isn't a valid
51 // ID, since IDs can't be more than 4 octets in MKV/WebM.
52 if (!(first_byte & 0xf0)) {
53 return Status(Status::kInvalidElementId);
54 }
55
56 num_bytes_remaining_ = CountLeadingZeros(first_byte);
57
58 id_ = static_cast<Id>(first_byte);
59 }
60
61 std::uint64_t local_num_bytes_read;
62 const Status status = AccumulateIntegerBytes(num_bytes_remaining_, reader,
63 &id_, &local_num_bytes_read);
64 *num_bytes_read += local_num_bytes_read;
65 num_bytes_remaining_ -= static_cast<int>(local_num_bytes_read);
66
67 return status;
68 }
69
id() const70 Id IdParser::id() const {
71 assert(num_bytes_remaining_ == 0);
72 return id_;
73 }
74
75 } // namespace webm
76