1diff --git a/lib/Makefile b/lib/Makefile
2index 30337431d10e..ca4853a86f86 100644
3--- a/lib/Makefile
4+++ b/lib/Makefile
5@@ -336,7 +336,7 @@ $(obj)/oid_registry_data.c: $(srctree)/include/linux/oid_registry.h \
6 $(call cmd,build_OID_registry)
7
8 quiet_cmd_build_OID_registry = GEN $@
9- cmd_build_OID_registry = perl $(src)/build_OID_registry $< $@
10+ cmd_build_OID_registry = $(CONFIG_SHELL) $(src)/build_OID_registry <$< >$@
11
12 clean-files += oid_registry_data.c
13
14diff --git a/lib/build_OID_registry b/lib/build_OID_registry
15index 8267e8d71338..80aab4b069e9 100755
16--- a/lib/build_OID_registry
17+++ b/lib/build_OID_registry
18@@ -1,4 +1,4 @@
19-#!/usr/bin/perl -w
20+#!/bin/sh
21 # SPDX-License-Identifier: GPL-2.0-or-later
22 #
23 # Build a static ASN.1 Object Identified (OID) registry
24@@ -7,202 +7,93 @@
25 # Written by David Howells (dhowells@redhat.com)
26 #
27
28-use strict;
29-use Cwd qw(abs_path);
30-
31-my @names = ();
32-my @oids = ();
33-
34-if ($#ARGV != 1) {
35- print STDERR "Format: ", $0, " <in-h-file> <out-c-file>\n";
36- exit(2);
37-}
38-
39-my $abs_srctree = abs_path($ENV{'srctree'});
40-
41-#
42-# Open the file to read from
43-#
44-open IN_FILE, "<$ARGV[0]" || die;
45-while (<IN_FILE>) {
46- chomp;
47- if (m!\s+OID_([a-zA-z][a-zA-Z0-9_]+),\s+/[*]\s+([012][.0-9]*)\s+[*]/!) {
48- push @names, $1;
49- push @oids, $2;
50- }
51-}
52-close IN_FILE || die;
53-
54 #
55-# Open the files to write into
56+# Read OID lines and determine the lengths of the encoded data arrays.
57 #
58-open C_FILE, ">$ARGV[1]" or die;
59-print C_FILE "/*\n";
60-my $scriptname = $0;
61-$scriptname =~ s#^\Q$abs_srctree/\E##;
62-print C_FILE " * Automatically generated by ", $scriptname, ". Do not edit\n";
63-print C_FILE " */\n";
64-
65-#
66-# Split the data up into separate lists and also determine the lengths of the
67-# encoded data arrays.
68-#
69-my @indices = ();
70-my @lengths = ();
71-my $total_length = 0;
72-
73-for (my $i = 0; $i <= $#names; $i++) {
74- my $name = $names[$i];
75- my $oid = $oids[$i];
76-
77- my @components = split(/[.]/, $oid);
78-
79- # Determine the encoded length of this OID
80- my $size = $#components;
81- for (my $loop = 2; $loop <= $#components; $loop++) {
82- my $c = $components[$loop];
83-
84- # We will base128 encode the number
85- my $tmp = ($c == 0) ? 0 : int(log($c)/log(2));
86- $tmp = int($tmp / 7);
87- $size += $tmp;
88- }
89- push @lengths, $size;
90- push @indices, $total_length;
91- $total_length += $size;
92-}
93+set -f -- # use "$@" array for data
94+total_length=0 IFS='.'
95+while IFS=' ' read -r line; do
96+ if [ "${line#OID_[a-z]}" != "$line" ]; then
97+ name="${line#*_}" name="${name%,*}" oid="${line#*/* }" oid="${oid% */}"
98+ # shellcheck disable=SC2086
99+ size="$(set -- $oid; printf '%u' "$(($# - 1))")"
100+ for c in $oid; do
101+ # We will base128 encode the number
102+ : "$((size += c ? $(awk "BEGIN{print int(log($c)/log(2)/7)}") : 0))"
103+ done
104+ set -- "$@" "$name:$oid:$total_length"
105+ : "$((total_length += size))"
106+ fi
107+done
108
109 #
110 # Emit the look-up-by-OID index table
111 #
112-print C_FILE "\n";
113-if ($total_length <= 255) {
114- print C_FILE "static const unsigned char oid_index[OID__NR + 1] = {\n";
115-} else {
116- print C_FILE "static const unsigned short oid_index[OID__NR + 1] = {\n";
117-}
118-for (my $i = 0; $i <= $#names; $i++) {
119- print C_FILE "\t[OID_", $names[$i], "] = ", $indices[$i], ",\n"
120-}
121-print C_FILE "\t[OID__NR] = ", $total_length, "\n";
122-print C_FILE "};\n";
123-
124-#
125-# Encode the OIDs
126-#
127-my @encoded_oids = ();
128-
129-for (my $i = 0; $i <= $#names; $i++) {
130- my @octets = ();
131-
132- my @components = split(/[.]/, $oids[$i]);
133-
134- push @octets, $components[0] * 40 + $components[1];
135-
136- for (my $loop = 2; $loop <= $#components; $loop++) {
137- my $c = $components[$loop];
138-
139- # Base128 encode the number
140- my $tmp = ($c == 0) ? 0 : int(log($c)/log(2));
141- $tmp = int($tmp / 7);
142-
143- for (; $tmp > 0; $tmp--) {
144- push @octets, (($c >> $tmp * 7) & 0x7f) | 0x80;
145- }
146- push @octets, $c & 0x7f;
147- }
148-
149- push @encoded_oids, \@octets;
150-}
151-
152-#
153-# Create a hash value for each OID
154-#
155-my @hash_values = ();
156-for (my $i = 0; $i <= $#names; $i++) {
157- my @octets = @{$encoded_oids[$i]};
158-
159- my $hash = $#octets;
160- foreach (@octets) {
161- $hash += $_ * 33;
162- }
163-
164- $hash = ($hash >> 24) ^ ($hash >> 16) ^ ($hash >> 8) ^ ($hash);
165-
166- push @hash_values, $hash & 0xff;
167-}
168-
169-#
170-# Emit the OID data
171-#
172-print C_FILE "\n";
173-print C_FILE "static const unsigned char oid_data[", $total_length, "] = {\n";
174-for (my $i = 0; $i <= $#names; $i++) {
175- my @octets = @{$encoded_oids[$i]};
176- print C_FILE "\t";
177- print C_FILE $_, ", " foreach (@octets);
178- print C_FILE "\t// ", $names[$i];
179- print C_FILE "\n";
180-}
181-print C_FILE "};\n";
182-
183-#
184-# Build the search index table (ordered by length then hash then content)
185-#
186-my @index_table = ( 0 .. $#names );
187-
188-@index_table = sort {
189- my @octets_a = @{$encoded_oids[$a]};
190- my @octets_b = @{$encoded_oids[$b]};
191-
192- return $hash_values[$a] <=> $hash_values[$b]
193- if ($hash_values[$a] != $hash_values[$b]);
194- return $#octets_a <=> $#octets_b
195- if ($#octets_a != $#octets_b);
196- for (my $i = $#octets_a; $i >= 0; $i--) {
197- return $octets_a[$i] <=> $octets_b[$i]
198- if ($octets_a[$i] != $octets_b[$i]);
199- }
200- return 0;
201-
202-} @index_table;
203-
204-#
205-# Emit the search index and hash value table
206-#
207-print C_FILE "\n";
208-print C_FILE "static const struct {\n";
209-print C_FILE "\tunsigned char hash;\n";
210-if ($#names <= 255) {
211- print C_FILE "\tenum OID oid : 8;\n";
212-} else {
213- print C_FILE "\tenum OID oid : 16;\n";
214-}
215-print C_FILE "} oid_search_table[OID__NR] = {\n";
216-for (my $i = 0; $i <= $#names; $i++) {
217- my @octets = @{$encoded_oids[$index_table[$i]]};
218- printf(C_FILE "\t[%3u] = { %3u, OID_%-35s }, // ",
219- $i,
220- $hash_values[$index_table[$i]],
221- $names[$index_table[$i]]);
222- printf C_FILE "%02x", $_ foreach (@octets);
223- print C_FILE "\n";
224-}
225-print C_FILE "};\n";
226-
227-#
228-# Emit the OID debugging name table
229-#
230-#print C_FILE "\n";
231-#print C_FILE "const char *const oid_name_table[OID__NR + 1] = {\n";
232-#
233-#for (my $i = 0; $i <= $#names; $i++) {
234-# print C_FILE "\t\"", $names[$i], "\",\n"
235-#}
236-#print C_FILE "\t\"Unknown-OID\"\n";
237-#print C_FILE "};\n";
238-
239-#
240-# Polish off
241-#
242-close C_FILE or die;
243+printf '/*\n * Automatically generated by %s. Do not edit\n */\n\n' "$0"
244+if [ "$total_length" -le 255 ]; then
245+ printf 'static const unsigned char oid_index[OID__NR + 1] = {\n'
246+else
247+ printf 'static const unsigned short oid_index[OID__NR + 1] = {\n'
248+fi
249+for i do
250+ printf '\t[OID_%s] = %u,\n' "${i%%:*}" "${i##*:}"
251+done
252+printf '\t[OID__NR] = %u\n};\n\n' "$total_length"
253+
254+#
255+# Encode the OIDs and emit the OID data
256+#
257+printf 'static const unsigned char oid_data[%u] = {\n' "$total_length"
258+for i do
259+ IFS='.' j=0 i="${i%:*}"
260+ for c in ${i##*:}; do
261+ case "$((j += 1))" in
262+ 1) o="$((c * 40))";;
263+ 2) : "$((o += c))";;
264+ *)
265+ # Base128 encode the number
266+ tmp="$((c ? $(awk "BEGIN{print int(log($c)/log(2)/7)+1}") : 1))"
267+ while [ "$((tmp -= 1))" -gt 0 ]; do
268+ o="$o.$((c >> tmp * 7 & 127 | 128))"
269+ done
270+ o="$o.$((c & 127))"
271+ esac
272+ done
273+
274+ set -- "$@" "${i%:*}:$o"
275+ shift
276+
277+ printf '\t'
278+ printf '%s, ' $o
279+ printf '\t// %s\n' "${i%:*}"
280+done
281+printf '};\n\n'
282+
283+printf 'static const struct {\n\tunsigned char hash;\n'
284+printf '\tenum OID oid : %u;\n} oid_search_table[OID__NR] = {' \
285+ "$(($# <= 255 ? 8 : 16))"
286+
287+#
288+# Create a hash value for each OID and build and emit the search index and hash
289+# value table (ordered by length then hash then content)
290+#
291+i=-1
292+for j do
293+ IFS='.'
294+ # shellcheck disable=SC2086
295+ count="$(set -- ${j##*:}; printf '%u' "$(($# - 1))")"
296+ hash="$count"
297+ set -- # use as array for reversing octet set
298+ for k in ${j##*:}; do
299+ : "$((hash += k * 33))"
300+ set -- "$k" "$@"
301+ done
302+ hash="$((hash >> 24 ^ hash >> 16 ^ hash >> 8 ^ hash & 255))"
303+ IFS=':'
304+ printf '%u:%s:%s:%u:%s\n' "$hash" "${j##*:}" "${j%%:*}" "$count" "$*"
305+done | sort -nt: -k1,1 -k4,4 -k5 | while IFS=':' read -r num octets name _; do
306+ printf '\n\t[%3u] = { %3u, OID_%-35s }, // ' "$((i += 1))" "$num" "$name"
307+ # shellcheck disable=SC2086
308+ printf '%02x' $octets
309+done
310+printf '\n};\n'