libzypp  17.14.0
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 
18 #include "zypp/Digest.h"
19 
20 namespace zypp {
21  namespace media {
22 
26 struct MediaBlock {
27  MediaBlock( off_t off_r, size_t size_r )
28  : off( off_r )
29  , size( size_r )
30  {}
31  off_t off;
32  size_t size;
33 };
34 
36 public:
37  MediaBlockList(off_t filesize=off_t(-1));
38 
43  inline bool haveBlocks() const {
44  return haveblocks;
45  }
51  size_t addBlock(off_t off, size_t size);
52 
56  inline MediaBlock getBlock(size_t blkno) const {
57  return blocks[blkno];
58  }
62  inline size_t numBlocks() const {
63  return blocks.size();
64  }
65 
69  inline void setFilesize(off_t newfilesize=off_t(-1)) {
70  filesize = newfilesize;
71  }
72  inline off_t getFilesize() const {
73  return filesize;
74  }
75  inline bool haveFilesize() const {
76  return filesize != off_t(-1);
77  }
78 
82  void setFileChecksum(std::string ctype, int cl, unsigned char *c);
83  bool createFileDigest(Digest &digest) const;
84  bool verifyFileDigest(Digest &digest) const;
85  inline bool haveFileChecksum() const {
86  return !fsumtype.empty() && fsum.size();
87  }
88 
92  void setChecksum(size_t blkno, std::string cstype, int csl, unsigned char *cs, size_t cspad=0);
93  bool checkChecksum(size_t blkno, const unsigned char *buf, size_t bufl) const;
94  bool createDigest(Digest &digest) const;
95  bool verifyDigest(size_t blkno, Digest &digest) const;
96  inline bool haveChecksum(size_t blkno) const {
97  return chksumlen && chksums.size() >= chksumlen * (blkno + 1);
98  }
99 
103  void setRsum(size_t blkno, int rsl, unsigned int rs, size_t rspad=0);
104  bool checkRsum(size_t blkno, const unsigned char *buf, size_t bufl) const;
105  unsigned int updateRsum(unsigned int rs, const char *bytes, size_t len) const;
106  bool verifyRsum(size_t blkno, unsigned int rs) const;
107  inline bool haveRsum(size_t blkno) const {
108  return rsumlen && rsums.size() >= blkno + 1;
109  }
110 
115  void reuseBlocks(FILE *wfp, std::string filename);
116 
120  std::string asString() const;
121 
122 private:
123  void writeBlock(size_t blkno, FILE *fp, const unsigned char *buf, size_t bufl, size_t start, std::vector<bool> &found) const;
124  bool checkChecksumRotated(size_t blkno, const unsigned char *buf, size_t bufl, size_t start) const;
125 
126  off_t filesize;
127  std::string fsumtype;
128  std::vector<unsigned char> fsum;
129 
131  std::vector<MediaBlock> blocks;
132 
133  std::string chksumtype;
135  size_t chksumpad;
136  std::vector<unsigned char> chksums;
137 
138  std::string rsumtype;
139  int rsumlen;
140  size_t rsumpad;
141  std::vector<unsigned int> rsums;
142 };
143 
144 inline std::ostream & operator<<(std::ostream &str, const MediaBlockList &bl)
145 { return str << bl.asString(); }
146 
147  } // namespace media
148 } // namespace zypp
149 
150 #endif // ZYPP_MEDIA_MEDIABLOCKLIST_H
151 
size_t addBlock(off_t off, size_t size)
add a block with offset off and size size to the block list.
void writeBlock(size_t blkno, FILE *fp, const unsigned char *buf, size_t bufl, size_t start, std::vector< bool > &found) const
void setFilesize(off_t newfilesize=off_t(-1))
set / return the size of the whole file
MediaBlock(off_t off_r, size_t size_r)
std::vector< MediaBlock > blocks
Compute Message Digests (MD5, SHA1 etc)
Definition: Digest.h:45
void reuseBlocks(FILE *wfp, std::string filename)
scan a file for blocks from our blocklist.
std::vector< unsigned char > fsum
bool createDigest(Digest &digest) const
String related utilities and Regular expression matching.
std::vector< unsigned char > chksums
std::vector< unsigned int > rsums
bool haveRsum(size_t blkno) const
std::ostream & operator<<(std::ostream &str, const MediaAccess &obj)
Definition: MediaAccess.cc:482
bool haveBlocks() const
do we have a blocklist describing the file? set to true when addBlock() is called
bool verifyFileDigest(Digest &digest) const
MediaBlockList(off_t filesize=off_t(-1))
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 verifyDigest(size_t blkno, Digest &digest) const
bool checkChecksumRotated(size_t blkno, const unsigned char *buf, size_t bufl, size_t start) const
a single block from the blocklist, consisting of an offset and a size
bool createFileDigest(Digest &digest) const
size_t numBlocks() const
return the number of blocks in the blocklist
unsigned int updateRsum(unsigned int rs, const char *bytes, size_t len) const
bool haveChecksum(size_t blkno) const
std::string asString() const
return block list as string
bool verifyRsum(size_t blkno, unsigned int rs) const
bool checkChecksum(size_t blkno, const unsigned char *buf, size_t bufl) const
void setFileChecksum(std::string ctype, int cl, unsigned char *c)
set / verify the checksum over the whole file
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
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 checkRsum(size_t blkno, const unsigned char *buf, size_t bufl) const
MediaBlock getBlock(size_t blkno) const
return the offset/size of a block with number blkno