Skip to content

Commit 6b643f4

Browse files
committedDec 19, 2013
Implement base64 stdlib
1 parent 1f6506b commit 6b643f4

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
 

‎stdlib/base64.rb

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
module Base64
2+
%x{
3+
var charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
4+
lookup = {};
5+
6+
for (var i = 0, length = charset.length; i < length; i++) {
7+
lookup[charset.charAt(i)] = i;
8+
}
9+
10+
function from(string) {
11+
var buffer = [];
12+
13+
for (var i = 0, length = string.length; i < length; i++) {
14+
var a, b, c, d;
15+
16+
a = lookup[string.charAt(i)];
17+
b = lookup[string.charAt(++i)];
18+
19+
buffer.push((a << 2) | (b >> 4));
20+
21+
c = lookup[string.charAt(++i)];
22+
23+
if (c == 64) {
24+
break;
25+
}
26+
27+
buffer.push(((b & 15) << 4) | (c >> 2));
28+
29+
d = lookup[string.charAt(++i)];
30+
31+
if (d == 64) {
32+
break;
33+
}
34+
35+
buffer.push(((c & 3) << 6) | d);
36+
}
37+
38+
return buffer;
39+
}
40+
41+
function decode(string) {
42+
var buffer = from(string),
43+
result = [];
44+
45+
for (var i = 0, length = buffer.length; i < length; i++) {
46+
if (buffer[i] < 128) {
47+
result.push(String.fromCharCode(buffer[i]));
48+
}
49+
else if (buffer[i] > 191 && buffer[i] < 224) {
50+
var a = (buffer[i] & 31) << 6,
51+
b = buffer[++i] & 63;
52+
53+
result.push(String.fromCharCode(a | b));
54+
}
55+
else {
56+
var a = (buffer[i] & 15) << 12,
57+
b = (buffer[++i] & 63) << 6,
58+
c = buffer[++i] & 63;
59+
60+
result.push(String.fromCharCode(a | b | c));
61+
}
62+
}
63+
64+
return result.join('');
65+
}
66+
67+
function to(string) {
68+
var buffer = [];
69+
70+
if (/^[\x00-\x7f]*$/.test(string)) {
71+
for (var i = 0, length = string.length; i < length; i++) {
72+
buffer.push(string.charCodeAt(i));
73+
}
74+
}
75+
else {
76+
for (var i = 0, length = string.length; i < length; i++) {
77+
var ch = string.charCodeAt(i);
78+
79+
if (ch < 128) {
80+
buffer.push(ch);
81+
}
82+
else if (ch < 2048) {
83+
buffer.push((ch >> 6) | 192);
84+
buffer.push((ch & 63) | 128);
85+
}
86+
else {
87+
buffer.push((ch >> 12) | 224);
88+
buffer.push(((ch >> 6) & 63) | 128);
89+
buffer.push((ch & 63) | 128);
90+
}
91+
}
92+
}
93+
94+
return buffer;
95+
}
96+
97+
function encode(string) {
98+
var buffer = to(string),
99+
result = [];
100+
101+
for (var i = 0, length = buffer.length; i < length; i++) {
102+
var a = buffer[i],
103+
b = buffer[++i],
104+
c = 0,
105+
d = a >> 2,
106+
e = ((a & 3) << 4) | (b >> 4),
107+
f = 0,
108+
g = 0;
109+
110+
if (isNaN(a)) {
111+
f = g = 64;
112+
}
113+
else {
114+
c = buffer[++i];
115+
f = ((b & 15) << 2) | (c >> 6);
116+
g = isNaN(c) ? 64 : c & 63;
117+
}
118+
119+
result.push(charset.charAt(d));
120+
result.push(charset.charAt(e));
121+
result.push(charset.charAt(f));
122+
result.push(charset.charAt(g));
123+
}
124+
125+
return result.join('');
126+
}
127+
}
128+
129+
def self.decode64(string)
130+
`decode(string.replace(/\r?\n/g, ''))`
131+
end
132+
133+
def self.encode64(string)
134+
`encode(string).replace(/(.{60})/g, "$1\n")`
135+
end
136+
137+
def self.strict_decode64(string)
138+
`decode(string)`
139+
end
140+
141+
def self.strict_encode64(string)
142+
`encode(string)`
143+
end
144+
145+
def self.urlsafe_decode64(string)
146+
`decode(string.replace(/\-/g, '+').replace(/_/g, '/'))`
147+
end
148+
149+
def self.urlsafe_encode64(string)
150+
`encode(string).replace(/\+/g, '-').replace(/\//g, '_')`
151+
end
152+
end

0 commit comments

Comments
 (0)
Please sign in to comment.