Beispiel #1
0
        public override byte[] Encode(Bytes.Buffer rawData, PdfDirectObject parameters, IDictionary <PdfName, PdfDirectObject> header)
        {
            List <byte[]> codeTable = CreateCodeTable();
            int           chunk     = 9;

            byte[] inputPattern = null;
            using (var output = new Bytes.Buffer())
            {
                output.WriteBits(CLEAR_TABLE, chunk);
                int foundCode = -1;
                int r;
                while ((r = rawData.ReadByte()) != -1)
                {
                    byte by = (byte)r;
                    if (inputPattern == null)
                    {
                        inputPattern = new byte[] { by };
                        foundCode    = by & 0xff;
                    }
                    else
                    {
                        inputPattern = inputPattern.SubArray(0, inputPattern.Length + 1);
                        inputPattern[inputPattern.Length - 1] = by;
                        int newFoundCode = FindPatternCode(codeTable, inputPattern);
                        if (newFoundCode == -1)
                        {
                            // use previous
                            chunk = CalculateChunk(codeTable.Count - 1, 1);
                            output.WriteBits(foundCode, chunk);
                            // create new table entry
                            codeTable.Add(inputPattern);

                            if (codeTable.Count == 4096)
                            {
                                // code table is full
                                output.WriteBits(CLEAR_TABLE, chunk);
                                codeTable = CreateCodeTable();
                            }

                            inputPattern = new byte[] { by };
                            foundCode    = by & 0xff;
                        }
                        else
                        {
                            foundCode = newFoundCode;
                        }
                    }
                }
                if (foundCode != -1)
                {
                    chunk = CalculateChunk(codeTable.Count - 1, 1);
                    output.WriteBits(foundCode, chunk);
                }

                // PPDFBOX-1977: the decoder wouldn't know that the encoder would output
                // an EOD as code, so he would have increased his own code table and
                // possibly adjusted the chunk. Therefore, the encoder must behave as
                // if the code table had just grown and thus it must be checked it is
                // needed to adjust the chunk, based on an increased table size parameter
                chunk = CalculateChunk(codeTable.Count, 1);

                output.WriteBits(EOD, chunk);

                // pad with 0
                output.WriteBits(0, 7);

                // must do or file will be empty :-(
                return(output.GetBuffer());
            }
        }