SA-MP Forums Archive
[Include] CRC32 in PAWN - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] CRC32 in PAWN (/showthread.php?tid=486260)



CRC32 in PAWN - BigETI - 07.01.2014

crc32.inc
CRC32 (Cyclic redunancy check 32) in PAWN



About
Checksums are used to detect, if there are any bit errors within "n" amount of bytes, thus allows to detect raw data error, instead of using the broken data.
For more information, visit this Wikipedia page: http://en.wikipedia.org/wiki/Cyclic_redundancy_check



Why?
Since I've been working on some file decoders/encoders in C++, CRC32 for example is being used to generate a checksum from "n" amount of bytes. Therefore I've ported this useful algorithm to PAWN.
CRC32 itself is a 32 bits long (4 bytes) hash, which can be computed faster than most of the other frequently hash algorithms.
This include can be used for the socket plugin, also to decode and encode PNG files, and etc.



Benefits


Disantvantages


Documentation
Stocks

Quote:
crc32(const data, bool:packed = true, bool:reset_crc = true)
  • Description:
    • Generates a CRC32 checksum from a single variable.
    • If "packed" is true, all 4 bytes will be used for the computation, otherwise only the lowest byte.
    • If "reset_crc" is true, the last checksum will be reset, otherwise it allows procedural computation of a CRC32 checksum.
  • Usage:
    • Example:
      pawn Код:
      printf("checksum: 0x%08x", crc32(1234)); // Print the checksum of the number "1234"
      if(0xAA1006D2 == crc32(1234)) // Compare the checksum of the number "1234" with a pre-computed checksum
      {
          // Success
      }
      else
      {
          // Error
      }
      pawn Код:
      crc32(1000); // Generate a checksum of the number "1000"
      crc32(2000, _, false); // Generate a checksum of the number "2000", without resetting the least result (Procedural)
      crc32(3000, _, false); // Generate a checksum of the number "3000", without resetting the least result (Procedural)
      crc32(4000, _, false); // Generate a checksum of the number "4000", without resetting the least result (Procedural)
      crc32(5000, _, false); // Generate a checksum of the number "5000", without resetting the least result (Procedural)
      new my_checksum = crc32(6000, _, false); // Generate a checksum of the number "6000", without resetting the least result (Procedural)
      printf("my_checksum: 0x%08x", my_checksum); Print "my_checksum"
      if(0x29D9227F == my_checksum) // Compare "my_checksum" with a pre-computed checksum
      {
          // Success
      }
      else
      {
          // Error
      }
Quote:
crc32_arr(const data[], bool:packed = true, data_len = sizeof data, bool:reset_crc = true)
  • Description:
    • Generates a CRC32 checksum from an array.
    • It supports any kind of array including PAWN strings and packed PAWN strings.
    • If "packed" is true, all 4 bytes will be used for the computation, otherwise only the lowest byte each cell.
    • If "reset_crc" is true, the last checksum will be reset, otherwise it allows procedural computation of a CRC32 checksum.
  • Usage:
    • Example:
      pawn Код:
      printf("checksum: 0x%08x", crc32_arr("Hello World")); // Print the checksum of the "Hello World" string
      printf("checksum: 0x%08x", crc32_arr(!"Hello World")); // Print the checksum of the "Hello World" string, which is packed
      if(0xFAA6A7FB == crc32_arr("Hello World")) // Compare the checksum of the "Hello World" string with a pre-computed checksum
      {
          // Success
      }
      else
      {
          // Error
      }
      pawn Код:
      crc32_arr({10, 20, 30, 40}); // Generate a checksum of the array "{10, 20, 30, 40}"
      crc32_arr("Hi", _, _, false); // Generate a checksum of the "Hi" string, without resetting the least result (Procedural)
      new my_checksum = crc32_arr(!"OK", _, _, false); // Generate a checksum of the "OK" string, which is packed, without resetting the least result (Procedural)
      printf("my_checksum: 0x%08x", my_checksum); // Print "my_checksum"
      if(0x2A1D7399 == my_checksum) // Compare "my_checksum" with a pre-computed checksum
      {
          // Success
      }
      else
      {
          // Error
      }

Setup
Just download this include and save it in "Pawno\includes\" folder as "crc32.inc".

After that you can use it in your scripts by doing:

pawn Код:
// On top of your script
#include <crc32>
// Your code
Compile and run.



Download

Quote:
crc32.inc - Pastebin

Changelog

Quote:
  • v1.0 -> Initial release ( 08.01.2014 )

Credits

Quote:
  • BigETI (myself) for porting this into PAWN
  • W. Wesley Peterson for inventing CRC and many others inventing CRC32 during the 70s
  • SA:MP development team

Best Regards

~ BigETI



Re: CRC32 in PAWN - cantiliano - 08.01.2014

Very good guy


Re: CRC32 in PAWN - chneubeul - 08.01.2014

Nice script, may be useful in load from file system.