Tolerate zlib deflation with window size < 32Kb
authorRoberto Tyley <roberto.tyley@guardian.co.uk>
Sun, 7 Aug 2011 18:46:13 +0000 (19:46 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 11 Aug 2011 20:02:47 +0000 (13:02 -0700)
commit7f684a2aff636f44a5063c05cf84c0db0460ae1a
tree3d7b763261b6aeb1e01cbb2b4a8bdc144c9f029a
parente9e0643fe64b613b6596b79b59df8ff10746f1a1
Tolerate zlib deflation with window size < 32Kb

Git currently reports loose objects as 'corrupt' if they've been
deflated using a window size less than 32Kb, because the
experimental_loose_object() function doesn't recognise the header
byte as a zlib header. This patch makes the function tolerant of
all valid window sizes (15-bit to 8-bit) - but doesn't sacrifice
it's accuracy in distingushing the standard loose-object format
from the experimental (now abandoned) format.

On memory constrained systems zlib may use a much smaller window
size - working on Agit, I found that Android uses a 4KB window;
giving a header byte of 0x48, not 0x78. Consequently all loose
objects generated appear 'corrupt', which is why Agit is a read-only
Git client at this time - I don't want my client to generate Git
repos that other clients treat as broken :(

This patch makes Git tolerant of different deflate settings - it
might appear that it changes experimental_loose_object() to the point
where it could incorrectly identify the experimental format as the
standard one, but the two criteria (bitmask & checksum) can only
give a false result for an experimental object where both of the
following are true:

1) object size is exactly 8 bytes when uncompressed (bitmask)
2) [single-byte in-pack git type&size header] * 256
   + [1st byte of the following zlib header] % 31 = 0 (checksum)

As it happens, for all possible combinations of valid object type
(1-4) and window bits (0-7), the only time when the checksum will be
divisible by 31 is for 0x1838 - ie object type *1*, a Commit - which,
due the fields all Commit objects must contain, could never be as
small as 8 bytes in size.

Given this, the combination of the two criteria (bitmask & checksum)
always correctly determines the buffer format, and is more tolerant
than the previous version.

The alternative to this patch is simply removing support for the
experimental format, which I am also totally cool with.

References:

Android uses a 4KB window for deflation:
http://android.git.kernel.org/?p=platform/libcore.git;a=blob;f=luni/src/main/native/java_util_zip_Deflater.cpp;h=c0b2feff196e63a7b85d97cf9ae5bb2583409c28;hb=refs/heads/gingerbread#l53

Code snippet searching for false positives with the zlib checksum:
https://gist.github.com/1118177

Signed-off-by: Roberto Tyley <roberto.tyley@guardian.co.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
19 files changed:
sha1_file.c
t/t1013-loose-object-format.sh [new file with mode: 0755]
t/t1013/objects/14/9cedb5c46929d18e0f118e9fa31927487af3b6 [new file with mode: 0644]
t/t1013/objects/16/56f9233d999f61ef23ef390b9c71d75399f435 [new file with mode: 0644]
t/t1013/objects/1e/72a6b2c4a577ab0338860fa9fe87f761fc9bbd [new file with mode: 0644]
t/t1013/objects/25/7cc5642cb1a054f08cc83f2d943e56fd3ebe99 [new file with mode: 0644]
t/t1013/objects/2e/65efe2a145dda7ee51d1741299f848e5bf752e [new file with mode: 0644]
t/t1013/objects/6b/aee0540ea990d9761a3eb9ab183003a71c3696 [new file with mode: 0644]
t/t1013/objects/70/e6a83d8dcb26fc8bc0cf702e2ddeb6adca18fd [new file with mode: 0644]
t/t1013/objects/76/e7fa9941f4d5f97f64fea65a2cba436bc79cbb [new file with mode: 0644]
t/t1013/objects/78/75c6237d3fcdd0ac2f0decc7d3fa6a50b66c09 [new file with mode: 0644]
t/t1013/objects/7a/37b887a73791d12d26c0d3e39568a8fb0fa6e8 [new file with mode: 0644]
t/t1013/objects/85/df50785d62d3b05ab03d9cbf7e4a0b49449730 [new file with mode: 0644]
t/t1013/objects/8d/4e360d6c70fbd72411991c02a09c442cf7a9fa [new file with mode: 0644]
t/t1013/objects/95/b1625de3ba8b2214d1e0d0591138aea733f64f [new file with mode: 0644]
t/t1013/objects/9a/e9e86b7bd6cb1472d9373702d8249973da0832 [new file with mode: 0644]
t/t1013/objects/bd/15045f6ce8ff75747562173640456a394412c8 [new file with mode: 0644]
t/t1013/objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 [new file with mode: 0644]
t/t1013/objects/f8/16d5255855ac160652ee5253b06cd8ee14165a [new file with mode: 0644]