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