fdchecksum32.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*====================================================================*
  2. *
  3. * uint32_t fdchecksum32 (int fd, size_t extent, uint32_t checksum);
  4. *
  5. * memory.h
  6. *
  7. * return the 32-bit checksum of a file region starting from the
  8. * current file position; extent is specified in bytes but will be
  9. * rounded to a multiple of four bytes;
  10. *
  11. * Motley Tools by Charles Maier;
  12. * Copyright (c) 2001-2006 by Charles Maier Associates;
  13. * Licensed under the Internet Software Consortium License;
  14. *
  15. *--------------------------------------------------------------------*/
  16. #ifndef FDCHECKSUM32_SOURCE
  17. #define FDCHECKSUM32_SOURCE
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include "../tools/memory.h"
  22. uint32_t fdchecksum32 (int fd, register size_t extent, register uint32_t checksum)
  23. {
  24. uint32_t memory;
  25. while (extent >= sizeof (memory))
  26. {
  27. if (read (fd, &memory, sizeof (memory)) != sizeof (memory))
  28. {
  29. return (-1);
  30. }
  31. extent -= sizeof (memory);
  32. checksum ^= memory;
  33. }
  34. return (~checksum);
  35. }
  36. #endif