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 #ifndef SRC_PARSER_UTILS_H_
9 #define SRC_PARSER_UTILS_H_
10
11 #include <cassert>
12 #include <cstdint>
13 #include <type_traits>
14
15 #include "webm/reader.h"
16 #include "webm/status.h"
17
18 namespace webm {
19
20 // Reads a single byte from the reader, and returns the status of the read. If
21 // the status is not Status::kOkCompleted, then no data was read.
22 Status ReadByte(Reader* reader, std::uint8_t* byte);
23
24 // Accumulates bytes from the reader into the integer. The integer will be
25 // extracted as a big-endian integer and stored with the native
26 // host-endianness. num_bytes_remaining is the number of bytes to
27 template <typename T>
AccumulateIntegerBytes(int num_to_read,Reader * reader,T * integer,std::uint64_t * num_actually_read)28 Status AccumulateIntegerBytes(int num_to_read, Reader* reader, T* integer,
29 std::uint64_t* num_actually_read) {
30 static_assert(std::is_integral<T>::value || std::is_enum<T>::value,
31 "T must be an integer or enum type");
32 // Use unsigned integers for bitwise arithmetic because it's well-defined (as
33 // opposed to signed integers, where left shifting a negative integer is
34 // undefined, for example).
35 using UnsignedT = typename std::make_unsigned<T>::type;
36
37 assert(reader != nullptr);
38 assert(integer != nullptr);
39 assert(num_actually_read != nullptr);
40 assert(num_to_read >= 0);
41 assert(static_cast<std::size_t>(num_to_read) <= sizeof(T));
42
43 *num_actually_read = 0;
44
45 if (num_to_read < 0 || static_cast<std::size_t>(num_to_read) > sizeof(T)) {
46 return Status(Status::kInvalidElementSize);
47 }
48
49 for (; num_to_read > 0; --num_to_read) {
50 std::uint8_t byte;
51 const Status status = ReadByte(reader, &byte);
52 if (!status.completed_ok()) {
53 return status;
54 }
55 ++*num_actually_read;
56 *integer = static_cast<T>((static_cast<UnsignedT>(*integer) << 8) | byte);
57 }
58
59 return Status(Status::kOkCompleted);
60 }
61
62 } // namespace webm
63
64 #endif // SRC_PARSER_UTILS_H_
65