libzypp  17.28.6
MediaBlockList.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_MEDIA_MEDIABLOCKLIST_H
13 #define ZYPP_MEDIA_MEDIABLOCKLIST_H
14 
15 #include <sys/types.h>
16 #include <vector>
17 #include <optional>
18 
19 #include <zypp/Digest.h>
20 
21 namespace zypp {
22  namespace media {
23 
27 struct MediaBlock {
28  MediaBlock( off_t off_r, size_t size_r )
29  : off( off_r )
30  , size( size_r )
31  {}
32  off_t off;
33  size_t size;
34 };
35 
37 public:
38  MediaBlockList(off_t filesize=off_t(-1));
39 
44  inline bool haveBlocks() const {
45  return haveblocks;
46  }
52  size_t addBlock(off_t off, size_t size);
53 
57  inline MediaBlock getBlock(size_t blkno) const {
58  return blocks[blkno];
59  }
63  inline size_t numBlocks() const {
64  return blocks.size();
65  }
66 
70  inline void setFilesize(off_t newfilesize=off_t(-1)) {
71  filesize = newfilesize;
72  }
73  inline off_t getFilesize() const {
74  return filesize;
75  }
76  inline bool haveFilesize() const {
77  return filesize != off_t(-1);
78  }
79 
83  void setFileChecksum(std::string ctype, int cl, unsigned char *c);
84  std::string fileChecksumType () const;
85 
86  const UByteArray &getFileChecksum( );
87  bool createFileDigest(Digest &digest) const;
88  bool verifyFileDigest(Digest &digest) const;
89  inline bool haveFileChecksum() const {
90  return !fsumtype.empty() && fsum.size();
91  }
92 
96  void setChecksum(size_t blkno, std::string cstype, int csl, unsigned char *cs, size_t cspad=0);
97  bool checkChecksum(size_t blkno, const unsigned char *buf, size_t bufl) const;
98  UByteArray getChecksum( size_t blkno ) const;
99  std::string getChecksumType( ) const;
100  bool createDigest(Digest &digest) const;
101  bool verifyDigest(size_t blkno, Digest &digest) const;
102  inline bool haveChecksum(size_t blkno) const {
103  return chksumlen && chksums.size() >= chksumlen * (blkno + 1);
104  }
105 
109  void setRsum(size_t blkno, int rsl, unsigned int rs, size_t rspad=0);
110  bool checkRsum(size_t blkno, const unsigned char *buf, size_t bufl) const;
111  unsigned int updateRsum(unsigned int rs, const char *bytes, size_t len) const;
112  bool verifyRsum(size_t blkno, unsigned int rs) const;
113  inline bool haveRsum(size_t blkno) const {
114  return rsumlen && rsums.size() >= blkno + 1;
115  }
116 
121  void reuseBlocks(FILE *wfp, std::string filename);
122 
126  std::string asString() const;
127 
128 private:
129  void writeBlock(size_t blkno, FILE *fp, const unsigned char *buf, size_t bufl, size_t start, std::vector<bool> &found) const;
130  bool checkChecksumRotated(size_t blkno, const unsigned char *buf, size_t bufl, size_t start) const;
131 
132  off_t filesize;
133  std::string fsumtype;
135 
137  std::vector<MediaBlock> blocks;
138 
139  std::string chksumtype;
141  size_t chksumpad;
142  std::vector<unsigned char> chksums;
143 
144  std::string rsumtype;
145  int rsumlen;
146  size_t rsumpad;
147  std::vector<unsigned int> rsums;
148 };
149 
150 inline std::ostream & operator<<(std::ostream &str, const MediaBlockList &bl)
151 { return str << bl.asString(); }
152 
153  } // namespace media
154 } // namespace zypp
155 
156 #endif // ZYPP_MEDIA_MEDIABLOCKLIST_H
157 
Compute Message Digests (MD5, SHA1 etc)
Definition: Digest.h:36
std::vector< unsigned int > rsums
void setRsum(size_t blkno, int rsl, unsigned int rs, size_t rspad=0)
set / verify the (weak) rolling checksum over a single block
bool haveChecksum(size_t blkno) const
void setFileChecksum(std::string ctype, int cl, unsigned char *c)
set / verify the checksum over the whole file
bool verifyRsum(size_t blkno, unsigned int rs) const
MediaBlock getBlock(size_t blkno) const
return the offset/size of a block with number blkno
void reuseBlocks(FILE *wfp, std::string filename)
scan a file for blocks from our blocklist.
const UByteArray & getFileChecksum()
void writeBlock(size_t blkno, FILE *fp, const unsigned char *buf, size_t bufl, size_t start, std::vector< bool > &found) const
bool createDigest(Digest &digest) const
std::string asString() const
return block list as string
UByteArray getChecksum(size_t blkno) const
bool checkChecksumRotated(size_t blkno, const unsigned char *buf, size_t bufl, size_t start) const
std::vector< unsigned char > chksums
size_t addBlock(off_t off, size_t size)
add a block with offset off and size size to the block list.
unsigned int updateRsum(unsigned int rs, const char *bytes, size_t len) const
void setChecksum(size_t blkno, std::string cstype, int csl, unsigned char *cs, size_t cspad=0)
set / verify the (strong) checksum over a single block
bool verifyDigest(size_t blkno, Digest &digest) const
bool haveBlocks() const
do we have a blocklist describing the file? set to true when addBlock() is called
bool checkRsum(size_t blkno, const unsigned char *buf, size_t bufl) const
std::vector< MediaBlock > blocks
std::string getChecksumType() const
bool createFileDigest(Digest &digest) const
size_t numBlocks() const
return the number of blocks in the blocklist
bool verifyFileDigest(Digest &digest) const
std::string fileChecksumType() const
bool haveRsum(size_t blkno) const
void setFilesize(off_t newfilesize=off_t(-1))
set / return the size of the whole file
bool checkChecksum(size_t blkno, const unsigned char *buf, size_t bufl) const
MediaBlockList(off_t filesize=off_t(-1))
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const MediaBlockList &bl)
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
a single block from the blocklist, consisting of an offset and a size
MediaBlock(off_t off_r, size_t size_r)