home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / aros / calcchecksum.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-27  |  1.9 KB  |  84 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: calcchecksum.c,v 1.3 1997/01/27 00:17:40 ldp Exp $
  4.     $Log: calcchecksum.c,v $
  5.     Revision 1.3  1997/01/27 00:17:40  ldp
  6.     Include proto instead of clib
  7.  
  8.     Revision 1.2  1996/12/10 13:59:44  aros
  9.     Moved #include into first column to allow makedepend to see it.
  10.  
  11.     Revision 1.1  1996/08/01 18:46:46  digulla
  12.     Calculate simple checksum for a block of memory
  13.  
  14.     Desc:
  15.     Lang:
  16. */
  17. #include <aros/system.h>
  18.  
  19. /*****************************************************************************
  20.  
  21.     NAME */
  22. #include <proto/aros.h>
  23.  
  24.     ULONG CalcChecksum (
  25.  
  26. /*  SYNOPSIS */
  27.     APTR  memory,
  28.     ULONG size)
  29.  
  30. /*  FUNCTION
  31.     Calculate a checksum for a given area of memory.
  32.  
  33.     INPUTS
  34.     memory - Start here
  35.     size - This many bytes. Must be a multiple of sizeof(ULONG)
  36.  
  37.     RESULT
  38.     The checksum for the memory. If you store the checksum somewhere
  39.     in the area and run CalcChecksum() again, the result will be 0.
  40.     To achieve this, you must set the place, where the checksum will
  41.     be placed later, to 0 before you call the function.
  42.  
  43.     NOTES
  44.     This function is not part of a library and may thus be called
  45.     any time.
  46.  
  47.     EXAMPLE
  48.     ULONG mem[512];
  49.  
  50.     mem[0] = 0; // Store checksum here
  51.     mem[0] = CalcChecksum (mem, sizeof (mem));
  52.  
  53.     if (CalcChecksum (mem, sizeof (mem))
  54.         printf ("Something is wrong !!\n");
  55.     else
  56.         printf ("Data is unchanged.\n");
  57.  
  58.     BUGS
  59.  
  60.     SEE ALSO
  61.     SumKickData(), SumLibrary()
  62.  
  63.     INTERNALS
  64.     The function uses the DOS way: sum all the ULONGs and return the
  65.     negative result. Not very safe, but then it's quite fast :)
  66.  
  67.     HISTORY
  68.     26-10-95    digulla created
  69.  
  70. ******************************************************************************/
  71. {
  72.     ULONG   sum;
  73.     ULONG * lptr;
  74.  
  75.     assert (memory);
  76.     assert ((size&(sizeof(ULONG)-1))==0);
  77.  
  78.     for (sum=0, lptr=(ULONG *)memory; size>0; size-=sizeof(ULONG))
  79.     sum += *lptr ++;
  80.  
  81.     return -sum;
  82. } /* CalcChecksum */
  83.  
  84.