Base64 decoding in Javascript

I never wrote a decoder in my previous post, so here’s my first iteration while half asleep.

function b64d(str) {

        var charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


        var str_pos = 0;
        var str_len = str.length;

        var numbits=0;
        var curval=0;

        var data = "";
        var done = false;

        while (done == false){
                // Not enough bits, get more
                if (numbits < 8) {
                        var nextchar = str.charAt(str_pos++);
                        if (nextchar == "=") { break; }
                        var sixbits = charset.indexOf(nextchar);
                        curval = (curval << 6) | sixbits;
                        numbits += 6;
                } else {

                        nextbyte = curval >> (numbits - 8);
                        curval = curval - (nextbyte << numbits - 8);
                        numbits -= 8;

                        data += String.fromCharCode(nextbyte);

                        if (str_pos >= str_len && numbits < 8){
                                done = true;
                        }
                }
        }

        return data;

}