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"
36 char bytes[sizeof(T)];
43 PassRefPtr<DataView> DataView::create(unsigned length)
45 RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(length, sizeof(uint8_t));
48 return create(buffer, 0, length);
51 PassRefPtr<DataView> DataView::create(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset, unsigned byteLength)
53 if (byteOffset > buffer->byteLength())
55 CheckedInt<uint32_t> checkedOffset(byteOffset);
56 CheckedInt<uint32_t> checkedLength(byteLength);
57 CheckedInt<uint32_t> checkedMax = checkedOffset + checkedLength;
58 if (!checkedMax.valid() || checkedMax.value() > buffer->byteLength())
60 return adoptRef(new DataView(buffer, byteOffset, byteLength));
63 DataView::DataView(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset, unsigned byteLength)
64 : ArrayBufferView(buffer, byteOffset)
65 , m_byteLength(byteLength)
69 static bool needToFlipBytes(bool littleEndian)
78 inline void swapBytes(char* p, char* q)
85 static void flipBytesFor16Bits(char* p)
90 static void flipBytesFor32Bits(char* p)
93 swapBytes(p + 1, p + 2);
96 static void flipBytesFor64Bits(char* p)
99 swapBytes(p + 1, p + 6);
100 swapBytes(p + 2, p + 5);
101 swapBytes(p + 3, p + 4);
104 static void flipBytesIfNeeded(char* value, size_t size, bool littleEndian)
106 if (!needToFlipBytes(littleEndian))
114 flipBytesFor16Bits(value);
117 flipBytesFor32Bits(value);
120 flipBytesFor64Bits(value);
123 ASSERT_NOT_REACHED();
129 T DataView::getData(unsigned byteOffset, bool littleEndian, ExceptionCode& ec) const
131 if (beyondRange<T>(byteOffset)) {
136 // We do not want to load the data directly since it would cause a bus error on architectures that don't support unaligned loads.
138 memcpy(value.bytes, static_cast<const char*>(m_baseAddress) + byteOffset, sizeof(T));
139 flipBytesIfNeeded(value.bytes, sizeof(T), littleEndian);
144 void DataView::setData(unsigned byteOffset, T value, bool littleEndian, ExceptionCode& ec)
146 if (beyondRange<T>(byteOffset)) {
151 // We do not want to store the data directly since it would cause a bus error on architectures that don't support unaligned stores.
153 tempValue.data = value;
154 flipBytesIfNeeded(tempValue.bytes, sizeof(T), littleEndian);
155 memcpy(static_cast<char*>(m_baseAddress) + byteOffset, tempValue.bytes, sizeof(T));
158 int8_t DataView::getInt8(unsigned byteOffset, ExceptionCode& ec)
160 return getData<int8_t>(byteOffset, false, ec);
163 uint8_t DataView::getUint8(unsigned byteOffset, ExceptionCode& ec)
165 return getData<uint8_t>(byteOffset, false, ec);
168 int16_t DataView::getInt16(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
170 return getData<int16_t>(byteOffset, littleEndian, ec);
173 uint16_t DataView::getUint16(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
175 return getData<uint16_t>(byteOffset, littleEndian, ec);
178 int32_t DataView::getInt32(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
180 return getData<int32_t>(byteOffset, littleEndian, ec);
183 uint32_t DataView::getUint32(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
185 return getData<uint32_t>(byteOffset, littleEndian, ec);
188 float DataView::getFloat32(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
190 return getData<float>(byteOffset, littleEndian, ec);
193 double DataView::getFloat64(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
195 return getData<double>(byteOffset, littleEndian, ec);
198 void DataView::setInt8(unsigned byteOffset, int8_t value, ExceptionCode& ec)
200 setData<int8_t>(byteOffset, value, false, ec);
203 void DataView::setUint8(unsigned byteOffset, uint8_t value, ExceptionCode& ec)
205 setData<uint8_t>(byteOffset, value, false, ec);
208 void DataView::setInt16(unsigned byteOffset, short value, bool littleEndian, ExceptionCode& ec)
210 setData<int16_t>(byteOffset, value, littleEndian, ec);
213 void DataView::setUint16(unsigned byteOffset, uint16_t value, bool littleEndian, ExceptionCode& ec)
215 setData<uint16_t>(byteOffset, value, littleEndian, ec);
218 void DataView::setInt32(unsigned byteOffset, int32_t value, bool littleEndian, ExceptionCode& ec)
220 setData<int32_t>(byteOffset, value, littleEndian, ec);
223 void DataView::setUint32(unsigned byteOffset, uint32_t value, bool littleEndian, ExceptionCode& ec)
225 setData<uint32_t>(byteOffset, value, littleEndian, ec);
228 void DataView::setFloat32(unsigned byteOffset, float value, bool littleEndian, ExceptionCode& ec)
230 setData<float>(byteOffset, value, littleEndian, ec);
233 void DataView::setFloat64(unsigned byteOffset, double value, bool littleEndian, ExceptionCode& ec)
235 setData<double>(byteOffset, value, littleEndian, ec);