1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "ISOBox.h"
28
29#include <JavaScriptCore/DataView.h>
30
31using JSC::DataView;
32
33namespace WebCore {
34
35ISOBox::PeekResult ISOBox::peekBox(DataView& view, unsigned offset)
36{
37 uint64_t size = 0;
38 if (!checkedRead<uint32_t>(size, view, offset, BigEndian))
39 return WTF::nullopt;
40
41 FourCC type = { uint32_t { 0 } };
42 if (!checkedRead<uint32_t>(type, view, offset, BigEndian))
43 return WTF::nullopt;
44
45 if (size == 1 && !checkedRead<uint64_t>(size, view, offset, BigEndian))
46 return WTF::nullopt;
47 else if (!size)
48 size = view.byteLength();
49
50 return std::make_pair(type, size);
51}
52
53bool ISOBox::read(DataView& view)
54{
55 unsigned localOffset { 0 };
56 return parse(view, localOffset);
57}
58
59bool ISOBox::read(DataView& view, unsigned& offset)
60{
61 unsigned localOffset = offset;
62 if (!parse(view, localOffset))
63 return false;
64
65 offset += m_size;
66 return true;
67}
68
69bool ISOBox::parse(DataView& view, unsigned& offset)
70{
71 if (!checkedRead<uint32_t>(m_size, view, offset, BigEndian))
72 return false;
73
74 if (!checkedRead<uint32_t>(m_boxType, view, offset, BigEndian))
75 return false;
76
77 if (m_size == 1 && !checkedRead<uint64_t>(m_size, view, offset, BigEndian))
78 return false;
79 else if (!m_size)
80 m_size = view.byteLength();
81
82 if (m_boxType == "uuid") {
83 struct ExtendedType {
84 uint8_t value[16];
85 } extendedTypeStruct;
86 if (!checkedRead<ExtendedType>(extendedTypeStruct, view, offset, BigEndian))
87 return false;
88
89 Vector<uint8_t> extendedType;
90 extendedType.reserveInitialCapacity(16);
91 for (auto& character : extendedTypeStruct.value)
92 extendedType.uncheckedAppend(character);
93 m_extendedType = WTFMove(extendedType);
94 }
95
96 return true;
97}
98
99bool ISOFullBox::parse(DataView& view, unsigned& offset)
100{
101 if (!ISOBox::parse(view, offset))
102 return false;
103
104 uint32_t versionAndFlags = 0;
105 if (!checkedRead<uint32_t>(versionAndFlags, view, offset, BigEndian))
106 return false;
107
108 m_version = versionAndFlags >> 24;
109 m_flags = versionAndFlags & 0xFFFFFF;
110 return true;
111}
112
113}
114