Advertisement
rg443

javascript LZW byte array

Jan 27th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. LZW = {
  2.  
  3.     // 158 bytes
  4.     compress: function (
  5.         a, // String to compress
  6.         b, // Placeholder for dictionary
  7.         c, // Placeholder for dictionary size
  8.         d, // Placeholder for iterator
  9.         e, // Placeholder for w
  10.         f, // Placeholder for result
  11.         g, // Placeholder for c
  12.         h  // Placeholder for wc
  13.     ){
  14.  
  15.         for (b = {}, c = d = 256; d--;)
  16.  
  17.             b[String.fromCharCode(d)] = d;
  18.  
  19.         for (e = f = []; g = a[++d];)
  20.  
  21.             e = b[h = e + g] ? h : (f.push(b[e]), b[h] = c++, g);
  22.  
  23.         f.push(b[e]);
  24.  
  25.         return f
  26.  
  27.     },
  28.  
  29.     // 158 bytes
  30.     decompress: function (
  31.         a, // Array to decompress
  32.         b, // Placeholder for dictionary
  33.         c, // Placeholder for dictionary size
  34.         d, // Placeholder for iterator
  35.         e, // Placeholder for w
  36.         f, // Placeholder for result
  37.         g, // Placeholder for entry
  38.         h  // Placeholder for k
  39.     ) {
  40.  
  41.         for (b = [], c = d = 256, e = String.fromCharCode; d--;)
  42.  
  43.             b[d] = e(d);
  44.  
  45.         for (e = f = e(a[d = 0]); (h = a[++d]) <= c;)
  46.  
  47.             g = b[h] || e + e[0], b[c++] = e + g[0], f += e = g;
  48.  
  49.         return f
  50.  
  51.     }
  52.  
  53. }
  54.  
  55. // utils
  56. function utf8b64( str ) {return window.btoa(unescape(encodeURIComponent( str )));}
  57.  
  58. function b64utf8( str ) {return decodeURIComponent(escape(window.atob( str )));}
  59.  
  60. // var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)));
  61. // c.map(function(c){return String.fromCharCode(c)}).join("");
  62.  
  63. /* usage:
  64. var c=LZW.compress("abcabc"); // byte array
  65. LZW.decompress(c);
  66. var base64String = btoa(String.fromCharCode.apply(null, new Uint8Array( LZW.compress("abcabc") )));
  67. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement