x86-64: Require BMI2 for AVX2 strcmp implementation
[glibc.git] / nss / nss_db / db-netgrp.c
blobef242ef8cd9b5177bf834ca564f734d11b11f251
1 /* Netgroup file parser in nss_db modules.
2 Copyright (C) 1996-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <ctype.h>
20 #include <dlfcn.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <netgroup.h>
24 #include <string.h>
25 #include <stdint.h>
26 #include <libc-lock.h>
27 #include <paths.h>
28 #include <stdlib.h>
30 #include "nsswitch.h"
31 #include "nss_db.h"
33 /* The hashing function we use. */
34 #include "../intl/hash-string.h"
37 #define DBFILE _PATH_VARDB "netgroup.db"
39 /* Maintenance of the shared handle open on the database. */
40 enum nss_status
41 _nss_db_setnetgrent (const char *group, struct __netgrent *result)
43 struct nss_db_map state;
44 enum nss_status status = internal_setent (DBFILE, &state);
46 if (status == NSS_STATUS_SUCCESS)
48 const struct nss_db_header *header = state.header;
49 const stridx_t *hashtable
50 = (const stridx_t *) ((const char *) header
51 + header->dbs[0].hashoffset);
52 const char *valstrtab = (const char *) header + header->valstroffset;
53 uint32_t hashval = __hash_string (group);
54 size_t grouplen = strlen (group);
55 size_t hidx = hashval % header->dbs[0].hashsize;
56 size_t hval2 = 1 + hashval % (header->dbs[0].hashsize - 2);
58 status = NSS_STATUS_NOTFOUND;
59 while (hashtable[hidx] != ~((stridx_t) 0))
61 const char *valstr = valstrtab + hashtable[hidx];
63 if (strncmp (valstr, group, grouplen) == 0
64 && isblank (valstr[grouplen]))
66 const char *cp = &valstr[grouplen + 1];
67 while (isblank (*cp))
68 ++cp;
69 if (*cp != '\0')
71 result->data = strdup (cp);
72 if (result->data == NULL)
73 status = NSS_STATUS_TRYAGAIN;
74 else
76 status = NSS_STATUS_SUCCESS;
77 result->cursor = result->data;
79 break;
83 if ((hidx += hval2) >= header->dbs[0].hashsize)
84 hidx -= header->dbs[0].hashsize;
87 internal_endent (&state);
90 return status;
95 enum nss_status
96 _nss_db_endnetgrent (struct __netgrent *result)
98 free (result->data);
99 result->data = NULL;
100 result->data_size = 0;
101 result->cursor = NULL;
102 return NSS_STATUS_SUCCESS;
106 extern enum nss_status _nss_netgroup_parseline (char **cursor,
107 struct __netgrent *result,
108 char *buffer, size_t buflen,
109 int *errnop);
111 enum nss_status
112 _nss_db_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
113 int *errnop)
115 enum nss_status status;
117 status = _nss_netgroup_parseline (&result->cursor, result, buffer, buflen,
118 errnop);
120 return status;