flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > CRC32 calculating macros

Author
Thread Post new topic Reply to topic
Grom PE



Joined: 13 Mar 2008
Posts: 114
Location: i@grompe.org.ru
Grom PE 31 Mar 2009, 06:05
Code:
; CRC32 calculating macros
; v1.00
; for flat assembler by Grom PE
;
; Notes:
;   calculating CRC32 of 10 mb file takes about 17s on my Pentium4 1.7 GHz
;
; Limitations:
;   data between crc32start and crc32end must be duplicated in case you
;   want to include it in resulting file, that's because "virtual"
;   directive doesn't allow to "load" and "store" from outside of it.
;
; Usage example:
;
; crc32filecheck 'test.bin', 0xb5a214e8
; ; - Useful together with my patching macros, for example =)
; ;   (http://board.flatassembler.net/topic.php?t=8744)
;
; crc32start
;   db 'This string has its own CRC32:'
; crc32end
;   db 'This string has its own CRC32:'
;   dd crc32value

macro crc32start
{
  crc32value = 0xFFFFFFFF
  virtual
}

macro crc32end
{
  local _r, _t, _l
    _l = $-$$
    crc32table rd 256

    repeat 256
      _r = %-1
      repeat 8
        _r = _r shr 1 xor (0xEDB88320 * _r and 1)
      end repeat
      store dword _r at crc32table+%*4-4
    end repeat

    repeat _l
      load _p byte from $$+%-1
      load _t dword from crc32table+(_p xor (crc32value and 0xFF))*4
      crc32value = crc32value shr 8 xor _t
    end repeat
    crc32value = crc32value xor 0xFFFFFFFF
  end virtual
}

macro crc32file filename*
{
  crc32start
    file filename
  crc32end
}

macro crc32filecheck filename*, value*
{
  crc32file filename
  if crc32value <> value
    "Error: crc32 doesn't match!"
  end if
}    
Post 31 Mar 2009, 06:05
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 31 Mar 2009, 06:14
Nice to see you are using the table method for CRC.

Not sure if you have seen it before, but if you are interested in computing file signatures with macros then see: SHA512 macros
Post 31 Mar 2009, 06:14
View user's profile Send private message Visit poster's website Reply with quote
Grom PE



Joined: 13 Mar 2008
Posts: 114
Location: i@grompe.org.ru
Grom PE 31 Mar 2009, 06:36
revolution, yes, I've seen your SHA512 macros, haven't got a practical use for them, but source of inspiration =)
Post 31 Mar 2009, 06:36
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 31 Mar 2009, 07:06
Since you are using the ISO3309 polynomial do you know what is special about this value: 0x2144DF1C? Answer below.

I use it in a lot of my crc checks.




If you append the computed crc to the end of the message and then the receiver computes the crc of the entire received message (including the crc) the magic value 0x2144DF1C always appears.
Post 31 Mar 2009, 07:06
View user's profile Send private message Visit poster's website Reply with quote
Grom PE



Joined: 13 Mar 2008
Posts: 114
Location: i@grompe.org.ru
Grom PE 31 Mar 2009, 07:18
Interesting, I didn't know about this.
Is there much difference anyway between
calculating CRC of message+CRC then checking against 0x2144DF1C and
calculating CRC of message and checking against the received CRC?
Post 31 Mar 2009, 07:18
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 31 Mar 2009, 07:28
Not really a lot of difference, but a neat mathematical trick.

I used it the do checking on blocks of NAND flash. I have one optimised function to compute CRC that is based upon a 32 bytes per loop structure. If I didn't use the "magic" constant then I would have had to make an addition to the CRC loop and put in a special tail section to compute just the last 28 bytes. Instead, with the "magic" constant, I can do a full loop without special care and check for the fixed value 0x2144DF1C.
Post 31 Mar 2009, 07:28
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 01 Apr 2009, 12:20
Nice job! Smile
Post 01 Apr 2009, 12:20
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20430
Location: In your JS exploiting you and your system
revolution 04 May 2009, 11:43
Code:
macro embed_crc start,finish {
 local b,c
   c = 0xffffffff
      repeat finish-start
         load b byte from start+%-1
          c = c xor b
         repeat 8
                    c = (c shr 1) xor (0xedb88320 * (c and 1))
          end repeat
  end repeat
  dd c xor 0xffffffff
}    
start is inclusive and finish is exclusive.

Use like this
Code:
MyCode:
   mov     eax,0x12345678
      xor     eax,edx
     ret

     embed_crc $$,$    
Post 04 May 2009, 11:43
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.