1 | /* |
2 | * Copyright (C) 2010, 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/PersistentEncoder.h> |
28 | |
29 | #include <wtf/SHA1.h> |
30 | |
31 | namespace WTF { |
32 | namespace Persistence { |
33 | |
34 | Encoder::Encoder() |
35 | { |
36 | } |
37 | |
38 | Encoder::~Encoder() |
39 | { |
40 | } |
41 | |
42 | uint8_t* Encoder::grow(size_t size) |
43 | { |
44 | size_t newPosition = m_buffer.size(); |
45 | m_buffer.grow(m_buffer.size() + size); |
46 | return m_buffer.data() + newPosition; |
47 | } |
48 | |
49 | void Encoder::updateChecksumForData(SHA1& sha1, const uint8_t* data, size_t size) |
50 | { |
51 | auto typeSalt = Salt<uint8_t*>::value; |
52 | sha1.addBytes(reinterpret_cast<uint8_t*>(&typeSalt), sizeof(typeSalt)); |
53 | sha1.addBytes(data, size); |
54 | } |
55 | |
56 | void Encoder::encodeFixedLengthData(const uint8_t* data, size_t size) |
57 | { |
58 | updateChecksumForData(m_sha1, data, size); |
59 | |
60 | uint8_t* buffer = grow(size); |
61 | memcpy(buffer, data, size); |
62 | } |
63 | |
64 | template<typename Type> |
65 | void Encoder::encodeNumber(Type value) |
66 | { |
67 | Encoder::updateChecksumForNumber(m_sha1, value); |
68 | |
69 | uint8_t* buffer = grow(sizeof(Type)); |
70 | memcpy(buffer, &value, sizeof(Type)); |
71 | } |
72 | |
73 | void Encoder::encode(bool value) |
74 | { |
75 | encodeNumber(value); |
76 | } |
77 | |
78 | void Encoder::encode(uint8_t value) |
79 | { |
80 | encodeNumber(value); |
81 | } |
82 | |
83 | void Encoder::encode(uint16_t value) |
84 | { |
85 | encodeNumber(value); |
86 | } |
87 | |
88 | void Encoder::encode(uint32_t value) |
89 | { |
90 | encodeNumber(value); |
91 | } |
92 | |
93 | void Encoder::encode(uint64_t value) |
94 | { |
95 | encodeNumber(value); |
96 | } |
97 | |
98 | void Encoder::encode(int32_t value) |
99 | { |
100 | encodeNumber(value); |
101 | } |
102 | |
103 | void Encoder::encode(int64_t value) |
104 | { |
105 | encodeNumber(value); |
106 | } |
107 | |
108 | void Encoder::encode(float value) |
109 | { |
110 | encodeNumber(value); |
111 | } |
112 | |
113 | void Encoder::encode(double value) |
114 | { |
115 | encodeNumber(value); |
116 | } |
117 | |
118 | void Encoder::encodeChecksum() |
119 | { |
120 | SHA1::Digest hash; |
121 | m_sha1.computeHash(hash); |
122 | encodeFixedLengthData(hash.data(), hash.size()); |
123 | } |
124 | |
125 | } |
126 | } |
127 | |