1 /* This file is part of GNU Dico
2    Copyright (C) 2008-2020 Sergey Poznyakoff
3 
4    GNU Dico is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Dico is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Dico.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #include <config.h>
18 #include <xdico.h>
19 #include <xalloc.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <c-strcase.h>
23 
24 void
trimnl(char * buf,size_t len)25 trimnl(char *buf, size_t len)
26 {
27     if (len > 1 && buf[--len] == '\n') {
28 	buf[len] = 0;
29 	if (len > 1 && buf[--len] == '\r')
30 	    buf[len] = 0;
31     }
32 }
33 
34 
35 /* X-versions of dico library calls */
36 
37 char *
make_full_file_name(const char * dir,const char * file)38 make_full_file_name(const char *dir, const char *file)
39 {
40     char *s = dico_full_file_name(dir, file);
41     if (!s)
42 	xalloc_die();
43     return s;
44 }
45 
46 dico_list_t
xdico_list_create(void)47 xdico_list_create(void)
48 {
49     dico_list_t p = dico_list_create();
50     if (!p)
51 	xalloc_die();
52     return p;
53 }
54 
55 dico_iterator_t
xdico_list_iterator(dico_list_t list)56 xdico_list_iterator(dico_list_t list)
57 {
58     dico_iterator_t p = dico_list_iterator(list);
59     if (!p && errno == ENOMEM)
60 	xalloc_die();
61     return p;
62 }
63 
64 void
xdico_list_append(struct dico_list * list,void * data)65 xdico_list_append(struct dico_list *list, void *data)
66 {
67     if (dico_list_append(list, data) && errno == ENOMEM)
68 	xalloc_die();
69 }
70 
71 void
xdico_list_prepend(struct dico_list * list,void * data)72 xdico_list_prepend(struct dico_list *list, void *data)
73 {
74     if (dico_list_prepend(list, data) && errno == ENOMEM)
75 	xalloc_die();
76 }
77 
78 dico_assoc_list_t
xdico_assoc_create(int flags)79 xdico_assoc_create(int flags)
80 {
81     dico_assoc_list_t p = dico_assoc_create(flags);
82     if (!p)
83 	xalloc_die();
84     return p;
85 }
86 
87 void
xdico_assoc_append(dico_assoc_list_t assoc,const char * key,const char * value)88 xdico_assoc_append(dico_assoc_list_t assoc, const char *key, const char *value)
89 {
90     if (dico_assoc_append(assoc, key, value) && errno == ENOMEM)
91 	xalloc_die();
92 }
93 
94 int
xdico_assoc_add(dico_assoc_list_t assoc,const char * key,const char * value,size_t count,int replace)95 xdico_assoc_add(dico_assoc_list_t assoc, const char *key,
96 		const char *value, size_t count, int replace)
97 {
98     int rc = dico_assoc_add(assoc, key, value, count, replace);
99     if (rc && errno == ENOMEM)
100 	xalloc_die();
101     return rc;
102 }
103 
104 char *
xdico_assign_string(char ** dest,char * str)105 xdico_assign_string(char **dest, char *str)
106 {
107     if (*dest)
108 	free (*dest);
109     return *dest = str ? xstrdup (str) : NULL;
110 }
111 
112 static char *mech_to_capa_table[][2] = {
113     { "EXTERNAL", "external" },
114     { "SKEY", "skey" },
115     { "GSSAPI", "gssapi" },
116     { "KERBEROS_V4", "kerberos_v4" }
117 };
118 
119 char *
xdico_sasl_mech_to_capa(char * mech)120 xdico_sasl_mech_to_capa(char *mech)
121 {
122     int i;
123     size_t len;
124     char *rets, *p;
125 
126     for (i = 0; i < DICO_ARRAY_SIZE(mech_to_capa_table); i++)
127 	if (strcmp(mech_to_capa_table[i][0], mech) == 0)
128 	    return xstrdup(mech_to_capa_table[i][1]);
129 
130     len = strlen(mech) + 1;
131     rets = p = xmalloc(len + 1);
132     *p++ = 'x';
133     for (; *mech; mech++)
134 	*p++ = tolower(*mech);
135     *p = 0;
136     return rets;
137 }
138 
139 int
xdico_sasl_capa_match_p(const char * mech,const char * capa)140 xdico_sasl_capa_match_p(const char *mech, const char *capa)
141 {
142     int i;
143 
144     for (i = 0; i < DICO_ARRAY_SIZE(mech_to_capa_table); i++)
145 	if (c_strcasecmp(mech_to_capa_table[i][0], mech) == 0)
146 	    return c_strcasecmp(mech_to_capa_table[i][1], capa) == 0;
147 
148     if (*capa == 'x')
149 	return c_strcasecmp(mech, capa + 1) == 0;
150     return 0;
151 }
152 
153 
154 int
dicod_free_item(void * item,void * data DICO_ARG_UNUSED)155 dicod_free_item (void *item, void *data DICO_ARG_UNUSED)
156 {
157     free(item);
158     return 0;
159 }
160 
161 
162