root/src/add-ons/kernel/file_systems/btrfs/crc_table.cpp
/*
 *  This software is part of the Haiku distribution and is covered
 *  by the MIT License.
 *
 *  Copyright (c) 2003 Tyler Dauwalder, tyler@dauwalder.net
 */

/* file crc_table.cpp
 *
 * Standalone program to generate the CRC table used for calculating
 * UDF tag id CRC values.
 *
 * This code based off of crc code in UDF-2.50 specs, as permitted.
 * See UDF-2.50 6.5 for more information.
 *
 * Reflected version by Jéme Duval
 */



#include "system_dependencies.h"


typedef unsigned int uint32 ;


uint32
reflect32 (uint32 b)
{
        uint32 rw = 0;

        for (int i = 0; i < 32; i++) {
                if (b & 1)
                        rw |= 1 << (31 - i);
                b >>= 1;
        }
        return rw;
}


int
main(int argc, char* argv[]) {
        uint32 crc, poly;

        if (argc != 2) {
                fprintf(stderr, "USAGE: crc_table <octal polynomial=3667067501 for btrfs>\n");
                return 0;
        }
        sscanf(argv[1], "%lo", &poly);
        printf("//! CRC 0%o table, as generated by crc_table.cpp\n", poly);
        printf("static uint32 kCrcTable[256] = {\n\t");
        for (int n = 0; n < 256; n++) {
                crc = reflect32(n);
                for (int i = 0; i < 8; i++) {
                        if (crc & 0x80000000)
                                crc = (crc << 1) ^ poly;
                        else
                                crc <<= 1;
                }
                crc = reflect32(crc);
                printf("0x%08x%s%s", crc, (n != 255 ? "," : ""), (n % 6 != 5 ? " " : ""));
                if (n % 6 == 5)
                        printf("\n\t");
        }
        printf("\n};\n");
        return 0;
}