1 | /* |
2 | * Copyright (C) 2011, 2014-2015 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/PersistentCoders.h> |
28 | |
29 | #include <wtf/text/CString.h> |
30 | #include <wtf/text/WTFString.h> |
31 | |
32 | namespace WTF { |
33 | namespace Persistence { |
34 | |
35 | void Coder<AtomString>::encode(Encoder& encoder, const AtomString& atomString) |
36 | { |
37 | encoder << atomString.string(); |
38 | } |
39 | |
40 | bool Coder<AtomString>::decode(Decoder& decoder, AtomString& atomString) |
41 | { |
42 | String string; |
43 | if (!decoder.decode(string)) |
44 | return false; |
45 | |
46 | atomString = string; |
47 | return true; |
48 | } |
49 | |
50 | void Coder<CString>::encode(Encoder& encoder, const CString& string) |
51 | { |
52 | // Special case the null string. |
53 | if (string.isNull()) { |
54 | encoder << std::numeric_limits<uint32_t>::max(); |
55 | return; |
56 | } |
57 | |
58 | uint32_t length = string.length(); |
59 | encoder << length; |
60 | encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.data()), length); |
61 | } |
62 | |
63 | bool Coder<CString>::decode(Decoder& decoder, CString& result) |
64 | { |
65 | uint32_t length; |
66 | if (!decoder.decode(length)) |
67 | return false; |
68 | |
69 | if (length == std::numeric_limits<uint32_t>::max()) { |
70 | // This is the null string. |
71 | result = CString(); |
72 | return true; |
73 | } |
74 | |
75 | // Before allocating the string, make sure that the decoder buffer is big enough. |
76 | if (!decoder.bufferIsLargeEnoughToContain<char>(length)) |
77 | return false; |
78 | |
79 | char* buffer; |
80 | CString string = CString::newUninitialized(length, buffer); |
81 | if (!decoder.decodeFixedLengthData(reinterpret_cast<uint8_t*>(buffer), length)) |
82 | return false; |
83 | |
84 | result = string; |
85 | return true; |
86 | } |
87 | |
88 | |
89 | void Coder<String>::encode(Encoder& encoder, const String& string) |
90 | { |
91 | // Special case the null string. |
92 | if (string.isNull()) { |
93 | encoder << std::numeric_limits<uint32_t>::max(); |
94 | return; |
95 | } |
96 | |
97 | uint32_t length = string.length(); |
98 | bool is8Bit = string.is8Bit(); |
99 | |
100 | encoder << length << is8Bit; |
101 | |
102 | if (is8Bit) |
103 | encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.characters8()), length * sizeof(LChar)); |
104 | else |
105 | encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.characters16()), length * sizeof(UChar)); |
106 | } |
107 | |
108 | template <typename CharacterType> |
109 | static inline bool decodeStringText(Decoder& decoder, uint32_t length, String& result) |
110 | { |
111 | // Before allocating the string, make sure that the decoder buffer is big enough. |
112 | if (!decoder.bufferIsLargeEnoughToContain<CharacterType>(length)) |
113 | return false; |
114 | |
115 | CharacterType* buffer; |
116 | String string = String::createUninitialized(length, buffer); |
117 | if (!decoder.decodeFixedLengthData(reinterpret_cast<uint8_t*>(buffer), length * sizeof(CharacterType))) |
118 | return false; |
119 | |
120 | result = string; |
121 | return true; |
122 | } |
123 | |
124 | bool Coder<String>::decode(Decoder& decoder, String& result) |
125 | { |
126 | uint32_t length; |
127 | if (!decoder.decode(length)) |
128 | return false; |
129 | |
130 | if (length == std::numeric_limits<uint32_t>::max()) { |
131 | // This is the null string. |
132 | result = String(); |
133 | return true; |
134 | } |
135 | |
136 | bool is8Bit; |
137 | if (!decoder.decode(is8Bit)) |
138 | return false; |
139 | |
140 | if (is8Bit) |
141 | return decodeStringText<LChar>(decoder, length, result); |
142 | return decodeStringText<UChar>(decoder, length, result); |
143 | } |
144 | |
145 | void Coder<SHA1::Digest>::encode(Encoder& encoder, const SHA1::Digest& digest) |
146 | { |
147 | encoder.encodeFixedLengthData(digest.data(), sizeof(digest)); |
148 | } |
149 | |
150 | bool Coder<SHA1::Digest>::decode(Decoder& decoder, SHA1::Digest& digest) |
151 | { |
152 | return decoder.decodeFixedLengthData(digest.data(), sizeof(digest)); |
153 | } |
154 | |
155 | } |
156 | } |
157 | |