Uint8Array.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Base64.h>
  7. #include <AK/StringBuilder.h>
  8. #include <AK/StringUtils.h>
  9. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  10. #include <LibJS/Runtime/TypedArray.h>
  11. #include <LibJS/Runtime/Uint8Array.h>
  12. #include <LibJS/Runtime/VM.h>
  13. #include <LibJS/Runtime/ValueInlines.h>
  14. namespace JS {
  15. void Uint8ArrayConstructorHelpers::initialize(Realm& realm, Object& constructor)
  16. {
  17. auto& vm = constructor.vm();
  18. static constexpr u8 attr = Attribute::Writable | Attribute::Configurable;
  19. constructor.define_native_function(realm, vm.names.fromBase64, from_base64, 1, attr);
  20. constructor.define_native_function(realm, vm.names.fromHex, from_hex, 1, attr);
  21. }
  22. void Uint8ArrayPrototypeHelpers::initialize(Realm& realm, Object& prototype)
  23. {
  24. auto& vm = prototype.vm();
  25. static constexpr u8 attr = Attribute::Writable | Attribute::Configurable;
  26. prototype.define_native_function(realm, vm.names.toBase64, to_base64, 0, attr);
  27. prototype.define_native_function(realm, vm.names.toHex, to_hex, 0, attr);
  28. prototype.define_native_function(realm, vm.names.setFromBase64, set_from_base64, 1, attr);
  29. }
  30. static ThrowCompletionOr<Alphabet> parse_alphabet(VM& vm, Object& options)
  31. {
  32. // Let alphabet be ? Get(opts, "alphabet").
  33. auto alphabet = TRY(options.get(vm.names.alphabet));
  34. // If alphabet is undefined, set alphabet to "base64".
  35. if (alphabet.is_undefined())
  36. return Alphabet::Base64;
  37. // If alphabet is neither "base64" nor "base64url", throw a TypeError exception.
  38. if (alphabet.is_string()) {
  39. if (alphabet.as_string().utf8_string_view() == "base64"sv)
  40. return Alphabet::Base64;
  41. if (alphabet.as_string().utf8_string_view() == "base64url"sv)
  42. return Alphabet::Base64URL;
  43. }
  44. return vm.throw_completion<TypeError>(ErrorType::OptionIsNotValidValue, alphabet, "alphabet"sv);
  45. }
  46. static ThrowCompletionOr<LastChunkHandling> parse_last_chunk_handling(VM& vm, Object& options)
  47. {
  48. // Let lastChunkHandling be ? Get(opts, "lastChunkHandling").
  49. auto last_chunk_handling = TRY(options.get(vm.names.lastChunkHandling));
  50. // If lastChunkHandling is undefined, set lastChunkHandling to "loose".
  51. if (last_chunk_handling.is_undefined())
  52. return LastChunkHandling::Loose;
  53. // If lastChunkHandling is not one of "loose", "strict", or "stop-before-partial", throw a TypeError exception.
  54. if (last_chunk_handling.is_string()) {
  55. if (last_chunk_handling.as_string().utf8_string_view() == "loose"sv)
  56. return LastChunkHandling::Loose;
  57. if (last_chunk_handling.as_string().utf8_string_view() == "strict"sv)
  58. return LastChunkHandling::Strict;
  59. if (last_chunk_handling.as_string().utf8_string_view() == "stop-before-partial"sv)
  60. return LastChunkHandling::StopBeforePartial;
  61. }
  62. return vm.throw_completion<TypeError>(ErrorType::OptionIsNotValidValue, last_chunk_handling, "lastChunkHandling"sv);
  63. }
  64. // 1 Uint8Array.prototype.toBase64 ( [ options ] ), https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tobase64
  65. JS_DEFINE_NATIVE_FUNCTION(Uint8ArrayPrototypeHelpers::to_base64)
  66. {
  67. auto options_value = vm.argument(0);
  68. // 1. Let O be the this value.
  69. // 2. Perform ? ValidateUint8Array(O).
  70. auto typed_array = TRY(validate_uint8_array(vm));
  71. // 3. Let opts be ? GetOptionsObject(options).
  72. auto* options = TRY(Temporal::get_options_object(vm, options_value));
  73. // 4. Let alphabet be ? Get(opts, "alphabet").
  74. // 5. If alphabet is undefined, set alphabet to "base64".
  75. // 6. If alphabet is neither "base64" nor "base64url", throw a TypeError exception.
  76. auto alphabet = TRY(parse_alphabet(vm, *options));
  77. // 7. Let omitPadding be ToBoolean(? Get(opts, "omitPadding")).
  78. auto omit_padding_value = TRY(options->get(vm.names.omitPadding)).to_boolean();
  79. auto omit_padding = omit_padding_value ? AK::OmitPadding::Yes : AK::OmitPadding::No;
  80. // 8. Let toEncode be ? GetUint8ArrayBytes(O).
  81. auto to_encode = TRY(get_uint8_array_bytes(vm, typed_array));
  82. String out_ascii;
  83. // 9. If alphabet is "base64", then
  84. if (alphabet == Alphabet::Base64) {
  85. // a. Let outAscii be the sequence of code points which results from encoding toEncode according to the base64
  86. // encoding specified in section 4 of RFC 4648. Padding is included if and only if omitPadding is false.
  87. out_ascii = MUST(encode_base64(to_encode, omit_padding));
  88. }
  89. // 10. Else,
  90. else {
  91. // a. Assert: alphabet is "base64url".
  92. // b. Let outAscii be the sequence of code points which results from encoding toEncode according to the base64url
  93. // encoding specified in section 5 of RFC 4648. Padding is included if and only if omitPadding is false.
  94. out_ascii = MUST(encode_base64url(to_encode, omit_padding));
  95. }
  96. // 11. Return CodePointsToString(outAscii).
  97. return PrimitiveString::create(vm, move(out_ascii));
  98. }
  99. // 2 Uint8Array.prototype.toHex ( ), https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tobase64
  100. JS_DEFINE_NATIVE_FUNCTION(Uint8ArrayPrototypeHelpers::to_hex)
  101. {
  102. // 1. Let O be the this value.
  103. // 2. Perform ? ValidateUint8Array(O).
  104. auto typed_array = TRY(validate_uint8_array(vm));
  105. // 3. Let toEncode be ? GetUint8ArrayBytes(O).
  106. auto to_encode = TRY(get_uint8_array_bytes(vm, typed_array));
  107. // 4. Let out be the empty String.
  108. StringBuilder out;
  109. // 5. For each byte byte of toEncode, do
  110. for (auto byte : to_encode.bytes()) {
  111. // a. Let hex be Number::toString(𝔽(byte), 16).
  112. // b. Set hex to StringPad(hex, 2, "0", START).
  113. // c. Set out to the string-concatenation of out and hex.
  114. out.appendff("{:02x}", byte);
  115. }
  116. // 6. Return out.
  117. return PrimitiveString::create(vm, MUST(out.to_string()));
  118. }
  119. // 3 Uint8Array.fromBase64 ( string [ , options ] ), https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.frombase64
  120. JS_DEFINE_NATIVE_FUNCTION(Uint8ArrayConstructorHelpers::from_base64)
  121. {
  122. auto& realm = *vm.current_realm();
  123. auto string_value = vm.argument(0);
  124. auto options_value = vm.argument(1);
  125. // 1. If string is not a String, throw a TypeError exception.
  126. if (!string_value.is_string())
  127. return vm.throw_completion<TypeError>(ErrorType::NotAString, string_value);
  128. // 2. Let opts be ? GetOptionsObject(options).
  129. auto* options = TRY(Temporal::get_options_object(vm, options_value));
  130. // 3. Let alphabet be ? Get(opts, "alphabet").
  131. // 4. If alphabet is undefined, set alphabet to "base64".
  132. // 5. If alphabet is neither "base64" nor "base64url", throw a TypeError exception.
  133. auto alphabet = TRY(parse_alphabet(vm, *options));
  134. // 6. Let lastChunkHandling be ? Get(opts, "lastChunkHandling").
  135. // 7. If lastChunkHandling is undefined, set lastChunkHandling to "loose".
  136. // 8. If lastChunkHandling is not one of "loose", "strict", or "stop-before-partial", throw a TypeError exception.
  137. auto last_chunk_handling = TRY(parse_last_chunk_handling(vm, *options));
  138. // 9. Let result be FromBase64(string, alphabet, lastChunkHandling).
  139. auto result = JS::from_base64(vm, string_value.as_string().utf8_string_view(), alphabet, last_chunk_handling);
  140. // 10. If result.[[Error]] is not none, then
  141. if (result.error.has_value()) {
  142. // a. Throw result.[[Error]].
  143. return result.error.release_value();
  144. }
  145. // 11. Let resultLength be the length of result.[[Bytes]].
  146. auto result_length = result.bytes.size();
  147. // 12. Let ta be ? AllocateTypedArray("Uint8Array", %Uint8Array%, "%Uint8Array.prototype%", resultLength).
  148. auto typed_array = TRY(Uint8Array::create(realm, result_length));
  149. // 13. Set the value at each index of ta.[[ViewedArrayBuffer]].[[ArrayBufferData]] to the value at the corresponding
  150. // index of result.[[Bytes]].
  151. auto& array_buffer_data = typed_array->viewed_array_buffer()->buffer();
  152. for (size_t index = 0; index < result_length; ++index)
  153. array_buffer_data[index] = result.bytes[index];
  154. // 14. Return ta.
  155. return typed_array;
  156. }
  157. // 4 Uint8Array.prototype.setFromBase64 ( string [ , options ] ), https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.setfrombase64
  158. JS_DEFINE_NATIVE_FUNCTION(Uint8ArrayPrototypeHelpers::set_from_base64)
  159. {
  160. auto& realm = *vm.current_realm();
  161. auto string_value = vm.argument(0);
  162. auto options_value = vm.argument(1);
  163. // 1. Let into be the this value.
  164. // 2. Perform ? ValidateUint8Array(into).
  165. auto into = TRY(validate_uint8_array(vm));
  166. // 3. If string is not a String, throw a TypeError exception.
  167. if (!string_value.is_string())
  168. return vm.throw_completion<TypeError>(ErrorType::NotAString, string_value);
  169. // 4. Let opts be ? GetOptionsObject(options).
  170. auto* options = TRY(Temporal::get_options_object(vm, options_value));
  171. // 5. Let alphabet be ? Get(opts, "alphabet").
  172. // 6. If alphabet is undefined, set alphabet to "base64".
  173. // 7. If alphabet is neither "base64" nor "base64url", throw a TypeError exception.
  174. auto alphabet = TRY(parse_alphabet(vm, *options));
  175. // 8. Let lastChunkHandling be ? Get(opts, "lastChunkHandling").
  176. // 9. If lastChunkHandling is undefined, set lastChunkHandling to "loose".
  177. // 10. If lastChunkHandling is not one of "loose", "strict", or "stop-before-partial", throw a TypeError exception.
  178. auto last_chunk_handling = TRY(parse_last_chunk_handling(vm, *options));
  179. // 11. Let taRecord be MakeTypedArrayWithBufferWitnessRecord(into, seq-cst).
  180. auto typed_array_record = make_typed_array_with_buffer_witness_record(into, ArrayBuffer::Order::SeqCst);
  181. // 12. If IsTypedArrayOutOfBounds(taRecord) is true, throw a TypeError exception.
  182. if (is_typed_array_out_of_bounds(typed_array_record))
  183. return vm.throw_completion<TypeError>(ErrorType::BufferOutOfBounds, "TypedArray"sv);
  184. // 13. Let byteLength be TypedArrayLength(taRecord).
  185. auto byte_length = typed_array_length(typed_array_record);
  186. // 14. Let result be FromBase64(string, alphabet, lastChunkHandling, byteLength).
  187. auto result = JS::from_base64(vm, string_value.as_string().utf8_string_view(), alphabet, last_chunk_handling, byte_length);
  188. // 15. Let bytes be result.[[Bytes]].
  189. auto bytes = move(result.bytes);
  190. // 16. Let written be the length of bytes.
  191. auto written = bytes.size();
  192. // 17. NOTE: FromBase64 does not invoke any user code, so the ArrayBuffer backing into cannot have been detached or shrunk.
  193. // 18. Assert: written ≤ byteLength.
  194. VERIFY(written <= byte_length);
  195. // 19. Perform SetUint8ArrayBytes(into, bytes).
  196. set_uint8_array_bytes(into, bytes);
  197. // 20. If result.[[Error]] is not none, then
  198. if (result.error.has_value()) {
  199. // a. Throw result.[[Error]].
  200. return result.error.release_value();
  201. }
  202. // 21. Let resultObject be OrdinaryObjectCreate(%Object.prototype%).
  203. auto result_object = Object::create(realm, realm.intrinsics().object_prototype());
  204. // 22. Perform ! CreateDataPropertyOrThrow(resultObject, "read", 𝔽(result.[[Read]])).
  205. MUST(result_object->create_data_property(vm.names.read, Value { result.read }));
  206. // 23. Perform ! CreateDataPropertyOrThrow(resultObject, "written", 𝔽(written)).
  207. MUST(result_object->create_data_property(vm.names.written, Value { written }));
  208. // 24. Return resultObject.
  209. return result_object;
  210. }
  211. // 5 Uint8Array.fromHex ( string ), https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.fromhex
  212. JS_DEFINE_NATIVE_FUNCTION(Uint8ArrayConstructorHelpers::from_hex)
  213. {
  214. auto& realm = *vm.current_realm();
  215. auto string_value = vm.argument(0);
  216. // 1. If string is not a String, throw a TypeError exception.
  217. if (!string_value.is_string())
  218. return vm.throw_completion<TypeError>(ErrorType::NotAString, string_value);
  219. // 2. Let result be FromHex(string).
  220. auto result = JS::from_hex(vm, string_value.as_string().utf8_string_view());
  221. // 3. If result.[[Error]] is not none, then
  222. if (result.error.has_value()) {
  223. // a. Throw result.[[Error]].
  224. return result.error.release_value();
  225. }
  226. // 4. Let resultLength be the length of result.[[Bytes]].
  227. auto result_length = result.bytes.size();
  228. // 5. Let ta be ? AllocateTypedArray("Uint8Array", %Uint8Array%, "%Uint8Array.prototype%", resultLength).
  229. auto typed_array = TRY(Uint8Array::create(realm, result_length));
  230. // 6. Set the value at each index of ta.[[ViewedArrayBuffer]].[[ArrayBufferData]] to the value at the corresponding
  231. // index of result.[[Bytes]].
  232. auto& array_buffer_data = typed_array->viewed_array_buffer()->buffer();
  233. for (size_t index = 0; index < result_length; ++index)
  234. array_buffer_data[index] = result.bytes[index];
  235. // 7. Return ta.
  236. return typed_array;
  237. }
  238. // 7 ValidateUint8Array ( ta ), https://tc39.es/proposal-arraybuffer-base64/spec/#sec-validateuint8array
  239. ThrowCompletionOr<NonnullGCPtr<TypedArrayBase>> validate_uint8_array(VM& vm)
  240. {
  241. auto this_object = TRY(vm.this_value().to_object(vm));
  242. // 1. Perform ? RequireInternalSlot(ta, [[TypedArrayName]]).
  243. if (!this_object->is_typed_array())
  244. return vm.throw_completion<TypeError>(ErrorType::NotAnObjectOfType, "Uint8Array");
  245. auto& typed_array = static_cast<TypedArrayBase&>(*this_object.ptr());
  246. // 2. If ta.[[TypedArrayName]] is not "Uint8Array", throw a TypeError exception.
  247. if (typed_array.kind() != TypedArrayBase::Kind::Uint8Array)
  248. return vm.throw_completion<TypeError>(ErrorType::NotAnObjectOfType, "Uint8Array");
  249. // 3. Return UNUSED.
  250. return typed_array;
  251. }
  252. // 8 GetUint8ArrayBytes ( ta ), https://tc39.es/proposal-arraybuffer-base64/spec/#sec-getuint8arraybytes
  253. ThrowCompletionOr<ByteBuffer> get_uint8_array_bytes(VM& vm, TypedArrayBase const& typed_array)
  254. {
  255. // 1. Let buffer be ta.[[ViewedArrayBuffer]].
  256. // 2. Let taRecord be MakeTypedArrayWithBufferWitnessRecord(ta, SEQ-CST).
  257. auto typed_array_record = make_typed_array_with_buffer_witness_record(typed_array, ArrayBuffer::Order::SeqCst);
  258. // 3. If IsTypedArrayOutOfBounds(taRecord) is true, throw a TypeError exception.
  259. if (is_typed_array_out_of_bounds(typed_array_record))
  260. return vm.throw_completion<TypeError>(ErrorType::BufferOutOfBounds, "TypedArray"sv);
  261. // 4. Let len be TypedArrayLength(taRecord).
  262. auto length = typed_array_length(typed_array_record);
  263. // 5. Let byteOffset be ta.[[ByteOffset]].
  264. auto byte_offset = typed_array.byte_offset();
  265. // 6. Let bytes be a new empty List.
  266. ByteBuffer bytes;
  267. // 7. Let index be 0.
  268. // 8. Repeat, while index < len,
  269. for (u32 index = 0; index < length; ++index) {
  270. // a. Let byteIndex be byteOffset + index.
  271. auto byte_index = byte_offset + index;
  272. // b. Let byte be ℝ(GetValueFromBuffer(buffer, byteIndex, UINT8, true, UNORDERED)).
  273. auto byte = typed_array.get_value_from_buffer(byte_index, ArrayBuffer::Order::Unordered);
  274. // c. Append byte to bytes.
  275. bytes.append(MUST(byte.to_u8(vm)));
  276. // d. Set index to index + 1.
  277. }
  278. // 9. Return bytes.
  279. return bytes;
  280. }
  281. // 9 SetUint8ArrayBytes ( into, bytes ), https://tc39.es/proposal-arraybuffer-base64/spec/#sec-writeuint8arraybytes
  282. void set_uint8_array_bytes(TypedArrayBase& into, ReadonlyBytes bytes)
  283. {
  284. // 1. Let offset be into.[[ByteOffset]].
  285. auto offset = into.byte_offset();
  286. // 2. Let len be the length of bytes.
  287. auto length = bytes.size();
  288. // 3. Let index be 0.
  289. // 4. Repeat, while index < len,
  290. for (u32 index = 0; index < length; ++index) {
  291. // a. Let byte be bytes[index].
  292. auto byte = bytes[index];
  293. // b. Let byteIndexInBuffer be index + offset.
  294. auto byte_index_in_buffer = index + offset;
  295. // c. Perform SetValueInBuffer(into.[[ViewedArrayBuffer]], byteIndexInBuffer, uint8, 𝔽(byte), true, unordered).
  296. into.set_value_in_buffer(byte_index_in_buffer, Value { byte }, ArrayBuffer::Order::Unordered);
  297. // d. Set index to index + 1.
  298. }
  299. }
  300. // 10.1 SkipAsciiWhitespace ( string, index ), https://tc39.es/proposal-arraybuffer-base64/spec/#sec-skipasciiwhitespace
  301. static size_t skip_ascii_whitespace(StringView string, size_t index)
  302. {
  303. // 1. Let length be the length of string.
  304. auto length = string.length();
  305. // 2. Repeat, while index < length,
  306. while (index < length) {
  307. // a. Let char be the code unit at index index of string.
  308. auto ch = string[index];
  309. // b. If char is neither 0x0009 (TAB), 0x000A (LF), 0x000C (FF), 0x000D (CR), nor 0x0020 (SPACE), then
  310. if (ch != '\t' && ch != '\n' && ch != '\f' && ch != '\r' && ch != ' ') {
  311. // i. Return index.
  312. return index;
  313. }
  314. // c. Set index to index + 1.
  315. ++index;
  316. }
  317. // 3. Return index.
  318. return index;
  319. }
  320. // 10.2 DecodeBase64Chunk ( chunk [ , throwOnExtraBits ] ), https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64
  321. static ThrowCompletionOr<ByteBuffer> decode_base64_chunk(VM& vm, StringBuilder& chunk, Optional<bool> throw_on_extra_bits = {})
  322. {
  323. // 1. Let chunkLength be the length of chunk.
  324. auto chunk_length = chunk.length();
  325. // 2. If chunkLength is 2, then
  326. if (chunk_length == 2) {
  327. // a. Set chunk to the string-concatenation of chunk and "AA".
  328. chunk.append("AA"sv);
  329. }
  330. // 3. Else if chunkLength is 3, then
  331. else if (chunk_length == 3) {
  332. // a. Set chunk to the string-concatenation of chunk and "A".
  333. chunk.append("A"sv);
  334. }
  335. // 4. Else,
  336. else {
  337. // a. Assert: chunkLength is 4.
  338. VERIFY(chunk_length == 4);
  339. }
  340. // 5. Let byteSequence be the unique sequence of 3 bytes resulting from decoding chunk as base64 (such that applying
  341. // the base64 encoding specified in section 4 of RFC 4648 to byteSequence would produce chunk).
  342. // 6. Let bytes be a List whose elements are the elements of byteSequence, in order.
  343. auto bytes = MUST(decode_base64(chunk.string_view()));
  344. // 7. If chunkLength is 2, then
  345. if (chunk_length == 2) {
  346. // a. Assert: throwOnExtraBits is present.
  347. VERIFY(throw_on_extra_bits.has_value());
  348. // b. If throwOnExtraBits is true and bytes[1] ≠ 0, then
  349. if (*throw_on_extra_bits && bytes[1] != 0) {
  350. // i. Throw a SyntaxError exception.
  351. return vm.throw_completion<SyntaxError>("Extra bits found at end of chunk"sv);
  352. }
  353. // c. Return « bytes[0] ».
  354. return MUST(bytes.slice(0, 1));
  355. }
  356. // 8. Else if chunkLength is 3, then
  357. if (chunk_length == 3) {
  358. // a. Assert: throwOnExtraBits is present.
  359. VERIFY(throw_on_extra_bits.has_value());
  360. // b. If throwOnExtraBits is true and bytes[2] ≠ 0, then
  361. if (*throw_on_extra_bits && bytes[2] != 0) {
  362. // i. Throw a SyntaxError exception.
  363. return vm.throw_completion<SyntaxError>("Extra bits found at end of chunk"sv);
  364. }
  365. // c. Return « bytes[0], bytes[1] ».
  366. return MUST(bytes.slice(0, 2));
  367. }
  368. // 9. Else,
  369. // a. Return bytes.
  370. return bytes;
  371. }
  372. // 10.3 FromBase64 ( string, alphabet, lastChunkHandling [ , maxLength ] ), https://tc39.es/proposal-arraybuffer-base64/spec/#sec-frombase64
  373. DecodeResult from_base64(VM& vm, StringView string, Alphabet alphabet, LastChunkHandling last_chunk_handling, Optional<size_t> max_length)
  374. {
  375. // FIXME: We can only use simdutf when the last-chunk-handling parameter is "loose". Upstream is planning to implement
  376. // the remaining options. When that is complete, we should be able to remove the slow implementation below. See:
  377. // https://github.com/simdutf/simdutf/issues/440
  378. if (last_chunk_handling == LastChunkHandling::Loose) {
  379. auto output = MUST(ByteBuffer::create_uninitialized(max_length.value_or_lazy_evaluated([&]() {
  380. return AK::size_required_to_decode_base64(string);
  381. })));
  382. auto result = alphabet == Alphabet::Base64
  383. ? AK::decode_base64_into(string, output)
  384. : AK::decode_base64url_into(string, output);
  385. if (result.is_error()) {
  386. auto error = vm.throw_completion<SyntaxError>(result.error().error.string_literal());
  387. return { .read = result.error().valid_input_bytes, .bytes = move(output), .error = move(error) };
  388. }
  389. return { .read = result.value(), .bytes = move(output), .error = {} };
  390. }
  391. // 1. If maxLength is not present, then
  392. if (!max_length.has_value()) {
  393. // a. Let maxLength be 2**53 - 1.
  394. max_length = MAX_ARRAY_LIKE_INDEX;
  395. // b. NOTE: Because the input is a string, the length of strings is limited to 2**53 - 1 characters, and the
  396. // output requires no more bytes than the input has characters, this limit can never be reached. However, it
  397. // is editorially convenient to use a finite value here.
  398. }
  399. // 2. NOTE: The order of validation and decoding in the algorithm below is not observable. Implementations are
  400. // encouraged to perform them in whatever order is most efficient, possibly interleaving validation with decoding,
  401. // as long as the behaviour is observably equivalent.
  402. // 3. If maxLength is 0, then
  403. if (max_length == 0uz) {
  404. // a. Return the Record { [[Read]]: 0, [[Bytes]]: « », [[Error]]: none }.
  405. return { .read = 0, .bytes = {}, .error = {} };
  406. }
  407. // 4. Let read be 0.
  408. size_t read = 0;
  409. // 5. Let bytes be « ».
  410. ByteBuffer bytes;
  411. // 6. Let chunk be the empty String.
  412. StringBuilder chunk;
  413. // 7. Let chunkLength be 0.
  414. size_t chunk_length = 0;
  415. // 8. Let index be 0.
  416. size_t index = 0;
  417. // 9. Let length be the length of string.
  418. auto length = string.length();
  419. // 10. Repeat,
  420. while (true) {
  421. // a. Set index to SkipAsciiWhitespace(string, index).
  422. index = skip_ascii_whitespace(string, index);
  423. // b. If index = length, then
  424. if (index == length) {
  425. // i. If chunkLength > 0, then
  426. if (chunk_length > 0) {
  427. // 1. If lastChunkHandling is "stop-before-partial", then
  428. if (last_chunk_handling == LastChunkHandling::StopBeforePartial) {
  429. // a. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }.
  430. return { .read = read, .bytes = move(bytes), .error = {} };
  431. }
  432. // 2. Else if lastChunkHandling is "loose", then
  433. else if (last_chunk_handling == LastChunkHandling::Loose) {
  434. VERIFY_NOT_REACHED();
  435. }
  436. // 3. Else,
  437. else {
  438. // a. Assert: lastChunkHandling is "strict".
  439. VERIFY(last_chunk_handling == LastChunkHandling::Strict);
  440. // b. Let error be a new SyntaxError exception.
  441. auto error = vm.throw_completion<SyntaxError>("Invalid trailing data"sv);
  442. // c. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
  443. return { .read = read, .bytes = move(bytes), .error = move(error) };
  444. }
  445. }
  446. // ii. Return the Record { [[Read]]: length, [[Bytes]]: bytes, [[Error]]: none }.
  447. return { .read = length, .bytes = move(bytes), .error = {} };
  448. }
  449. // c. Let char be the substring of string from index to index + 1.
  450. auto ch = string[index];
  451. // d. Set index to index + 1.
  452. ++index;
  453. // e. If char is "=", then
  454. if (ch == '=') {
  455. // i. If chunkLength < 2, then
  456. if (chunk_length < 2) {
  457. // 1. Let error be a new SyntaxError exception.
  458. auto error = vm.throw_completion<SyntaxError>("Unexpected padding character"sv);
  459. // 2. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
  460. return { .read = read, .bytes = move(bytes), .error = move(error) };
  461. }
  462. // ii. Set index to SkipAsciiWhitespace(string, index).
  463. index = skip_ascii_whitespace(string, index);
  464. // iii. If chunkLength = 2, then
  465. if (chunk_length == 2) {
  466. // 1. If index = length, then
  467. if (index == length) {
  468. // a. If lastChunkHandling is "stop-before-partial", then
  469. if (last_chunk_handling == LastChunkHandling::StopBeforePartial) {
  470. // i. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }.
  471. return { .read = read, .bytes = move(bytes), .error = {} };
  472. }
  473. // b. Let error be a new SyntaxError exception.
  474. auto error = vm.throw_completion<SyntaxError>("Incomplete number of padding characters"sv);
  475. // c. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
  476. return { .read = read, .bytes = move(bytes), .error = move(error) };
  477. }
  478. // 2. Set char to the substring of string from index to index + 1.
  479. ch = string[index];
  480. // 3. If char is "=", then
  481. if (ch == '=') {
  482. // a. Set index to SkipAsciiWhitespace(string, index + 1).
  483. index = skip_ascii_whitespace(string, index + 1);
  484. }
  485. }
  486. // iv. If index < length, then
  487. if (index < length) {
  488. // 1. Let error be a new SyntaxError exception.
  489. auto error = vm.throw_completion<SyntaxError>("Unexpected padding character"sv);
  490. // 2. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
  491. return { .read = read, .bytes = move(bytes), .error = move(error) };
  492. }
  493. // v. If lastChunkHandling is "strict", let throwOnExtraBits be true.
  494. // vi. Else, let throwOnExtraBits be false.
  495. auto throw_on_extra_bits = last_chunk_handling == LastChunkHandling::Strict;
  496. // vii. Let decodeResult be Completion(DecodeBase64Chunk(chunk, throwOnExtraBits)).
  497. auto decode_result = decode_base64_chunk(vm, chunk, throw_on_extra_bits);
  498. // viii. If decodeResult is an abrupt completion, then
  499. if (decode_result.is_error()) {
  500. // 1. Let error be decodeResult.[[Value]].
  501. auto error = decode_result.release_error();
  502. // 2. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
  503. return { .read = read, .bytes = move(bytes), .error = move(error) };
  504. }
  505. // ix. Set bytes to the list-concatenation of bytes and ! decodeResult.
  506. bytes.append(decode_result.release_value());
  507. // x. Return the Record { [[Read]]: length, [[Bytes]]: bytes, [[Error]]: none }.
  508. return { .read = length, .bytes = move(bytes), .error = {} };
  509. }
  510. // f. If alphabet is "base64url", then
  511. if (alphabet == Alphabet::Base64URL) {
  512. // i. If char is either "+" or "/", then
  513. if (ch == '+' || ch == '/') {
  514. // 1. Let error be a new SyntaxError exception.
  515. auto error = vm.throw_completion<SyntaxError>(MUST(String::formatted("Invalid character '{}'"sv, ch)));
  516. // 2. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
  517. return { .read = read, .bytes = move(bytes), .error = move(error) };
  518. }
  519. // ii. Else if char is "-", then
  520. else if (ch == '-') {
  521. // 1. Set char to "+".
  522. ch = '+';
  523. }
  524. // iii. Else if char is "_", then
  525. else if (ch == '-') {
  526. // 1. Set char to "/".
  527. ch = '/';
  528. }
  529. }
  530. // g. If the sole code unit of char is not an element of the standard base64 alphabet, then
  531. static constexpr auto standard_base64_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"sv;
  532. if (!standard_base64_alphabet.contains(ch)) {
  533. // i. Let error be a new SyntaxError exception.
  534. auto error = vm.throw_completion<SyntaxError>(MUST(String::formatted("Invalid character '{}'"sv, ch)));
  535. // ii. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
  536. return { .read = read, .bytes = move(bytes), .error = move(error) };
  537. }
  538. // h. Let remaining be maxLength - the length of bytes.
  539. auto remaining = *max_length - bytes.size();
  540. // i. If remaining = 1 and chunkLength = 2, or if remaining = 2 and chunkLength = 3, then
  541. if ((remaining == 1 && chunk_length == 2) || (remaining == 2 && chunk_length == 3)) {
  542. // i. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }.
  543. return { .read = read, .bytes = move(bytes), .error = {} };
  544. }
  545. // j. Set chunk to the string-concatenation of chunk and char.
  546. chunk.append(ch);
  547. // k. Set chunkLength to the length of chunk.
  548. chunk_length = chunk.length();
  549. // l. If chunkLength = 4, then
  550. if (chunk_length == 4) {
  551. // i. Set bytes to the list-concatenation of bytes and ! DecodeBase64Chunk(chunk).
  552. bytes.append(MUST(decode_base64_chunk(vm, chunk)));
  553. // ii. Set chunk to the empty String.
  554. chunk.clear();
  555. // iii. Set chunkLength to 0.
  556. chunk_length = 0;
  557. // iv. Set read to index.
  558. read = index;
  559. // v. If the length of bytes = maxLength, then
  560. if (bytes.size() == max_length) {
  561. // 1. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }.
  562. return { .read = read, .bytes = move(bytes), .error = {} };
  563. }
  564. }
  565. }
  566. }
  567. // 10.4 FromHex ( string [ , maxLength ] ), https://tc39.es/proposal-arraybuffer-base64/spec/#sec-fromhex
  568. DecodeResult from_hex(VM& vm, StringView string, Optional<size_t> max_length)
  569. {
  570. // 1. If maxLength is not present, let maxLength be 2**53 - 1.
  571. if (!max_length.has_value())
  572. max_length = MAX_ARRAY_LIKE_INDEX;
  573. // 2. Let length be the length of string.
  574. auto length = string.length();
  575. // 3. Let bytes be « ».
  576. ByteBuffer bytes;
  577. // 4. Let read be 0.
  578. size_t read = 0;
  579. // 5. If length modulo 2 is not 0, then
  580. if (length % 2 != 0) {
  581. // a. Let error be a new SyntaxError exception.
  582. auto error = vm.throw_completion<SyntaxError>("Hex string must have an even length"sv);
  583. // b. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
  584. return { .read = read, .bytes = move(bytes), .error = move(error) };
  585. }
  586. // 6. Repeat, while read < length and the length of bytes < maxLength,
  587. while (read < length && bytes.size() < *max_length) {
  588. // a. Let hexits be the substring of string from read to read + 2.
  589. auto hexits = string.substring_view(read, 2);
  590. // d. Let byte be the integer value represented by hexits in base-16 notation, using the letters A-F and a-f
  591. // for digits with values 10 through 15.
  592. // NOTE: We do this early so that we don't have to effectively parse hexits twice.
  593. auto byte = AK::StringUtils::convert_to_uint_from_hex<u8>(hexits, AK::TrimWhitespace::No);
  594. // b. If hexits contains any code units which are not in "0123456789abcdefABCDEF", then
  595. if (!byte.has_value()) {
  596. // i. Let error be a new SyntaxError exception.
  597. auto error = vm.throw_completion<SyntaxError>("Hex string must only contain hex characters"sv);
  598. // ii. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
  599. return { .read = read, .bytes = move(bytes), .error = move(error) };
  600. }
  601. // c. Set read to read + 2.
  602. read += 2;
  603. // e. Append byte to bytes.
  604. bytes.append(*byte);
  605. }
  606. // 7. Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }.
  607. return { .read = read, .bytes = move(bytes), .error = {} };
  608. }
  609. }