Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

constify return value of 'read' callback.

Make sure we don't modify data supplied by user.

Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
  • Loading branch information...
commit 3729c8e354d0d4e11e69521c9cd78d6679a77309 1 parent c739523
@trofi trofi authored
View
7 src/bdelta.cpp
@@ -21,14 +21,14 @@
#include "file.h"
#include "compatibility.h"
-void *f_read(void *f, void *buf, unsigned place, unsigned num) {
+const void *f_read(void *f, void *buf, unsigned place, unsigned num) {
fseek((FILE *)f, place, SEEK_SET);
fread_fixed((FILE *)f, buf, num);
return buf;
}
-void *m_read(void *f, void *buf, unsigned place, unsigned num) {
- memcpy (buf, (char*)f + place, num);
+const void *m_read(void *f, void * buf, unsigned place, unsigned num) {
+ memcpy (buf, (const char*)f + place, num);
return buf;
}
@@ -92,7 +92,6 @@ int main(int argc, char **argv) {
// 141-160 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941
// 161-180 947 953 967 971 977 983 991 997
- int seq[] = {503, 127, 31, 7, 5, 3, -31, 31, 7, 5, 3, -7, 2};
my_pass(b, 997, 1994, 0);
my_pass(b, 503, 1006, 0);
my_pass(b, 127, 254, 0);
View
2  src/bdelta.h
@@ -22,7 +22,7 @@ typedef struct _BDelta_Instance BDelta_Instance;
// Callback function must return a pointer to the data requested.
// A "fill and forget" buffer is provided, but can be ignored, so
// long as the data persists throughout the life of bdelta_pass().
-typedef void *(*bdelta_readCallback)(void *handle, void *buf, unsigned place, unsigned num);
+typedef const void *(*bdelta_readCallback)(void *handle, void *buf, unsigned place, unsigned num);
BDelta_Instance *bdelta_init_alg(unsigned data1_size, unsigned data2_size,
bdelta_readCallback cb, void *handle1, void *handle2,
View
2  src/checksum.h
@@ -18,7 +18,7 @@ struct Hash {
public:
typedef uint64_t Value;
Hash() {}
- Hash(Token *buf, unsigned blocksize) {
+ Hash(const Token *buf, unsigned blocksize) {
value = 0;
for (unsigned num = 0; num < blocksize; ++num)
advance_add(buf[num]);
View
30 src/libbdelta.cpp
@@ -58,10 +58,10 @@ struct _BDelta_Instance {
int access_int;
int errorcode;
- Token *read1(void *buf, unsigned place, unsigned num)
- {return (Token*)cb(handle1, buf, place, num);}
- Token *read2(void *buf, unsigned place, unsigned num)
- {return (Token*)cb(handle2, buf, place, num);}
+ const Token *read1(void *buf, unsigned place, unsigned num)
+ {return (const Token*)cb(handle1, buf, place, num);}
+ const Token *read2(void *buf, unsigned place, unsigned num)
+ {return (const Token*)cb(handle2, buf, place, num);}
};
struct Checksums_Instance {
@@ -82,15 +82,15 @@ struct Checksums_Instance {
};
-unsigned match_buf_forward(void *buf1, void *buf2, unsigned num) {
+unsigned match_buf_forward(const void *buf1, const void *buf2, unsigned num) {
unsigned i = 0;
- while (i < num && ((Token*)buf1)[i]==((Token*)buf2)[i]) ++i;
+ while (i < num && ((const Token*)buf1)[i]==((const Token*)buf2)[i]) ++i;
return i;
}
-unsigned match_buf_backward(void *buf1, void *buf2, unsigned num) {
+unsigned match_buf_backward(const void *buf1, const void *buf2, unsigned num) {
int i = num;
do --i;
- while (i >= 0 && ((Token*)buf1)[i] == ((Token*)buf2)[i]);
+ while (i >= 0 && ((const Token*)buf1)[i] == ((const Token*)buf2)[i]);
return num - i - 1;
}
unsigned match_forward(BDelta_Instance *b, unsigned p1, unsigned p2) {
@@ -99,8 +99,8 @@ unsigned match_forward(BDelta_Instance *b, unsigned p1, unsigned p2) {
numtoread = std::min(b->data1_size - p1, b->data2_size - p2);
if (numtoread > 4096) numtoread = 4096;
Token buf1[4096], buf2[4096];
- Token *read1 = b->read1(buf1, p1, numtoread),
- *read2 = b->read2(buf2, p2, numtoread);
+ const Token *read1 = b->read1(buf1, p1, numtoread),
+ *read2 = b->read2(buf2, p2, numtoread);
p1 += numtoread; p2 += numtoread;
match = match_buf_forward(read1, read2, numtoread);
num += match;
@@ -115,8 +115,8 @@ unsigned match_backward(BDelta_Instance *b, unsigned p1, unsigned p2, unsigned b
if (numtoread > blocksize) numtoread = blocksize;
p1 -= numtoread; p2 -= numtoread;
Token buf1[4096], buf2[4096];
- Token *read1 = b->read1(buf1, p1, numtoread),
- *read2 = b->read2(buf2, p2, numtoread);
+ const Token *read1 = b->read1(buf1, p1, numtoread),
+ *read2 = b->read2(buf2, p2, numtoread);
match = match_buf_backward(read1, read2, numtoread);
num += match;
} while (match && match == numtoread);
@@ -149,8 +149,8 @@ void findMatches(BDelta_Instance *b, Checksums_Instance *h, unsigned minMatchSiz
unsigned best1, best2, bestnum = 0;
unsigned processMatchesPos;
- Token *inbuf = b->read2(buf1, start, blocksize),
- *outbuf;
+ const Token *inbuf = b->read2(buf1, start, blocksize),
+ *outbuf;
Hash hash = Hash(inbuf, blocksize);
unsigned buf_loc = blocksize;
for (unsigned j = start + blocksize; j <= end; ++j) {
@@ -307,7 +307,7 @@ void bdelta_pass_2(BDelta_Instance *b, unsigned blocksize, unsigned minMatchSize
for (unsigned i = 0; i < numunused; ++i) {
unsigned first = unused[i].p, last = unused[i].p + unused[i].num;
for (unsigned loc = first; loc + blocksize <= last; loc += blocksize) {
- Token *read = b->read1(buf, loc, blocksize);
+ const Token *read = b->read1(buf, loc, blocksize);
Hash::Value blocksum = Hash(read, blocksize).getValue();
// Adjacent checksums are never repeated.
//if (! h.numchecksums || blocksum != h.checksums[h.numchecksums - 1].cksum)
Please sign in to comment.
Something went wrong with that request. Please try again.