|
| 1 | +#!/usr/bin/perl -w |
| 2 | +# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | +# |
| 4 | +# Script to generate SMB1 error mapping tables. |
| 5 | +# |
| 6 | +# Copyright (C) 2026 KylinSoft Co., Ltd. All rights reserved. |
| 7 | +# Author(s): Huiwen He <hehuiwen@kylinos.cn> |
| 8 | +# ChenXiaoSong <chenxiaosong@kylinos.cn> |
| 9 | +# |
| 10 | +use strict; |
| 11 | + |
| 12 | +if ($#ARGV != 1) { |
| 13 | + print STDERR "Usage: $0 <in-file> <out-file>\n"; |
| 14 | + exit(2); |
| 15 | +} |
| 16 | + |
| 17 | +# Parse input parameters and extract filenames |
| 18 | +my $in_file = $ARGV[0]; |
| 19 | +my $out_file = $ARGV[1]; |
| 20 | +my $input_name = (split m|/|, $in_file)[-1]; |
| 21 | +my $output_name = (split m|/|, $out_file)[-1]; |
| 22 | +my $script_name = (split m|/|, $0)[-1]; |
| 23 | +my @list = (); |
| 24 | +my %seen = (); |
| 25 | + |
| 26 | +# Parse annotated entries from the input file |
| 27 | +open(my $in, "<", $in_file) or die "Cannot open $in_file: $!"; |
| 28 | +if ($in_file =~ /nterr\.h$/) { |
| 29 | + while (<$in>) { |
| 30 | + # Handle backslash line continuation |
| 31 | + $_ .= <$in> while s/\\\s*\n//; |
| 32 | + |
| 33 | + # Match #define NT_STATUS_... followed by // CLASS, CODE or /* CLASS, CODE */ |
| 34 | + my $re = qr{^\s*#define\s+(NT_STATUS_[A-Za-z0-9_]+)\s+(.+?)\s*} . |
| 35 | + qr{(?://\s*|/\*\s*)([A-Z0-9_]+)\s*,\s*([A-Za-z0-9_]+)}; |
| 36 | + |
| 37 | + if (/$re/) { |
| 38 | + my ($name, $val_str, $class, $code) = ($1, $2, $3, $4); |
| 39 | + |
| 40 | + # Skip duplicate macro names |
| 41 | + next if $seen{$name}++; |
| 42 | + |
| 43 | + # Clean up value string (remove parens, spaces) |
| 44 | + $val_str =~ s/[\s\(\)]//g; |
| 45 | + my $val = 0; |
| 46 | + foreach my $part (split(/\|/, $val_str)) { |
| 47 | + $val |= hex($part); |
| 48 | + } |
| 49 | + push @list, { val => $val, name => $name, class => $class, code => $code }; |
| 50 | + } elsif (/^\s*#define\s+NT_STATUS_.*(?:\/\/|\/\*)/) { |
| 51 | + # Error if macro has a comment (// or /*) but fails mapping format |
| 52 | + die "Error: Invalid mapping comment format in $in_file: $_"; |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +close($in); |
| 57 | + |
| 58 | +# Fail if no entries were found to avoid broken builds |
| 59 | +die "Error: No mapping entries found in $in_file\n" unless @list; |
| 60 | + |
| 61 | +# Sort entries numerically by value |
| 62 | +@list = sort { $a->{val} <=> $b->{val} } @list; |
| 63 | + |
| 64 | +# Generate the C mapping table output file |
| 65 | +open(my $out, ">", $out_file) or die "Cannot open $out_file: $!"; |
| 66 | +print $out "/* Autogenerated from $input_name by $script_name */\n\n"; |
| 67 | + |
| 68 | +if ($output_name eq "smb1_mapping_table.c") { |
| 69 | + # Generate NT status -> DOS error mapping file |
| 70 | + |
| 71 | + my $count = scalar @list; |
| 72 | + my $full_names = ""; |
| 73 | + |
| 74 | + for (my $i = 0; $i < $count; $i++) { |
| 75 | + my $e = $list[$i]; |
| 76 | + my $val = $e->{val}; |
| 77 | + |
| 78 | + $full_names .= $e->{name}; |
| 79 | + |
| 80 | + # Merge synonyms |
| 81 | + if ($i < $count - 1 && $list[$i + 1]->{val} == $val) { |
| 82 | + $full_names .= " or "; |
| 83 | + next; |
| 84 | + } |
| 85 | + |
| 86 | + printf $out "\t{ %s, %s, 0x%08x, \"%s\" },\n", $e->{class}, $e->{code}, $val, $full_names; |
| 87 | + |
| 88 | + $full_names = ""; |
| 89 | + } |
| 90 | +} |
| 91 | +close($out); |
0 commit comments