| OLD | NEW |
| (Empty) |
| 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 "core/dom/ExceptionCode.h" | |
| 31 #include "platform/CheckedInt.h" | |
| 32 #include "wtf/CPU.h" | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 template<typename T> | |
| 37 union Value { | |
| 38 T data; | |
| 39 char bytes[sizeof(T)]; | |
| 40 }; | |
| 41 | |
| 42 } | |
| 43 | |
| 44 namespace blink { | |
| 45 | |
| 46 PassRefPtr<DataView> DataView::create(PassRefPtr<ArrayBuffer> buffer, unsigned b
yteOffset, unsigned byteLength) | |
| 47 { | |
| 48 RELEASE_ASSERT(byteOffset <= buffer->byteLength()); | |
| 49 CheckedInt<uint32_t> checkedOffset(byteOffset); | |
| 50 CheckedInt<uint32_t> checkedLength(byteLength); | |
| 51 CheckedInt<uint32_t> checkedMax = checkedOffset + checkedLength; | |
| 52 RELEASE_ASSERT(checkedMax.isValid()); | |
| 53 RELEASE_ASSERT(checkedMax.value() <= buffer->byteLength()); | |
| 54 return adoptRef(new DataView(buffer, byteOffset, byteLength)); | |
| 55 } | |
| 56 | |
| 57 DataView::DataView(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset, unsigned
byteLength) | |
| 58 : ArrayBufferView(buffer, byteOffset) | |
| 59 , m_byteLength(byteLength) | |
| 60 { | |
| 61 } | |
| 62 | |
| 63 static bool needToFlipBytes(bool littleEndian) | |
| 64 { | |
| 65 #if CPU(BIG_ENDIAN) | |
| 66 return littleEndian; | |
| 67 #else | |
| 68 return !littleEndian; | |
| 69 #endif | |
| 70 } | |
| 71 | |
| 72 inline void swapBytes(char* p, char* q) | |
| 73 { | |
| 74 char temp = *p; | |
| 75 *p = *q; | |
| 76 *q = temp; | |
| 77 } | |
| 78 | |
| 79 static void flipBytesFor16Bits(char* p) | |
| 80 { | |
| 81 swapBytes(p, p + 1); | |
| 82 } | |
| 83 | |
| 84 static void flipBytesFor32Bits(char* p) | |
| 85 { | |
| 86 swapBytes(p, p + 3); | |
| 87 swapBytes(p + 1, p + 2); | |
| 88 } | |
| 89 | |
| 90 static void flipBytesFor64Bits(char* p) | |
| 91 { | |
| 92 swapBytes(p, p + 7); | |
| 93 swapBytes(p + 1, p + 6); | |
| 94 swapBytes(p + 2, p + 5); | |
| 95 swapBytes(p + 3, p + 4); | |
| 96 } | |
| 97 | |
| 98 static void flipBytesIfNeeded(char* value, size_t size, bool littleEndian) | |
| 99 { | |
| 100 if (!needToFlipBytes(littleEndian)) | |
| 101 return; | |
| 102 | |
| 103 switch (size) { | |
| 104 case 1: | |
| 105 // Nothing to do. | |
| 106 break; | |
| 107 case 2: | |
| 108 flipBytesFor16Bits(value); | |
| 109 break; | |
| 110 case 4: | |
| 111 flipBytesFor32Bits(value); | |
| 112 break; | |
| 113 case 8: | |
| 114 flipBytesFor64Bits(value); | |
| 115 break; | |
| 116 default: | |
| 117 ASSERT_NOT_REACHED(); | |
| 118 break; | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 template<typename T> | |
| 123 T DataView::getData(unsigned byteOffset, bool littleEndian, ExceptionState& exce
ptionState) const | |
| 124 { | |
| 125 if (beyondRange<T>(byteOffset)) { | |
| 126 exceptionState.throwDOMException(IndexSizeError, "The provided offset ("
+ String::number(byteOffset) + ") is outside the allowed range."); | |
| 127 return 0; | |
| 128 } | |
| 129 | |
| 130 // We do not want to load the data directly since it would cause a bus error
on architectures that don't support unaligned loads. | |
| 131 Value<T> value; | |
| 132 memcpy(value.bytes, static_cast<const char*>(m_baseAddress) + byteOffset, si
zeof(T)); | |
| 133 flipBytesIfNeeded(value.bytes, sizeof(T), littleEndian); | |
| 134 return value.data; | |
| 135 } | |
| 136 | |
| 137 template<typename T> | |
| 138 void DataView::setData(unsigned byteOffset, T value, bool littleEndian, Exceptio
nState& exceptionState) | |
| 139 { | |
| 140 if (beyondRange<T>(byteOffset)) { | |
| 141 exceptionState.throwDOMException(IndexSizeError, "The provided offset ("
+ String::number(byteOffset) + ") is outside the allowed range."); | |
| 142 return; | |
| 143 } | |
| 144 | |
| 145 // We do not want to store the data directly since it would cause a bus erro
r on architectures that don't support unaligned stores. | |
| 146 Value<T> tempValue; | |
| 147 tempValue.data = value; | |
| 148 flipBytesIfNeeded(tempValue.bytes, sizeof(T), littleEndian); | |
| 149 memcpy(static_cast<char*>(m_baseAddress) + byteOffset, tempValue.bytes, size
of(T)); | |
| 150 } | |
| 151 | |
| 152 int16_t DataView::getInt16(unsigned byteOffset, bool littleEndian, ExceptionStat
e& exceptionState) | |
| 153 { | |
| 154 return getData<int16_t>(byteOffset, littleEndian, exceptionState); | |
| 155 } | |
| 156 | |
| 157 uint16_t DataView::getUint16(unsigned byteOffset, bool littleEndian, ExceptionSt
ate& exceptionState) | |
| 158 { | |
| 159 return getData<uint16_t>(byteOffset, littleEndian, exceptionState); | |
| 160 } | |
| 161 | |
| 162 int32_t DataView::getInt32(unsigned byteOffset, bool littleEndian, ExceptionStat
e& exceptionState) | |
| 163 { | |
| 164 return getData<int32_t>(byteOffset, littleEndian, exceptionState); | |
| 165 } | |
| 166 | |
| 167 uint32_t DataView::getUint32(unsigned byteOffset, bool littleEndian, ExceptionSt
ate& exceptionState) | |
| 168 { | |
| 169 return getData<uint32_t>(byteOffset, littleEndian, exceptionState); | |
| 170 } | |
| 171 | |
| 172 float DataView::getFloat32(unsigned byteOffset, bool littleEndian, ExceptionStat
e& exceptionState) | |
| 173 { | |
| 174 return getData<float>(byteOffset, littleEndian, exceptionState); | |
| 175 } | |
| 176 | |
| 177 double DataView::getFloat64(unsigned byteOffset, bool littleEndian, ExceptionSta
te& exceptionState) | |
| 178 { | |
| 179 return getData<double>(byteOffset, littleEndian, exceptionState); | |
| 180 } | |
| 181 | |
| 182 void DataView::setInt16(unsigned byteOffset, short value, bool littleEndian, Exc
eptionState& exceptionState) | |
| 183 { | |
| 184 setData<int16_t>(byteOffset, value, littleEndian, exceptionState); | |
| 185 } | |
| 186 | |
| 187 void DataView::setUint16(unsigned byteOffset, uint16_t value, bool littleEndian,
ExceptionState& exceptionState) | |
| 188 { | |
| 189 setData<uint16_t>(byteOffset, value, littleEndian, exceptionState); | |
| 190 } | |
| 191 | |
| 192 void DataView::setInt32(unsigned byteOffset, int32_t value, bool littleEndian, E
xceptionState& exceptionState) | |
| 193 { | |
| 194 setData<int32_t>(byteOffset, value, littleEndian, exceptionState); | |
| 195 } | |
| 196 | |
| 197 void DataView::setUint32(unsigned byteOffset, uint32_t value, bool littleEndian,
ExceptionState& exceptionState) | |
| 198 { | |
| 199 setData<uint32_t>(byteOffset, value, littleEndian, exceptionState); | |
| 200 } | |
| 201 | |
| 202 void DataView::setFloat32(unsigned byteOffset, float value, bool littleEndian, E
xceptionState& exceptionState) | |
| 203 { | |
| 204 setData<float>(byteOffset, value, littleEndian, exceptionState); | |
| 205 } | |
| 206 | |
| 207 void DataView::setFloat64(unsigned byteOffset, double value, bool littleEndian,
ExceptionState& exceptionState) | |
| 208 { | |
| 209 setData<double>(byteOffset, value, littleEndian, exceptionState); | |
| 210 } | |
| 211 | |
| 212 void DataView::neuter() | |
| 213 { | |
| 214 ArrayBufferView::neuter(); | |
| 215 m_byteLength = 0; | |
| 216 } | |
| 217 | |
| 218 } // namespace blink | |
| OLD | NEW |