contrib/python-zstandard/zstd/common/entropy_common.c
changeset 30822 b54a2984cdd4
parent 30434 2e484bdea8c4
child 37495 b1fb341d8a61
equal deleted inserted replaced
30821:7005c03f7387 30822:b54a2984cdd4
   157 
   157 
   158 
   158 
   159 /*! HUF_readStats() :
   159 /*! HUF_readStats() :
   160     Read compact Huffman tree, saved by HUF_writeCTable().
   160     Read compact Huffman tree, saved by HUF_writeCTable().
   161     `huffWeight` is destination buffer.
   161     `huffWeight` is destination buffer.
       
   162     `rankStats` is assumed to be a table of at least HUF_TABLELOG_MAX U32.
   162     @return : size read from `src` , or an error Code .
   163     @return : size read from `src` , or an error Code .
   163     Note : Needed by HUF_readCTable() and HUF_readDTableX?() .
   164     Note : Needed by HUF_readCTable() and HUF_readDTableX?() .
   164 */
   165 */
   165 size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
   166 size_t HUF_readStats(BYTE* huffWeight, size_t hwSize, U32* rankStats,
   166                      U32* nbSymbolsPtr, U32* tableLogPtr,
   167                      U32* nbSymbolsPtr, U32* tableLogPtr,
   185             for (n=0; n<oSize; n+=2) {
   186             for (n=0; n<oSize; n+=2) {
   186                 huffWeight[n]   = ip[n/2] >> 4;
   187                 huffWeight[n]   = ip[n/2] >> 4;
   187                 huffWeight[n+1] = ip[n/2] & 15;
   188                 huffWeight[n+1] = ip[n/2] & 15;
   188     }   }   }
   189     }   }   }
   189     else  {   /* header compressed with FSE (normal case) */
   190     else  {   /* header compressed with FSE (normal case) */
       
   191         FSE_DTable fseWorkspace[FSE_DTABLE_SIZE_U32(6)];  /* 6 is max possible tableLog for HUF header (maybe even 5, to be tested) */
   190         if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
   192         if (iSize+1 > srcSize) return ERROR(srcSize_wrong);
   191         oSize = FSE_decompress(huffWeight, hwSize-1, ip+1, iSize);   /* max (hwSize-1) values decoded, as last one is implied */
   193         oSize = FSE_decompress_wksp(huffWeight, hwSize-1, ip+1, iSize, fseWorkspace, 6);   /* max (hwSize-1) values decoded, as last one is implied */
   192         if (FSE_isError(oSize)) return oSize;
   194         if (FSE_isError(oSize)) return oSize;
   193     }
   195     }
   194 
   196 
   195     /* collect weight stats */
   197     /* collect weight stats */
   196     memset(rankStats, 0, (HUF_TABLELOG_ABSOLUTEMAX + 1) * sizeof(U32));
   198     memset(rankStats, 0, (HUF_TABLELOG_MAX + 1) * sizeof(U32));
   197     weightTotal = 0;
   199     weightTotal = 0;
   198     {   U32 n; for (n=0; n<oSize; n++) {
   200     {   U32 n; for (n=0; n<oSize; n++) {
   199             if (huffWeight[n] >= HUF_TABLELOG_ABSOLUTEMAX) return ERROR(corruption_detected);
   201             if (huffWeight[n] >= HUF_TABLELOG_MAX) return ERROR(corruption_detected);
   200             rankStats[huffWeight[n]]++;
   202             rankStats[huffWeight[n]]++;
   201             weightTotal += (1 << huffWeight[n]) >> 1;
   203             weightTotal += (1 << huffWeight[n]) >> 1;
   202     }   }
   204     }   }
   203     if (weightTotal == 0) return ERROR(corruption_detected);
   205     if (weightTotal == 0) return ERROR(corruption_detected);
   204 
   206 
   205     /* get last non-null symbol weight (implied, total must be 2^n) */
   207     /* get last non-null symbol weight (implied, total must be 2^n) */
   206     {   U32 const tableLog = BIT_highbit32(weightTotal) + 1;
   208     {   U32 const tableLog = BIT_highbit32(weightTotal) + 1;
   207         if (tableLog > HUF_TABLELOG_ABSOLUTEMAX) return ERROR(corruption_detected);
   209         if (tableLog > HUF_TABLELOG_MAX) return ERROR(corruption_detected);
   208         *tableLogPtr = tableLog;
   210         *tableLogPtr = tableLog;
   209         /* determine last weight */
   211         /* determine last weight */
   210         {   U32 const total = 1 << tableLog;
   212         {   U32 const total = 1 << tableLog;
   211             U32 const rest = total - weightTotal;
   213             U32 const rest = total - weightTotal;
   212             U32 const verif = 1 << BIT_highbit32(rest);
   214             U32 const verif = 1 << BIT_highbit32(rest);