home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / turbopas / crcasm.arc / TESTCRC.PAS < prev   
Pascal/Delphi Source File  |  1989-10-30  |  2KB  |  72 lines

  1. {$A+,B-,D-,E-,F-,I+,L-,N-,O-,R-,S-,V+}
  2. {$M 8192,0,0}
  3. { TESTCRC - Simple test program for crc routines.  This program opens the
  4.   file specified in the first command line parameter, computes three
  5.   kinds of CRC, and writes them to stdout in hex.  Then it checks the
  6.   peculiar behavior of the XModem CRC.  E. Floyd [76067,747], 10-29-89. }
  7. Program TestCRC;
  8. Uses CRC;
  9. Const
  10.   BufSize = 32768;
  11. Type
  12.   Str2 = String[2];
  13.   Str4 = String[4];
  14.   Str8 = String[8];
  15. Var
  16.   Crc32 : LongInt;
  17.   InFile : File;
  18.   InBuf : Array[1..BufSize] Of Byte;
  19.   Len, Crc16, CrcArc, SaveCrc : Word;
  20.  
  21. Function HexByte(b : Byte) : Str2;
  22. Const
  23.   Hex : Array[$0..$F] Of Char = '0123456789abcdef';
  24. Begin
  25.   HexByte := Hex[b Shr 4] + Hex[b And $F];
  26. End;
  27.  
  28. Function HexWord(w : Word) : Str4;
  29. Begin
  30.   HexWord := HexByte(Hi(w)) + HexByte(Lo(w));
  31. End;
  32.  
  33. Function HexLong(ww : LongInt) : Str8;
  34. Var
  35.   w : Array[1..2] Of Word Absolute ww;
  36. Begin
  37.   HexLong := HexWord(w[2]) + HexWord(w[1]);
  38. End;
  39.  
  40. Begin
  41.   If ParamCount < 1 Then Begin
  42.     WriteLn('Run like: TESTCRC <filename>');
  43.     WriteLn('Prints crc16, CrcArc and crc32 in hex');
  44.   End Else Begin
  45.     {$I-}
  46.     Assign(InFile, ParamStr(1));
  47.     Reset(InFile, 1);
  48.     {$I+}
  49.     If IoResult = 0 Then Begin
  50.       Crc16 := 0;            { "XModem" crc starts with zero.. }
  51.       CrcArc := 0;           { ..as does ARC crc }
  52.       Crc32 := $FFFFFFFF;    { 32 bit crc starts with all bits on }
  53.       Repeat
  54.         BlockRead(InFile, InBuf, BufSize, Len);
  55.         Crc16 := UpdateCrc16(Crc16, InBuf, Len);
  56.         CrcArc := UpdateCrcArc(CrcArc, InBuf, Len);
  57.         Crc32 := UpdateCrc32(Crc32, InBuf, Len);
  58.       Until Eof(InFile);
  59.       Close(InFile);
  60.       SaveCrc := Crc16;      { Save near-complete XModem crc for test below }
  61.       FillChar(InBuf, 2, 0); { Finish XModem crc with two nulls }
  62.       Crc16 := UpdateCrc16(Crc16, InBuf, 2);
  63.       Crc32 := Not(Crc32);   { Finish 32 bit crc by inverting all bits }
  64.       WriteLn('Crc16 = ', HexWord(Crc16), ', CrcArc = ', HexWord(CrcArc),
  65.         ', Crc32 = ', HexLong(Crc32));
  66.       { Now test for XModem crc trick - update the near-complete crc with.. }
  67.       Crc16 := Swap(Crc16);  { ..the complete crc in hi:lo order in memory. }
  68.       WriteLn('XModem crc test = ', HexWord(UpdateCrc16(SaveCrc, Crc16, 2)));
  69.       { The result should always be zero }
  70.     End Else WriteLn('Unable to open file ', ParamStr(1));
  71.   End;
  72. End.