3db06d634430b73598bf1db4824dd5cd8f500a22
[framework/web/webkit-efl.git] / Source / WebCore / html / canvas / DataView.cpp
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 "DataView.h"
28
29 #include "CheckedInt.h"
30
31 namespace {
32
33 template<typename T>
34 union Value {
35     T data;
36     char bytes[sizeof(T)];
37 };
38
39 }
40
41 namespace WebCore {
42
43 PassRefPtr<DataView> DataView::create(unsigned length)
44 {
45     RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(length, sizeof(uint8_t));
46     if (!buffer.get())
47         return 0;
48     return create(buffer, 0, length);
49 }
50
51 PassRefPtr<DataView> DataView::create(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset, unsigned byteLength)
52 {
53     if (byteOffset > buffer->byteLength())
54         return 0;
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())
59         return 0;
60     return adoptRef(new DataView(buffer, byteOffset, byteLength));
61 }
62
63 DataView::DataView(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset, unsigned byteLength)
64     : ArrayBufferView(buffer, byteOffset)
65     , m_byteLength(byteLength)
66 {
67 }
68
69 static bool needToFlipBytes(bool littleEndian)
70 {
71 #if CPU(BIG_ENDIAN)
72     return littleEndian;
73 #else
74     return !littleEndian;
75 #endif
76 }
77
78 inline void swapBytes(char* p, char* q)
79 {
80     char temp = *p;
81     *p = *q;
82     *q = temp;
83 }
84
85 static void flipBytesFor16Bits(char* p)
86 {
87     swapBytes(p, p + 1);
88 }
89
90 static void flipBytesFor32Bits(char* p)
91 {
92     swapBytes(p, p + 3);
93     swapBytes(p + 1, p + 2);
94 }
95
96 static void flipBytesFor64Bits(char* p)
97 {
98     swapBytes(p, p + 7);
99     swapBytes(p + 1, p + 6);
100     swapBytes(p + 2, p + 5);
101     swapBytes(p + 3, p + 4);
102 }
103
104 static void flipBytesIfNeeded(char* value, size_t size, bool littleEndian)
105 {
106     if (!needToFlipBytes(littleEndian))
107         return;
108
109     switch (size) {
110     case 1:
111         // Nothing to do.
112         break;
113     case 2:
114         flipBytesFor16Bits(value);
115         break;
116     case 4:
117         flipBytesFor32Bits(value);
118         break;
119     case 8:
120         flipBytesFor64Bits(value);
121         break;
122     default:
123         ASSERT_NOT_REACHED();
124         break;
125     }
126 }
127
128 template<typename T>
129 T DataView::getData(unsigned byteOffset, bool littleEndian, ExceptionCode& ec) const
130 {
131     if (beyondRange<T>(byteOffset)) {
132         ec = INDEX_SIZE_ERR;
133         return 0;
134     }
135
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.
137     Value<T> value;
138     memcpy(value.bytes, static_cast<const char*>(m_baseAddress) + byteOffset, sizeof(T));
139     flipBytesIfNeeded(value.bytes, sizeof(T), littleEndian);
140     return value.data;
141 }
142
143 template<typename T>
144 void DataView::setData(unsigned byteOffset, T value, bool littleEndian, ExceptionCode& ec)
145 {
146     if (beyondRange<T>(byteOffset)) {
147         ec = INDEX_SIZE_ERR;
148         return;
149     }
150
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.
152     Value<T> tempValue;
153     tempValue.data = value;
154     flipBytesIfNeeded(tempValue.bytes, sizeof(T), littleEndian);
155     memcpy(static_cast<char*>(m_baseAddress) + byteOffset, tempValue.bytes, sizeof(T));
156 }
157
158 int8_t DataView::getInt8(unsigned byteOffset, ExceptionCode& ec)
159 {
160     return getData<int8_t>(byteOffset, false, ec);
161 }
162
163 uint8_t DataView::getUint8(unsigned byteOffset, ExceptionCode& ec)
164 {
165     return getData<uint8_t>(byteOffset, false, ec);
166 }
167
168 int16_t DataView::getInt16(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
169 {
170     return getData<int16_t>(byteOffset, littleEndian, ec);
171 }
172
173 uint16_t DataView::getUint16(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
174 {
175     return getData<uint16_t>(byteOffset, littleEndian, ec);
176 }
177
178 int32_t DataView::getInt32(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
179 {
180     return getData<int32_t>(byteOffset, littleEndian, ec);
181 }
182
183 uint32_t DataView::getUint32(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
184 {
185     return getData<uint32_t>(byteOffset, littleEndian, ec);
186 }
187
188 float DataView::getFloat32(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
189 {
190     return getData<float>(byteOffset, littleEndian, ec);
191 }
192
193 double DataView::getFloat64(unsigned byteOffset, bool littleEndian, ExceptionCode& ec)
194 {
195     return getData<double>(byteOffset, littleEndian, ec);
196 }
197
198 void DataView::setInt8(unsigned byteOffset, int8_t value, ExceptionCode& ec)
199 {
200     setData<int8_t>(byteOffset, value, false, ec);
201 }
202
203 void DataView::setUint8(unsigned byteOffset, uint8_t value, ExceptionCode& ec)
204 {
205     setData<uint8_t>(byteOffset, value, false, ec);
206 }
207
208 void DataView::setInt16(unsigned byteOffset, short value, bool littleEndian, ExceptionCode& ec)
209 {
210     setData<int16_t>(byteOffset, value, littleEndian, ec);
211 }
212
213 void DataView::setUint16(unsigned byteOffset, uint16_t value, bool littleEndian, ExceptionCode& ec)
214 {
215     setData<uint16_t>(byteOffset, value, littleEndian, ec);
216 }
217
218 void DataView::setInt32(unsigned byteOffset, int32_t value, bool littleEndian, ExceptionCode& ec)
219 {
220     setData<int32_t>(byteOffset, value, littleEndian, ec);
221 }
222
223 void DataView::setUint32(unsigned byteOffset, uint32_t value, bool littleEndian, ExceptionCode& ec)
224 {
225     setData<uint32_t>(byteOffset, value, littleEndian, ec);
226 }
227
228 void DataView::setFloat32(unsigned byteOffset, float value, bool littleEndian, ExceptionCode& ec)
229 {
230     setData<float>(byteOffset, value, littleEndian, ec);
231 }
232
233 void DataView::setFloat64(unsigned byteOffset, double value, bool littleEndian, ExceptionCode& ec)
234 {
235     setData<double>(byteOffset, value, littleEndian, ec);
236 }
237
238 }