douxi1738 2012-07-10 12:44
浏览 174
已采纳

CRC32从C ++ / C#到PHP

I'm not good at algorithms like CRC32, MD5 etc. so I'm even having difficulty asking the question :)

Basically there is a C# application that uses

[DllImport("Crc32.dll")]
private static extern UInt32 CRC32Calc(UInt32 crc32, byte[] buffer, uint length);

and further down in the code uses it in a method Generate like this

UInt32 crc = CRC32Calc(crcSeed, rawData, (uint)rawData.Length);

while using a certain crcSeed value.

My job is to rework the entire Generate method into a PHP function while preserving the correct CRC calculation.

I think that the PHP's

int crc32 ( string $str )

function will not work because I can't set the crcSeed. So my question is:

how can I make the exact crc32 calculation in PHP without resorting to outside dll's etc. so I can use the code an a Linux machine?

EDIT:

CRC is calculated in chunks with the crcSeed being the initial one.

The CRC32Calc method is actually using the SCTP CRC-32C version, so now, only a PHP implementation is needed.

  • 写回答

1条回答 默认 最新

  • doubailian4459 2012-07-10 13:17
    关注

    In general, data is processed a chunk at a time instead of all at once, in order to have the memory usage of an application constant as opposed to proportional to the length of the input. As a result, you need functions that can process data a chunk at a time. crc functions are written to support this, so they accept the crc value so far from the previous chunks as an argument, and the function then computes the crc value after applying the data in the current chunk.

    updated_crc = crc(last_crc, this_chunk_data_pointer, this_chunk_length)
    

    The crc value so far is what you are calling the "seed".

    The very first crc you provide for the first chunk is the crc value of a zero-length sequence. It is defined by the crc standard in use. Typically it's zero, but it can be other values such as all binary ones.

    first_crc = crc(0, first_chunk_data_pointer, first_chunk_length)
    

    A quick google search does not turn up a built-in function in php that supports computing crc's in chunks -- only all at once. You may need to roll your own. You can find many examples of efficient crc calculations online, which generally use a table of 256 crc's. First you need to know which crc you are calculating based on what it's for. Is it the crc-32 used in gzip, png, ethernet etc.? Is it the crc-32c used in iSCSI? Something else?

    Update:

    Ok, so it's crc-32c. You can look here for a crc code generator that supports that crc (as well as many others).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵