S
S
shasoft2020-03-11 22:14:53
C++ / C#
shasoft, 2020-03-11 22:14:53

How to decompress LZMA2 stream using Lzma2Dec_DecodeToDic (LZMA SDK)?

Packed the data stream using the LZMA2 method

bool res = false;
      // Создать упаковщик
      CLzma2EncHandle enc;
      enc = Lzma2Enc_Create(&g_AlignedAlloc, &g_BigAlloc);
      if (enc) {
        // Создать параметры
        CLzma2EncProps props;
        Lzma2EncProps_Init(&props);
        lzma2::initProps(&props.lzmaProps,level);
        Lzma2EncProps_Normalize(&props);
        if (Lzma2Enc_SetProps(enc, &props) == SZ_OK) {
          // Тип упаковки
          WORD wType = ARC_LZMA2;
          pOut->write(&wType,sizeof(wType));
          // Параметр упаковщика (требуется для распаковки)
          Byte prop = Lzma2Enc_WriteProperties(enc);
          pOut->write(&prop, 1);
          // Упаковать
          res = (Lzma2Enc_Encode2(
            enc,
            pOut, NULL, 0,
            pIn, NULL, 0,
            NULL) == SZ_OK);
        }
        // Уничтожить упаковщик
        Lzma2Enc_Destroy(enc);
      }
      return res;

But I don’t understand how it can be unpacked using the Lzma2Dec_DecodeToDic or Lzma2Dec_DecodeToBuf functions .

Can someone suggest how to do this?

I'm trying to unpack like this (code from here )
#define IN_BUF_SIZE (1<<16)
#define OUT_BUF_SIZE (1<<16)
    // Распаковать
    bool lzma2::unpack(stream_in *pIn,stream_out *pOut)
    {
      bool ret = false;
      // Тип упаковки
      WORD wType;
      if (pIn->read(&wType, sizeof(wType)) && wType==ARC_LZMA2) {
        Byte prop;
        if (pIn->read(&prop, sizeof(prop))) {
          UInt64 unpackSize;
          int i;
          SRes res = 0;
          CLzma2Dec state;
          Lzma2Dec_Construct(&state);
          RINOK(Lzma2Dec_AllocateProbs(&state, prop, &g_Alloc));
          unpackSize = 512000;
          //m_lUncompressedSize = unpackSize;
          int thereIsSize = (unpackSize != (UInt64)(Int64)-1);
          Byte inBuf[IN_BUF_SIZE];
          Byte outBuf[OUT_BUF_SIZE];
          state.decoder.dic = outBuf;
          state.decoder.dicBufSize = OUT_BUF_SIZE;
          size_t inPos = 0, inSize = 0, outPos = 0;
          Lzma2Dec_Init(&state);
          for (;;)
          {
            if (inPos == inSize)
            {
              inSize = IN_BUF_SIZE;
              pIn->read(inBuf, inSize ,&inSize);
              inPos = 0;
            }
            {
              SRes res;
              SizeT inProcessed = inSize - inPos;
              SizeT outProcessed = OUT_BUF_SIZE - outPos;
              ELzmaFinishMode finishMode = LZMA_FINISH_ANY;
              ELzmaStatus status;
              if (thereIsSize && outProcessed > unpackSize)
              {
                outProcessed = (SizeT)unpackSize;
                finishMode = LZMA_FINISH_END;
              }
              //* Вот этот кусок на втором вызове возвращает ошибку SZ_ERROR_DATA
              res = Lzma2Dec_DecodeToDic(&state
                , OUT_BUF_SIZE
                , inBuf + inPos
                , &inProcessed
                , finishMode
                , &status);
              state.decoder.dicPos = 0;
              //*/
              /*
              res = Lzma2Dec_DecodeToBuf(&state
                , outBuf + outPos
                , &outProcessed
                , inBuf + inPos
                , &inProcessed
                , finishMode, &status);
              //*/

              inPos += inProcessed;
              outPos += outProcessed;
              unpackSize -= outProcessed;
              if ((pOut) && (res == SZ_OK))
                if (pOut->write(outBuf, outPos) != outPos)
                  return false;
              outPos = 0;
              if (res != SZ_OK || thereIsSize && unpackSize == 0)
                return res;
              /*
              if (pfnExtractCallback)
              {
                if (!pfnExtractCallback((m_lUncompressedSize - unpackSize)
                  , m_lUncompressedSize
                  , 0
                  , 0
                  , m_dwExtraData))
                {
                  return SZ_ERROR_PROGRESS;
                }
              }
              //*/
              if (inProcessed == 0 && outProcessed == 0)
              {
                if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK)
                  return SZ_ERROR_DATA;
                return res;
              }
            }
          }
        }
        int g = 0;
      }
      return ret;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2020-03-11
@2ord

https://cpp.hotexamples.com/examples/-/-/Lzma2Dec_...
4th link in Google on Lzma2Dec_DecodeToDic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question