• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/date_parser.h"
9 
10 #include <cassert>
11 #include <cstdint>
12 #include <limits>
13 
14 #include "src/parser_utils.h"
15 #include "webm/element.h"
16 #include "webm/reader.h"
17 #include "webm/status.h"
18 
19 namespace webm {
20 
21 // Spec reference:
22 // http://matroska.org/technical/specs/index.html#EBML_ex
23 // https://github.com/Matroska-Org/ebml-specification/blob/master/specification.markdown#ebml-element-types
DateParser(std::int64_t default_value)24 DateParser::DateParser(std::int64_t default_value)
25     : default_value_(default_value) {}
26 
Init(const ElementMetadata & metadata,std::uint64_t max_size)27 Status DateParser::Init(const ElementMetadata& metadata,
28                         std::uint64_t max_size) {
29   assert(metadata.size == kUnknownElementSize || metadata.size <= max_size);
30 
31   if (metadata.size != 0 && metadata.size != 8) {
32     return Status(Status::kInvalidElementSize);
33   }
34 
35   num_bytes_remaining_ = static_cast<int>(metadata.size);
36 
37   // The meaning of a 0-byte element is still being debated. EBML says the value
38   // is zero; Matroska says it's the default according to whatever the document
39   // spec says. Neither specifies what a 0-byte mandatory element means. I've
40   // asked about this on the Matroska mailing list. I'm going to assume a 0-byte
41   // mandatory element should be treated the same as a 0-byte optional element,
42   // meaning that they both get their default value (which may be some value
43   // other than zero). This applies to all non-master-elements (not just dates).
44   // This parser is an EBML-level parser, and so will default to a value of
45   // zero. The Matroska-level parser can reset this default value to something
46   // else after parsing (as needed).
47   // See:
48   // https://github.com/Matroska-Org/ebml-specification/pull/17
49   // http://lists.matroska.org/pipermail/matroska-devel/2015-October/004866.html
50   if (metadata.size == 0) {
51     value_ = default_value_;
52   } else {
53     value_ = 0;
54   }
55 
56   return Status(Status::kOkCompleted);
57 }
58 
Feed(Callback * callback,Reader * reader,std::uint64_t * num_bytes_read)59 Status DateParser::Feed(Callback* callback, Reader* reader,
60                         std::uint64_t* num_bytes_read) {
61   assert(callback != nullptr);
62   assert(reader != nullptr);
63   assert(num_bytes_read != nullptr);
64 
65   const Status status = AccumulateIntegerBytes(num_bytes_remaining_, reader,
66                                                &value_, num_bytes_read);
67   num_bytes_remaining_ -= static_cast<int>(*num_bytes_read);
68 
69   return status;
70 }
71 
72 }  // namespace webm
73