msvcrt: Import fmod implementation from musl.
[wine.git] / dlls / wldap32 / control.c
blob3afd0ddecf0bd8dcf2c1743596870b0e66ddf6b5
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 "windef.h"
23 #include "winbase.h"
24 #include "winnls.h"
25 #include "winldap.h"
27 #include "wine/debug.h"
28 #include "winldap_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
32 /***********************************************************************
33 * ldap_control_freeA (WLDAP32.@)
35 * See ldap_control_freeW.
37 ULONG CDECL ldap_control_freeA( LDAPControlA *control )
39 TRACE( "(%p)\n", control );
40 controlfreeA( control );
41 return LDAP_SUCCESS;
44 /***********************************************************************
45 * ldap_control_freeW (WLDAP32.@)
47 * Free an LDAPControl structure.
49 * PARAMS
50 * control [I] LDAPControl structure to free.
52 * RETURNS
53 * LDAP_SUCCESS
55 ULONG CDECL ldap_control_freeW( LDAPControlW *control )
57 TRACE( "(%p)\n", control );
58 controlfreeW( control );
59 return LDAP_SUCCESS;
62 /***********************************************************************
63 * ldap_controls_freeA (WLDAP32.@)
65 * See ldap_controls_freeW.
67 ULONG CDECL ldap_controls_freeA( LDAPControlA **controls )
69 TRACE( "(%p)\n", controls );
70 controlarrayfreeA( controls );
71 return LDAP_SUCCESS;
74 /***********************************************************************
75 * ldap_controls_freeW (WLDAP32.@)
77 * Free an array of LDAPControl structures.
79 * PARAMS
80 * controls [I] Array of LDAPControl structures to free.
82 * RETURNS
83 * LDAP_SUCCESS
85 ULONG CDECL ldap_controls_freeW( LDAPControlW **controls )
87 TRACE( "(%p)\n", controls );
88 controlarrayfreeW( controls );
89 return LDAP_SUCCESS;
92 /***********************************************************************
93 * ldap_create_sort_controlA (WLDAP32.@)
95 * See ldap_create_sort_controlW.
97 ULONG CDECL ldap_create_sort_controlA( LDAP *ld, LDAPSortKeyA **sortkey, UCHAR critical, LDAPControlA **control )
99 ULONG ret;
100 LDAPSortKeyW **sortkeyW;
101 LDAPControlW *controlW;
103 TRACE( "(%p, %p, 0x%02x, %p)\n", ld, sortkey, critical, control );
105 if (!ld || !sortkey || !control) return LDAP_PARAM_ERROR;
107 if (!(sortkeyW = sortkeyarrayAtoW( sortkey ))) return LDAP_NO_MEMORY;
109 ret = ldap_create_sort_controlW( ld, sortkeyW, critical, &controlW );
110 if (ret == LDAP_SUCCESS)
112 LDAPControlA *controlA = controlWtoA( controlW );
113 if (controlA) *control = controlA;
114 else ret = LDAP_NO_MEMORY;
115 ldap_control_freeW( controlW );
118 sortkeyarrayfreeW( sortkeyW );
119 return ret;
122 /***********************************************************************
123 * ldap_create_sort_controlW (WLDAP32.@)
125 * Create a control for server sorted search results.
127 * PARAMS
128 * ld [I] Pointer to an LDAP context.
129 * sortkey [I] Array of LDAPSortKey structures, each specifying an
130 * attribute to use as a sort key, a matching rule and
131 * the sort order (ascending or descending).
132 * critical [I] Tells the server this control is critical to the
133 * search operation.
134 * control [O] LDAPControl created.
136 * RETURNS
137 * Success: LDAP_SUCCESS
138 * Failure: An LDAP error code.
140 * NOTES
141 * Pass the created control as a server control in subsequent calls
142 * to ldap_search_ext(_s) to obtain sorted search results.
144 ULONG CDECL ldap_create_sort_controlW( LDAP *ld, LDAPSortKeyW **sortkey, UCHAR critical, LDAPControlW **control )
146 ULONG ret;
147 LDAPSortKeyU **sortkeyU;
148 LDAPControlU *controlU;
150 TRACE( "(%p, %p, 0x%02x, %p)\n", ld, sortkey, critical, control );
152 if (!ld || !sortkey || !control) return LDAP_PARAM_ERROR;
154 if (!(sortkeyU = sortkeyarrayWtoU( sortkey ))) return LDAP_NO_MEMORY;
156 ret = map_error( ldap_funcs->fn_ldap_create_sort_control( CTX(ld), sortkeyU, critical, &controlU ) );
157 if (ret == LDAP_SUCCESS)
159 LDAPControlW *controlW = controlUtoW( controlU );
160 if (controlW) *control = controlW;
161 else ret = LDAP_NO_MEMORY;
162 ldap_funcs->fn_ldap_control_free( controlU );
165 sortkeyarrayfreeU( sortkeyU );
166 return ret;
169 /***********************************************************************
170 * ldap_create_vlv_controlA (WLDAP32.@)
172 * See ldap_create_vlv_controlW.
174 INT CDECL ldap_create_vlv_controlA( LDAP *ld, LDAPVLVInfo *info, UCHAR critical, LDAPControlA **control )
176 INT ret;
177 LDAPControlW *controlW;
179 TRACE( "(%p, %p, 0x%02x, %p)\n", ld, info, critical, control );
181 if (!ld || !control) return ~0u;
183 ret = ldap_create_vlv_controlW( ld, info, critical, &controlW );
184 if (ret == LDAP_SUCCESS)
186 LDAPControlA *controlA = controlWtoA( controlW );
187 if (controlA) *control = controlA;
188 else ret = LDAP_NO_MEMORY;
189 ldap_control_freeW( controlW );
192 return ret;
195 /***********************************************************************
196 * ldap_create_vlv_controlW (WLDAP32.@)
198 * Create a virtual list view control.
200 * PARAMS
201 * ld [I] Pointer to an LDAP context.
202 * info [I] LDAPVLVInfo structure specifying a list view window.
203 * critical [I] Tells the server this control is critical to the
204 * search operation.
205 * control [O] LDAPControl created.
207 * RETURNS
208 * Success: LDAP_SUCCESS
209 * Failure: An LDAP error code.
211 * NOTES
212 * Pass the created control in conjunction with a sort control as
213 * server controls in subsequent calls to ldap_search_ext(_s). The
214 * server will then return a sorted, contiguous subset of results
215 * that meets the criteria specified in the LDAPVLVInfo structure.
217 INT CDECL ldap_create_vlv_controlW( LDAP *ld, LDAPVLVInfo *info, UCHAR critical, LDAPControlW **control )
219 ULONG ret;
220 LDAPVLVInfoU *infoU = NULL;
221 LDAPControlU *controlU;
223 TRACE( "(%p, %p, 0x%02x, %p)\n", ld, info, critical, control );
225 if (!ld || !control) return ~0u;
227 if (info && !(infoU = vlvinfoWtoU( info ))) return LDAP_NO_MEMORY;
229 ret = map_error( ldap_funcs->fn_ldap_create_vlv_control( CTX(ld), infoU, &controlU ) );
230 if (ret == LDAP_SUCCESS)
232 LDAPControlW *controlW = controlUtoW( controlU );
233 if (controlW) *control = controlW;
234 else ret = LDAP_NO_MEMORY;
235 ldap_funcs->fn_ldap_control_free( controlU );
238 vlvinfofreeU( infoU );
239 return ret;
242 static inline void bv_val_dup( const struct berval *src, struct berval *dst )
244 if ((dst->bv_val = RtlAllocateHeap( GetProcessHeap(), 0 , src->bv_len )))
246 memcpy( dst->bv_val, src->bv_val, src->bv_len );
247 dst->bv_len = src->bv_len;
249 else
250 dst->bv_len = 0;
253 /***********************************************************************
254 * ldap_encode_sort_controlA (WLDAP32.@)
256 * See ldap_encode_sort_controlW.
258 ULONG CDECL ldap_encode_sort_controlA( LDAP *ld, LDAPSortKeyA **sortkeys, LDAPControlA *ret, BOOLEAN critical )
260 LDAPControlA *control;
261 ULONG result;
263 if ((result = ldap_create_sort_controlA( ld, sortkeys, critical, &control )) == LDAP_SUCCESS)
265 ret->ldctl_oid = strdupU(control->ldctl_oid);
266 bv_val_dup( &control->ldctl_value, &ret->ldctl_value );
267 ret->ldctl_iscritical = control->ldctl_iscritical;
268 ldap_control_freeA( control );
270 return result;
273 /***********************************************************************
274 * ldap_encode_sort_controlW (WLDAP32.@)
276 * Create a control for server sorted search results.
278 * PARAMS
279 * ld [I] Pointer to an LDAP context.
280 * sortkey [I] Array of LDAPSortKey structures, each specifying an
281 * attribute to use as a sort key, a matching rule and
282 * the sort order (ascending or descending).
283 * critical [I] Tells the server this control is critical to the
284 * search operation.
285 * control [O] LDAPControl created.
287 * RETURNS
288 * Success: LDAP_SUCCESS
289 * Failure: An LDAP error code.
291 * NOTES
292 * This function is obsolete. Use its equivalent
293 * ldap_create_sort_control instead.
295 ULONG CDECL ldap_encode_sort_controlW( LDAP *ld, LDAPSortKeyW **sortkeys, LDAPControlW *ret, BOOLEAN critical )
297 LDAPControlW *control;
298 ULONG result;
300 if ((result = ldap_create_sort_controlW( ld, sortkeys, critical, &control )) == LDAP_SUCCESS)
302 ret->ldctl_oid = strdupW(control->ldctl_oid);
303 bv_val_dup( &control->ldctl_value, &ret->ldctl_value );
304 ret->ldctl_iscritical = control->ldctl_iscritical;
305 ldap_control_freeW( control );
307 return result;
310 /***********************************************************************
311 * ldap_free_controlsA (WLDAP32.@)
313 * See ldap_free_controlsW.
315 ULONG CDECL ldap_free_controlsA( LDAPControlA **controls )
317 return ldap_controls_freeA( controls );
320 /***********************************************************************
321 * ldap_free_controlsW (WLDAP32.@)
323 * Free an array of LDAPControl structures.
325 * PARAMS
326 * controls [I] Array of LDAPControl structures to free.
328 * RETURNS
329 * LDAP_SUCCESS
331 * NOTES
332 * Obsolete, use ldap_controls_freeW.
334 ULONG CDECL ldap_free_controlsW( LDAPControlW **controls )
336 return ldap_controls_freeW( controls );