From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: *** X-Spam-Status: No, score=3.2 required=5.0 tests=BAYES_00,RATWARE_MS_HASH, RATWARE_OUTLOOK_NONAME autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,9c86eb13dd395066 X-Google-Attributes: gid103376,public From: "David C. Hoos, Sr." Subject: Re: CRC in Ada? Date: 1997/03/05 Message-ID: <01bc2962$13e77140$018c71a5@dhoossr.iquest.com> X-Deja-AN: 223250010 Distribution: world References: <1997Mar2.220652@nova.wright.edu> Organization: Ada95 Press, Inc. Newsgroups: comp.lang.ada Date: 1997-03-05T00:00:00+00:00 List-Id: This is a multi-part message in MIME format. ------=_NextPart_000_01BC292F.C954A260 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Here is an implementation of a 16-bit CRC in both C and Ada. The C code is perhaps ten years old, and is from an implementation of Xmodem. The Ada code was translated from that. The test driver tests both the C code (as an imported set of functions), and the Ada code. The form of the test driver is not suitable for performance testing, as the CRC generation is swamped by the file reads. I'd like to modify this in the next couple of days so as to be able to compare the speeds of the two implementations. The test driver does serve to demonstrate the equivalence of the two implementations, though. The generating polynomial is one frequently used (x1021), but the value can be changed as one likes. -- David C. Hoos, Sr., http://www.dbhwww.com http://www.ada95.com Dr. John B. Matthews wrote in article <1997Mar2.220652@nova.wright.edu>... > Hi! Can anyone point me to 16-bit CRC code in Ada? I've checked the > PAL and several other archives without luck. Any help appreciated. > > Thanks, > > John > ---------------------------------------------------------------- > John B. Matthews, M.D. > jmatthews@nova.wright.edu; john_matthews@ccmail.dayton.saic.com > "Whom the gods would destroy, they first invite to program in C" > > ------=_NextPart_000_01BC292F.C954A260 Content-Type: application/octet-stream; name="crc.c" Content-Transfer-Encoding: 7bit Content-Description: crc.c (C source code file) Content-Disposition: attachment; filename="crc.c" /* crc.c */ #include "crc.h" #define POLY 0x1021 static unsigned short CRCtable[256]; /* calculate CRC table entry */ unsigned short CalcTable(data,genpoly,accum) unsigned short data; unsigned short genpoly; unsigned short accum; {static int i; data <<= 8; for(i=8;i>0;i--) { if((data^accum) & 0x8000) accum = (accum << 1) ^ genpoly; else accum <<= 1; data <<= 1; } return(accum); } /* initialize CRC table */ void InitCRC(void) {int i; for(i=0;i<256;i++) CRCtable[i] = CalcTable(i,POLY,0); } /* compute updated CRC */ unsigned short UpdateCRC(crc,byte) unsigned short crc; unsigned char byte; { return( (crc << 8) ^ CRCtable[ (crc >> 8) ^ byte ] ); } ------=_NextPart_000_01BC292F.C954A260 Content-Type: application/octet-stream; name="crc.h" Content-Transfer-Encoding: 7bit Content-Description: crc.h (C Header File) Content-Disposition: attachment; filename="crc.h" void InitCRC(void); unsigned short UpdateCRC(unsigned short,unsigned char); ------=_NextPart_000_01BC292F.C954A260 Content-Type: application/octet-stream; name="crc_pkg.adb" Content-Transfer-Encoding: 7bit Content-Description: crc_pkg.adb (ObjectAda File) Content-Disposition: attachment; filename="crc_pkg.adb" package body CRC_pkg is type Lookup_Table_Type is array (Interfaces.Unsigned_8) of Interfaces.Unsigned_16; Lookup_Table : Lookup_Table_Type; procedure Update (The_CRC_Value : in out Interfaces.Unsigned_16; With_The_Byte : in Interfaces.Unsigned_8) is use Interfaces; begin The_CRC_Value := Shift_Left (Value => The_CRC_Value, Amount => 8) xor Lookup_Table (Interfaces.Unsigned_8 (Shift_Right (Value => The_CRC_Value, Amount => 8)) xor With_The_Byte); end Update; begin -- CRC package initialization declare function Lookup_Table_Entry (For_The_Index : in Interfaces.Unsigned_8; And_The_Polynomial : in Interfaces.Unsigned_16) return Interfaces.Unsigned_16 is use Interfaces; Temporary_Data : Interfaces.Unsigned_16 := Shift_Left (Value => Interfaces.Unsigned_16 (For_The_Index), Amount => 8); Temporary_Accumulator : Interfaces.Unsigned_16 := 0; begin for I in reverse 1 .. 8 loop if ((Temporary_Data xor Temporary_Accumulator) and 16#8000#) > 0 then Temporary_Accumulator := Shift_Left (Value => Temporary_Accumulator, Amount => 1) xor And_The_Polynomial; else Temporary_Accumulator := Shift_Left (Value => Temporary_Accumulator, Amount => 1); end if; Temporary_Data := Shift_Left (Value => Temporary_Data, Amount => 1); end loop; return Temporary_Accumulator; end Lookup_Table_Entry; begin for I in Lookup_Table'Range loop Lookup_Table (I) := Lookup_Table_Entry (For_The_Index => I, And_The_Polynomial => Polynomial); end loop; end; end CRC_pkg; ------=_NextPart_000_01BC292F.C954A260 Content-Type: application/octet-stream; name="crc_pkg.ads" Content-Transfer-Encoding: 7bit Content-Description: crc_pkg.ads (ObjectAda File) Content-Disposition: attachment; filename="crc_pkg.ads" with Interfaces; package CRC_pkg is use type Interfaces.Unsigned_8; use type Interfaces.Unsigned_16; Polynomial : constant Interfaces.Unsigned_16 := 16#1021#; procedure Update (The_CRC_Value : in out Interfaces.Unsigned_16; With_The_Byte : in Interfaces.Unsigned_8); end CRC_pkg; ------=_NextPart_000_01BC292F.C954A260 Content-Type: application/octet-stream; name="test_crc.adb" Content-Transfer-Encoding: quoted-printable Content-Description: test_crc.adb (ObjectAda File) Content-Disposition: attachment; filename="test_crc.adb" with Ada.Command_Line; with Ada.Sequential_IO; with Ada.Text_IO; with CRC_pkg; with Interfaces.C; procedure Test_CRC is package C renames Interfaces.C; package Command_Line renames Ada.Command_Line; package Text_IO renames Ada.Text_IO; use type C.Unsigned_Char; use type C.Unsigned_Short; procedure Init_CRC; function Updated_CRC=20 (Previous_CRC_Accumulation : C.Unsigned_Short; Current_Byte : C.Unsigned_char) return C.Unsigned_Short; pragma Import (C, Init_CRC, "InitCRC"); pragma Import (C, Updated_CRC, "UpdateCRC"); pragma Linker_Options ("crc.o"); Current_CRC_Accumulation : C.Unsigned_Short; package Unsigned_Short_IO is new Ada.Text_IO.Modular_IO = (C.Unsigned_Short); package Unsigned_Char_IO is new Ada.Sequential_IO (C.Unsigned_Char); The_File : Unsigned_Char_IO.File_Type; The_Unsigned_Char : C.Unsigned_Char; begin Text_Io.Put_Line (Integer'Image (C.Unsigned_Char'Size)); Text_Io.Put_Line ("16-bit CRCs (x1021) by file:"); Text_Io.Put_Line ("(Using imported C subprograms)"); Init_CRC; for F in 1 .. Command_Line.Argument_Count loop Text_IO.Put=20 ("File """ & Command_Line.Argument (F)); begin Unsigned_Char_IO.Open (File =3D> The_File, Name =3D> Command_Line.Argument (F), Mode =3D> Unsigned_Char_IO.In_File); exception when Text_Io.Name_Error =3D> Text_IO.Put_Line (""": -- NOT FOUND!"); end; if Unsigned_Char_IO.Is_Open (The_File) then Current_CRC_Accumulation :=3D 0; while not Unsigned_Char_IO.End_Of_File (The_File) loop Unsigned_Char_IO.Read=20 (Item =3D> The_Unsigned_Char, File =3D> The_File); Current_CRC_Accumulation :=3D Updated_CRC (Previous_CRC_Accumulation =3D> Current_CRC_Accumulation, Current_Byte =3D> The_Unsigned_Char); end loop; Unsigned_Char_IO.Close (The_File); Text_IO.Put (""":"); Unsigned_Short_IO.Put (Item =3D> Current_CRC_Accumulation, Width =3D> 10, Base =3D> 16); Text_IO.New_Line; end if; end loop; =20 Text_Io.Put_Line ("(Using Ada subprogram)"); for F in 1 .. Command_Line.Argument_Count loop Text_IO.Put=20 ("File """ & Command_Line.Argument (F)); begin Unsigned_Char_IO.Open (File =3D> The_File, Name =3D> Command_Line.Argument (F), Mode =3D> Unsigned_Char_IO.In_File); exception when Text_Io.Name_Error =3D> Text_IO.Put_Line (""": -- NOT FOUND!"); end; if Unsigned_Char_IO.Is_Open (The_File) then Current_CRC_Accumulation :=3D 0; while not Unsigned_Char_IO.End_Of_File (The_File) loop Unsigned_Char_IO.Read=20 (Item =3D> The_Unsigned_Char, File =3D> The_File); CRC_Pkg.Update (The_Crc_Value =3D> Interfaces.Unsigned_16 = (Current_CRC_Accumulation), With_The_Byte =3D> Interfaces.Unsigned_8 (The_Unsigned_Char)); end loop; Unsigned_Char_IO.Close (The_File); Text_IO.Put (""":"); Unsigned_Short_IO.Put (Item =3D> Current_CRC_Accumulation, Width =3D> 10, Base =3D> 16); Text_IO.New_Line; end if; end loop; =20 end Test_CRC; ------=_NextPart_000_01BC292F.C954A260--