Add test suite, fix about five IPS bugs found

This commit is contained in:
Alcaro
2016-12-23 13:13:21 +01:00
parent 81a35ee437
commit 3f3bf6c3a5
10 changed files with 381 additions and 27 deletions

View File

@@ -1,3 +1,4 @@
#pragma once
#include "../arlib.h"
namespace patch {
@@ -27,17 +28,24 @@ enum result {
namespace ips {
result apply(const file& patch, const file& source, file& target);
static inline result apply(const file& patch, const file& source, file&& target) { return apply(patch, source, (file&)target); }
result create(const file& source, const file& target, file& patch);
static inline result create(const file& source, const file& target, file&& patch) { return create(source, target, (file&)patch); }
}
namespace ups {
result apply(const file& patch, const file& source, file& target);
static inline result apply(const file& patch, const file& source, file&& target) { return apply(patch, source, (file&)target); }
//ups is worthless
//result create(const file& source, const file& target, file& patch);
}
namespace bps {
result apply(const file& patch, const file& source, file& target, bool accept_wrong_input);
result apply(const file& patch, const file& source, file& target, bool accept_wrong_input = false);
static inline result apply(const file& patch, const file& source, file&& target, bool accept_wrong_input = false)
{
return apply(patch, source, (file&)target, accept_wrong_input);
}
//Because this one can take quite a long time, a progress meter is supplied. total is guaranteed to
// be constant between every call until this function returns, done is guaranteed to increase
// between each call, and done/total is an approximate percentage counter. Anything else is
@@ -47,6 +55,21 @@ result apply(const file& patch, const file& source, file& target, bool accept_wr
//To cancel patch creation, return true from the callback. It's safe to pass in NULL if you're not interested.
result create(const file& source, const file& target, const file& metadata, file& patch,
function<bool(size_t done, size_t total)> progress);
static inline result create(const file& source, const file& target, const file& metadata, file&& patch,
function<bool(size_t done, size_t total)> progress)
{
return create(source, target, metadata, (file&)patch, progress);
}
static inline result create(const file& source, const file& target, file& patch,
function<bool(size_t done, size_t total)> progress)
{
return create(source, target, file::mem(NULL), (file&)patch, progress);
}
static inline result create(const file& source, const file& target, file&& patch,
function<bool(size_t done, size_t total)> progress)
{
return create(source, target, (file&)patch, progress);
}
struct info {
result parse(const file& patch, bool changefrac = false);
@@ -71,6 +94,105 @@ struct info {
};
}
//Used for patch application.
class filebufreader {
file& f;
size_t fpos;
array<byte> buf;
size_t bufpos;
uint32_t crc;
public:
filebufreader(file& f) : f(f), fpos(0), bufpos(0), crc(0) {}
arrayview<byte> peek(size_t bytes)
{
if (buf.size()-bufpos < bytes)
{
buf = buf.slice(bufpos, buf.size()-bufpos);
bufpos = 0;
size_t bytehave = buf.size();
size_t byteread = bytes + 4096;
buf.resize(bytehave + byteread);
byteread = f.read(buf.slice(bytehave, byteread), fpos);
fpos += byteread;
buf.resize(bytehave + byteread);
}
return buf.slice(bufpos, min(buf.size()-bufpos, bytes));
}
arrayview<byte> read(size_t bytes)
{
arrayview<byte> ret = peek(bytes);
if (ret.size() != bytes) return NULL;
bufpos += bytes;
crc = crc32_update(ret, crc); // TODO: perhaps it's faster if this one is calculated in large batches
return ret;
}
byte read() { return read(1)[0]; }
size_t remaining() { return buf.size()-bufpos + f.size()-fpos; }
uint32_t crc32() { return crc; }
};
class streamreader {
filebufreader f;
public:
streamreader(file& f) : f(f) {}
arrayview<byte> bytes(size_t n) { return f.read(n); }
uint8_t u8()
{
return f.read(1)[0];
}
uint16_t u16()
{
arrayview<byte> b = f.read(2);
return b[0] | b[1]<<8;
}
uint32_t u24()
{
arrayview<byte> b = f.read(3);
return b[0] | b[1]<<8 | b[2]<<16;
}
uint32_t u32()
{
arrayview<byte> b = f.read(4);
return b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24;
}
// size_t bpsnum() // close to uleb128, but uleb lacks the +1 that ensures there's only one way to encode an integer
// {
// size_t ret = 0;
// size_t shift = 0;
// while (true)
// {
// uint8_t next = f.read();
// if (SIZE_MAX>>shift < (next&0x7F)) return (size_t)-1;
// size_t shifted = (next&0x7F)<<shift;
//
//#define assert_sum(a,b) do { if (SIZE_MAX-(a)<(b)) error(e_too_big); } while(0)
//#define assert_shift(a,b) do { if (SIZE_MAX>>(b)<(a)) error(e_too_big); } while(0)
//
// }
//#define decodeto(var) \
// do { \
// var=0; \
// unsigned int shift=0; \
// while (true) \
// { \
// uint8_t next=readpatch8(); \
// assert_shift(next&0x7F, shift); \
// size_t addthis=(next&0x7F)<<shift; \
// assert_sum(var, addthis); \
// var+=addthis; \
// if (next&0x80) break; \
// shift+=7; \
// assert_sum(var, 1U<<shift); \
// var+=1<<shift; \
// } \
// } while(false)
//
// arrayview<byte> b = f.peek(16);
// }
};
//Deprecated
struct mem {
mem() : ptr(NULL), len(0) {}