Transform lz77 decompression from JAVASCRIPT to C#

Apr 6 2009 6:12 AM

i am a students, i am transform a code about lz77 decompression in c# from JSCRIPT
this is my c# code

public class lz77DeCompression
{
    private char ReferencePrefix;
    private int ReferencePrefixCode;
    private int ReferenceIntBase;
    private int ReferenceIntFloorCode;
    private int ReferenceIntCeilCode;
    private int MaxStringDistance;
    private int MinStringLength;
    private int MaxStringLength;
    private int MaxWindowLength;

    public lz77DeCompression()
    {
        this.ReferencePrefix ='`';
        this.ReferencePrefixCode = (int)this.ReferencePrefix;
        this.ReferenceIntBase = 96;
        this.ReferenceIntFloorCode =(int)' ';
        this.ReferenceIntCeilCode = this.ReferenceIntFloorCode + this.ReferenceIntBase -1;
        this.MaxStringDistance = (int)Math.Pow(this.ReferenceIntBase ,2)-1;
        this.MinStringLength = 5;
        this.MaxStringLength = (int)Math.Pow(this.ReferenceIntBase, 1) - 1 + this.MinStringLength;
        this.MaxWindowLength = this.MaxStringDistance + this.MinStringLength;
    }
   
    private int decodeReferenceInt(string words, int width)
    {
       int value;
       int i;
       int charcode;
       value = 0;
      
       for (i = 0; i < width;i++ )
       {
           value *= this.ReferenceIntBase;
           charcode = (int)words[i];
         
               if ((charcode >= this.ReferenceIntFloorCode) && (charcode <= this.ReferenceIntCeilCode))
               {
                   value += charcode - this.ReferenceIntFloorCode;
               }
      
       
          /* else
           {
               Response.Write ( "<script type=\"javascript\">alert("+ charcode+")<//script>");
           }*/
       }
       return value;
    }

    private int decodeReferenceInt(char words, int width)
    {
        int value;
        int i;
        int charcode;
        value = 0;

        for (i = 0; i < width; i++)
        {
            value *= this.ReferenceIntBase;
            charcode = (int)words;

            if ((charcode >= this.ReferenceIntFloorCode) && (charcode <= this.ReferenceIntCeilCode))
            {
                value += charcode - this.ReferenceIntFloorCode;
            }


            /* else
             {
                 Response.Write ( "<script type=\"javascript\">alert("+ charcode+")<//script>");
             }*/
        }
        return value;
    }
   
    private int decodeReferenceLength(char words)
    {
        return decodeReferenceInt(words, 1) + this.MinStringLength;
    }
   
    public string decompress(string words)
    {
        string decompressed;
        int pos,distance,length;
        int getSubString;
        char currentChar;
        char nextChar;
        decompressed ="";
        pos = 0;
        while (pos < words.Length)
        {
            currentChar = words[pos];
            if (currentChar!=this.ReferencePrefix)
            {
                decompressed += currentChar;
                pos++;
            }
            else
            {
                nextChar = words[pos + 1];
                if (nextChar != this.ReferencePrefix)
                {
                    distance = decodeReferenceInt(words.Substring(pos + 1, 2), 2);
                    length = decodeReferenceLength (words[pos+3]);
                    getSubString=decompressed.Length - distance - length;
                    decompressed += decompressed.Substring(getSubString, length);
                    pos += this.MinStringLength - 1;

                }
                else
                {
                    decompressed += this.ReferencePrefix;
                    pos += 2;
                }
            }
        }
        return decompressed;
    }
}

and this the JSCRIPT code

//lz77 class
ReferencePrefix = "`";
ReferencePrefixCode = ReferencePrefix.charCodeAt(0);

ReferenceIntBase = 96;
ReferenceIntFloorCode = " ".charCodeAt(0);
ReferenceIntCeilCode = ReferenceIntFloorCode + ReferenceIntBase - 1;

MaxStringDistance = Math.pow(ReferenceIntBase, 2) - 1;
MinStringLength = 5;
MaxStringLength = Math.pow(ReferenceIntBase, 1) - 1 + MinStringLength;

MaxWindowLength = MaxStringDistance + MinStringLength;

function decodeReferenceInt(data, width) {
  var value = 0;
  for (var i = 0; i < width; i++) {
    value *= ReferenceIntBase;
    var charCode = data.charCodeAt(i);
    if ((charCode >= ReferenceIntFloorCode) && (charCode <= ReferenceIntCeilCode)) {
      value += charCode - ReferenceIntFloorCode;
    } else {
      throw "Invalid char code in reference int: " + charCode;
    }
  }
  return value;
}

function decodeReferenceLength(data) {
  return decodeReferenceInt(data, 1) + MinStringLength;
}

function decompress(data) {
  var decompressed = "";
  var pos = 0;
  while (pos < data.length) {
    var currentChar = data.charAt(pos);
    if (currentChar != ReferencePrefix) {
      decompressed += currentChar;
      pos++;
    } else {
      var nextChar = data.charAt(pos + 1);
      if (nextChar != ReferencePrefix) {
        var distance = decodeReferenceInt(data.substr(pos + 1, 2), 2);
        var length = decodeReferenceLength(data.charAt(pos + 3));
        decompressed += decompressed.substr(decompressed.length - distance - length, length);
        pos += MinStringLength - 1;
      } else {
        decompressed += ReferencePrefix;
        pos += 2;
      }
    }
  }
  return decompressed;
}
//end class

when i give input input in c# code
input :
I'm just a little bit caught in the middle
Life is a maze and lov` ,"r` >!I don't know where to go I can't do it alone I've tried
And I don't know why
then the output :
I'm just a little bit caught in the middle
Life is a maze and love is a rddle
I don't know where to go I can't do it alone I've tried
And I don't know why
when i input in jscript
input :
I'm just a little bit caught in the middle
Life is a maze and lov` ,"r` >!I don't know where to go I can't do it alone I've tried
And I don't know why
then the output
output:
I'm just a little bit caught in the middle
Life is a maze and love is a riddle
I don't know where to go I can't do it alone I've tried
And I don't know why

the right output is in JSCRIPT, in c# there is missing one character
but when i try differen input :
in c#
input :
I'm just a little bit caught in the middle Life is a maze and lov` ,"r` >!I don't know where to go I can't do it alone I've tried And I don't know why
output :
I'm just a little bit caught in the middle Life is a maze and love is a riddle I don't know where to go I can't do it alone I've tried And I don't know why

in JSCRIPT
input :
I'm just a little bit caught in the middle Life is a maze and lov` ,"r` >!I don't know where to go I can't do it alone I've tried And I don't know why
output :
I'm just a little bit caught in the middle Life is a maze and love is a riddle I don't know where to go I can't do it alone I've tried And I don't know why

u can see, they have same output, and the both output are right ..

i am confuse about my program, i dont know where is the error, and what is the solution ..
i hope somebody here, can help me to solve my problem with that code ..
thank you very much ..

regards
novhard..


Answers (1)