Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 649269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:55:33+00:00 2026-05-13T21:55:33+00:00

I am attempting to reverse engineer an LZ1/LZ77 decompression algorithm. The length of an

  • 0

I am attempting to reverse engineer an LZ1/LZ77 decompression algorithm. The length of an area of the decode buffer/window to be output is encoded in the file as a variable length integer. I’ve read as much as I can about variable length integer encoding and the method being used in this case does not appear to be like any others I have seen. Perhaps to avoid patent issues or maybe just to obfuscate. The included code might not be quite complete but it is working on at least several files at this point.

I cannot see how, if at all, the formulas being used below could be reduced into something simpler. Most of the variable length integer encoding algorithms use some sort of loop but for this one, I have not been able to do that because the formula just doesn’t seem to be consistent when evaluating each nibble.

Suggestions are greatly appreciated.

private static int getLength(BitReader bitStream)
{
    const int minSize = 2;

    int length = 0;

    byte nibble3, nibble2, nibble1;

    nibble3 = bitStream.ReadNibble();

    if (nibble3 >= 0xc)
    {
        nibble2 = bitStream.ReadNibble();
        nibble1 = bitStream.ReadNibble();

        if (nibble3 == 0xF & nibble2 == 0xF & nibble1 == 0xF) return -1;

        if ((nibble3 & 2) != 0)
        {
            length = (((((nibble3 & 7) + 3) << 6) + 8)) + 
                ((nibble2 & 7) << 3) + nibble1 + minSize;
        }
        else if ((nibble3 & 1) != 0)
        {
            length = (((nibble3 & 7) << 6) + 8) + 
                ((((nibble2 & 7)) + 1) << 3) + nibble1 + minSize;
        }
        else
        {
            length = ((((nibble3 & 7) << 4) + 8)) + 
                ((nibble2 & 7) << 4) + nibble1 + minSize;
        }
    }
    else if ((nibble3 & 8) != 0)
    {
        nibble1 = bitStream.ReadNibble();

        length = ((((nibble3 & 7) << 1) + 1) << 3) + nibble1 + minSize;
    }
    else
    {
        length = nibble3 + minSize;
    }

    return length;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:55:33+00:00Added an answer on May 13, 2026 at 9:55 pm

    It turns out that the variable length integer encoding algorithm being used is very similar to the Dlugosz’ Variable-Length Integer Encoding method. Indeed, there are multiple calculations that are required, rather than a single formula.

    Based on that, I re-wrote the code as follows. I am still trying to figure out the exact format of the mechanism where a leading 0xFFF is used.

        private static int getLength(BitReader bitStream)
        {
            const int minSize = 2;
            int length = 0;
            byte nibble3, nibble2, nibble1;
            byte nibble;
            nibble = bitStream.ReadNibble();
            if (nibble == 0xF)
            {
                nibble2 = bitStream.ReadNibble();
                nibble1 = bitStream.ReadNibble();
                if (nibble2 == 0xf && nibble1 == 0xF)
                {
                    //The next nibble specifies the number of nibbles to be read, maybe.
                    byte nibblesToRead = (byte) (bitStream.ReadNibble()) ;
                    //The Dlugosz' mechanism would use a mask on the value but that doesn't appear to be the case here.
                    //nibblesToRead &= 7;
                    //switch (nibblesToRead & 7){
                    //    case 0: nibblesToRead = 5; break;
                    //    case 1: nibblesToRead = 8; break;
                    //    case 2: nibblesToRead = 16; break;                           
                    //}
                    byte value=0;
                    byte[] values = new byte[nibblesToRead];
                    bool c=true;
                    for (int i = 0; i < nibblesToRead; i++)
                    {
                        value = bitStream.ReadNibble();
                        //values[i] = value;
                        length += (((value << 1) | 1) << 3);
                    }
                    value = bitStream.ReadNibble();
                    length += value;
                }
            }
            else if((nibble >= 0xC)){
               nibble2 = bitStream.ReadNibble();
               nibble1 = bitStream.ReadNibble();
               length = ((((((nibble & 1) <<1)|1))<< 3) + ((nibble2<<1)|1)<<3)+nibble1;
            }
            else if ((nibble & 8)!=0){
                nibble1 = bitStream.ReadNibble();
                length = ((((nibble & 3)<<1) | 1) << 3) + nibble1;
            }
            else{
                length=nibble;
            }
            return length + minSize;
          };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.