| 1 | /* |
| 2 | * Copyright (C) 2008-2016 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2014 Adobe Systems Incorporated. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
| 15 | * its contributors may be used to endorse or promote products derived |
| 16 | * from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include "config.h" |
| 31 | #include "ImageData.h" |
| 32 | |
| 33 | #include <JavaScriptCore/JSCInlines.h> |
| 34 | #include <JavaScriptCore/TypedArrayInlines.h> |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | ExceptionOr<Ref<ImageData>> ImageData::create(unsigned sw, unsigned sh) |
| 39 | { |
| 40 | if (!sw || !sh) |
| 41 | return Exception { IndexSizeError }; |
| 42 | |
| 43 | Checked<int, RecordOverflow> dataSize = 4; |
| 44 | dataSize *= sw; |
| 45 | dataSize *= sh; |
| 46 | if (dataSize.hasOverflowed()) |
| 47 | return Exception { RangeError, "Cannot allocate a buffer of this size"_s }; |
| 48 | |
| 49 | IntSize size(sw, sh); |
| 50 | auto data = adoptRef(*new ImageData(size)); |
| 51 | data->data()->zeroFill(); |
| 52 | return data; |
| 53 | } |
| 54 | |
| 55 | RefPtr<ImageData> ImageData::create(const IntSize& size) |
| 56 | { |
| 57 | Checked<int, RecordOverflow> dataSize = 4; |
| 58 | dataSize *= size.width(); |
| 59 | dataSize *= size.height(); |
| 60 | if (dataSize.hasOverflowed()) |
| 61 | return nullptr; |
| 62 | |
| 63 | return adoptRef(*new ImageData(size)); |
| 64 | } |
| 65 | |
| 66 | RefPtr<ImageData> ImageData::create(const IntSize& size, Ref<Uint8ClampedArray>&& byteArray) |
| 67 | { |
| 68 | Checked<int, RecordOverflow> dataSize = 4; |
| 69 | dataSize *= size.width(); |
| 70 | dataSize *= size.height(); |
| 71 | if (dataSize.hasOverflowed()) |
| 72 | return nullptr; |
| 73 | |
| 74 | if (dataSize.unsafeGet() < 0 || static_cast<unsigned>(dataSize.unsafeGet()) > byteArray->length()) |
| 75 | return nullptr; |
| 76 | |
| 77 | return adoptRef(*new ImageData(size, WTFMove(byteArray))); |
| 78 | } |
| 79 | |
| 80 | ExceptionOr<RefPtr<ImageData>> ImageData::create(Ref<Uint8ClampedArray>&& byteArray, unsigned sw, Optional<unsigned> sh) |
| 81 | { |
| 82 | unsigned length = byteArray->length(); |
| 83 | if (!length || length % 4) |
| 84 | return Exception { InvalidStateError, "Length is not a non-zero multiple of 4"_s }; |
| 85 | |
| 86 | ASSERT(length > 0); |
| 87 | length /= 4; |
| 88 | if (!sw || length % sw) |
| 89 | return Exception { IndexSizeError, "Length is not a multiple of sw"_s }; |
| 90 | |
| 91 | unsigned height = length / sw; |
| 92 | if (sh && sh.value() != height) |
| 93 | return Exception { IndexSizeError, "sh value is not equal to height"_s }; |
| 94 | |
| 95 | return create(IntSize(sw, height), WTFMove(byteArray)); |
| 96 | } |
| 97 | |
| 98 | ImageData::ImageData(const IntSize& size) |
| 99 | : m_size(size) |
| 100 | , m_data(Uint8ClampedArray::createUninitialized((size.area() * 4).unsafeGet())) |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | ImageData::ImageData(const IntSize& size, Ref<Uint8ClampedArray>&& byteArray) |
| 105 | : m_size(size) |
| 106 | , m_data(WTFMove(byteArray)) |
| 107 | { |
| 108 | RELEASE_ASSERT_WITH_SECURITY_IMPLICATION((size.area() * 4).unsafeGet() <= m_data->length()); |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | |