Crc32 Routine in all 2/2

 BBS: Inland Empire Archive
Date: 09-17-82 (15:22)             Number: 241
From: RICH GELDREICH               Refer#: NONE
  To: ALL                           Recvd: NO  
Subj: Crc32 Routine in all  2/2      Conf: (2) Quik_Bas
>>> Continued from previous message
    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

'Uses in-line coding to find a CRC-32 of a block of memory
'Although the "Length" variable is a signed integer, memory blocks >32k
'may be processed by this routine. (To process a block of memory 65535
'bytes long, -1, or &HFFFF, would be passed to this procedure, for
'instance.)
'
FUNCTION QBCRC& (BYVAL Segment, BYVAL Offset, BYVAL Length, BYVAL CRC&)
'QB4.5 users : FUNCTION QBCRC& (Segment,Offset,Length,CRC&)

'switch to input segment
DEF SEG = Segment


'Since we're going to calculate the CRC 4 bytes at a time, this FOR/NEXT
'takes care of all the oddballs left over...
FOR A = 1 TO Length AND 3
    CRC& = CRCTable&((CINT(CRC&) AND 255) XOR PEEK(Offset)) XOR _
(((CRC& AND &HFFFFFF00) \ 256) AND &HFFFFFF)
    Offset = Offset + 1
NEXT

'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
---
 * SLMR 2.0 * It's a good day to die. ...unless you want to live!(duh)

--- MsgToss 2.0b
 * Origin: Computer Co-Op - Voorhees, NJ | Ted Hare (1:266/29)
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