| 1 | /* |
| 2 | * Copyright (C) 2011 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 "ArgumentCoders.h" |
| 28 | |
| 29 | #include "DataReference.h" |
| 30 | #include <wtf/text/CString.h> |
| 31 | #include <wtf/text/WTFString.h> |
| 32 | |
| 33 | namespace IPC { |
| 34 | |
| 35 | void ArgumentCoder<WallTime>::encode(Encoder& encoder, const WallTime& time) |
| 36 | { |
| 37 | encoder << time.secondsSinceEpoch().value(); |
| 38 | } |
| 39 | |
| 40 | bool ArgumentCoder<WallTime>::decode(Decoder& decoder, WallTime& time) |
| 41 | { |
| 42 | double value; |
| 43 | if (!decoder.decode(value)) |
| 44 | return false; |
| 45 | |
| 46 | time = WallTime::fromRawSeconds(value); |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | Optional<WallTime> ArgumentCoder<WallTime>::decode(Decoder& decoder) |
| 51 | { |
| 52 | Optional<double> time; |
| 53 | decoder >> time; |
| 54 | if (!time) |
| 55 | return WTF::nullopt; |
| 56 | return WallTime::fromRawSeconds(*time); |
| 57 | } |
| 58 | |
| 59 | void ArgumentCoder<AtomicString>::encode(Encoder& encoder, const AtomicString& atomicString) |
| 60 | { |
| 61 | encoder << atomicString.string(); |
| 62 | } |
| 63 | |
| 64 | bool ArgumentCoder<AtomicString>::decode(Decoder& decoder, AtomicString& atomicString) |
| 65 | { |
| 66 | String string; |
| 67 | if (!decoder.decode(string)) |
| 68 | return false; |
| 69 | |
| 70 | atomicString = string; |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | void ArgumentCoder<CString>::encode(Encoder& encoder, const CString& string) |
| 75 | { |
| 76 | // Special case the null string. |
| 77 | if (string.isNull()) { |
| 78 | encoder << std::numeric_limits<uint32_t>::max(); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | uint32_t length = string.length(); |
| 83 | encoder << length; |
| 84 | encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.data()), length, 1); |
| 85 | } |
| 86 | |
| 87 | bool ArgumentCoder<CString>::decode(Decoder& decoder, CString& result) |
| 88 | { |
| 89 | uint32_t length; |
| 90 | if (!decoder.decode(length)) |
| 91 | return false; |
| 92 | |
| 93 | if (length == std::numeric_limits<uint32_t>::max()) { |
| 94 | // This is the null string. |
| 95 | result = CString(); |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | // Before allocating the string, make sure that the decoder buffer is big enough. |
| 100 | if (!decoder.bufferIsLargeEnoughToContain<char>(length)) { |
| 101 | decoder.markInvalid(); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | char* buffer; |
| 106 | CString string = CString::newUninitialized(length, buffer); |
| 107 | if (!decoder.decodeFixedLengthData(reinterpret_cast<uint8_t*>(buffer), length, 1)) |
| 108 | return false; |
| 109 | |
| 110 | result = string; |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void ArgumentCoder<String>::encode(Encoder& encoder, const String& string) |
| 116 | { |
| 117 | // Special case the null string. |
| 118 | if (string.isNull()) { |
| 119 | encoder << std::numeric_limits<uint32_t>::max(); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | uint32_t length = string.length(); |
| 124 | bool is8Bit = string.is8Bit(); |
| 125 | |
| 126 | encoder << length << is8Bit; |
| 127 | |
| 128 | if (is8Bit) |
| 129 | encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.characters8()), length * sizeof(LChar), alignof(LChar)); |
| 130 | else |
| 131 | encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(string.characters16()), length * sizeof(UChar), alignof(UChar)); |
| 132 | } |
| 133 | |
| 134 | template <typename CharacterType> |
| 135 | static inline bool decodeStringText(Decoder& decoder, uint32_t length, String& result) |
| 136 | { |
| 137 | // Before allocating the string, make sure that the decoder buffer is big enough. |
| 138 | if (!decoder.bufferIsLargeEnoughToContain<CharacterType>(length)) { |
| 139 | decoder.markInvalid(); |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | CharacterType* buffer; |
| 144 | String string = String::createUninitialized(length, buffer); |
| 145 | if (!decoder.decodeFixedLengthData(reinterpret_cast<uint8_t*>(buffer), length * sizeof(CharacterType), alignof(CharacterType))) |
| 146 | return false; |
| 147 | |
| 148 | result = string; |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | bool ArgumentCoder<String>::decode(Decoder& decoder, String& result) |
| 153 | { |
| 154 | uint32_t length; |
| 155 | if (!decoder.decode(length)) |
| 156 | return false; |
| 157 | |
| 158 | if (length == std::numeric_limits<uint32_t>::max()) { |
| 159 | // This is the null string. |
| 160 | result = String(); |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | bool is8Bit; |
| 165 | if (!decoder.decode(is8Bit)) |
| 166 | return false; |
| 167 | |
| 168 | if (is8Bit) |
| 169 | return decodeStringText<LChar>(decoder, length, result); |
| 170 | return decodeStringText<UChar>(decoder, length, result); |
| 171 | } |
| 172 | |
| 173 | Optional<String> ArgumentCoder<String>::decode(Decoder& decoder) |
| 174 | { |
| 175 | uint32_t length; |
| 176 | if (!decoder.decode(length)) |
| 177 | return WTF::nullopt; |
| 178 | |
| 179 | if (length == std::numeric_limits<uint32_t>::max()) { |
| 180 | // This is the null string. |
| 181 | return String(); |
| 182 | } |
| 183 | |
| 184 | bool is8Bit; |
| 185 | if (!decoder.decode(is8Bit)) |
| 186 | return WTF::nullopt; |
| 187 | |
| 188 | String result; |
| 189 | if (is8Bit) { |
| 190 | if (!decodeStringText<LChar>(decoder, length, result)) |
| 191 | return WTF::nullopt; |
| 192 | return result; |
| 193 | } |
| 194 | if (!decodeStringText<UChar>(decoder, length, result)) |
| 195 | return WTF::nullopt; |
| 196 | return result; |
| 197 | } |
| 198 | |
| 199 | void ArgumentCoder<SHA1::Digest>::encode(Encoder& encoder, const SHA1::Digest& digest) |
| 200 | { |
| 201 | encoder.encodeFixedLengthData(digest.data(), sizeof(digest), 1); |
| 202 | } |
| 203 | |
| 204 | bool ArgumentCoder<SHA1::Digest>::decode(Decoder& decoder, SHA1::Digest& digest) |
| 205 | { |
| 206 | return decoder.decodeFixedLengthData(digest.data(), sizeof(digest), 1); |
| 207 | } |
| 208 | |
| 209 | } // namespace IPC |
| 210 | |