Skip to content

Commit fa6f867

Browse files
author
jossonsmith
committed
Fixed bug of Base64 encoding and decoding
1 parent 2b66b6d commit fa6f867

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

sources/net.sf.j2s.java.core/src/java/lang/Encoding.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,15 @@ Encoding.encodeBase64 = function (str) {
9696
var index = 0;
9797
var buf = [];
9898
var c0, c1, c2;
99-
var c = 0;
100-
while (index < length && c++ < 60000) {
99+
while (index < length) {
101100
c0 = str.charCodeAt (index++);
102101
buf[buf.length] = b64[c0 >> 2];
103102
if (index < length) {
104103
c1 = str.charCodeAt (index++);
105104
buf[buf.length] = b64[((c0 << 4 ) & 0x30) | (c1 >> 4)];
106105
if (index < length){
107106
c2 = str.charCodeAt (index++);
108-
buf[buf.length] = b64[((c1 << 2) & 0x3c) | (c1 >> 6)];
107+
buf[buf.length] = b64[((c1 << 2) & 0x3c) | (c2 >> 6)];
109108
buf[buf.length] = b64[c2 & 0x3F];
110109
} else {
111110
buf[buf.length] = b64[((c1 << 2) & 0x3c)];
@@ -140,11 +139,13 @@ Encoding.decodeBase64 = function (str) {
140139
c1 = xb64[str.charAt (index++)];
141140
c2 = xb64[str.charAt (index++)];
142141
c3 = xb64[str.charAt (index++)];
143-
if (c2 == null) c2 = 0;
144-
if (c3 == null) c3 = 0;
145142
buf[buf.length] = String.fromCharCode(((c0 << 2) & 0xff) | c1 >> 4);
146-
buf[buf.length] = String.fromCharCode(((c1 << 4) & 0xff) | c2 >> 2);
147-
buf[buf.length] = String.fromCharCode(((c2 << 6) & 0xff) | c3);
143+
if (c2 != null) {
144+
buf[buf.length] = String.fromCharCode(((c1 << 4) & 0xff) | c2 >> 2);
145+
if (c3 != null) {
146+
buf[buf.length] = String.fromCharCode(((c2 << 6) & 0xff) | c3);
147+
}
148+
}
148149
}
149150
return buf.join ('');
150151
};

0 commit comments

Comments
 (0)