ntdll: Factor out heap serialization to heap_(lock|unlock) helpers.
[wine.git] / dlls / wldap32 / value.c
blob219f6a6d2f511407be9f456aca4ca68760d9c137
1 /*
2 * WLDAP32 - LDAP support for Wine
4 * Copyright 2005 Hans Leidekker
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.
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.
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
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnls.h"
26 #include "winldap.h"
28 #include "wine/debug.h"
29 #include "winldap_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
33 /***********************************************************************
34 * ldap_count_values_len (WLDAP32.@)
36 * Count the number of values in an array of berval structures.
38 * PARAMS
39 * values [I] Pointer to an array of berval structures.
41 * RETURNS
42 * Success: The number of values counted.
43 * Failure: 0
45 * NOTES
46 * Call ldap_count_values_len with the result of a call to
47 * ldap_get_values_len.
49 ULONG CDECL ldap_count_values_len( struct berval **values )
51 ULONG ret = 0;
52 struct berval **ptr = values;
54 TRACE( "(%p)\n", values );
56 if (!values) return 0;
57 while (*ptr++) ret++;
58 return ret;
61 /***********************************************************************
62 * ldap_count_valuesA (WLDAP32.@)
64 * See ldap_count_valuesW.
66 ULONG CDECL ldap_count_valuesA( char **values )
68 ULONG ret = 0;
69 char **ptr = values;
71 TRACE( "(%p)\n", values );
73 if (!values) return 0;
74 while (*ptr++) ret++;
75 return ret;
78 /***********************************************************************
79 * ldap_count_valuesW (WLDAP32.@)
81 * Count the number of values in a string array.
83 * PARAMS
84 * values [I] Pointer to an array of strings.
86 * RETURNS
87 * Success: The number of values counted.
88 * Failure: 0
90 * NOTES
91 * Call ldap_count_valuesW with the result of a call to
92 * ldap_get_valuesW.
94 ULONG CDECL ldap_count_valuesW( WCHAR **values )
96 ULONG ret = 0;
97 WCHAR **p = values;
99 TRACE( "(%p)\n", values );
101 if (!values) return 0;
102 while (*p++) ret++;
103 return ret;
106 /***********************************************************************
107 * ldap_get_valuesA (WLDAP32.@)
109 * See ldap_get_valuesW.
111 char ** CDECL ldap_get_valuesA( LDAP *ld, LDAPMessage *entry, char *attr )
113 char **ret;
114 WCHAR *attrW = NULL, **retW;
116 TRACE( "(%p, %p, %s)\n", ld, entry, debugstr_a(attr) );
118 if (!ld || !entry || !attr || !(attrW = strAtoW( attr ))) return NULL;
120 retW = ldap_get_valuesW( ld, entry, attrW );
121 ret = strarrayWtoA( retW );
123 ldap_value_freeW( retW );
124 free( attrW );
125 return ret;
128 static char *bv2str( struct bervalU *bv )
130 char *str = NULL;
131 unsigned int len = bv->bv_len;
133 if ((str = malloc( len + 1 )))
135 memcpy( str, bv->bv_val, len );
136 str[len] = '\0';
138 return str;
141 static char **bv2str_array( struct bervalU **bv )
143 unsigned int len = 0, i = 0;
144 struct bervalU **p = bv;
145 char **str;
147 while (*p)
149 len++;
150 p++;
152 if (!(str = malloc( (len + 1) * sizeof(char *) ))) return NULL;
154 p = bv;
155 while (*p)
157 str[i] = bv2str( *p );
158 if (!str[i])
160 while (i > 0) free( str[--i] );
161 free( str );
162 return NULL;
164 i++;
165 p++;
167 str[i] = NULL;
168 return str;
171 /***********************************************************************
172 * ldap_get_valuesW (WLDAP32.@)
174 * Retrieve string values for a given attribute.
176 * PARAMS
177 * ld [I] Pointer to an LDAP context.
178 * entry [I] Entry to retrieve values from.
179 * attr [I] Attribute to retrieve values for.
181 * RETURNS
182 * Success: Pointer to a character array holding the values.
183 * Failure: NULL
185 * NOTES
186 * Call ldap_get_valuesW with the result of a call to
187 * ldap_first_entry or ldap_next_entry. Free the returned
188 * array with a call to ldap_value_freeW.
190 WCHAR ** CDECL ldap_get_valuesW( LDAP *ld, LDAPMessage *entry, WCHAR *attr )
192 WCHAR **ret = NULL;
193 char *attrU, **retU;
194 struct bervalU **bv;
196 TRACE( "(%p, %p, %s)\n", ld, entry, debugstr_w(attr) );
198 if (ld && entry && attr && (attrU = strWtoU( attr )))
200 struct ldap_get_values_len_params params = { CTX(ld), MSG(entry), attrU, &bv };
202 if (!LDAP_CALL( ldap_get_values_len, &params ))
204 retU = bv2str_array( bv );
205 ret = strarrayUtoW( retU );
207 LDAP_CALL( ldap_value_free_len, bv );
208 strarrayfreeU( retU );
210 free( attrU );
212 return ret;
215 /***********************************************************************
216 * ldap_get_values_lenA (WLDAP32.@)
218 * See ldap_get_values_lenW.
220 struct berval ** CDECL ldap_get_values_lenA( LDAP *ld, LDAPMessage *message, char *attr )
222 WCHAR *attrW;
223 struct berval **ret;
225 TRACE( "(%p, %p, %s)\n", ld, message, debugstr_a(attr) );
227 if (!ld || !message || !attr || !(attrW = strAtoW( attr ))) return NULL;
229 ret = ldap_get_values_lenW( ld, message, attrW );
231 free( attrW );
232 return ret;
235 /***********************************************************************
236 * ldap_get_values_lenW (WLDAP32.@)
238 * Retrieve binary values for a given attribute.
240 * PARAMS
241 * ld [I] Pointer to an LDAP context.
242 * message [I] Entry to retrieve values from.
243 * attr [I] Attribute to retrieve values for.
245 * RETURNS
246 * Success: Pointer to a berval array holding the values.
247 * Failure: NULL
249 * NOTES
250 * Call ldap_get_values_lenW with the result of a call to
251 * ldap_first_entry or ldap_next_entry. Free the returned
252 * array with a call to ldap_value_free_len.
254 struct berval ** CDECL ldap_get_values_lenW( LDAP *ld, LDAPMessage *message, WCHAR *attr )
256 char *attrU = NULL;
257 struct bervalU **retU;
258 struct berval **ret = NULL;
260 TRACE( "(%p, %p, %s)\n", ld, message, debugstr_w(attr) );
262 if (ld && message && attr && (attrU = strWtoU( attr )))
264 struct ldap_get_values_len_params params = { CTX(ld), MSG(message), attrU, &retU };
266 if (!LDAP_CALL( ldap_get_values_len, &params ))
268 ret = bvarrayUtoW( retU );
269 bvarrayfreeU( retU );
272 free( attrU );
274 return ret;
277 /***********************************************************************
278 * ldap_value_free_len (WLDAP32.@)
280 * Free an array of berval structures.
282 * PARAMS
283 * values [I] Array of berval structures.
285 * RETURNS
286 * Success: LDAP_SUCCESS
287 * Failure: An LDAP error code.
289 ULONG CDECL ldap_value_free_len( struct berval **values )
291 TRACE( "(%p)\n", values );
293 bvarrayfreeW( values );
294 return LDAP_SUCCESS;
297 /***********************************************************************
298 * ldap_value_freeA (WLDAP32.@)
300 * See ldap_value_freeW.
302 ULONG CDECL ldap_value_freeA( char **values )
304 TRACE( "(%p)\n", values );
306 strarrayfreeA( values );
307 return LDAP_SUCCESS;
310 /***********************************************************************
311 * ldap_value_freeW (WLDAP32.@)
313 * Free an array of string values.
315 * PARAMS
316 * values [I] Array of string values.
318 * RETURNS
319 * Success: LDAP_SUCCESS
320 * Failure: An LDAP error code.
322 ULONG CDECL ldap_value_freeW( WCHAR **values )
324 TRACE( "(%p)\n", values );
326 strarrayfreeW( values );
327 return LDAP_SUCCESS;