|
1 /* |
|
2 * Copyright (C) 2009 Apple 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 * 1. Redistributions of source code must retain the above copyright |
|
8 * notice, this list of conditions and the following disclaimer. |
|
9 * 2. Redistributions in binary form must reproduce the above copyright |
|
10 * notice, this list of conditions and the following disclaimer in the |
|
11 * documentation and/or other materials provided with the distribution. |
|
12 * |
|
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
|
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
|
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
24 */ |
|
25 |
|
26 #ifndef ArrayBufferView_h |
|
27 #define ArrayBufferView_h |
|
28 |
|
29 #include <algorithm> |
|
30 #include "ExceptionCode.h" |
|
31 #include <limits.h> |
|
32 #include <wtf/PassRefPtr.h> |
|
33 #include <wtf/RefCounted.h> |
|
34 #include <wtf/RefPtr.h> |
|
35 #include "ArrayBuffer.h" |
|
36 |
|
37 namespace WebCore { |
|
38 |
|
39 class ArrayBufferView : public RefCounted<ArrayBufferView> { |
|
40 public: |
|
41 virtual bool isByteArray() const { return false; } |
|
42 virtual bool isUnsignedByteArray() const { return false; } |
|
43 virtual bool isShortArray() const { return false; } |
|
44 virtual bool isUnsignedShortArray() const { return false; } |
|
45 virtual bool isIntArray() const { return false; } |
|
46 virtual bool isUnsignedIntArray() const { return false; } |
|
47 virtual bool isFloatArray() const { return false; } |
|
48 |
|
49 PassRefPtr<ArrayBuffer> buffer() const |
|
50 { |
|
51 return m_buffer; |
|
52 } |
|
53 |
|
54 void* baseAddress() const |
|
55 { |
|
56 return m_baseAddress; |
|
57 } |
|
58 |
|
59 unsigned byteOffset() const |
|
60 { |
|
61 return m_byteOffset; |
|
62 } |
|
63 |
|
64 virtual unsigned length() const = 0; |
|
65 virtual unsigned byteLength() const = 0; |
|
66 virtual PassRefPtr<ArrayBufferView> slice(int start, int end) const = 0; |
|
67 |
|
68 virtual ~ArrayBufferView(); |
|
69 |
|
70 protected: |
|
71 ArrayBufferView(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset); |
|
72 |
|
73 void setImpl(ArrayBufferView* array, unsigned byteOffset, ExceptionCode& ec); |
|
74 |
|
75 static void calculateOffsetAndLength(int start, int end, unsigned arraySize, |
|
76 unsigned* offset, unsigned* length); |
|
77 |
|
78 // Helper to verify that a given sub-range of an ArrayBuffer is |
|
79 // within range. |
|
80 template <typename T> |
|
81 static bool verifySubRange(PassRefPtr<ArrayBuffer> buffer, |
|
82 unsigned byteOffset, |
|
83 unsigned numElements) |
|
84 { |
|
85 if (!buffer) |
|
86 return false; |
|
87 if (sizeof(T) > 1 && byteOffset % sizeof(T)) |
|
88 return false; |
|
89 if (byteOffset > buffer->byteLength()) |
|
90 return false; |
|
91 unsigned remainingElements = (buffer->byteLength() - byteOffset) / sizeof(T); |
|
92 if (numElements > remainingElements) |
|
93 return false; |
|
94 return true; |
|
95 } |
|
96 |
|
97 // Input offset is in number of elements from this array's view; |
|
98 // output offset is in number of bytes from the underlying buffer's view. |
|
99 template <typename T> |
|
100 static void clampOffsetAndNumElements(PassRefPtr<ArrayBuffer> buffer, |
|
101 unsigned arrayByteOffset, |
|
102 unsigned *offset, |
|
103 unsigned *numElements) |
|
104 { |
|
105 unsigned maxOffset = (UINT_MAX - arrayByteOffset) / sizeof(T); |
|
106 if (*offset > maxOffset) { |
|
107 *offset = buffer->byteLength(); |
|
108 *numElements = 0; |
|
109 return; |
|
110 } |
|
111 *offset = arrayByteOffset + *offset * sizeof(T); |
|
112 *offset = std::min(buffer->byteLength(), *offset); |
|
113 unsigned remainingElements = (buffer->byteLength() - *offset) / sizeof(T); |
|
114 *numElements = std::min(remainingElements, *numElements); |
|
115 } |
|
116 |
|
117 // This is the address of the ArrayBuffer's storage, plus the byte offset. |
|
118 void* m_baseAddress; |
|
119 |
|
120 unsigned m_byteOffset; |
|
121 |
|
122 private: |
|
123 RefPtr<ArrayBuffer> m_buffer; |
|
124 }; |
|
125 |
|
126 } // namespace WebCore |
|
127 |
|
128 #endif // ArrayBufferView_h |