Posts: 1,046
Threads: 29
Joined: Mar 2010
07.01.2014, 22:06
(
Последний раз редактировалось BigETI; 08.01.2014 в 01:09.
)
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
- Allows fast checksum comparison
- The algorithm computes pretty much fast
- Widely used to generate checksums (examples from Wikipedia: HDLC, ANSI X3.66, ITU-T V.42, Ethernet, Serial ATA, MPEG-2, PKZIP, Gzip, Bzip2, PNG, and more...)
Disantvantages
- Do NOT store CRC32 hashed passwords, use a slower but more secure algorithm like Whirlpool and salts! Video: [ame=http://www.youtube.com/watch?v=8ZtInClXe1Q]How NOT to Store Passwords! - Computerphile[/ame]
- Do NOT rely on checksums computed from ridiculous large arrays! CRC32 may collide more frequently, thus claiming broken data to be valid. Use checksum algorithms designed for very large data streams instead.
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
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
Posts: 47
Threads: 4
Joined: May 2009
Reputation:
0
Nice script, may be useful in load from file system.