| 1 |
# The following is Public Domain / Creative Commons CC0 from |
| 2 |
# http://billauer.co.il/blog/2011/05/perl-crc32-crc-xs-module/ |
| 3 |
|
| 4 |
sub mycrc32 { |
| 5 |
my ($input, $init_value, $polynomial) = @_; |
| 6 |
|
| 7 |
$init_value = 0 unless (defined $init_value); |
| 8 |
$polynomial = 0xedb88320 unless (defined $polynomial); |
| 9 |
|
| 10 |
my @lookup_table; |
| 11 |
|
| 12 |
for (my $i=0; $i<256; $i++) { |
| 13 |
my $x = $i; |
| 14 |
for (my $j=0; $j<8; $j++) { |
| 15 |
if ($x & 1) { |
| 16 |
$x = ($x >> 1) ^ $polynomial; |
| 17 |
} else { |
| 18 |
$x = $x >> 1; |
| 19 |
} |
| 20 |
} |
| 21 |
push @lookup_table, $x; |
| 22 |
} |
| 23 |
|
| 24 |
my $crc = $init_value ^ 0xffffffff; |
| 25 |
|
| 26 |
foreach my $x (unpack ('C*', $input)) { |
| 27 |
$crc = (($crc >> 8) & 0xffffff) ^ $lookup_table[ ($crc ^ $x) & 0xff ]; |
| 28 |
} |
| 29 |
|
| 30 |
$crc = $crc ^ 0xffffffff; |
| 31 |
|
| 32 |
return $crc; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
# Fix the checksum of an InnoDB tablespace page. |
| 37 |
# Inputs: |
| 38 |
# $page A bytestring with the page data. |
| 39 |
# $full_crc32 Checksum type, see get_full_crc32() in innodb-util.pl |
| 40 |
# Returns: the modified page as a bytestring. |
| 41 |
sub fix_page_crc { |
| 42 |
my ($page, $full_crc32)= @_; |
| 43 |
my $ps= length($page); |
| 44 |
my $polynomial = 0x82f63b78; # CRC-32C |
| 45 |
if ($full_crc32) { |
| 46 |
my $ck = mycrc32(substr($page, 0, $ps - 4), 0, $polynomial); |
| 47 |
substr($page, $ps - 4, 4) = pack("N", $ck); |
| 48 |
} else { |
| 49 |
my $ck= pack("N", |
| 50 |
mycrc32(substr($page, 4, 22), 0, $polynomial) ^ |
| 51 |
mycrc32(substr($page, 38, $ps - 38 - 8), 0, $polynomial)); |
| 52 |
substr($page, 0, 4)= $ck; |
| 53 |
substr($page, $ps-8, 4)= $ck; |
| 54 |
} |
| 55 |
return $page; |
| 56 |
} |