summaryrefslogtreecommitdiffstats
path: root/fs/smb/client/gen_smb2_mapping
blob: eb9fa727ddd8739bf692281f7297b53e1ddae68b (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl -w
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Generate an SMB2 status -> error mapping table,
# sorted by NT status code (cpu-endian, ascending).
#
# Copyright (C) 2025 Red Hat, Inc. All Rights Reserved.
# Written by David Howells (dhowells@redhat.com)
#
use strict;

if ($#ARGV != 1) {
	print STDERR "Format: ", $0, " <in-h-file> <out-c-file>\n";
	exit(2);
}

my %statuses = ();
my @list = ();

#
# Read the file
#
open IN_FILE, "<$ARGV[0]" || die;
while (<IN_FILE>) {
	chomp;

	if (m!^#define\s*([A-Za-z0-9_]+)\s+cpu_to_le32[(]([0-9a-fA-Fx]+)[)]\s+//\s+([-A-Z0-9_]+)!) {
		my $status = $1;
		my $code = $2;
		my $ncode = hex($2);
		my $error = $3;
		my $s;

		next if ($status =~ /^STATUS_SEVERITY/);

		die "Duplicate status $status"
			if exists($statuses{$status});

		my %s = (
			status => $status,
			code   => $code,
			ncode  => $ncode,
			error  => $error
		);
		$statuses{$status} = \%s;
		push @list, \%s;
	}
}
close IN_FILE || die;


@list = sort( { $a->{ncode} <=> $b->{ncode} } @list);

open OUT_FILE, ">$ARGV[1]" || die;
my $list_size = scalar @list;
my $full_status = "";
for (my $i = 0; $i < $list_size; $i++) {
	my $entry = $list[$i];
	my $status = $entry->{status};
	my $code   = $entry->{code};
	my $ncode  = $entry->{ncode};
	my $error  = $entry->{error};

	next if ($ncode == 0);

	$full_status .= $status;
	# There may be synonyms
	if ($i < $list_size - 1) {
		my $next_entry = $list[$i + 1];
		my $next_ncode = $next_entry->{ncode};
		if ($next_ncode == $ncode) {
			$full_status .= " or ";
			next;
		}
	}

	my $pad = " ";
	if (length($full_status) < 40) {
		my $n = 40 - length($full_status);
		$pad = "\t" x ((($n-1) / 8) + 1);
	}
	print(OUT_FILE "{ $code, $error, \"$full_status\" },\n");

	$full_status = "";
}
close OUT_FILE || die;