1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/media/MediaBlockList.h
12 #ifndef ZYPP_MEDIA_MEDIABLOCKLIST_H
13 #define ZYPP_MEDIA_MEDIABLOCKLIST_H
15 #include <sys/types.h>
18 #include "zypp/Digest.h"
24 * a single block from the blocklist, consisting of an offset and a size
27 MediaBlock( off_t off_r, size_t size_r )
35 class MediaBlockList {
37 MediaBlockList(off_t filesize=off_t(-1));
40 * do we have a blocklist describing the file?
41 * set to true when addBlock() is called
43 inline bool haveBlocks() const {
47 * add a block with offset off and size size to the block list. Note
48 * that blocks must be ordered and must not overlap. returns the
51 size_t addBlock(off_t off, size_t size);
54 * return the offset/size of a block with number blkno
56 inline MediaBlock getBlock(size_t blkno) const {
60 * return the number of blocks in the blocklist
62 inline size_t numBlocks() const {
67 * set / return the size of the whole file
69 inline void setFilesize(off_t newfilesize=off_t(-1)) {
70 filesize = newfilesize;
72 inline off_t getFilesize() const {
75 inline bool haveFilesize() const {
76 return filesize != off_t(-1);
80 * set / verify the checksum over the whole file
82 void setFileChecksum(std::string ctype, int cl, unsigned char *c);
83 const std::vector<unsigned char> &getFileChecksum( );
84 bool createFileDigest(Digest &digest) const;
85 bool verifyFileDigest(Digest &digest) const;
86 inline bool haveFileChecksum() const {
87 return !fsumtype.empty() && fsum.size();
91 * set / verify the (strong) checksum over a single block
93 void setChecksum(size_t blkno, std::string cstype, int csl, unsigned char *cs, size_t cspad=0);
94 bool checkChecksum(size_t blkno, const unsigned char *buf, size_t bufl) const;
95 std::vector<unsigned char> getChecksum( size_t blkno );
96 bool createDigest(Digest &digest) const;
97 bool verifyDigest(size_t blkno, Digest &digest) const;
98 inline bool haveChecksum(size_t blkno) const {
99 return chksumlen && chksums.size() >= chksumlen * (blkno + 1);
103 * set / verify the (weak) rolling checksum over a single block
105 void setRsum(size_t blkno, int rsl, unsigned int rs, size_t rspad=0);
106 bool checkRsum(size_t blkno, const unsigned char *buf, size_t bufl) const;
107 unsigned int updateRsum(unsigned int rs, const char *bytes, size_t len) const;
108 bool verifyRsum(size_t blkno, unsigned int rs) const;
109 inline bool haveRsum(size_t blkno) const {
110 return rsumlen && rsums.size() >= blkno + 1;
114 * scan a file for blocks from our blocklist. if we find a suitable block,
115 * it is removed from the list
117 void reuseBlocks(FILE *wfp, std::string filename);
120 * return block list as string
122 std::string asString() const;
125 void writeBlock(size_t blkno, FILE *fp, const unsigned char *buf, size_t bufl, size_t start, std::vector<bool> &found) const;
126 bool checkChecksumRotated(size_t blkno, const unsigned char *buf, size_t bufl, size_t start) const;
129 std::string fsumtype;
130 std::vector<unsigned char> fsum;
133 std::vector<MediaBlock> blocks;
135 std::string chksumtype;
138 std::vector<unsigned char> chksums;
140 std::string rsumtype;
143 std::vector<unsigned int> rsums;
146 inline std::ostream & operator<<(std::ostream &str, const MediaBlockList &bl)
147 { return str << bl.asString(); }
152 #endif // ZYPP_MEDIA_MEDIABLOCKLIST_H