[whatwg] Specs for window.atob() and window.btoa()

Jorge jorge at jorgechamorro.com
Fri Feb 4 08:37:06 PST 2011


Wrt to the note "some base64 encoders add newlines or other whitespace to their output. atob() throws an exception if its input contains characters other than +/=0-9A-Za-z, so other characters need to be removed before atob() is used for decoding" in http://aryeh.name/spec/base64.html , I think that in the end it's better to ignore any other chars instead of throwing, because skipping over any such chars while decoding is cheaper and requires less memory than scanning the input twice, first to clean it and second to decode it, something you'd not want to end up doing -just in case- everytime.

Say, for example, that you've got a 4MB base64 with (perhaps?) some whitespace, in order to clean it up you're going to have to have it in memory along the cleaned up version at least while constructing the clean version, but if atob() skipped over anything other than +/=0-9A-Za-z you could just pass it directly, and the whole process would be even faster too, given there was no need to clean it up first. FWIW, that's how nodejs is doing it right now.

Also, some tools (e.g. the openssl decoder) *expect* the newlines to be there, and fail if they aren't.

On 01/02/2011, at 09:34, Ian Hickson wrote:

> On Wed, 25 Aug 2010, Boris Zbarsky wrote:
>> On 8/25/10 9:37 PM, Boris Zbarsky wrote:
>>> Note that this issue means that using atob or btoa for dealing with 
>>> this is a huge pain if non-ASCII chars are involved, since those take 
>>> and return byte arrays masquerading as JS strings, not actual Unicode 
>>> strings.
>> Of course I can't find any actual spec for atob/btoa.  I would think it 
>> needs to be part of the Window spec, no?
> On Thu, 6 Jan 2011, Aryeh Gregor wrote:
>> I've written a provisional spec for window.atob() and window.btoa():
>> http://aryeh.name/spec/base64.html
>> These are functions supported by all browsers except IE, which do base64 
>> encoding and decoding.
> Awesome. I've updated the spec accordingly.
>> I also wrote a fairly complete test suite, at:
>> http://dvcs.w3.org/hg/html/raw-file/tip/tests/submission/AryehGregor/base64.html
> Cool.
> -- 
> Ian Hickson               U+1047E                )\._.,--....,'``.    fL
> http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
> Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

More information about the whatwg mailing list