easy64.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. define("dojox/encoding/easy64", ["dojo/_base/lang"], function(lang) {
  2. var easy64 = lang.getObject("dojox.encoding.easy64", true);
  3. /*=====
  4. easy64 = dojox.encoding.easy64;
  5. =====*/
  6. var c = function(input, length, result){
  7. for(var i = 0; i < length; i += 3){
  8. result.push(
  9. String.fromCharCode((input[i] >>> 2) + 33),
  10. String.fromCharCode(((input[i] & 3) << 4) + (input[i + 1] >>> 4) + 33),
  11. String.fromCharCode(((input[i + 1] & 15) << 2) + (input[i + 2] >>> 6) + 33),
  12. String.fromCharCode((input[i + 2] & 63) + 33)
  13. );
  14. }
  15. };
  16. easy64.encode = function(input){
  17. // summary: encodes input data in easy64 string
  18. // input: Array: an array of numbers (0-255) to encode
  19. var result = [], reminder = input.length % 3, length = input.length - reminder;
  20. c(input, length, result);
  21. if(reminder){
  22. var t = input.slice(length);
  23. while(t.length < 3){ t.push(0); }
  24. c(t, 3, result);
  25. for(var i = 3; i > reminder; result.pop(), --i);
  26. }
  27. return result.join(""); // String
  28. };
  29. easy64.decode = function(input){
  30. // summary: decodes the input string back to array of numbers
  31. // input: String: the input string to decode
  32. var n = input.length, r = [], b = [0, 0, 0, 0], i, j, d;
  33. for(i = 0; i < n; i += 4){
  34. for(j = 0; j < 4; ++j){ b[j] = input.charCodeAt(i + j) - 33; }
  35. d = n - i;
  36. for(j = d; j < 4; b[++j] = 0);
  37. r.push(
  38. (b[0] << 2) + (b[1] >>> 4),
  39. ((b[1] & 15) << 4) + (b[2] >>> 2),
  40. ((b[2] & 3) << 6) + b[3]
  41. );
  42. for(j = d; j < 4; ++j, r.pop());
  43. }
  44. return r;
  45. };
  46. return easy64;
  47. });