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_DATE_PARSER_H_
9 #define SRC_DATE_PARSER_H_
10 
11 #include <cassert>
12 #include <cstdint>
13 
14 #include "src/element_parser.h"
15 #include "webm/callback.h"
16 #include "webm/element.h"
17 #include "webm/reader.h"
18 #include "webm/status.h"
19 
20 namespace webm {
21 
22 // Parses an EBML date from a byte stream. EBML dates are signed integer values
23 // that represent the offset, in nanoseconds, from 2001-01-01T00:00:00.00 UTC.
24 class DateParser : public ElementParser {
25  public:
26   // Constructs a new parser which will use the given default_value as the
27   // value for the element if its size is zero. Defaults to the value zero (as
28   // the EBML spec indicates).
29   explicit DateParser(std::int64_t default_value = 0);
30 
31   DateParser(DateParser&&) = default;
32   DateParser& operator=(DateParser&&) = default;
33 
34   DateParser(const DateParser&) = delete;
35   DateParser& operator=(const DateParser&) = delete;
36 
37   Status Init(const ElementMetadata& metadata, std::uint64_t max_size) override;
38 
39   Status Feed(Callback* callback, Reader* reader,
40               std::uint64_t* num_bytes_read) override;
41 
42   // Gets the parsed date. This must not be called until the parse had been
43   // successfully completed.
value()44   std::int64_t value() const {
45     assert(num_bytes_remaining_ == 0);
46     return value_;
47   }
48 
49   // Gets the parsed date. This must not be called until the parse had been
50   // successfully completed.
mutable_value()51   std::int64_t* mutable_value() {
52     assert(num_bytes_remaining_ == 0);
53     return &value_;
54   }
55 
56  private:
57   std::int64_t value_;
58   std::int64_t default_value_;
59   int num_bytes_remaining_ = -1;
60 };
61 
62 }  // namespace webm
63 
64 #endif  // SRC_DATE_PARSER_H_
65