|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Devin Rousso <[email protected]>. 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 INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | + * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +#include "config.h" |
| 27 | +#include "JSGenericTypedArrayViewPrototype.h" |
| 28 | + |
| 29 | +#include "ParseInt.h" |
| 30 | +#include <wtf/text/Base64.h> |
| 31 | + |
| 32 | +namespace JSC { |
| 33 | + |
| 34 | +JSC_DEFINE_HOST_FUNCTION(uint8ArrayPrototypeToBase64, (JSGlobalObject* globalObject, CallFrame* callFrame)) |
| 35 | +{ |
| 36 | + VM& vm = globalObject->vm(); |
| 37 | + auto scope = DECLARE_THROW_SCOPE(vm); |
| 38 | + |
| 39 | + JSUint8Array* uint8Array = jsDynamicCast<JSUint8Array*>(callFrame->thisValue()); |
| 40 | + if (UNLIKELY(!uint8Array)) |
| 41 | + return throwVMTypeError(globalObject, scope, "Uint8Array.prototype.toBase64 requires that |this| be a Uint8Array"_s); |
| 42 | + |
| 43 | + OptionSet<Base64EncodeOption> options; |
| 44 | + |
| 45 | + JSValue optionsValue = callFrame->argument(0); |
| 46 | + if (!optionsValue.isUndefined()) { |
| 47 | + JSObject* optionsObject = jsDynamicCast<JSObject*>(optionsValue); |
| 48 | + if (UNLIKELY(!optionsObject)) |
| 49 | + return throwVMTypeError(globalObject, scope, "Uint8Array.prototype.toBase64 requires that options be an object"_s); |
| 50 | + |
| 51 | + JSValue alphabetValue = optionsObject->get(globalObject, vm.propertyNames->alphabet); |
| 52 | + RETURN_IF_EXCEPTION(scope, { }); |
| 53 | + if (!alphabetValue.isUndefined()) { |
| 54 | + JSString* alphabetString = jsDynamicCast<JSString*>(alphabetValue); |
| 55 | + if (UNLIKELY(!alphabetString)) |
| 56 | + return throwVMTypeError(globalObject, scope, "Uint8Array.prototype.toBase64 requires that alphabet be \"base64\" or \"base64url\""_s); |
| 57 | + |
| 58 | + StringView alphabetStringView = alphabetString->view(globalObject); |
| 59 | + if (alphabetStringView == "base64url"_s) |
| 60 | + options.add(Base64EncodeOption::URL); |
| 61 | + else if (alphabetStringView != "base64"_s) |
| 62 | + return throwVMTypeError(globalObject, scope, "Uint8Array.prototype.toBase64 requires that alphabet be \"base64\" or \"base64url\""_s); |
| 63 | + } |
| 64 | + |
| 65 | + JSValue omitPaddingValue = optionsObject->get(globalObject, vm.propertyNames->omitPadding); |
| 66 | + RETURN_IF_EXCEPTION(scope, { }); |
| 67 | + if (omitPaddingValue.toBoolean(globalObject)) |
| 68 | + options.add(Base64EncodeOption::OmitPadding); |
| 69 | + } |
| 70 | + |
| 71 | + IdempotentArrayBufferByteLengthGetter<std::memory_order_seq_cst> byteLengthGetter; |
| 72 | + if (UNLIKELY(isIntegerIndexedObjectOutOfBounds(uint8Array, byteLengthGetter))) |
| 73 | + return throwVMTypeError(globalObject, scope, typedArrayBufferHasBeenDetachedErrorMessage); |
| 74 | + |
| 75 | + const uint8_t* data = uint8Array->typedVector(); |
| 76 | + size_t length = uint8Array->length(); |
| 77 | + RELEASE_AND_RETURN(scope, JSValue::encode(jsString(vm, base64EncodeToString({ data, length }, options)))); |
| 78 | +} |
| 79 | + |
| 80 | +JSC_DEFINE_HOST_FUNCTION(uint8ArrayPrototypeToHex, (JSGlobalObject* globalObject, CallFrame* callFrame)) |
| 81 | +{ |
| 82 | + VM& vm = globalObject->vm(); |
| 83 | + auto scope = DECLARE_THROW_SCOPE(vm); |
| 84 | + |
| 85 | + JSUint8Array* uint8Array = jsDynamicCast<JSUint8Array*>(callFrame->thisValue()); |
| 86 | + if (UNLIKELY(!uint8Array)) |
| 87 | + return throwVMTypeError(globalObject, scope, "Uint8Array.prototype.toHex requires that |this| be a Uint8Array"_s); |
| 88 | + |
| 89 | + IdempotentArrayBufferByteLengthGetter<std::memory_order_seq_cst> byteLengthGetter; |
| 90 | + if (UNLIKELY(isIntegerIndexedObjectOutOfBounds(uint8Array, byteLengthGetter))) |
| 91 | + return throwVMTypeError(globalObject, scope, typedArrayBufferHasBeenDetachedErrorMessage); |
| 92 | + |
| 93 | + StringBuilder builder; |
| 94 | + builder.reserveCapacity(uint8Array->length() * 2); |
| 95 | + |
| 96 | + const uint8_t* data = uint8Array->typedVector(); |
| 97 | + size_t length = uint8Array->length(); |
| 98 | + for (size_t i = 0; i < length; ++i) { |
| 99 | + builder.append(radixDigits[data[i] / 16]); |
| 100 | + builder.append(radixDigits[data[i] % 16]); |
| 101 | + } |
| 102 | + RELEASE_AND_RETURN(scope, JSValue::encode(jsString(vm, builder.toString()))); |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace JSC |
0 commit comments