xref: /illumos-gate/usr/src/lib/smbsrv/libsmb/common/smb_status_tbl.c (revision 45ede40b2394db7967e59f19288fae9b62efd4aa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*
27  * This file provides a text translation service for NT status codes.
28  */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 
33 /*
34  * Include the generated file with ntx_table[]
35  * See smb_status_gen.awk
36  */
37 #include "smb_status_tbl.h"
38 static const int ntx_rows = sizeof (ntx_table) / sizeof (ntx_table[0]);
39 
40 /*
41  * Comparison function for bsearch(3C).
42  */
43 static int
44 xlate_compare(const void *vkey, const void *vrow)
45 {
46 	const smb_status_table_t *key = vkey;
47 	const smb_status_table_t *row = vrow;
48 
49 	if (key->value == row->value)
50 		return (0);
51 	if (key->value < row->value)
52 		return (-1);
53 	return (1);
54 }
55 
56 /*
57  * Translate an ntstatus value to a meaningful text string. If there isn't
58  * a corresponding text string in the table, the text representation of the
59  * status value is returned. This uses a static buffer so there is a
60  * possible concurrency issue if the caller hangs on to this pointer for a
61  * while but it should be harmless and really remote since the value will
62  * almost always be found in the table.
63  */
64 const char *
65 xlate_nt_status(unsigned int ntstatus)
66 {
67 	static char unknown[16];
68 	smb_status_table_t key;
69 	const smb_status_table_t *tep;
70 
71 	key.value = ntstatus;
72 	key.name = NULL;
73 	tep = bsearch(&key, ntx_table, ntx_rows,
74 	    sizeof (*tep), xlate_compare);
75 
76 	if (tep != NULL)
77 		return (tep->name);
78 
79 	(void) sprintf(unknown, "0x%08X", ntstatus);
80 	return ((const char *)unknown);
81 }
82