Annotation of elwix/tools/oldlzma/SRC/7zip/Archive/7z_C/7zDecode.c, revision 1.1.1.1

1.1       misho       1: /* 7zDecode.c */
                      2: 
                      3: #include "7zDecode.h"
                      4: #ifdef _SZ_ONE_DIRECTORY
                      5: #include "LzmaDecode.h"
                      6: #else
                      7: #include "../../Compress/LZMA_C/LzmaDecode.h"
                      8: #endif
                      9: 
                     10: CMethodID k_Copy = { { 0x0 }, 1 };
                     11: CMethodID k_LZMA = { { 0x3, 0x1, 0x1 }, 3 };
                     12: 
                     13: #ifdef _LZMA_IN_CB
                     14: 
                     15: typedef struct _CLzmaInCallbackImp
                     16: {
                     17:   ILzmaInCallback InCallback;
                     18:   ISzInStream *InStream;
                     19:   size_t Size;
                     20: } CLzmaInCallbackImp;
                     21: 
                     22: int LzmaReadImp(void *object, unsigned char **buffer, UInt32 *size)
                     23: {
                     24:   CLzmaInCallbackImp *cb = (CLzmaInCallbackImp *)object;
                     25:   size_t processedSize;
                     26:   SZ_RESULT res;
                     27:   *size = 0;
                     28:   res = cb->InStream->Read((void *)cb->InStream, (void **)buffer, cb->Size, &processedSize);
                     29:   *size = (UInt32)processedSize;
                     30:   if (processedSize > cb->Size)
                     31:     return (int)SZE_FAIL;
                     32:   cb->Size -= processedSize;
                     33:   if (res == SZ_OK)
                     34:     return 0;
                     35:   return (int)res;
                     36: }
                     37: 
                     38: #endif
                     39: 
                     40: SZ_RESULT SzDecode(CFileSize *packSizes, CFolder *folder,
                     41:     #ifdef _LZMA_IN_CB
                     42:     ISzInStream *inStream,
                     43:     #else
                     44:     Byte *inBuffer,
                     45:     #endif
                     46:     Byte *outBuffer, size_t outSize, 
                     47:     size_t *outSizeProcessed, ISzAlloc *allocMain)
                     48: {
                     49:   UInt32 si;
                     50:   size_t inSize = 0;
                     51:   CCoderInfo *coder;
                     52:   if (folder->NumPackStreams != 1)
                     53:     return SZE_NOTIMPL;
                     54:   if (folder->NumCoders != 1)
                     55:     return SZE_NOTIMPL;
                     56:   coder = folder->Coders;
                     57:   *outSizeProcessed = 0;
                     58: 
                     59:   for (si = 0; si < folder->NumPackStreams; si++)
                     60:     inSize += (size_t)packSizes[si];
                     61: 
                     62:   if (AreMethodsEqual(&coder->MethodID, &k_Copy))
                     63:   {
                     64:     size_t i;
                     65:     if (inSize != outSize)
                     66:       return SZE_DATA_ERROR;
                     67:     #ifdef _LZMA_IN_CB
                     68:     for (i = 0; i < inSize;)
                     69:     {
                     70:       size_t j;
                     71:       Byte *inBuffer;
                     72:       size_t bufferSize;
                     73:       RINOK(inStream->Read((void *)inStream,  (void **)&inBuffer, inSize - i, &bufferSize));
                     74:       if (bufferSize == 0)
                     75:         return SZE_DATA_ERROR;
                     76:       if (bufferSize > inSize - i)
                     77:         return SZE_FAIL;
                     78:       *outSizeProcessed += bufferSize;
                     79:       for (j = 0; j < bufferSize && i < inSize; j++, i++)
                     80:         outBuffer[i] = inBuffer[j];
                     81:     }
                     82:     #else
                     83:     for (i = 0; i < inSize; i++)
                     84:       outBuffer[i] = inBuffer[i];
                     85:     *outSizeProcessed = inSize;
                     86:     #endif
                     87:     return SZ_OK;
                     88:   }
                     89: 
                     90:   if (AreMethodsEqual(&coder->MethodID, &k_LZMA))
                     91:   {
                     92:     #ifdef _LZMA_IN_CB
                     93:     CLzmaInCallbackImp lzmaCallback;
                     94:     #endif
                     95: 
                     96:     int lc, lp, pb;
                     97:     size_t lzmaInternalSize;
                     98:     void *lzmaInternalData;
                     99:     int result;
                    100:     UInt32 outSizeProcessedLoc;
                    101: 
                    102:     #ifdef _LZMA_IN_CB
                    103:     lzmaCallback.Size = inSize;
                    104:     lzmaCallback.InStream = inStream;
                    105:     lzmaCallback.InCallback.Read = LzmaReadImp;
                    106:     #endif
                    107: 
                    108:     if (coder->Properties.Capacity < 5)
                    109:       return SZE_FAIL;
                    110:     lc = (unsigned char)coder->Properties.Items[0];
                    111:     if (lc >= (9 * 5 * 5))
                    112:       return SZE_FAIL;
                    113:     for (pb = 0; lc >= (9 * 5); pb++, lc -= (9 * 5));
                    114:     for (lp = 0; lc >= 9; lp++, lc -= 9);
                    115: 
                    116:     lzmaInternalSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp))) * sizeof(CProb);
                    117:     lzmaInternalData = allocMain->Alloc(lzmaInternalSize);
                    118:     if (lzmaInternalData == 0)
                    119:       return SZE_OUTOFMEMORY;
                    120: 
                    121:     result = LzmaDecode((Byte *)lzmaInternalData, (UInt32)lzmaInternalSize,
                    122:         lc, lp, pb,
                    123:         #ifdef _LZMA_IN_CB
                    124:         &lzmaCallback.InCallback,
                    125:         #else
                    126:         inBuffer, (UInt32)inSize,
                    127:         #endif
                    128:         outBuffer, (UInt32)outSize, 
                    129:         &outSizeProcessedLoc);
                    130:     *outSizeProcessed = (size_t)outSizeProcessedLoc;
                    131:     allocMain->Free(lzmaInternalData);
                    132:     /*
                    133:     NOT_ENOUGH_MEM error is impossible for this code
                    134:     if (result = LZMA_RESULT_NOT_ENOUGH_MEM)
                    135:       return SZE_OUTOFMEMORY;
                    136:     */
                    137:     if (result == LZMA_RESULT_DATA_ERROR)
                    138:       return SZE_DATA_ERROR;
                    139:     if (result != LZMA_RESULT_OK)
                    140:       return SZE_FAIL;
                    141:     return SZ_OK;
                    142:   }
                    143:   return SZE_NOTIMPL;
                    144: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>