1 | /* |
2 | * Copyright (C) 2014 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 | #pragma once |
27 | |
28 | #include <wtf/EnumTraits.h> |
29 | #include <wtf/SHA1.h> |
30 | #include <wtf/persistence/PersistentCoder.h> |
31 | |
32 | namespace WTF { |
33 | namespace Persistence { |
34 | |
35 | class Decoder { |
36 | WTF_MAKE_FAST_ALLOCATED; |
37 | public: |
38 | WTF_EXPORT_PRIVATE Decoder(const uint8_t* buffer, size_t bufferSize); |
39 | WTF_EXPORT_PRIVATE ~Decoder(); |
40 | |
41 | size_t length() const { return m_bufferEnd - m_buffer; } |
42 | size_t currentOffset() const { return m_bufferPosition - m_buffer; } |
43 | |
44 | WTF_EXPORT_PRIVATE bool verifyChecksum(); |
45 | |
46 | WTF_EXPORT_PRIVATE bool decodeFixedLengthData(uint8_t*, size_t); |
47 | |
48 | WTF_EXPORT_PRIVATE bool decode(bool&); |
49 | WTF_EXPORT_PRIVATE bool decode(uint8_t&); |
50 | WTF_EXPORT_PRIVATE bool decode(uint16_t&); |
51 | WTF_EXPORT_PRIVATE bool decode(uint32_t&); |
52 | WTF_EXPORT_PRIVATE bool decode(uint64_t&); |
53 | WTF_EXPORT_PRIVATE bool decode(int32_t&); |
54 | WTF_EXPORT_PRIVATE bool decode(int64_t&); |
55 | WTF_EXPORT_PRIVATE bool decode(float&); |
56 | WTF_EXPORT_PRIVATE bool decode(double&); |
57 | |
58 | template<typename E> auto decode(E& e) -> std::enable_if_t<std::is_enum<E>::value, bool> |
59 | { |
60 | uint64_t value; |
61 | if (!decode(value)) |
62 | return false; |
63 | if (!isValidEnum<E>(value)) |
64 | return false; |
65 | |
66 | e = static_cast<E>(value); |
67 | return true; |
68 | } |
69 | |
70 | template<typename T> bool decodeEnum(T& result) |
71 | { |
72 | static_assert(sizeof(T) <= 8, "Enum type T must not be larger than 64 bits!" ); |
73 | |
74 | uint64_t value; |
75 | if (!decode(value)) |
76 | return false; |
77 | |
78 | result = static_cast<T>(value); |
79 | return true; |
80 | } |
81 | |
82 | template<typename T> auto decode(T& t) -> std::enable_if_t<!std::is_enum<T>::value, bool> |
83 | { |
84 | return Coder<T>::decode(*this, t); |
85 | } |
86 | |
87 | template<typename T> |
88 | bool bufferIsLargeEnoughToContain(size_t numElements) const |
89 | { |
90 | static_assert(std::is_arithmetic<T>::value, "Type T must have a fixed, known encoded size!" ); |
91 | |
92 | if (numElements > std::numeric_limits<size_t>::max() / sizeof(T)) |
93 | return false; |
94 | |
95 | return bufferIsLargeEnoughToContain(numElements * sizeof(T)); |
96 | } |
97 | |
98 | static const bool isIPCDecoder = false; |
99 | |
100 | private: |
101 | WTF_EXPORT_PRIVATE bool bufferIsLargeEnoughToContain(size_t) const; |
102 | template<typename Type> bool decodeNumber(Type&); |
103 | |
104 | const uint8_t* m_buffer; |
105 | const uint8_t* m_bufferPosition; |
106 | const uint8_t* m_bufferEnd; |
107 | |
108 | SHA1 m_sha1; |
109 | }; |
110 | |
111 | } |
112 | } |
113 | |
114 | |