6254729761
This reverts r13961 and r13963. Review URL: https://codereview.chromium.org//11233061 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@13967 260f80e4-7a28-3924-810f-c04153c831b5
50 lines
1.5 KiB
Dart
50 lines
1.5 KiB
Dart
/**
|
|
* A simple base64 decoder class, used to decode web browser screenshots
|
|
* returned by WebDriver.
|
|
*/
|
|
class Base64Decoder {
|
|
|
|
static int getVal(String s, pos) {
|
|
int code = s.charCodeAt(pos);
|
|
if (code >= 65 && code < (65+26)) { // 'A'..'Z'
|
|
return code - 65;
|
|
} else if (code >= 97 && code < (97+26)) { // 'a'..'z'
|
|
return code - 97 + 26;
|
|
} else if (code >= 48 && code < (48+10)) { // '0'..'9'
|
|
return code - 48 + 52;
|
|
} else if (code == 43) { // '+'
|
|
return 62;
|
|
} else if (code == 47) { // '/'
|
|
return 63;
|
|
} else {
|
|
throw 'Invalid character $s';
|
|
}
|
|
}
|
|
|
|
static List<int> decode(String s) {
|
|
var rtn = new List<int>();
|
|
var pos = 0;
|
|
while (pos < s.length) {
|
|
if (s[pos+2] =='=') { // Single byte as two chars.
|
|
int v = (getVal(s, pos) << 18 ) | (getVal(s, pos+1) << 12 );
|
|
rtn.add((v >> 16) & 0xff);
|
|
break;
|
|
} else if (s[pos+3] == '=') { // Two bytes as 3 chars.
|
|
int v = (getVal(s, pos) << 18 ) | (getVal(s, pos+1) << 12 ) |
|
|
(getVal(s, pos + 2) << 6);
|
|
rtn.add((v >> 16) & 0xff);
|
|
rtn.add((v >> 8) & 0xff);
|
|
break;
|
|
} else { // Three bytes as 4 chars.
|
|
int v = (getVal(s, pos) << 18 ) | (getVal(s, pos+1) << 12 ) |
|
|
(getVal(s, pos + 2) << 6) | getVal(s, pos+3);
|
|
pos += 4;
|
|
rtn.add((v >> 16 ) & 0xff);
|
|
rtn.add((v >> 8) & 0xff);
|
|
rtn.add(v & 0xff);
|
|
}
|
|
}
|
|
return rtn;
|
|
}
|
|
}
|