easy64.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.encoding.easy64"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.encoding.easy64"] = true;
  8. dojo.provide("dojox.encoding.easy64");
  9. dojo.getObject("encoding.easy64", true, dojox);
  10. (function(){
  11. var c = function(input, length, result){
  12. for(var i = 0; i < length; i += 3){
  13. result.push(
  14. String.fromCharCode((input[i] >>> 2) + 33),
  15. String.fromCharCode(((input[i] & 3) << 4) + (input[i + 1] >>> 4) + 33),
  16. String.fromCharCode(((input[i + 1] & 15) << 2) + (input[i + 2] >>> 6) + 33),
  17. String.fromCharCode((input[i + 2] & 63) + 33)
  18. );
  19. }
  20. };
  21. dojox.encoding.easy64.encode = function(input){
  22. // summary: encodes input data in easy64 string
  23. // input: Array: an array of numbers (0-255) to encode
  24. var result = [], reminder = input.length % 3, length = input.length - reminder;
  25. c(input, length, result);
  26. if(reminder){
  27. var t = input.slice(length);
  28. while(t.length < 3){ t.push(0); }
  29. c(t, 3, result);
  30. for(var i = 3; i > reminder; result.pop(), --i);
  31. }
  32. return result.join(""); // String
  33. };
  34. dojox.encoding.easy64.decode = function(input){
  35. // summary: decodes the input string back to array of numbers
  36. // input: String: the input string to decode
  37. var n = input.length, r = [], b = [0, 0, 0, 0], i, j, d;
  38. for(i = 0; i < n; i += 4){
  39. for(j = 0; j < 4; ++j){ b[j] = input.charCodeAt(i + j) - 33; }
  40. d = n - i;
  41. for(j = d; j < 4; b[++j] = 0);
  42. r.push(
  43. (b[0] << 2) + (b[1] >>> 4),
  44. ((b[1] & 15) << 4) + (b[2] >>> 2),
  45. ((b[2] & 3) << 6) + b[3]
  46. );
  47. for(j = d; j < 4; ++j, r.pop());
  48. }
  49. return r;
  50. };
  51. })();
  52. }