1/*
2 * Copyright (C) 2010, 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 "ArgumentDecoder.h"
28
29#include "DataReference.h"
30#include <stdio.h>
31
32namespace IPC {
33
34ArgumentDecoder::ArgumentDecoder(const uint8_t* buffer, size_t bufferSize)
35{
36    initialize(buffer, bufferSize);
37}
38
39ArgumentDecoder::ArgumentDecoder(const uint8_t* buffer, size_t bufferSize, Vector<Attachment> attachments)
40{
41    initialize(buffer, bufferSize);
42
43    m_attachments = WTF::move(attachments);
44}
45
46ArgumentDecoder::~ArgumentDecoder()
47{
48    ASSERT(m_buffer);
49    free(m_buffer);
50#if !USE(UNIX_DOMAIN_SOCKETS)
51    // FIXME: We need to dispose of the mach ports in cases of failure.
52#else
53    Vector<Attachment>::iterator end = m_attachments.end();
54    for (Vector<Attachment>::iterator it = m_attachments.begin(); it != end; ++it)
55        it->dispose();
56#endif
57}
58
59static inline uint8_t* roundUpToAlignment(uint8_t* ptr, unsigned alignment)
60{
61    // Assert that the alignment is a power of 2.
62    ASSERT(alignment && !(alignment & (alignment - 1)));
63
64    uintptr_t alignmentMask = alignment - 1;
65    return reinterpret_cast<uint8_t*>((reinterpret_cast<uintptr_t>(ptr) + alignmentMask) & ~alignmentMask);
66}
67
68void ArgumentDecoder::initialize(const uint8_t* buffer, size_t bufferSize)
69{
70    m_buffer = static_cast<uint8_t*>(malloc(bufferSize));
71
72    ASSERT(!(reinterpret_cast<uintptr_t>(m_buffer) % alignof(uint64_t)));
73
74    m_bufferPos = m_buffer;
75    m_bufferEnd = m_buffer + bufferSize;
76    memcpy(m_buffer, buffer, bufferSize);
77}
78
79static inline bool alignedBufferIsLargeEnoughToContain(const uint8_t* alignedPosition, const uint8_t* bufferEnd, size_t size)
80{
81    return bufferEnd >= alignedPosition && static_cast<size_t>(bufferEnd - alignedPosition) >= size;
82}
83
84bool ArgumentDecoder::alignBufferPosition(unsigned alignment, size_t size)
85{
86    uint8_t* alignedPosition = roundUpToAlignment(m_bufferPos, alignment);
87    if (!alignedBufferIsLargeEnoughToContain(alignedPosition, m_bufferEnd, size)) {
88        // We've walked off the end of this buffer.
89        markInvalid();
90        return false;
91    }
92
93    m_bufferPos = alignedPosition;
94    return true;
95}
96
97bool ArgumentDecoder::bufferIsLargeEnoughToContain(unsigned alignment, size_t size) const
98{
99    return alignedBufferIsLargeEnoughToContain(roundUpToAlignment(m_bufferPos, alignment), m_bufferEnd, size);
100}
101
102bool ArgumentDecoder::decodeFixedLengthData(uint8_t* data, size_t size, unsigned alignment)
103{
104    if (!alignBufferPosition(alignment, size))
105        return false;
106
107    memcpy(data, m_bufferPos, size);
108    m_bufferPos += size;
109
110    return true;
111}
112
113bool ArgumentDecoder::decodeVariableLengthByteArray(DataReference& dataReference)
114{
115    uint64_t size;
116    if (!decode(size))
117        return false;
118
119    if (!alignBufferPosition(1, size))
120        return false;
121
122    uint8_t* data = m_bufferPos;
123    m_bufferPos += size;
124
125    dataReference = DataReference(data, size);
126    return true;
127}
128
129template<typename Type>
130static void decodeValueFromBuffer(Type& value, uint8_t*& bufferPosition)
131{
132    memcpy(&value, bufferPosition, sizeof(value));
133    bufferPosition += sizeof(Type);
134}
135
136bool ArgumentDecoder::decode(bool& result)
137{
138    if (!alignBufferPosition(sizeof(result), sizeof(result)))
139        return false;
140
141    decodeValueFromBuffer(result, m_bufferPos);
142    return true;
143}
144
145bool ArgumentDecoder::decode(uint8_t& result)
146{
147    if (!alignBufferPosition(sizeof(result), sizeof(result)))
148        return false;
149
150    decodeValueFromBuffer(result, m_bufferPos);
151    return true;
152}
153
154bool ArgumentDecoder::decode(uint16_t& result)
155{
156    if (!alignBufferPosition(sizeof(result), sizeof(result)))
157        return false;
158
159    decodeValueFromBuffer(result, m_bufferPos);
160    return true;
161}
162
163bool ArgumentDecoder::decode(uint32_t& result)
164{
165    if (!alignBufferPosition(sizeof(result), sizeof(result)))
166        return false;
167
168    decodeValueFromBuffer(result, m_bufferPos);
169    return true;
170}
171
172bool ArgumentDecoder::decode(uint64_t& result)
173{
174    if (!alignBufferPosition(sizeof(result), sizeof(result)))
175        return false;
176
177    decodeValueFromBuffer(result, m_bufferPos);
178    return true;
179}
180
181bool ArgumentDecoder::decode(int32_t& result)
182{
183    if (!alignBufferPosition(sizeof(result), sizeof(result)))
184        return false;
185
186    decodeValueFromBuffer(result, m_bufferPos);
187    return true;
188}
189
190bool ArgumentDecoder::decode(int64_t& result)
191{
192    if (!alignBufferPosition(sizeof(result), sizeof(result)))
193        return false;
194
195    decodeValueFromBuffer(result, m_bufferPos);
196    return true;
197}
198
199bool ArgumentDecoder::decode(float& result)
200{
201    if (!alignBufferPosition(sizeof(result), sizeof(result)))
202        return false;
203
204    decodeValueFromBuffer(result, m_bufferPos);
205    return true;
206}
207
208bool ArgumentDecoder::decode(double& result)
209{
210    if (!alignBufferPosition(sizeof(result), sizeof(result)))
211        return false;
212
213    decodeValueFromBuffer(result, m_bufferPos);
214    return true;
215}
216
217bool ArgumentDecoder::removeAttachment(Attachment& attachment)
218{
219    if (m_attachments.isEmpty())
220        return false;
221
222    attachment = m_attachments.last();
223    m_attachments.removeLast();
224    return true;
225}
226
227} // namespace IPC
228