Exemplo n.º 1
0
unsigned long ulzma(u8 *src, u8 *dst)
{
	unsigned char properties[LZMA_PROPERTIES_SIZE];
	UInt32 outSize;
	SizeT inProcessed;
	SizeT outProcessed;
	int res;
	CLzmaDecoderState state;
	SizeT mallocneeds;
	unsigned char scratchpad[15980];

	memcpy(properties, src, LZMA_PROPERTIES_SIZE);
	outSize = *(UInt32 *)(src + LZMA_PROPERTIES_SIZE);
	if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
		printf("Incorrect stream properties\n");
	}
	mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
	if (mallocneeds > 15980) {
		printf("Decoder scratchpad too small!\n");
	}
	state.Probs = (CProb *)scratchpad;
	res = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, (SizeT)0xffffffff, &inProcessed,
		dst, outSize, &outProcessed);
	if (res != 0) {
		printf("Decoding error = %d\n", res);
	}
	return outSize;
}
Exemplo n.º 2
0
int lzma_init(gzFile infile, struct LZMAFile **lzmaFile)
{
  LZMAFile *inBuffer;

  g_totalRead = 0;
  
  *lzmaFile = inBuffer = (LZMAFile *)malloc(sizeof(struct LZMAFile));
  if (inBuffer == NULL) return -1;

  memset(inBuffer, 0, sizeof(LZMAFile));
  inBuffer->File = infile;
  inBuffer->InCallback.Read = LzmaReadCompressed;
  inBuffer->waitEOS = 1;


  /* Read LZMA properties for compressed stream */
  if (!MyReadFileAndCheck(infile, inBuffer->properties, LZMA_PROPERTIES_SIZE))
    return -1;

  /* Read uncompressed size */
  {
    int i;
    for (i = 0; i < 8; i++)
    {
      unsigned char b;
      if (!MyReadFileAndCheck(infile, &b, 1))
        return -1;
      if (b != 0xFF)
        inBuffer->waitEOS = 0;
      if (i < 4)
        inBuffer->outSize += (UInt32)(b) << (i * 8);
      else
        inBuffer->outSizeHigh += (UInt32)(b) << ((i - 4) * 8);
    }
  }

  /* Decode LZMA properties and allocate memory */
  if (LzmaDecodeProperties(&(inBuffer->state.Properties), inBuffer->properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK)
  {
    PrintMessage("Incorrect stream properties");
    return -1;
  }
  inBuffer->state.Probs = (CProb *)malloc(LzmaGetNumProbs(&(inBuffer->state.Properties)) * sizeof(CProb));
  if (inBuffer->state.Probs == NULL) return -1;
  inBuffer->state.Dictionary = (unsigned char *)malloc(inBuffer->state.Properties.DictionarySize);
  if (inBuffer->state.Dictionary == NULL) return -1;

  LzmaDecoderInit(&(inBuffer->state));

  return 0;
}
Exemplo n.º 3
0
int LzmaRamDecompress(
    const unsigned char *inBuffer, 
    size_t inSize,
    unsigned char *outBuffer,
    size_t outSize,
    size_t *outSizeProcessed,
    void * (*allocFunc)(size_t size), 
    void (*freeFunc)(void *))
{
  CLzmaDecoderState state;  /* it's about 24 bytes structure, if int is 32-bit */
  int result;
  SizeT outSizeProcessedLoc;
  SizeT inProcessed;
  int useFilter;
  
  if (inSize < LZMA_PROPS_SIZE)
    return 1;
  useFilter = inBuffer[0];

  *outSizeProcessed = 0;
  if (useFilter > 1)
    return 1;

  if (LzmaDecodeProperties(&state.Properties, inBuffer + 1, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK)
    return 1;
  state.Probs = (CProb *)allocFunc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
  if (state.Probs == 0)
    return SZE_OUTOFMEMORY;
  
  result = LzmaDecode(&state,
    inBuffer + LZMA_PROPS_SIZE, (SizeT)inSize - LZMA_PROPS_SIZE, &inProcessed,
    outBuffer, (SizeT)outSize, &outSizeProcessedLoc);
  freeFunc(state.Probs);
  if (result != LZMA_RESULT_OK)
    return 1;
  *outSizeProcessed = (size_t)outSizeProcessedLoc;
  if (useFilter == 1)
  {
    UInt32 _prevMask;
    UInt32 _prevPos;
    x86_Convert_Init(_prevMask, _prevPos);
    x86_Convert(outBuffer, (UInt32)outSizeProcessedLoc, 0, &_prevMask, &_prevPos, 0);
  }
  return 0;
}
Exemplo n.º 4
0
unsigned long ulzma(unsigned char * src, unsigned char * dst)
{
	unsigned char properties[LZMA_PROPERTIES_SIZE];
	UInt32 outSize;
	SizeT inProcessed;
	SizeT outProcessed;
	int res;
	CLzmaDecoderState state;
	SizeT mallocneeds;
#if !defined(__PRE_RAM__)
	/* in ramstage, this can go in BSS */
	static
#endif
	/* in pre-ram, it must go on the stack */
	unsigned char scratchpad[15980];
	unsigned char *cp;

	memcpy(properties, src, LZMA_PROPERTIES_SIZE);
	/* The outSize in LZMA stream is a 64bit integer stored in little-endian
	 * (ref: lzma.cc@LZMACompress: put_64). To prevent accessing by
	 * unaligned memory address and to load in correct endianess, read each
	 * byte and re-costruct. */
	cp = src + LZMA_PROPERTIES_SIZE;
	outSize = cp[3] << 24 | cp[2] << 16 | cp[1] << 8 | cp[0];
	if (LzmaDecodeProperties(&state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
		printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n");
		return 0;
	}
	mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
	if (mallocneeds > 15980) {
		printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n");
		return 0;
	}
	state.Probs = (CProb *)scratchpad;
	res = LzmaDecode(&state, src + LZMA_PROPERTIES_SIZE + 8, (SizeT)0xffffffff, &inProcessed,
		dst, outSize, &outProcessed);
	if (res != 0) {
		printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res);
		return 0;
	}
	return outSize;
}
Exemplo n.º 5
0
size_t ulzman(const void *src, size_t srcn, void *dst, size_t dstn)
{
    unsigned char properties[LZMA_PROPERTIES_SIZE];
    const int data_offset = LZMA_PROPERTIES_SIZE + 8;
    UInt32 outSize;
    SizeT inProcessed;
    SizeT outProcessed;
    int res;
    CLzmaDecoderState state;
    SizeT mallocneeds;
    MAYBE_STATIC unsigned char scratchpad[15980];
    const unsigned char *cp;

    /* Note: these timestamps aren't useful for memory-mapped media (x86) */
    timestamp_add_now(TS_START_ULZMA);
    memcpy(properties, src, LZMA_PROPERTIES_SIZE);
    /* The outSize in LZMA stream is a 64bit integer stored in little-endian
     * (ref: lzma.cc@LZMACompress: put_64). To prevent accessing by
     * unaligned memory address and to load in correct endianness, read each
     * byte and re-construct. */
    cp = src + LZMA_PROPERTIES_SIZE;
    outSize = cp[3] << 24 | cp[2] << 16 | cp[1] << 8 | cp[0];
    if (LzmaDecodeProperties(&state.Properties, properties,
                             LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
        printk(BIOS_WARNING, "lzma: Incorrect stream properties.\n");
        return 0;
    }
    mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
    if (mallocneeds > 15980) {
        printk(BIOS_WARNING, "lzma: Decoder scratchpad too small!\n");
        return 0;
    }
    state.Probs = (CProb *)scratchpad;
    res = LzmaDecode(&state, src + data_offset, srcn - data_offset,
                     &inProcessed, dst, outSize, &outProcessed);
    if (res != 0) {
        printk(BIOS_WARNING, "lzma: Decoding error = %d\n", res);
        return 0;
    }
    timestamp_add_now(TS_END_ULZMA);
    return outProcessed;
}
Exemplo n.º 6
0
void *
lzma_decoder_create(I7z_stream *st, unsigned char *prop, unsigned int propsize)
{
  ReadStream *rs;
  LZMA_Decoder *dec;

  if ((dec = calloc(1, sizeof(*dec))) == NULL) {
    err_message_fnc("No enough memory.\n");
    return NULL;
  }

  /* Decode LZMA properties and allocate memory */
  if (LzmaDecodeProperties(&dec->state.Properties, prop, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
    err_message_fnc("Incorrect stream properties.\n");
    return NULL;
  }
  if ((dec->state.Probs = (CProb *)malloc(LzmaGetNumProbs(&dec->state.Properties) * sizeof(CProb))) == NULL) {
    free(dec);
    err_message_fnc("No enough memory.\n");
    return NULL;
  }
  if ((dec->state.Dictionary = (unsigned char *)malloc(dec->state.Properties.DictionarySize)) == NULL) {
    free(dec->state.Probs);
    free(dec);
    err_message_fnc("No enough memory.\n");
    return NULL;
  }

  if ((rs = calloc(1, sizeof(*rs))) == NULL) {
    err_message_fnc("No enough memory.\n");
    return NULL;
  }
  rs->InCallback.Read = read_stream;
  rs->st = st;

  LzmaDecoderInit(&dec->state);

  dec->offset = 0;
  dec->rs = rs;

  return dec;
}
Exemplo n.º 7
0
int LzmaMyDecodeProperties(CLzmaDecoderState *vs, int vsSize,
  const unsigned char *propsData, int propsDataSize, UInt32 *outProbsSize,
  UInt32 *outDictionarySize)
{
  int retval;

  /*
    First verify that the state structure passed by the caller is the
    correct size.
  */
  if (sizeof(*vs) != vsSize)
    return LZMA_RESULT_DATA_ERROR;   /* for lack of a better error code */

  retval = LzmaDecodeProperties(&vs->Properties, propsData, propsDataSize);
  if (retval == LZMA_RESULT_OK)
  {
    *outProbsSize = LzmaGetNumProbs(&vs->Properties) * sizeof(CProb);
    *outDictionarySize = vs->Properties.DictionarySize;
  }
  return retval;
}
Exemplo n.º 8
0
unsigned long ulzman(const unsigned char *src, unsigned long srcn,
		     unsigned char *dst, unsigned long dstn)
{
	unsigned char properties[LZMA_PROPERTIES_SIZE];
	const int data_offset = LZMA_PROPERTIES_SIZE + 8;
	UInt32 outSize;
	SizeT inProcessed;
	SizeT outProcessed;
	int res;
	CLzmaDecoderState state;
	SizeT mallocneeds;
	unsigned char *scratchpad;

	memcpy(properties, src, LZMA_PROPERTIES_SIZE);
	memcpy(&outSize, src + LZMA_PROPERTIES_SIZE, sizeof(outSize));
	if (outSize > dstn)
		outSize = dstn;
	if (LzmaDecodeProperties(&state.Properties, properties,
				 LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK) {
		printf("lzma: Incorrect stream properties.\n");
		return 0;
	}
	mallocneeds = (LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
	scratchpad = malloc(mallocneeds);
	if (!scratchpad) {
		printf("lzma: Cannot allocate %u bytes for scratchpad!\n",
		       mallocneeds);
		return 0;
	}
	state.Probs = (CProb *)scratchpad;
	res = LzmaDecode(&state, src + data_offset, srcn - data_offset,
			 &inProcessed, dst, outSize, &outProcessed);
	free(scratchpad);
	if (res != 0) {
		printf("lzma: Decoding error = %d\n", res);
		return 0;
	}
	return outProcessed;
}
Exemplo n.º 9
0
int cli_LzmaInit(CLI_LZMA **Lp, uint64_t size_override) {
  CLI_LZMA *L = *Lp;

  if(!L) {
	  *Lp = L = cli_calloc(sizeof(*L), 1);
	  if(!L) {
		  return CL_EMEM;
	  }
  }

  L->initted = 0;
  if(size_override) L->usize=size_override;

  if (!L->next_in || L->avail_in < LZMA_PROPERTIES_SIZE + 8) return LZMA_RESULT_OK;
  if (LzmaDecodeProperties(&L->state.Properties, L->next_in, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK)
    return LZMA_RESULT_DATA_ERROR;

  L->next_in += LZMA_PROPERTIES_SIZE;
  L->avail_in -= LZMA_PROPERTIES_SIZE;

  if (!L->usize) {
    L->usize=(uint64_t)cli_readint32(L->next_in) + ((uint64_t)cli_readint32(L->next_in+4)<<32);
    L->next_in += 8;
    L->avail_in -= 8;
  }
    
  if (!(L->state.Probs = (CProb *)cli_malloc(LzmaGetNumProbs(&L->state.Properties) * sizeof(CProb))))
    return LZMA_RESULT_DATA_ERROR;

  if (!(L->state.Dictionary = (unsigned char *)cli_malloc(L->state.Properties.DictionarySize))) {
    free(L->state.Probs);
    return LZMA_RESULT_DATA_ERROR;
  }

  L->initted = 1;

  LzmaDecoderInit(&L->state);
  return LZMA_RESULT_OK;
}
Exemplo n.º 10
0
static int lzma_init_props(void)
{
	unsigned char props[LZMA_PROPERTIES_SIZE];
	int res;
	int i;

	/* read lzma properties */
	for (i = 0; i < LZMA_PROPERTIES_SIZE; i++)
		props[i] = lzma_get_byte();

	/* read the lower half of uncompressed size in the header */
	lzma_outsize = ((SizeT) lzma_get_byte()) +
		       ((SizeT) lzma_get_byte() << 8) +
		       ((SizeT) lzma_get_byte() << 16) +
		       ((SizeT) lzma_get_byte() << 24);

	/* skip rest of the header (upper half of uncompressed size) */
	for (i = 0; i < 4; i++)
		lzma_get_byte();

	res = LzmaDecodeProperties(&lzma_state.Properties, props,
					LZMA_PROPERTIES_SIZE);
	return res;
}
Exemplo n.º 11
0
SZ_RESULT SzDecode(const CFileSize *packSizes, const CFolder *folder,
    #ifdef _LZMA_IN_CB
    ISzInStream *inStream,
    #else
    const Byte *inBuffer,
    #endif
    Byte *outBuffer, size_t outSize,
    size_t *outSizeProcessed, ISzAlloc *allocMain)
{
  UInt32 si;
  size_t inSize = 0;
  CCoderInfo *coder;
  if (folder->NumPackStreams != 1)
    return SZE_NOTIMPL;
  if (folder->NumCoders != 1)
    return SZE_NOTIMPL;
  coder = folder->Coders;
  *outSizeProcessed = 0;

  for (si = 0; si < folder->NumPackStreams; si++)
    inSize += (size_t)packSizes[si];

  if (AreMethodsEqual(&coder->MethodID, &k_Copy))
  {
    size_t i;
    if (inSize != outSize)
      return SZE_DATA_ERROR;
    #ifdef _LZMA_IN_CB
    for (i = 0; i < inSize;)
    {
      size_t j;
      Byte *inBuffer;
      size_t bufferSize;
      RINOK(inStream->Read((void *)inStream,  (void **)&inBuffer, inSize - i, &bufferSize));
      if (bufferSize == 0)
        return SZE_DATA_ERROR;
      if (bufferSize > inSize - i)
        return SZE_FAIL;
      *outSizeProcessed += bufferSize;
      for (j = 0; j < bufferSize && i < inSize; j++, i++)
        outBuffer[i] = inBuffer[j];
    }
    #else
    for (i = 0; i < inSize; i++)
      outBuffer[i] = inBuffer[i];
    *outSizeProcessed = inSize;
    #endif
    return SZ_OK;
  }

  if (AreMethodsEqual(&coder->MethodID, &k_LZMA))
  {
    #ifdef _LZMA_IN_CB
    CLzmaInCallbackImp lzmaCallback;
    #else
    SizeT inProcessed;
    #endif

    CLzmaDecoderState state;  /* it's about 24-80 bytes structure, if int is 32-bit */
    int result;
    SizeT outSizeProcessedLoc;

    #ifdef _LZMA_IN_CB
    lzmaCallback.Size = inSize;
    lzmaCallback.InStream = inStream;
    lzmaCallback.InCallback.Read = LzmaReadImp;
    #endif

    if (LzmaDecodeProperties(&state.Properties, coder->Properties.Items,
        coder->Properties.Capacity) != LZMA_RESULT_OK)
      return SZE_FAIL;

    state.Probs = (CProb *)allocMain->Alloc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
    if (state.Probs == 0)
      return SZE_OUTOFMEMORY;

    #ifdef _LZMA_OUT_READ
    if (state.Properties.DictionarySize == 0)
      state.Dictionary = 0;
    else
    {
      state.Dictionary = (unsigned char *)allocMain->Alloc(state.Properties.DictionarySize);
      if (state.Dictionary == 0)
      {
        allocMain->Free(state.Probs);
        return SZE_OUTOFMEMORY;
      }
    }
    LzmaDecoderInit(&state);
    #endif

    result = LzmaDecode(&state,
        #ifdef _LZMA_IN_CB
        &lzmaCallback.InCallback,
        #else
        inBuffer, (SizeT)inSize, &inProcessed,
        #endif
        outBuffer, (SizeT)outSize, &outSizeProcessedLoc);
    *outSizeProcessed = (size_t)outSizeProcessedLoc;
    allocMain->Free(state.Probs);
    #ifdef _LZMA_OUT_READ
    allocMain->Free(state.Dictionary);
    #endif
    if (result == LZMA_RESULT_DATA_ERROR)
      return SZE_DATA_ERROR;
    if (result != LZMA_RESULT_OK)
      return SZE_FAIL;
    return SZ_OK;
  }
  return SZE_NOTIMPL;
}
static int LzmaUncompress(struct sqlzma_un *un)
{
	int err, i, ret;
	SizeT outSize, inProcessed, outProcessed, srclen;
	/* it's about 24-80 bytes structure, if int is 32-bit */
	CLzmaDecoderState state;
	unsigned char *dst, *src, a[8];
	struct sized_buf *sbuf;

	/* Decode LZMA properties and allocate memory */
	err = -EINVAL;
	src = (void *)un->un_cmbuf;
	ret = LzmaDecodeProperties(&state.Properties, src,
				   LZMA_PROPERTIES_SIZE);
	src += LZMA_PROPERTIES_SIZE;
	if (unlikely(ret != LZMA_RESULT_OK))
		goto out;
	i = LzmaGetNumProbs(&state.Properties);
	if (unlikely(i <= 0))
		i = 1;
	i *= sizeof(CProb);
	sbuf = un->un_a + SQUN_PROB;
	if (unlikely(sbuf->sz < i)) {
		if (sbuf->buf && sbuf->buf != un->un_prob)
			kfree(sbuf->buf);
#ifdef __KERNEL__
		printk("%s:%d: %d --> %d\n", __func__, __LINE__, sbuf->sz, i);
#else
		printf("%d --> %d\n", sbuf->sz, i);
#endif
		err = -ENOMEM;
		sbuf->sz = 0;
		sbuf->buf = kmalloc(i, GFP_ATOMIC);
		if (unlikely(!sbuf->buf))
			goto out;
		sbuf->sz = i;
	}
	state.Probs = (void *)sbuf->buf;

	/* Read uncompressed size */
	memcpy(a, src, sizeof(a));
	src += sizeof(a);
	outSize = a[0] | (a[1] << 8) | (a[2] << 16) | (a[3] << 24);

	err = -EINVAL;
	dst = un->un_resbuf;
	if (unlikely(!dst || outSize > un->un_reslen))
		goto out;
	un->un_reslen = outSize;
	srclen = un->un_cmlen - (src - un->un_cmbuf);

	/* Decompress */
	err = LzmaDecode(&state, src, srclen, &inProcessed, dst, outSize,
			 &outProcessed);
	if (unlikely(err))
		err = -EINVAL;

 out:
#ifndef __KERNEL__
	if (err)
		fprintf(stderr, "err %d\n", err);
#endif
	return err;
}
Exemplo n.º 13
0
SZ_RESULT SzDecodeLzma(CCoderInfo *coder, CFileSize inSize,
    #ifdef _LZMA_IN_CB
    ISzInStream *inStream,
    #else
    const Byte *inBuffer,
    #endif
    Byte *outBuffer, size_t outSize, ISzAlloc *allocMain)
{
  #ifdef _LZMA_IN_CB
  CLzmaInCallbackImp lzmaCallback;
  #else
  SizeT inProcessed;
  #endif
  
  CLzmaDecoderState state;  /* it's about 24-80 bytes structure, if int is 32-bit */
  int result;
  SizeT outSizeProcessedLoc;
  
  #ifdef _LZMA_IN_CB
  lzmaCallback.Size = inSize;
  lzmaCallback.InStream = inStream;
  lzmaCallback.InCallback.Read = LzmaReadImp;
  #endif
  
  if (LzmaDecodeProperties(&state.Properties, coder->Properties.Items, 
      (unsigned)coder->Properties.Capacity) != LZMA_RESULT_OK)
    return SZE_FAIL;
  
  state.Probs = (CProb *)allocMain->Alloc(LzmaGetNumProbs(&state.Properties) * sizeof(CProb));
  if (state.Probs == 0)
    return SZE_OUTOFMEMORY;
  
  #ifdef _LZMA_OUT_READ
  if (state.Properties.DictionarySize == 0)
    state.Dictionary = 0;
  else
  {
    state.Dictionary = (unsigned char *)allocMain->Alloc(state.Properties.DictionarySize);
    if (state.Dictionary == 0)
    {
      allocMain->Free(state.Probs);
      return SZE_OUTOFMEMORY;
    }
  }
  LzmaDecoderInit(&state);
  #endif
  
  result = LzmaDecode(&state,
  #ifdef _LZMA_IN_CB
    &lzmaCallback.InCallback,
  #else
    inBuffer, (SizeT)inSize, &inProcessed,
  #endif
    outBuffer, (SizeT)outSize, &outSizeProcessedLoc);
  allocMain->Free(state.Probs);
  #ifdef _LZMA_OUT_READ
  allocMain->Free(state.Dictionary);
  #endif
  if (result == LZMA_RESULT_DATA_ERROR)
    return SZE_DATA_ERROR;
  if (result != LZMA_RESULT_OK)
    return SZE_FAIL;
  return (outSizeProcessedLoc == outSize) ? SZ_OK : SZE_DATA_ERROR;
}
Exemplo n.º 14
0
static PyObject *pylzma_decomp_decompress(CDecompressionObject *self, PyObject *args)
{
    PyObject *result=NULL;
    unsigned char *data, *next_in, *next_out;
    int length, start_total_out, res, max_length=BLOCK_SIZE;
    SizeT avail_in, avail_out;
    unsigned char properties[LZMA_PROPERTIES_SIZE];
    SizeT inProcessed, outProcessed;
    
    if (!PyArg_ParseTuple(args, "s#|l", &data, &length, &max_length))
        return NULL;

    if (max_length <= 0)
    {
        PyErr_SetString(PyExc_ValueError, "bufsize must be greater than zero");
        return NULL;
    }
    
    start_total_out = self->total_out;
    if (self->unconsumed_length > 0) {
        self->unconsumed_tail = (unsigned char *)realloc(self->unconsumed_tail, self->unconsumed_length + length);
        next_in = (unsigned char *)self->unconsumed_tail;
        memcpy(next_in + self->unconsumed_length, data, length);
    } else
        next_in = data;
    
    avail_in = self->unconsumed_length + length;
    
    if (self->need_properties && avail_in < sizeof(properties)) {
        // we need enough bytes to read the properties
        if (!self->unconsumed_length) {
            self->unconsumed_tail = (unsigned char *)malloc(length);
            memcpy(self->unconsumed_tail, data, length);
        }
        self->unconsumed_length += length;
        
        return PyString_FromString("");
    }

    if (self->need_properties) {
        self->need_properties = 0;
        memcpy(&properties, next_in, sizeof(properties));
        avail_in -= sizeof(properties);
        next_in += sizeof(properties);
        if (self->unconsumed_length >= sizeof(properties)-length) {
            self->unconsumed_length -= sizeof(properties)-length;
            if (self->unconsumed_length > 0) {
                memcpy(self->unconsumed_tail, self->unconsumed_tail+sizeof(properties), self->unconsumed_length);
                self->unconsumed_tail = (unsigned char *)realloc(self->unconsumed_tail, self->unconsumed_length);
            } else
                FREE_AND_NULL(self->unconsumed_tail);
        }
        
        if (LzmaDecodeProperties(&self->state.Properties, properties, LZMA_PROPERTIES_SIZE) != LZMA_RESULT_OK)
        {
            PyErr_SetString(PyExc_TypeError, "Incorrect stream properties");
            goto exit;
        }
        
        self->state.Probs = (CProb *)malloc(LzmaGetNumProbs(&self->state.Properties) * sizeof(CProb));
        if (self->state.Probs == 0) {
            PyErr_NoMemory();
            goto exit;
        }
        
        if (self->state.Properties.DictionarySize == 0)
            self->state.Dictionary = 0;
        else {
            self->state.Dictionary = (unsigned char *)malloc(self->state.Properties.DictionarySize);
            if (self->state.Dictionary == 0) {
                free(self->state.Probs);
                self->state.Probs = NULL;
                PyErr_NoMemory();
                goto exit;
            }
        }
    
        LzmaDecoderInit(&self->state);
    }

    if (avail_in == 0)
        // no more bytes to decompress
        return PyString_FromString("");
    
    if (!(result = PyString_FromStringAndSize(NULL, max_length)))
        return NULL;
    
    next_out = (unsigned char *)PyString_AS_STRING(result);
    avail_out = max_length;
    
    Py_BEGIN_ALLOW_THREADS
    // Decompress until EOS marker is reached
    res = LzmaDecode(&self->state, next_in, avail_in, &inProcessed,
                     next_out, avail_out, &outProcessed, 0);
    Py_END_ALLOW_THREADS
    self->total_out += outProcessed;
    next_in += inProcessed;
    avail_in -= inProcessed;
    next_out += outProcessed;
    avail_out -= outProcessed;
    
    if (res != LZMA_RESULT_OK) {
        PyErr_SetString(PyExc_ValueError, "data error during decompression");
        DEC_AND_NULL(result);
        goto exit;
    }

    /* Not all of the compressed data could be accomodated in the output buffer
    of specified size. Return the unconsumed tail in an attribute.*/
    if (avail_in > 0)
    {
        if (avail_in != self->unconsumed_length) {
            if (avail_in > self->unconsumed_length) {
                self->unconsumed_tail = (unsigned char *)realloc(self->unconsumed_tail, avail_in);
                memcpy(self->unconsumed_tail, next_in, avail_in);
            }
            if (avail_in < self->unconsumed_length) {
                memcpy(self->unconsumed_tail, next_in, avail_in);
                self->unconsumed_tail = (unsigned char *)realloc(self->unconsumed_tail, avail_in);
            }
        }
        
        if (!self->unconsumed_tail) {
            PyErr_NoMemory();
            DEC_AND_NULL(result);
            goto exit;
        }
    } else
        FREE_AND_NULL(self->unconsumed_tail);
    
    self->unconsumed_length = avail_in;

    _PyString_Resize(&result, self->total_out - start_total_out);
    
exit:
    return result;    
}
Exemplo n.º 15
0
/* should be the first function */
void decompress_entry(unsigned long reg_a0, unsigned long reg_a1,
	unsigned long reg_a2, unsigned long reg_a3,
	unsigned long icache_size, unsigned long icache_lsize,
	unsigned long dcache_size, unsigned long dcache_lsize)
{
	unsigned char props[LZMA_PROPERTIES_SIZE];
	unsigned int i;  /* temp value */
	SizeT osize; /* uncompressed size */
	int res;

	board_init();

	printf("\n\nLZMA loader for " CONFIG_BOARD_NAME
			", Copyright (C) 2007-2008 OpenWrt.org\n\n");

	decompress_init();

	/* lzma args */
	for (i = 0; i < LZMA_PROPERTIES_SIZE; i++)
		props[i] = get_byte();

	/* skip rest of the LZMA coder property */
	/* read the lower half of uncompressed size in the header */
	osize = ((SizeT)get_byte()) +
		((SizeT)get_byte() << 8) +
		((SizeT)get_byte() << 16) +
		((SizeT)get_byte() << 24);

	/* skip rest of the header (upper half of uncompressed size) */
	for (i = 0; i < 4; i++)
		get_byte();

	res = LzmaDecodeProperties(&lzma_state.Properties, props,
					LZMA_PROPERTIES_SIZE);
	if (res != LZMA_RESULT_OK) {
		printf("Incorrect LZMA stream properties!\n");
		halt();
	}

	printf("decompressing kernel... ");

	lzma_state.Probs = (CProb *)workspace;
	res = decompress_data(&lzma_state, (unsigned char *)LOADADDR, osize);

	if (res != LZMA_RESULT_OK) {
		printf("failed, ");
		switch (res) {
		case LZMA_RESULT_DATA_ERROR:
			printf("data error!\n");
			break;
		default:
			printf("unknown error %d!\n", res);
		}
		halt();
	} else
		printf("done!\n");

	blast_dcache(dcache_size, dcache_lsize);
	blast_icache(icache_size, icache_lsize);

	printf("launching kernel...\n\n");

#ifdef CONFIG_PASS_KARGS
	reg_a0 = 0;
	reg_a1 = 0;
	reg_a2 = (unsigned long)env_vars;
	reg_a3 = 0;
#endif
	/* Jump to load address */
	((kernel_entry) LOADADDR)(reg_a0, reg_a1, reg_a2, reg_a3);
}