Author Topic: CRC16, CRC32, EOR, SUM and SHA1 functions  (Read 5630 times)

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
At long last (thanks to a lot of help from Kitty Hello - you're a legend!!!) , I now have CRC calculation functions that work correctly. I'm also rather proud of it as it's my first bit of inline code. It's a porting of the "GetCRC" code by Dirk Stöcker with a few additions. The original has been modified to use Kitty Hello's file reading function, and I've written a HEX conversion function as most utilities seem to report the results in HEX format.

It supports the following functions (listed are the routine and some of the utilities that use them)
CRC-16 1          : PCompress2, Arc, DMS, ProPack, LhA, Zoo, Shrink
CRC-16 2          : CCITT, XModem, LU
CRC-16 3          : Donald Kindred's CRC
CRC-16 4          : old Zoom, CompDisk
CRC-32 1          : Olaf Barthel's CRC, PCompress, Zoom 5, LhPak
CRC-32 2          : Zip, GZip, LZX, RAR, Arj
CRC-32 3          : Brik (Binary mode), Ace
CRC-32 4          : GPatch (old)
CRC-32 5          : BZip2
CRC-32 6          : BZip2 inverted
CRC-32 7          : chksum utility
CHS-16 1          : PowerPacker Passwords
CHS-16 2          : BSD sum
CHS-16 3          : SYSV sum
CHS-32 1M (WRAP)  : Olaf Barthel's CRC, Bootblock of OFS/FFS
CHS-32 1I         :
CHS-32 2          : LightFileSystem
EOR Byte          :
EOR Word (M/I)    :
EOR Long (M/I)    : ByteKiller, RSI-Packer
Sum Byte Signed   :
Sum Byte Unsigned : SQ
Sum Word Si (M/I) :
Sum Word Un (M/I) :
Sum Long M     : Root-, Bitmap-, File-, Datablock of OFS/FFS
Sum Long I        :
SHA1

The following would be supported if I knew a bit more about how to do inline code. It only requires either importing a few commands from some standard (external) C libraries like stdio.h (commands like memcpy) or providing the same functionality through GLBasic commands. I've commented the code out in case anyone wants to get the MD4/MD5 code working.
MD4               : EDonkey, Samba
MD5               :

I'd also put this in a library if I knew how to get a GLBasic library working properly with inline calls to functions, but it all works as is regardless. Chop out any of the computation functions that aren't relevant to you to streamline the code.

Usage

NOTE : The file you want to process must be able to fit totally within memory.

The inline "extern" block MUST go before you use any functions or you'll get errors about the function name not being "declared in this scope".

Example usage
Code: GLBasic [Select]
ReadFile("C:/temp/a.txt", CRCBuffer$)                                   // Read file into buffer
StringLength=LEN(CRCBuffer$)                                                    // Calculate its length


//
// CRC example - Calculate CRC32 according to IEEE802.3 standard
//

INLINE
CRCValue = DoCRC32_2((const unsigned char*)CRCBuffer_Str.c_str(), StringLength);
ENDINLINE
DEBUG "CRCValue2 = "+Convert_CRC_To_Hex$(CRCValue)+"\n"


//
// SHA1 example - Calculate SHA1 value for the file
//
GLOBAL SHA1ResultArray#[]
DIM SHA1ResultArray#[20]

INLINE
unsigned char Result[20];
sha1((const unsigned char*)CRCBuffer_Str.c_str(), StringLength, Result);
int Loop;
for (Loop=0;Loop<20;Loop++)
        SHA1ResultArray(Loop) = Result[Loop];
ENDINLINE

DEBUG "SHA1 result = "
FOR Loop = 0 TO 19
        DEBUG Convert_SHA_To_Hex$(SHA1ResultArray[Loop])
NEXT

The code is attached.


[attachment deleted by admin]
« Last Edit: 2009-Nov-20 by FutureCow »

MrTAToad

  • Guest
Re: CRC16, CRC32, EOR, SUM and SHA1 functions
« Reply #1 on: 2009-Nov-28 »
I've enabled MD4 & 5, and put in a function to allow the has value be returned from a string.

I've taken the test code out, as it prevents successful compiling.


[attachment deleted by admin]

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Re: CRC16, CRC32, EOR, SUM and SHA1 functions
« Reply #2 on: 2009-Nov-29 »
Awesome! Cheers MrTAToad!

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2640
    • View Profile
Re: CRC16, CRC32, EOR, SUM and SHA1 functions
« Reply #3 on: 2009-Nov-30 »
Great stuff, handy for all sorts off stuff, network error checking and and game updates immediately come to mind...   :happy:
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Re: CRC16, CRC32, EOR, SUM and SHA1 functions
« Reply #4 on: 2009-Nov-30 »
Non-interesting fact of the day : It was called checksum4 as it was my fourth complete rewrite trying to get the checksum functions working. I hope you'll all sleep better at night now.   :whistle:

MrTAToad

  • Guest
Re: CRC16, CRC32, EOR, SUM and SHA1 functions
« Reply #5 on: 2009-Dec-01 »
We will indeed!

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1781
    • View Profile
    • Homepage
Re: CRC16, CRC32, EOR, SUM and SHA1 functions
« Reply #6 on: 2011-Mar-27 »
I'm too stupid to use this, any hints? If I load the file as an extra file to my project and move out the code between "your code goes here" and "end" to my main program, I just get various error messages about not being declared and so on... I would realy like to use SHA1 but I have no idea how to write that myself in GLBasic.

Offline FutureCow

  • HelpEditor
  • Prof. Inline
  • ******
  • Posts: 680
    • View Profile
Re: CRC16, CRC32, EOR, SUM and SHA1 functions
« Reply #7 on: 2011-Mar-27 »
Hey Moru, did you note this line :
The inline "extern" block MUST go before you use any functions or you'll get errors about the function name not being "declared in this scope".

That's all I can suggest right now, it's been a long time since I ported this code, I'd have to load it up and try it again under the new version of GLB to ensure nothing's changed. Did you try the example I gave as well?

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1781
    • View Profile
    • Homepage
Re: CRC16, CRC32, EOR, SUM and SHA1 functions
« Reply #8 on: 2011-Mar-27 »
Yes I tried all that but I might be too tired at the moment, new try later when I really need the code :-)