13 #include <sys/types.h>
28 using namespace zypp::base;
33 MediaBlockList::MediaBlockList(off_t size)
44 MediaBlockList::addBlock(off_t off,
size_t size)
48 return blocks.size() - 1;
52 MediaBlockList::setFileChecksum(std::string ctype,
int cl,
unsigned char *c)
58 memcpy(&fsum[0], c, cl);
62 MediaBlockList::createFileDigest(
Digest &digest)
const
64 return digest.
create(fsumtype);
68 MediaBlockList::verifyFileDigest(
Digest &digest)
const
70 if (!haveFileChecksum())
73 if (dig.empty() || dig.size() < fsum.size())
75 return memcmp(&dig[0], &fsum[0], fsum.size()) ?
false :
true;
79 MediaBlockList::setChecksum(
size_t blkno, std::string cstype,
int csl,
unsigned char *cs,
size_t cspad)
91 if (csl != chksumlen || cstype != chksumtype || cspad != chksumpad || blkno != chksums.size() / chksumlen)
93 chksums.resize(chksums.size() + csl);
94 memcpy(&chksums[csl * blkno], cs, csl);
98 MediaBlockList::setRsum(
size_t blkno,
int rsl,
unsigned int rs,
size_t rspad)
109 if (rsl != rsumlen || rspad != rsumpad || blkno != rsums.size())
115 MediaBlockList::createDigest(
Digest &digest)
const
117 return digest.
create(chksumtype);
121 MediaBlockList::verifyDigest(
size_t blkno,
Digest &digest)
const
123 if (!haveChecksum(blkno))
125 size_t size = blocks[blkno].size;
128 if (chksumpad > size)
130 char pad[chksumpad - size];
131 memset(pad, 0, chksumpad - size);
132 digest.
update(pad, chksumpad - size);
135 if (dig.empty() || dig.size() < size_t(chksumlen))
137 return memcmp(&dig[0], &chksums[chksumlen * blkno], chksumlen) ?
false :
true;
141 MediaBlockList::updateRsum(
unsigned int rs,
const char* bytes,
size_t len)
const
146 s = (rs >> 16) & 65535;
148 for (; len > 0 ; len--)
150 unsigned short c = (
unsigned char)*bytes++;
154 return (s & 65535) << 16 | (m & 65535);
158 MediaBlockList::verifyRsum(
size_t blkno,
unsigned int rs)
const
160 if (!haveRsum(blkno))
162 size_t size = blocks[blkno].size;
168 s = (rs >> 16) & 65535;
170 m += s * (rsumpad - size);
171 rs = (s & 65535) << 16 | (m & 65535);
184 return rs == rsums[blkno];
188 MediaBlockList::checkRsum(
size_t blkno,
const unsigned char *buf,
size_t bufl)
const
190 if (blkno >= blocks.size() || bufl < blocks[blkno].size)
192 unsigned int rs = updateRsum(0, (
const char *)buf, blocks[blkno].size);
193 return verifyRsum(blkno, rs);
197 MediaBlockList::checkChecksum(
size_t blkno,
const unsigned char *buf,
size_t bufl)
const
199 if (blkno >= blocks.size() || bufl < blocks[blkno].size)
202 if (!createDigest(dig))
204 dig.
update((
const char *)buf, blocks[blkno].size);
205 return verifyDigest(blkno, dig);
210 MediaBlockList::checkChecksumRotated(
size_t blkno,
const unsigned char *buf,
size_t bufl,
size_t start)
const
212 if (blkno >= blocks.size() || bufl < blocks[blkno].size)
217 if (!createDigest(dig))
219 size_t size = blocks[blkno].size;
220 size_t len = bufl - start > size ? size : bufl - start;
221 dig.
update((
const char *)buf + start, len);
223 dig.
update((
const char *)buf, size - len);
224 return verifyDigest(blkno, dig);
229 MediaBlockList::writeBlock(
size_t blkno, FILE *fp,
const unsigned char *buf,
size_t bufl,
size_t start, vector<bool> &found)
const
231 if (blkno >= blocks.size() || bufl < blocks[blkno].size)
233 off_t off = blocks[blkno].off;
234 size_t size = blocks[blkno].size;
235 if (fseeko(fp, off, SEEK_SET))
239 size_t len = bufl - start > size ? size : bufl - start;
240 if (fwrite(buf + start, len, 1, fp) != 1)
242 if (size > len && fwrite(buf, size - len, 1, fp) != 1)
245 found[blocks.size()] =
true;
249 fetchnext(FILE *fp,
unsigned char *bp,
size_t blksize,
size_t pushback,
unsigned char *pushbackp)
257 memmove(bp, pushbackp, pushback);
276 MediaBlockList::reuseBlocks(FILE *wfp,
string filename)
280 if (!chksumlen || (fp = fopen(filename.c_str(),
"r")) == 0)
282 size_t nblks = blocks.size();
284 found.resize(nblks + 1);
285 if (rsumlen && !rsums.empty())
287 size_t blksize = blocks[0].size;
288 if (nblks == 1 && rsumpad && rsumpad > blksize)
291 unsigned int hm = rsums.size() * 2;
292 while (hm & (hm - 1))
297 unsigned int *ht =
new unsigned int[hm + 1];
298 memset(ht, 0, (hm + 1) *
sizeof(
unsigned int));
299 for (
unsigned int i = 0; i < rsums.size(); i++)
301 if (blocks[i].size != blksize && (i != nblks - 1 || rsumpad != blksize))
303 unsigned int r = rsums[i];
304 unsigned int h = r & hm;
311 unsigned char *buf =
new unsigned char[blksize];
312 unsigned char *buf2 =
new unsigned char[blksize];
314 unsigned char *pushbackp = 0;
316 if ((blksize & (blksize - 1)) == 0)
317 for (bshift = 0; size_t(1 << bshift) != blksize; bshift++)
321 memset(buf, 0, blksize);
324 int sql = nblks > 1 && chksumlen < 16 ? 2 : 1;
327 for (
size_t i = 0; i < blksize; i++)
353 b += a - (oc << bshift);
355 b += a - oc * blksize;
358 if (
size_t(i) != blksize - 1)
364 r = ((
unsigned int)b & 255);
365 else if (rsumlen == 2)
366 r = ((
unsigned int)b & 65535);
367 else if (rsumlen == 3)
368 r = ((
unsigned int)a & 255) << 16 | ((
unsigned int)b & 65535);
370 r = ((
unsigned int)a & 65535) << 16 | ((
unsigned int)b & 65535);
371 unsigned int h = r & hm;
373 for (; ht[h]; h = (h + hh++) & hm)
375 size_t blkno = ht[h] - 1;
376 if (rsums[blkno] != r)
382 if (eof || blkno + 1 >= nblks)
384 pushback =
fetchnext(fp, buf2, blksize, pushback, pushbackp);
388 if (!checkRsum(blkno + 1, buf2, blksize))
391 if (!checkChecksumRotated(blkno, buf, blksize, i + 1))
393 if (sql == 2 && !checkChecksum(blkno + 1, buf2, blksize))
395 writeBlock(blkno, wfp, buf, blksize, i + 1, found);
398 writeBlock(blkno + 1, wfp, buf2, blksize, 0, found);
405 pushback =
fetchnext(fp, buf2, blksize, pushback, pushbackp);
409 if (!checkRsum(blkno, buf2, blksize))
411 if (!checkChecksum(blkno, buf2, blksize))
413 writeBlock(blkno, wfp, buf2, blksize, 0, found);
417 memset(buf, 0, blksize);
428 else if (chksumlen >= 16)
433 unsigned char *buf =
new unsigned char[bufl];
434 for (
size_t blkno = 0; blkno < blocks.size(); ++blkno)
436 if (off > blocks[blkno].off)
438 size_t blksize = blocks[blkno].size;
443 buf =
new unsigned char[bufl];
445 size_t skip = blocks[blkno].off - off;
448 size_t l = skip > bufl ? bufl : skip;
449 if (fread(buf, l, 1, fp) != 1)
454 if (fread(buf, blksize, 1, fp) != 1)
456 if (checkChecksum(blkno, buf, blksize))
457 writeBlock(blkno, wfp, buf, blksize, 0, found);
464 std::vector<MediaBlock> nblocks;
465 std::vector<unsigned char> nchksums;
466 std::vector<unsigned int> nrsums;
468 for (
size_t blkno = 0; blkno < blocks.size(); ++blkno)
473 nblocks.push_back(blocks[blkno]);
474 if (chksumlen && (blkno + 1) * chksumlen <= chksums.size())
476 nchksums.resize(nblocks.size() * chksumlen);
477 memcpy(&nchksums[(nblocks.size() - 1) * chksumlen], &chksums[blkno * chksumlen], chksumlen);
479 if (rsumlen && (blkno + 1) <= rsums.size())
480 nrsums.push_back(rsums[blkno]);
494 if (filesize != off_t(-1))
496 long long size = filesize;
500 s =
"[ BlockList, filesize unknown\n";
502 s +=
" No block information\n";
507 for (i = 0; i < blocks.size(); ++i)
509 long long off=blocks[i].off;
510 long long size=blocks[i].size;
512 if (chksumlen && chksums.size() >= (i + 1) * chksumlen)
514 s +=
" " + chksumtype +
":";
515 for (j = 0; j < size_t(chksumlen); j++)
518 if (rsumlen && rsums.size() > i)
Compute Message Digests (MD5, SHA1 etc)
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
std::string form(const char *format,...)
Printf style construction of std::string.
std::string asString(const std::string &t)
Global asString() that works with std::string too.
bool update(const char *bytes, size_t len)
feed data into digest computation algorithm