Note that there are some explanatory texts on larger screens.

plurals
  1. POMoving compressed strings between C++ and PHP with zlib
    primarykey
    data
    text
    <p>I'm working on a project where a Windows web server running PHP is communicating over a very slow connection with a back end Linux server running an application written in C++. Because the connection between the two machines is so slow, I'd like to compress the traffic moving between them.</p> <p>I've gotten to where I can compress a string, save it to a file, read the file, and uncompress the string in C++ using Zlib, and likewise in PHP. However, if I try to compress a string in one language and decompress it in the other (as will be happening in the real world), I get errors griping that the compressed data is corrupted. I've also noticed that the same string compressed in C++ results in a different file than in PHP, which leads me to believe that Zlib is using a different compression algorithm on each language.</p> <p>I'm using default settings on both sides. The C++ I'm using to do the compression and decompression is </p> <pre><code>compress((Bytef*)compressed, (uLongf*)&amp;compressedLength, (Bytef*)uncompressed, (uLong)uncomressedLength); uncompress((Bytef*)uncompressed, (uLongf*)&amp;uncomressedLength, (Bytef*)compressed, (uLong)compressedLength); </code></pre> <p>while the PHP code is</p> <pre><code>$compressed = gzcompress($uncompressed); $uncompressed = gzuncompress($compressed); </code></pre> <p>Why are these resulting in different compressed strings? Is that what's causing the problems with decompression? What should I be doing to get this to work? Also, I'm not committed to Zlib. Zlib's what my initial research uncovered, but if there's a better way to do this, I'm all ears.</p> <p><strong>Edit</strong>: Actually, after doing a little more testing, it appears that C++ was working with my initial test case, but not universally. I tried it with the input "hellohellohello", and on decompression, it reported a Z_DATA_ERROR and decompressed it to just "hello". I guess that means I'm doing something wrong on the C++ side, which may explain why PHP is unhappy decompressing C++ compressed strings.</p> <p><strong>Edit 2</strong>: I tried out the zpipe.c sample program, and it correctly uncompresses strings compressed by PHP and produces compressed strings PHP can uncompress. Clearly, the problem(s) exist in my C++ code. Either my usage of compress and uncompress is incorrect, or I'm reading and writing the file incorrectly. Neither the compress or decompress programs interact correctly with zpipe.</p> <p><strong>Update</strong>: I've now gotten to where I can compress a string using PHP and read it with either PHP or C++, and I can compress a string with C++ and read it with C++, but attempting to read it with PHP results in PHP Warning: gzuncompress(): data error. What could be different that would cause this combination of working/not working scenarios?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload