mrhooray / crc-rs

Rust implementation of CRC(16, 32, 64) with support of various standards

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`CRC_32_CKSUM` does not imitate the behaviour of `cksum`

7Ji opened this issue · comments

When the cksum calculates its output, it would append a byte stream of octal numbers representing the length of the original input. However, using CRC_32_CKSUM alone does not imitate the behaviour and some additional logic is needed to get the same output as cksum.

A test program:

use crc;

const CKSUM: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_CKSUM);

fn main() {
    let arg = std::env::args().nth(1).unwrap();
    let input = arg.as_bytes();
    let mut digest = CKSUM.digest();
    digest.update(input);
    let mut i = input.len();
    while i > 0 {
        let len_oct: u8 = (i & 0xFF).try_into().unwrap();
        digest.update(&[len_oct]);
        i >>= 8;
    }
    println!("No length: {}, has length: {}", CKSUM.checksum(input), digest.finalize());
}

Result:

> cargo run 123456789
No length: 1985902208, has length: 930766865
> printf 123456789 | cksum
930766865 9

Should this be considered a bug or a feature?