hashsum.cc 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Cryptographic API Base
  2. #include <config.h>
  3. #include <unistd.h>
  4. #include "hashsum_template.h"
  5. // Summation::AddFD - Add content of file into the checksum /*{{{*/
  6. // ---------------------------------------------------------------------
  7. /* */
  8. bool SummationImplementation::AddFD(int const Fd, unsigned long long Size) {
  9. unsigned char Buf[64 * 64];
  10. bool const ToEOF = (Size == 0);
  11. while (Size != 0 || ToEOF)
  12. {
  13. unsigned long long n = sizeof(Buf);
  14. if (!ToEOF) n = std::min(Size, n);
  15. ssize_t const Res = read(Fd, Buf, n);
  16. if (Res < 0 || (!ToEOF && Res != (ssize_t) n)) // error, or short read
  17. return false;
  18. if (ToEOF && Res == 0) // EOF
  19. break;
  20. Size -= Res;
  21. Add(Buf,Res);
  22. }
  23. return true;
  24. }
  25. bool SummationImplementation::AddFD(FileFd &Fd, unsigned long long Size) {
  26. unsigned char Buf[64 * 64];
  27. bool const ToEOF = (Size == 0);
  28. while (Size != 0 || ToEOF)
  29. {
  30. unsigned long long n = sizeof(Buf);
  31. if (!ToEOF) n = std::min(Size, n);
  32. unsigned long long a = 0;
  33. if (Fd.Read(Buf, n, &a) == false) // error
  34. return false;
  35. if (ToEOF == false)
  36. {
  37. if (a != n) // short read
  38. return false;
  39. }
  40. else if (a == 0) // EOF
  41. break;
  42. Size -= a;
  43. Add(Buf, a);
  44. }
  45. return true;
  46. }
  47. /*}}}*/