libzypp: zypp/media/ZsyncParser.cc Source File

libzypp 17.25.10

ZsyncParser.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #include <zypp/media/ZsyncParser.h>
14 #include <zypp/base/Logger.h>
15
16 #include <sys/types.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <vector>
22 #include <iostream>
23 #include <fstream>
24
25 using std::endl;
26 using namespace zypp::base;
27
28 namespace zypp {
29 namespace media {
30
31 ZsyncParser::ZsyncParser()
32 {
33 filesize = off_t(-1);
34 blksize = 0;
35 sql = rsl = csl = 0;
36 }
37
38 static int
39 hexstr2bytes(unsigned char *buf, const char *str, int buflen)
40 {
41 int i;
42 for (i = 0; i < buflen; i++)
43 {
44 #define c2h(c) (((c)>='0' && (c)<='9') ? ((c)-'0') \
45 : ((c)>='a' && (c)<='f') ? ((c)-('a'-10)) \
46 : ((c)>='A' && (c)<='F') ? ((c)-('A'-10)) \
47 : -1)
48 int v = c2h(*str);
49 str++;
50 if (v < 0)
51 return 0;
52 buf[i] = v;
53 v = c2h(*str);
54 str++;
55 if (v < 0)
56 return 0;
57 buf[i] = (buf[i] << 4) | v;
58 #undef c2h
59 }
60 return buflen;
61 }
62
63 void
64 ZsyncParser::parse(std::string filename)
65 {
66 char buf[4096];
67
68 std::ifstream is(filename.c_str());
69 if (!is)
70 ZYPP_THROW(Exception("ZsyncParser: no such file"));
71 is.exceptions(std::ifstream::eofbit | std::ifstream::failbit | std::ifstream::badbit);
72 off_t filesize = off_t(-1);
73 while (is.good())
74 {
75 is.getline(buf, sizeof(buf));
76 if (!*buf)
77 break;
78 if (!strncmp(buf, "Length: ", 8))
79 filesize = (off_t)strtoull(buf + 8, 0, 10);
80 else if (!strncmp(buf, "Hash-Lengths: ", 14))
81 (void)sscanf(buf + 14, "%d,%d,%d", &sql, &rsl, &csl);
82 else if (!strncmp(buf, "Blocksize: ", 11))
83 blksize = atoi(buf + 11);
84 else if (!strncmp(buf, "URL: http://", 12) || !strncmp(buf, "URL: https://", 13) || !strncmp(buf, "URL: ftp://", 11) || !strncmp(buf, "URL: tftp://", 12) )
85 urls.push_back(buf + 5);
86 else if (!strncmp(buf, "SHA-1: ", 7))
87 {
88 unsigned char sha1[20];
89 if (hexstr2bytes(sha1, buf + 7, 20) == 20)
90 bl.setFileChecksum("SHA1", 20, sha1);
91 }
92 }
93 if (filesize == off_t(-1))
94 ZYPP_THROW(Exception("Parse Error"));
95 if (blksize <= 0 || (blksize & (blksize - 1)) != 0)
96 ZYPP_THROW(Exception("Parse Error: illegal block size"));
97 bl.setFilesize(filesize);
98
99 if (filesize)
100 {
101 if (csl < 3 || csl > 16 || rsl < 1 || rsl > 4 || sql < 1 || sql > 2)
102 ZYPP_THROW(Exception("Parse Error: illegal hash lengths"));
103 size_t nblks = (filesize + blksize - 1) / blksize;
104 size_t i;
105 off_t off = 0;
106 size_t size = blksize;
107 for (i = 0; i < nblks; i++)
108 {
109 if (i == nblks - 1)
110 {
111 size = filesize % blksize;
112 if (!size)
113 size = blksize;
114 }
115 size_t blkno = bl.addBlock(off, size);
116 unsigned char rp[16];
117 rp[0] = rp[1] = rp[2] = rp[3] = 0;
118 is.read((char *)rp + 4 - rsl, rsl);
119 bl.setRsum(blkno, rsl, rp[0] << 24 | rp[1] << 16 | rp[2] << 8 | rp[3], blksize);
120 is.read((char *)rp, csl);
121 bl.setChecksum(blkno, "MD4", csl, rp, blksize);
122 off += size;
123 }
124 }
125 is.close();
126 }
127
128 std::vector<Url>
129 ZsyncParser::getUrls()
130 {
131 std::vector<Url> ret;
132 size_t i;
133 for (i = 0; i < urls.size(); i++)
134 ret.push_back(Url(urls[i]));
135 return ret;
136 }
137
138 MediaBlockList
139 ZsyncParser::getBlockList()
140 {
141 return bl;
142 }
143
144 } // namespace media
145 } // namespace zypp
146
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:392
Logger.h
c2h
#define c2h(c)
ZsyncParser.h
zypp::Exception
Base class for Exception.
Definition: Exception.h:146
zypp::Url
Url manipulation class.
Definition: Url.h:92
zypp::media::MediaBlockList
Definition: MediaBlockList.h:35
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:101
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:50
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:42
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:82
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::ZsyncParser::blksize
size_t blksize
Definition: ZsyncParser.h:45
zypp::media::ZsyncParser::getBlockList
MediaBlockList getBlockList()
return the block list from the parsed metalink data
Definition: ZsyncParser.cc:139
zypp::media::ZsyncParser::filesize
off_t filesize
Definition: ZsyncParser.h:44
zypp::media::ZsyncParser::urls
std::vector< std::string > urls
Definition: ZsyncParser.h:50
zypp::media::ZsyncParser::parse
void parse(std::string filename)
parse a file consisting of zlink data
Definition: ZsyncParser.cc:64
zypp::media::ZsyncParser::sql
int sql
Definition: ZsyncParser.h:46
zypp::media::ZsyncParser::ZsyncParser
ZsyncParser()
Definition: ZsyncParser.cc:31
zypp::media::ZsyncParser::bl
MediaBlockList bl
Definition: ZsyncParser.h:49
zypp::media::ZsyncParser::csl
int csl
Definition: ZsyncParser.h:48
zypp::media::ZsyncParser::getUrls
std::vector< Url > getUrls()
return the download urls from the parsed metalink data
Definition: ZsyncParser.cc:129
zypp::media::ZsyncParser::rsl
int rsl
Definition: ZsyncParser.h:47
str
String related utilities and Regular expression matching.
zypp::base
Definition: DrunkenBishop.cc:25
zypp::media::hexstr2bytes
static int hexstr2bytes(unsigned char *buf, const char *str, int buflen)
Definition: MetaLinkParser.cc:304
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2