10 Helpers
10.1 SkipAsciiWhitespace ( string, index )
The abstract operation SkipAsciiWhitespace takes arguments string (a string) and index (a non-negative integer) and returns a non-negative integer. It performs the following steps when called:
- Let length be the length of string.
- Repeat, while index < length,
- Let char be the code unit at index index of string.
- If char is neither 0x0009 (TAB), 0x000A (LF), 0x000C (FF), 0x000D (CR), nor 0x0020 (SPACE), then
- Return index.
- Set index to index + 1.
- Return index.
10.2 DecodeBase64Chunk ( chunk [ , throwOnExtraBits ] )
The abstract operation DecodeBase64Chunk takes argument chunk (a string) and optional argument throwOnExtraBits (a boolean) and returns either a normal completion containing a List of byte values, or a throw completion.
The standard base64 alphabet is the String whose elements are the code units corresponding to every letter and number in the Unicode Basic Latin block along with "+" and "/"; that is, it is "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".
- Let chunkLength be the length of chunk.
- If chunkLength is 2, then
- Set chunk to the string-concatenation of chunk and "AA".
- Else if chunkLength is 3, then
- Set chunk to the string-concatenation of chunk and "A".
- Else,
- Assert: chunkLength is 4.
- Let byteSequence be the unique sequence of 3 bytes resulting from decoding chunk as base64 (such that applying the base64 encoding specified in section 4 of RFC 4648 to byteSequence would produce chunk).
- Let bytes be a List whose elements are the elements of byteSequence, in order.
- If chunkLength is 2, then
- Assert: throwOnExtraBits is present.
- If throwOnExtraBits is true and bytes[1] β 0, then
- Throw a SyntaxError exception.
- Return Β« bytes[0] Β».
- Else if chunkLength is 3, then
- Assert: throwOnExtraBits is present.
- If throwOnExtraBits is true and bytes[2] β 0, then
- Throw a SyntaxError exception.
- Return Β« bytes[0], bytes[1] Β».
- Else,
- Return bytes.
10.3 FromBase64 ( string, alphabet, lastChunkHandling [ , maxLength ] )
The abstract operation FromBase64 takes arguments string (a string), alphabet ("base64" or "base64url"), and lastChunkHandling ("loose", "strict", or "stop-before-partial") and optional argument maxLength (a non-negative integer) and returns a Record with fields [[Read]] (an integral Number), [[Bytes]] (a List of byte values), and [[Error]] (either none or a SyntaxError object). It performs the following steps when called:
- If maxLength is not present, then
- Let maxLength be 253 - 1.
- NOTE: Because the input is a string, the length of strings is limited to 253 - 1 characters, and the output requires no more bytes than the input has characters, this limit can never be reached. However, it is editorially convenient to use a finite value here.
- NOTE: The order of validation and decoding in the algorithm below is not observable. Implementations are encouraged to perform them in whatever order is most efficient, possibly interleaving validation with decoding, as long as the behaviour is observably equivalent.
- If maxLength is 0, then
- Return the Record { [[Read]]: 0, [[Bytes]]: Β« Β», [[Error]]: none }.
- Let read be 0.
- Let bytes be Β« Β».
- Let chunk be the empty String.
- Let chunkLength be 0.
- Let index be 0.
- Let length be the length of string.
- Repeat,
- Set index to SkipAsciiWhitespace(string, index).
- If index = length, then
- If chunkLength > 0, then
- If lastChunkHandling is "stop-before-partial", then
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }.
- Else if lastChunkHandling is "loose", then
- If chunkLength is 1, then
- Let error be a new SyntaxError exception.
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
- Set bytes to the list-concatenation of bytes and ! DecodeBase64Chunk(chunk, false).
- Else,
- Assert: lastChunkHandling is "strict".
- Let error be a new SyntaxError exception.
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
- Return the Record { [[Read]]: length, [[Bytes]]: bytes, [[Error]]: none }.
- Let char be the substring of string from index to index + 1.
- Set index to index + 1.
- If char is "=", then
- If chunkLength < 2, then
- Let error be a new SyntaxError exception.
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
- Set index to SkipAsciiWhitespace(string, index).
- If chunkLength = 2, then
- If index = length, then
- If lastChunkHandling is "stop-before-partial", then
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }.
- Let error be a new SyntaxError exception.
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
- Set char to the substring of string from index to index + 1.
- If char is "=", then
- Set index to SkipAsciiWhitespace(string, index + 1).
- If index < length, then
- Let error be a new SyntaxError exception.
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
- If lastChunkHandling is "strict", let throwOnExtraBits be true.
- Else, let throwOnExtraBits be false.
- Let decodeResult be Completion(DecodeBase64Chunk(chunk, throwOnExtraBits)).
- If decodeResult is an abrupt completion, then
- Let error be decodeResult.[[Value]].
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
- Set bytes to the list-concatenation of bytes and ! decodeResult.
- Return the Record { [[Read]]: length, [[Bytes]]: bytes, [[Error]]: none }.
- If alphabet is "base64url", then
- If char is either "+" or "/", then
- Let error be a new SyntaxError exception.
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
- Else if char is "-", then
- Set char to "+".
- Else if char is "_", then
- Set char to "/".
- If the sole code unit of char is not an element of the standard base64 alphabet, then
- Let error be a new SyntaxError exception.
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
- Let remaining be maxLength - the length of bytes.
- If remaining = 1 and chunkLength = 2, or if remaining = 2 and chunkLength = 3, then
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }.
- Set chunk to the string-concatenation of chunk and char.
- Set chunkLength to the length of chunk.
- If chunkLength = 4, then
- Set bytes to the list-concatenation of bytes and ! DecodeBase64Chunk(chunk).
- Set chunk to the empty String.
- Set chunkLength to 0.
- Set read to index.
- If the length of bytes = maxLength, then
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }.
10.4 FromHex ( string [ , maxLength ] )
The abstract operation FromHex takes argument string (a string) and optional argument maxLength (a non-negative integer) and returns a Record with fields [[Read]] (an integral Number), [[Bytes]] (a List of byte values), and [[Error]] (either none or a SyntaxError object). It performs the following steps when called:
- If maxLength is not present, let maxLength be 253 - 1.
- Let length be the length of string.
- Let bytes be Β« Β».
- Let read be 0.
- If length modulo 2 is not 0, then
- Let error be a new SyntaxError exception.
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
- Repeat, while read < length and the length of bytes < maxLength,
- Let hexits be the substring of string from read to read + 2.
- If hexits contains any code units which are not in "0123456789abcdefABCDEF", then
- Let error be a new SyntaxError exception.
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }.
- Set read to read + 2.
- Let byte be the integer value represented by hexits in base-16 notation, using the letters A-F and a-f for digits with values 10 through 15.
- Append byte to bytes.
- Return the Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }.
10.5 GetOptionsObject ( options )
- If options is undefined, then
- Return OrdinaryObjectCreate(null).
- If Type(options) is Object, then
- Return options.
- Throw a TypeError exception.