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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,255a2533fba89f5 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-09-18 11:31:19 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!12.120.28.37.MISMATCH!attla2!ip.att.net!attbi_feed3!attbi.com!sccrnsc01.POSTED!not-for-mail From: tmoran@acm.org Newsgroups: comp.lang.ada Subject: Parity using 'Write, was Re: Calculate and set Parity References: X-Newsreader: Tom's custom newsreader Message-ID: <0C3i9.459302$me6.55005@sccrnsc01> NNTP-Posting-Host: 12.234.13.56 X-Complaints-To: abuse@attbi.com X-Trace: sccrnsc01 1032373756 12.234.13.56 (Wed, 18 Sep 2002 18:29:16 GMT) NNTP-Posting-Date: Wed, 18 Sep 2002 18:29:16 GMT Organization: AT&T Broadband Date: Wed, 18 Sep 2002 18:29:16 GMT Xref: archiver1.google.com comp.lang.ada:29123 Date: 2002-09-18T18:29:16+00:00 List-Id: Assuming a reasonable compiler implementation of streams, where extra bits from base type expansion are zeros, here's a way to calculate the parity (or byte checksum) of an arbitrary record using 'write. It accumulates a checksum in a stream object, and uses functions to fetch a byte or a parity boolean from that accumulated checksum element. S : aliased Xors.Stream_Type; type something is record ... ... S.Check_Element := 0; Some_Record_Type'Write(S'access, R); Parity := Xors.Parity(S); Checksum_Byte := Xors.Check_Byte(S); ... with Ada.Streams; package Xors is type Stream_Type is new Ada.Streams.Root_Stream_Type with record Check_Element: Ada.Streams.Stream_Element := 0; end record; procedure Read(Stream : in out Stream_Type; -- nop Item : out Ada.Streams.Stream_Element_Array; Last : out Ada.Streams.Stream_Element_Offset); procedure Write(Stream : in out Stream_Type; Item : in Ada.Streams.Stream_Element_Array); function Check_Byte(Stream : Stream_Type) return Ada.Streams.Stream_Element; function Parity(Stream : Stream_Type) return Boolean; end Xors; package body Xors is use Ada.Streams; procedure Read(Stream : in out Stream_Type; -- nop Item : out Stream_Element_Array; Last : out Stream_Element_Offset) is begin Last := Item'First - 1; end Read; procedure Write(Stream : in out Stream_Type; Item : in Stream_Element_Array) is begin for I in Item'range loop Stream.Check_Element := Stream.Check_Element xor Item(I); end loop; end Write; function Check_Byte(Stream : Stream_Type) return Stream_Element is Source : Stream_Element := Stream.Check_Element; Target : Stream_Element := 0; begin while Source /= 0 loop Target := Target xor (Source mod 255); -- assume integer'size >= stream_element'size Source := Stream_Element(Integer(Source) / 256); end loop; return Target; end Check_Byte; function Parity(Stream : Stream_Type) return Boolean is Source : Stream_Element := Check_Byte(Stream); Target : Boolean := False; begin while Source /= 0 loop Target := Target xor ((Source mod 2) = 1); Source := Source / 2; end loop; return Target; end Parity; end Xors;