Your problem is that you're using a string to store binary data. That binary data can have a byte in it that has the value 0. Which when interpreted as a string that 0 becomes the strings terminator. Hence you get a truncation.
Instead try something like (fyi, this is untested & uncompiled code):
// in this version our dest is a pointer to a pointer to a character// and we store the length of destination in the passed in parameter destLengthint CompressString(char* src, char** destination, size_t* destLength){ //Define the source, destination, source length, and destination length int srcLen=strlen(src); int destLen=compressBound(srcLen); char *dest=new char[destLen]; //Decompress the string in src and place it in dest int result=compress((unsigned char *)dest,(uLongf*)&destLen,(const unsigned char *)src,srcLen); //Return the results of the compression *destination = dest; *destLength = destLen; return(result);}// since src is not really a string, we must know how long in bytes it isint DecompressString(char* src, int srcLen, char** destination, int* destLen){ //Define the source, destination, source length, and destination length char *dest=new char[destLen]; //Decompress the string in src and place it in dest int result=uncompress((unsigned char *)dest,(uLongf*)destLen,(const unsigned char *)src,srcLen); //Return the results of the decompression *destination = dest; return(result);}int main(int argc, char *argv[]) { //Define the needed variables string src="bla bla bla lorum ipsum this is a test string yadda yadda yadda, etc. etc."; char* compressed; char* decompressed; int compressedLength; int decompressedLength; int result; //Compress the string, and output the results cout << "Compressing string: " << src.c_str() << "\n"; result=CompressString(src.c_str(), &compressed, &compressedLength); cout << "Error code: " << result << "\n"; // The best way to achieve this would be to print out each byte (in hex) in the string, for now lets just comment it out. //cout << "Compressed String: " << compressed.c_str() << "\n\n"; //Decompress the string, and output the results // The best way to achieve this would be to print out each byte (in hex) in the string, for now lets just comment it out. //cout << "Decompressing String: " << compressed.c_str() << "\n"; result=DecompressString(compressed, compressedLength, &decompressed, &decompressedLength); cout << "Error code: " << result << "\n"; // at this stage we have to buffers that have been allocated by // CompressString and DecompressString, the decompressed one does NOT // have a NULL terminator on it, so we must make a string to print out string decompressedString(decompressed, decompressedLength); cout << "Decompressed String: " << decompressedString.c_str() << "n\n"; // now free the memory we allocated earlier delete[] compressed; delete[] decompressed;}
There are a few other problems with your code. You were using delete instead of delete[] on memory you new[]. If you use the array new (new TYPE[count]) then you should use delete[] to delete that memory.
When dealing with sizes of things (in bytes especially) you should use size_t whenever possible. If I were to totally rewrite my example above I'd use size_t's for compressedLength, decompressedLength, etc.
As for printing out binary data, I'd recommend doing something like:
void printBytes(char* src, size_t count){ char* lim = src + count; while(src < lim) { // the C way of printing something out... printf("%02x", *src); // in c++ I think you can do something like, but I'm not 100% certain it'll work std::cout << std::hex << int(*src); src++; }}
Hope this helps, and that my code isn't too buggy. :-)
[Edited by - smoret on March 4, 2008 7:31:47 PM]