CRC-32 2/2

 BBS: Inland Empire Archive
Date: 01-03-93 (01:25)             Number: 210
From: ERIC B. FORD                 Refer#: NONE
  To: VICTOR YIU                    Recvd: NO  
Subj: CRC-32 2/2                     Conf: (2) Quik_Bas
'Adjust length- divide it by 4 using unsigned division
Length = ((Length AND &HFFFC) \ 4) AND &H3FFF

'Drop into loop
GOTO b

'Updates the CRC on the next 4 bytes, doing this 4 bytes at a time
'results in a small improvement in speed, since the compiled code
'will jump less often...
A:  CRC& = CRCTable&((CINT(CRC&) AND 255) XOR PEEK(Offset))
XOR (((CRC& AND &HFFFFFF00) \ 256) AND &HFFFFFF)
    CRC& = CRCTable&((CINT(CRC&) AND 255) XOR PEEK(Offset +
1)) XOR (((CRC& AND &HFFFFFF00) \ 256) AND &HFFFFFF)
    CRC& = CRCTable&((CINT(CRC&) AND 255) XOR PEEK(Offset +
2)) XOR (((CRC& AND &HFFFFFF00) \ 256) AND &HFFFFFF)
    CRC& = CRCTable&((CINT(CRC&) AND 255) XOR PEEK(Offset +
3)) XOR (((CRC& AND &HFFFFFF00) \ 256) AND &HFFFFFF)
    Offset = Offset + 4             '4 bytes over now
    Length = Length - 1             '1 less byte
b:  IF Length <> 0 THEN GOTO A      'if more bytes then go

'All done
    QBCRC& = CRC&

END FUNCTION

'Reads in the CRC table
SUB CRC32Init
    FOR A = 0 TO 255
        READ CRCTable&(A)
    NEXT
END SUB

'Finds a PKZIP compatible CRC of any size file.
'There isn't any error checking in this example program- that's up to you.
'(QuickBASIC 4.5 users must change the "SSEG" functions to "VARSEG"
'functions for this procedure to work properly.)
FUNCTION FileCRC& (FileName$)
    handle = FREEFILE                          'get free file handle
    OPEN FileName$ FOR INPUT AS handle: CLOSE  'check to see if it's there
    OPEN FileName$ FOR BINARY AS handle        'open file in binary mode
    b$ = SPACE$(LoadBuffer)                    'allocate space for buffer
    CRC& = &HFFFFFFFF                          'init CRC
    FOR A = 1 TO LOF(handle) \ LoadBuffer      'do the blocks
        GET handle, , b$                       'get block
        CRC& = QBCRC(SSEG(b$), SADD(b$), LoadBuffer, CRC&)
    NEXT
    whatsleft = LOF(handle) MOD LoadBuffer     'do the oddballs now
    b$ = SPACE$(whatsleft)                     'whatever's left over
    GET handle, , b$                           'get it
    CRC& = QBCRC(SSEG(b$), SADD(b$), whatsleft, CRC&)
    CLOSE handle                               'close file
    FileCRC& = NOT CRC&                        'return CRC to caller
END FUNCTION

---
 * Origin: Eric Ford (1:3632/1.6)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET