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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -271,7 +271,7 @@
$(call cmd,build_OID_registry)
quiet_cmd_build_OID_registry = GEN $@
- cmd_build_OID_registry = perl $(srctree)/$(src)/build_OID_registry $< $@
+ cmd_build_OID_registry = $(CONFIG_SHELL) $(srctree)/$(src)/build_OID_registry <$< >$@
clean-files += oid_registry_data.c
--- a/lib/build_OID_registry
+++ b/lib/build_OID_registry
@@ -1,4 +1,4 @@
-#!/usr/bin/perl -w
+#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Build a static ASN.1 Object Identified (OID) registry
@@ -7,197 +7,93 @@
# Written by David Howells (dhowells@redhat.com)
#
-use strict;
-
-my @names = ();
-my @oids = ();
-
-if ($#ARGV != 1) {
- print STDERR "Format: ", $0, " <in-h-file> <out-c-file>\n";
- exit(2);
-}
-
#
-# Open the file to read from
+# Read OID lines and determine the lengths of the encoded data arrays.
#
-open IN_FILE, "<$ARGV[0]" || die;
-while (<IN_FILE>) {
- chomp;
- if (m!\s+OID_([a-zA-z][a-zA-Z0-9_]+),\s+/[*]\s+([012][.0-9]*)\s+[*]/!) {
- push @names, $1;
- push @oids, $2;
- }
-}
-close IN_FILE || die;
+set -f -- # use "$@" array for data
+total_length=0 IFS='.'
+while IFS=' ' read -r line; do
+ if [ "${line#OID_[a-z]}" != "$line" ]; then
+ name="${line#*_}" name="${name%,*}" oid="${line#*/* }" oid="${oid% */}"
+ # shellcheck disable=SC2086
+ size="$(set -- $oid; printf '%u' "$(($# - 1))")"
+ for c in $oid; do
+ # We will base128 encode the number
+ : "$((size += c ? $(awk "BEGIN{print int(log($c)/log(2)/7)}") : 0))"
+ done
+ set -- "$@" "$name:$oid:$total_length"
+ : "$((total_length += size))"
+ fi
+done
#
-# Open the files to write into
-#
-open C_FILE, ">$ARGV[1]" or die;
-print C_FILE "/*\n";
-print C_FILE " * Automatically generated by ", $0, ". Do not edit\n";
-print C_FILE " */\n";
-
-#
-# Split the data up into separate lists and also determine the lengths of the
-# encoded data arrays.
-#
-my @indices = ();
-my @lengths = ();
-my $total_length = 0;
-
-for (my $i = 0; $i <= $#names; $i++) {
- my $name = $names[$i];
- my $oid = $oids[$i];
-
- my @components = split(/[.]/, $oid);
-
- # Determine the encoded length of this OID
- my $size = $#components;
- for (my $loop = 2; $loop <= $#components; $loop++) {
- my $c = $components[$loop];
-
- # We will base128 encode the number
- my $tmp = ($c == 0) ? 0 : int(log($c)/log(2));
- $tmp = int($tmp / 7);
- $size += $tmp;
- }
- push @lengths, $size;
- push @indices, $total_length;
- $total_length += $size;
-}
-
-#
# Emit the look-up-by-OID index table
#
-print C_FILE "\n";
-if ($total_length <= 255) {
- print C_FILE "static const unsigned char oid_index[OID__NR + 1] = {\n";
-} else {
- print C_FILE "static const unsigned short oid_index[OID__NR + 1] = {\n";
-}
-for (my $i = 0; $i <= $#names; $i++) {
- print C_FILE "\t[OID_", $names[$i], "] = ", $indices[$i], ",\n"
-}
-print C_FILE "\t[OID__NR] = ", $total_length, "\n";
-print C_FILE "};\n";
+printf '/*\n * Automatically generated by %s. Do not edit\n */\n\n' "$0"
+if [ "$total_length" -le 255 ]; then
+ printf 'static const unsigned char oid_index[OID__NR + 1] = {\n'
+else
+ printf 'static const unsigned short oid_index[OID__NR + 1] = {\n'
+fi
+for i do
+ printf '\t[OID_%s] = %u,\n' "${i%%:*}" "${i##*:}"
+done
+printf '\t[OID__NR] = %u\n};\n\n' "$total_length"
#
-# Encode the OIDs
+# Encode the OIDs and emit the OID data
#
-my @encoded_oids = ();
+printf 'static const unsigned char oid_data[%u] = {\n' "$total_length"
+for i do
+ IFS='.' j=0 i="${i%:*}"
+ for c in ${i##*:}; do
+ case "$((j += 1))" in
+ 1) o="$((c * 40))";;
+ 2) : "$((o += c))";;
+ *)
+ # Base128 encode the number
+ tmp="$((c ? $(awk "BEGIN{print int(log($c)/log(2)/7)+1}") : 1))"
+ while [ "$((tmp -= 1))" -gt 0 ]; do
+ o="$o.$((c >> tmp * 7 & 127 | 128))"
+ done
+ o="$o.$((c & 127))"
+ esac
+ done
-for (my $i = 0; $i <= $#names; $i++) {
- my @octets = ();
+ set -- "$@" "${i%:*}:$o"
+ shift
- my @components = split(/[.]/, $oids[$i]);
+ printf '\t'
+ printf '%s, ' $o
+ printf '\t// %s\n' "${i%:*}"
+done
+printf '};\n\n'
- push @octets, $components[0] * 40 + $components[1];
+printf 'static const struct {\n\tunsigned char hash;\n'
+printf '\tenum OID oid : %u;\n} oid_search_table[OID__NR] = {' \
+ "$(($# <= 255 ? 8 : 16))"
- for (my $loop = 2; $loop <= $#components; $loop++) {
- my $c = $components[$loop];
-
- # Base128 encode the number
- my $tmp = ($c == 0) ? 0 : int(log($c)/log(2));
- $tmp = int($tmp / 7);
-
- for (; $tmp > 0; $tmp--) {
- push @octets, (($c >> $tmp * 7) & 0x7f) | 0x80;
- }
- push @octets, $c & 0x7f;
- }
-
- push @encoded_oids, \@octets;
-}
-
#
-# Create a hash value for each OID
+# Create a hash value for each OID and build and emit the search index and hash
+# value table (ordered by length then hash then content)
#
-my @hash_values = ();
-for (my $i = 0; $i <= $#names; $i++) {
- my @octets = @{$encoded_oids[$i]};
-
- my $hash = $#octets;
- foreach (@octets) {
- $hash += $_ * 33;
- }
-
- $hash = ($hash >> 24) ^ ($hash >> 16) ^ ($hash >> 8) ^ ($hash);
-
- push @hash_values, $hash & 0xff;
-}
-
-#
-# Emit the OID data
-#
-print C_FILE "\n";
-print C_FILE "static const unsigned char oid_data[", $total_length, "] = {\n";
-for (my $i = 0; $i <= $#names; $i++) {
- my @octets = @{$encoded_oids[$i]};
- print C_FILE "\t";
- print C_FILE $_, ", " foreach (@octets);
- print C_FILE "\t// ", $names[$i];
- print C_FILE "\n";
-}
-print C_FILE "};\n";
-
-#
-# Build the search index table (ordered by length then hash then content)
-#
-my @index_table = ( 0 .. $#names );
-
-@index_table = sort {
- my @octets_a = @{$encoded_oids[$a]};
- my @octets_b = @{$encoded_oids[$b]};
-
- return $hash_values[$a] <=> $hash_values[$b]
- if ($hash_values[$a] != $hash_values[$b]);
- return $#octets_a <=> $#octets_b
- if ($#octets_a != $#octets_b);
- for (my $i = $#octets_a; $i >= 0; $i--) {
- return $octets_a[$i] <=> $octets_b[$i]
- if ($octets_a[$i] != $octets_b[$i]);
- }
- return 0;
-
-} @index_table;
-
-#
-# Emit the search index and hash value table
-#
-print C_FILE "\n";
-print C_FILE "static const struct {\n";
-print C_FILE "\tunsigned char hash;\n";
-if ($#names <= 255) {
- print C_FILE "\tenum OID oid : 8;\n";
-} else {
- print C_FILE "\tenum OID oid : 16;\n";
-}
-print C_FILE "} oid_search_table[OID__NR] = {\n";
-for (my $i = 0; $i <= $#names; $i++) {
- my @octets = @{$encoded_oids[$index_table[$i]]};
- printf(C_FILE "\t[%3u] = { %3u, OID_%-35s }, // ",
- $i,
- $hash_values[$index_table[$i]],
- $names[$index_table[$i]]);
- printf C_FILE "%02x", $_ foreach (@octets);
- print C_FILE "\n";
-}
-print C_FILE "};\n";
-
-#
-# Emit the OID debugging name table
-#
-#print C_FILE "\n";
-#print C_FILE "const char *const oid_name_table[OID__NR + 1] = {\n";
-#
-#for (my $i = 0; $i <= $#names; $i++) {
-# print C_FILE "\t\"", $names[$i], "\",\n"
-#}
-#print C_FILE "\t\"Unknown-OID\"\n";
-#print C_FILE "};\n";
-
-#
-# Polish off
-#
-close C_FILE or die;
+i=-1
+for j do
+ IFS='.'
+ # shellcheck disable=SC2086
+ count="$(set -- ${j##*:}; printf '%u' "$(($# - 1))")"
+ hash="$count"
+ set -- # use as array for reversing octet set
+ for k in ${j##*:}; do
+ : "$((hash += k * 33))"
+ set -- "$k" "$@"
+ done
+ hash="$((hash >> 24 ^ hash >> 16 ^ hash >> 8 ^ hash & 255))"
+ IFS=':'
+ printf '%u:%s:%s:%u:%s\n' "$hash" "${j##*:}" "${j%%:*}" "$count" "$*"
+done | sort -nt: -k1,1 -k4,4 -k5 | while IFS=':' read -r num octets name _; do
+ printf '\n\t[%3u] = { %3u, OID_%-35s }, // ' "$((i += 1))" "$num" "$name"
+ # shellcheck disable=SC2086
+ printf '%02x' $octets
+done
+printf '\n};\n'
|