Compression and decompression

Posted by Nick Gammon on Thu 09 Dec 2004 03:25 AM — 6 posts, 31,985 views.

Australia Forum Administrator #0
Introduction

Lua strings have the useful attribute that they can contain any character, including the 'null' character (hex 00) which is often used in C to delimit strings.

With this in mind, version 3.57 of MUSHclient offers compression and decompression for Lua scripting. This is based on the zLib library which is already incorporated into MUSHclient for use in MCCP decompression.

Compress


utils.compress (s [, method] )


Compresses string s and returns the compressed form. Note that it may contain nulls (bytes with a zero value).

The optional argument 'method' indicates the level of compression you want.

  • 0 = no compression
  • 1 = best speed
    ....
  • 9 = best compression


The default, if omitted, is 6.

Decompression


utils.decompress (s)


Decompresses string s and returns the decompressed form. Raises an error if decompression cannot be done (eg. bad compressed data).

These two functions should be complementary, so that this should always be true:


x = "some string" -- for any (string) data whatsoever

y = utils.decompress (utils.compress (x)) --> y should be same as x

Greece #1
I think all the languages allow nulls. I know VB does.
Australia Forum Administrator #2
It's the way that data is passed into MUSHclient that is the problem. Here is the code for Base64Encode, for example:


VARIANT CMUSHclientDoc::Base64Encode(LPCTSTR Text, BOOL MultiLine) 
{
int len = strlen (Text);

...

}


The first thing it does is find the length of the supplied text by doing strlen, which searches for a null. That terminates the string.
USA #3
Thats passing back into MC from... what? the lua dll? is that before or after compression? (passed into MC proper? or passed into MC's zlib?)

So does Lua use PWSZ strings? or still BSTRs?

and shouldnt the workaround be easy enough?
Australia Forum Administrator #4
That's the code generated when I do a COM interface, in this case from a script language, other than Lua, into MUSHclient. The LPCTSTR data type is a long pointer to a C-string. A C-string is a null-terminated one.

However with Lua the scripting interface is different, it does not rely on COM. Thus, if I expect nulls in strings, which I do for something like a hash, encryption or (de)compression, then I ask Lua for a string, and a length, as 2 separate fields. That way imbedded nulls don't affect the result.

Greece #5
Hmm, I don't remember much about C++ strings, but I think that the STL and MFC string types allow nulls in them. I don't know if you could change all the declarations to that type though.