1

I am transferring data from a C++ client to a nodejs server.

I compress the string using zlib deflate first, then I use curl_easy_escape to url encode the compressed string.

std::string s = zlib_compress(temp.str());
std::cout << s <<"\n";
CURL *handle = curl_easy_init();
char* o = curl_easy_escape(handle, s.data(), s.size());
std::cout << o <<"\n";

Then I send it using:

std::string bin(o);
curl_easy_setopt(handle, CURLOPT_POSTFIELDSIZE, bin.size());
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, bin.data());
curl_easy_perform(handle);

When I run this, I get the output:

x??с??Ҵ4?
x%DA%D3%D1%81%80%E2%92%D2%B44%1D%03%00%1BW%03%E5

Now, I receive the second encoded string on my nodejs server as it is. I now try to decode it.

var x = req.params;
for (var key in req.body)
{   
console.log(key);
var x = unescape(key);
var buffer = new Buffer(x);
console.log(x);
zlib.inflate(buffer, function(err, buffer) {

    console.log(err+" here");
});
}

Which outputs:

x%DA%D3%D1%81%80%E2%92%D2%B44%1D%03%00%1BW%03%E5
xÚÓÑâÒ´4å
Error: incorrect header check here

What is the problem here? How do I debug it?

1 Answer 1

1

You can debug it by printing the decimal value for each byte in the compressed string in C++ and node.js code. For C++ that code would be:

for(int i=0; i<s.size(); i++) {
    std::cout << static_cast<int>(s[i]);
}

In node.js code you would need to print the decimal value for each byte contained in variable buffer.

If the decimal values for each byte are identical in both C++ and node.js parts, then zlib libraries are incompatible or functions do not match: e.g. zlib_compress in C++ may correspond to something else than zlib.inflate in node.js: maybe there is a function like zlib.decompress() .

The root cause can be in that characters are 1-byte in C++ std::string and 2-byte in node.js . Specifying the encoding when constructing Buffer in node.js may solve the problem if that is it:

var buffer = new Buffer(x, 'binary');

See https://nodejs.org/api/buffer.html#buffer_new_buffer_str_encoding

As the data is zlib compressed here, or in a general compressed case, the encoding should be binary.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't know if I should accept your answer or not, because: 1. Yes, the problem was due to encoding. 2. But it shouldn't be utf8, as any compressed data is binary. So, yes, binary in encoding solved the problem.
If you found the question insightful, will you please upvote it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.