Skip to contentSkip to conversation

Fix cryptoapi deflate not handling PAGE_SIZE chunks.

PATCH In review Nigel Cunningham 2 participants 4 messages Jul 18, 2005 – Jul 31, 2005 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix cryptoapi deflate not handling PAGE_SIZE chunks.

Hi Herbert.

Here's a resend of a patch I'm using in Suspend2's new cryptoapi support, which is needed for us to successfully compress pages using deflate. It's along the lines of the existing fix in the decompression code.

Regards,

Nigel

crypto/deflate.c +9 -2
diff -ruNp 190-cryptoapi-deflate.patch-old/crypto/deflate.c 190-cryptoapi-deflate.patch-new/crypto/deflate.c
@@ -143,8 +143,15 @@ static int deflate_compress(void *ctx, c
 
 	ret = zlib_deflate(stream, Z_FINISH);
 	if (ret != Z_STREAM_END) {
-		ret = -EINVAL;
-		goto out;
+	    	if (!(ret == Z_OK && !stream->avail_in && !stream->avail_out)) {
+			ret = -EINVAL;
+			goto out;
+		} else {
+			u8 zerostuff = 0;
+			stream->next_out = &zerostuff;
+			stream->avail_out = 1; 
+			ret = zlib_deflate(stream, Z_FINISH);
+		}
 	}
 	ret = 0;
 	*dlen = stream->total_out;
Signature · 5 lines
Evolution.
Enumerate the requirements.
Consider the interdependencies.
Calculate the probabilities.
Be amazed that people believe it happened. 
maintainer
#1 lore raw
Quoted 17 lines · Nigel Cunningham

Here's a resend of a patch I'm using in Suspend2's new cryptoapi support, which is needed for us to successfully compress pages using deflate. It's along the lines of the existing fix in the decompression code.

diff -ruNp 190-cryptoapi-deflate.patch-old/crypto/deflate.c 190-cryptoapi-deflate.patch-new/crypto/deflate.c

crypto/deflate.c +2 -2
@@ -143,8 +143,15 @@ static int deflate_compress(void *ctx, c
 
 	ret = zlib_deflate(stream, Z_FINISH);
 	if (ret != Z_STREAM_END) {
-		ret = -EINVAL;
-		goto out;
+	    	if (!(ret == Z_OK && !stream->avail_in && !stream->avail_out)) {
+			ret = -EINVAL;

crypto/deflate.c static int deflate_compress(void *ctx, c · line 157

Quoted 10 lines
crypto/deflate.c +7 -0
+			goto out;
+		} else {
+			u8 zerostuff = 0;
+			stream->next_out = &zerostuff;
+			stream->avail_out = 1; 
+			ret = zlib_deflate(stream, Z_FINISH);
+		}
 	}
 	ret = 0;
 	*dlen = stream->total_out;

Hi Nigel, I need a bit more information about this patch. Do you have a specific input stream that requires a fix like this?

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#2 lore raw

Hi.

Quoted 24 lines · Herbert Xu
Quoted 23 lines · Nigel Cunningham

Here's a resend of a patch I'm using in Suspend2's new cryptoapi support, which is needed for us to successfully compress pages using deflate. It's along the lines of the existing fix in the decompression code.

diff -ruNp 190-cryptoapi-deflate.patch-old/crypto/deflate.c 190-cryptoapi-deflate.patch-new/crypto/deflate.c

crypto/deflate.c +8 -2
@@ -143,8 +143,15 @@ static int deflate_compress(void *ctx, c
 
 	ret = zlib_deflate(stream, Z_FINISH);
 	if (ret != Z_STREAM_END) {
-		ret = -EINVAL;
-		goto out;
+	    	if (!(ret == Z_OK && !stream->avail_in && !stream->avail_out)) {
+			ret = -EINVAL;
+			goto out;
+		} else {
+			u8 zerostuff = 0;
+			stream->next_out = &zerostuff;
+			stream->avail_out = 1; 
+			ret = zlib_deflate(stream, Z_FINISH);
Quoted 8 lines

crypto/deflate.c static int deflate_compress(void *ctx, c · line 157

Quoted 4 lines
crypto/deflate.c +1 -0
+		}
 	}
 	ret = 0;
 	*dlen = stream->total_out;

Hi Nigel, I need a bit more information about this patch. Do you have a specific input stream that requires a fix like this?

Yes, Suspend2 if the user selects deflate as their compressor. The output data will be PAGE_SIZE chunks, but deflate sometimes thinks it has an extra byte to give us back.

I agree that it's ugly and don't recall using it when I had gzip support in an earlier version of Suspend2. Are you thinking there might be a better way? If so, I can dig out the old (non crypto api) code.

Regards,

Nigel

Signature · 4 lines
Evolution.
Enumerate the requirements.
Consider the interdependencies.
Calculate the probabilities.
maintainer
#3 lore raw
Quoted 4 lines · Nigel Cunningham

Yes, Suspend2 if the user selects deflate as their compressor. The output data will be PAGE_SIZE chunks, but deflate sometimes thinks it has an extra byte to give us back.

Are you saying that you're feeding it PAGE_SIZE chunks and it's giving you back something bigger than a page? That is expected since the nature of compression in general is that not everything is compressible. Data streams which are not compressible will end up bigger than what you start with.

What would be a bug is if you feed it something that's deflateBound^-1(PAGE_SIZE) bytes long and it spits back something that's longer than a page.

Quoted 3 lines

I agree that it's ugly and don't recall using it when I had gzip support in an earlier version of Suspend2. Are you thinking there might be a better way? If so, I can dig out the old (non crypto api) code.

Well if you could give me a snippet of code that actually uses this stuff to compress pages then I might have a better idea of what it's trying to do.

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt