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