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 "ArrayBuffer.h"
30#include "ExceptionCode.h"
31
32#include <algorithm>
33#include <limits.h>
34#include <wtf/PassRefPtr.h>
35#include <wtf/RefCounted.h>
36#include <wtf/RefPtr.h>
37
38namespace WebCore {
39
40class ArrayBufferView : public RefCounted<ArrayBufferView> {
41  public:
42    virtual bool isByteArray() const { return false; }
43    virtual bool isUnsignedByteArray() const { return false; }
44    virtual bool isShortArray() const { return false; }
45    virtual bool isUnsignedShortArray() const { return false; }
46    virtual bool isIntArray() const { return false; }
47    virtual bool isUnsignedIntArray() const { return false; }
48    virtual bool isFloatArray() const { return false; }
49    virtual bool isDataView() const { return false; }
50
51    PassRefPtr<ArrayBuffer> buffer() const
52    {
53        return m_buffer;
54    }
55
56    void* baseAddress() const
57    {
58        return m_baseAddress;
59    }
60
61    unsigned byteOffset() const
62    {
63        return m_byteOffset;
64    }
65
66    virtual unsigned byteLength() 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    void setRangeImpl(const char* data, size_t dataByteLength, unsigned byteOffset, ExceptionCode& ec);
76
77    void zeroRangeImpl(unsigned byteOffset, size_t rangeByteLength, ExceptionCode& ec);
78
79    static void calculateOffsetAndLength(int start, int end, unsigned arraySize,
80                                         unsigned* offset, unsigned* length);
81
82    // Helper to verify that a given sub-range of an ArrayBuffer is
83    // within range.
84    template <typename T>
85    static bool verifySubRange(PassRefPtr<ArrayBuffer> buffer,
86                               unsigned byteOffset,
87                               unsigned numElements)
88    {
89        if (!buffer)
90            return false;
91        if (sizeof(T) > 1 && byteOffset % sizeof(T))
92            return false;
93        if (byteOffset > buffer->byteLength())
94            return false;
95        unsigned remainingElements = (buffer->byteLength() - byteOffset) / sizeof(T);
96        if (numElements > remainingElements)
97            return false;
98        return true;
99    }
100
101    // Input offset is in number of elements from this array's view;
102    // output offset is in number of bytes from the underlying buffer's view.
103    template <typename T>
104    static void clampOffsetAndNumElements(PassRefPtr<ArrayBuffer> buffer,
105                                          unsigned arrayByteOffset,
106                                          unsigned *offset,
107                                          unsigned *numElements)
108    {
109        unsigned maxOffset = (UINT_MAX - arrayByteOffset) / sizeof(T);
110        if (*offset > maxOffset) {
111            *offset = buffer->byteLength();
112            *numElements = 0;
113            return;
114        }
115        *offset = arrayByteOffset + *offset * sizeof(T);
116        *offset = std::min(buffer->byteLength(), *offset);
117        unsigned remainingElements = (buffer->byteLength() - *offset) / sizeof(T);
118        *numElements = std::min(remainingElements, *numElements);
119    }
120
121    // This is the address of the ArrayBuffer's storage, plus the byte offset.
122    void* m_baseAddress;
123
124    unsigned m_byteOffset;
125
126  private:
127    RefPtr<ArrayBuffer> m_buffer;
128};
129
130} // namespace WebCore
131
132#endif // ArrayBufferView_h
133