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