static int brotli_open(struct stream_encoded *stream, int fd) { struct br_enc_data *data = mem_calloc(1, sizeof(*data)); stream->data = NULL; if (!data) { return -1; } data->fdread = fd; BrotliStateInit(&data->br_stream); stream->data = data; return 0; }
void QBrotliDecoder::decode() { if (m_pInputDevice == nullptr || m_pOutputDevice == nullptr) { emit onError("IO devices not set."); return; } if (!m_pInputDevice->isReadable()) { emit onError("Input not readable."); return; } if (!m_pOutputDevice->isWritable()) { emit onError("Output not writeable."); return; } BrotliState state; qint64 totalsize=-1; if (!m_pInputDevice->isSequential()) totalsize = m_pInputDevice->size(); BrotliStateInit(&state); int finish = 0; BrotliResult r; BrotliInput input = initInput(m_pInputDevice); BrotliOutput output = initOutput(m_pOutputDevice); for (;;) { r = BrotliDecompressStreaming(input,output,finish,&state); if (r==0) { //error emit onError("Decoding error."); break; } else if (r==1) { //done break; } if (totalsize!=-1) emit onProgress((double)m_pInputDevice->pos()/(double)totalsize); if (m_pInputDevice->atEnd()) finish=1; } emit onProgress(1.0); emit onDone(); BrotliStateCleanup(&state); }