 +		totalCost >>= (largestBits - maxNbBits);  /* note : totalCost is necessarily a multiple of baseCost */
 +
 +		/* repay normalized cost */
-+		{   U32 const noSymbol = 0xF0F0F0F0;
++		{	U32 const noSymbol = 0xF0F0F0F0;
 +			U32 rankLast[HUF_TABLELOG_MAX+2];
 +			int pos;
 +
 +			/* Get pos of last (smallest) symbol per rank */
 +			memset(rankLast, 0xF0, sizeof(rankLast));
-+			{   U32 currentNbBits = maxNbBits;
++			{	U32 currNbBits = maxNbBits;
 +				for (pos=n ; pos >= 0; pos--) {
-+					if (huffNode[pos].nbBits >= currentNbBits) continue;
-+					currentNbBits = huffNode[pos].nbBits;   /* < maxNbBits */
-+					rankLast[maxNbBits-currentNbBits] = pos;
++					if (huffNode[pos].nbBits >= currNbBits) continue;
++					currNbBits = huffNode[pos].nbBits;   /* < maxNbBits */
++					rankLast[maxNbBits-currNbBits] = pos;
 +			}   }
 +
 +			while (totalCost > 0) {

+	for (maxW = tableLog; rankStats[maxW]==0; maxW--) {}  /* necessarily finds a solution before 0 */
+
+	/* Get start index of each weight */
+	{   U32 w, nextRankStart = 0;
+		for (w=1; w<maxW+1; w++) {
+			U32 current = nextRankStart;
+			nextRankStart += rankStats[w];
+			rankStart[w] = current;
+		}
+		rankStart[0] = nextRankStart;   /* put all 0w symbols at the end of sorted list*/
+		sizeOfSort = nextRankStart;
+	}
+
+	/* sort symbols by weight */
+	{   U32 s;
+		for (s=0; s<nbSymbols; s++) {
+			U32 const w = weightList[s];
+			U32 const r = rankStart[w]++;

+
+			/* Consume header */
+			ZSTD_refDDict(zds->dctx, zds->ddict);
+			{	size_t const h1Size = ZSTD_nextSrcSizeToDecompress(zds->dctx);  /* == ZSTD_frameHeaderSize_prefix */
+				CHECK_F(ZSTD_decompressContinue(zds->dctx, NULL, 0, zds->headerBuffer, h1Size));
+				{	size_t const h2Size = ZSTD_nextSrcSizeToDecompress(zds->dctx);
+					CHECK_F(ZSTD_decompressContinue(zds->dctx, NULL, 0, zds->headerBuffer+h1Size, h2Size));
+			}   }
+
