PatchR - Usage and File Format specifications

The ResidualVM Patch (patchr) is a binary patch engine, useful for changes in games files.
Its mainly used to fix bugs in scripts of games, when the distribution of patched script is
forbidden due to copyright. The patching is all done in ram, at every read() call, so it could
patch even big files.

A patch come in form of a binary file, with .patchr extension, generated by the diffr tool.
In order to make these patches available to ResidualVM, these files should be enclosed in a
lab file, usually residualvm-grim-patch.lab (for game fixes) or datausr.lab (for user contents).
These files can be generated with mklab tool.

Tools usage:
DIFFR:
Synatx: diffr [-m][-n] oldfile newfile patchfile

Diffr compares (oldfile) to (newfile) and writes to (patchfile) a binary patch suitable for
use by patchr or ResidualVM (if enclosed in a lab file, see above).
-m   Mix diff and extra stream (see File format section). 
-n   Doesn't compress ctrl stream (see File format section). 
Both these options increase slightly the size of
patchfile, but they reduce the patching memory usage (about 44kB less each).

If you wants to use the resulting patchfile with ResidualVM, the filename of patchfile must be
oldfile.patchr (with the original file extension, for example sg.lua.patchr)
If you have multiple versions of a file with the same filename (for example from differents versions
of the game), you could provide patches for all of them by calling the others patchfiles
oldfile_1.patchr, oldfile_2.patchr and so on. ResidualVM recognize the correct patch by checking the
md5 sum of the oldfile (see file format section).

Note that diffr uses a lot of memory, according to bsdiff manual.

PATCHR:
Syntax: patchr [-a] oldfile newfile patchfile
Patchr generates (newfile) from (oldfile) and (patchfile) where (patchfile) is a binary patch built by diffr.
-a   Show the contents of the the patch file

PatchR - File format:
It's modeled on bsdiff format (http://www.daemonology.net/bsdiff/), but:
- it has a different signature
- it uses gzip insted of bzip2 in order to avoid to add new dependences to ResidualVM and to
  reduce decompression time and memory usage (at the cost of bigger patches)
- it checks the md5sum of the first 5000 bytes and the size of the file to be patched
- it uses 32 bit offsets instead of 64 bit offsets
- the patching process isn't performed in one step, but at every read() call, with a big save of
  memory if the resulting file is large
- instead of an arithmetic difference between the original data and the diff block, it uses a xor
- the bytes are xored in groups of 4 or 8 bytes (respectively on 32 or 64 bit machines) to improve performances
- optionally (-m flag in diffr) it mixs diff and extra stream in only one stream in order to
  reduce decompression memory usage (about 44kB less, according to http://zlib.net/zlib_tech.html, Memory footprint section)
- the ctrl block could be uncompressed (-n flags in diffr). Useful for small patches, same advanages as above

All values, except the signature, are encoded in little endian

Header (size = 48)
Offset	Size	Var
0		4		Signature = 'PATR'
4		2		VersionMajor = 2
6		2		VersionMinor <= 0
8		4		flags
12		16		md5sum of old file
28		4		lenght of old file
32		4		lenght of new file
36		4		length of gzipped ctrl block (x)
40		4		length of gzipped diff block (y)
44		4		length of gzipped extra block (z) (if zero the extra block is missing or mixed with diff block)

File
0		48		Header
48		x		Gzipped or uncompressed ctrl block
48+x	y		Gzipped diff block
48+x+y	z		Gzipped extra block (it could be missing)
