1 | /* |
2 | * Copyright (C) 2010, 2011, 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 | #include "config.h" |
27 | #include <wtf/persistence/PersistentDecoder.h> |
28 | |
29 | #include <wtf/persistence/PersistentEncoder.h> |
30 | |
31 | namespace WTF { |
32 | namespace Persistence { |
33 | |
34 | Decoder::Decoder(const uint8_t* buffer, size_t bufferSize) |
35 | : m_buffer(buffer) |
36 | , m_bufferPosition(buffer) |
37 | , m_bufferEnd(buffer + bufferSize) |
38 | { |
39 | } |
40 | |
41 | Decoder::~Decoder() |
42 | { |
43 | } |
44 | |
45 | bool Decoder::bufferIsLargeEnoughToContain(size_t size) const |
46 | { |
47 | return size <= static_cast<size_t>(m_bufferEnd - m_bufferPosition); |
48 | } |
49 | |
50 | bool Decoder::decodeFixedLengthData(uint8_t* data, size_t size) |
51 | { |
52 | if (!bufferIsLargeEnoughToContain(size)) |
53 | return false; |
54 | |
55 | memcpy(data, m_bufferPosition, size); |
56 | m_bufferPosition += size; |
57 | |
58 | Encoder::updateChecksumForData(m_sha1, data, size); |
59 | return true; |
60 | } |
61 | |
62 | template<typename Type> |
63 | bool Decoder::decodeNumber(Type& value) |
64 | { |
65 | if (!bufferIsLargeEnoughToContain(sizeof(value))) |
66 | return false; |
67 | |
68 | memcpy(&value, m_bufferPosition, sizeof(value)); |
69 | m_bufferPosition += sizeof(Type); |
70 | |
71 | Encoder::updateChecksumForNumber(m_sha1, value); |
72 | return true; |
73 | } |
74 | |
75 | bool Decoder::decode(bool& result) |
76 | { |
77 | return decodeNumber(result); |
78 | } |
79 | |
80 | bool Decoder::decode(uint8_t& result) |
81 | { |
82 | return decodeNumber(result); |
83 | } |
84 | |
85 | bool Decoder::decode(uint16_t& result) |
86 | { |
87 | return decodeNumber(result); |
88 | } |
89 | |
90 | bool Decoder::decode(uint32_t& result) |
91 | { |
92 | return decodeNumber(result); |
93 | } |
94 | |
95 | bool Decoder::decode(uint64_t& result) |
96 | { |
97 | return decodeNumber(result); |
98 | } |
99 | |
100 | bool Decoder::decode(int32_t& result) |
101 | { |
102 | return decodeNumber(result); |
103 | } |
104 | |
105 | bool Decoder::decode(int64_t& result) |
106 | { |
107 | return decodeNumber(result); |
108 | } |
109 | |
110 | bool Decoder::decode(float& result) |
111 | { |
112 | return decodeNumber(result); |
113 | } |
114 | |
115 | bool Decoder::decode(double& result) |
116 | { |
117 | return decodeNumber(result); |
118 | } |
119 | |
120 | bool Decoder::verifyChecksum() |
121 | { |
122 | SHA1::Digest computedHash; |
123 | m_sha1.computeHash(computedHash); |
124 | |
125 | SHA1::Digest savedHash; |
126 | if (!decodeFixedLengthData(savedHash.data(), sizeof(savedHash))) |
127 | return false; |
128 | |
129 | return computedHash == savedHash; |
130 | } |
131 | |
132 | } |
133 | } |
134 | |