1  /**
     2   * com.sekati.crypt.LZW
     3   * @version 1.0.3
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   * 
     8   * Sourced from ascrypt for dependencies only - version 1.1, author Mika Pamu
     9   * Original Flash port by Ash & Lalek & Shoebox and others.
    10   * @see http://www.razorberry.com/blog/archives/2004/08/22/lzw-compression-methods-in-as2/
    11   * @see http://www.lalex.com/blog/comments/200405/164-compression-lzw-actionscript-2.html
    12   */
    13  
    14  import com.sekati.crypt.ICipher;
    15  
    16  /**
    17   * Compresses and decompresses text with the LZW algorithm.
    18   */
    19  class com.sekati.crypt.LZW implements ICipher {
    20  
    21  	/**
    22  	 * Compresses the specified text.
    23  	 * @param src (String)
    24  	 * @return String
    25  	 */
    26  	public static function compress(src:String):String {
    27  		var chars:Number = 256;
    28  		var original:String = src;
    29  		var dict:Array = new Array( );
    30  		var i:Number;
    31  		var xstr:String;
    32  		for (i = 0; i < chars ; i++) dict[String( i )] = i;
    33  		var result:String = new String( "" );
    34  		var splitted:Array = original.split( "" );
    35  		var buffer:Array = new Array( );
    36  		for (i = 0; i <= splitted.length ; i++) {
    37  			var current:String = splitted[i];
    38  			if (buffer.length == 0) xstr = String( current.charCodeAt( 0 ) );
    39  			else xstr = buffer.join( "-" ) + "-" + String( current.charCodeAt( 0 ) );
    40  			if (dict[xstr] !== undefined) {
    41  				buffer.push( current.charCodeAt( 0 ) );
    42  			} else {
    43  				result += String.fromCharCode( dict[buffer.join( "-" )] );
    44  				dict[xstr] = chars;
    45  				chars++;
    46  				delete buffer;
    47  				buffer = new Array( );
    48  				buffer.push( current.charCodeAt( 0 ) );
    49  			}
    50  		}
    51  		return result;
    52  	}
    53  
    54  	/**
    55  	 * Decompresses the specified text.
    56  	 * @param src (String)
    57  	 * @return String 
    58  	 */
    59  	public static function decompress(src:String):String {
    60  		var chars:Number = 256;
    61  		var dict:Array = new Array( );
    62  		var i:Number;
    63  		for (i = 0; i < chars ; i++) {
    64  			var c:String = String.fromCharCode( i );
    65  			dict[i] = c;
    66  		}
    67  		var original:String = src;
    68  		var splitted:Array = original.split( "" );
    69  		var buffer:String = new String( "" );
    70  		var chain:String = new String( "" );
    71  		var result:String = new String( "" );
    72  		for (i = 0; i < splitted.length ; i++) {
    73  			var code:Number = original.charCodeAt( i );
    74  			var current:String = dict[code];
    75  			if (buffer == "") {
    76  				buffer = current;
    77  				result += current;
    78  			} else {
    79  				if (code <= 255) {
    80  					result += current;
    81  					chain = buffer + current;
    82  					dict[chars] = chain;
    83  					chars++;
    84  					buffer = current;
    85  				} else {
    86  					chain = dict[code];
    87  					if (chain == undefined) chain = buffer + buffer.slice( 0, 1 );
    88  					result += chain;
    89  					dict[chars] = buffer + chain.slice( 0, 1 );
    90  					chars++;
    91  					buffer = chain;
    92  				}
    93  			}
    94  		}
    95  		return result;
    96  	}
    97  
    98  	private function LZW() {
    99  	}
   100  }