UINT16 WordOfStart;\r
UINT16 WordOfCount;\r
\r
- for (Index = 1; Index <= 16; Index++) {\r
+ for (Index = 0; Index <= 16; Index++) {\r
Count[Index] = 0;\r
}\r
\r
for (Index = 0; Index < NumOfChar; Index++) {\r
Count[BitLen[Index]]++;\r
}\r
-\r
+ \r
+ Start[0] = 0;\r
Start[1] = 0;\r
\r
for (Index = 1; Index <= 16; Index++) {\r
}\r
\r
JuBits = (UINT16) (16 - TableBits);\r
-\r
+ \r
+ Weight[0] = 0;\r
for (Index = 1; Index <= TableBits; Index++) {\r
Start[Index] >>= JuBits;\r
Weight[Index] = (UINT16) (1U << (TableBits - Index));\r
\r
while (Index <= 16) {\r
Weight[Index] = (UINT16) (1U << (16 - Index));\r
- Index++;\r
+ Index++; \r
}\r
\r
Index = (UINT16) (Start[TableBits + 1] >> JuBits);\r
\r
if (Index != 0) {\r
Index3 = (UINT16) (1U << TableBits);\r
- while (Index != Index3) {\r
- Table[Index++] = 0;\r
+ if (Index < Index3) {\r
+ SetMem16 (Table + Index, (Index3 - Index) * sizeof (*Table), 0);\r
}\r
}\r
\r
for (Char = 0; Char < NumOfChar; Char++) {\r
\r
Len = BitLen[Char];\r
- if (Len == 0) {\r
+ if (Len == 0 || Len >= 17) {\r
continue;\r
}\r
\r
Index = (UINT16) (Len - TableBits);\r
\r
while (Index != 0) {\r
- if (*Pointer == 0) {\r
- Sd->mRight[Avail] = Sd->mLeft[Avail] = 0;\r
+ if (*Pointer == 0 && Avail < (2 * NC - 1)) {\r
+ Sd->mRight[Avail] = Sd->mLeft[Avail] = 0;\r
*Pointer = Avail++;\r
}\r
-\r
- if (Index3 & Mask) {\r
- Pointer = &Sd->mRight[*Pointer];\r
- } else {\r
- Pointer = &Sd->mLeft[*Pointer];\r
+ \r
+ if (*Pointer < (2 * NC - 1)) {\r
+ if ((Index3 & Mask) != 0) {\r
+ Pointer = &Sd->mRight[*Pointer];\r
+ } else {\r
+ Pointer = &Sd->mLeft[*Pointer];\r
+ }\r
}\r
\r
Index3 <<= 1;\r
volatile UINT16 Index;\r
UINT32 Mask;\r
\r
+ //\r
+ // Read Extra Set Code Length Array size \r
+ //\r
Number = (UINT16) GetBits (Sd, nbit);\r
\r
if (Number == 0) {\r
+ //\r
+ // This represents only Huffman code used\r
+ //\r
CharC = (UINT16) GetBits (Sd, nbit);\r
\r
for (Index = 0; Index < 256; Index++) {\r
Sd->mPTTable[Index] = CharC;\r
}\r
\r
- for (Index = 0; Index < nn; Index++) {\r
- Sd->mPTLen[Index] = 0;\r
- }\r
+ SetMem (Sd->mPTLen, nn, 0);\r
\r
return 0;\r
}\r
\r
Index = 0;\r
\r
- while (Index < Number) {\r
+ while (Index < Number && Index < NPT) {\r
\r
CharC = (UINT16) (Sd->mBitBuf >> (BITBUFSIZ - 3));\r
\r
+ //\r
+ // If a code length is less than 7, then it is encoded as a 3-bit\r
+ // value. Or it is encoded as a series of "1"s followed by a \r
+ // terminating "0". The number of "1"s = Code length - 4.\r
+ //\r
if (CharC == 7) {\r
Mask = 1U << (BITBUFSIZ - 1 - 3);\r
while (Mask & Sd->mBitBuf) {\r
CharC += 1;\r
}\r
}\r
-\r
+ \r
FillBuf (Sd, (UINT16) ((CharC < 7) ? 3 : CharC - 3));\r
\r
Sd->mPTLen[Index++] = (UINT8) CharC;\r
-\r
+ \r
+ //\r
+ // For Code&Len Set, \r
+ // After the third length of the code length concatenation,\r
+ // a 2-bit value is used to indicated the number of consecutive \r
+ // zero lengths after the third length.\r
+ //\r
if (Index == Special) {\r
CharC = (UINT16) GetBits (Sd, 2);\r
- while ((INT16) (--CharC) >= 0) {\r
+ while ((INT16) (--CharC) >= 0 && Index < NPT) {\r
Sd->mPTLen[Index++] = 0;\r
}\r
}\r
}\r
\r
- while (Index < nn) {\r
+ while (Index < nn && Index < NPT) {\r
Sd->mPTLen[Index++] = 0;\r
}\r
-\r
+ \r
return MakeTable (Sd, nn, Sd->mPTLen, 8, Sd->mPTTable);\r
}\r
\r
Number = (UINT16) GetBits (Sd, CBIT);\r
\r
if (Number == 0) {\r
+ //\r
+ // This represents only Huffman code used\r
+ //\r
CharC = (UINT16) GetBits (Sd, CBIT);\r
\r
- for (Index = 0; Index < NC; Index++) {\r
- Sd->mCLen[Index] = 0;\r
- }\r
+ SetMem (Sd->mCLen, NC, 0);\r
\r
for (Index = 0; Index < 4096; Index++) {\r
Sd->mCTable[Index] = CharC;\r
}\r
\r
Index = 0;\r
- while (Index < Number) {\r
-\r
+ while (Index < Number && Index < NC) {\r
CharC = Sd->mPTTable[Sd->mBitBuf >> (BITBUFSIZ - 8)];\r
if (CharC >= NT) {\r
Mask = 1U << (BITBUFSIZ - 1 - 8);\r
CharC = (UINT16) (GetBits (Sd, CBIT) + 20);\r
}\r
\r
- while ((INT16) (--CharC) >= 0) {\r
+ while ((INT16) (--CharC) >= 0 && Index < NC) {\r
Sd->mCLen[Index++] = 0;\r
}\r
\r
}\r
}\r
\r
- while (Index < NC) {\r
- Sd->mCLen[Index++] = 0;\r
- }\r
+ SetMem (Sd->mCLen + Index, NC - Index, 0);\r
\r
MakeTable (Sd, NC, Sd->mCLen, 12, Sd->mCTable);\r
\r