Subject: Re: Help with easy checksum
From: Erik Naggum <erik@naggum.no>
Date: 2000/02/14
Newsgroups: comp.lang.lisp,comp.text.interleaf
Message-ID: <3159560088216201@naggum.no>

* Scott Sheffield <scott.y.sheffield@lmco.com>
| I have created a binary file using Interleaf LISP and I need to put a
| checksum at the end of the file so that it can be loaded into the
| hardware.  The problem is though I have a good idea what a checksum is
| and does I have no idea how to do this in LISP.  Here is what I am told I
| need to do:

  when you are told what to do in language-specific terms, there's a
  serious flaw in the thinking of the person telling you what to do.  I'll
  try to correct that serious flaw by simplifying your requirement to this
  simple rule:

    when the contents of the file is regarded as a sequence of 32-bit
    integers, the value of the last 32-bit integer of the file is such that
    the sum of the 32-bit integers throughout the file is 0 modulo 2^32.

  this is fairly easy to hack together, but it depends on how you write to
  your stream.  if you write 32-bit integers to the stream (which means it
  should be opened with :ELEMENT-TYPE '(SIGNED-BYTE 32) or UNSIGNED-BYTE
  ditto), just sum them up while you write each integer, then write
  (LDB (BYTE 32 0) (- <SUM>)) to the same stream just before closing it.

  if you don't write 32-bit integers to the file, you're probably better
  off writing it in one pass and reading it as 32-bit integers in another
  pass, only to tack on the same 32-bit integer as describe above at the
  end.

| 2) Write 32 bit chunks to the file, also accumulate the 32 bit chunks
| into this 32 bit integer object. The accumulation is a add, ignoring
| overflows (which is standard), then the final step is to take the
| negative (2's complement) all the accumulations have been done.

  no, ignoring overflows is _not_ standard, it's a a hardware-oriented
  design decision local to particular languages.  the standard addition
  function does not overflow to begin with.  please don't confuse the
  internal hardware representation of a value with the value.

  however, whether you ignore overflow or extract a bit field from an
  integer is only a conceptual difference.  C-based life forms think in
  terms of ignoring overflow because they don't know what it means not to.
  Common Lisp programmers don't ignore overflows, but talk about values
  _modulo_ some other value, instead.

#:Erik