+int StrBufDestroyStreamContext(eStreamType type, vStreamT **vStream, const char **Err)
+{
+ int err;
+ int rc = 0;
+ *Err = NULL;
+
+ if ((vStream == NULL) || (*vStream==NULL)) {
+ *Err = strerror(EINVAL);
+ return EINVAL;
+ }
+ switch (type)
+ {
+ case eBase64Encode:
+ case eBase64Decode:
+ free(*vStream);
+ *vStream = NULL;
+ break;
+ case eZLibDecode:
+ {
+ z_enc_stream *stream = (z_enc_stream *)*vStream;
+ (void)inflateEnd(&stream->zstream);
+ free(stream->OutBuf.buf);
+ free(stream);
+ }
+ case eZLibEncode:
+ {
+ z_enc_stream *stream = (z_enc_stream *)*vStream;
+ err = deflateEnd(&stream->zstream);
+ if (err != Z_OK) {
+ *Err = zError(err);
+ rc = -1;
+ }
+ free(stream->OutBuf.buf);
+ free(stream);
+ *vStream = NULL;
+ break;
+ }
+ case eEmtyCodec:
+ break; /// TODO
+ }
+ return rc;
+}
+
+int StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, const char* pIn, long pInLen, vStreamT *vStream, int LastChunk, const char **Err)
+{
+ int rc = 0;
+ switch (type)
+ {
+ case eBase64Encode:
+ {
+ /// TODO
+/*
+ // base64_decodestate *state = (base64_decodestate*)vStream;
+ long ExpectLen;
+
+ if (In != NULL)
+ {
+ pIn = In->buf;
+ pInLen = In->BufUsed;
+ }
+ if ((In == NULL) || (vStream == NULL))
+ return;
+
+ ExpectLen = (pInLen / 4) * 3;
+
+ if (Target->BufSize - Target->BufUsed < ExpectLen)
+ {
+ IncreaseBuf(Target, 1, Target->BufUsed + ExpectLen + 1);
+ }
+
+ //// ExpectLen = base64_encode_block(pIn, pInLen, Target->buf + Target->BufUsed, state);
+ Target->BufUsed += ExpectLen;
+ Target->buf[Target->BufUsed] = '\0';
+*/
+ }
+ break;
+ case eBase64Decode:
+ {
+/*
+ base64_decodestate *state = (base64_decodestate *)vStream;
+ long ExpectLen;
+
+ if (In != NULL)
+ {
+ pIn = In->buf;
+ pInLen = In->BufUsed;
+ }
+ if ((pIn == NULL) || (vStream == NULL))
+ return;
+
+ ExpectLen = (pInLen / 4) * 3;
+
+ if (Target->BufSize - Target->BufUsed < ExpectLen)
+ {
+ IncreaseBuf(Target, 1, Target->BufUsed + ExpectLen + 1);
+ }
+
+ ExpectLen = base64_decode_block(pIn, pInLen, Target->buf + Target->BufUsed, state);
+ Target->BufUsed += ExpectLen;
+ Target->buf[Target->BufUsed] = '\0';
+*/
+ }
+ break;
+ case eZLibEncode:
+ {
+ z_enc_stream *stream = (z_enc_stream *)vStream;
+ int org_outbuf_len = stream->OutBuf.BufUsed;
+ int err;
+ unsigned int chunkavail;
+
+ if (In->ReadWritePointer != NULL)
+ {
+ stream->zstream.next_in = (Bytef *) In->ReadWritePointer;
+ stream->zstream.avail_in = (uInt) In->Buf->BufUsed -
+ (In->ReadWritePointer - In->Buf->buf);
+ }
+ else
+ {
+ stream->zstream.next_in = (Bytef *) In->Buf->buf;
+ stream->zstream.avail_in = (uInt) In->Buf->BufUsed;
+ }
+
+ stream->zstream.next_out = (unsigned char*)stream->OutBuf.buf + stream->OutBuf.BufUsed;
+ stream->zstream.avail_out = chunkavail = (uInt) stream->OutBuf.BufSize - stream->OutBuf.BufUsed;
+
+ err = deflate(&stream->zstream, (LastChunk) ? Z_FINISH : Z_NO_FLUSH);
+
+ stream->OutBuf.BufUsed += (chunkavail - stream->zstream.avail_out);
+
+ if (Target &&
+ (LastChunk ||
+ (stream->OutBuf.BufUsed != org_outbuf_len)
+ ))
+ {
+ iSwapBuffers(Target->Buf, &stream->OutBuf);
+ }
+
+ if (stream->zstream.avail_in == 0)
+ {
+ FlushStrBuf(In->Buf);
+ In->ReadWritePointer = NULL;
+ }
+ else
+ {
+ if (stream->zstream.avail_in < 64)
+ {
+ memmove(In->Buf->buf,
+ In->Buf->buf + In->Buf->BufUsed - stream->zstream.avail_in,
+ stream->zstream.avail_in);
+
+ In->Buf->BufUsed = stream->zstream.avail_in;
+ In->Buf->buf[In->Buf->BufUsed] = '\0';
+ }
+ else
+ {
+
+ In->ReadWritePointer = In->Buf->buf +
+ (In->Buf->BufUsed - stream->zstream.avail_in);
+ }
+ }
+ rc = (LastChunk && (err != Z_FINISH));
+ if (!rc && (err != Z_OK)) {
+ *Err = zError(err);
+ }
+
+ }
+ break;
+ case eZLibDecode: {
+ z_enc_stream *stream = (z_enc_stream *)vStream;
+ int org_outbuf_len = stream->zstream.total_out;
+ int err;
+
+ if ((stream->zstream.avail_out != 0) && (stream->zstream.next_in != NULL)) {
+ if (In->ReadWritePointer != NULL)
+ {
+ stream->zstream.next_in = (Bytef *) In->ReadWritePointer;
+ stream->zstream.avail_in = (uInt) In->Buf->BufUsed -
+ (In->ReadWritePointer - In->Buf->buf);
+ }
+ else
+ {
+ stream->zstream.next_in = (Bytef *) In->Buf->buf;
+ stream->zstream.avail_in = (uInt) In->Buf->BufUsed;
+ }
+ }
+
+ stream->zstream.next_out = (unsigned char*)stream->OutBuf.buf + stream->OutBuf.BufUsed;
+ stream->zstream.avail_out = (uInt) stream->OutBuf.BufSize - stream->OutBuf.BufUsed;
+
+ err = inflate(&stream->zstream, Z_NO_FLUSH);
+
+ ///assert(ret != Z_STREAM_ERROR); /* state not clobbered * /
+ switch (err) {
+ case Z_NEED_DICT:
+ err = Z_DATA_ERROR; /* and fall through */
+
+ case Z_DATA_ERROR:
+ *Err = zError(err);
+ case Z_MEM_ERROR:
+ (void)inflateEnd(&stream->zstream);
+ return err;
+ }
+
+ stream->OutBuf.BufUsed += stream->zstream.total_out + org_outbuf_len;
+
+ if (Target) iSwapBuffers(Target->Buf, &stream->OutBuf);
+
+ if (stream->zstream.avail_in == 0)
+ {
+ FlushStrBuf(In->Buf);
+ In->ReadWritePointer = NULL;
+ }
+ else
+ {
+ if (stream->zstream.avail_in < 64)
+ {
+ memmove(In->Buf->buf,
+ In->Buf->buf + In->Buf->BufUsed - stream->zstream.avail_in,
+ stream->zstream.avail_in);
+
+ In->Buf->BufUsed = stream->zstream.avail_in;
+ In->Buf->buf[In->Buf->BufUsed] = '\0';
+ }
+ else
+ {
+
+ In->ReadWritePointer = In->Buf->buf +
+ (In->Buf->BufUsed - stream->zstream.avail_in);
+ }
+ }
+ }
+ break;
+ case eEmtyCodec: {
+
+ }
+ break; /// TODO
+ }
+ return rc;
+}
+
+/**
+ * @ingroup StrBuf_DeEnCoder
+ * @brief decode a buffer from base 64 encoding; destroys original
+ * @param Buf Buffor to transform
+ */
+int StrBufDecodeHex(StrBuf *Buf)
+{
+ unsigned int ch;
+ char *pch, *pche, *pchi;
+
+ if (Buf == NULL) return -1;
+
+ pch = pchi = Buf->buf;
+ pche = pch + Buf->BufUsed;
+
+ while (pchi < pche){
+ ch = decode_hex(pchi);
+ *pch = ch;
+ pch ++;
+ pchi += 2;
+ }
+
+ *pch = '\0';
+ Buf->BufUsed = pch - Buf->buf;
+ return Buf->BufUsed;
+}
+
+/**
+ * @ingroup StrBuf_DeEnCoder