【问题标题】:Some uuEncoded files are not decoding properly in C#某些 uuEncoded 文件在 C# 中未正确解码
【发布时间】:2016-09-30 23:07:40
【问题描述】:

我正在获取一个 SGML 文件,并使用 uuDecoder 从中提取数据并从中创建 PDF。

多年来一直运行良好,但从最近几个月开始,我们发现某些 PDF 文件无法加载,并且在 chrome 中显示“加载 PDF 文档失败”。

我已经完成了下面这个问题,它与我的情况类似,但它是在 Python 中,我在 c# 中有它

How can we figure out why certain uuencoded files are not decoding properly using Python?

下面是一个 txt 文件的示例,该文件包含有问题的嵌入式 uuencoded pdf: https://www.sec.gov/Archives/edgar/data/1631661/000163166116000004/0001631661-16-000004.txt

我的 uuDecoder 算法代码与此完全相同: http://blog.stevex.net/2004/04/c-classes-to-decode-yenc-and-uuencode-encoded-usenet-binaries/

我发现它在下面的代码中抛出 Index out of range 异常,它期望一行中有 61 个字符,但有些行没有确切的 61 个字符:

public static byte[] uuDecode(string buffer) 
        { 
            // Create an output array
            byte[] outBuffer = new byte[(buffer.Length-1)/4*3];
            int outIdx = 0;

            // Get the string as an array of ASCII bytes
            byte[] asciiBytes = Encoding.ASCII.GetBytes(buffer);

            for (int i=0; i<asciiBytes.Length; i++)
            {
                asciiBytes[i] = (byte)((asciiBytes[i]-0x20) & 0x3f);
            }

            // Convert each block of 4 input bytes into 3 
            // output bytes
            for (int i = 1; i <= (asciiBytes.Length-1); i += 4) 
            { 
                outBuffer[outIdx++] = (byte)(asciiBytes[i] << 2 | asciiBytes[i+1] >> 4);
                outBuffer[outIdx++] = (byte)(asciiBytes[i+1] << 4 | asciiBytes[i+2] >> 2);
                outBuffer[outIdx++] = (byte)(asciiBytes[i+2] << 6 | asciiBytes[i+3]);
            } 

            return outBuffer;
        } 

请注意这里没有与“索引超出范围”异常相关的任何内容,因此请不要将其重定向到那里。

我尝试用空格填充缺失的字符,如下所示:

if (line.Length < 61) ////Making sure length is 61 characters
                {
                    var builder = new StringBuilder();
                    builder.Append(line);
                    var missing = 61 - line.Length;

                    for (int i = 0; i < missing; i++)
                    {
                        builder.Append(" ");
                    }

                    line = builder.ToString();

                }

有人可以帮我弄清楚为什么这不适用于少数 PDF 文档吗?

【问题讨论】:

  • 发布代码和重现问题的示例。 “几乎相似”意味着我们无法找到由差异引起的任何错误
  • 这是完全相同的算法,现在如果它回答了你的问题@PanagiotisKanavos
  • 是否值得将整个算法代码放在这里@PanagiotisKanavos?

标签: c#


【解决方案1】:

我认为问题出在空间上。改进的 uuEncode 使用 ``(代码 0x60)而不是可以修剪的空格(0x20)。尝试将所有行向右填充到全尺寸。试试这个转换(pdf.uue 是文件的 uuencoded 部分 - 从 beginend ):

        string[] all = File.ReadAllLines(@"d:\tmp\pdf.uue");
        for (int i = 1; i < all.Length - 2; i++)
        {
            if (all[i].Length < 61)
                all[i] = all[i].PadRight(61, ' ');
        }
        File.WriteAllLines(@"d:\tmp\pdf-2.uue", all);

循环是1 .. Length-2 以跳过开始/结束行。

【讨论】:

  • 我不明白你能解释一下吗
  • @Neel 测试我的例子。我使用 WinRar 对原始 uuencoded 部分进行 uudecode,结果 PDF 是错误的。修复后,它们看起来还不错。
  • 我猜你用空格正确地填充了剩余的缺失字符?
  • 是的,它被 eedecoded 为 0 的空格填充。问题是空格是“不安全”的代码 - 它可以从行尾删除以优化文本文件。但在这种情况下,它是真实数据。
  • 我已经尝试过那个伙伴,它开始为其中一些加载,但仍然有很多文件仍未打开:(
猜你喜欢
  • 1970-01-01
  • 2018-07-28
  • 2011-06-07
  • 2020-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
相关资源
最近更新 更多