| use strict; |
| use warnings; |
| use XML::Parser; |
| use XML::Simple; |
| |
| package CSIF_Structure_Check; |
| use File::Find; |
| return 1; |
| |
| # File list |
| my @all_list = (); |
| my $max_depth = 0; |
| # XML parsing result |
| my @data_tree = (); |
| ################################################################################## |
| # Subroutine: xml_parser() |
| # |
| # |
| ################################################################################## |
| sub xml_parser { |
| my ($user, $xml_file) = @_; |
| my @ret_list = (); |
| my $simple = XML::Simple->new(); |
| my $data_tree = $simple->XMLin($xml_file); |
| my $row_idx = 0; |
| while ($data_tree->{'path'}->[$row_idx]) { |
| if (ref($data_tree->{'path'}->[$row_idx]) eq "HASH") { |
| if ($user =~ $data_tree->{'path'}->[$row_idx]->{'group'}) { |
| push @ret_list, $data_tree->{'path'}->[$row_idx]->{'r_path'}; # if ($data_tree->{'path'}->[$row_idx]->{'r_path'} =~ /\.h$/); |
| } |
| } |
| $row_idx++; |
| } |
| |
| return @ret_list; |
| } |
| |
| ################################################################################## |
| # Subroutine: xml_format_checker() |
| # |
| # |
| ################################################################################## |
| sub xml_format_checker { |
| my ($xml_file) = @_; |
| my $parser = XML::Parser->new(ErrorContext => 2); |
| eval {$parser->parsefile($xml_file);}; |
| |
| if ($@) { |
| $@ =~ s/at \/.*?S//s; # Remove module line number |
| #print STDERR . "\nERROR in " . $xml_file . ":\n$@\n"; |
| return 0; |
| } else { |
| #print STDERR . " " . $xml_file . " is well-formed:\n"; |
| return 1; |
| } |
| } |
| |
| ################################################################################## |
| # Subroutine: crc32() |
| # - ARGUMENT: $input, $init_value, $polynomial |
| # - RETURN: $crc |
| ################################################################################## |
| sub crc32 { |
| 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; |
| } |
| |
| |
| ################################################################################## |
| # Subroutine: file_checksum_input() |
| # - ARGUMENT: $filename (e.g., ../common/interface/modem/mt6297/CSIF/xxx.h) |
| # - RETURN: $str |
| ################################################################################## |
| sub file_checksum_input { |
| my ($filename) = @_; |
| open(FILE, $filename) or die "Could not open $filename, $!"; |
| my $str = ""; |
| my @keyword = <FILE>; |
| close(FILE); |
| $str = join("", @keyword); |
| |
| return $str; |
| } |
| |
| ################################################################################## |
| # Subroutine: folder_checksum_input() |
| # - ARGUMENT: $folder_name (e.g., ../common/interface/modem/mt6297/CSIF/) |
| # - RETURN: $total_str |
| ################################################################################## |
| sub folder_checksum_input { |
| my ($folder_name) = @_; |
| find({ |
| wanted => \&wanted |
| }, $folder_name); |
| my $str = ""; |
| my $total_str = ""; |
| foreach (sort{crc32($a)<=>crc32($b)}@all_list) |
| { |
| open(FILE, $_); |
| my @keyword = <FILE>; |
| close(FILE); |
| $str = join("", @keyword); |
| $total_str = join($total_str, "", $str); |
| } |
| @all_list = (); |
| |
| return $total_str; |
| } |
| |
| ################################################################################## |
| # Subroutine: merge_checksum_input() |
| # - ARGUMENT: @path_list |
| # - RETURN: $checksum_input |
| ################################################################################## |
| sub merge_checksum_input{ |
| my (@path_list) = @_; |
| my $checksum_input = ""; |
| my $checksum_str = ""; |
| foreach (@path_list) |
| { |
| print $_ . "\n"; |
| if($_ =~ /\.h$/) { |
| # Read out file contents |
| $checksum_str = file_checksum_input($_); |
| # Merge file contents |
| $checksum_input = join($checksum_input, "", $checksum_str); |
| } else { |
| $checksum_str = folder_checksum_input($_); |
| $checksum_input = join($checksum_input, "", $checksum_str); |
| } |
| } |
| |
| return $checksum_input; |
| } |
| |
| ################################################################################## |
| # Subroutine: calc_folder_depth() |
| # - ARGUMENT: $folder_path |
| # - RETURN: $depth |
| ################################################################################## |
| sub calc_folder_depth { |
| my ($folder_path) = @_; |
| my $depth = $folder_path =~ tr /\///; # Contain the number of substitutions made |
| |
| return $depth; |
| } |
| |
| ################################################################################## |
| # Subroutine: preprocess_depth() |
| # - ARGUMENT: |
| # - RETURN: |
| ################################################################################## |
| sub preprocess_depth { |
| my $depth = calc_folder_depth($File::Find::dir); # Contain the number of substitutions made |
| return @_ if $depth < $max_depth; |
| |
| return; |
| } |
| |
| ################################################################################## |
| # Subroutine: wanted() |
| # - ARGUMENT: |
| # - RETURN: |
| ################################################################################## |
| sub wanted { |
| push @all_list, $File::Find::name if ($_ =~ /\.h$/); |
| } |
| |
| ################################################################################## |
| # Subroutine: get_csif_checksum_value() |
| # - ARGUMENT: |
| # $user: 0 => CSIF; |
| # 1 => SLM; |
| # 2 => ST; |
| # 3 => other; |
| # $csif_folder_path: ../common/interface/modem/mt6297/CSIF |
| # - RETURN: $checksum_value |
| ################################################################################## |
| sub get_csif_checksum_value { |
| my ($user, $csif_folder_path, $xml_file) = @_; |
| |
| # Initialize global variables |
| @all_list = (); |
| $max_depth = 0; |
| |
| #my $method = 0; |
| |
| if($user == 0) { |
| print "User 0: $csif_folder_path\n"; |
| #if ($method) { |
| # print "Method " . $method . "\n"; |
| # my $universal_comm_csif_depth = &calc_folder_depth($csif_folder_path); |
| # # Modify global variable |
| # $max_depth = $universal_comm_csif_depth + 1; |
| # # Preprocess CSIF folder w/ depth 1 and aggregate files into @all_list |
| # find({ |
| # preprocess => \&preprocess_depth, |
| # wanted => \&wanted, |
| # }, $csif_folder_path); |
| #} else { |
| @all_list = xml_parser($user, $xml_file); |
| #} |
| my $checksum_value = sprintf("%u", &crc32(&merge_checksum_input(@all_list))); |
| $checksum_value = join($checksum_value, "", "u"); |
| return $checksum_value; |
| } elsif ($user == 1) { |
| my @CSIF_SLM = (); |
| #if ($method) { |
| # print "User 1: @CSIF_SLM\n"; |
| # print "Method " . $method . "\n"; |
| # my $CSIF_SLM_PATH = join($csif_folder_path, "", "/slm/"); |
| # @CSIF_SLM = ($csif_folder_path, $CSIF_SLM_PATH); |
| #} else { |
| # @CSIF_SLM = ($csif_folder_path, xml_parser($user, $xml_file)); |
| @CSIF_SLM = xml_parser($user, $xml_file); |
| #} |
| my $checksum_value = sprintf("%u", &crc32(&merge_checksum_input(@CSIF_SLM))); |
| $checksum_value = join($checksum_value, "", "u"); |
| return $checksum_value; |
| } elsif ($user == 2) { |
| my @CSIF_NL1FWK = (); |
| #if ($method) { |
| # print "User 1: @CSIF_NL1FWK\n"; |
| # print "Method " . $method . "\n"; |
| # my $CSIF_NL1FWK_PATH = join($csif_folder_path, "", "/slm/"); |
| # @CSIF_NL1FWK = ($csif_folder_path, $CSIF_NL1FWK_PATH); |
| #} else { |
| # @CSIF_NL1FWK = ($csif_folder_path, xml_parser($user, $xml_file)); |
| @CSIF_NL1FWK = xml_parser($user, $xml_file); |
| #} |
| my $checksum_value = sprintf("%u", &crc32(&merge_checksum_input(@CSIF_NL1FWK))); |
| $checksum_value = join($checksum_value, "", "u"); |
| return $checksum_value; |
| }elsif ($user == 3) { |
| #my $universal_comm_csif_depth = &calc_folder_depth($csif_folder_path); |
| # Modify global variable |
| #$max_depth = $universal_comm_csif_depth + 1; |
| # Preprocess CSIF folder w/ depth 1 and aggregate files into @all_list |
| #find({ |
| # preprocess => \&preprocess_depth, |
| # wanted => \&wanted, |
| #}, $csif_folder_path); |
| @all_list = (@all_list, xml_parser($user, $xml_file)); |
| #foreach (@all_list) { |
| # print $_ . "\n"; |
| #} |
| my $checksum_value = sprintf("%u", &crc32(&merge_checksum_input(@all_list))); |
| $checksum_value = join($checksum_value, "", "u"); |
| return $checksum_value; |
| } elsif ($user == 4) { |
| #my $universal_comm_csif_depth = &calc_folder_depth($csif_folder_path); |
| # Modify global variable |
| #$max_depth = $universal_comm_csif_depth + 1; |
| # Preprocess CSIF folder w/ depth 1 and aggregate files into @all_list |
| #find({ |
| # preprocess => \&preprocess_depth, |
| # wanted => \&wanted, |
| #}, $csif_folder_path); |
| @all_list = (@all_list, xml_parser($user, $xml_file)); |
| #foreach (@all_list) { |
| # print $_ . "\n"; |
| #} |
| my $checksum_value = sprintf("%u", &crc32(&merge_checksum_input(@all_list))); |
| $checksum_value = join($checksum_value, "", "u"); |
| return $checksum_value; |
| } else { |
| return 0; |
| } |
| } |
| |
| ############################## |
| # test code |
| ############################## |
| #my $FILENAME= ($ARGV[0]); |
| #printf "%s",get_cuif_checksum_value(0,$FILENAME); |
| #print "\n"; |