16 #include <sys/types.h>
26 using namespace zypp::base;
31 ZsyncParser::ZsyncParser()
39 hexstr2bytes(
unsigned char *buf,
const char *str,
int buflen)
42 for (i = 0; i < buflen; i++)
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)) \
57 buf[i] = (buf[i] << 4) | v;
64 ZsyncParser::parse(
string filename)
68 std::ifstream is(filename.c_str());
71 is.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit);
72 off_t filesize = off_t(-1);
75 is.getline(buf,
sizeof(buf));
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))
88 unsigned char sha1[20];
90 bl.setFileChecksum(
"SHA1", 20, sha1);
93 if (filesize == off_t(-1))
95 if (blksize <= 0 || (blksize & (blksize - 1)) != 0)
97 bl.setFilesize(filesize);
101 if (csl < 3 || csl > 16 || rsl < 1 || rsl > 4 || sql < 1 || sql > 2)
103 size_t nblks = (filesize + blksize - 1) / blksize;
106 size_t size = blksize;
107 for (i = 0; i < nblks; i++)
111 size = filesize % blksize;
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);
129 ZsyncParser::getUrls()
131 std::vector<Url> ret;
133 for (i = 0; i < urls.size(); i++)
134 ret.push_back(
Url(urls[i]));
139 ZsyncParser::getBlockList()