views:

176

answers:

2

Can zlib-compressed string contain whitespace? By whitespace I mean ' ', \n, \t.

+4  A: 

Yes; it's just a stream of bytes. Any byte value can appear in there (including zero, which is more likely to cause you problems than whitespace characters!)

RichieHindle
+5  A: 

Any byte can appear in a zlib-compresed string.

In fact, for a long enough properly compressed string, any byte (from 0 to 255) should have a more-or-less equal probability, or else the string could be further compressed.

You can try this yourself -- for example using Python:

>>> z = open('/dev/urandom').read(1000000).encode('zlib') # compress a long string of junk
>>> [z.count(chr(i)) for i in range(256)] # number of occurrences of each byte
[3936, 3861, 3978, 3951, 3858, 3937, 3945, 3828, 3984, 3871, 3985, 
 3961, 3879, 3924, 3817, 3984, 3963, 3858, 4029, 3903, 3884, 3817, 
 ... yada ...
dF
+1: Nice proof!
RichieHindle