PDA

View Full Version : IMG Compress/Decompress algorithm!!!


acme_pjz
12 Jun 2008, 13:11
I had discovered the compress and decompress algorithm of *.img file by decompile landgen.exe using IDA+HexRays Decompiler.

These are decompress codes:
Private Sub DecompressTest(lpbm() As Byte, lpbuf() As Byte)
On Error GoTo a
Dim v5 As Long 'repeat count
Dim nbm As Long 'lpbm
Dim nbuf As Long 'lpbuf
Dim nOffset As Long
'////////////////compress format
'////////////////1 byte:
'0xxxxxxx
'x=color=0-127
'////////////////2-byte: repeat=3-17
'1rrrrooo oooooooo
'r+2=repeat
'o+1=offset=1-&h800&
'////////////////3-byte: repeat=18-273
'10000ooo oooooooo rrrrrrrr
'r+18=repeat
'o=offset=1-&h7ff&
'////////////////2 byte: EOF
'10000000 00000000=&h80 &h00
Do
Do
Do
v5 = lpbuf(nbuf)
If v5 And &H80& Then Exit Do
lpbm(nbm) = v5 'm_clrs(v5)
nbuf = nbuf + 1
nbm = nbm + 1
Loop
nOffset = (v5 * 256& + lpbuf(nbuf + 1)) And &H7FF&
v5 = (v5 \ 8&) And &HF&
If v5 = 0 Then Exit Do
nOffset = nOffset + 1
v5 = v5 + 2
Do
lpbm(nbm) = lpbm(nbm - nOffset)
nbm = nbm + 1
v5 = v5 - 1
Loop While v5
nbuf = nbuf + 2
Loop
If nOffset = 0 Then Exit Do
v5 = lpbuf(nbuf + 2) + 18&
Do
lpbm(nbm) = lpbm(nbm - nOffset)
nbm = nbm + 1
v5 = v5 - 1
Loop While v5
nbuf = nbuf + 3
Loop
Exit Sub
a:
MsgBox "Error in decompressing data!", vbExclamation
End Sub

and the compress algorithm:

pisto
15 Jul 2008, 13:21
thx. but what language is that code written in?

robowurmz
15 Jul 2008, 14:28
Looks like Visual Basic.

pisto
15 Jul 2008, 20:03
Looks like Visual Basic.
it's completely unreadable for me.
acme, could you translate it into pseudocode / C?

Etho.
18 Jul 2008, 03:40
I'm fairly fluent with Visual Basic, but I'm also having a hard time understanding what you are doing here. Could you please add some code comments or possibly a working example of compression and decompression?

pisto
18 Jul 2008, 18:33
first. I hate Visual Basic sintax.
second. why the hell do you use loops where humans use if statements??

I'll post a C version of the decompression algorithm, but anyway, the offset that you get from the 3 or 2 bytes command (let's call it coff), and off is the index of the next byte to be written in the output buffer (out[]), and i is the number of iterations:
for(;i>0;i--)
out[off]=out[off++-coff];
looks strange.

EDIT:
Etho, I hope that this java code is readable for you (inf is a stream of bytes)

String decode() throws IOException {
//[...]
int cmd;
int next=0; //offset of next write
while((cmd=inf.read())!=-1){ //read a byte
if((cmd&0x80)==0) //command: 1 byte (color)
imgData[next++]=(byte) cmd;
else{
int arg1=(cmd>>3)&0xF, //arg1=bits 2-5
arg2=inf.read();
if(arg2==-1)
return "not enough data";
arg2=((cmd<<8)|arg2)&0x7FF; //arg2=bits 6-16
if(arg1==0){
if(arg2==0) //command: 0x80 0x00
return null;
int arg3=inf.read();
if(arg3==-1)
return "not enough data";
next=copyData(next,arg2,arg3+18); //command: 3 bytes
}
else
next=copyData(next,arg2+1,arg1+2); //command: 2 bytes
}
}
if(next!=imgData.length)
return "not enough data";
return null;
}

int copyData(int offset,int coffset,int rep){
for(;rep>0;rep--)
imgData[offset]=imgData[offset++-coffset];
return offset;
}
damn, no indexing...

I'm going to test this soon

pisto
19 Jul 2008, 08:53
good job acme_pjz, your algorithm works perfectly! I could write a IMG converter, from compressed to uncompressed format, and the result is identical to W:A decompression

Etho.
19 Jul 2008, 19:27
It works? That's great. I know a few people, myself included, who could have really used this information a few years ago. It still has potential to be very useful, it's just that the number of 3rd party programmers for Worms has dwindled a lot.

But anyways... who's going to make the first W:A mission editor?

Muzer
21 Jul 2008, 19:43
But anyways... who's going to make the first W:A mission editor?
There already is one, The Fiddler. But as you know, it is 3.0 only.

pisto
27 Jul 2008, 15:55
It works? That's great. I know a few people, myself included, who could have really used this information a few years ago. It still has potential to be very useful, it's just that the number of 3rd party programmers for Worms has dwindled a lot.

But anyways... who's going to make the first W:A mission editor?

I see that the img format is optimized for default sized maps.

well, probably next RubberWorm will be able to set events and sequences online,and other settings from wam files.

acme_pjz
7 Aug 2008, 03:41
Thanks for the Interactive Disassembler and Hex-Rays Decompiler... make it possible to decompile the landgen.exe assembler to C-like pseudocode.

There is just a problem that my compress algorithm is not very good... the compressed file is usually a bit bigger then the original one...

why the hell do you use loops where humans use if statements??The decompiled code is exactly three loops...

pisto
7 Aug 2008, 15:58
why the hell do you use loops where humans use if statements??oh right, decompiler isn't human:p

acme_pjz
13 Aug 2008, 08:42
HAHA ;D :D :) :lol