C ++ - Using zlib with const data

To compress / decompress data using zlib, first I need to create a structure named z_stream

. z_stream

has two non-const pointers called next_in

and next_out

.

If I want to make a function like this:

void ungzip(std::vector<unsigned char>& dst,const std::vector<unsigned char>& src)
{
  z_stream strm;
  // more code
}

      

and others like that,

void gzip  (std::vector<unsigned char>& dst,const std::vector<unsigned char>& src);

      

What should I do?

Copy src to local std::vector<unsigned char>

std::vector<unsigned char> tmp(src);

      

and use it as source or installation pointer like this strm.next_in = const_cast<char*>(&src[0])

,?

Does zlib save input data?

+3


source to share


1 answer


Probably the cleanest way would be to simply define ZLIB_CONST

at compile time to be instead of an input pointer const unsigned char *

.

Note, however, that simply dropping the constant will actually work (this is legal if the character being referenced is not actually constant). The code for this should be:



strm.next_in = (unsigned char *)&src[0];

      

or the uglier option const_cast

if you do this kind of thing.

+1


source







All Articles