32 #include <boost/regex.hpp>
35 using namespace boost;
55 #if 1 || !defined(ZLIB_CONST)
57 return const_cast<Bytef*
>(
reinterpret_cast<const Bytef*
>(s.c_str()));
61 return reinterpret_cast<const Bytef*
>(s.c_str());
69 bool FuncioExtractoraIstream::operator()(
string & bloc) {
74 const streamsize bytes = ist_.readsome(&buffer_[0],
MIDA_BUFFER);
75 bloc.assign(&buffer_[0], static_cast<string::size_type>(bytes));
79 bool FuncioEscriptoraOstream::operator()(
const string & bloc) {
83 ost_.write(bloc.c_str(), bloc.length());
87 string TransformacioChunked::transforma(
const string & bloc)
const {
91 oss << std::hex << bloc.length() <<
"\r\n";
93 oss.write(bloc.c_str(), bloc.length());
98 string TransformacioChunked::destransforma(
const string & text_transformat,
101 istringstream iss(text_transformat);
103 r.reserve(text_transformat.length());
104 iss.exceptions(ios::badbit|ios::failbit|ios::eofbit);
106 std::array<char, MIDA_BUFFER> buffer;
110 const regex re_parcial_mida(
"^([[:xdigit:]]+)", regbase::perl);
115 getline(iss, params,
'\n');
116 if (!regex_search(params, m, re_parcial_mida)) {
120 unsigned long mida_bloc;
121 mida_bloc = utils::from_string<unsigned long>(m[1], std::hex);
122 if (0 == mida_bloc) {
129 const streamsize fi = iss.str().size();
130 while (iss.tellg() < fi && llegits < mida_bloc) {
131 const size_t bufvalids = std::min<size_t>(
MIDA_BUFFER, mida_bloc-llegits);
132 const size_t bytes =
static_cast<size_t>(iss.readsome(&buffer[0], bufvalids));
133 r.append(&buffer[0], static_cast<streamsize>(bytes));
136 if (llegits != mida_bloc) {
142 iss.seekg(2, ios::cur );
155 catch (
const ios_base::failure &) {
157 if (
TRANSFORMAT == estat && (iss.fail() || iss.bad())) {
163 string TransformacioChunked::finalitza()
const {
175 AplicadorTransformacioBase::~AplicadorTransformacioBase() {
181 void AplicadorTransformacioBase::aplica() {
184 while (extractora_(buffer)) {
185 const bool ok = escriptora_(pTrans->
transforma(buffer));
194 bool TransformacioDeflate::inicialitza(
Mode mode)
const {
200 strm_.zalloc = Z_NULL;
201 strm_.zfree = Z_NULL;
202 strm_.opaque = Z_NULL;
204 if (DEFLATE == mode) {
219 ret = ::deflateInit2(&strm_, Z_BEST_SPEED,
229 ret = ::inflateInit(&strm_);
231 const bool ok = ( Z_OK == ret );
232 inicialitzat_ = (ok ? mode : CAP);
241 string TransformacioDeflate::transforma(
const string & bloc)
const {
243 if (DEFLATE != inicialitzat_) {
244 inicialitza(DEFLATE);
247 strm_.avail_in = bloc.length();
249 std::array<unsigned char,MIDA_BUFFER_ZLIB> buf;
251 strm_.avail_out = MIDA_BUFFER_ZLIB;
252 strm_.next_out = buf.data();
253 const int ret = ::deflate(&strm_, Z_SYNC_FLUSH);
254 if (Z_STREAM_ERROR == ret) {
257 const size_t bytes = MIDA_BUFFER_ZLIB - strm_.avail_out;
258 t.append(reinterpret_cast<char*>(buf.data()), bytes);
259 }
while (0 == strm_.avail_out);
260 assert( 0 == strm_.avail_in );
264 string TransformacioDeflate::finalitza()
const {
269 string TransformacioDeflate::destransforma(
const string & text_transformat,
273 inicialitza(INFLATE);
274 strm_.avail_in = text_transformat.length();
276 std::array<unsigned char,MIDA_BUFFER_ZLIB> buf;
278 strm_.avail_out = MIDA_BUFFER_ZLIB;
279 strm_.next_out = buf.data();
280 const int ret = ::inflate(&strm_, Z_SYNC_FLUSH);
293 const size_t bytes = MIDA_BUFFER_ZLIB - strm_.avail_out;
294 d.append(reinterpret_cast<char*>(buf.data()), bytes);
295 }
while (0 == strm_.avail_out);
299 void TransformacioDeflate::desinicialitza()
const {
300 if (CAP == inicialitzat_) {
304 if (DEFLATE == inicialitzat_) {
305 ::deflateEnd(&strm_);
307 else { assert( INFLATE == inicialitzat_ );
308 ::inflateEnd(&strm_);
311 memset(&strm_, 0x00,
sizeof(z_stream));
314 string CadenaTransformacions::transforma(
const string &bloc)
const {
316 for (
auto it=cadena_.begin(); it != cadena_.end(); ++it) {
317 r = (*it)->transforma(r);
322 string CadenaTransformacions::finalitza()
const {
324 for (
auto it=cadena_.begin(); it != cadena_.end(); ++it) {
325 f.append( (*it)->finalitza() );
330 string CadenaTransformacions::destransforma(
const string & text,
334 for (
auto rit=cadena_.rbegin(); estat ==
TRANSFORMAT && rit != cadena_.rend(); ++rit) {
335 r = (*rit)->destransforma(r, estat);