News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

checksum to pe

Started by thomas_remkus, August 06, 2009, 01:42:40 PM

Previous topic - Next topic

thomas_remkus

How can I add the checksum of my application to the pe header?

qWord

AFAIK the linker does this automatically.

regards, qWord
FPU in a trice: SmplMath
It's that simple!

dedndave

the exe headers i have examined had both checksum values set to 0
you can use a program like PE Editor to set them if you like (or a hex-editor, for that matter)
but, it doesn't seem to be neccessary

evlncrn8

its only important for digitally signed ones, or drivers...
its quite simple though to calculate..

// * RFC 1141 : Incremental Updating of the Internet Checksum
//             also the algorythm microsoft use to calculate the pe checksum :)

//--------------------------------

static unsigned int pe_cksum (unsigned short int *addr, unsigned int len, unsigned long long base_sum) {

      unsigned int            nleft   = len;
      unsigned long long      sum      = ~base_sum + 1;
      unsigned short int      *w      = addr;
      unsigned short int      answer   = NULL;

      // Our algorithm is simple, using a 32 bit accumulator (sum), we add
      // sequential 16 bit words to it, and at the end, fold back all the
      // carry bits from the top 16 bits into the lower 16 bits.

      while ( nleft > 1 ) {
            sum      += *w++;
            nleft   -= 2;
      }

      // mop up an odd byte, if necessary

      if ( 1 == nleft ) {
            *( unsigned char * )( &answer )   = *( unsigned char * )w;
            sum   += answer;
      }

      // add back carry outs from top 16 bits to low 16 bits
      // add hi 16 to low 16
      sum      = ( sum >> 16 ) + ( sum & 0xFFFF );
      // add carry
      sum      += ( sum >> 16 );
      // * truncate to 16 bits
      answer   = ( unsigned short )( sum & 0xFFFF );
      // * add len
      sum      = answer + len;
      return( ( unsigned int )sum );
}


think its also exported somewhere.. most likely covered here in some thread...

qWord

FPU in a trice: SmplMath
It's that simple!

Mirno

Code I wrote ages ago (and is now in circulation amongst several asm programmers) is:


    mov ecx, [len]
    mov edx, [buf]
    shr  ecx, 1
    xor  eax, eax
@@:
    adc ax, [edx + (ecx * 2) - 2]
    dec ecx
    jnz @b
    adc eax, [len]
    ret


Remember to zero out the checksum dword first, before calculating.

Thanks,

Mirno