Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==========================================
- // My deflate code is:
- // ==========================================
- int ret;
- z_stream strm = {};
- // 22 byte input: 34 byte output expected.
- //
- // -MAX_WBITS = 24 byte output
- // MAX_WBITS = 30 byte output
- // MAX_WBITS + 16 = 42 byte output
- ret = deflateInit(&strm, 9); // 9 is maximum compression level
- if (ret)
- {
- std::stringstream error;
- error << "Error " << ret << " occurred with initiating compression.";
- return CreateError(error.str().c_str());
- }
- unsigned char * output_buffer = (unsigned char *)malloc(SendMsgSize + 256);
- if (!output_buffer)
- {
- deflateEnd(&strm);
- std::stringstream error;
- error << "Error with compressing send binary, could not allocate enough memory. Desired " << SendMsgSize + 256 << " bytes.";
- return CreateError(error.str().c_str());
- }
- strm.next_in = (unsigned char *)SendMsg;
- strm.avail_in = SendMsgSize;
- // Allocate memory for compression
- strm.avail_out = _msize(output_buffer);
- strm.next_out = output_buffer;
- ret = deflate(&strm, Z_FINISH);
- if (ret != Z_STREAM_END)
- {
- std::stringstream error;
- error << "Error with compressing send binary, deflate() returned " << ret << ". Zlib error: " << (strm.msg ? strm.msg : "");
- free(output_buffer);
- deflateEnd(&strm);
- return CreateError(error.str().c_str());
- }
- deflateEnd(&strm);
- void * output_bufferResize = realloc(output_buffer, strm.total_out);
- if (!output_bufferResize)
- {
- free(output_buffer); // realloc will not free on error
- CreateError("Error with compressing send binary, reallocating memory to remove excess space after compression failed.");
- return;
- }
- free(SendMsg);
- SendMsg = (char *)output_bufferResize;
- SendMsgSize = strm.total_out;
- // ==========================================
- // When inflating back, I use:
- // ==========================================
- z_stream strm = { };
- int ret = inflateInit(&strm);
- if (ret)
- {
- std::stringstream error;
- error << "Error " << ret << " occurred with initiating decompression.";
- return CreateError(error.str().c_str());
- }
- unsigned char * output_buffer = NULL, *output_buffer_pointer = NULL;
- strm.next_in = (unsigned char *)receivedMsg.content.data();
- strm.avail_in = receivedMsg.content.size();
- // run inflate() on input until output buffer not full, finish
- // compression if all of source has been read in
- do {
- // Expand memory for decompression
- output_buffer_pointer = (unsigned char *)realloc(output_buffer, (output_buffer ? _msize(output_buffer) : 0) + 1024);
- if (!output_buffer_pointer)
- {
- std::stringstream error;
- error << "Error with decompression, could not allocate enough memory. Desired "
- << (output_buffer ? _msize(output_buffer) : 0) + 1024 << " bytes.";
- free(output_buffer);
- inflateEnd(&strm);
- return CreateError(error.str().c_str());
- }
- output_buffer = output_buffer_pointer;
- output_buffer_pointer += _msize(output_buffer) - 1024;
- strm.avail_out = 1024;
- strm.next_out = output_buffer_pointer;
- ret = inflate(&strm, Z_FINISH);
- if (ret < Z_OK)
- {
- std::stringstream error;
- error << "Error with decompression, inflate() returned error " << ret
- << ". Zlib error: " << (strm.msg ? strm.msg : "");
- free(output_buffer);
- inflateEnd(&strm);
- return CreateError(error.str().c_str());
- }
- } while (strm.avail_in != 0);
- if (ret < 0)
- {
- std::stringstream error;
- error << "Error with decompression: " << ret << ". Zlib error: " << (strm.msg ? strm.msg : "");
- inflateEnd(&strm);
- return CreateError(error.str().c_str());
- }
- inflateEnd(&strm);
- // Update data with new message content
- receivedMsg.content.assign((char *)output_buffer, _msize(output_buffer));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement