explorerframe: Avoid signed-unsigned integer comparisons.
[wine] / dlls / wldap32 / dn.c
1 /*
2  * WLDAP32 - LDAP support for Wine
3  *
4  * Copyright 2005 Hans Leidekker
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #ifdef HAVE_LDAP_H
26 #include <ldap.h>
27 #endif
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
32
33 #include "winldap_private.h"
34 #include "wldap32.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
38
39 /***********************************************************************
40  *      ldap_dn2ufnA     (WLDAP32.@)
41  *
42  * See ldap_dn2ufnW.
43  */
44 PCHAR CDECL ldap_dn2ufnA( PCHAR dn )
45 {
46     PCHAR ret = NULL;
47 #ifdef HAVE_LDAP
48     WCHAR *dnW, *retW;
49
50     TRACE( "(%s)\n", debugstr_a(dn) );
51
52     dnW = strAtoW( dn );
53     if (!dnW) return NULL;
54
55     retW = ldap_dn2ufnW( dnW );
56     ret = strWtoA( retW );
57
58     strfreeW( dnW );
59     ldap_memfreeW( retW );
60
61 #endif
62     return ret;
63 }
64
65 /***********************************************************************
66  *      ldap_dn2ufnW     (WLDAP32.@)
67  *
68  * Convert a DN to a user-friendly name.
69  *
70  * PARAMS
71  *  dn  [I] DN to convert.
72  *
73  * RETURNS
74  *  Success: Pointer to a string containing the user-friendly name. 
75  *  Failure: NULL
76  *
77  * NOTES
78  *  Free the string with ldap_memfree.
79  */
80 PWCHAR CDECL ldap_dn2ufnW( PWCHAR dn )
81 {
82     PWCHAR ret = NULL;
83 #ifdef HAVE_LDAP
84     char *dnU, *retU;
85
86     TRACE( "(%s)\n", debugstr_w(dn) );
87
88     dnU = strWtoU( dn );
89     if (!dnU) return NULL;
90
91     retU = ldap_dn2ufn( dnU );
92     ret = strUtoW( retU );
93
94     strfreeU( dnU );
95     ldap_memfree( retU );
96
97 #endif
98     return ret;
99 }
100
101 /***********************************************************************
102  *      ldap_explode_dnA     (WLDAP32.@)
103  *
104  * See ldap_explode_dnW.
105  */
106 PCHAR * CDECL ldap_explode_dnA( PCHAR dn, ULONG notypes )
107 {
108     PCHAR *ret = NULL;
109 #ifdef HAVE_LDAP
110     WCHAR *dnW, **retW;
111
112     TRACE( "(%s, 0x%08x)\n", debugstr_a(dn), notypes );
113
114     dnW = strAtoW( dn );
115     if (!dnW) return NULL;
116
117     retW = ldap_explode_dnW( dnW, notypes );
118     ret = strarrayWtoA( retW );
119
120     strfreeW( dnW );
121     ldap_value_freeW( retW );
122
123 #endif
124     return ret;
125 }
126
127 /***********************************************************************
128  *      ldap_explode_dnW     (WLDAP32.@)
129  *
130  * Break up a DN into its components.
131  *
132  * PARAMS
133  *  dn       [I] DN to break up.
134  *  notypes  [I] Remove attribute type information from the components.
135  *
136  * RETURNS
137  *  Success: Pointer to a NULL-terminated array that contains the DN
138  *           components. 
139  *  Failure: NULL
140  *
141  * NOTES
142  *  Free the string array with ldap_value_free.
143  */
144 PWCHAR * CDECL ldap_explode_dnW( PWCHAR dn, ULONG notypes )
145 {
146     PWCHAR *ret = NULL;
147 #ifdef HAVE_LDAP
148     char *dnU, **retU;
149
150     TRACE( "(%s, 0x%08x)\n", debugstr_w(dn), notypes );
151
152     dnU = strWtoU( dn );
153     if (!dnU) return NULL;
154
155     retU = ldap_explode_dn( dnU, notypes );
156     ret = strarrayUtoW( retU );
157
158     strfreeU( dnU );
159     ldap_memvfree( (void **)retU );
160
161 #endif
162     return ret;
163 }
164
165 /***********************************************************************
166  *      ldap_get_dnA     (WLDAP32.@)
167  *
168  * See ldap_get_dnW.
169  */
170 PCHAR CDECL ldap_get_dnA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
171 {
172     PCHAR ret = NULL;
173 #ifdef HAVE_LDAP
174     PWCHAR retW;
175
176     TRACE( "(%p, %p)\n", ld, entry );
177
178     if (!ld || !entry) return NULL;
179
180     retW = ldap_get_dnW( ld, entry );
181
182     ret = strWtoA( retW );
183     ldap_memfreeW( retW );
184
185 #endif
186     return ret;
187 }
188
189 /***********************************************************************
190  *      ldap_get_dnW     (WLDAP32.@)
191  *
192  * Retrieve the DN from a given LDAP message.
193  *
194  * PARAMS
195  *  ld     [I] Pointer to an LDAP context.
196  *  entry  [I] LDAPMessage structure to retrieve the DN from.
197  *
198  * RETURNS
199  *  Success: Pointer to a string that contains the DN.
200  *  Failure: NULL
201  *
202  * NOTES
203  *  Free the string with ldap_memfree.
204  */
205 PWCHAR CDECL ldap_get_dnW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
206 {
207     PWCHAR ret = NULL;
208 #ifdef HAVE_LDAP
209     char *retU;
210
211     TRACE( "(%p, %p)\n", ld, entry );
212
213     if (!ld || !entry) return NULL;
214
215     retU = ldap_get_dn( ld, entry );
216
217     ret = strUtoW( retU );
218     ldap_memfree( retU );
219
220 #endif
221     return ret;
222 }
223
224 /***********************************************************************
225  *      ldap_ufn2dnA     (WLDAP32.@)
226  *
227  * See ldap_ufn2dnW.
228  */
229 ULONG CDECL ldap_ufn2dnA( PCHAR ufn, PCHAR *dn )
230 {
231     ULONG ret = WLDAP32_LDAP_SUCCESS;
232 #ifdef HAVE_LDAP
233     PWCHAR ufnW = NULL, dnW = NULL;
234
235     TRACE( "(%s, %p)\n", debugstr_a(ufn), dn );
236
237     if (!dn) return WLDAP32_LDAP_PARAM_ERROR;
238
239     *dn = NULL;
240
241     if (ufn) {
242         ufnW = strAtoW( ufn );
243         if (!ufnW) return WLDAP32_LDAP_NO_MEMORY;
244     }
245
246     ret = ldap_ufn2dnW( ufnW, &dnW );
247
248     if (dnW) {
249         *dn = strWtoA( dnW );
250         if (!*dn) ret = WLDAP32_LDAP_NO_MEMORY;
251     }
252
253     strfreeW( ufnW );
254     ldap_memfreeW( dnW );
255
256 #endif
257     return ret;
258 }
259
260 /***********************************************************************
261  *      ldap_ufn2dnW     (WLDAP32.@)
262  *
263  * Convert a user-friendly name to a DN.
264  *
265  * PARAMS
266  *  ufn  [I] User-friendly name to convert.
267  *  dn   [O] Receives a pointer to a string containing the DN. 
268  *
269  * RETURNS
270  *  Success: LDAP_SUCCESS
271  *  Failure: An LDAP error code.
272  *
273  * NOTES
274  *  Free the string with ldap_memfree.
275  */
276 ULONG CDECL ldap_ufn2dnW( PWCHAR ufn, PWCHAR *dn )
277 {
278     ULONG ret = WLDAP32_LDAP_SUCCESS;
279 #ifdef HAVE_LDAP
280     char *ufnU = NULL;
281
282     TRACE( "(%s, %p)\n", debugstr_w(ufn), dn );
283
284     if (!dn) return WLDAP32_LDAP_PARAM_ERROR;
285
286     *dn = NULL;
287
288     if (ufn) {
289         ufnU = strWtoU( ufn );
290         if (!ufnU) return WLDAP32_LDAP_NO_MEMORY;
291
292         /* FIXME: do more than just a copy */
293         *dn = strUtoW( ufnU );
294         if (!*dn) ret = WLDAP32_LDAP_NO_MEMORY;
295     }
296
297     strfreeU( ufnU );
298
299 #endif
300     return ret;
301 }