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.
27 #include "core/html/canvas/DataView.h"
29 #include "bindings/core/v8/ExceptionState.h"
30 #include "core/dom/ExceptionCode.h"
31 #include "platform/CheckedInt.h"
39 char bytes[sizeof(T)];
46 PassRefPtr<DataView> DataView::create(unsigned length)
48 RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(length, sizeof(uint8_t));
51 return create(buffer, 0, length);
54 PassRefPtr<DataView> DataView::create(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset, unsigned byteLength)
56 if (byteOffset > buffer->byteLength())
58 CheckedInt<uint32_t> checkedOffset(byteOffset);
59 CheckedInt<uint32_t> checkedLength(byteLength);
60 CheckedInt<uint32_t> checkedMax = checkedOffset + checkedLength;
61 if (!checkedMax.isValid() || checkedMax.value() > buffer->byteLength())
63 return adoptRef(new DataView(buffer, byteOffset, byteLength));
66 DataView::DataView(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset, unsigned byteLength)
67 : ArrayBufferView(buffer, byteOffset)
68 , m_byteLength(byteLength)
72 static bool needToFlipBytes(bool littleEndian)
81 inline void swapBytes(char* p, char* q)
88 static void flipBytesFor16Bits(char* p)
93 static void flipBytesFor32Bits(char* p)
96 swapBytes(p + 1, p + 2);
99 static void flipBytesFor64Bits(char* p)
102 swapBytes(p + 1, p + 6);
103 swapBytes(p + 2, p + 5);
104 swapBytes(p + 3, p + 4);
107 static void flipBytesIfNeeded(char* value, size_t size, bool littleEndian)
109 if (!needToFlipBytes(littleEndian))
117 flipBytesFor16Bits(value);
120 flipBytesFor32Bits(value);
123 flipBytesFor64Bits(value);
126 ASSERT_NOT_REACHED();
132 T DataView::getData(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState) const
134 if (beyondRange<T>(byteOffset)) {
135 exceptionState.throwDOMException(IndexSizeError, "The provided offset (" + String::number(byteOffset) + ") is outside the allowed range.");
139 // We do not want to load the data directly since it would cause a bus error on architectures that don't support unaligned loads.
141 memcpy(value.bytes, static_cast<const char*>(m_baseAddress) + byteOffset, sizeof(T));
142 flipBytesIfNeeded(value.bytes, sizeof(T), littleEndian);
147 void DataView::setData(unsigned byteOffset, T value, bool littleEndian, ExceptionState& exceptionState)
149 if (beyondRange<T>(byteOffset)) {
150 exceptionState.throwDOMException(IndexSizeError, "The provided offset (" + String::number(byteOffset) + ") is outside the allowed range.");
154 // We do not want to store the data directly since it would cause a bus error on architectures that don't support unaligned stores.
156 tempValue.data = value;
157 flipBytesIfNeeded(tempValue.bytes, sizeof(T), littleEndian);
158 memcpy(static_cast<char*>(m_baseAddress) + byteOffset, tempValue.bytes, sizeof(T));
161 int8_t DataView::getInt8(unsigned byteOffset, ExceptionState& exceptionState)
163 return getData<int8_t>(byteOffset, false, exceptionState);
166 uint8_t DataView::getUint8(unsigned byteOffset, ExceptionState& exceptionState)
168 return getData<uint8_t>(byteOffset, false, exceptionState);
171 int16_t DataView::getInt16(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState)
173 return getData<int16_t>(byteOffset, littleEndian, exceptionState);
176 uint16_t DataView::getUint16(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState)
178 return getData<uint16_t>(byteOffset, littleEndian, exceptionState);
181 int32_t DataView::getInt32(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState)
183 return getData<int32_t>(byteOffset, littleEndian, exceptionState);
186 uint32_t DataView::getUint32(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState)
188 return getData<uint32_t>(byteOffset, littleEndian, exceptionState);
191 float DataView::getFloat32(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState)
193 return getData<float>(byteOffset, littleEndian, exceptionState);
196 double DataView::getFloat64(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState)
198 return getData<double>(byteOffset, littleEndian, exceptionState);
201 void DataView::setInt8(unsigned byteOffset, int8_t value, ExceptionState& exceptionState)
203 setData<int8_t>(byteOffset, value, false, exceptionState);
206 void DataView::setUint8(unsigned byteOffset, uint8_t value, ExceptionState& exceptionState)
208 setData<uint8_t>(byteOffset, value, false, exceptionState);
211 void DataView::setInt16(unsigned byteOffset, short value, bool littleEndian, ExceptionState& exceptionState)
213 setData<int16_t>(byteOffset, value, littleEndian, exceptionState);
216 void DataView::setUint16(unsigned byteOffset, uint16_t value, bool littleEndian, ExceptionState& exceptionState)
218 setData<uint16_t>(byteOffset, value, littleEndian, exceptionState);
221 void DataView::setInt32(unsigned byteOffset, int32_t value, bool littleEndian, ExceptionState& exceptionState)
223 setData<int32_t>(byteOffset, value, littleEndian, exceptionState);
226 void DataView::setUint32(unsigned byteOffset, uint32_t value, bool littleEndian, ExceptionState& exceptionState)
228 setData<uint32_t>(byteOffset, value, littleEndian, exceptionState);
231 void DataView::setFloat32(unsigned byteOffset, float value, bool littleEndian, ExceptionState& exceptionState)
233 setData<float>(byteOffset, value, littleEndian, exceptionState);
236 void DataView::setFloat64(unsigned byteOffset, double value, bool littleEndian, ExceptionState& exceptionState)
238 setData<double>(byteOffset, value, littleEndian, exceptionState);
241 void DataView::neuter()
243 ArrayBufferView::neuter();