diff options
| author | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2008-11-09 16:06:45 +0100 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@users.sourceforge.net> | 2008-11-09 16:06:45 +0100 |
| commit | a2bfc5e9f4c66db6a29810c064b981d772aefb2a (patch) | |
| tree | 7ea6efd4ffe7e265a9dee447693e36332e36f375 /UCoreUtils.pas | |
| parent | 8712e487d22e7650a3dc529a63a70cf1d64e1bc4 (diff) | |
| download | tuxcmd-a2bfc5e9f4c66db6a29810c064b981d772aefb2a.tar.xz | |
Port CRC32 and MD5 functions to standard FreePascal RTL
Added compat dir for Kylix compatibility
Diffstat (limited to 'UCoreUtils.pas')
| -rw-r--r-- | UCoreUtils.pas | 334 |
1 files changed, 0 insertions, 334 deletions
diff --git a/UCoreUtils.pas b/UCoreUtils.pas index bcaa927..240bea6 100644 --- a/UCoreUtils.pas +++ b/UCoreUtils.pas @@ -3,9 +3,6 @@ Copyright (C) 2008 Tomas Bzatek <tbzatek@users.sourceforge.net> Check for updates on tuxcmd.sourceforge.net - Portions of this unit (CRC32, THash_MD5) are part of the Delphi Encryption Compendium - Copyright: Hagen Reddmann mailto:HaReddmann@AOL.COM - This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or @@ -33,24 +30,6 @@ type TOpenStringArray = array of string; TOpenPCharArray = array of PChar; - THash_MD5 = class - private - FCount: Int64; - FBuffer: array[0..63] of Byte; - FDigest: array[0..9] of LongWord; - protected - function TestVector: Pointer; - procedure Transform(Buffer: PIntArray); - public - constructor Create; - procedure Init; - procedure Done; - procedure Calc(const Data; DataSize: Integer); - function DigestKey: string; - function GetKeyStrH: string; - end; - - const ConstERRSpawn = 26; ConstQuotationCharacters = [' ', '"', '''', '(', ')', ':', '&']; @@ -134,10 +113,6 @@ function EnsureUTF8String(s: PChar): PChar; overload; function Min(Val1, Val2: longint): longint; -// Calculate CRC32 Checksum, CRC is default $FFFFFFFF, -// After calc you must inverse Result with NOT -function CRC32(CRC: LongWord; Data: Pointer; DataSize: LongWord): LongWord; - procedure ReportGTKVersion; // Internal locking @@ -1603,310 +1578,6 @@ begin if Val1 < Val2 then Result := Val1 else Result := Val2; end; - -(********************************************************************************************************************************) -{$IFDEF CPU64} -function CRC32(CRC: LongWord; Data: Pointer; DataSize: LongWord): LongWord; -begin - Result := 0; -end; -{$ELSE} -{$IFDEF CPUPOWERPC} -function CRC32(CRC: LongWord; Data: Pointer; DataSize: LongWord): LongWord; -begin - Result := 0; -end; -{$ELSE} -function CRC32(CRC: LongWord; Data: Pointer; DataSize: LongWord): LongWord; assembler; -asm - AND EDX,EDX - JZ @Exit - AND ECX,ECX - JLE @Exit - PUSH EBX - PUSH EDI - XOR EBX,EBX - LEA EDI,CS:[OFFSET @CRC32] -@Start: MOV BL,AL - SHR EAX,8 - XOR BL,[EDX] - XOR EAX,[EDI + EBX * 4] - INC EDX - DEC ECX - JNZ @Start - POP EDI - POP EBX -@Exit: RET - DB 0, 0, 0, 0, 0 // Align Table -@CRC32: DD 000000000h, 077073096h, 0EE0E612Ch, 0990951BAh - DD 0076DC419h, 0706AF48Fh, 0E963A535h, 09E6495A3h - DD 00EDB8832h, 079DCB8A4h, 0E0D5E91Eh, 097D2D988h - DD 009B64C2Bh, 07EB17CBDh, 0E7B82D07h, 090BF1D91h - DD 01DB71064h, 06AB020F2h, 0F3B97148h, 084BE41DEh - DD 01ADAD47Dh, 06DDDE4EBh, 0F4D4B551h, 083D385C7h - DD 0136C9856h, 0646BA8C0h, 0FD62F97Ah, 08A65C9ECh - DD 014015C4Fh, 063066CD9h, 0FA0F3D63h, 08D080DF5h - DD 03B6E20C8h, 04C69105Eh, 0D56041E4h, 0A2677172h - DD 03C03E4D1h, 04B04D447h, 0D20D85FDh, 0A50AB56Bh - DD 035B5A8FAh, 042B2986Ch, 0DBBBC9D6h, 0ACBCF940h - DD 032D86CE3h, 045DF5C75h, 0DCD60DCFh, 0ABD13D59h - DD 026D930ACh, 051DE003Ah, 0C8D75180h, 0BFD06116h - DD 021B4F4B5h, 056B3C423h, 0CFBA9599h, 0B8BDA50Fh - DD 02802B89Eh, 05F058808h, 0C60CD9B2h, 0B10BE924h - DD 02F6F7C87h, 058684C11h, 0C1611DABh, 0B6662D3Dh - DD 076DC4190h, 001DB7106h, 098D220BCh, 0EFD5102Ah - DD 071B18589h, 006B6B51Fh, 09FBFE4A5h, 0E8B8D433h - DD 07807C9A2h, 00F00F934h, 09609A88Eh, 0E10E9818h - DD 07F6A0DBBh, 0086D3D2Dh, 091646C97h, 0E6635C01h - DD 06B6B51F4h, 01C6C6162h, 0856530D8h, 0F262004Eh - DD 06C0695EDh, 01B01A57Bh, 08208F4C1h, 0F50FC457h - DD 065B0D9C6h, 012B7E950h, 08BBEB8EAh, 0FCB9887Ch - DD 062DD1DDFh, 015DA2D49h, 08CD37CF3h, 0FBD44C65h - DD 04DB26158h, 03AB551CEh, 0A3BC0074h, 0D4BB30E2h - DD 04ADFA541h, 03DD895D7h, 0A4D1C46Dh, 0D3D6F4FBh - DD 04369E96Ah, 0346ED9FCh, 0AD678846h, 0DA60B8D0h - DD 044042D73h, 033031DE5h, 0AA0A4C5Fh, 0DD0D7CC9h - DD 05005713Ch, 0270241AAh, 0BE0B1010h, 0C90C2086h - DD 05768B525h, 0206F85B3h, 0B966D409h, 0CE61E49Fh - DD 05EDEF90Eh, 029D9C998h, 0B0D09822h, 0C7D7A8B4h - DD 059B33D17h, 02EB40D81h, 0B7BD5C3Bh, 0C0BA6CADh - DD 0EDB88320h, 09ABFB3B6h, 003B6E20Ch, 074B1D29Ah - DD 0EAD54739h, 09DD277AFh, 004DB2615h, 073DC1683h - DD 0E3630B12h, 094643B84h, 00D6D6A3Eh, 07A6A5AA8h - DD 0E40ECF0Bh, 09309FF9Dh, 00A00AE27h, 07D079EB1h - DD 0F00F9344h, 08708A3D2h, 01E01F268h, 06906C2FEh - DD 0F762575Dh, 0806567CBh, 0196C3671h, 06E6B06E7h - DD 0FED41B76h, 089D32BE0h, 010DA7A5Ah, 067DD4ACCh - DD 0F9B9DF6Fh, 08EBEEFF9h, 017B7BE43h, 060B08ED5h - DD 0D6D6A3E8h, 0A1D1937Eh, 038D8C2C4h, 04FDFF252h - DD 0D1BB67F1h, 0A6BC5767h, 03FB506DDh, 048B2364Bh - DD 0D80D2BDAh, 0AF0A1B4Ch, 036034AF6h, 041047A60h - DD 0DF60EFC3h, 0A867DF55h, 0316E8EEFh, 04669BE79h - DD 0CB61B38Ch, 0BC66831Ah, 0256FD2A0h, 05268E236h - DD 0CC0C7795h, 0BB0B4703h, 0220216B9h, 05505262Fh - DD 0C5BA3BBEh, 0B2BD0B28h, 02BB45A92h, 05CB36A04h - DD 0C2D7FFA7h, 0B5D0CF31h, 02CD99E8Bh, 05BDEAE1Dh - DD 09B64C2B0h, 0EC63F226h, 0756AA39Ch, 0026D930Ah - DD 09C0906A9h, 0EB0E363Fh, 072076785h, 005005713h - DD 095BF4A82h, 0E2B87A14h, 07BB12BAEh, 00CB61B38h - DD 092D28E9Bh, 0E5D5BE0Dh, 07CDCEFB7h, 00BDBDF21h - DD 086D3D2D4h, 0F1D4E242h, 068DDB3F8h, 01FDA836Eh - DD 081BE16CDh, 0F6B9265Bh, 06FB077E1h, 018B74777h - DD 088085AE6h, 0FF0F6A70h, 066063BCAh, 011010B5Ch - DD 08F659EFFh, 0F862AE69h, 0616BFFD3h, 0166CCF45h - DD 0A00AE278h, 0D70DD2EEh, 04E048354h, 03903B3C2h - DD 0A7672661h, 0D06016F7h, 04969474Dh, 03E6E77DBh - DD 0AED16A4Ah, 0D9D65ADCh, 040DF0B66h, 037D83BF0h - DD 0A9BCAE53h, 0DEBB9EC5h, 047B2CF7Fh, 030B5FFE9h - DD 0BDBDF21Ch, 0CABAC28Ah, 053B39330h, 024B4A3A6h - DD 0BAD03605h, 0CDD70693h, 054DE5729h, 023D967BFh - DD 0B3667A2Eh, 0C4614AB8h, 05D681B02h, 02A6F2B94h - DD 0B40BBE37h, 0C30C8EA1h, 05A05DF1Bh, 02D02EF8Dh - DD 074726F50h, 0736E6F69h, 0706F4320h, 067697279h - DD 028207468h, 031202963h, 020393939h, 048207962h - DD 06E656761h, 064655220h, 06E616D64h, 06FBBA36Eh -end; -{$ENDIF} -{$ENDIF} -(********************************************************************************************************************************) -constructor THash_MD5.Create; -begin - Init; -end; - -function THash_MD5.DigestKey: string; -type TxCharArray = array[1..40] of char; - PxCharArray = ^TxCharArray; -begin - Result := Copy(PxCharArray(@FDigest)^, 1, 16); -end; - -procedure THash_MD5.Init; -begin - FillChar(FBuffer, SizeOf(FBuffer), 0); - FDigest[0] := $67452301; - FDigest[1] := $EFCDAB89; - FDigest[2] := $98BADCFE; - FDigest[3] := $10325476; - FDigest[4] := $C3D2E1F0; - FCount := 0; -end; - -{$R-} -procedure THash_MD5.Done; -var - I: Integer; - S: Int64; -begin - try - I := FCount and $3F; - FBuffer[I] := $80; - Inc(I); - if I > 64 - 8 then - begin - FillChar(FBuffer[I], 64 - I, 0); - Transform(@FBuffer); - I := 0; - end; - FillChar(FBuffer[I], 64 - I, 0); - S := Int64(FCount) * 8; - Move(S, FBuffer[64 - 8], SizeOf(S)); - Transform(@FBuffer); - FillChar(FBuffer, SizeOf(FBuffer), 0); - except - end; -end; - -procedure THash_MD5.Calc(const Data; DataSize: Integer); -var - Index: Integer; - P: PChar; -begin - if DataSize <= 0 then Exit; - Index := FCount and $3F; - Inc(FCount, DataSize); - if Index > 0 then - begin - if DataSize < 64 - Index then - begin - Move(Data, FBuffer[Index], DataSize); - Exit; - end; - Move(Data, FBuffer[Index], 64 - Index); - Transform(@FBuffer); - Index := 64 - Index; - Dec(DataSize, Index); - end; - P := @TByteArray(Data)[Index]; - Inc(Index, DataSize and not $3F); - while DataSize >= 64 do - begin - Transform(Pointer(P)); - Inc(P, 64); - Dec(DataSize, 64); - end; - Move(TByteArray(Data)[Index], FBuffer, DataSize); -end; -{$R+} - -function THash_MD5.GetKeyStrH: string; -const HexTable = '0123456789ABCDEF'; -var i: integer; - Value: string; -begin - Result := ''; - Value := DigestKey; - if Value = '' then Exit; - for i := 1 to Length(Value) do - Result := Result + HexTable[Byte(Value[i]) shr 4 + 1] + HexTable[Byte(Value[i]) and $F + 1]; -end; - -{$IFDEF CPU64} -function THash_MD5.TestVector: Pointer; -begin - Result := nil; -end; -{$ELSE} -{$IFDEF CPUPOWERPC} -function THash_MD5.TestVector: Pointer; -begin - Result := nil; -end; -{$ELSE} -function THash_MD5.TestVector: Pointer; -asm - MOV EAX,OFFSET @Vector - RET -@Vector: DB 03Eh,0D8h,034h,08Ch,0D2h,0A4h,045h,0D6h - DB 075h,05Dh,04Bh,0C9h,0FEh,0DCh,0C2h,0C6h -end; -{$ENDIF} -{$ENDIF} - -{$Q-} -procedure THash_MD5.Transform(Buffer: PIntArray); -var - A, B, C, D: LongWord; -begin - A := FDigest[0]; - B := FDigest[1]; - C := FDigest[2]; - D := FDigest[3]; - - Inc(A, Buffer[ 0] + $D76AA478 + (D xor (B and (C xor D)))); A := A shl 7 or A shr 25 + B; - Inc(D, Buffer[ 1] + $E8C7B756 + (C xor (A and (B xor C)))); D := D shl 12 or D shr 20 + A; - Inc(C, Buffer[ 2] + $242070DB + (B xor (D and (A xor B)))); C := C shl 17 or C shr 15 + D; - Inc(B, Buffer[ 3] + $C1BDCEEE + (A xor (C and (D xor A)))); B := B shl 22 or B shr 10 + C; - Inc(A, Buffer[ 4] + $F57C0FAF + (D xor (B and (C xor D)))); A := A shl 7 or A shr 25 + B; - Inc(D, Buffer[ 5] + $4787C62A + (C xor (A and (B xor C)))); D := D shl 12 or D shr 20 + A; - Inc(C, Buffer[ 6] + $A8304613 + (B xor (D and (A xor B)))); C := C shl 17 or C shr 15 + D; - Inc(B, Buffer[ 7] + $FD469501 + (A xor (C and (D xor A)))); B := B shl 22 or B shr 10 + C; - Inc(A, Buffer[ 8] + $698098D8 + (D xor (B and (C xor D)))); A := A shl 7 or A shr 25 + B; - Inc(D, Buffer[ 9] + $8B44F7AF + (C xor (A and (B xor C)))); D := D shl 12 or D shr 20 + A; - Inc(C, Buffer[10] + $FFFF5BB1 + (B xor (D and (A xor B)))); C := C shl 17 or C shr 15 + D; - Inc(B, Buffer[11] + $895CD7BE + (A xor (C and (D xor A)))); B := B shl 22 or B shr 10 + C; - Inc(A, Buffer[12] + $6B901122 + (D xor (B and (C xor D)))); A := A shl 7 or A shr 25 + B; - Inc(D, Buffer[13] + $FD987193 + (C xor (A and (B xor C)))); D := D shl 12 or D shr 20 + A; - Inc(C, Buffer[14] + $A679438E + (B xor (D and (A xor B)))); C := C shl 17 or C shr 15 + D; - Inc(B, Buffer[15] + $49B40821 + (A xor (C and (D xor A)))); B := B shl 22 or B shr 10 + C; - - Inc(A, Buffer[ 1] + $F61E2562 + (C xor (D and (B xor C)))); A := A shl 5 or A shr 27 + B; - Inc(D, Buffer[ 6] + $C040B340 + (B xor (C and (A xor B)))); D := D shl 9 or D shr 23 + A; - Inc(C, Buffer[11] + $265E5A51 + (A xor (B and (D xor A)))); C := C shl 14 or C shr 18 + D; - Inc(B, Buffer[ 0] + $E9B6C7AA + (D xor (A and (C xor D)))); B := B shl 20 or B shr 12 + C; - Inc(A, Buffer[ 5] + $D62F105D + (C xor (D and (B xor C)))); A := A shl 5 or A shr 27 + B; - Inc(D, Buffer[10] + $02441453 + (B xor (C and (A xor B)))); D := D shl 9 or D shr 23 + A; - Inc(C, Buffer[15] + $D8A1E681 + (A xor (B and (D xor A)))); C := C shl 14 or C shr 18 + D; - Inc(B, Buffer[ 4] + $E7D3FBC8 + (D xor (A and (C xor D)))); B := B shl 20 or B shr 12 + C; - Inc(A, Buffer[ 9] + $21E1CDE6 + (C xor (D and (B xor C)))); A := A shl 5 or A shr 27 + B; - Inc(D, Buffer[14] + $C33707D6 + (B xor (C and (A xor B)))); D := D shl 9 or D shr 23 + A; - Inc(C, Buffer[ 3] + $F4D50D87 + (A xor (B and (D xor A)))); C := C shl 14 or C shr 18 + D; - Inc(B, Buffer[ 8] + $455A14ED + (D xor (A and (C xor D)))); B := B shl 20 or B shr 12 + C; - Inc(A, Buffer[13] + $A9E3E905 + (C xor (D and (B xor C)))); A := A shl 5 or A shr 27 + B; - Inc(D, Buffer[ 2] + $FCEFA3F8 + (B xor (C and (A xor B)))); D := D shl 9 or D shr 23 + A; - Inc(C, Buffer[ 7] + $676F02D9 + (A xor (B and (D xor A)))); C := C shl 14 or C shr 18 + D; - Inc(B, Buffer[12] + $8D2A4C8A + (D xor (A and (C xor D)))); B := B shl 20 or B shr 12 + C; - - Inc(A, Buffer[ 5] + $FFFA3942 + (B xor C xor D)); A := A shl 4 or A shr 28 + B; - Inc(D, Buffer[ 8] + $8771F681 + (A xor B xor C)); D := D shl 11 or D shr 21 + A; - Inc(C, Buffer[11] + $6D9D6122 + (D xor A xor B)); C := C shl 16 or C shr 16 + D; - Inc(B, Buffer[14] + $FDE5380C + (C xor D xor A)); B := B shl 23 or B shr 9 + C; - Inc(A, Buffer[ 1] + $A4BEEA44 + (B xor C xor D)); A := A shl 4 or A shr 28 + B; - Inc(D, Buffer[ 4] + $4BDECFA9 + (A xor B xor C)); D := D shl 11 or D shr 21 + A; - Inc(C, Buffer[ 7] + $F6BB4B60 + (D xor A xor B)); C := C shl 16 or C shr 16 + D; - Inc(B, Buffer[10] + $BEBFBC70 + (C xor D xor A)); B := B shl 23 or B shr 9 + C; - Inc(A, Buffer[13] + $289B7EC6 + (B xor C xor D)); A := A shl 4 or A shr 28 + B; - Inc(D, Buffer[ 0] + $EAA127FA + (A xor B xor C)); D := D shl 11 or D shr 21 + A; - Inc(C, Buffer[ 3] + $D4EF3085 + (D xor A xor B)); C := C shl 16 or C shr 16 + D; - Inc(B, Buffer[ 6] + $04881D05 + (C xor D xor A)); B := B shl 23 or B shr 9 + C; - Inc(A, Buffer[ 9] + $D9D4D039 + (B xor C xor D)); A := A shl 4 or A shr 28 + B; - Inc(D, Buffer[12] + $E6DB99E5 + (A xor B xor C)); D := D shl 11 or D shr 21 + A; - Inc(C, Buffer[15] + $1FA27CF8 + (D xor A xor B)); C := C shl 16 or C shr 16 + D; - Inc(B, Buffer[ 2] + $C4AC5665 + (C xor D xor A)); B := B shl 23 or B shr 9 + C; - - Inc(A, Buffer[ 0] + $F4292244 + (C xor (B or not D))); A := A shl 6 or A shr 26 + B; - Inc(D, Buffer[ 7] + $432AFF97 + (B xor (A or not C))); D := D shl 10 or D shr 22 + A; - Inc(C, Buffer[14] + $AB9423A7 + (A xor (D or not B))); C := C shl 15 or C shr 17 + D; - Inc(B, Buffer[ 5] + $FC93A039 + (D xor (C or not A))); B := B shl 21 or B shr 11 + C; - Inc(A, Buffer[12] + $655B59C3 + (C xor (B or not D))); A := A shl 6 or A shr 26 + B; - Inc(D, Buffer[ 3] + $8F0CCC92 + (B xor (A or not C))); D := D shl 10 or D shr 22 + A; - Inc(C, Buffer[10] + $FFEFF47D + (A xor (D or not B))); C := C shl 15 or C shr 17 + D; - Inc(B, Buffer[ 1] + $85845DD1 + (D xor (C or not A))); B := B shl 21 or B shr 11 + C; - Inc(A, Buffer[ 8] + $6FA87E4F + (C xor (B or not D))); A := A shl 6 or A shr 26 + B; - Inc(D, Buffer[15] + $FE2CE6E0 + (B xor (A or not C))); D := D shl 10 or D shr 22 + A; - Inc(C, Buffer[ 6] + $A3014314 + (A xor (D or not B))); C := C shl 15 or C shr 17 + D; - Inc(B, Buffer[13] + $4E0811A1 + (D xor (C or not A))); B := B shl 21 or B shr 11 + C; - Inc(A, Buffer[ 4] + $F7537E82 + (C xor (B or not D))); A := A shl 6 or A shr 26 + B; - Inc(D, Buffer[11] + $BD3AF235 + (B xor (A or not C))); D := D shl 10 or D shr 22 + A; - Inc(C, Buffer[ 2] + $2AD7D2BB + (A xor (D or not B))); C := C shl 15 or C shr 17 + D; - Inc(B, Buffer[ 9] + $EB86D391 + (D xor (C or not A))); B := B shl 21 or B shr 11 + C; - - Inc(FDigest[0], A); - Inc(FDigest[1], B); - Inc(FDigest[2], C); - Inc(FDigest[3], D); -end; -{$Q+} (********************************************************************************************************************************) procedure signal_proc(signal_number: integer); cdecl; @@ -2090,11 +1761,6 @@ end; - - - - - initialization InternalLockInit(True); SetupSignals; |
