diff options
author | Jose <jose@zeroc.com> | 2016-08-19 21:25:18 +0200 |
---|---|---|
committer | Jose <jose@zeroc.com> | 2016-08-19 21:25:18 +0200 |
commit | c8d32e04873be7938915c606027c84c8ab832f6b (patch) | |
tree | f862751cfaddcf5bb7b82a7c04a299d471f948d4 /js/src/Ice/Base64.js | |
parent | Fix ICE-7278 (diff) | |
download | ice-c8d32e04873be7938915c606027c84c8ab832f6b.tar.bz2 ice-c8d32e04873be7938915c606027c84c8ab832f6b.tar.xz ice-c8d32e04873be7938915c606027c84c8ab832f6b.zip |
ES6 mapping updates
Diffstat (limited to 'js/src/Ice/Base64.js')
-rw-r--r-- | js/src/Ice/Base64.js | 396 |
1 files changed, 197 insertions, 199 deletions
diff --git a/js/src/Ice/Base64.js b/js/src/Ice/Base64.js index 6211fd2c08d..1442ac5b371 100644 --- a/js/src/Ice/Base64.js +++ b/js/src/Ice/Base64.js @@ -7,265 +7,263 @@ // // ********************************************************************** -var Ice = require("../Ice/Buffer").Ice; +const Ice = require("../Ice/Buffer").Ice; -var Buffer = Ice.Buffer; +const Buffer = Ice.Buffer; -var Base64 = {}; +const _codeA = "A".charCodeAt(0); +const _codea = "a".charCodeAt(0); +const _code0 = "0".charCodeAt(0); -var _codeA = "A".charCodeAt(0); -var _codea = "a".charCodeAt(0); -var _code0 = "0".charCodeAt(0); - -Base64.encode = function(buf) // Expects native Buffer +function encodeChar(uc) { - if(buf === null || buf.length === 0) + if(uc < 26) { - return ""; + return String.fromCharCode(_codeA + uc); } - var base64Bytes = (((buf.length * 4) / 3) + 1); - var newlineBytes = (((base64Bytes * 2) / 76) + 1); - var totalBytes = base64Bytes + newlineBytes; - - var v = []; - - var by1; - var by2; - var by3; - var by4; - var by5; - var by6; - var by7; - - for(var i = 0; i < buf.length; i += 3) + if(uc < 52) { - by1 = buf[i] & 0xff; - by2 = 0; - by3 = 0; - - if((i + 1) < buf.length) - { - by2 = buf[i + 1] & 0xff; - } - - if((i + 2) < buf.length) - { - by3 = buf[i + 2] & 0xff; - } - - by4 = (by1 >> 2) & 0xff; - by5 = (((by1 & 0x3) << 4) | (by2 >> 4)) & 0xff; - by6 = (((by2 & 0xf) << 2) | (by3 >> 6)) & 0xff; - by7 = by3 & 0x3f; - - v.push(encodeChar(by4)); - v.push(encodeChar(by5)); - - if((i + 1) < buf.length) - { - v.push(encodeChar(by6)); - } - else - { - v.push("="); - } - - if((i + 2) < buf.length) - { - v.push(encodeChar(by7)); - } - else - { - v.push("="); - } + return String.fromCharCode(_codea + (uc - 26)); } - var retval = v.join(""); - var outString = []; - var iter = 0; - - while((retval.length - iter) > 76) + if(uc < 62) { - outString.push(retval.substring(iter, iter + 76)); - outString.push("\r\n"); - iter += 76; + return String.fromCharCode(_code0 + (uc - 52)); } - outString.push(retval.substring(iter)); + if(uc == 62) + { + return "+"; + } - return outString.join(""); -}; + return "/"; +} -Base64.decode = function(str) // Returns native Buffer +function decodeChar(c) { - var newStr = []; - - for(var j = 0; j < str.length; j++) + if(c >= 'A' && c <= 'Z') { - var c = str.charAt(j); - if(Base64.isBase64(c)) - { - newStr.push(c); - } + return c.charCodeAt(0) - _codeA; } - if(newStr.length === 0) + if(c >= 'a' && c <= 'z') { - return null; + return c.charCodeAt(0) - _codea + 26; } - // Note: This is how we were previously computing the size of the return - // sequence. The method below is more efficient (and correct). - // size_t lines = str.size() / 78; - // size_t totalBytes = (lines * 76) + (((str.size() - (lines * 78)) * 3) / 4); - - // Figure out how long the final sequence is going to be. - var totalBytes = (newStr.length * 3 / 4) + 1; - - var retval = new Buffer(); - retval.resize(totalBytes); - - var by1; - var by2; - var by3; - var by4; - - var c1; - var c2; - var c3; - var c4; - - var off = 0; + if(c >= '0' && c <= '9') + { + return c.charCodeAt(0) - _code0 + 52; + } - for(var i = 0; i < newStr.length; i += 4) + if(c == '+') { - c1 = "A"; - c2 = "A"; - c3 = "A"; - c4 = "A"; + return 62; + } - c1 = newStr[i]; + return 63; +} - if((i + 1) < newStr.length) +class Base64 +{ + // Expects native Buffer + static encode(buf) + { + if(buf === null || buf.length === 0) { - c2 = newStr[i + 1]; + return ""; } - if((i + 2) < newStr.length) + let v = []; + + let by1; + let by2; + let by3; + let by4; + let by5; + let by6; + let by7; + + for(let i = 0; i < buf.length; i += 3) { - c3 = newStr[i + 2]; + by1 = buf[i] & 0xff; + by2 = 0; + by3 = 0; + + if((i + 1) < buf.length) + { + by2 = buf[i + 1] & 0xff; + } + + if((i + 2) < buf.length) + { + by3 = buf[i + 2] & 0xff; + } + + by4 = (by1 >> 2) & 0xff; + by5 = (((by1 & 0x3) << 4) | (by2 >> 4)) & 0xff; + by6 = (((by2 & 0xf) << 2) | (by3 >> 6)) & 0xff; + by7 = by3 & 0x3f; + + v.push(encodeChar(by4)); + v.push(encodeChar(by5)); + + if((i + 1) < buf.length) + { + v.push(encodeChar(by6)); + } + else + { + v.push("="); + } + + if((i + 2) < buf.length) + { + v.push(encodeChar(by7)); + } + else + { + v.push("="); + } } - if((i + 3) < newStr.length) + let retval = v.join(""); + let outString = []; + let iter = 0; + + while((retval.length - iter) > 76) { - c4 = newStr[i + 3]; + outString.push(retval.substring(iter, iter + 76)); + outString.push("\r\n"); + iter += 76; } - by1 = decodeChar(c1) & 0xff; - by2 = decodeChar(c2) & 0xff; - by3 = decodeChar(c3) & 0xff; - by4 = decodeChar(c4) & 0xff; + outString.push(retval.substring(iter)); + + return outString.join(""); + } - retval.put((by1 << 2) | (by2 >> 4)); + static decode(str) // Returns native Buffer + { + let newStr = []; - if(c3 != "=") + for(let j = 0; j < str.length; j++) { - retval.put(((by2 & 0xf) << 4) | (by3 >> 2)); + let c = str.charAt(j); + if(Base64.isBase64(c)) + { + newStr.push(c); + } } - if(c4 != "=") + if(newStr.length === 0) { - retval.put(((by3 & 0x3) << 6) | by4); + return null; } - } - return retval.remaining > 0 ? retval.getArrayAt(0, retval.position) : retval.getArrayAt(0); -}; + // Note: This is how we were previously computing the size of the return + // sequence. The method below is more efficient (and correct). + // size_t lines = str.size() / 78; + // size_t totalBytes = (lines * 76) + (((str.size() - (lines * 78)) * 3) / 4); -Base64.isBase64 = function(c) -{ - if(c >= 'A' && c <= 'Z') - { - return true; - } + // Figure out how long the final sequence is going to be. + let totalBytes = (newStr.length * 3 / 4) + 1; - if(c >= 'a' && c <= 'z') - { - return true; - } + let retval = new Buffer(); + retval.resize(totalBytes); - if(c >= '0' && c <= '9') - { - return true; - } + let by1; + let by2; + let by3; + let by4; - if(c == '+') - { - return true; - } - - if(c == '/') - { - return true; - } - - if(c == '=') - { - return true; - } + let c1; + let c2; + let c3; + let c4; - return false; -}; + let off = 0; -function encodeChar(uc) -{ - if(uc < 26) - { - return String.fromCharCode(_codeA + uc); - } + for(let i = 0; i < newStr.length; i += 4) + { + c1 = "A"; + c2 = "A"; + c3 = "A"; + c4 = "A"; + + c1 = newStr[i]; + + if((i + 1) < newStr.length) + { + c2 = newStr[i + 1]; + } + + if((i + 2) < newStr.length) + { + c3 = newStr[i + 2]; + } + + if((i + 3) < newStr.length) + { + c4 = newStr[i + 3]; + } + + by1 = decodeChar(c1) & 0xff; + by2 = decodeChar(c2) & 0xff; + by3 = decodeChar(c3) & 0xff; + by4 = decodeChar(c4) & 0xff; + + retval.put((by1 << 2) | (by2 >> 4)); + + if(c3 != "=") + { + retval.put(((by2 & 0xf) << 4) | (by3 >> 2)); + } + + if(c4 != "=") + { + retval.put(((by3 & 0x3) << 6) | by4); + } + } - if(uc < 52) - { - return String.fromCharCode(_codea + (uc - 26)); + return retval.remaining > 0 ? retval.getArrayAt(0, retval.position) : retval.getArrayAt(0); } - if(uc < 62) + static isBase64(c) { - return String.fromCharCode(_code0 + (uc - 52)); - } + if(c >= 'A' && c <= 'Z') + { + return true; + } - if(uc == 62) - { - return "+"; - } + if(c >= 'a' && c <= 'z') + { + return true; + } - return "/"; -} + if(c >= '0' && c <= '9') + { + return true; + } -function decodeChar(c) -{ - if(c >= 'A' && c <= 'Z') - { - return c.charCodeAt(0) - _codeA; - } + if(c == '+') + { + return true; + } - if(c >= 'a' && c <= 'z') - { - return c.charCodeAt(0) - _codea + 26; - } + if(c == '/') + { + return true; + } - if(c >= '0' && c <= '9') - { - return c.charCodeAt(0) - _code0 + 52; - } + if(c == '=') + { + return true; + } - if(c == '+') - { - return 62; + return false; } - - return 63; } Ice.Base64 = Base64; |