Exemple #1
0
void LzmaMyDecoderInit(CLzmaDecoderState *vs, void *probsPtr,
  void *dictionaryPtr)
{
  vs->Probs = (CProb*)probsPtr;
  vs->Dictionary = (unsigned char*)dictionaryPtr;
  LzmaDecoderInit(vs);
}
Exemple #2
0
int cli_LzmaInitUPX(CLI_LZMA **Lp, uint32_t dictsz) {
  CLI_LZMA *L = *Lp;

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

  L->state.Properties.pb = 2; /* FIXME: these  */
  L->state.Properties.lp = 0; /* values may    */
  L->state.Properties.lc = 3; /* not be static */

  L->state.Properties.DictionarySize = dictsz;

  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;
}
Exemple #3
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;
}
Exemple #4
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;
}
Exemple #5
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;
}
Exemple #6
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;
}
Exemple #7
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;
}
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;    
}
Exemple #9
0
int un7zip(unsigned char *in, unsigned int in_size, 
           unsigned char *buf, unsigned int buf_size, 
           unsigned char *out, unsigned int out_size, UN7ZIP_OUT_CALLBACK outCallback)
{
    unsigned int lzmaInternalSize, dictSize;
    int lc, lp, pb;
    unsigned int nowPos, unzipSize, outSizeProcessed;
    int ret;

    // 1) Read first byte of properties for LZMA compressed stream,
    // check that it has correct value and calculate three
    // LZMA property variables:
    unsigned char prop0 = in[0];
    if(prop0 >= ( 9 * 5 * 5))
    {
        //PRINTF("properties error\n");
        return LZMA_RESULT_DATA_ERROR;
    }

    for(pb = 0; prop0 >= (9 * 5); pb++, prop0 -= (9 * 5));
    for(lp = 0; prop0 >= 9; lp++, prop0 -= 9);
    lc = prop0;

    // 2) Calculate required amount for LZMA lzmaInternalSize:
    lzmaInternalSize = (UNZIP_BASE_SIZE + (UNZIP_LIT_SIZE << (lc + lp))) * sizeof(unsigned short) + 100;
    if(lzmaInternalSize > buf_size)
        return LZMA_RESULT_NOT_ENOUGH_MEM;

    // 3) Get decompressed data dictionary size:
    dictSize = get_dictionary_size(in);
    if(dictSize > m_dict_size)
        return LZMA_RESULT_NOT_ENOUGH_MEM;
        
    unzipSize = get_unzip_size(in);
    if(!outCallback && unzipSize > out_size)
        return LZMA_RESULT_NOT_ENOUGH_MEM;

    // 4) Init decoder
    ret = LzmaDecoderInit(buf, lzmaInternalSize, lc, lp, pb, 
                          m_dict_buffer, dictSize, in+13, in_size-13);
    if(ret != LZMA_RESULT_OK)
        return ret;

    // 4) Decompress data:
    for(nowPos=0; nowPos<unzipSize; nowPos+=outSizeProcessed)
    {
        UInt32 blockSize = unzipSize - nowPos;
        if(blockSize > out_size)
            blockSize = out_size;
        ret = LzmaDecode(buf, out, blockSize, &outSizeProcessed);
        if(ret != LZMA_RESULT_OK || !outSizeProcessed)
            break;
        if(outCallback)
        {
            out_size = outSizeProcessed;
            if(outCallback(&out, &out_size))
                break;
        }
    }
    if(!ret)
    {
        for(lc=0; lc<(int)sizeof(unsigned int); lc++)
            buf[lc] = ((unsigned char *)&nowPos)[lc];
    }
    return ret;
}
Exemple #10
0
int main2(int numargs, const char *args[], char *rs)
{
  FILE *inputHandle, *outputHandle;
  unsigned int length, processedSize;
  unsigned int compressedSize, outSize, outSizeProcessed, lzmaInternalSize;
  void *inStream, *outStream, *lzmaInternalData;
  unsigned char properties[5];
  unsigned char prop0;
  int ii;
  int lc, lp, pb;
  int res;
  #ifdef _LZMA_IN_CB
  CBuffer bo;
  #endif

  sprintf(rs + strlen(rs), "\nLZMA Decoder 4.02 Copyright (c) 1999-2004 Igor Pavlov  2004-06-10\n");
  if (numargs < 2 || numargs > 3)
  {
    sprintf(rs + strlen(rs), "\nUsage:  lzmaDec file.lzma [outFile]\n");
    return 1;
  }

  inputHandle = fopen(args[1], "rb");
  if (inputHandle == 0)
  {
    sprintf(rs + strlen(rs), "\n Open input file error");
    return 1;
  }

  fseek(inputHandle, 0, SEEK_END);
  length = ftell(inputHandle);
  fseek(inputHandle, 0, SEEK_SET);

  if (!MyReadFile(inputHandle, properties, sizeof(properties)))
    return 1;
  
  outSize = 0;
  for (ii = 0; ii < 4; ii++)
  {
    unsigned char b;
    if (!MyReadFile(inputHandle, &b, sizeof(b)))
      return 1;
    outSize += (unsigned int)(b) << (ii * 8);
  }

  if (outSize == 0xFFFFFFFF)
  {
    sprintf(rs + strlen(rs), "\nstream version is not supported");
    return 1;
  }

  for (ii = 0; ii < 4; ii++)
  {
    unsigned char b;
    if (!MyReadFile(inputHandle, &b, sizeof(b)))
      return 1;
    if (b != 0)
    {
      sprintf(rs + strlen(rs), "\n too long file");
      return 1;
    }
  }

  compressedSize = length - 13;
  inStream = malloc(compressedSize);
  if (inStream == 0)
  {
    sprintf(rs + strlen(rs), "\n can't allocate");
    return 1;
  }
  if (!MyReadFile(inputHandle, inStream, compressedSize))
  {
    sprintf(rs + strlen(rs), "\n can't read");
    return 1;
  }

  fclose(inputHandle);

  prop0 = properties[0];
  if (prop0 >= (9*5*5))
  {
    sprintf(rs + strlen(rs), "\n Properties error");
    return 1;
  }
  for (pb = 0; prop0 >= (9 * 5); 
    pb++, prop0 -= (9 * 5));
  for (lp = 0; prop0 >= 9; 
    lp++, prop0 -= 9);
  lc = prop0;

  lzmaInternalSize = 
    (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb);

  #ifdef _LZMA_OUT_READ
  lzmaInternalSize += 100;
  #endif

  outStream = malloc(outSize);
  lzmaInternalData = malloc(lzmaInternalSize);
  if (outStream == 0 || lzmaInternalData == 0)
  {
    sprintf(rs + strlen(rs), "\n can't allocate");
    return 1;
  }

  #ifdef _LZMA_IN_CB
  bo.InCallback.Read = LzmaReadCompressed;
  bo.Buffer = (unsigned char *)inStream;
  bo.Size = compressedSize;
  #endif

  #ifdef _LZMA_OUT_READ
  {
    UInt32 nowPos;
    unsigned char *dictionary;
    UInt32 dictionarySize = 0;
    int i;
    for (i = 0; i < 4; i++)
      dictionarySize += (UInt32)(properties[1 + i]) << (i * 8);
    dictionary = malloc(dictionarySize);
    if (dictionary == 0)
    {
      sprintf(rs + strlen(rs), "\n can't allocate");
      return 1;
    }
    LzmaDecoderInit((unsigned char *)lzmaInternalData, lzmaInternalSize,
        lc, lp, pb,
        dictionary, dictionarySize,
        #ifdef _LZMA_IN_CB
        &bo.InCallback
        #else
        (unsigned char *)inStream, compressedSize
        #endif
        );
    for (nowPos = 0; nowPos < outSize;)
    {
      UInt32 blockSize = outSize - nowPos;
      UInt32 kBlockSize = 0x10000;
      if (blockSize > kBlockSize)
        blockSize = kBlockSize;
      res = LzmaDecode((unsigned char *)lzmaInternalData, 
      ((unsigned char *)outStream) + nowPos, blockSize, &outSizeProcessed);
      if (res != 0)
      {
        sprintf(rs + strlen(rs), "\nerror = %d\n", res);
        return 1;
      }
      if (outSizeProcessed == 0)
      {
        outSize = nowPos;
        break;
      }
      nowPos += outSizeProcessed;
    }
    free(dictionary);
  }

  #else
  res = LzmaDecode((unsigned char *)lzmaInternalData, lzmaInternalSize,
      lc, lp, pb,
      #ifdef _LZMA_IN_CB
      &bo.InCallback,
      #else
      (unsigned char *)inStream, compressedSize,
      #endif
      (unsigned char *)outStream, outSize, &outSizeProcessed);
  outSize = outSizeProcessed;
  #endif

  if (res != 0)
  {
    sprintf(rs + strlen(rs), "\nerror = %d\n", res);
    return 1;
  }

  if (numargs > 2)
  {
    outputHandle = fopen(args[2], "wb+");
    if (outputHandle == 0)
    {
      sprintf(rs + strlen(rs), "\n Open output file error");
      return 1;
    }
    processedSize = fwrite(outStream, 1, outSize, outputHandle);
    if (processedSize != outSize)
    {
      sprintf(rs + strlen(rs), "\n can't write");
      return 1;
    }
    fclose(outputHandle);
  }
  free(lzmaInternalData);
  free(outStream);
  free(inStream);
  return 0;
}
HRESULT LzmaDecoderCodeReal(
    LzmaDecoder     *lzmaDecoder,
    UINT64          *anInSize,
    UINT64          *anOutSize)
{
    BOOL                  aPeviousIsMatch         = FALSE;
    BYTE                  aPreviousByte           = 0;
    UINT32                aRepDistances[kNumRepDistances];
    int                   i;
    UINT64                aNowPos64               = 0;
    UINT64                aSize                   = *anOutSize;
    ISequentialInStream   my_in_stream;
//  WindowOut             out_window;
    CState                aState;

    CStateInit(&aState);

    if (anOutSize == NULL)
    {
        printf("CodeReal: invalid argument %x\n", (UINT32) anOutSize );
        return E_INVALIDARG;
    }


    LzmaDecoderInit(lzmaDecoder);

    my_in_stream.data           = in_stream.data;
    my_in_stream.remainingBytes = in_stream.remainingBytes;

    for(i = 0 ; i < (int) kNumRepDistances; i++)
        aRepDistances[i] = 0;

    //while(aNowPos64 < aSize)
    while(my_in_stream.remainingBytes > 0)
    {
        UINT64 aNext = MyMin(aNowPos64 + (1 << 18), aSize);
        while(aNowPos64 < aNext)
        {
            UINT32 aPosState = (UINT32)(aNowPos64) & lzmaDecoder->m_PosStateMask;
            if (BitDecode(&my_in_stream,
                          &lzmaDecoder->m_MainChoiceDecoders[aState][aPosState],
                          &lzmaDecoder->m_RangeDecoder) == (UINT32) kMainChoiceLiteralIndex)
            {
                CStateUpdateChar(&aState);
                if(aPeviousIsMatch)
                {
                    BYTE aMatchByte = OutWindowGetOneByte(0 - aRepDistances[0] - 1);
                    aPreviousByte = LitDecodeWithMatchByte(&my_in_stream,
                                                           &lzmaDecoder->m_LiteralDecoder,
                                                           &lzmaDecoder->m_RangeDecoder,
                                                           (UINT32)(aNowPos64),
                                                           aPreviousByte,
                                                           aMatchByte);
                    aPeviousIsMatch = FALSE;
                }
                else
                    aPreviousByte = LitDecodeNormal(&my_in_stream,
                                                    &lzmaDecoder->m_LiteralDecoder,
                                                    &lzmaDecoder->m_RangeDecoder,
                                                    (UINT32)(aNowPos64),
                                                    aPreviousByte);
                OutWindowPutOneByte(aPreviousByte);
                aNowPos64++;
            }
            else
            {
                UINT32 aDistance, aLen;
                aPeviousIsMatch = TRUE;
                if(BitDecode(&my_in_stream,
                             &lzmaDecoder->m_MatchChoiceDecoders[aState],
                             &lzmaDecoder->m_RangeDecoder) == (UINT32) kMatchChoiceRepetitionIndex)
                {
                    if(BitDecode(&my_in_stream,
                                 &lzmaDecoder->m_MatchRepChoiceDecoders[aState],
                                 &lzmaDecoder->m_RangeDecoder) == 0)
                    {
                        if(BitDecode(&my_in_stream,
                                     &lzmaDecoder->m_MatchRepShortChoiceDecoders[aState][aPosState],
                                     &lzmaDecoder->m_RangeDecoder) == 0)
                        {
                            CStateUpdateShortRep(&aState);
                            aPreviousByte = OutWindowGetOneByte(0 - aRepDistances[0] - 1);
                            OutWindowPutOneByte(aPreviousByte);
                            aNowPos64++;
                            continue;
                        }
                        aDistance = aRepDistances[0];
                    }
                    else
                    {
                        if(BitDecode(&my_in_stream,
                                     &lzmaDecoder->m_MatchRep1ChoiceDecoders[aState],
                                     &lzmaDecoder->m_RangeDecoder) == 0)
                        {
                            aDistance = aRepDistances[1];
                            aRepDistances[1] = aRepDistances[0];
                        }
                        else
                        {
                            if (BitDecode(&my_in_stream,
                                          &lzmaDecoder->m_MatchRep2ChoiceDecoders[aState],
                                          &lzmaDecoder->m_RangeDecoder) == 0)
                            {
                                aDistance = aRepDistances[2];
                            }
                            else
                            {
                                aDistance = aRepDistances[3];
                                aRepDistances[3] = aRepDistances[2];
                            }
                            aRepDistances[2] = aRepDistances[1];
                            aRepDistances[1] = aRepDistances[0];
                        }
                        aRepDistances[0] = aDistance;
                    }
                    aLen = LenDecode(&my_in_stream,
                                     &lzmaDecoder->m_RepMatchLenDecoder,
                                     &lzmaDecoder->m_RangeDecoder,
                                     aPosState) + kMatchMinLen;
                    CStateUpdateRep(&aState);
                }
                else
                {
                    UINT32 aPosSlot;
                    aLen = kMatchMinLen + LenDecode(&my_in_stream,
                                                    &lzmaDecoder->m_LenDecoder,
                                                    &lzmaDecoder->m_RangeDecoder,
                                                    aPosState);
                    CStateUpdateMatch(&aState);
                    aPosSlot = BitTreeDecode(&my_in_stream,
                                             &lzmaDecoder->m_PosSlotDecoder[GetLenToPosState(aLen)],
                                             &lzmaDecoder->m_RangeDecoder);
                    if (aPosSlot >= (UINT32) kStartPosModelIndex)
                    {
                        aDistance = kDistStart[aPosSlot];
                        if (aPosSlot < (UINT32) kEndPosModelIndex)
                            aDistance += ReverseBitTreeDecoder2Decode(&my_in_stream,
                                         &lzmaDecoder->m_PosDecoders[aPosSlot - kStartPosModelIndex],
                                         &lzmaDecoder->m_RangeDecoder);
                        else
                        {
                            aDistance += (RangeDecodeDirectBits(&my_in_stream,
                                                                &lzmaDecoder->m_RangeDecoder,
                                                                kDistDirectBits[aPosSlot] - kNumAlignBits) << kNumAlignBits);
                            aDistance += ReverseBitTreeDecoderDecode(&my_in_stream,
                                         &lzmaDecoder->m_PosAlignDecoder,
                                         &lzmaDecoder->m_RangeDecoder);
                        }
                    }
                    else
                        aDistance = aPosSlot;


                    aRepDistances[3] = aRepDistances[2];
                    aRepDistances[2] = aRepDistances[1];
                    aRepDistances[1] = aRepDistances[0];

                    aRepDistances[0] = aDistance;
                }
                if (aDistance >= aNowPos64)
                {
                    printf("CodeReal: invalid data\n" );
                    return E_INVALIDDATA;
                }
                OutWindowCopyBackBlock(aDistance, aLen);
                aNowPos64 += aLen;
                aPreviousByte = OutWindowGetOneByte(0 - 1);
            }
        }
    }

    //BRCM modification
    LzmaDecoderFreeBuffer(lzmaDecoder);

    OutWindowFlush();
    return S_OK;
}