2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "CheckedInt.h"
30 #include "ExceptionCode.h"
37 char bytes[sizeof(T)];
44 PassRefPtr<DataView> DataView::create(unsigned length)
46 RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(length, sizeof(uint8_t));
49 return create(buffer, 0, length);
52 PassRefPtr<DataView> DataView::create(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset, unsigned byteLength)
54 if (byteOffset > buffer->byteLength())
56 CheckedInt<uint32_t> checkedOffset(byteOffset);
57 CheckedInt<uint32_t> checkedLength(byteLength);
58 CheckedInt<uint32_t> checkedMax = checkedOffset + checkedLength;
59 if (!checkedMax.valid() || checkedMax.value() > buffer->byteLength())
61 return adoptRef(new DataView(buffer, byteOffset, byteLength));
64 DataView::DataView(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset, unsigned byteLength)
65 : ArrayBufferView(buffer, byteOffset)
66 , m_byteLength(byteLength)
70 static bool needToFlipBytes(bool littleEndian)
79 inline void swapBytes(char* p, char* q)
86 static void flipBytesFor16Bits(char* p)
91 static void flipBytesFor32Bits(char* p)
94 swapBytes(p + 1, p + 2);
97 static void flipBytesFor64Bits(char* p)
100 swapBytes(p + 1, p + 6);
101 swapBytes(p + 2, p + 5);
102 swapBytes(p + 3, p + 4);
105 static void flipBytesIfNeeded(char* value, size_t size, bool littleEndian)
107 if (!needToFlipBytes(littleEndian))
115 flipBytesFor16Bits(value);
118 flipBytesFor32Bits(value);
121 flipBytesFor64Bits(value);
124 ASSERT_NOT_REACHED();
130 T DataView::getData(unsigned byteOffset, bool littleEndian, ExceptionCode& ec) const
132 if (beyondRange<T>(byteOffset)) {
137 // We do not want to load the data directly since it would cause a bus error on architectures that don't support unaligned loads.
139 memcpy(value.bytes, static_cast<const char*>(m_baseAddress) + byteOffset, sizeof(T));
140 flipBytesIfNeeded(value.bytes, sizeof(T), littleEndian);
145 void DataView::setData(unsigned byteOffset, T value, bool littleEndian, ExceptionCode& ec)
147 if (beyondRange<T>(byteOffset)) {
152 // We do not want to store the data directly since it would cause a bus error on architectures that don't support unaligned stores.
154 tempValue.data = value;
155 flipBytesIfNeeded(tempValue.bytes, sizeof(T), littleEndian);
156 memcpy(static_cast<char*>(m_baseAddress) + byteOffset, tempValue.bytes, sizeof(T));
159 int8_t DataView::getInt8(unsigned byteOffset, ExceptionCode& ec)
161 return getData<int8_t>(byteOffset, false, ec);
164 uint8_t DataView::getUint8(unsigned byteOffset, ExceptionCode& ec)
166 return getData<uint8_t>(byteOffset, false, ec);
169 int16_t DataView::getInt16(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
171 return getData<int16_t>(byteOffset, littleEndian, ec);
174 uint16_t DataView::getUint16(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
176 return getData<uint16_t>(byteOffset, littleEndian, ec);
179 int32_t DataView::getInt32(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
181 return getData<int32_t>(byteOffset, littleEndian, ec);
184 uint32_t DataView::getUint32(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
186 return getData<uint32_t>(byteOffset, littleEndian, ec);
189 float DataView::getFloat32(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
191 return getData<float>(byteOffset, littleEndian, ec);
194 double DataView::getFloat64(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
196 return getData<double>(byteOffset, littleEndian, ec);
199 void DataView::setInt8(unsigned byteOffset, int8_t value, ExceptionCode& ec)
201 setData<int8_t>(byteOffset, value, false, ec);
204 void DataView::setUint8(unsigned byteOffset, uint8_t value, ExceptionCode& ec)
206 setData<uint8_t>(byteOffset, value, false, ec);
209 void DataView::setInt16(unsigned byteOffset, short value, bool littleEndian, ExceptionCode& ec)
211 setData<int16_t>(byteOffset, value, littleEndian, ec);
214 void DataView::setUint16(unsigned byteOffset, uint16_t value, bool littleEndian, ExceptionCode& ec)
216 setData<uint16_t>(byteOffset, value, littleEndian, ec);
219 void DataView::setInt32(unsigned byteOffset, int32_t value, bool littleEndian, ExceptionCode& ec)
221 setData<int32_t>(byteOffset, value, littleEndian, ec);
224 void DataView::setUint32(unsigned byteOffset, uint32_t value, bool littleEndian, ExceptionCode& ec)
226 setData<uint32_t>(byteOffset, value, littleEndian, ec);
229 void DataView::setFloat32(unsigned byteOffset, float value, bool littleEndian, ExceptionCode& ec)
231 setData<float>(byteOffset, value, littleEndian, ec);
234 void DataView::setFloat64(unsigned byteOffset, double value, bool littleEndian, ExceptionCode& ec)
236 setData<double>(byteOffset, value, littleEndian, ec);
239 void DataView::neuter()
241 ArrayBufferView::neuter();