[Vorbis-dev] Another question, about the CRC computing

Simon OUALID symon at tatouage.fr
Sat Jul 30 03:30:15 PDT 2005


Hello again,

Thanks to Ralph's help, I continued my work on the little 100% java api 
I am writing for editing tags. Everything seems ok in the data my code 
write in the ogg file but the CRC. It looks like I don't use the right 
polynom or I don't compute the CRC over the good sequence of bytes.

Should I compute it over the bytes between the two "OggS" sequences ? 
First "OggS" included ? I also thought it was something with the way I 
compute the CRC, so I putted here the code I use to calculate it... :)

I also tried with the JDK's api CRC32 class. But I think it doesn't use 
the right polynom (must be a "zip" one ?).

Any help would be greatly appreciated. Since I am 32bits far from a 
working code ! ^_^

Thanks in advance.

Simon

// CRC32.java

/**
 * Implements a general CRC class that lets you change the polynomial.
 */
public class *CRC32* {
    private int polynomial = 0x04c11db7;
    private int crc = 0;

    /**
     * Calculates a CRC value for a byte to be used by CRC calculation
functions.
     */
    private int CRC32Value(int i) {
        int crc = i;

        for (int j = 8; j > 0; j--) {
            if ((crc & 1) == 1)
                crc = (crc >>> 1) ^ polynomial;
            else
                crc >>>= 1;
        }
        return crc;

    }

    /**
     * Calculates the CRC-32 of a block of data all at once
     */
    public int calculateCRC32(byte[] buffer, int offset, int length) {
        for (int i = offset; i < offset + length; i++) {
            int tmp1 = (crc >>> 8) & 0x00FFFFFF;
            int tmp2 = CRC32Value(((int) crc ^ buffer[i]) & 0xff);
            crc = tmp1 ^ tmp2;
        }
        return crc;
    }

    /**
     * Calculates the CRC-32 of a block of data all at once
     */
    public int calculateCRC32(byte[] buffer) {
        return calculateCRC32(buffer, 0, buffer.length);
    }

    /**
     * Resets the state to process more data.
     */
    public void reset() {
        crc = 0;
    }

    public void setPolynomial(int polynomial) {
        this.polynomial = polynomial;
    }



More information about the Vorbis-dev mailing list