首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在行ASCII85解码和FlateDecoding时发生错误

在行ASCII85解码和FlateDecoding时发生错误
EN

Stack Overflow用户
提问于 2020-01-29 09:27:43
回答 1查看 876关注 0票数 2

我已经使用以下代码解码pdf格式的文本流。在有些情况下,流需要由2个过滤器解码。<< /Length 2348 /Filter /ASCII85Decode /FlateDecode >> I首先由ASCII85Decode解码流,然后由Flatedecode解码。在某些情况下,扁平的最终结果变为空。对这个问题有什么想法吗?

代码语言:javascript
复制
    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();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-29 14:46:14

我不知道你的InflaterInputStream,所以我用System.IO.Compression.DeflateStream代替。

我从示例文件中提取了使用/Filter [ /ASCII85Decode /FlateDecode ]的所有流内容,并试图在String rawStreamChars中对它们进行解码,如下所示:

代码语言:javascript
复制
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方法中有问题。H 214F 215

好吧,或者有一些不太明显的bug在起作用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59964039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档