File: crc32.pl

package info (click to toggle)
mariadb 1%3A11.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 772,520 kB
  • sloc: ansic: 2,414,714; cpp: 1,791,394; asm: 381,336; perl: 62,905; sh: 49,647; pascal: 40,897; java: 39,363; python: 20,791; yacc: 20,432; sql: 17,907; xml: 12,344; ruby: 8,544; cs: 6,542; makefile: 6,145; ada: 1,879; lex: 1,193; javascript: 996; objc: 80; tcl: 73; awk: 46; php: 22
file content (56 lines) | stat: -rw-r--r-- 1,459 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# The following is Public Domain / Creative Commons CC0 from
# http://billauer.co.il/blog/2011/05/perl-crc32-crc-xs-module/

sub mycrc32 {
 my ($input, $init_value, $polynomial) = @_;

 $init_value = 0 unless (defined $init_value);
 $polynomial = 0xedb88320 unless (defined $polynomial);

 my @lookup_table;

 for (my $i=0; $i<256; $i++) {
   my $x = $i;
   for (my $j=0; $j<8; $j++) {
     if ($x & 1) {
       $x = ($x >> 1) ^ $polynomial;
     } else {
       $x = $x >> 1;
     }
   }
   push @lookup_table, $x;
 }

 my $crc = $init_value ^ 0xffffffff;

 foreach my $x (unpack ('C*', $input)) {
   $crc = (($crc >> 8) & 0xffffff) ^ $lookup_table[ ($crc ^ $x) & 0xff ];
 }

 $crc = $crc ^ 0xffffffff;

 return $crc;
}


# Fix the checksum of an InnoDB tablespace page.
# Inputs:
#   $page        A bytestring with the page data.
#   $full_crc32  Checksum type, see get_full_crc32() in innodb-util.pl
# Returns: the modified page as a bytestring.
sub fix_page_crc {
  my ($page, $full_crc32)= @_;
  my $ps= length($page);
  my $polynomial = 0x82f63b78; # CRC-32C
  if ($full_crc32) {
    my $ck = mycrc32(substr($page, 0, $ps - 4), 0, $polynomial);
    substr($page, $ps - 4, 4) = pack("N", $ck);
  } else {
    my $ck= pack("N",
                 mycrc32(substr($page, 4, 22), 0, $polynomial) ^
                 mycrc32(substr($page, 38, $ps - 38 - 8), 0, $polynomial));
    substr($page, 0, 4)= $ck;
    substr($page, $ps-8, 4)= $ck;
  }
  return $page;
}