All pastes #819959 Raw Edit

Mine

public text v1 · immutable
·published 2007-12-17 08:51 UTC
rendered paste body
//This is my code (ignore the debug-output):

private String decodeString(String encodedStuff)
    {
        byte[] encodedString = encodedStuff.getBytes();
        byte[] decodedString = new byte[encodedString.length];

        byte mask = '0';
        int pos = 0;
        int dpos = 0;

        while (pos != encodedString.length)
        {
            if (pos % 8 == 0)
            {
                mask = encodedString[pos];
                System.out.println("New Mask: " + mask);
            }
            else
            {
                byte b = encodedString[pos];
                if ((mask & (0x1 << (pos % 8))) == 0)
                {
                    System.out.println("decreasing");
                    decodedString[dpos++] = --b;
                }
                else
                {
                    System.out.println("keeping");
                    decodedString[dpos++] = b;
                }
                System.out.println(new String(decodedString));
            }
            pos++;
        }
        return new String(decodedString);
    }



// This is the 'C' function that the documentation gives me as an example of how to decode:

There are the GameSetting and three null-terminated strings here back-to-back,
all encoded into a single null terminated string.
(But we don't know the reason for this encoding!)

Every even byte-value was incremented by 1. So all encoded bytes are odd.
A control-byte stores the transformations for the next 7 bytes.

Since all NullBytes were transformed to 1, they will never occure inside the
encoded string. But a NullByte marks the end of the encoded string.

The encoded string starts with a control byte.
The control byte holds a bitfield with one bit for each byte of the next 7
bytes block. Bit 1 (not Bit 0) corresponds to the following byte right after
the control-byte, bit 2 to the next, and so on.
Only Bit 1-7 contribute to encoded string. Bit 0 is unused and always set.

Decoding these bytes works as follows:

If the corresponding bit is a '1' then the character is moved over directly.
If the corresponding bit is a '0' then subtract 1 from the character.

After a control-byte and the belonging 7 bytes follows a new control-byte
until you find a NULL character in the stream.


Example decompression code (in 'C'):

char* EncodedString;
char* DecodedString;
char  mask;
int   pos=0;
int   dpos=0;

while (EncodedString[pos] != 0)
{
  if (pos%8 == 0) mask=EncodedString[pos];
  else
  {
    if ((mask & (0x1 << (pos%8))) == 0)
      DecodedString[dpos++] = EncodedString[pos] - 1;
    else
      DecodedString[dpos++] = EncodedString[pos];
  }
  pos++;
}



//
// This is the output I get:
//

Laqs]Envnload\DotA Allstars v7/59c.v3xMYM|LevenT

//
// This is what I approximately expect:
//

Maps\Download\DotA Allstars v.6.49b.w3gMYM|Levent