简体   繁体   English

如何在C#中复制Perl的解包功能?

[英]How can I replicate Perl's unpack functionality in C#?

I am trying to recreate a Perl script in C# but have a problem creating a checksum value that a target system needs. 我试图在C#中重新创建一个Perl脚本,但是在创建目标系统所需的校验和值时遇到问题。

In Perl this checksum is calculated using the unpack function: 在Perl中,使用unpack函数计算此校验和:

while (<PACKAGE>) {
    $checksum += unpack("%32C*", $_);
}
$checksum %= 32767;
close(PACKAGE);

where PACKAGE is the .tar file input stream 其中PACKAGE是.tar文件输入流

I need to replicate this in C# but can't find a means of replicating that unpack function. 我需要在C#中复制它,但无法找到复制该unpack函数的方法。

All help appreciated! 所有帮助赞赏!

(I know there are much better checksum calculations available but can't change target system so can't change calculation) (我知道有更好的校验和计算可用但不能改变目标系统因此无法改变计算)

There seems to be a library in Mono called DataConvert that was written to provide facilities similar to Perl's pack/unpack. Mono中似乎有一个名为DataConvert的库,它是为了提供类似于Perl的打包/解包而设置的。 Does this do what you need? 这样做你需要的吗?

Perkl's unpack is described here and here . 这里这里描述 Perkl的解包。 From that you should be able to write an equivalent in C#. 从那以后你应该能够在C#中编写一个等价物。

To supplement Mitch Wheat's comment, here's a Java implementation (which does a single block only). 为了补充Mitch Wheat的评论,这里是一个Java实现(仅执行一个块)。 I'm sure you'll find a way to convert it into C#, and to do multiple blocks. 我相信你会找到一种方法将它转换为C#,并做多个块。

int sum = 0;
for (byte b : buffer) {
    sum += (int) b & 255;
}
return sum % 32767;

Hope this helps! 希望这可以帮助!

In my testing here, unpack with %32C appears to be the additive sum of the bytes, limited to 32 bits. 在我的测试中,使用%32C解压缩似乎是字节的附加和,限制为32位。

print unpack("%32C*", 'A');
65
print unpack("%32C*", 'AA');
130

It shouldn't be hard to reproduce that. 重现这一点应该不难。

Based on the comments of Chris Jester-Young and piCookie, I developed the following function. 根据Chris Jester-Young和piCookie的评论,我开发了以下功能。 I hope you find it useful. 希望对你有帮助。

int fileCheckSum(const char *fileName)
{
   FILE *fp;
   long fileSize;
   char *fileBuffer;
   size_t result;
   int sum = 0;
   long index;

   fp = fopen(fileName, "rb");
   if (fp == NULL)
   {
      fputs ("File error",stderr); 
      exit (1);
   }

   fseek(fp, 0L, SEEK_END);
   fileSize = ftell(fp);
   fseek(fp, 0L, SEEK_SET);

   fileBuffer = (char*) malloc (sizeof(char) * fileSize);   
   if (fileBuffer == NULL)
   {
      fputs ("Memory error",stderr);
      exit (2);
   }

   result = fread(fileBuffer, 1, fileSize, fp);   
   if (result != fileSize)
   {
      fputs ("Reading error", stderr);
      if (fileBuffer != NULL)
         free(fileBuffer);

      exit (3);
   }

   for (index = 0; index < fileSize; index++)
   {
      sum += fileBuffer[index] & 255;
   }

   fclose(fp);
   if (fileBuffer != NULL)
      free(fileBuffer);

   return sum % 32767;  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中复制任务管理器的切换用户功能? - How can I replicate Task Manager's switch user functionality in C#? 我应该如何在Java中复制C#的&#39;using&#39;语句的功能? - How should I replicate the functionality of C#'s 'using' statement in Java? 如何使用C#中的扩展事件复制sp_trace的功能? - How can I replicate the functionality of sp_trace with extended events from C#? 如何在C#中复制Python的已排序内置函数的行为? - How can I replicate the behavior of Python's sorted built-in function in C#? 如何在 PHP 中复制这个 C# 哈希? (toByteArray(), ComputeHash()) - How can I replicate this C# hashing in PHP? (toByteArray(), ComputeHash()) 使用C#Restsharp,如何复制表单发布? - Using C# Restsharp, how can I replicate a form post? 如何在C#中模拟Microsoft Excel的Solver功能(GRG Nonlinear)? - How can I emulate Microsoft Excel's Solver functionality (GRG Nonlinear) in C#? 在C#中是否有与Perl打包和解压缩的等价物? - Is there an equivalent in C# to pack and unpack from Perl? 尝试在Perl中复制C#哈希(在Linux上) - Trying to replicate C# hashing in Perl (on Linux) 如何在 C# 中复制 PHP 的 PRNG 算法实现? - How do I replicate PHP's PRNG algorithm implementation in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM