hashsum.cc 1.3 KB

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