Esempio n. 1
0
	std::streamsize memrdbuf::load()
	{
		lzma_.next_out = reinterpret_cast<uint8_t *>(&buf_[0]);
		if (lzma_.total_out > 0)
		{
			lzma_.avail_out = buf_.size();
			lzma_.total_out = 0;
			lzma_ret retval = lzma_code(&lzma_, action_);
			if (retval != LZMA_OK && retval != LZMA_STREAM_END) throw compress_error{"Compression failed with liblzma error " + util::t2s(retval)};
			if (lzma_.total_out > 0) goto loaded;
		}
		lzma_.next_out = reinterpret_cast<uint8_t *>(&buf_[0]);
		while (lzma_.total_out == 0)
		{
			if (lzma_.avail_in == 0) action_ = LZMA_FINISH;
			lzma_.avail_out = buf_.size();
			lzma_ret retval = lzma_code(&lzma_, action_);
			if (retval == LZMA_STREAM_END) break;
			else if (retval != LZMA_OK) throw compress_error{"Compression failed with liblzma error " + util::t2s(retval)};
		}
	loaded:	char *start = &buf_[0], *end = start + lzma_.total_out;
		pos_ += egptr() - eback();
		setg(start, start, end);
		return lzma_.total_out;
	}
Esempio n. 2
0
	std::streamsize buf_base::load()
	{
		lzma_.next_out = reinterpret_cast<uint8_t *>(&buf_[0]);
		if (lzma_.total_out > 0)
		{
			lzma_.avail_out = buf_.size();
			lzma_.total_out = 0;
			lzma_ret retval = lzma_code(&lzma_, action_);
			if (retval != LZMA_OK && retval != LZMA_STREAM_END) throw compress_error{"Compression failed with liblzma error " + util::t2s(retval)};
			if (lzma_.total_out > 0) goto loaded; // Valid use case?
		}
		lzma_.next_out = reinterpret_cast<uint8_t *>(&buf_[0]);
		while (lzma_.total_out == 0)
		{
			lzma_.total_in = 0;
			lzma_.next_in = reinterpret_cast<uint8_t *>(&inbuf_[0]);
			std::streamsize insize = fill();
			if (insize == 0) action_ = LZMA_FINISH;
			lzma_.avail_in = insize;
			lzma_.avail_out = buf_.size();
			lzma_ret retval = lzma_code(&lzma_, action_);
			//std::cout << "Got " << lzma_.total_in << " bytes, compressed to " << lzma_.total_out << "\n";
			if (retval == LZMA_STREAM_END) break;
			else if (retval != LZMA_OK) throw compress_error{"Compression failed with liblzma error " + util::t2s(retval)};
		}
	loaded:	char *start = &buf_[0], *end = start + lzma_.total_out;
		pos_ += egptr() - eback();
		setg(start, start, end);
		return lzma_.total_out;
	}
    int LzmaConnectorWriter::writeData (const unsigned char *pSrc, unsigned int uiSrcLen, unsigned char **pDest, unsigned int &uiDestLen, bool bLocalFlush)
    {
        int rc;
        unsigned int uiOldAvailableSpace = 0;
        bool bNeedFlush = false;
        _lzmaCompStream.next_in = pSrc;
        _lzmaCompStream.avail_in = uiSrcLen;
        uiDestLen = 0;
        *pDest = NULL;
        _bFlushed = false;

        while ((_lzmaCompStream.avail_in > 0) || bNeedFlush) {
            bNeedFlush = false;
            uiOldAvailableSpace = _lzmaCompStream.avail_out;
            if (bLocalFlush) {
                rc = lzma_code (&_lzmaCompStream, LZMA_SYNC_FLUSH);
                if (rc > 1) {
                    checkAndLogMsg ("LzmaConnectorWriter::writeData", Logger::L_MildError,
                                    "lzma_code() called with flag LZMA_SYNC_FLUSH returned with error code %d\n", rc);
                    uiDestLen = 0;
                    *pDest = NULL;
                    return -1;
                }
            }
            else {
                rc = lzma_code (&_lzmaCompStream, LZMA_RUN);
                if (rc > 1) {
                    checkAndLogMsg ("LzmaConnectorWriter::writeData", Logger::L_MildError,
                                    "lzma_code() called with flag LZMA_RUN returned with error code %d\n", rc);
                    uiDestLen = 0;
                    *pDest = NULL;
                    return -1;
                }
            }
            uiDestLen += uiOldAvailableSpace - _lzmaCompStream.avail_out;

            if (_lzmaCompStream.avail_out == 0) {
                bNeedFlush = true;
                _ulOutBufSize *= 2;
                _pOutputBuffer = (unsigned char *) realloc (_pOutputBuffer, _ulOutBufSize);
            }
            _lzmaCompStream.avail_out = _ulOutBufSize - uiDestLen;
            _lzmaCompStream.next_out = _pOutputBuffer + uiDestLen;
        }

        if (uiDestLen > 0) {
            *pDest = _pOutputBuffer;
        }
        else {
            *pDest = NULL;
        }
        _lzmaCompStream.avail_out = _ulOutBufSize;
        _lzmaCompStream.next_out = _pOutputBuffer;

        return 0;
    }
Esempio n. 4
0
void GSDumpLzma::Decompress() {
	lzma_action action = LZMA_RUN;

	m_strm.next_out  = m_area;
	m_strm.avail_out = m_buff_size;

	// Nothing left in the input buffer. Read data from the file
	if (m_strm.avail_in == 0 && !feof(m_fp)) {
		m_strm.next_in   = m_inbuf;
		m_strm.avail_in  = fread(m_inbuf, 1, BUFSIZ, m_fp);

		if (ferror(m_fp)) {
			fprintf(stderr, "Read error: %s\n", strerror(errno));
			throw "BAD"; // Just exit the program
		}
	}

	lzma_ret ret = lzma_code(&m_strm, action);

	if (ret != LZMA_OK) {
		if (ret == LZMA_STREAM_END)
			fprintf(stderr, "LZMA decoder finished without error\n\n");
		else {
			fprintf(stderr, "Decoder error: (error code %u)\n", ret);
			throw "BAD"; // Just exit the program
		}
	}

	m_start = 0;
	m_avail = m_buff_size - m_strm.avail_out;
}
Esempio n. 5
0
	bool decodeFillBuffer(std::string &error) {
		for (;0 != strm.avail_out;) {
			LOG_VERBOSE("decodeFillBuffer: %i bytes to go\n", (int) strm.avail_out);

			if (!fill_input_buffer(error)) return false;

			if (0 == strm.avail_in) {
				error.assign("Unexpected end of file");
				return false;
			}

			lzma_ret ret = lzma_code(&strm, LZMA_RUN);
			if (LZMA_OK != ret && LZMA_STREAM_END != ret) {
				errnoLzmaToStr("failed decoding data", ret, error);
				return false;
			}

			if (0 == strm.avail_out) return true; // done filling buffer

			if (LZMA_STREAM_END == ret) {
				if (lzma_index_iter_next(&iter, LZMA_INDEX_ITER_ANY)) {
					error.assign("Unexepected end of file");
					return false;
				}
				/* restart decoder */
				if (!loadBlock(error)) return false;
			}
		}
		return true;
	}
Esempio n. 6
0
	bool decode(std::string &error) {
		assert(0 != strm.avail_out);
		const unsigned char *pos = strm.next_out;

		for (;;) {
			if (!fill_input_buffer(error)) return false;

			if (0 == strm.avail_in) {
				error.assign("Unexpected end of file");
				return false;
			}

			lzma_ret ret = lzma_code(&strm, LZMA_RUN);
			if (LZMA_OK != ret && LZMA_STREAM_END != ret) {
				errnoLzmaToStr("failed decoding data", ret, error);
				return false;
			}

			if (LZMA_STREAM_END == ret && pos == strm.next_out) {
				/* end of stream AND we didn't get new data this round */
				if (lzma_index_iter_next(&iter, LZMA_INDEX_ITER_ANY)) {
					error.assign("Unexepected end of file");
					return false;
				}
				/* restart decoder */
				if (!loadBlock(error)) return false;
			} else {
				return true;
			}
		}
	}
Esempio n. 7
0
int LZMACompressor::Write(const char* data, size_t len) {
  _stream.next_in = (uint8_t*) data;
  _stream.avail_in = len;
  _total_in += len;
  int lzma_ret;
  int out_len = 0;
  char buf[BUFFER_SIZE];
  do {
    _stream.next_out = (uint8_t*) buf;
    _stream.avail_out = sizeof(buf);
    _stream.total_out = 0;
    switch ((lzma_ret = lzma_code(&_stream, LZMA_RUN))) {
    case LZMA_OK:
    case LZMA_STREAM_END:
      _writer->Write(buf, _stream.total_out);
      out_len += _stream.total_out;
      break;
    case LZMA_MEM_ERROR:
    case LZMA_DATA_ERROR:
    default:
      return -1;
    };
  } while (_stream.avail_out == 0);
  return out_len;
};
Esempio n. 8
0
static int xzclose(/*@only@*/ XZFILE *xzfile)
	/*@globals fileSystem @*/
	/*@modifies *xzfile, fileSystem @*/
{
    lzma_ret ret;
    size_t n;
    int rc;

    if (!xzfile)
	return -1;
    if (xzfile->encoding) {
	for (;;) {
	    xzfile->strm.avail_out = kBufferSize;
	    xzfile->strm.next_out = (uint8_t *)xzfile->buf;
	    ret = lzma_code(&xzfile->strm, LZMA_FINISH);
	    if (ret != LZMA_OK && ret != LZMA_STREAM_END)
		return -1;
	    n = kBufferSize - xzfile->strm.avail_out;
	    if (n && fwrite(xzfile->buf, 1, n, xzfile->fp) != n)
		return -1;
	    if (ret == LZMA_STREAM_END)
		break;
	}
    }
    lzma_end(&xzfile->strm);
    rc = fclose(xzfile->fp);
    memset(xzfile, 0, sizeof(*xzfile));
    free(xzfile);
    return rc;
}
Esempio n. 9
0
/* lzma_write writes up to 'count' bytes from the buffer 'buf' 
   to the file referred to by the lzmafile pointer. On success, 
   the number of bytes written is returned. On error, -1 is returned. */
long lzma_write(struct lzmafile* file, const void* buf, size_t count)
{
  int ret;
  lzma_stream* lstr = &file->str;
  unsigned char *bufout;      /* compressed output buffer */ 
  bufout = malloc(sizeof(char) * count);

  if (file->mode != 'w') {
    fprintf(stderr,"lzma_write error: file was not opened for writting\n");
    free(bufout);
    return -1;
  }
  lstr->next_in = buf;
  lstr->avail_in = count;
  while (lstr->avail_in) {
    lstr->next_out = bufout;
    lstr->avail_out = count;
    ret = lzma_code(lstr, LZMA_RUN);
    if (ret != LZMA_OK) {
      fprintf(stderr,"lzma_write error: encoding failed: %d\n",ret);
      free(bufout);
      return -1;
    }
    ret = fwrite(bufout, 1, count - lstr->avail_out, file->fp);
    if (ret != count - lstr->avail_out) {
      fprintf(stderr,"lzma_write error\n");
      free(bufout);
      return -1;
    }
  }
  free(bufout);
  return count;
}
Esempio n. 10
0
/* lzma_read attempts to read up to 'count' bytes from the 
   lzmafile pointer into the buffer 'buf'. On success, the 
   number of bytes is returned. On error, -1 is returned. 
   The 'buf' variable is not terminated by '\0'. */
long lzma_read(struct lzmafile* file, void* buf, size_t count)
{
  int ret;
  lzma_stream* lstr;
  if (file->mode != 'r') return -1;
  
  lstr = &file->str;
  lstr->next_out = buf;
  lstr->avail_out = count;
  
  /* decompress until EOF or output buffer is full */
  while (lstr->avail_out) {
    if (lstr->avail_in == 0) {
      /* refill input buffer */
      ret = fread(file->rdbuf, 1, BUF_SIZE, file->fp);
      if (ret == 0) {
        break; /* EOF */
      }
      lstr->next_in = file->rdbuf; /* buffer containing lzma data just read */
      lstr->avail_in = ret; /* number of bytes read */
    }
    ret = lzma_code(lstr, LZMA_RUN); 
    /* this fills up lstr->next_out and decreases lstr->avail_out */
    /* it also emptys lstr->next_in and decreases lstr->avail_in */
    if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
      fprintf(stderr,"lzma_read error: decoding failed: %d\n",ret);
      return -1;
    }
    if (ret == LZMA_STREAM_END) {
      break; /* EOF */
    }
  }
  return count - lstr->avail_out; /* length of buf that has valid data */
}
Esempio n. 11
0
File: common.c Progetto: hornos/pixz
void decode_index(void) {
    if (fseek(gInFile, -LZMA_STREAM_HEADER_SIZE, SEEK_END) == -1)
        die("Error seeking to stream footer");
    uint8_t hdrbuf[LZMA_STREAM_HEADER_SIZE];
    if (fread(hdrbuf, LZMA_STREAM_HEADER_SIZE, 1, gInFile) != 1)
        die("Error reading stream footer");
    lzma_stream_flags flags;
    if (lzma_stream_footer_decode(&flags, hdrbuf) != LZMA_OK)
        die("Error decoding stream footer");

    gCheck = flags.check;
    size_t index_seek = -LZMA_STREAM_HEADER_SIZE - flags.backward_size;
    if (fseek(gInFile, index_seek, SEEK_CUR) == -1)
        die("Error seeking to index");
    if (lzma_index_decoder(&gStream, &gIndex, MEMLIMIT) != LZMA_OK)
        die("Error creating index decoder");

    uint8_t ibuf[CHUNKSIZE];
    gStream.avail_in = 0;
    lzma_ret err = LZMA_OK;
    while (err != LZMA_STREAM_END) {
        if (gStream.avail_in == 0) {
            gStream.avail_in = fread(ibuf, 1, CHUNKSIZE, gInFile);
            if (ferror(gInFile))
                die("Error reading index");
            gStream.next_in = ibuf;
        }

        err = lzma_code(&gStream, LZMA_RUN);
        if (err != LZMA_OK && err != LZMA_STREAM_END)
            die("Error decoding index");
    }
}
Esempio n. 12
0
int lzma_compress_size( const unsigned char* data, unsigned size ){
	//Initialize LZMA
	lzma_stream strm = LZMA_STREAM_INIT;
	if( lzma_easy_encoder( &strm, 9 | LZMA_PRESET_EXTREME, LZMA_CHECK_CRC64 ) != LZMA_OK )
		return {};
	
	std::vector<uint8_t> out( size*2 ); //TODO: Find value properly
	//Setup in/out buffers
	strm.next_in  = (uint8_t*)data;
	strm.avail_in = size;
	strm.next_out  = out.data();
	strm.avail_out = out.size();
	
	//Compress
	lzma_ret ret = lzma_code( &strm, LZMA_FINISH );
	if( ret != LZMA_STREAM_END ){
		lzma_end( &strm ); //TODO: Use RAII
		return {};
	}
	
	lzma_end( &strm );
	
	//Return only the range actually needed
	//return out.left( out.size() - strm.avail_out );
	return out.size() - strm.avail_out;
}
Esempio n. 13
0
// don't inline, to be friendly to js engine osr
void __attribute__ ((noinline)) doit(char *buffer, int size, int i) {
  static char *buffer2 = NULL;
  static char *buffer3 = NULL;

  unsigned long maxCompressedSize = lzma_stream_buffer_bound(size);

  if (!buffer2) buffer2 = (char*)malloc(maxCompressedSize);
  if (!buffer3) buffer3 = (char*)malloc(size);

  size_t compressedSize = 0;
  int ret = lzma_easy_buffer_encode(LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC64, NULL, (const uint8_t*)buffer, size, (uint8_t*)buffer2, &compressedSize, maxCompressedSize);
  assert(ret == LZMA_OK);
  if (i == 0) printf("sizes: %d,%d\n", size, compressedSize);

  lzma_stream strm = LZMA_STREAM_INIT;
  ret = lzma_stream_decoder(&strm, UINT64_MAX, 0);
  assert(ret == LZMA_OK);
  strm.next_in = (const uint8_t*)buffer2;
  strm.avail_in = compressedSize;
  strm.next_out = (uint8_t*)buffer3;
  strm.avail_out = size;
  ret = lzma_code (&strm, LZMA_FINISH);
  assert(ret == LZMA_OK || ret == LZMA_STREAM_END);
  size_t decompressedSize = size - strm.avail_out;
  assert(decompressedSize == size);
  if (i == 0) assert(strcmp(buffer, buffer3) == 0);
}
Esempio n. 14
0
	void code(lzma_stream &stream, std::istream &in, std::ostream &out)
	{
		std::vector<char> inbuf(chunksize, 0), outbuf(chunksize, 0);
		while (true)
		{
			stream.next_in = reinterpret_cast<uint8_t *>(&inbuf[0]);
			in.read(&inbuf[0], inbuf.size());
			std::streamsize insize = in.gcount();
			stream.avail_in = insize;
			stream.total_in = 0;
			do
			{
				stream.total_out = 0;
				stream.next_out = reinterpret_cast<uint8_t *>(&outbuf[0]);
				stream.avail_out = outbuf.size();
				lzma_ret retval = lzma_code(&stream, insize == 0 ? LZMA_FINISH : LZMA_RUN);
				if (retval == LZMA_STREAM_END)
				{
					if (stream.avail_in != 0) throw compress_error{"Unprocessed input remaining in stream"};
					out.write(&outbuf[0], stream.total_out);
					lzma_end(&stream);
					return;
				}
				if (retval != LZMA_OK) throw compress_error{"Stream encoding failed"};
				//std::cout << "Retrieved " << stream.total_in << " bytes and output " << stream.total_out << " bytes\n";
				out.write(&outbuf[0], stream.total_out);
			}
			while (stream.total_out > 0);
		}
	}
Esempio n. 15
0
int xz_dec_read(lzma_stream *str, char *buf_in, int *len_in, char *buf_out, int *len_out)
{
	lzma_ret ret_xz;

	return -1; /* TODO */

    str->next_in = (uint8_t *)buf_in;
    str->avail_in = *len_in;

    while(1)
    {
    	str->next_out = (uint8_t *)buf_out;
    	str->avail_out = BUFLEN;

    	ret_xz = lzma_code(str, len_in ? LZMA_RUN : LZMA_FINISH);
    	if((ret_xz != LZMA_OK) && (ret_xz != LZMA_STREAM_END))
        {
        	fprintf(stderr, "lzma_code ret %d\n", ret_xz);
        	break;
        }

    	if(str->avail_out != 0)
        {
        	/* didn't use it all, so we're done */
        	break;
        }
    }

	return ret_xz;
}
Esempio n. 16
0
/* lzma_putc writes the character 'c', cast to an unsigned char, 
   to the lzmafile file pointer. Returns the character written as
   an unsigned char cast to an int or EOF on error. */
int lzma_putc(int c, struct lzmafile* file)
{
  int ret;
  lzma_stream* lstr = &file->str;

  unsigned char bufout[1];      /* compressed output buffer */ 
  char buf[1];

  if (file->mode != 'w') {
    fprintf(stderr,"lzma_putc error: file was not opened for writting\n");
    return EOF;
  }
  buf[0] = c;

  lstr->next_in = (void*)buf;
  lstr->avail_in = 1;
  while (lstr->avail_in) {
    lstr->next_out = bufout;
    lstr->avail_out = 1;
    ret = lzma_code(lstr, LZMA_RUN);
    if (ret != LZMA_OK) {
      fprintf(stderr,"lzma_putc error: encoding failed: %d\n",ret);
      return EOF;
    }
    ret = fwrite(bufout, 1, 1 - lstr->avail_out, file->fp);
    if (ret != 1 - lstr->avail_out) {
      fprintf(stderr,"lzma_putc error\n");
      return EOF;
    }
  }
  return (unsigned char)c;
}
Esempio n. 17
0
int xz_enc_write(lzma_stream *str, char *buf_in, uint32_t len_in, FILE *fp_out)
{
	uint8_t buf_out[BUFLEN];
	lzma_ret ret_xz;

    str->next_in = (uint8_t *)buf_in;
    str->avail_in = len_in;

    while(1)
    {
    	str->next_out = buf_out;
    	str->avail_out = BUFLEN;

    	ret_xz = lzma_code(str, len_in ? LZMA_RUN : LZMA_FINISH);
    	if((ret_xz != LZMA_OK) && (ret_xz != LZMA_STREAM_END))
        {
        	fprintf(stderr, "lzma_code ret %d\n", ret_xz);
        	break;
        }

        fwrite(buf_out, BUFLEN - str->avail_out, 1, fp_out);

    	if(str->avail_out != 0)
        {
        	/* didn't use it all, so we're done */
        	break;
        }
    }

	return ret_xz;
}
Esempio n. 18
0
/* lzma_close flushes the stream pointed to by the lzmafile pointer
   and closes the underlying file descriptor. Upon successful 
   completion 0 is returned. On error, EOF is returned. */
int lzma_close(struct lzmafile* file)
{
  int ret, outsize;
  unsigned char buf[BUF_SIZE];    /* buffer used when flushing remaining
				     output data in write mode */
  if (!file) return -1;
  if (file->mode == 'w') {
    /* flush LZMA output buffer */
    for (;;) {
      file->str.next_out = buf;
      file->str.avail_out = BUF_SIZE;
      ret = lzma_code(&file->str, LZMA_FINISH);
      if (ret != LZMA_STREAM_END && ret != LZMA_OK) {
	fprintf(stderr,"lzma_close error: encoding failed: %d\n",ret);
	lzma_end(&file->str);
	fclose(file->fp);
	free(file);
	return EOF;
      }
      outsize = BUF_SIZE - file->str.avail_out;
      if (fwrite(buf, 1, outsize, file->fp) != outsize) {
	lzma_end(&file->str);
	fclose(file->fp);
	free(file);
	return EOF;
      }
      if (ret == LZMA_STREAM_END) break;
    }
  }
  lzma_end(&file->str);
  ret = fclose(file->fp);
  free(file);
  return ret;
}
Esempio n. 19
0
static ssize_t xzwrite(XZFILE *xzfile, void *buf, size_t len)
	/*@globals fileSystem @*/
	/*@modifies xzfile, fileSystem @*/
{
    lzma_ret ret;
    size_t n;

    if (!xzfile || !xzfile->encoding)
	return -1;
    if (!len)
	return 0;
/*@-temptrans@*/
    xzfile->strm.next_in = buf;
/*@=temptrans@*/
    xzfile->strm.avail_in = len;
    for (;;) {
	xzfile->strm.next_out = (uint8_t *)xzfile->buf;
	xzfile->strm.avail_out = kBufferSize;
	ret = lzma_code(&xzfile->strm, LZMA_RUN);
	if (ret != LZMA_OK)
	    return -1;
	n = kBufferSize - xzfile->strm.avail_out;
	if (n && fwrite(xzfile->buf, 1, n, xzfile->fp) != n)
	    return -1;
	if (!xzfile->strm.avail_in)
	    return len;
    }
    /*@notreached@*/
}
Esempio n. 20
0
/*
 * Finish off an encoded strip by flushing the last
 * string and tacking on an End Of Information code.
 */
static int
LZMAPostEncode(TIFF* tif)
{
    static const char module[] = "LZMAPostEncode";
    LZMAState *sp = EncoderState(tif);
    lzma_ret ret;

    sp->stream.avail_in = 0;
    do {
        ret = lzma_code(&sp->stream, LZMA_FINISH);
        switch (ret) {
        case LZMA_STREAM_END:
        case LZMA_OK:
            if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
                tif->tif_rawcc =
                    tif->tif_rawdatasize - sp->stream.avail_out;
                TIFFFlushData1(tif);
                sp->stream.next_out = tif->tif_rawdata;
                sp->stream.avail_out = (size_t)tif->tif_rawdatasize;  /* this is a safe typecast, as check is made already in ZIPPreEncode */
            }
            break;
        default:
            TIFFErrorExt(tif->tif_clientdata, module, "Liblzma error: %s",
                     LZMAStrerror(ret));
            return 0;
        }
    } while (ret != LZMA_STREAM_END);
    return 1;
}
Esempio n. 21
0
File: rpmio.c Progetto: akozumpl/rpm
static ssize_t lzread(LZFILE *lzfile, void *buf, size_t len)
{
    lzma_ret ret;
    int eof = 0;

    if (!lzfile || lzfile->encoding)
      return -1;
    if (lzfile->eof)
      return 0;
    lzfile->strm.next_out = buf;
    lzfile->strm.avail_out = len;
    for (;;) {
	if (!lzfile->strm.avail_in) {
	    lzfile->strm.next_in = lzfile->buf;
	    lzfile->strm.avail_in = fread(lzfile->buf, 1, kBufferSize, lzfile->file);
	    if (!lzfile->strm.avail_in)
		eof = 1;
	}
	ret = lzma_code(&lzfile->strm, LZMA_RUN);
	if (ret == LZMA_STREAM_END) {
	    lzfile->eof = 1;
	    return len - lzfile->strm.avail_out;
	}
	if (ret != LZMA_OK)
	    return -1;
	if (!lzfile->strm.avail_out)
	    return len;
	if (eof)
	    return -1;
      }
}
Esempio n. 22
0
/*
 * Encode a chunk of pixels.
 */
static int
LZMAEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
{
    static const char module[] = "LZMAEncode";
    LZMAState *sp = EncoderState(tif);

    assert(sp != NULL);
    assert(sp->state == LSTATE_INIT_ENCODE);

    (void) s;
    sp->stream.next_in = bp;
    sp->stream.avail_in = (size_t) cc;
    if ((tmsize_t)sp->stream.avail_in != cc) {
        TIFFErrorExt(tif->tif_clientdata, module,
                 "Liblzma cannot deal with buffers this size");
        return 0;
    }
    do {
        lzma_ret ret = lzma_code(&sp->stream, LZMA_RUN);
        if (ret != LZMA_OK) {
            TIFFErrorExt(tif->tif_clientdata, module,
                "Encoding error at scanline %lu, %s",
                (unsigned long) tif->tif_row, LZMAStrerror(ret));
            return 0;
        }
        if (sp->stream.avail_out == 0) {
            tif->tif_rawcc = tif->tif_rawdatasize;
            TIFFFlushData1(tif);
            sp->stream.next_out = tif->tif_rawdata;
            sp->stream.avail_out = (size_t)tif->tif_rawdatasize;  /* this is a safe typecast, as check is made already in LZMAPreEncode */
        }
    } while (sp->stream.avail_in > 0);
    return 1;
}
Esempio n. 23
0
static ssize_t lzread(void *cookie, char *buf, size_t len)
{
  LZFILE *lzfile = cookie;
  lzma_ret ret;
  int eof = 0;

  if (!lzfile || lzfile->encoding)
    return -1;
  if (lzfile->eof)
    return 0;
  lzfile->strm.next_out = (unsigned char *)buf;
  lzfile->strm.avail_out = len;
  for (;;)
    {
      if (!lzfile->strm.avail_in)
	{
	  lzfile->strm.next_in = lzfile->buf;
	  lzfile->strm.avail_in = fread(lzfile->buf, 1, sizeof(lzfile->buf), lzfile->file);
	  if (!lzfile->strm.avail_in)
	    eof = 1;
	}
      ret = lzma_code(&lzfile->strm, LZMA_RUN);
      if (ret == LZMA_STREAM_END)
	{
	  lzfile->eof = 1;
	  return len - lzfile->strm.avail_out;
	}
      if (ret != LZMA_OK)
	return -1;
      if (!lzfile->strm.avail_out)
	return len;
      if (eof)
	return -1;
    }
}
Esempio n. 24
0
static ssize_t lzwrite(void *cookie, const char *buf, size_t len)
{
  LZFILE *lzfile = cookie;
  lzma_ret ret;
  size_t n;
  if (!lzfile || !lzfile->encoding)
    return -1;
  if (!len)
    return 0;
  lzfile->strm.next_in = (unsigned char *)buf;
  lzfile->strm.avail_in = len;
  for (;;)
    {
      lzfile->strm.next_out = lzfile->buf;
      lzfile->strm.avail_out = sizeof(lzfile->buf);
      ret = lzma_code(&lzfile->strm, LZMA_RUN);
      if (ret != LZMA_OK)
	return -1;
      n = sizeof(lzfile->buf) - lzfile->strm.avail_out;
      if (n && fwrite(lzfile->buf, 1, n, lzfile->file) != n)
	return -1;
      if (!lzfile->strm.avail_in)
	return len;
    }
}
Esempio n. 25
0
static int lzclose(void *cookie)
{
  LZFILE *lzfile = cookie;
  lzma_ret ret;
  size_t n;
  int rc;

  if (!lzfile)
    return -1;
  if (lzfile->encoding)
    {
      for (;;)
	{
	  lzfile->strm.avail_out = sizeof(lzfile->buf);
	  lzfile->strm.next_out = lzfile->buf;
	  ret = lzma_code(&lzfile->strm, LZMA_FINISH);
	  if (ret != LZMA_OK && ret != LZMA_STREAM_END)
	    return -1;
	  n = sizeof(lzfile->buf) - lzfile->strm.avail_out;
	  if (n && fwrite(lzfile->buf, 1, n, lzfile->file) != n)
	    return -1;
	  if (ret == LZMA_STREAM_END)
	    break;
	}
    }
  lzma_end(&lzfile->strm);
  rc = fclose(lzfile->file);
  free(lzfile);
  return rc;
}
Esempio n. 26
0
bool lzma_filter_process(struct lzma_filter *lf, const void *buf, size_t len, struct evbuffer *output) {
	lzma_stream l_s = lf->l_s;
	l_s.next_in = buf;
	l_s.avail_in = len;

	uint8_t target_buf[512];

	lzma_ret l_r;
	do {
		l_s.next_out = target_buf;
		l_s.avail_out = sizeof(target_buf);
		l_r = lzma_code(&l_s, len == 0 ? LZMA_FINISH : LZMA_RUN);
		if ((l_r == LZMA_STREAM_END) || (l_r == LZMA_OK)) {
			if (evbuffer_add(output, target_buf, sizeof(target_buf) - l_s.avail_out) != 0) {
				_lzma_filter_error_cb_call(lf, "evbuffer_add %d", l_r);
				return false;
			}
		}
		else {
			_lzma_filter_error_cb_call(lf, "lzma error: %d", l_r);
			return false;
		}
	} while (((len == 0) && (l_r != LZMA_STREAM_END)) || (l_s.avail_in > 0));

	return true;
}
Esempio n. 27
0
int decompress_blob_xz(const void *src, uint64_t src_size,
                       void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max) {

#ifdef HAVE_XZ
        _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
        lzma_ret ret;
        size_t space;

        assert(src);
        assert(src_size > 0);
        assert(dst);
        assert(dst_alloc_size);
        assert(dst_size);
        assert(*dst_alloc_size == 0 || *dst);

        ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
        if (ret != LZMA_OK)
                return -ENOMEM;

        space = MIN(src_size * 2, dst_max ?: (size_t) -1);
        if (!greedy_realloc(dst, dst_alloc_size, space, 1))
                return -ENOMEM;

        s.next_in = src;
        s.avail_in = src_size;

        s.next_out = *dst;
        s.avail_out = space;

        for (;;) {
                size_t used;

                ret = lzma_code(&s, LZMA_FINISH);

                if (ret == LZMA_STREAM_END)
                        break;
                else if (ret != LZMA_OK)
                        return -ENOMEM;

                if (dst_max > 0 && (space - s.avail_out) >= dst_max)
                        break;
                else if (dst_max > 0 && space == dst_max)
                        return -ENOBUFS;

                used = space - s.avail_out;
                space = MIN(2 * space, dst_max ?: (size_t) -1);
                if (!greedy_realloc(dst, dst_alloc_size, space, 1))
                        return -ENOMEM;

                s.avail_out = space - used;
                s.next_out = *dst + used;
        }

        *dst_size = space - s.avail_out;
        return 0;
#else
        return -EPROTONOSUPPORT;
#endif
}
Esempio n. 28
0
/*
 * Utility function to push input data through compressor,
 * writing full output blocks as necessary.
 *
 * Note that this handles both the regular write case (finishing ==
 * false) and the end-of-archive case (finishing == true).
 */
static int
drive_compressor(struct archive_write_filter *f,
    struct private_data *data, int finishing)
{
	int ret;

	for (;;) {
		if (data->stream.avail_out == 0) {
			data->total_out += data->compressed_buffer_size;
			ret = __archive_write_filter(f->next_filter,
			    data->compressed,
			    data->compressed_buffer_size);
			if (ret != ARCHIVE_OK)
				return (ARCHIVE_FATAL);
			data->stream.next_out = data->compressed;
			data->stream.avail_out = data->compressed_buffer_size;
		}

		/* If there's nothing to do, we're done. */
		if (!finishing && data->stream.avail_in == 0)
			return (ARCHIVE_OK);

		ret = lzma_code(&(data->stream),
		    finishing ? LZMA_FINISH : LZMA_RUN );

		switch (ret) {
		case LZMA_OK:
			/* In non-finishing case, check if compressor
			 * consumed everything */
			if (!finishing && data->stream.avail_in == 0)
				return (ARCHIVE_OK);
			/* In finishing case, this return always means
			 * there's more work */
			break;
		case LZMA_STREAM_END:
			/* This return can only occur in finishing case. */
			if (finishing)
				return (ARCHIVE_OK);
			archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
			    "lzma compression data error");
			return (ARCHIVE_FATAL);
		case LZMA_MEMLIMIT_ERROR:
			archive_set_error(f->archive, ENOMEM,
			    "lzma compression error: "
			    "%ju MiB would have been needed",
			    (uintmax_t)((lzma_memusage(&(data->stream))
				    + 1024 * 1024 -1)
				/ (1024 * 1024)));
			return (ARCHIVE_FATAL);
		default:
			/* Any other return value indicates an error. */
			archive_set_error(f->archive, ARCHIVE_ERRNO_MISC,
			    "lzma compression failed:"
			    " lzma_code() call returned status %d",
			    ret);
			return (ARCHIVE_FATAL);
		}
	}
}
Esempio n. 29
0
int decompress_startswith_xz(const void *src, uint64_t src_size,
                             void **buffer, size_t *buffer_size,
                             const void *prefix, size_t prefix_len,
                             uint8_t extra) {

#ifdef HAVE_XZ
        _cleanup_(lzma_end) lzma_stream s = LZMA_STREAM_INIT;
        lzma_ret ret;

        /* Checks whether the decompressed blob starts with the
         * mentioned prefix. The byte extra needs to follow the
         * prefix */

        assert(src);
        assert(src_size > 0);
        assert(buffer);
        assert(buffer_size);
        assert(prefix);
        assert(*buffer_size == 0 || *buffer);

        ret = lzma_stream_decoder(&s, UINT64_MAX, 0);
        if (ret != LZMA_OK)
                return -EBADMSG;

        if (!(greedy_realloc(buffer, buffer_size, ALIGN_8(prefix_len + 1), 1)))
                return -ENOMEM;

        s.next_in = src;
        s.avail_in = src_size;

        s.next_out = *buffer;
        s.avail_out = *buffer_size;

        for (;;) {
                ret = lzma_code(&s, LZMA_FINISH);

                if (ret != LZMA_STREAM_END && ret != LZMA_OK)
                        return -EBADMSG;

                if (*buffer_size - s.avail_out >= prefix_len + 1)
                        return memcmp(*buffer, prefix, prefix_len) == 0 &&
                                ((const uint8_t*) *buffer)[prefix_len] == extra;

                if (ret == LZMA_STREAM_END)
                        return 0;

                s.avail_out += *buffer_size;

                if (!(greedy_realloc(buffer, buffer_size, *buffer_size * 2, 1)))
                        return -ENOMEM;

                s.next_out = *buffer + *buffer_size - s.avail_out;
        }

#else
        return -EPROTONOSUPPORT;
#endif
}
Esempio n. 30
0
void lzma_base::reset( bool compress, bool realloc )
{
	lzma_stream *s = static_cast<lzma_stream *>( stream_ );

	if ( realloc )
		lzma_error::check( lzma_code( s, lzma::full_flush ) );
	else
		lzma_end( s );
}