Code Duplication    Length = 59-59 lines in 3 locations

src/PBurggraf/CRC/CRC32/AbstractCRC32.php 1 location

@@ 10-68 (lines=59) @@
7
/**
8
 * @author Philip Burggraf <[email protected]>
9
 */
10
abstract class AbstractCRC32 extends AbstractCRC
11
{
12
    /**
13
     * @param string $buffer
14
     *
15
     * @return int
16
     */
17
    public function calculate(string $buffer): int
18
    {
19
        $crc = $this->init;
20
21
        $bufferLength = strlen($buffer);
22
23
        for ($bufferPosition = 0; $bufferPosition < $bufferLength; ++$bufferPosition) {
24
            $character = ord($buffer[$bufferPosition]);
25
26
            if ($this->reverseIn) {
27
                $character = $this->binaryReverse($character, 8);
28
            }
29
30
            $crc = $this->lookupTable[(($crc >> 24) ^ $character) & 0xff] ^ ($crc << 8);
31
            $crc &= 0xffffffff;
32
        }
33
34
        if ($this->reverseOut) {
35
            $crc = $this->binaryReverse($crc, 32);
36
        }
37
38
        return $crc ^ $this->xorOut;
39
    }
40
41
    /**
42
     * @param int $polynomial
43
     *
44
     * @return array
45
     */
46
    public function generateTable(int $polynomial): array
47
    {
48
        $tableSize = 256;
49
50
        $table = [];
51
52
        for ($iterator = 0; $iterator < $tableSize; ++$iterator) {
53
            $temp = 0;
54
            $a = ($iterator << 24);
55
            for ($j = 0; $j < 8; ++$j) {
56
                if ((($temp ^ $a) & 0x80000000) !== 0) {
57
                    $temp = (($temp << 1) ^ $polynomial);
58
                } else {
59
                    $temp <<= 1;
60
                }
61
                $a <<= 1;
62
            }
63
            $table[$iterator] = $temp & 0xffffffff;
64
        }
65
66
        return $table;
67
    }
68
}
69

src/PBurggraf/CRC/CRC16/AbstractCRC16.php 1 location

@@ 10-68 (lines=59) @@
7
/**
8
 * @author Philip Burggraf <[email protected]>
9
 */
10
abstract class AbstractCRC16 extends AbstractCRC
11
{
12
    /**
13
     * @param string $buffer
14
     *
15
     * @return int
16
     */
17
    public function calculate(string $buffer): int
18
    {
19
        $crc = $this->init;
20
21
        $bufferLength = strlen($buffer);
22
23
        for ($bufferPosition = 0; $bufferPosition < $bufferLength; ++$bufferPosition) {
24
            $character = ord($buffer[$bufferPosition]);
25
26
            if ($this->reverseIn) {
27
                $character = $this->binaryReverse($character, 8);
28
            }
29
30
            $crc = $this->lookupTable[(($crc >> 8) ^ $character) & 0xff] ^ ($crc << 8);
31
            $crc &= 0xffff;
32
        }
33
34
        if ($this->reverseOut) {
35
            $crc = $this->binaryReverse($crc, 16);
36
        }
37
38
        return $crc ^ $this->xorOut;
39
    }
40
41
    /**
42
     * @param int $polynomial
43
     *
44
     * @return array
45
     */
46
    public function generateTable(int $polynomial): array
47
    {
48
        $tableSize = 256;
49
50
        $table = [];
51
52
        for ($iterator = 0; $iterator < $tableSize; ++$iterator) {
53
            $temp = 0;
54
            $a = ($iterator << 8);
55
            for ($j = 0; $j < 8; ++$j) {
56
                if ((($temp ^ $a) & 0x8000) !== 0) {
57
                    $temp = (($temp << 1) ^ $polynomial);
58
                } else {
59
                    $temp <<= 1;
60
                }
61
                $a <<= 1;
62
            }
63
            $table[$iterator] = $temp & 0xffff;
64
        }
65
66
        return $table;
67
    }
68
}
69

src/PBurggraf/CRC/CRC24/AbstractCRC24.php 1 location

@@ 10-68 (lines=59) @@
7
/**
8
 * @author Philip Burggraf <[email protected]>
9
 */
10
abstract class AbstractCRC24 extends AbstractCRC
11
{
12
    /**
13
     * @param string $buffer
14
     *
15
     * @return int
16
     */
17
    public function calculate(string $buffer): int
18
    {
19
        $crc = $this->init;
20
21
        $bufferLength = strlen($buffer);
22
23
        for ($bufferPosition = 0; $bufferPosition < $bufferLength; ++$bufferPosition) {
24
            $character = ord($buffer[$bufferPosition]);
25
26
            if ($this->reverseIn) {
27
                $character = $this->binaryReverse($character, 8);
28
            }
29
30
            $crc = $this->lookupTable[(($crc >> 16) ^ $character) & 0xff] ^ ($crc << 8);
31
            $crc &= 0xffffff;
32
        }
33
34
        if ($this->reverseOut) {
35
            $crc = $this->binaryReverse($crc, 24);
36
        }
37
38
        return $crc ^ $this->xorOut;
39
    }
40
41
    /**
42
     * @param int $polynomial
43
     *
44
     * @return array
45
     */
46
    public function generateTable(int $polynomial): array
47
    {
48
        $tableSize = 256;
49
50
        $table = [];
51
52
        for ($iterator = 0; $iterator < $tableSize; ++$iterator) {
53
            $temp = 0;
54
            $a = ($iterator << 16);
55
            for ($j = 0; $j < 8; ++$j) {
56
                if ((($temp ^ $a) & 0x800000) !== 0) {
57
                    $temp = (($temp << 1) ^ $polynomial);
58
                } else {
59
                    $temp <<= 1;
60
                }
61
                $a <<= 1;
62
            }
63
            $table[$iterator] = $temp & 0xffffff;
64
        }
65
66
        return $table;
67
    }
68
}
69