1 /* ASN.1 Object identifier (OID) registry
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/export.h>
14 #include <linux/oid_registry.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/bug.h>
18 #include "oid_registry_data.c"
20 MODULE_DESCRIPTION("OID Registry");
21 MODULE_AUTHOR("Red Hat, Inc.");
22 MODULE_LICENSE("GPL");
25 * look_up_OID - Find an OID registration for the specified data
26 * @data: Binary representation of the OID
27 * @datasize: Size of the binary representation
29 enum OID
look_up_OID(const void *data
, size_t datasize
)
31 const unsigned char *octets
= data
;
34 unsigned i
, j
, k
, hash
;
37 /* Hash the OID data */
40 for (i
= 0; i
< datasize
; i
++)
41 hash
+= octets
[i
] * 33;
42 hash
= (hash
>> 24) ^ (hash
>> 16) ^ (hash
>> 8) ^ hash
;
45 /* Binary search the OID registry. OIDs are stored in ascending order
46 * of hash value then ascending order of size and then in ascending
47 * order of reverse value.
54 xhash
= oid_search_table
[j
].hash
;
64 oid
= oid_search_table
[j
].oid
;
65 len
= oid_index
[oid
+ 1] - oid_index
[oid
];
75 /* Variation is most likely to be at the tail end of the
76 * OID, so do the comparison in reverse.
79 unsigned char a
= oid_data
[oid_index
[oid
] + --len
];
80 unsigned char b
= octets
[len
];
97 EXPORT_SYMBOL_GPL(look_up_OID
);
100 * sprint_OID - Print an Object Identifier into a buffer
101 * @data: The encoded OID to print
102 * @datasize: The size of the encoded OID
103 * @buffer: The buffer to render into
104 * @bufsize: The size of the buffer
106 * The OID is rendered into the buffer in "a.b.c.d" format and the number of
107 * bytes is returned. -EBADMSG is returned if the data could not be intepreted
108 * and -ENOBUFS if the buffer was too small.
110 int sprint_oid(const void *data
, size_t datasize
, char *buffer
, size_t bufsize
)
112 const unsigned char *v
= data
, *end
= v
+ datasize
;
122 ret
= count
= snprintf(buffer
, bufsize
, "%u.%u", n
/ 40, n
% 40);
143 ret
+= count
= snprintf(buffer
, bufsize
, ".%lu", num
);
153 snprintf(buffer
, bufsize
, "(bad)");
156 EXPORT_SYMBOL_GPL(sprint_oid
);
159 * sprint_OID - Print an Object Identifier into a buffer
160 * @oid: The OID to print
161 * @buffer: The buffer to render into
162 * @bufsize: The size of the buffer
164 * The OID is rendered into the buffer in "a.b.c.d" format and the number of
167 int sprint_OID(enum OID oid
, char *buffer
, size_t bufsize
)
171 BUG_ON(oid
>= OID__NR
);
173 ret
= sprint_oid(oid_data
+ oid_index
[oid
],
174 oid_index
[oid
+ 1] - oid_index
[oid
],
176 BUG_ON(ret
== -EBADMSG
);
179 EXPORT_SYMBOL_GPL(sprint_OID
);