Kodewerx

Our culture has advanced beyond all that you could possibly comprehend with one hundred percent of your brain.
It is currently Sun Apr 28, 2024 1:04 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Tue Mar 20, 2007 1:23 am 
Offline
Kommunist
Kommunist

Joined: Wed Feb 28, 2007 5:52 pm
Posts: 79
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];
   }
}


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Mar 20, 2007 2:20 pm 
I may implement this into my master code searcher, if that is fine with you.


Top
  
Reply with quote  
 Post subject:
PostPosted: Tue Mar 20, 2007 2:52 pm 
Offline
Kommunist
Kommunist

Joined: Mon Jan 29, 2007 2:23 pm
Posts: 313
thanks for sharing :)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Mar 20, 2007 3:45 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
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...

_________________
I have to return some video tapes.

Feed me a stray cat.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Mar 20, 2007 10:09 pm 
Offline
Kommunist
Kommunist

Joined: Wed Feb 28, 2007 5:52 pm
Posts: 79
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Mar 20, 2007 10:42 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
I've said it before; it's going GPL.

_________________
I have to return some video tapes.

Feed me a stray cat.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Mar 21, 2007 4:26 pm 
Offline
Kommunist
Kommunist
User avatar

Joined: Tue Oct 03, 2006 9:39 am
Posts: 312
Location: 4e-2f-41
Parasyte wrote:
Stupid devkitarm/ndslib...

_________________
Quote:
Fix your shitty signature, bitchcakes.

Ok, I did.


Top
 Profile  
Reply with quote  
PostPosted: Fri Jul 13, 2007 6:32 am 
Offline
Kommunist
Kommunist

Joined: Thu Feb 15, 2007 6:55 am
Posts: 2
ndslib? Now that's old! :-P


Top
 Profile  
Reply with quote  
PostPosted: Fri Jul 13, 2007 6:33 am 
So was the post you replied to.


Top
  
Reply with quote  
PostPosted: Fri Jul 04, 2008 9:34 pm 
Offline
Kommunist
Kommunist

Joined: Mon Jun 23, 2008 3:26 am
Posts: 1
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 36 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group