Kodewerx
https://www.kodewerx.org/forum/

ARM9 binary decompression code
https://www.kodewerx.org/forum/viewtopic.php?f=11&t=1048
Page 1 of 1

Author:  chishm [ Tue Mar 20, 2007 1:23 am ]
Post subject:  ARM9 binary decompression code

Here is some code to decompress compressed DS ARM9 binaries. I've had this bit of code sitting around for a while. Use it to make me some hook finders :P

sourceData should point to the start of the ARM9 binary and headerBottom should be the size of it. The compressed data "header" is actually a trailer, as the data is decompressed from the tail to the head, in place in memory.
Code:
unsigned int get_decompressed_data_size (const unsigned char* sourceData, unsigned int headerBottom) {
   unsigned int decompressedLength;
   decompressedLength = (((unsigned int*)sourceData)[headerBottom / 4 - 1]) + headerBottom;
   return decompressedLength;
}

void decompress_ARM9_binary (const unsigned char* sourceData, unsigned int headerBottom, unsigned char* destData) {
   
   int srcPos, destPos;

   int controlByte;

   int runsLeft;
   unsigned int srcLength, destLength;
   int srcStart;

   srcLength = ((unsigned int*)sourceData)[headerBottom / 4 - 2];
   destLength = (((unsigned int*)sourceData)[headerBottom / 4 - 1]) + headerBottom;

   srcPos = headerBottom - (srcLength >> 24);
   destPos = destLength;
   srcStart = headerBottom - (srcLength & 0x00FFFFFF);

   while (srcPos > srcStart) {
      controlByte = sourceData[--srcPos];

      for (runsLeft = 0x08; (runsLeft > 0) && (srcPos >= 0) && (destPos >=0); --runsLeft) {

         if (controlByte & 0x80) {
            // copy data run from decompressed buffer
            int runRepeats, runLength;   
            unsigned char temp;

            runRepeats = sourceData[--srcPos];
            runLength = ((sourceData[--srcPos] | (runRepeats << 8)) & 0x0fff) + 0x02;
            runRepeats = (runRepeats >> 4) + 0x2;
   
            for ( ; runRepeats >= 0; runRepeats-= 0x1) {
               temp = destData[destPos + runLength];
               destData[--destPos] = temp;
            }
         } else {
            // copy byte from source compressed data
            destData[--destPos] = sourceData[--srcPos];
         }

         controlByte = controlByte << 1;
      }
   }

   while ((srcPos >= 0) && (destPos >= 0)) {
      destData[--destPos] = sourceData[--srcPos];
   }
}

Author:  dlong [ Tue Mar 20, 2007 2:20 pm ]
Post subject: 

I may implement this into my master code searcher, if that is fine with you.

Author:  kickenchicken57 [ Tue Mar 20, 2007 2:52 pm ]
Post subject: 

thanks for sharing :)

Author:  Parasyte [ Tue Mar 20, 2007 3:45 pm ]
Post subject: 

I have a program that does this in addition to converting both executables to ELF format (which means it relocates each autoload section to the proper location in memory). It's entirely unmaintainable, but works so far. :X I've been planning to integrate it into Kwurdi If I can ever get it working again... Stupid devkitarm/ndslib...

Author:  chishm [ Tue Mar 20, 2007 10:09 pm ]
Post subject: 

dlong:
Sure, I posted it for people to use. Consider it public domain, although credit for the code would be nice.

Parasyte:
Sounds even better, but you haven't 1-up'd me until you release the source :P

Author:  Parasyte [ Tue Mar 20, 2007 10:42 pm ]
Post subject: 

I've said it before; it's going GPL.

Author:  NEo_Bazz [ Wed Mar 21, 2007 4:26 pm ]
Post subject: 

Parasyte wrote:
Stupid devkitarm/ndslib...

Author:  punani [ Fri Jul 13, 2007 6:32 am ]
Post subject:  Re: ARM9 binary decompression code

ndslib? Now that's old! :-P

Author:  dlong [ Fri Jul 13, 2007 6:33 am ]
Post subject:  Re: ARM9 binary decompression code

So was the post you replied to.

Author:  komdori [ Fri Jul 04, 2008 9:34 pm ]
Post subject:  Re: ARM9 binary decompression code

chishm wrote:
Here is some code to decompress compressed DS ARM9 binaries. I've had this bit of code sitting around for a while. Use it to make me some hook finders :P

sourceData should point to the start of the ARM9 binary and headerBottom should be the size of it. The compressed data "header" is actually a trailer, as the data is decompressed from the tail to the head, in place in memory.


Very nice! Hmm, am I doing something wrong? It seems to fail for certain cases, like 0836.

Page 1 of 1 All times are UTC - 8 hours [ DST ]
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/