crc32
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
crc32 � ��������� ������� CRC32 ��� ������
��������
������� ��������� ����������� ���������� ��� 32-������ ��������� (CRC32) ��� ������ str . ��� ������ ������������ ��� �������� ����������� ������������ ������.
� PHP ����� ����� ����� ����, ������� ������ ����������� ����� ����� ��������� �������������� �� 32-������ ����������. �� 64-������ ���������� ��� ���������� crc32() ����� �������������� ������.
������� ��� ����� ������������ ������ «%u» � �������� sprintf() ��� printf() ��� ��������� ���������� ������������� ����� crc32() ��� �����.
��� ������������������ ������������� ����� �� ������ ������������ ��� ������ «%x» � �������� sprintf() � printf() , ��� �� ������� ����������� dechex() . ��� ���� ������� ����� ����������� � ����������� ���������� crc32() � ���������� �����.
��� ������������� 64-������ �������� ����� ��������������� ����������� ���������� ������������� ����� ��� ������� ��������, �� ��� ������ ����������������� �������������, �������� �������������� 0xFFFFFFFF######## �������� ��� ���. ��� ��� ����������������� ������������� �������� ����� ��������������, ���� ������ �� ������ ���, ���� ���� ��� ������ ������ ��������� ������������ �������� � 50% ������� ��� �������� � 32-������ �� 64-������ ���������.
����������� �����, �������� ���������� ����� ����� ���� �� ����� ������ ����� � ����� ���� ���������� ����� ����������������� ������������� (��� �������� ������ md5() ).
����� ��������������� ����� ����� ����� �������� � �������������� ������� hash() . hash(«crc32b», $str) ������ �� �� ������, ��� � str_pad(dechex(crc32($str)), 8, ‘0’, STR_PAD_LEFT) .
crc32
(PHP 4 >= 4.0.1, PHP 5)
crc32 � Вычисляет полином CRC32 для строки
Описание
Функция вычисляет циклический избыточный код 32-битных полиномов (CRC32) для строки str . Это обычно используется для контроля целостности передаваемых данных.
В PHP целые числа имеют знак, поэтому многие контрольные суммы могут оказаться отрицательными. Для получения строкового представления CRC32 без знака используйте формат «%u» в функциях sprintf() или printf() .
Список параметров
Возвращаемые значения
Возвращает контрольную сумму crc32 строки str в виде целого числа.
Примеры
Пример #1 Вывод контрольной суммы CRC32
Этот пример иллюстрирует вывод вычисленной контрольной суммы с помощью функции printf() :
Смотрите также
- md5() — Возвращает MD5-хэш строки
- sha1() — Возвращает SHA1-хэш строки
Crc32 — ��������� CRC32 ��� ������
(PHP 4 >= 4.0.1, PHP 5)
crc32 — ��������� CRC32 ��� ������
�������� int crc32 ( string str )
������� ��������� ����������� ����� �� ��������� CRC32 ��� ������ str . ��� ������ ������������ ��� �������� ������������ �������� ������.
� PHP ����� ����� ����� ����, � ��� ������� ����� ���������� ������������� �����. ��� ��������� ���������� ������������� CRC32 ��� ����� ����������� ������ «%u» � �������� sprintf() ��� printf() .
���� ������ ������������ ����� ����������� CRC32 � ������� ������� printf() :
������ 1. ����� ����������� ����� CRC32
��. ����� �������� ������� md5() � sha1() . Crc32 — ��������� CRC32 ��� ������������� ���� � ��������� ����� �� ������ �� PDA. �������� ���������� CRC32 ��������� public static uint CalculateCRC(System.IO.Stream stream) const int buffer_size = 1024; uint result = 0xFFFFFFFF; byte] buffer = new byte[buffer_size]; // // Crc32 — ��������� CRC32 ��� ������by Clifford M. Roche ������������ ���� ������ � ������� �������� �������� ������� CRC32 ���������. ��� ������������ ������� ������, ������ ����� �������� ���������� — ���� ��� Borland C++ � MSVC++ NET. (������� ������ ���� ����� ���������� �� MSVC++6) ����� �������� � ��������� �������. ��������� ��������� ���������� (��������� �������) ���� �� �������, �� � ���� ��� ������ ���, ����� ��� �� �������������. ��, ������, ������ �������� C++ — ��� ���� ����� ���������������. ����� ������������ CRC32?��� ��� ������������ ��� ������ �� ����� ��������� �������� — � ����� �������� ���������� ��� ������ ����������, ������� ������ ���������� ���������� ������� ��� ������ ������, ���������� ��� �������� � �������� ������-������ �� �������, � ��������� �����, ������� �� ������������� ���� �������. ���� ��������� ��������, �������� � ���� ��������� ����������� ������ � ������, ��������� �� �������� �� � �������� �������. �������� �� �� ����� ��������/���������, ������ ����� � CRC32. ���� � ���������, ��������, ����� ������� �������, ����� ��� ���� ������ � ���� ������, ������ ��� ������������� ������� �� ���������. �� �� ����� �������� ����������� ����� ���������� CRC32 � �����. ����� ����, ����������� �� ����� ������� ���������� ��������� ������� ������ ����������, ��� CRC32. CRC32, ��� ���� �������������� ���������, � ��������� ����� �������� �������� ����������, ��������� ��� �� �������� ������������ � ������, ������� ����� ���������� ������ ��� ���� ��������� (����� ����� ����� ������ � ���������� �������������, � ����� ����������� ������� ����������������� ������ ������� ������) � � ��� ��� ������ ������������ � �������� ������� �������� �����/���������� ��-�� �� ���������. ���� CRC32 �� ����������� ��������, � ������ ���� ����, ��� 2 ����� ����� ����� ���������� �������� CRC32, ������ ��� ��������� �����, ��� CRC32 ���������� ��������� ���������� ��� ��������� ������� �����. CRC32 �������� �������� 32-������ (4-��������) ����� (����� ��������� ��� �������), ��� ���������� ���������� ��������� ����� ������. ������������� CRC32 ��� ����������� �������� �������� � ��, ��� ��� �������� ������� ���, ��� ��������� ��������� � ����� (���� �� ���������, �������� ��� �������) ������ �������� ������� ��������� � ������� CRC32. CRC32 ����� 2 ^ 32 ��������� ��������; � ����� ��������� 4.294967E +09. �� ���� �������� ������� CRC32 ����� ������� � �����, ��� ��� ��������� ��� ������� ��� ����������. ��� ����� ���� ������������ ��� �������� ������, ����� �������, ���� �� ��������, ����� ������ ������������� ������� ������ ���� � ����� ���������, ��� ��� ����� ���� ������������ ��� �������� ������ ��� ������, ������� ���������� �� ��������� �� ����� ����, ����� ��� ���� �����, ��������. ��� CRC32 ��������?CRC32 ��� ��������� ������� �������, � ������� �� �� ������ �������������� ��������. �� ��� ��� ��, ��� �������, ��������: ������� �� ���������� ����� �������� ����������, � � ���� ��������� �� ���������� CRC32 ������. � ����� ���� ������ � ������, ��� ��������� ������� �������� ������� � ������ �����������. ����� �� ������ ������, ��� ������� ����� ������������� ������� CRC32, � ����������� ������� ����������� ���������� �������� CRC32 ��� ������� ����� � �����, ������ ��� ���������, ������� �� ���������. ��� �������� ��� ������� ����� � ������, ��������� ��� �����. ����� ���������� ����� ���������� �������� ���������� ��������. ������ ��������� CRC32 — ������� �������, ������� ��������� ���������� ���� ��� ���������� �� ������ ������, � ������� �������� CRC32, ������� ���� � ���, � ��������� ����� � ����� �������� �������. ���� ��� �������� �������� CRC32, �� ������ ������������ 0xFFFFFFFF. ������� ����������, � ������ — ��������� �������, � ������� ������ ����������� �������� CRC32. ���������� ����� ������. �������� ��� ����� ����� ���������. >< private : static DWORD CRC32Table [ 256 ] ; �� ������ — � ��� ���� ������� ��� ������� ���� ������, ������� �� ��������� ������������, ������� ����� ������������ ��� ��� CRC32 �������. � ��� ����� ���� ��� ��������� �������, ���� ����� ��������� ������ ����� — ���������� ��� ���������� ������ ���������� ����������, ������ ��� ���������� ������ ���������� � ���������� �������� CRC32 ��� ����������� �����. ���������� ����� ���� ����������� ������, �� ������ ������ ��� ��� ������� CRC32. �� ����� ���� ��� �� ����� ������� ��� ������� ���������, ���� �� ������. ��������� ����������� � ����������, �� ������ ��������� � ������� ������� ������ �����, ����� ���������� ������������ �. ��� ����� ����� ����� ������������� ������������ �������, ��� ��� ��� ����� ����������� ��������� ����� ���������. �������� ������� �������������� �������� ������� ������ ���������� ������� ������������ �������� ��� �������������, ������� ���� ��� ��������, ������� �� ���������� � ����� �������. ������������ ��������, ������� �� �����������, ������������ ������ ��������� ����������� ���������� �������� � ����� ������, ������ ����������, ������������ �� ����� �� ���� ��������, �������� ������� ������� � �� ������ � ����� ������ ������. ��� ���������, ��� ��� ��� ��� ��� ������� ����������� ��������. ��������, ������� �� ����� ������������ ��� �������� ����� �������, ����� 0x04c11db7. �������������, ���� ��������� ����� ������������ ������� ������� ������������ � ���������, ������ ��� WinZip, PKZip, � Ethernet. ����������: ��� ����� ����� ������������ ������� �� ������� ���� ���� ���, ���� ���� �� ����������� ������������� �������. ����, �������, �� ����������� �������, �������������� � ��� ����. ������ ��� ���������� — ����������� CRC ���������, ������� �� ������ �������� ���������� � ����� ������� ������, ��� ����� ���� �������, ������� ���������. � ���� ������ 0x04c11db7 ���������� 0xedb88320. ���� ��������� ��������� ����� �������� �������� � �������, ��������� ��� �� � �������, � ��������� �������� ���������. � �������� ����� ��� ������������� ���� ���, ��� �� ���������� ������ ����� ����. ����������: ��������� — ��� ������� ������� ������� �������� ��������� ������� ��������� ��������. ��������, 10100111 ������ ��� ��������� 11100101. DWORD dwPolynomial = 0xEDB88320 ; CRC32Table = new DWORD [ 256 ] ; DWORD dwCRC ; CRC32Table [ i ] = dwCRC ; ������� ���� ����� ��� 256 ������� � ������ �������� ������, ����������� �� ������� � ������� ����������. � ����������� �� ����� �������� �� ������ ������ ��� �� ������ ������� ����� ����������, ����� ��������� �������������� ������� ������ ���, ����� �� ������ ������� �������. ���� ��� ��� ������, ������ �������� ���� ��� � ��������� ����������, ���������� ���� ������ � ����, �� �����, ��� ��� ��� �������� ������, � ��������� � ���������� �������� ����� � �������. ���� � ����� 256 * 4 ���� ������ (��� �� ����� ���� �� �����), �� ����� �������� ���� ������� ��� ������������� ��������� ������������� �������. ��� ��� �� ���, ��� �������� ������� ������� �������������� �������������� ������������� ���� ������ 14 �������� ������ ��������� ��������� �������: ������� CRC32���� ������� ������, �� ����� ��������� �� ������� GetCRC32. ��� ������� ��������� ���� ���� � ���������� ����������� �������� CRC32 (���� ������� ����������). ������, ��� ����� �������� — ��� ��, ��� ������� �������� ����������. ��� ��� �� ���, ��� �� �������� ���� �����������, ��� �� ��� ��� �� ����� ����� ���� �������, ������ ��������� �� ����� ������� � ������ ������ ��� (��� ���������� ��������� ������), ��������� ����� �� ����� ���� �������� ����� ���� ������� � �� �����, ������ ������� ���� �������. ����� ����, ������� � �������� ������ ���� ��� ���, �� ��� ���������� ������ ���, ����� ������� ����������, � ���� �� ����������� ��������� CRC32 �������� 4gig �����, �� �������, ������ ���� ��� ��� ����� ��� ������� ��������������� ����� ����� ������� ���������� ����� �����. �������� ��������: ������������� ���������� ������� �� ������� ������������ �������������. ��� ����� �������� � ������ ������������� ���������� ������ ��������� ���� � ����� � �������� ����� ����� �������� � �������� ����� ������������������ ����������, ��������� ������� ����� ������ (��� ��� �������� � ���������� �������� � ������� �� ����� ���������� ����������). � ����� �������� GetCRC32 ���������� ������ ����������� �, ����� �������, ����� ������ ���������� �� ����� ��������. �� ����� ������ ���������� �� ������������ __fastcall. __fastcall �������� ����� �������� ���������� � ������� ����� �������� ����������, � ������� �� ������������ ������� ����������� �� ����� ����. ��� ����� �������� � ��������� ���������� �������� ��� ��, ��� ��� ������ inline. � ���������� ������ � ���������� ��� CRC32 ��������� �������� ��������� ������� ����������, �� ������� ������� �� �������, � ���������� CRC32 �������. ������ ��� ����������� ���������� �������� ������� — ���� ������ CRC32 ���������. ���, ��� �������� ������� — ��������� ������ �, ��������� ��� ��� �������, ������� CRC32 ������� ��� ���. DWORD ECRC32 :: CRC32ByStruct ( BYTE * byStruct , DWORD dwSize , DWORD & dwCRC32 ) < // MAKE SURE WE HAVE A VALID BYTE STREAM // LOOP TO READ THE STRUCTURE // FINISH UP dwCRC32 ; ������� ��������� � BYTE ��������� �� ������� ����� ���������, � �������� � ����� ����� ������ ���� ���� ���������, ��������� �������� ������� ����� � GetCRC32 ������ � ���������� ��������� CRC32. � ��������, ����� ���������� CRC32 �������� ����������, �� ������ ����������� �������� 0xFFFFFFFF. ����� ����������� ��������� � �������, ������� ����� ����������, ����� �����, ���������������� ��� ��������� � NULL, ������ ��� ���������, ��� �������, ����������� �� 8 ����. ������� �������, ���� � ��� ���� ���������, ������� ����� 7 �������� (7 ����), sizeof(���������) ����� 8 ����. ��������� ��� ����� ��������������� ��� «��������» �������� � �� ����� ���� ������ ��, ���� �� ���������� ��������� ����� � ���� �� �������. ��� ����� �������� �������� �������� CRC32. ���������� ��� ���� ������� — ��� �������� ������� ���������, ���� �� ������ ������ ������ ��������� ��� �����������, �� ������ ������������ ��� ��������, ��� �������� ��� �������� ������������ ����� �������������� �����-�����������. ������ �� ������� ��������� ��� ������, ��������� ��� ����� ��������� � ��������� ������������� CRC32 �������� ���� ���������� ��������. ������ ��� ����������� ������� CRC ��� ����� ����� �����, � ���� �������, �������� ���, ������� ��������� ����� ������� — � ���������������� ��� �� ���. ������ �� ������ �������� �������������� �������, ������� ����� ��������� ������ ����� ����� ����������. // NOW WE CAN GET THE FILE SIZE try < if ( dwSize == INVAL >) < // SOMETHING SCREWED UP ��� ������� ���������� ������ CRC32ByFile, ������� �� �� ������ � ��� ������ ������������. ��� ���������� � ������ — ����� ����, ��� CRC32ByFile ������� ��������� ���� (�������, ��� �� � ��������), ����� ������� ������� �������� ��������� ����� � ��������� �� DWORD, ������� ������ ������������ ������. ������� ���������� true, ���� ��� �������. ��������� ��� �� ����� ���� ������ ���� � �������� ������ � ��� CRC32 ��������. // OPEN THE SPECIFIED FILE // CALCULATE THE CRC32 SIGNATURE bSuccess = ReadFile ( hFile , cBuffer , sizeof ( cBuffer ) , while ( bSuccess && dwBytesInOut ) < ��� ��������� �������� ���� �� �������, ������������� � ����, ������� ��������� ����, ��������� ���, ��������� ����� �������, ��������� CRC32-�������� ��� ���� ������ � �����������, ���� �� ����� �� ����� �����. ������ ���������� CRCCRC32, ��� � � �����, ����� ������������ ��� ��� �� ��� �� �������� — ��� �������� ����������� ������ � ������, ����� ��� �����-�� �������� ����������. ������������ ���������� ��������� ������ �������������, ������� ����� ������ ������������ � �����, ����� �������� �������� CRC32 � ������, ������� �� ��������� �� ���������; ��� �����������, ��� ������, ������� ��������� � ����� ���������� — �� ��, ������ ���� ����� ���������. ���� TCP � UDP ��������� ������ �������� CRC � ����������, ��� ����� ������ ���������, ��� ��������� ������ �����. ��������� � ������, ������� ������������ � ���������� �������, ����� � ����� ������ ����������� � ��������������� � ����� ���� ��� �������� CRC. CRC ����� ���� ���� ������������ � ����� ������������, ����� ���������, ��� ��������� ��������� � ������ ���� ��������� �� �������� ���������. ��� �������, ������� ������ ������������ � PGP ��� ���������� ��������� ����� �������, ��� ���������� ������� ����� ���������, ��� ��� ���� ���������� ������������� ����, ���, ��� ��� ��������. PGP �� ���������� CRC32 ��� �������� ��������, �������� CRC32 ��������� ���� ��� ����� ��������. ������ ����� ���������, ������������ ��� ���� ����� — ��� MD5, SHA8, SHA256 � �.�. ��� ���������� HASH-��������� � ������������ ������ � ����� ������������. ���� ��� ����� ������������ ��������� ��������, ������� ���������� ������� ���������� ��������, ��� CRC32 � ������ ����� �������, ��� CRC32, ��������, �������� �������� �� ��� ��� ���������. � �������, ���� � ��� ���� �����-���� ���������, �������, � ��� �����, �� ����� ������ ����� ���� � irc �� ������ #gamedev ��� ����� kurifu ��� seta. ������� ����������� ����� ����� �� CRC32: ��������� ���������������. ���� ��� ���� ���, ���� ��� � ���, ��� ��������� �������� �����, �������������� ������������� ����������������� ����������� ����� �� CRC32 � �������������� ����� ����������� ������ �� ��������� ����� search, �� � ��������� �� ����� ��� �������. ��� ���, ������ �� ���. � ����� ����� ��� �������� ��������� ����������� � ������ �������, ���� � ����� ������ ��� �� � ��. ���� ������� �������! �������� � C# 2008 ��� .NET 3.5, �� 4 ����� �� ��������! |
08.11.2012, 00:49 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
�������� ������ �� COM-�����. ������� ����������� ����� ��� �� ���������. ������ ����������� ����� ������ ����������� ����� ����� ��� ������ ������������ ��� �� ������� �������� ����������� �����? |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
08.11.2012, 10:23 | 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
08.11.2012, 13:55 [��] | 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Special-Owl, �� �� ������, �������. =) �� ����� �� �� ��� ��� ���� ����� ���������? ��� ��������� � �������� �� ���? � ��� ������ array.Length? |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
08.11.2012, 14:27 | 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
��� ���������� ���� � ������� ��� �������� � ������������ ������. ���� �� �������, �� ����� ����� ����� ������� � �������� ���: 1)��������� ���������� 111111111111111111111111111 (32 �������, 0xFFFFFFFF) 2.1)�������� ���������(����� ��������� ����� ��� ������� ��� �������, �� � ����� ��� �����) ���� ����������� ���������� (last_byte = (byte)(result & 0xFF)) (��������� ���������, �� �� ����� ���� ��� �� �����, ������) 2.2)�������� ����������� ������ ������ �� 1 ���� (8 ��� (������)), �.�. ����, ��������, 32 �������, ����� 8 ����� � 24 �������. 2.3) ��������� �������� ����������� ��� � ����������� ���������� (� ������ ��� � 00000000111111111111111111111111) � ����������� ����� ��������. 2.3.1) ����� ��������� ������� ���������� ����� ���������� �������� ����������� ��� ���������� ����� ����������� ���������� � �������� ��������� ��������� �������. 2.3.2) �������� ��� — ��� ��������� ��������� ������������ ������ ���� ��������� �� �����, �.�. 3) ���������� ����������������� ����������� ���������� ������: ��� ������ ��������� ����������� ����� CRC (CRC32 — CRC16 — CRC8)��� ������ ������� ������� ��������� � ������. ����, ��� �� ����� CRC? ���� ������, ��� ���� �� �������������� �������� ����������� �����. ����������� ����� — ��� ����� �������� ����������� �������� ���������� �� ������� �������� ��� �������� �� ������� �����. ��������, ���� �� ���������� �������� — ������������� ���� ��������. ��� ����� ����������� ��� ���� ������������� ���������, � ���� ����� ����������� ������, �� � ����� ��������� ����������� 0, ���� �������� — �� 1. ��� ����� ����� �������������� ����� ����� ���������, � ������������ � �������� ����� ��������. ���� ��� ����������, ������ ��� �������� �������� ������, � ������������ ���������� ���� ��������. �� ����� ������ ����������� ������� ������ — ����� ��������������� � ����������� �� ������, �.�. ��� ��������� ���������� ����� ���������, �������� ����� ����� �� ����������. ������� ���������� ��������� ����� «�����������» ��������, � ��� ����� CRC. �� ����, CRC — ��� �� �����, � ��������� ������� ������ ������ ���������� (��������������� ���������) �� ���������, � ������ — ������� �� ������� ��������� �� ���������. ��� �� �����, CRC ����������� ����� �������� «����������� �����». � �������� CRC ������ ����� ������ ��� ���������. �� ����, ���� ���� �� ���� ��� ��������� ��������� ��������� ��� ��������, ����������� ����� ���� ���������, ������ �����������. ��� ������� ���� ����� ��������, ��� ��� �� ��������� ���������� ����������, ���������� �������� ��������� ��� �������� ��� ���. ������ ��� ���������� � ���������� CRC, ����������� ��� ������� ������. ��� �� ���������, �� ������� �� ������ ������ �������� ���������? ��� ��������� ����� ����� ����� �����, �� ������ ������������ �����, ������� 1 ����� — 8, 16 � 32 ����. ������ ��� ����� �������, ���� ���������� �������� ������ � �������, � �� � ������. ���������-�������� ������ ���������� � ���� �������� (����������) ��� ����� �������: x^8 + x^2 + x^1 + x^0. ����� ������� ����� «x» �������� ������� ����-������� � �����, ������� � �������, � ������� ������ ��������� �� ������� �������� � ������������� ��� ������������� �����. �� ���� ���������� ����� ����� — ��� �� ��� ���� ��� (1)00000111 � �������� ������� ���������, ��� 7 � ����������. � ������� � ������ ��������������� ������� ������ �����. ��� ��� �� ������� ����������� �����? ���������� ������� ����� — ������� ��������� �� ������� «� ���» — � ��� ����������� � ����� ���������� ���������� ���������� �, ��������������, ��������� ������� CRC. �� ���������� ������ ������� �����. � ����� ����, ������� ����� �� ��������� ����������� �� ������ ���������: ������� ������������ ������� �������� ������������������ ����� �� ����� (1)00000111, ��� ��������� x^8 + x^2 + x^1 + x^0. �������� ��� ���� �������������� �������. ��� �� ����� ��������, ��������� ����� ��������� �� ����� �����. ��� ��� �������? ���������� ��� ����������� ���������, ������� ������������ ��� ���������� CRC. ��������, ��� CRC32 ��� ����� ���� ����� 0x04C11DB7, � ��� CRC16 ��� ����� ���� 0x8005. ����� ��� �������� ��������������� ����� ������� ��������� ����������� ����� CRC ����� ������ �� �����-�� ������ �����. � ���������. ����� ��������� ��� ������ � ������� ����� ���������� ��� ������� ����� «�����», ��� � ��������, �������. �� ��������� ����� ���������������, ������� ������� ������� �� ����� Basic .NET, ������� ����� ������������ ����������� ����� CRC, �������� ��� ����������, ������� � ������ ����, � ��������� �������� CRC � ���� 32-���������� ������������ �����. Public Shared Function GetCrc(ByVal bytes As Byte(), ByVal poly As UInteger, Optional ByVal w > ‘������ ������� ����� �� ���������: ‘������ ������� �� ����� ���������� ���������� ��������: ‘����� � XOR: Dim poppedBit As Integer = CInt(register >> (width — 1)) And 1 ‘���������� ����� ������� ��������. Dim shiftedBit As Byte = Convert.ToByte(msgFifo.Dequeue) register = register > (32 — width)) ‘��������� ������� �������. crc32(PHP 4 >= 4.0.1, PHP 5, PHP 7) crc32 � ��������� ������� CRC32 ��� ������ ��������������� ��������� ����������� ���������� ��� 32-������ ��������� (CRC32) ��� ������ str . ��� ������ ������������ ��� �������� ����������� ������������ ������. � PHP ����� ����� ����� ����, ������� ������ ����������� ����� ����� ��������� �������������� �� 32-������ ����������. �� 64-������ ���������� ��� ���������� crc32() ����� �������������� ������. ������� ��� ����� ������������ ������ «%u» � �������� sprintf() ��� printf() ��� ��������� ���������� ������������� ����� crc32() ��� �����. ��� ������������������ ������������� ����� �� ������ ������������ ��� ������ «%x» � �������� sprintf() � printf() , ��� �� ������� ����������� dechex() . ��� ���� ������� ����� ����������� � ����������� ���������� crc32() � ���������� �����. ��� ������������� 64-������ �������� ����� ��������������� ����������� ���������� ������������� ����� ��� ������� ��������, �� ��� ������ ����������������� �������������, �������� �������������� 0xFFFFFFFF######## �������� ��� ���. ��� ��� ����������������� ������������� �������� ����� ��������������, ���� ������ �� ������ ���, ���� ���� ��� ������ ������ ��������� ������������ �������� � 50% ������� ��� �������� � 32-������ �� 64-������ ���������. ����������� �����, �������� ���������� ����� ����� ���� �� ����� ������ ����� � ����� ���� ���������� ����� ����������������� ������������� (��� �������� ������ md5() ). ����� ��������������� ����� ����� ����� �������� � �������������� ������� hash() . hash(«crc32b», $str) ������ �� �� ������, ��� � str_pad(dechex(crc32($str)), 8, ‘0’, STR_PAD_LEFT) . ������ ���������������������� ������������������ ����������� ����� crc32 ������ str � ���� ������ �����. ������������� #1 ����� ����������� ����� CRC32 ���� ������ ������������ ����� ����������� ����������� ����� � ������� ������� printf() : �������� �����
User Contributed Notes 23 notesThis function returns an unsigned integer from a 64-bit Linux platform. It does return the signed integer from other 32-bit platforms even a 64-bit Windows one. The reason is because the two constants PHP_INT_SIZE and PHP_INT_MAX have different values on the 64-bit Linux platform. I’ve created a work-around function to handle this situation. function get_signed_int ( $in ) < Hope this helps. The khash() function by sukitsupaluk has two problems, it does not use all 62 characters from the $map set and when corrected it then produces different results on 64-bit compared to 32-bit PHP systems. Here is my modified version : /** khash() — maps a crc32 result into a (62-character) result$pefile» ; I see a lot of function for crc32_file, but for php version >= 5.1.2 don’t forget you can use this : function crc32_file ( $filename ) Using crc32(file_get_contents($filename)) will use too many memory on big file so don’t use it. A faster way I’ve found to return CRC values of larger files, is instead of using the file()/implode() method used below, is to us file_get_contents() (PHP 4 >= 4.3.0) which uses memory mapping techniques if supported by your OS to enhance performance. Here’s my example function: // $file is the path to the file you want to check. $crc = crc32 ( $file_string ); return sprintf ( «%u» , $crc ); $file_to_crc = / home / path / to / file . jpg ; echo file_crc ( $file_to_crc ); // Outputs CRC value for given file. I’ve found in testing this method is MUCH faster for larger binary files. bit by bit crc32 computation function bitbybit_crc32 ( $str , $first_call = false )< //reflection in 32 bits of crc32 polynomial 0x04C11DB7 //=0xFFFFFFFF; //keep track of register value after each call //initialize register on first call $n = strlen ( $str ); //xor first $zeros=min(4,strlen($str)) bytes into the register //put in enough zeros at the end //xor the register with 0xFFFFFFFF $str = «123456789» ; //whatever for( $i = 0 ; $i strlen ( $str ); $i += $blocksize ) $crc = bitbybit_crc32 ( substr ( $str , $i , $blocksize ),! $i ); Implementation crc64() in php 64bit /** // ECMA polynomial // ISO polynomial ( 0x8 60 )) ^ $poly64rev ; $crc64tab [ $i ] = $part ; /** if ( $crc64tab === null ) < for ( $i = 0 ; $i strlen ( $string ); $i ++) < return sprintf ( $format , $crc ); This function returns the same int value on a 64 bit mc. like the crc32() function on a 32 bit mc. The crc32_combine() function provided by petteri at qred dot fi has a bug that causes an infinite loop, a shift operation on a 32-bit signed int might never reach zero. Replacing the function gf2_matrix_times() with the following seems to fix it: function gf2_matrix_times ( $mat , $vec ) while ( $vec ) < Otherwise, it’s probably the best solution if you can’t use hash_file(). Using a 1meg read buffer, the function only takes twice as long to process a 300meg files than hash_file() in my test. Dealing with 32 bit unsigned values overflowing 32 bit php signed values can be done by adding 0x10000000 to any unexpected negative result, rather than using sprintf. $i = crc32(‘1’); The crc32() function can return a signed integer in certain environments. Assuming that it will always return an unsigned integer is not portable. Depending on your desired behavior, you should probably use sprintf() on the result or the generic hash() instead. Also note that integer arithmetic operators do not have the precision to work correctly with the integer output. /* OLDCRC */ if( function_exists ( ‘gmp_init’ )) < private static $CRCTable = array(); public static function encode ( $seq )< $crc = gmp_init ( INITIALCRC , 16 ); if (! $init ) for( $k = 0 ; $k strlen ( $seq ); $k ++) < $res = gmp_strval ( $crc , 16 ); return $res ; Here is a tested and working CRC16-Algorithm: function crc16 ( $string ) < Regards, A faster implementation of modbus CRC16 function crc16($data) I made this code to verify Transmition with Vantage Pro2 ( weather station ) based on CRC16-CCITT standard. // CRC16-CCITT validator $test = chr ( 0xC6 ). chr ( 0xCE ). chr ( 0xA2 ). chr ( 0x03 ); // CRC16-CCITT = 0xE2B4 function genCRC (& $ptr ) 625 KB/s on my 2.2GHz Turion static $CRC32Table , $Reflect8Table ; for( $i = 0 ; $i 256 ; $i ++) $CRC32Table [ $i ] = $remainder ; if (isset( $Reflect8Table [ $i ])) continue; $remainder = 0xffffffff ; $str = decbin ( $remainder ); = microtime (); For those who want a more familiar return value for the function: function strcrc32 ( $text ) < And to show the result in Hex string: function int32_to_hex ( $value ) < MODBUS RTU, CRC16, function crc16 ( $string , $length = 0 )< $auchCRCHi =array( 0x00 , 0xC1 , 0x81 , 0x40 , 0x01 , 0xC0 , 0x80 , 0x41 , 0x01 , 0xC0 , 0x80 , 0x41 , 0x00 , 0xC1 , 0x81 , small sample convert crc32 to character map function khash ( $data ) < /* I used the abs value of this function on a 32-bit system. When porting the code to a 64-bit system I�ve found that the value is different. The following code has the same outcome on both systems. /* Old solution I needed the crc32 of a file that was pretty large, so I didn’t want to read it into memory. [ ‘__crc32_table’ ]=array(); // Lookup table array function __crc32_init_table () < // Builds lookup table array // 256 values representing ASCII character codes. // Swap bit 0 for bit 7, bit 1 for bit 6, etc. function __crc32_string ( $text ) < // Creates a CRC from a text string // You need unsigned variables because negative values // Start out with all bits set high. // Perform the algorithm on each character in the string, // Exclusive OR the result with the beginning value. function __crc32_file ( $name ) < // Creates a CRC from a file // Start out with all bits set high. if(( $fp = fopen ( $name , ‘rb’ ))=== false ) return false ; // Perform the algorithm on each character in file // Exclusive OR the result with the beginning value. ���������� ����������/����������� ���������� ���������� �������� ��� ���������� CRC. ��������� �� ���� ���������� ������������ � Ross Williams [1] . ����������CRC-4 [ ������� ]CRC-8 [ ������� ]CRC-16 [ ������� ]CRC-CCITT ���������� �� ������������� CRC-16, ��� ��� ���������� ������ ������� � ������� ������. CRC-32 [ ������� ]�������� CRC32 ������� �� ����������� �������� 0xEDB88320 (���������� ����������� �������� 0x04C11DB7). ����������� ���������� �� C [ ������� ]�������� ��������������� ��������� ������� ������������ ������� CRC:
Crc32 — ��������� CRC32 ��� ������������� ����������� ���� ���������� ������� [3] . � ������������ ���������� CRC ���������������� ������ � ����������� �� ���������: Cyclic Redundancy Code ��� Cyclic Redundancy Check [4] . ��� ������ ������������ �������� �������������� ������� ����������� �����, ��� ������ � ���������� ���������� ����� �������� ��� ���-�������. ���������������� ����������������� ������� �������� ����� � ���������� ����������� �������� ������� �� ��������� ����������� ��. � �������, ��� � ������������ ����� �������� ���� ����� � ��������� ���� ����������� ����������� �������� ����������� � ��� ����-��������. ������������� � � �� ������� �� �������������� ���������, ��� ��� ���������� �������� ������������� �� �������� ����� ������� ����������� �� ���������. ����� � ���� ������� ��������� ��������������� ������ ����������, �������������� � 1968 ����. ��� ��������, �� ������������ ���������� ������� ������ ����� ��� �����, � �� ��� ���� ������������ � �������� �����������, �������������� ����� RS-������������ ������. ����� ����: ������ ������� ��������� �� ������ ���������� �������, �� � ���������� �������� ������� ������� (���� ����� ������). �� ������ �� ����� �� ���� ��������� ��������� ������. ������ ����������� ������ ����� �������� ����������� ����������������, � �������� ���������� ���� ���������, ������� �� ������ �������� ��� �������� �����-������ ���������; ��������� �� ������ � ���������� ������� �������� �������� ���������� �� ���������� ����������� �������. � � ���� �������� ����� ������� �������� ��������� ���������, ������������ ����������� �����. CRC ��� ������ ����� �������� ��� �������� �����: ��������� ������� ��������, �������� ���������� � ��� �������������� �������������� ������� �� ������ �������� ����������� ����� ���������� �� �������� ������������. ����������� ������ ����� ����� ����� ���� ����������� ����� ������������ ����� ��������� ��������, ����������� �� ������������ ����� �� ������ ����������� ���������. ����������� ���������� ��� ��������������� ����������� ������������ � ����� ��������� � ����� �������� ������. �� ����������� ������� ������� ����� �������� ���������� ����������� �����: ��������������, ��������� ����� ����������� ��������� ������������ �������� ������. ��� �������� ������� �� ��������� ������, ����������, ����� ���������� ��������� �������� ���������� ���������� ������ ������� �����������: ������������� �������, ������ �������� ������� � ������ ������. �������� �������� � ���, ��� ��� ������� ��������������� ���-������� � ����������� ����� ������� ������ � ��������� �������� � ��������� ������������ �� ������ �������� CRC. ���� �������� � ����������� ����� �� ����� ����� �����, ����������� ������� � ��������������� �������� ������, � ����� ��������� ��������� �������� ������. �������������� ���������������� CRC ���������� �� ��������� ������� � �������� �������� �����������, �� ���� ����������� ��� �������� ����� . �������� CRC �������� �� ���� �������� �� ������� ����������, ���������������� ������� ������, �� ����� ������������� ����������� ���������. ������ �������� ������������������ ����� ������� ���������� �������������� �������� ������� , ������������������ ������������� �������� ������������ ����� �������� ������������������. ��������, ������������������ ����� 1011010 ������������� ����������: ���������� ��������� ����������� ������� ������� ����� , ��� ��������� � ������ ���� �������� ������������������� ����� . �������� ����������� ����� � ��������� � ����������� ����������� G(x) ������� N ������������ ��� ������� ������������������ ����� N, �������������� ��������� R(x), ������������ � ������� ��� ������� ���������� P(x), ��������������� ������� ����� ���, �� ��������� G(x): � ���������, �������������� �������� CRC. � ���������, ������������ �������� ������������ ������� ������. � ����������� ���������. � ������� ������������ ����������. ��������� �������������� ������������� ������� ����� � ������� ������������������, ��� �������� �������� ����������� ��� �������� ������� �������������������. ��� ������� � �������� ��������� ���������� �� ����������� ������� G(x) ������� N ����� �������� 2 N ��������� �������� �� �������. G(x) ������ �������� ������������ �����������. ������ ��� ��������� � ������������ � ������������ � ���-������� � ��������� ������� ����������� ����������. ��� �� �����, ���������� ��������� ������������������� ���������� �����������, ���������� �������� ��������������� � ��������������� ���������� (����������� ����� ��������, �������� ����������). � ������ ��������� ��������� �� ���, � ����� ��������������� ���������� �� ����� ��. ���������� CRC��������� ��������������� � ������������ ����������� ����� CRC, � ������ ������� ����� ��������� � ��������-����������. ���������� �������� ��������� �����������, ����������� � ������������ cyclic reduntancy code; ������ �� ��� ������� � ����� ������. ������ ���������� ����������� ��������� ���������� ����������� ����� �������� ������ �����, ��� ��������� ���������� ��������� � �������������� �����, ������������ ��� ���������� ���������� �������� ����. ��� ���� ����������� ����������� ��, ��� ������ ����� � ������� ����������� ����������� ����� �������� ���������. �� �������� ����� ����� �������������� 8, 16 � 32 � ������� �����, ��� �������� ���������� ������������ ����������� ����������� �������������� �������. � ��������� ��������, ������ ��� �������� ������������ �������� � ��������� ��������� ��������� (��������� �����). ��� ��������� �� ���� �������� �������������; ���� �� � ������������, �� ����� ������������ �������� ���������� CRC, ���� ������ ����������� �������� �� ����� ������������� ������������, �����, ��� �������� ������� ��������� �����. �������� ����������� ����� ������� ������ ����� � ��� ����� ���� ������� (CRC-1), �������� (CRC-8) ��� ����� ������ �������. ���� ������� ��� � ����� �1�, �� ����� ���������� ����� �� ���� ������ � ����������� ����������� �������� XOR. ��������������, ���� ������� ��� � ����� �0�, �� ����� ������ �������� XOR �� �����������. ����� ������ �������� ������ ������� ���, � ������� ��� ������������� � ��� �������� ��������������� ������ ����. �� ����� �������� ���� ����������� ��������� ��� �� �����, � �������� ����������� �� ��� ���, ���� �� ���������� ��������� ��� �����. ����� ����������� ����� �����, � ����� �������� �������, ������� � �������� ����������� ������. �������� ������������ � ����������������� ��������� �� �����, ��� ����������� ���������� ���� �������� ������ ����������, � ����� ������� �� ���������� ������������� ����������� � ��������� ��������� ������� ������� ������������ ���� �����. [1] [5] ���� �������� �������� � ������ ����������-����������: �������� ����������������� �������� �� �������� ������ ������������ � ����� �������������� ������� ���������������� �� check reduntancy code . ��� ���� ������ ������ ������������ �������� �� �������� �������� ������������ �� ���� ���������. � 1993�2004 ����� ������ ������ ���������� ������������� ����������� ����������� ����������� �� 16, [1] 24 � 32 ���, [6] [7] � ����� ��������, ������ ������, ������ ������������������� ����������, ������������������ � ������ �������� ����������. [7] ���� �� ����������� ����� ������������ ��� ����� ��� ���������� � ��������� iSCSI. ����� ���������� � ������������� IEEE ������� ��� CRC-32 ������������ � Ethernet, FDDI; ����� ���� ��������� �������� ����������� ���� �������� [8] . ������������� ������� �������� � CRC-32C � ��������� ������� ����� �� ������������������ ��� ����� ��������� ��������� �� 58 ��� �� 131 ����, � � ��������� ���������� ����� �������� ��������� ����� ���� ���� ���� � ������� � ���� ��� �� ���� ���������� �������������. [7] � �������, �������� ITU-T G.hn ���������� CRC-32C � ����� ����������� ������ � �������� ��������. ���� � ������� ����������� �������� ���������������� ���������� � ���������� CRC.�� �������� ���������� CRC ����� �������� ���- � ����-��������, � ����� �������� ������� ��������� �����. � ������������� ����������� CRC ��� ���������� ������� ���� ��������� ��������� ��������� �������� ���������.
������������ ��������� CRC-128 (IEEE) � CRC-256 (IEEE) � ��������� ����� ��������� ������������������ ���-���������. ������� �������� ���������� CRC����� �� ����� ��������� �������� �������� Ross N. Williams [25] . � ��� ������������ ��������� ���������:
|