Home | History | Annotate | Download | only in canvas
      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 #include "config.h"
     27 #include "ArrayBufferView.h"
     28 
     29 #include "ArrayBuffer.h"
     30 
     31 namespace WebCore {
     32 
     33 ArrayBufferView::ArrayBufferView(PassRefPtr<ArrayBuffer> buffer,
     34                        unsigned byteOffset)
     35         : m_byteOffset(byteOffset)
     36         , m_buffer(buffer)
     37 {
     38     m_baseAddress = m_buffer ? (static_cast<char*>(m_buffer->data()) + m_byteOffset) : 0;
     39 }
     40 
     41 ArrayBufferView::~ArrayBufferView()
     42 {
     43 }
     44 
     45 void ArrayBufferView::setImpl(ArrayBufferView* array, unsigned byteOffset, ExceptionCode& ec)
     46 {
     47     if (byteOffset > byteLength()
     48         || byteOffset + array->byteLength() > byteLength()
     49         || byteOffset + array->byteLength() < byteOffset) {
     50         // Out of range offset or overflow
     51         ec = INDEX_SIZE_ERR;
     52         return;
     53     }
     54 
     55     char* base = static_cast<char*>(baseAddress());
     56     memmove(base + byteOffset, array->baseAddress(), array->byteLength());
     57 }
     58 
     59 void ArrayBufferView::setRangeImpl(const char* data, size_t dataByteLength, unsigned byteOffset, ExceptionCode& ec)
     60 {
     61     if (byteOffset > byteLength()
     62         || byteOffset + dataByteLength > byteLength()
     63         || byteOffset + dataByteLength < byteOffset) {
     64         // Out of range offset or overflow
     65         ec = INDEX_SIZE_ERR;
     66         return;
     67     }
     68 
     69     char* base = static_cast<char*>(baseAddress());
     70     memmove(base + byteOffset, data, dataByteLength);
     71 }
     72 
     73 void ArrayBufferView::zeroRangeImpl(unsigned byteOffset, size_t rangeByteLength, ExceptionCode& ec)
     74 {
     75     if (byteOffset > byteLength()
     76         || byteOffset + rangeByteLength > byteLength()
     77         || byteOffset + rangeByteLength < byteOffset) {
     78         // Out of range offset or overflow
     79         ec = INDEX_SIZE_ERR;
     80         return;
     81     }
     82 
     83     char* base = static_cast<char*>(baseAddress());
     84     memset(base + byteOffset, 0, rangeByteLength);
     85 }
     86 
     87 void ArrayBufferView::calculateOffsetAndLength(int start, int end, unsigned arraySize,
     88                                           unsigned* offset, unsigned* length)
     89 {
     90     if (start < 0)
     91         start += arraySize;
     92     if (start < 0)
     93         start = 0;
     94     if (end < 0)
     95         end += arraySize;
     96     if (end < 0)
     97         end = 0;
     98     if (end < start)
     99         end = start;
    100     *offset = static_cast<unsigned>(start);
    101     *length = static_cast<unsigned>(end - start);
    102 }
    103 
    104 }
    105