ascii85.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.ascii85"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.encoding.ascii85"] = true;
  8. dojo.provide("dojox.encoding.ascii85");
  9. dojo.getObject("encoding.ascii85", true, dojox);
  10. (function(){
  11. var c = function(input, length, result){
  12. var i, j, n, b = [0, 0, 0, 0, 0];
  13. for(i = 0; i < length; i += 4){
  14. n = ((input[i] * 256 + input[i+1]) * 256 + input[i+2]) * 256 + input[i+3];
  15. if(!n){
  16. result.push("z");
  17. }else{
  18. for(j = 0; j < 5; b[j++] = n % 85 + 33, n = Math.floor(n / 85));
  19. }
  20. result.push(String.fromCharCode(b[4], b[3], b[2], b[1], b[0]));
  21. }
  22. };
  23. dojox.encoding.ascii85.encode = function(input){
  24. // summary: encodes input data in ascii85 string
  25. // input: Array: an array of numbers (0-255) to encode
  26. var result = [], reminder = input.length % 4, length = input.length - reminder;
  27. c(input, length, result);
  28. if(reminder){
  29. var t = input.slice(length);
  30. while(t.length < 4){ t.push(0); }
  31. c(t, 4, result);
  32. var x = result.pop();
  33. if(x == "z"){ x = "!!!!!"; }
  34. result.push(x.substr(0, reminder + 1));
  35. }
  36. return result.join(""); // String
  37. };
  38. dojox.encoding.ascii85.decode = function(input){
  39. // summary: decodes the input string back to array of numbers
  40. // input: String: the input string to decode
  41. var n = input.length, r = [], b = [0, 0, 0, 0, 0], i, j, t, x, y, d;
  42. for(i = 0; i < n; ++i){
  43. if(input.charAt(i) == "z"){
  44. r.push(0, 0, 0, 0);
  45. continue;
  46. }
  47. for(j = 0; j < 5; ++j){ b[j] = input.charCodeAt(i + j) - 33; }
  48. d = n - i;
  49. if(d < 5){
  50. for(j = d; j < 4; b[++j] = 0);
  51. b[d] = 85;
  52. }
  53. t = (((b[0] * 85 + b[1]) * 85 + b[2]) * 85 + b[3]) * 85 + b[4];
  54. x = t & 255;
  55. t >>>= 8;
  56. y = t & 255;
  57. t >>>= 8;
  58. r.push(t >>> 8, t & 255, y, x);
  59. for(j = d; j < 5; ++j, r.pop());
  60. i += 4;
  61. }
  62. return r;
  63. };
  64. })();
  65. }