libzypp  17.23.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  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();
88  }
89 
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);
100  }
101 
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;
111  }
112 
117  void reuseBlocks(FILE *wfp, std::string filename);
118 
122  std::string asString() const;
123 
124 private:
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;
127 
128  off_t filesize;
129  std::string fsumtype;
130  std::vector<unsigned char> fsum;
131 
133  std::vector<MediaBlock> blocks;
134 
135  std::string chksumtype;
137  size_t chksumpad;
138  std::vector<unsigned char> chksums;
139 
140  std::string rsumtype;
141  int rsumlen;
142  size_t rsumpad;
143  std::vector<unsigned int> rsums;
144 };
145 
146 inline std::ostream & operator<<(std::ostream &str, const MediaBlockList &bl)
147 { return str << bl.asString(); }
148 
149  } // namespace media
150 } // namespace zypp
151 
152 #endif // ZYPP_MEDIA_MEDIABLOCKLIST_H
153 
zypp::media::MediaBlockList::haveRsum
bool haveRsum(size_t blkno) const
Definition: MediaBlockList.h:109
zypp::media::MediaBlockList::fsumtype
std::string fsumtype
Definition: MediaBlockList.h:129
zypp::media::MediaBlockList::chksums
std::vector< unsigned char > chksums
Definition: MediaBlockList.h:138
zypp::media::MediaBlockList::getChecksum
std::vector< unsigned char > getChecksum(size_t blkno)
Definition: MediaBlockList.cc:212
zypp::media::MediaBlockList::createDigest
bool createDigest(Digest &digest) const
Definition: MediaBlockList.cc:119
zypp::media::MediaBlockList::MediaBlockList
MediaBlockList(off_t filesize=off_t(-1))
Definition: MediaBlockList.cc:32
zypp::media::MediaBlockList::haveFilesize
bool haveFilesize() const
Definition: MediaBlockList.h:75
zypp::media::MediaBlockList::checkChecksumRotated
bool checkChecksumRotated(size_t blkno, const unsigned char *buf, size_t bufl, size_t start) const
Definition: MediaBlockList.cc:224
zypp::media::MediaBlockList
Definition: MediaBlockList.h:35
zypp::media::MediaBlockList::verifyDigest
bool verifyDigest(size_t blkno, Digest &digest) const
Definition: MediaBlockList.cc:125
zypp::media::MediaBlockList::rsums
std::vector< unsigned int > rsums
Definition: MediaBlockList.h:143
zypp::media::MediaBlockList::numBlocks
size_t numBlocks() const
return the number of blocks in the blocklist
Definition: MediaBlockList.h:62
zypp::media::MediaBlock::off
off_t off
Definition: MediaBlockList.h:31
zypp::media::MediaBlock::MediaBlock
MediaBlock(off_t off_r, size_t size_r)
Definition: MediaBlockList.h:27
zypp::media::MediaBlockList::createFileDigest
bool createFileDigest(Digest &digest) const
Definition: MediaBlockList.cc:66
zypp::media::MediaBlockList::rsumlen
int rsumlen
Definition: MediaBlockList.h:141
zypp::media::MediaBlock
a single block from the blocklist, consisting of an offset and a size
Definition: MediaBlockList.h:26
zypp::media::MediaBlockList::setChecksum
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
Definition: MediaBlockList.cc:83
zypp::Digest
Compute Message Digests (MD5, SHA1 etc)
Definition: Digest.h:45
zypp::media::operator<<
std::ostream & operator<<(std::ostream &str, const MediaAccess &obj)
Definition: MediaAccess.cc:481
zypp::media::MediaBlockList::haveBlocks
bool haveBlocks() const
do we have a blocklist describing the file? set to true when addBlock() is called
Definition: MediaBlockList.h:43
zypp::media::MediaBlockList::blocks
std::vector< MediaBlock > blocks
Definition: MediaBlockList.h:133
zypp::media::MediaBlockList::verifyRsum
bool verifyRsum(size_t blkno, unsigned int rs) const
Definition: MediaBlockList.cc:162
zypp::media::MediaBlockList::setRsum
void setRsum(size_t blkno, int rsl, unsigned int rs, size_t rspad=0)
set / verify the (weak) rolling checksum over a single block
Definition: MediaBlockList.cc:102
zypp::media::MediaBlockList::checkChecksum
bool checkChecksum(size_t blkno, const unsigned char *buf, size_t bufl) const
Definition: MediaBlockList.cc:201
zypp::media::MediaBlockList::setFileChecksum
void setFileChecksum(std::string ctype, int cl, unsigned char *c)
set / verify the checksum over the whole file
Definition: MediaBlockList.cc:51
zypp::media::MediaBlockList::chksumlen
int chksumlen
Definition: MediaBlockList.h:136
zypp::media::MediaBlockList::setFilesize
void setFilesize(off_t newfilesize=off_t(-1))
set / return the size of the whole file
Definition: MediaBlockList.h:69
zypp::media::MediaBlockList::checkRsum
bool checkRsum(size_t blkno, const unsigned char *buf, size_t bufl) const
Definition: MediaBlockList.cc:192
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
zypp::media::MediaBlockList::verifyFileDigest
bool verifyFileDigest(Digest &digest) const
Definition: MediaBlockList.cc:72
zypp::media::MediaBlockList::asString
std::string asString() const
return block list as string
Definition: MediaBlockList.cc:503
zypp::media::MediaBlockList::chksumpad
size_t chksumpad
Definition: MediaBlockList.h:137
zypp::media::MediaBlockList::writeBlock
void writeBlock(size_t blkno, FILE *fp, const unsigned char *buf, size_t bufl, size_t start, std::vector< bool > &found) const
Definition: MediaBlockList.cc:243
zypp::media::MediaBlockList::reuseBlocks
void reuseBlocks(FILE *wfp, std::string filename)
scan a file for blocks from our blocklist.
Definition: MediaBlockList.cc:290
zypp::media::MediaBlockList::filesize
off_t filesize
Definition: MediaBlockList.h:128
zypp::media::MediaBlockList::getFilesize
off_t getFilesize() const
Definition: MediaBlockList.h:72
Digest.h
zypp::media::MediaBlockList::haveChecksum
bool haveChecksum(size_t blkno) const
Definition: MediaBlockList.h:98
zypp::media::MediaBlockList::getBlock
MediaBlock getBlock(size_t blkno) const
return the offset/size of a block with number blkno
Definition: MediaBlockList.h:56
zypp::media::MediaBlockList::addBlock
size_t addBlock(off_t off, size_t size)
add a block with offset off and size size to the block list.
Definition: MediaBlockList.cc:43
zypp::media::MediaBlockList::haveblocks
bool haveblocks
Definition: MediaBlockList.h:132
zypp::media::MediaBlockList::chksumtype
std::string chksumtype
Definition: MediaBlockList.h:135
zypp::media::MediaBlockList::updateRsum
unsigned int updateRsum(unsigned int rs, const char *bytes, size_t len) const
Definition: MediaBlockList.cc:145
zypp::media::MediaBlockList::haveFileChecksum
bool haveFileChecksum() const
Definition: MediaBlockList.h:86
zypp::media::MediaBlock::size
size_t size
Definition: MediaBlockList.h:32
zypp::media::MediaBlockList::rsumpad
size_t rsumpad
Definition: MediaBlockList.h:142
str
String related utilities and Regular expression matching.
zypp::media::MediaBlockList::rsumtype
std::string rsumtype
Definition: MediaBlockList.h:140
zypp::media::MediaBlockList::getFileChecksum
const std::vector< unsigned char > & getFileChecksum()
Definition: MediaBlockList.cc:60
zypp::media::MediaBlockList::fsum
std::vector< unsigned char > fsum
Definition: MediaBlockList.h:130