Advertisement
Guest User

Potentially broken deflate/zlib code

a guest
Dec 26th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==========================================
  2. // My deflate code is:
  3. // ==========================================
  4. int ret;
  5. z_stream strm = {};
  6.  
  7. // 22 byte input: 34 byte output expected.
  8. //
  9. // -MAX_WBITS = 24 byte output
  10. // MAX_WBITS = 30 byte output
  11. // MAX_WBITS + 16 = 42 byte output
  12.  
  13. ret = deflateInit(&strm, 9); // 9 is maximum compression level
  14. if (ret)
  15. {
  16.     std::stringstream error;
  17.     error << "Error " << ret << " occurred with initiating compression.";
  18.     return CreateError(error.str().c_str());
  19. }
  20.  
  21. unsigned char * output_buffer = (unsigned char *)malloc(SendMsgSize + 256);
  22. if (!output_buffer)
  23. {
  24.     deflateEnd(&strm);
  25.     std::stringstream error;
  26.     error << "Error with compressing send binary, could not allocate enough memory. Desired " << SendMsgSize + 256 << " bytes.";
  27.     return CreateError(error.str().c_str());
  28. }
  29.  
  30. strm.next_in = (unsigned char *)SendMsg;
  31. strm.avail_in = SendMsgSize;
  32.  
  33. // Allocate memory for compression
  34. strm.avail_out = _msize(output_buffer);
  35. strm.next_out = output_buffer;
  36.  
  37. ret = deflate(&strm, Z_FINISH);
  38. if (ret != Z_STREAM_END)
  39. {
  40.     std::stringstream error;
  41.     error << "Error with compressing send binary, deflate() returned " << ret << ". Zlib error: " << (strm.msg ? strm.msg : "");
  42.     free(output_buffer);
  43.     deflateEnd(&strm);
  44.     return CreateError(error.str().c_str());
  45. }
  46.  
  47. deflateEnd(&strm);
  48.  
  49. void * output_bufferResize = realloc(output_buffer, strm.total_out);
  50. if (!output_bufferResize)
  51. {
  52.     free(output_buffer); // realloc will not free on error
  53.     CreateError("Error with compressing send binary, reallocating memory to remove excess space after compression failed.");
  54.     return;
  55. }
  56. free(SendMsg);
  57.  
  58. SendMsg = (char *)output_bufferResize;
  59. SendMsgSize = strm.total_out;
  60.  
  61.  
  62. // ==========================================
  63. // When inflating back, I use:
  64. // ==========================================
  65. z_stream strm = { };
  66. int ret = inflateInit(&strm);
  67. if (ret)
  68. {
  69.     std::stringstream error;
  70.     error << "Error " << ret << " occurred with initiating decompression.";
  71.     return CreateError(error.str().c_str());
  72. }
  73.  
  74. unsigned char * output_buffer = NULL, *output_buffer_pointer = NULL;
  75. strm.next_in = (unsigned char *)receivedMsg.content.data();
  76. strm.avail_in = receivedMsg.content.size();
  77. // run inflate() on input until output buffer not full, finish
  78. // compression if all of source has been read in
  79. do {
  80.     // Expand memory for decompression
  81.     output_buffer_pointer = (unsigned char *)realloc(output_buffer, (output_buffer ? _msize(output_buffer) : 0) + 1024);
  82.     if (!output_buffer_pointer)
  83.     {
  84.         std::stringstream error;
  85.         error << "Error with decompression, could not allocate enough memory. Desired "
  86.             << (output_buffer ? _msize(output_buffer) : 0) + 1024 << " bytes.";
  87.         free(output_buffer);
  88.  
  89.         inflateEnd(&strm);
  90.         return CreateError(error.str().c_str());
  91.     }
  92.  
  93.     output_buffer = output_buffer_pointer;
  94.     output_buffer_pointer += _msize(output_buffer) - 1024;
  95.     strm.avail_out = 1024;
  96.     strm.next_out = output_buffer_pointer;
  97.     ret = inflate(&strm, Z_FINISH);
  98.     if (ret < Z_OK)
  99.     {
  100.         std::stringstream error;
  101.         error << "Error with decompression, inflate() returned error " << ret
  102.             << ". Zlib error: " << (strm.msg ? strm.msg : "");
  103.         free(output_buffer);
  104.         inflateEnd(&strm);
  105.         return CreateError(error.str().c_str());
  106.     }
  107.  
  108. } while (strm.avail_in != 0);
  109.  
  110. if (ret < 0)
  111. {
  112.     std::stringstream error;
  113.     error << "Error with decompression: " << ret << ". Zlib error: " << (strm.msg ? strm.msg : "");
  114.     inflateEnd(&strm);
  115.     return CreateError(error.str().c_str());
  116. }
  117. inflateEnd(&strm);
  118.  
  119. // Update data with new message content
  120. receivedMsg.content.assign((char *)output_buffer, _msize(output_buffer));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement