1 /* 2 * Copyright (C) 2010 Google 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 * 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. 13 * 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. 24 */ 25 26 #include "config.h" 27 #include "core/html/canvas/DataView.h" 28 29 #include "bindings/core/v8/ExceptionState.h" 30 #include "bindings/core/v8/custom/V8DataViewCustom.h" 31 #include "bindings/core/v8/custom/V8TypedArrayCustom.h" 32 #include "core/dom/ExceptionCode.h" 33 #include "platform/CheckedInt.h" 34 #include "wtf/CPU.h" 35 36 namespace { 37 38 template<typename T> 39 union Value { 40 T data; 41 char bytes[sizeof(T)]; 42 }; 43 44 } 45 46 namespace blink { 47 48 PassRefPtr<DataView> DataView::create(unsigned length) 49 { 50 RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(length, sizeof(uint8_t)); 51 if (!buffer.get()) 52 return nullptr; 53 return create(buffer, 0, length); 54 } 55 56 PassRefPtr<DataView> DataView::create(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset, unsigned byteLength) 57 { 58 if (byteOffset > buffer->byteLength()) 59 return nullptr; 60 CheckedInt<uint32_t> checkedOffset(byteOffset); 61 CheckedInt<uint32_t> checkedLength(byteLength); 62 CheckedInt<uint32_t> checkedMax = checkedOffset + checkedLength; 63 if (!checkedMax.isValid() || checkedMax.value() > buffer->byteLength()) 64 return nullptr; 65 return adoptRef(new DataView(buffer, byteOffset, byteLength)); 66 } 67 68 DataView::DataView(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset, unsigned byteLength) 69 : ArrayBufferView(buffer, byteOffset) 70 , m_byteLength(byteLength) 71 { 72 } 73 74 static bool needToFlipBytes(bool littleEndian) 75 { 76 #if CPU(BIG_ENDIAN) 77 return littleEndian; 78 #else 79 return !littleEndian; 80 #endif 81 } 82 83 inline void swapBytes(char* p, char* q) 84 { 85 char temp = *p; 86 *p = *q; 87 *q = temp; 88 } 89 90 static void flipBytesFor16Bits(char* p) 91 { 92 swapBytes(p, p + 1); 93 } 94 95 static void flipBytesFor32Bits(char* p) 96 { 97 swapBytes(p, p + 3); 98 swapBytes(p + 1, p + 2); 99 } 100 101 static void flipBytesFor64Bits(char* p) 102 { 103 swapBytes(p, p + 7); 104 swapBytes(p + 1, p + 6); 105 swapBytes(p + 2, p + 5); 106 swapBytes(p + 3, p + 4); 107 } 108 109 static void flipBytesIfNeeded(char* value, size_t size, bool littleEndian) 110 { 111 if (!needToFlipBytes(littleEndian)) 112 return; 113 114 switch (size) { 115 case 1: 116 // Nothing to do. 117 break; 118 case 2: 119 flipBytesFor16Bits(value); 120 break; 121 case 4: 122 flipBytesFor32Bits(value); 123 break; 124 case 8: 125 flipBytesFor64Bits(value); 126 break; 127 default: 128 ASSERT_NOT_REACHED(); 129 break; 130 } 131 } 132 133 template<typename T> 134 T DataView::getData(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState) const 135 { 136 if (beyondRange<T>(byteOffset)) { 137 exceptionState.throwDOMException(IndexSizeError, "The provided offset (" + String::number(byteOffset) + ") is outside the allowed range."); 138 return 0; 139 } 140 141 // We do not want to load the data directly since it would cause a bus error on architectures that don't support unaligned loads. 142 Value<T> value; 143 memcpy(value.bytes, static_cast<const char*>(m_baseAddress) + byteOffset, sizeof(T)); 144 flipBytesIfNeeded(value.bytes, sizeof(T), littleEndian); 145 return value.data; 146 } 147 148 template<typename T> 149 void DataView::setData(unsigned byteOffset, T value, bool littleEndian, ExceptionState& exceptionState) 150 { 151 if (beyondRange<T>(byteOffset)) { 152 exceptionState.throwDOMException(IndexSizeError, "The provided offset (" + String::number(byteOffset) + ") is outside the allowed range."); 153 return; 154 } 155 156 // We do not want to store the data directly since it would cause a bus error on architectures that don't support unaligned stores. 157 Value<T> tempValue; 158 tempValue.data = value; 159 flipBytesIfNeeded(tempValue.bytes, sizeof(T), littleEndian); 160 memcpy(static_cast<char*>(m_baseAddress) + byteOffset, tempValue.bytes, sizeof(T)); 161 } 162 163 int8_t DataView::getInt8(unsigned byteOffset, ExceptionState& exceptionState) 164 { 165 return getData<int8_t>(byteOffset, false, exceptionState); 166 } 167 168 uint8_t DataView::getUint8(unsigned byteOffset, ExceptionState& exceptionState) 169 { 170 return getData<uint8_t>(byteOffset, false, exceptionState); 171 } 172 173 int16_t DataView::getInt16(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState) 174 { 175 return getData<int16_t>(byteOffset, littleEndian, exceptionState); 176 } 177 178 uint16_t DataView::getUint16(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState) 179 { 180 return getData<uint16_t>(byteOffset, littleEndian, exceptionState); 181 } 182 183 int32_t DataView::getInt32(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState) 184 { 185 return getData<int32_t>(byteOffset, littleEndian, exceptionState); 186 } 187 188 uint32_t DataView::getUint32(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState) 189 { 190 return getData<uint32_t>(byteOffset, littleEndian, exceptionState); 191 } 192 193 float DataView::getFloat32(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState) 194 { 195 return getData<float>(byteOffset, littleEndian, exceptionState); 196 } 197 198 double DataView::getFloat64(unsigned byteOffset, bool littleEndian, ExceptionState& exceptionState) 199 { 200 return getData<double>(byteOffset, littleEndian, exceptionState); 201 } 202 203 void DataView::setInt8(unsigned byteOffset, int8_t value, ExceptionState& exceptionState) 204 { 205 setData<int8_t>(byteOffset, value, false, exceptionState); 206 } 207 208 void DataView::setUint8(unsigned byteOffset, uint8_t value, ExceptionState& exceptionState) 209 { 210 setData<uint8_t>(byteOffset, value, false, exceptionState); 211 } 212 213 void DataView::setInt16(unsigned byteOffset, short value, bool littleEndian, ExceptionState& exceptionState) 214 { 215 setData<int16_t>(byteOffset, value, littleEndian, exceptionState); 216 } 217 218 void DataView::setUint16(unsigned byteOffset, uint16_t value, bool littleEndian, ExceptionState& exceptionState) 219 { 220 setData<uint16_t>(byteOffset, value, littleEndian, exceptionState); 221 } 222 223 void DataView::setInt32(unsigned byteOffset, int32_t value, bool littleEndian, ExceptionState& exceptionState) 224 { 225 setData<int32_t>(byteOffset, value, littleEndian, exceptionState); 226 } 227 228 void DataView::setUint32(unsigned byteOffset, uint32_t value, bool littleEndian, ExceptionState& exceptionState) 229 { 230 setData<uint32_t>(byteOffset, value, littleEndian, exceptionState); 231 } 232 233 void DataView::setFloat32(unsigned byteOffset, float value, bool littleEndian, ExceptionState& exceptionState) 234 { 235 setData<float>(byteOffset, value, littleEndian, exceptionState); 236 } 237 238 void DataView::setFloat64(unsigned byteOffset, double value, bool littleEndian, ExceptionState& exceptionState) 239 { 240 setData<double>(byteOffset, value, littleEndian, exceptionState); 241 } 242 243 v8::Handle<v8::Object> DataView::wrap(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 244 { 245 return V8TypedArray<DataView>::wrap(this, creationContext, isolate); 246 } 247 248 void DataView::neuter() 249 { 250 ArrayBufferView::neuter(); 251 m_byteLength = 0; 252 } 253 254 } // namespace blink 255