665255afe22bd1a16234c0c0b621766a0bdf1fca
[reactos.git] / reactos / dll / win32 / wldap32 / value.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
23 //#include "wine/port.h"
24 #include <wine/debug.h>
25
26 //#include <stdarg.h>
27
28 //#include "windef.h"
29 #include <winbase.h>
30 #include <winnls.h>
31
32 #ifdef HAVE_LDAP_H
33 #include <ldap.h>
34 #endif
35
36 #include "winldap_private.h"
37 #include "wldap32.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
40
41 /***********************************************************************
42 * ldap_count_values_len (WLDAP32.@)
43 *
44 * Count the number of values in an array of berval structures.
45 *
46 * PARAMS
47 * vals [I] Pointer to an array of berval structures.
48 *
49 * RETURNS
50 * Success: The number of values counted.
51 * Failure: 0
52 *
53 * NOTES
54 * Call ldap_count_values_len with the result of a call to
55 * ldap_get_values_len.
56 */
57 ULONG CDECL WLDAP32_ldap_count_values_len( struct WLDAP32_berval **vals )
58 {
59 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
60 #ifdef HAVE_LDAP
61
62 TRACE( "(%p)\n", vals );
63 ret = ldap_count_values_len( (struct berval **)vals );
64
65 #endif
66 return ret;
67 }
68
69 /***********************************************************************
70 * ldap_count_valuesA (WLDAP32.@)
71 *
72 * See ldap_count_valuesW.
73 */
74 ULONG CDECL ldap_count_valuesA( PCHAR *vals )
75 {
76 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
77 #ifdef HAVE_LDAP
78 WCHAR **valsW = NULL;
79
80 TRACE( "(%p)\n", vals );
81
82 if (!vals) return 0;
83
84 valsW = strarrayAtoW( vals );
85 if (!valsW) return WLDAP32_LDAP_NO_MEMORY;
86
87 ret = ldap_count_valuesW( valsW );
88 strarrayfreeW( valsW );
89
90 #endif
91 return ret;
92 }
93
94 /***********************************************************************
95 * ldap_count_valuesW (WLDAP32.@)
96 *
97 * Count the number of values in a string array.
98 *
99 * PARAMS
100 * vals [I] Pointer to an array of strings.
101 *
102 * RETURNS
103 * Success: The number of values counted.
104 * Failure: 0
105 *
106 * NOTES
107 * Call ldap_count_valuesW with the result of a call to
108 * ldap_get_valuesW.
109 */
110 ULONG CDECL ldap_count_valuesW( PWCHAR *vals )
111 {
112 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
113 #ifdef HAVE_LDAP
114 WCHAR **p = vals;
115
116 TRACE( "(%p)\n", vals );
117
118 if (!vals) return 0;
119
120 ret = 0;
121 while (*p++) ret++;
122
123 #endif
124 return ret;
125 }
126
127 /***********************************************************************
128 * ldap_get_valuesA (WLDAP32.@)
129 *
130 * See ldap_get_valuesW.
131 */
132 PCHAR * CDECL ldap_get_valuesA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry, PCHAR attr )
133 {
134 PCHAR *ret = NULL;
135 #ifdef HAVE_LDAP
136 WCHAR *attrW = NULL, **retW;
137
138 TRACE( "(%p, %p, %s)\n", ld, entry, debugstr_a(attr) );
139
140 if (!ld || !entry || !attr) return NULL;
141
142 attrW = strAtoW( attr );
143 if (!attrW) return NULL;
144
145 retW = ldap_get_valuesW( ld, entry, attrW );
146
147 ret = strarrayWtoA( retW );
148 ldap_value_freeW( retW );
149 strfreeW( attrW );
150
151 #endif
152 return ret;
153 }
154
155 #ifdef HAVE_LDAP
156 static char *bv2str( struct berval *bv )
157 {
158 char *str = NULL;
159 unsigned int len = bv->bv_len;
160
161 str = HeapAlloc( GetProcessHeap(), 0, len + 1 );
162 if (str)
163 {
164 memcpy( str, bv->bv_val, len );
165 str[len] = '\0';
166 }
167 return str;
168 }
169
170 static char **bv2str_array( struct berval **bv )
171 {
172 unsigned int len = 0, i = 0;
173 struct berval **p = bv;
174 char **str;
175
176 while (*p)
177 {
178 len++;
179 p++;
180 }
181 str = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(char *) );
182 if (!str) return NULL;
183
184 p = bv;
185 while (*p)
186 {
187 str[i] = bv2str( *p );
188 if (!str[i])
189 {
190 while (i > 0) HeapFree( GetProcessHeap(), 0, str[--i] );
191 HeapFree( GetProcessHeap(), 0, str );
192 return NULL;
193 }
194 i++;
195 p++;
196 }
197 str[i] = NULL;
198 return str;
199 }
200 #endif
201
202 /***********************************************************************
203 * ldap_get_valuesW (WLDAP32.@)
204 *
205 * Retrieve string values for a given attribute.
206 *
207 * PARAMS
208 * ld [I] Pointer to an LDAP context.
209 * entry [I] Entry to retrieve values from.
210 * attr [I] Attribute to retrieve values for.
211 *
212 * RETURNS
213 * Success: Pointer to a character array holding the values.
214 * Failure: NULL
215 *
216 * NOTES
217 * Call ldap_get_valuesW with the result of a call to
218 * ldap_first_entry or ldap_next_entry. Free the returned
219 * array with a call to ldap_value_freeW.
220 */
221 PWCHAR * CDECL ldap_get_valuesW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry, PWCHAR attr )
222 {
223 PWCHAR *ret = NULL;
224 #ifdef HAVE_LDAP
225 char *attrU = NULL, **retU;
226 struct berval **bv;
227
228 TRACE( "(%p, %p, %s)\n", ld, entry, debugstr_w(attr) );
229
230 if (!ld || !entry || !attr) return NULL;
231
232 attrU = strWtoU( attr );
233 if (!attrU) return NULL;
234
235 bv = ldap_get_values_len( ld, entry, attrU );
236
237 retU = bv2str_array( bv );
238 ret = strarrayUtoW( retU );
239
240 ldap_value_free_len( bv );
241 strarrayfreeU( retU );
242 strfreeU( attrU );
243
244 #endif
245 return ret;
246 }
247
248 /***********************************************************************
249 * ldap_get_values_lenA (WLDAP32.@)
250 *
251 * See ldap_get_values_lenW.
252 */
253 struct WLDAP32_berval ** CDECL ldap_get_values_lenA( WLDAP32_LDAP *ld,
254 WLDAP32_LDAPMessage *message, PCHAR attr )
255 {
256 #ifdef HAVE_LDAP
257 WCHAR *attrW = NULL;
258 struct WLDAP32_berval **ret;
259
260 TRACE( "(%p, %p, %s)\n", ld, message, debugstr_a(attr) );
261
262 if (!ld || !message || !attr) return NULL;
263
264 attrW = strAtoW( attr );
265 if (!attrW) return NULL;
266
267 ret = ldap_get_values_lenW( ld, message, attrW );
268
269 strfreeW( attrW );
270 return ret;
271
272 #else
273 return NULL;
274 #endif
275 }
276
277 /***********************************************************************
278 * ldap_get_values_lenW (WLDAP32.@)
279 *
280 * Retrieve binary values for a given attribute.
281 *
282 * PARAMS
283 * ld [I] Pointer to an LDAP context.
284 * message [I] Entry to retrieve values from.
285 * attr [I] Attribute to retrieve values for.
286 *
287 * RETURNS
288 * Success: Pointer to a berval array holding the values.
289 * Failure: NULL
290 *
291 * NOTES
292 * Call ldap_get_values_lenW with the result of a call to
293 * ldap_first_entry or ldap_next_entry. Free the returned
294 * array with a call to ldap_value_free_len.
295 */
296 struct WLDAP32_berval ** CDECL ldap_get_values_lenW( WLDAP32_LDAP *ld,
297 WLDAP32_LDAPMessage *message, PWCHAR attr )
298 {
299 #ifdef HAVE_LDAP
300 char *attrU = NULL;
301 struct berval **ret;
302
303 TRACE( "(%p, %p, %s)\n", ld, message, debugstr_w(attr) );
304
305 if (!ld || !message || !attr) return NULL;
306
307 attrU = strWtoU( attr );
308 if (!attrU) return NULL;
309
310 ret = ldap_get_values_len( ld, message, attrU );
311
312 strfreeU( attrU );
313 return (struct WLDAP32_berval **)ret;
314
315 #else
316 return NULL;
317 #endif
318 }
319
320 /***********************************************************************
321 * ldap_value_free_len (WLDAP32.@)
322 *
323 * Free an array of berval structures.
324 *
325 * PARAMS
326 * vals [I] Array of berval structures.
327 *
328 * RETURNS
329 * Success: LDAP_SUCCESS
330 * Failure: An LDAP error code.
331 */
332 ULONG CDECL WLDAP32_ldap_value_free_len( struct WLDAP32_berval **vals )
333 {
334 #ifdef HAVE_LDAP
335
336 TRACE( "(%p)\n", vals );
337 ldap_value_free_len( (struct berval **)vals );
338
339 #endif
340 return WLDAP32_LDAP_SUCCESS;
341 }
342
343 /***********************************************************************
344 * ldap_value_freeA (WLDAP32.@)
345 *
346 * See ldap_value_freeW.
347 */
348 ULONG CDECL ldap_value_freeA( PCHAR *vals )
349 {
350 TRACE( "(%p)\n", vals );
351
352 strarrayfreeA( vals );
353 return WLDAP32_LDAP_SUCCESS;
354 }
355
356 /***********************************************************************
357 * ldap_value_freeW (WLDAP32.@)
358 *
359 * Free an array of string values.
360 *
361 * PARAMS
362 * vals [I] Array of string values.
363 *
364 * RETURNS
365 * Success: LDAP_SUCCESS
366 * Failure: An LDAP error code.
367 */
368 ULONG CDECL ldap_value_freeW( PWCHAR *vals )
369 {
370 TRACE( "(%p)\n", vals );
371
372 strarrayfreeW( vals );
373 return WLDAP32_LDAP_SUCCESS;
374 }