Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: pkg/webdriver/lib/src/base64decoder.dart

Issue 12321078: Make base64 decoding available in dart:crypto Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove duplicate base64 decoding functionality from webdriver and use dart:crypto Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/webdriver/lib/webdriver.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of webdriver;
6
7 /**
8 * A simple base64 decoder class, used to decode web browser screenshots
9 * returned by WebDriver.
10 */
11 class Base64Decoder {
12
13 static int getVal(String s, pos) {
14 int code = s.codeUnitAt(pos);
15 if (code >= 65 && code < (65+26)) { // 'A'..'Z'
16 return code - 65;
17 } else if (code >= 97 && code < (97+26)) { // 'a'..'z'
18 return code - 97 + 26;
19 } else if (code >= 48 && code < (48+10)) { // '0'..'9'
20 return code - 48 + 52;
21 } else if (code == 43) { // '+'
22 return 62;
23 } else if (code == 47) { // '/'
24 return 63;
25 } else {
26 throw 'Invalid character $s';
27 }
28 }
29
30 static List<int> decode(String s) {
31 var rtn = new List<int>();
32 var pos = 0;
33 while (pos < s.length) {
34 if (s[pos+2] =='=') { // Single byte as two chars.
35 int v = (getVal(s, pos) << 18 ) | (getVal(s, pos+1) << 12 );
36 rtn.add((v >> 16) & 0xff);
37 break;
38 } else if (s[pos+3] == '=') { // Two bytes as 3 chars.
39 int v = (getVal(s, pos) << 18 ) | (getVal(s, pos+1) << 12 ) |
40 (getVal(s, pos + 2) << 6);
41 rtn.add((v >> 16) & 0xff);
42 rtn.add((v >> 8) & 0xff);
43 break;
44 } else { // Three bytes as 4 chars.
45 int v = (getVal(s, pos) << 18 ) | (getVal(s, pos+1) << 12 ) |
46 (getVal(s, pos + 2) << 6) | getVal(s, pos+3);
47 pos += 4;
48 rtn.add((v >> 16 ) & 0xff);
49 rtn.add((v >> 8) & 0xff);
50 rtn.add(v & 0xff);
51 }
52 }
53 return rtn;
54 }
55 }
OLDNEW
« no previous file with comments | « no previous file | pkg/webdriver/lib/webdriver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698