13 #include <sys/types.h> 32 MediaBlockList::MediaBlockList(off_t size)
43 MediaBlockList::addBlock(off_t off,
size_t size)
47 return blocks.size() - 1;
51 MediaBlockList::setFileChecksum(std::string ctype,
int cl,
unsigned char *c)
57 memcpy(&fsum[0], c, cl);
60 const std::vector<unsigned char> &MediaBlockList::getFileChecksum()
66 MediaBlockList::createFileDigest(
Digest &digest)
const 68 return digest.
create(fsumtype);
72 MediaBlockList::verifyFileDigest(
Digest &digest)
const 74 if (!haveFileChecksum())
77 if (dig.empty() || dig.size() < fsum.size())
79 return memcmp(&dig[0], &fsum[0], fsum.size()) ?
false :
true;
83 MediaBlockList::setChecksum(
size_t blkno, std::string cstype,
int csl,
unsigned char *cs,
size_t cspad)
95 if (csl != chksumlen || cstype != chksumtype || cspad != chksumpad || blkno != chksums.size() / chksumlen)
97 chksums.resize(chksums.size() + csl);
98 memcpy(&chksums[csl * blkno], cs, csl);
102 MediaBlockList::setRsum(
size_t blkno,
int rsl,
unsigned int rs,
size_t rspad)
113 if (rsl != rsumlen || rspad != rsumpad || blkno != rsums.size())
119 MediaBlockList::createDigest(
Digest &digest)
const 121 return digest.
create(chksumtype);
125 MediaBlockList::verifyDigest(
size_t blkno,
Digest &digest)
const 127 if (!haveChecksum(blkno))
129 size_t size = blocks[blkno].size;
132 if (chksumpad > size)
134 char pad[chksumpad - size];
135 memset(pad, 0, chksumpad - size);
136 digest.
update(pad, chksumpad - size);
139 if (dig.empty() || dig.size() < size_t(chksumlen))
141 return memcmp(&dig[0], &chksums[chksumlen * blkno], chksumlen) ? false :
true;
145 MediaBlockList::updateRsum(
unsigned int rs,
const char* bytes,
size_t len)
const 150 s = (rs >> 16) & 65535;
152 for (; len > 0 ; len--)
154 unsigned short c = (
unsigned char)*bytes++;
158 return (s & 65535) << 16 | (m & 65535);
162 MediaBlockList::verifyRsum(
size_t blkno,
unsigned int rs)
const 164 if (!haveRsum(blkno))
166 size_t size = blocks[blkno].size;
172 s = (rs >> 16) & 65535;
174 m += s * (rsumpad - size);
175 rs = (s & 65535) << 16 | (m & 65535);
188 return rs == rsums[blkno];
192 MediaBlockList::checkRsum(
size_t blkno,
const unsigned char *buf,
size_t bufl)
const 194 if (blkno >= blocks.size() || bufl < blocks[blkno].size)
196 unsigned int rs = updateRsum(0, (
const char *)buf, blocks[blkno].size);
197 return verifyRsum(blkno, rs);
201 MediaBlockList::checkChecksum(
size_t blkno,
const unsigned char *buf,
size_t bufl)
const 203 if (blkno >= blocks.size() || bufl < blocks[blkno].size)
206 if (!createDigest(dig))
208 dig.
update((
const char *)buf, blocks[blkno].size);
209 return verifyDigest(blkno, dig);
212 std::vector<unsigned char> MediaBlockList::getChecksum(
size_t blkno)
214 if ( !haveChecksum(blkno) )
217 std::vector<unsigned char> buf ( chksumlen,
'\0' );
218 memcpy( buf.data(), chksums.data()+(chksumlen * blkno), chksumlen );
224 MediaBlockList::checkChecksumRotated(
size_t blkno,
const unsigned char *buf,
size_t bufl,
size_t start)
const 226 if (blkno >= blocks.size() || bufl < blocks[blkno].size)
231 if (!createDigest(dig))
233 size_t size = blocks[blkno].size;
234 size_t len = bufl - start > size ? size : bufl - start;
235 dig.
update((
const char *)buf + start, len);
237 dig.
update((
const char *)buf, size - len);
238 return verifyDigest(blkno, dig);
243 MediaBlockList::writeBlock(
size_t blkno, FILE *fp,
const unsigned char *buf,
size_t bufl,
size_t start, vector<bool> &found)
const 245 if (blkno >= blocks.size() || bufl < blocks[blkno].size)
247 off_t off = blocks[blkno].off;
248 size_t size = blocks[blkno].size;
249 if (fseeko(fp, off, SEEK_SET))
253 size_t len = bufl - start > size ? size : bufl - start;
254 if (fwrite(buf + start, len, 1, fp) != 1)
256 if (size > len && fwrite(buf, size - len, 1, fp) != 1)
259 found[blocks.size()] =
true;
263 fetchnext(FILE *fp,
unsigned char *bp,
size_t blksize,
size_t pushback,
unsigned char *pushbackp)
271 memmove(bp, pushbackp, pushback);
290 MediaBlockList::reuseBlocks(FILE *wfp,
string filename)
294 if (!chksumlen || (fp = fopen(filename.c_str(),
"r")) == 0)
296 size_t nblks = blocks.size();
298 found.resize(nblks + 1);
299 if (rsumlen && !rsums.empty())
301 size_t blksize = blocks[0].size;
302 if (nblks == 1 && rsumpad && rsumpad > blksize)
305 unsigned int hm = rsums.size() * 2;
306 while (hm & (hm - 1))
311 unsigned int *ht =
new unsigned int[hm + 1];
312 memset(ht, 0, (hm + 1) *
sizeof(
unsigned int));
313 for (
unsigned int i = 0; i < rsums.size(); i++)
315 if (blocks[i].size != blksize && (i != nblks - 1 || rsumpad != blksize))
317 unsigned int r = rsums[i];
318 unsigned int h = r & hm;
325 unsigned char *buf =
new unsigned char[blksize];
326 unsigned char *buf2 =
new unsigned char[blksize];
328 unsigned char *pushbackp = 0;
330 if ((blksize & (blksize - 1)) == 0)
331 for (bshift = 0; size_t(1 << bshift) != blksize; bshift++)
335 memset(buf, 0, blksize);
338 int sql = nblks > 1 && chksumlen < 16 ? 2 : 1;
341 for (
size_t i = 0; i < blksize; i++)
367 b += a - (oc << bshift);
369 b += a - oc * blksize;
372 if (
size_t(i) != blksize - 1)
378 r = ((
unsigned int)b & 255);
379 else if (rsumlen == 2)
380 r = ((
unsigned int)b & 65535);
381 else if (rsumlen == 3)
382 r = ((
unsigned int)a & 255) << 16 | ((
unsigned int)b & 65535);
384 r = ((
unsigned int)a & 65535) << 16 | ((
unsigned int)b & 65535);
385 unsigned int h = r & hm;
387 for (; ht[h]; h = (h + hh++) & hm)
389 size_t blkno = ht[h] - 1;
390 if (rsums[blkno] != r)
396 if (eof || blkno + 1 >= nblks)
398 pushback =
fetchnext(fp, buf2, blksize, pushback, pushbackp);
402 if (!checkRsum(blkno + 1, buf2, blksize))
405 if (!checkChecksumRotated(blkno, buf, blksize, i + 1))
407 if (sql == 2 && !checkChecksum(blkno + 1, buf2, blksize))
409 writeBlock(blkno, wfp, buf, blksize, i + 1, found);
412 writeBlock(blkno + 1, wfp, buf2, blksize, 0, found);
419 pushback =
fetchnext(fp, buf2, blksize, pushback, pushbackp);
423 if (!checkRsum(blkno, buf2, blksize))
425 if (!checkChecksum(blkno, buf2, blksize))
427 writeBlock(blkno, wfp, buf2, blksize, 0, found);
431 memset(buf, 0, blksize);
442 else if (chksumlen >= 16)
447 unsigned char *buf =
new unsigned char[bufl];
448 for (
size_t blkno = 0; blkno < blocks.size(); ++blkno)
450 if (off > blocks[blkno].off)
452 size_t blksize = blocks[blkno].size;
457 buf =
new unsigned char[bufl];
459 size_t skip = blocks[blkno].off - off;
462 size_t l = skip > bufl ? bufl : skip;
463 if (fread(buf, l, 1, fp) != 1)
468 if (fread(buf, blksize, 1, fp) != 1)
470 if (checkChecksum(blkno, buf, blksize))
471 writeBlock(blkno, wfp, buf, blksize, 0, found);
478 std::vector<MediaBlock> nblocks;
479 std::vector<unsigned char> nchksums;
480 std::vector<unsigned int> nrsums;
482 for (
size_t blkno = 0; blkno < blocks.size(); ++blkno)
487 nblocks.push_back(blocks[blkno]);
488 if (chksumlen && (blkno + 1) * chksumlen <= chksums.size())
490 nchksums.resize(nblocks.size() * chksumlen);
491 memcpy(&nchksums[(nblocks.size() - 1) * chksumlen], &chksums[blkno * chksumlen], chksumlen);
493 if (rsumlen && (blkno + 1) <= rsums.size())
494 nrsums.push_back(rsums[blkno]);
508 if (filesize != off_t(-1))
510 long long size = filesize;
514 s =
"[ BlockList, filesize unknown\n";
516 s +=
" No block information\n";
521 for (i = 0; i < blocks.size(); ++i)
523 long long off=blocks[i].off;
524 long long size=blocks[i].size;
526 if (chksumlen && chksums.size() >= (i + 1) * chksumlen)
528 s +=
" " + chksumtype +
":";
529 for (j = 0; j < size_t(chksumlen); j++)
532 if (rsumlen && rsums.size() > i)
std::string asString(const DefaultIntegral< Tp, TInitial > &obj)
Compute Message Digests (MD5, SHA1 etc)
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
std::vector< unsigned char > digestVector()
get vector of unsigned char representation of the digest
bool create(const std::string &name)
initialize creation of a new message digest
Easy-to use interface to the ZYPP dependency resolver.
bool update(const char *bytes, size_t len)
feed data into digest computation algorithm