2

我需要对文本文件进行字节移位。我对 perl 一无所知,但我在 perl 中找到了一段完美的代码,称为moz-byteshift.pl文档)。这正是我想做的,但我需要在 C# 中完成。

下面是 perl 文件的源代码:

#!/usr/bin/perl

# To perform a byteshift of 7
#   To decode: moz-byteshift.pl -s -7 <infile >outfile
#   To encode: moz-byteshift.pl -s  7 <infile >outfile

# To perform a byteshift of 13
#   To decode: moz-byteshift.pl -s -13 <infile >outfile
#   To encode: moz-byteshift.pl -s  13 <infile >outfile

use encoding 'latin1';
use strict;
use Getopt::Std;

use vars qw/$opt_s/;

getopts("s:");
if(!defined $opt_s) {
  die "Missing shift\n";
}

my $buffer;
while(1) {
  binmode(STDIN, ":raw");
  my $n=sysread STDIN, $buffer, 1;
  if($n == 0) {
    last;
  }
  my $byte = unpack("c", $buffer);
  $byte += 512 + $opt_s;
  $buffer = pack("c", $byte);
  binmode(STDOUT, ":raw");
  syswrite STDOUT, $buffer, 1;
}

如果有人至少可以解释 perl 脚本是如何工作的,那就太好了。C# 中等效的示例代码会更好。=)

谢谢您的帮助。

4

3 回答 3

4

没什么好说的。它一次读取一个文件,将每个字节的值调整为任意值(通过 -s 标志指定),然后写出调整后的字节。它是文本文件的 ROT-13 加密的二进制等价物。

其余的细节是特定于 Perl 如何做这些事情的。getopts() 是一个处理命令行开关的函数(来自 Getopt::Std 模块)。binmode() 将文件句柄置于原始模式以绕过 Perl 通常在 I/O 期间执行的任何魔法。sysread() 和 syswrite() 函数用于低级流访问。pack() 和 unpack() 函数用于读写二进制数据;Perl 不做原生类型。

在 C 中重新实现这将是微不足道的。我建议这样做(并在需要时从 C# 绑定到它)而不是直接移植到 C#。

于 2009-05-15T04:05:28.317 回答
1

代码的作用是:从标准输入一个一个地读取每个字节(在将其切换到原始模式后,不会发生转换)。解包获取刚刚读取的字符的字节值,因此“0”读取变为 0x30。选择 latin1 编码以使这种转换是一致的(例如,参见http://www.cs.tut.fi/~jkorpela/latin9.html)。

然后使用 -s 选项在命令行中指定的值与 512 一起添加到此字节以模拟模运算。这样,-s 0、-s 256 等是等效的。我不确定为什么需要这样做,因为我会假设下面的包已经解决了这个问题,但我认为他们一定有充分的理由把它放在那里。

然后,将原始字节写入标准输入。

当您在包含字符 012345 的文件上运行它时会发生以下情况(我将数据放在DATA部分中):

E:\Test> byteshift.pl -s 1 | xxd
0000000: 3132 3334 3536 0b                        123456.

每个字节值加一。

E:\Test> byteshift.pl -s 257 | xxd
0000000: 3132 3334 3536 0b                        123456.

请记住 257 % 256 = 1。即:

$byte += $opt_s;
$byte %= 256;

相当于代码中使用的单步。

很久以后:好的,我不知道 C#,但这是我能够使用在线文档拼凑而成的。知道 C# 的人应该解决这个问题:

using System;
using System.IO;

class BinaryRW {
    static void Main(string[] args) {
        BinaryWriter binWriter = new BinaryWriter(
                Console.OpenStandardOutput()
                );
        BinaryReader binReader = new BinaryReader(
                Console.OpenStandardInput()
                );

        int delta;

        if ( args.Length < 1 
                || ! int.TryParse( args[0], out delta ) )
        {
            Console.WriteLine(
                    "Provide a non-negative delta on the command line"
                    );
        } 
        else {       
            try  {
                while ( true ) {
                    int bin = binReader.ReadByte();
                    byte bout = (byte) ( ( bin + delta ) % 256 );
                    binWriter.Write( bout );
                }
            }

            catch(EndOfStreamException) { }

            catch(ObjectDisposedException) { }

            catch(IOException e) {
                Console.WriteLine( e );        
            }

            finally {
                binWriter.Close();
                binReader.Close();

            }
        }
    }
}

E:\Test> xxd bin
0000000: 3031 3233 3435 0d0a 0d0a                 012345....

E:\Test> b 0 < bin | xxd
0000000: 3031 3233 3435 0d0a 0d0a                 012345....

E:\Test> b 32 < bin | xxd
0000000: 5051 5253 5455 2d2a 2d2a                 PQRSTU-*-*

E:\Test> b 257 < bin | xxd
0000000: 3132 3334 3536 0e0b 0e0b                 123456....
于 2009-05-15T13:29:50.473 回答
1

从其他答案来看,C# 中的等价物看起来像这样:

using(Stream sIn = new FileStream(inPath))
{
  using(Stream sOut = new FileStream(outPath))
  {
    int b = sIn.ReadByte();
    while(b >= 0)
    {
      b = (byte)b+1; // or some other value
      sOut.WriteByte((byte)b);
      b = sIn.ReadByte();
    }
    sOut.Close();
  }
  sIn.Close();
}
于 2009-05-15T13:36:20.520 回答