1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaBlockList.cc
13 #include <sys/types.h>
22 #include "zypp/media/MediaBlockList.h"
23 #include "zypp/base/Logger.h"
24 #include "zypp/base/String.h"
27 using namespace zypp::base;
32 MediaBlockList::MediaBlockList(off_t size)
43 MediaBlockList::addBlock(off_t off, size_t size)
46 blocks.push_back(MediaBlock( off, size ));
47 return blocks.size() - 1;
51 MediaBlockList::setFileChecksum(std::string ctype, int cl, unsigned char *c)
57 memcpy(&fsum[0], c, cl);
61 MediaBlockList::createFileDigest(Digest &digest) const
63 return digest.create(fsumtype);
67 MediaBlockList::verifyFileDigest(Digest &digest) const
69 if (!haveFileChecksum())
71 vector<unsigned char>dig = digest.digestVector();
72 if (dig.empty() || dig.size() < fsum.size())
74 return memcmp(&dig[0], &fsum[0], fsum.size()) ? false : true;
78 MediaBlockList::setChecksum(size_t blkno, std::string cstype, int csl, unsigned char *cs, size_t cspad)
90 if (csl != chksumlen || cstype != chksumtype || cspad != chksumpad || blkno != chksums.size() / chksumlen)
92 chksums.resize(chksums.size() + csl);
93 memcpy(&chksums[csl * blkno], cs, csl);
97 MediaBlockList::setRsum(size_t blkno, int rsl, unsigned int rs, size_t rspad)
108 if (rsl != rsumlen || rspad != rsumpad || blkno != rsums.size())
114 MediaBlockList::createDigest(Digest &digest) const
116 return digest.create(chksumtype);
120 MediaBlockList::verifyDigest(size_t blkno, Digest &digest) const
122 if (!haveChecksum(blkno))
124 size_t size = blocks[blkno].size;
127 if (chksumpad > size)
129 char pad[chksumpad - size];
130 memset(pad, 0, chksumpad - size);
131 digest.update(pad, chksumpad - size);
133 vector<unsigned char>dig = digest.digestVector();
134 if (dig.empty() || dig.size() < size_t(chksumlen))
136 return memcmp(&dig[0], &chksums[chksumlen * blkno], chksumlen) ? false : true;
140 MediaBlockList::updateRsum(unsigned int rs, const char* bytes, size_t len) const
145 s = (rs >> 16) & 65535;
147 for (; len > 0 ; len--)
149 unsigned short c = (unsigned char)*bytes++;
153 return (s & 65535) << 16 | (m & 65535);
157 MediaBlockList::verifyRsum(size_t blkno, unsigned int rs) const
159 if (!haveRsum(blkno))
161 size_t size = blocks[blkno].size;
167 s = (rs >> 16) & 65535;
169 m += s * (rsumpad - size);
170 rs = (s & 65535) << 16 | (m & 65535);
183 return rs == rsums[blkno];
187 MediaBlockList::checkRsum(size_t blkno, const unsigned char *buf, size_t bufl) const
189 if (blkno >= blocks.size() || bufl < blocks[blkno].size)
191 unsigned int rs = updateRsum(0, (const char *)buf, blocks[blkno].size);
192 return verifyRsum(blkno, rs);
196 MediaBlockList::checkChecksum(size_t blkno, const unsigned char *buf, size_t bufl) const
198 if (blkno >= blocks.size() || bufl < blocks[blkno].size)
201 if (!createDigest(dig))
203 dig.update((const char *)buf, blocks[blkno].size);
204 return verifyDigest(blkno, dig);
207 // specialized version of checkChecksum that can deal with a "rotated" buffer
209 MediaBlockList::checkChecksumRotated(size_t blkno, const unsigned char *buf, size_t bufl, size_t start) const
211 if (blkno >= blocks.size() || bufl < blocks[blkno].size)
216 if (!createDigest(dig))
218 size_t size = blocks[blkno].size;
219 size_t len = bufl - start > size ? size : bufl - start;
220 dig.update((const char *)buf + start, len);
222 dig.update((const char *)buf, size - len);
223 return verifyDigest(blkno, dig);
226 // write block to the file. can also deal with "rotated" buffers
228 MediaBlockList::writeBlock(size_t blkno, FILE *fp, const unsigned char *buf, size_t bufl, size_t start, vector<bool> &found) const
230 if (blkno >= blocks.size() || bufl < blocks[blkno].size)
232 off_t off = blocks[blkno].off;
233 size_t size = blocks[blkno].size;
234 if (fseeko(fp, off, SEEK_SET))
238 size_t len = bufl - start > size ? size : bufl - start;
239 if (fwrite(buf + start, len, 1, fp) != 1)
241 if (size > len && fwrite(buf, size - len, 1, fp) != 1)
244 found[blocks.size()] = true;
248 fetchnext(FILE *fp, unsigned char *bp, size_t blksize, size_t pushback, unsigned char *pushbackp)
256 memmove(bp, pushbackp, pushback);
275 MediaBlockList::reuseBlocks(FILE *wfp, string filename)
279 if (!chksumlen || (fp = fopen(filename.c_str(), "r")) == 0)
281 size_t nblks = blocks.size();
283 found.resize(nblks + 1);
284 if (rsumlen && !rsums.empty())
286 size_t blksize = blocks[0].size;
287 if (nblks == 1 && rsumpad && rsumpad > blksize)
289 // create hash of checksums
290 unsigned int hm = rsums.size() * 2;
291 while (hm & (hm - 1))
296 unsigned int *ht = new unsigned int[hm + 1];
297 memset(ht, 0, (hm + 1) * sizeof(unsigned int));
298 for (unsigned int i = 0; i < rsums.size(); i++)
300 if (blocks[i].size != blksize && (i != nblks - 1 || rsumpad != blksize))
302 unsigned int r = rsums[i];
303 unsigned int h = r & hm;
310 unsigned char *buf = new unsigned char[blksize];
311 unsigned char *buf2 = new unsigned char[blksize];
313 unsigned char *pushbackp = 0;
315 if ((blksize & (blksize - 1)) == 0)
316 for (bshift = 0; size_t(1 << bshift) != blksize; bshift++)
320 memset(buf, 0, blksize);
323 int sql = nblks > 1 && chksumlen < 16 ? 2 : 1;
326 for (size_t i = 0; i < blksize; i++)
352 b += a - (oc << bshift);
354 b += a - oc * blksize;
357 if (size_t(i) != blksize - 1)
363 r = ((unsigned int)b & 255);
364 else if (rsumlen == 2)
365 r = ((unsigned int)b & 65535);
366 else if (rsumlen == 3)
367 r = ((unsigned int)a & 255) << 16 | ((unsigned int)b & 65535);
369 r = ((unsigned int)a & 65535) << 16 | ((unsigned int)b & 65535);
370 unsigned int h = r & hm;
372 for (; ht[h]; h = (h + hh++) & hm)
374 size_t blkno = ht[h] - 1;
375 if (rsums[blkno] != r)
381 if (eof || blkno + 1 >= nblks)
383 pushback = fetchnext(fp, buf2, blksize, pushback, pushbackp);
387 if (!checkRsum(blkno + 1, buf2, blksize))
390 if (!checkChecksumRotated(blkno, buf, blksize, i + 1))
392 if (sql == 2 && !checkChecksum(blkno + 1, buf2, blksize))
394 writeBlock(blkno, wfp, buf, blksize, i + 1, found);
397 writeBlock(blkno + 1, wfp, buf2, blksize, 0, found);
404 pushback = fetchnext(fp, buf2, blksize, pushback, pushbackp);
408 if (!checkRsum(blkno, buf2, blksize))
410 if (!checkChecksum(blkno, buf2, blksize))
412 writeBlock(blkno, wfp, buf2, blksize, 0, found);
416 memset(buf, 0, blksize);
418 i = size_t(-1); // start with 0 on next iteration
427 else if (chksumlen >= 16)
429 // dummy variant, just check the checksums
432 unsigned char *buf = new unsigned char[bufl];
433 for (size_t blkno = 0; blkno < blocks.size(); ++blkno)
435 if (off > blocks[blkno].off)
437 size_t blksize = blocks[blkno].size;
442 buf = new unsigned char[bufl];
444 size_t skip = blocks[blkno].off - off;
447 size_t l = skip > bufl ? bufl : skip;
448 if (fread(buf, l, 1, fp) != 1)
453 if (fread(buf, blksize, 1, fp) != 1)
455 if (checkChecksum(blkno, buf, blksize))
456 writeBlock(blkno, wfp, buf, blksize, 0, found);
462 // now throw out all of the blocks we found
463 std::vector<MediaBlock> nblocks;
464 std::vector<unsigned char> nchksums;
465 std::vector<unsigned int> nrsums;
467 for (size_t blkno = 0; blkno < blocks.size(); ++blkno)
472 nblocks.push_back(blocks[blkno]);
473 if (chksumlen && (blkno + 1) * chksumlen <= chksums.size())
475 nchksums.resize(nblocks.size() * chksumlen);
476 memcpy(&nchksums[(nblocks.size() - 1) * chksumlen], &chksums[blkno * chksumlen], chksumlen);
478 if (rsumlen && (blkno + 1) <= rsums.size())
479 nrsums.push_back(rsums[blkno]);
488 MediaBlockList::asString() const
493 if (filesize != off_t(-1))
495 long long size = filesize;
496 s = zypp::str::form("[ BlockList, file size %lld\n", size);
499 s = "[ BlockList, filesize unknown\n";
501 s += " No block information\n";
503 s += zypp::str::form(" Checksum pad %zd\n", chksumpad);
505 s += zypp::str::form(" Rsum pad %zd\n", rsumpad);
506 for (i = 0; i < blocks.size(); ++i)
508 long long off=blocks[i].off;
509 long long size=blocks[i].size;
510 s += zypp::str::form(" (%8lld, %8lld)", off, size);
511 if (chksumlen && chksums.size() >= (i + 1) * chksumlen)
513 s += " " + chksumtype + ":";
514 for (j = 0; j < size_t(chksumlen); j++)
515 s += zypp::str::form("%02hhx", chksums[i * chksumlen + j]);
517 if (rsumlen && rsums.size() > i)
520 s += zypp::str::form("%0*x", 2 * rsumlen, rsums[i]);