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

Side by Side Diff: chrome/browser/resources/cryptotoken/b64.js

Issue 2939273002: DO NOT SUBMIT: what chrome/browser/resources/ could eventually look like with clang-format (Closed)
Patch Set: Created 3 years, 6 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // WebSafeBase64Escape and Unescape. 5 // WebSafeBase64Escape and Unescape.
6 function B64_encode(bytes, opt_length) { 6 function B64_encode(bytes, opt_length) {
7 if (!opt_length) opt_length = bytes.length; 7 if (!opt_length)
8 opt_length = bytes.length;
8 var b64out = 9 var b64out =
9 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'; 10 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
10 var result = ''; 11 var result = '';
11 var shift = 0; 12 var shift = 0;
12 var accu = 0; 13 var accu = 0;
13 var inputIndex = 0; 14 var inputIndex = 0;
14 while (opt_length--) { 15 while (opt_length--) {
15 accu <<= 8; 16 accu <<= 8;
16 accu |= bytes[inputIndex++]; 17 accu |= bytes[inputIndex++];
17 shift += 8; 18 shift += 8;
18 while (shift >= 6) { 19 while (shift >= 6) {
19 var i = (accu >> (shift - 6)) & 63; 20 var i = (accu >> (shift - 6)) & 63;
20 result += b64out.charAt(i); 21 result += b64out.charAt(i);
21 shift -= 6; 22 shift -= 6;
22 } 23 }
23 } 24 }
24 if (shift) { 25 if (shift) {
25 accu <<= 8; 26 accu <<= 8;
26 shift += 8; 27 shift += 8;
27 var i = (accu >> (shift - 6)) & 63; 28 var i = (accu >> (shift - 6)) & 63;
28 result += b64out.charAt(i); 29 result += b64out.charAt(i);
29 } 30 }
30 return result; 31 return result;
31 } 32 }
32 33
33 // Normal base64 encode; not websafe, including padding. 34 // Normal base64 encode; not websafe, including padding.
34 function base64_encode(bytes, opt_length) { 35 function base64_encode(bytes, opt_length) {
35 if (!opt_length) opt_length = bytes.length; 36 if (!opt_length)
37 opt_length = bytes.length;
36 var b64out = 38 var b64out =
37 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; 39 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
38 var result = ''; 40 var result = '';
39 var shift = 0; 41 var shift = 0;
40 var accu = 0; 42 var accu = 0;
41 var inputIndex = 0; 43 var inputIndex = 0;
42 while (opt_length--) { 44 while (opt_length--) {
43 accu <<= 8; 45 accu <<= 8;
44 accu |= bytes[inputIndex++]; 46 accu |= bytes[inputIndex++];
45 shift += 8; 47 shift += 8;
46 while (shift >= 6) { 48 while (shift >= 6) {
47 var i = (accu >> (shift - 6)) & 63; 49 var i = (accu >> (shift - 6)) & 63;
48 result += b64out.charAt(i); 50 result += b64out.charAt(i);
49 shift -= 6; 51 shift -= 6;
50 } 52 }
51 } 53 }
52 if (shift) { 54 if (shift) {
53 accu <<= 8; 55 accu <<= 8;
54 shift += 8; 56 shift += 8;
55 var i = (accu >> (shift - 6)) & 63; 57 var i = (accu >> (shift - 6)) & 63;
56 result += b64out.charAt(i); 58 result += b64out.charAt(i);
57 } 59 }
58 while (result.length % 4) result += '='; 60 while (result.length % 4)
61 result += '=';
59 return result; 62 return result;
60 } 63 }
61 64
62 var B64_inmap = 65 var B64_inmap = [
63 [ 66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 0, 0,
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63, 0, 0, 67 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 0, 0, 0, 0, 0, 0,
65 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 0, 0, 0, 0, 0, 0, 68 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
66 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 69 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 0, 0, 0, 0, 64,
67 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 0, 0, 0, 0, 64, 70 0, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
68 0, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 71 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 0, 0, 0, 0, 0
69 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 0, 0, 0, 0, 0
70 ]; 72 ];
71 73
72 function B64_decode(string) { 74 function B64_decode(string) {
73 var bytes = []; 75 var bytes = [];
74 var accu = 0; 76 var accu = 0;
75 var shift = 0; 77 var shift = 0;
76 for (var i = 0; i < string.length; ++i) { 78 for (var i = 0; i < string.length; ++i) {
77 var c = string.charCodeAt(i); 79 var c = string.charCodeAt(i);
78 if (c < 32 || c > 127 || !B64_inmap[c - 32]) return []; 80 if (c < 32 || c > 127 || !B64_inmap[c - 32])
81 return [];
79 accu <<= 6; 82 accu <<= 6;
80 accu |= (B64_inmap[c - 32] - 1); 83 accu |= (B64_inmap[c - 32] - 1);
81 shift += 6; 84 shift += 6;
82 if (shift >= 8) { 85 if (shift >= 8) {
83 bytes.push((accu >> (shift - 8)) & 255); 86 bytes.push((accu >> (shift - 8)) & 255);
84 shift -= 8; 87 shift -= 8;
85 } 88 }
86 } 89 }
87 return bytes; 90 return bytes;
88 } 91 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698