Ошибка возникает при декодировании ASCII85 и FlateDecoding в строке - PullRequest
2 голосов
/ 29 января 2020

Я использовал следующий код для декодирования потока текста в формате PDF. Есть случаи, когда поток должен быть декодирован двумя фильтрами. << / Длина 2348 / Фильтр [/ ASCII85Decode / FlateDecode] >> Я декодирую поток сначала ASCII85Decode, а затем Flatedecode. В некоторых случаях конечный результат, закодированный в формате flate, становится нулевым. Есть идеи по этому вопросу?

    public byte[] decode(byte[] encodedInput)
    {
        bool strict = false;
        MemoryStream stream = new MemoryStream(encodedInput);
        InflaterInputStream zip = new InflaterInputStream(stream);
        MemoryStream output = new MemoryStream();
        byte[] b = new byte[strict ? 4092 : 1];
        try
        {
            int n;
            while ((n = zip.Read(b, 0, b.Length)) > 0)
            {
                output.Write(b, 0, n);
            }
            zip.Close();
            output.Close();
            return (output.ToArray());
        }
        catch
        {
            if (strict)
                return null;
            return (output.ToArray());
        }
    }

//ASCII85Decode

class ASCII85 : IASCII85
{
    /// <summary>
 /// Prefix mark that identifies an encoded ASCII85 string, traditionally 
'<~'
 /// </summary>
    public string PrefixMark = "<~";
    /// <summary>
    /// Suffix mark that identifies an encoded ASCII85 string, 
traditionally '~>'
    /// </summary>
    public string SuffixMark = "~>";
    /// <summary>
    /// Maximum line length for encoded ASCII85 string; 
    /// set to zero for one unbroken line.
    /// </summary>
    public int LineLength = 75;
    /// <summary>
    /// Add the Prefix and Suffix marks when encoding, and enforce their 
presence for decoding
    /// </summary>
    public bool EnforceMarks = true;

    private const int _asciiOffset = 33;
    private byte[] _encodedBlock = new byte[5];
    private byte[] _decodedBlock = new byte[4];
    private uint _tuple = 0;
    private int _linePos = 0;
    private uint[] pow85 = { 85 * 85 * 85 * 85, 85 * 85 * 85, 85 * 85, 85, 
1 };

    /// <summary>
    /// Decodes an ASCII85 encoded string into the original binary data
    /// </summary>
    /// <param name="inputString">ASCII85 encoded string</param>
    /// <returns>byte array of decoded binary data</returns>
    public byte[] decode(string inputString)
    {
        if (EnforceMarks)
        {
            bool x = !inputString.StartsWith(PrefixMark);
            bool y = !inputString.EndsWith(SuffixMark);
            bool a = !inputString.StartsWith(PrefixMark) && 
!inputString.EndsWith(SuffixMark);             
            if (a)
            {
                throw new Exception("ASCII85 encoded data should begin with 
'" + PrefixMark +
                    "' and end with '" + SuffixMark + "'");
            }
        }
        if (inputString.StartsWith("<~"))
        {
            inputString = inputString.Substring(PrefixMark.Length);
        }
        if (inputString.EndsWith("~>"))
        {
            inputString = inputString.Substring(0, inputString.Length - 
SuffixMark.Length);
        }

        MemoryStream ms = new MemoryStream();
        int count = 0;
        bool processChar = false;

        foreach (char c in inputString)
        {
            switch (c)
            {
                case 'z':
                    if (count != 0)
                    {
                        throw new Exception("The character 'z' is invalid 
inside an ASCII85 block.");
                    }
                    _decodedBlock[0] = 0;
                    _decodedBlock[1] = 0;
                    _decodedBlock[2] = 0;
                    _decodedBlock[3] = 0;
                    ms.Write(_decodedBlock, 0, _decodedBlock.Length);
                    processChar = false;
                    break;
                case '\n':
                case '\r':
                case '\t':
                case '\0':
                case '\f':
                case '\b':
                    processChar = false;
                    break;
                default:
                    if (c < '!' || c > 'u')
                    {
                        throw new Exception("Bad character '" + c + "' 
                    found. ASCII85 only allows characters '!' to 'u'.");
                    }
                    processChar = true;
                    break;
            }

            if (processChar)
            {
                _tuple += ((uint)(c - _asciiOffset) * pow85[count]);
                count++;
                if (count == _encodedBlock.Length)
                {
                    DecodeBlock();
                    ms.Write(_decodedBlock, 0, _decodedBlock.Length);
                    _tuple = 0;
                    count = 0;
                }
            }
        }

        if (count != 0)
        {
            if (count == 1)
            {
                throw new Exception("The last block of ASCII85 data cannot 
be a single byte.");
            }
            count--;
            _tuple += pow85[count];
            DecodeBlock(count);
            for (int i = 0; i < count; i++)
            {
                ms.WriteByte(_decodedBlock[i]);
            }
        }
        return ms.ToArray();
    }

    /// <summary>
    /// Encodes binary data into a plaintext ASCII85 format string
    /// </summary>
    /// <param name="ba">binary data to encode</param>
    /// <returns>ASCII85 encoded string</returns>
    public string encode(byte[] ba)
    {
        StringBuilder sb = new StringBuilder((int)(ba.Length *  
(_encodedBlock.Length / _decodedBlock.Length)));
        _linePos = 0;

        if (EnforceMarks)
        {
            AppendString(sb, PrefixMark);
        }

        int count = 0;
        _tuple = 0;
        foreach (byte b in ba)
        {
            if (count >= _decodedBlock.Length - 1)
            {
                _tuple |= b;
                if (_tuple == 0)
                {
                    AppendChar(sb, 'z');
                }
                else
                {
                    EncodeBlock(sb);
                }
                _tuple = 0;
                count = 0;
            }
            else
            {
                _tuple |= (uint)(b << (24 - (count * 8)));
                count++;
            }
        }

        if (count > 0)
        {
            EncodeBlock(count + 1, sb);
        }

        if (EnforceMarks)
        {
            AppendString(sb, SuffixMark);
        }
        return sb.ToString();
    }

    private void EncodeBlock(StringBuilder sb)
    {
        EncodeBlock(_encodedBlock.Length, sb);
    }

    private void EncodeBlock(int count, StringBuilder sb)
    {
        for (int i = _encodedBlock.Length - 1; i >= 0; i--)
        {
            _encodedBlock[i] = (byte)((_tuple % 85) + _asciiOffset);
            _tuple /= 85;
        }

        for (int i = 0; i < count; i++)
        {
            char c = (char)_encodedBlock[i];
            AppendChar(sb, c);
        }

    }

    private void DecodeBlock()
    {
        DecodeBlock(_decodedBlock.Length);
    }

    private void DecodeBlock(int bytes)
    {
        for (int i = 0; i < bytes; i++)
        {
            _decodedBlock[i] = (byte)(_tuple >> 24 - (i * 8));
        }
    }

    private void AppendString(StringBuilder sb, string s)
    {
        if (LineLength > 0 && (_linePos + s.Length > LineLength))
        {
            _linePos = 0;
            sb.Append('\n');
        }
        else
        {
            _linePos += s.Length;
        }
        sb.Append(s);
    }

    private void AppendChar(StringBuilder sb, char c)
    {
        sb.Append(c);
        _linePos++;
        if (LineLength > 0 && (_linePos >= LineLength))
        {
            _linePos = 0;
            sb.Append('\n');
        }
    }

    public string decode(byte[] ba)
    {
        throw new NotImplementedException();
    }
}

1 Ответ

2 голосов
/ 29 января 2020

Я не знаю ваш InflaterInputStream, поэтому я использовал System.IO.Compression.DeflateStream.

Я взял все содержимое потока с помощью /Filter [ /ASCII85Decode /FlateDecode ] из вашего файла примера и попытался декодировать их в String rawStreamChars вот так:

ASCII85 ascii85 = new ASCII85();
ascii85.EnforceMarks = false;
byte[] ascii85Decoded = ascii85.decode(rawStreamChars);

using (MemoryStream stream = new MemoryStream(ascii85Decoded))
{
    // Remove 2 bytes zlib header
    stream.ReadByte();
    stream.ReadByte();
    using (DeflateStream decompressionStream = new DeflateStream(stream, CompressionMode.Decompress))
    using (MemoryStream result = new MemoryStream())
    {
        decompressionStream.CopyTo(result);
        Console.Out.WriteLine(Encoding.GetEncoding("windows-1252").GetString(result.ToArray()));
    }
}

И в каждом случае я получал соответствующий поток контента.

Таким образом, вы либо

  • неверно извлекаете содержимое потока из объект потока; или
  • неправильно используете ваш класс ASCII85 (например, с помощью EnforceMarks = true); или
  • проблема в вашем методе decode.

Ну, или есть какая-то менее очевидная ошибка в работе.

...