#!/usr/bin/perl -w # SPDX-License-Identifier: GPL-2.0-or-later # # Script to generate SMB1 error mapping tables. # # Copyright (C) 2026 KylinSoft Co., Ltd. All rights reserved. # Author(s): Huiwen He # ChenXiaoSong # use strict; if ($#ARGV != 1) { print STDERR "Usage: $0 \n"; exit(2); } # Parse input parameters and extract filenames my $in_file = $ARGV[0]; my $out_file = $ARGV[1]; my $input_name = (split m|/|, $in_file)[-1]; my $output_name = (split m|/|, $out_file)[-1]; my $script_name = (split m|/|, $0)[-1]; my @list = (); my %seen = (); my $current_class = ""; # Parse annotated entries from the input file open(my $in, "<", $in_file) or die "Cannot open $in_file: $!"; if ($in_file =~ /nterr\.h$/) { while (<$in>) { # Handle backslash line continuation $_ .= <$in> while s/\\\s*\n//; # Match #define NT_STATUS_... followed by // CLASS, CODE or /* CLASS, CODE */ my $re = qr{^\s*#define\s+(NT_STATUS_[A-Za-z0-9_]+)\s+(.+?)\s*} . qr{(?://\s*|/\*\s*)([A-Z0-9_]+)\s*,\s*([A-Za-z0-9_]+)}; if (/$re/) { my ($name, $val_str, $class, $code) = ($1, $2, $3, $4); # Skip duplicate macro names next if $seen{$name}++; # Clean up value string (remove parens, spaces) $val_str =~ s/[\s\(\)]//g; my $val = 0; foreach my $part (split(/\|/, $val_str)) { $val |= hex($part); } push @list, { val => $val, name => $name, class => $class, code => $code }; } elsif (/^\s*#define\s+NT_STATUS_.*(?:\/\/|\/\*)/) { # Error if macro has a comment (// or /*) but fails mapping format die "Error: Invalid mapping comment format in $in_file: $_"; } } } elsif ($in_file =~ /smberr\.h$/) { while (<$in>) { # Handle backslash line continuation $_ .= <$in> while s/\\\s*\n//; # Detect current error class from header comments (ERRDOS or ERRSRV) if (/generated with the (\w+) error class/) { $current_class = $1; } # Match #define ERR/Err_... followed by // -POSIX_ERR or /* -POSIX_ERR */ if (/^\s*#define\s+((?:ERR|Err)[A-Za-z0-9_]+)\s+([0-9a-fA-FxX]+)\s*(?:\/\/|\/\*)\s*(-[A-Z0-9_]+)/) { my ($name, $val_str, $error) = ($1, $2, $3); my $val = ($val_str =~ /^0x/i) ? hex($val_str) : $val_str; push @list, { val => $val, name => $name, error => $error, class => $current_class }; } elsif ($current_class && /^\s*#define\s+(?:ERR|Err).*?(?:\/\/|\/\*)/) { # Error if macro has a comment (// or /*) but fails mapping format die "Error: Invalid mapping comment format in $in_file: $_"; } } } close($in); # Fail if no entries were found to avoid broken builds die "Error: No mapping entries found in $in_file\n" unless @list; # Sort entries numerically by value @list = sort { $a->{val} <=> $b->{val} } @list; # Generate the C mapping table output file open(my $out, ">", $out_file) or die "Cannot open $out_file: $!"; print $out "/* Autogenerated from $input_name by $script_name */\n\n"; if ($output_name eq "smb1_mapping_table.c") { # Generate NT status -> DOS error mapping file my $count = scalar @list; my $full_names = ""; for (my $i = 0; $i < $count; $i++) { my $e = $list[$i]; my $val = $e->{val}; $full_names .= $e->{name}; # Merge synonyms if ($i < $count - 1 && $list[$i + 1]->{val} == $val) { $full_names .= " or "; next; } printf $out "\t{ %s, %s, 0x%08x, \"%s\" },\n", $e->{class}, $e->{code}, $val, $full_names; $full_names = ""; } } elsif ($output_name eq "smb1_err_dos_map.c" || $output_name eq "smb1_err_srv_map.c") { # Generate SMB1 error -> POSIX error mapping file # Filtered by exact output filename my $filter = ($output_name eq "smb1_err_dos_map.c") ? "ERRDOS" : "ERRSRV"; foreach my $e (@list) { if (!$filter || $e->{class} eq $filter) { printf $out "\t{%s, %s},\n", $e->{name}, $e->{error}; } } } else { die "Error: Unsupported output target: $output_name\n"; } close($out);