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
29 #include "wine/debug.h"
30 #include "winldap_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(wldap32
);
34 /***********************************************************************
35 * ber_alloc_t (WLDAP32.@)
37 * Allocate a berelement structure.
40 * options [I] Must be LBER_USE_DER.
43 * Success: Pointer to an allocated berelement structure.
47 * Free the berelement structure with ber_free.
49 BerElement
* CDECL
ber_alloc_t( int options
)
53 if (!(ret
= malloc( sizeof(*ret
) ))) return NULL
;
54 if (!(BER(ret
) = ldap_funcs
->fn_ber_alloc_t( options
)))
63 /***********************************************************************
64 * ber_bvdup (WLDAP32.@)
66 * Copy a berval structure.
69 * berval [I] Pointer to the berval structure to be copied.
72 * Success: Pointer to a copy of the berval structure.
76 * Free the copy with ber_bvfree.
78 BERVAL
* CDECL
ber_bvdup( BERVAL
*berval
)
80 return bervalWtoW( berval
);
84 /***********************************************************************
85 * ber_bvecfree (WLDAP32.@)
87 * Free an array of berval structures.
90 * berval [I] Pointer to an array of berval structures.
96 * Use this function only to free an array of berval structures
97 * returned by a call to ber_scanf with a 'V' in the format string.
99 void CDECL
ber_bvecfree( BERVAL
**berval
)
101 bvarrayfreeW( berval
);
105 /***********************************************************************
106 * ber_bvfree (WLDAP32.@)
108 * Free a berval structure.
111 * berval [I] Pointer to a berval structure.
117 * Use this function only to free berval structures allocated by
120 void CDECL
ber_bvfree( BERVAL
*berval
)
126 /***********************************************************************
127 * ber_first_element (WLDAP32.@)
129 * Return the tag of the first element in a set or sequence.
132 * berelement [I] Pointer to a berelement structure.
133 * len [O] Receives the length of the first element.
134 * opaque [O] Receives a pointer to a cookie.
137 * Success: Tag of the first element.
138 * Failure: LBER_DEFAULT (no more data).
141 * len and cookie should be passed to ber_next_element.
143 ULONG CDECL
ber_first_element( BerElement
*ber
, ULONG
*len
, char **opaque
)
145 return ldap_funcs
->fn_ber_first_element( BER(ber
), len
, opaque
);
149 /***********************************************************************
150 * ber_flatten (WLDAP32.@)
152 * Flatten a berelement structure into a berval structure.
155 * berelement [I] Pointer to a berelement structure.
156 * berval [O] Pointer to a berval structure.
160 * Failure: LBER_ERROR
163 * Free the berval structure with ber_bvfree.
165 int CDECL
ber_flatten( BerElement
*ber
, BERVAL
**berval
)
167 struct bervalU
*bervalU
;
168 struct berval
*bervalW
;
170 if (ldap_funcs
->fn_ber_flatten( BER(ber
), &bervalU
)) return LBER_ERROR
;
172 if (!(bervalW
= bervalUtoW( bervalU
))) return LBER_ERROR
;
173 ldap_funcs
->fn_ber_bvfree( bervalU
);
174 if (!bervalW
) return LBER_ERROR
;
180 /***********************************************************************
181 * ber_free (WLDAP32.@)
183 * Free a berelement structure.
186 * berelement [I] Pointer to the berelement structure to be freed.
193 * Set buf to 0 if the berelement was allocated with ldap_first_attribute
194 * or ldap_next_attribute, otherwise set it to 1.
196 void CDECL
ber_free( BerElement
*ber
, int freebuf
)
198 ldap_funcs
->fn_ber_free( BER(ber
), freebuf
);
203 /***********************************************************************
204 * ber_init (WLDAP32.@)
206 * Initialise a berelement structure from a berval structure.
209 * berval [I] Pointer to a berval structure.
212 * Success: Pointer to a berelement structure.
216 * Call ber_free to free the returned berelement structure.
218 BerElement
* CDECL
ber_init( BERVAL
*berval
)
220 struct bervalU
*bervalU
;
223 if (!(ret
= malloc( sizeof(*ret
) ))) return NULL
;
224 if (!(bervalU
= bervalWtoU( berval
)))
229 if (!(BER(ret
) = ldap_funcs
->fn_ber_init( bervalU
)))
239 /***********************************************************************
240 * ber_next_element (WLDAP32.@)
242 * Return the tag of the next element in a set or sequence.
245 * berelement [I] Pointer to a berelement structure.
246 * len [I/O] Receives the length of the next element.
247 * opaque [I/O] Pointer to a cookie.
250 * Success: Tag of the next element.
251 * Failure: LBER_DEFAULT (no more data).
254 * len and cookie are initialized by ber_first_element and should
255 * be passed on in subsequent calls to ber_next_element.
257 ULONG CDECL
ber_next_element( BerElement
*ber
, ULONG
*len
, char *opaque
)
259 return ldap_funcs
->fn_ber_next_element( BER(ber
), len
, opaque
);
263 /***********************************************************************
264 * ber_peek_tag (WLDAP32.@)
266 * Return the tag of the next element.
269 * berelement [I] Pointer to a berelement structure.
270 * len [O] Receives the length of the next element.
273 * Success: Tag of the next element.
274 * Failure: LBER_DEFAULT (no more data).
276 ULONG CDECL
ber_peek_tag( BerElement
*ber
, ULONG
*len
)
278 return ldap_funcs
->fn_ber_peek_tag( BER(ber
), len
);
282 /***********************************************************************
283 * ber_skip_tag (WLDAP32.@)
285 * Skip the current tag and return the tag of the next element.
288 * berelement [I] Pointer to a berelement structure.
289 * len [O] Receives the length of the skipped element.
292 * Success: Tag of the next element.
293 * Failure: LBER_DEFAULT (no more data).
295 ULONG CDECL
ber_skip_tag( BerElement
*ber
, ULONG
*len
)
297 return ldap_funcs
->fn_ber_skip_tag( BER(ber
), len
);
301 /***********************************************************************
302 * ber_printf (WLDAP32.@)
304 * Encode a berelement structure.
307 * berelement [I/O] Pointer to a berelement structure.
308 * fmt [I] Format string.
309 * ... [I] Values to encode.
312 * Success: Non-negative number.
313 * Failure: LBER_ERROR
316 * berelement must have been allocated with ber_alloc_t. This function
317 * can be called multiple times to append data.
319 int WINAPIV
ber_printf( BerElement
*ber
, char *fmt
, ... )
326 __ms_va_start( list
, fmt
);
336 int i
= va_arg( list
, int );
337 ret
= ldap_funcs
->fn_ber_printf( BER(ber
), new_fmt
, i
);
343 char *str
= va_arg( list
, char * );
344 ret
= ldap_funcs
->fn_ber_printf( BER(ber
), new_fmt
, str
);
349 unsigned int tag
= va_arg( list
, unsigned int );
350 ret
= ldap_funcs
->fn_ber_printf( BER(ber
), new_fmt
, tag
);
355 char **array
= va_arg( list
, char ** );
356 ret
= ldap_funcs
->fn_ber_printf( BER(ber
), new_fmt
, array
);
361 struct berval
**array
= va_arg( list
, struct berval
** );
362 struct bervalU
**arrayU
;
363 if (!(arrayU
= bvarrayWtoU( array
)))
368 ret
= ldap_funcs
->fn_ber_printf( BER(ber
), new_fmt
, arrayU
);
369 bvarrayfreeU( arrayU
);
374 char *str
= va_arg( list
, char * );
375 int len
= va_arg( list
, int );
376 new_fmt
[0] = 'B'; /* 'X' is deprecated */
377 ret
= ldap_funcs
->fn_ber_printf( BER(ber
), new_fmt
, str
, len
);
385 ret
= ldap_funcs
->fn_ber_printf( BER(ber
), new_fmt
);
389 FIXME( "Unknown format '%c'\n", new_fmt
[0] );
393 if (ret
== -1) break;
400 /***********************************************************************
401 * ber_scanf (WLDAP32.@)
403 * Decode a berelement structure.
406 * berelement [I/O] Pointer to a berelement structure.
407 * fmt [I] Format string.
408 * ... [I] Pointers to values to be decoded.
411 * Success: Non-negative number.
412 * Failure: LBER_ERROR
415 * berelement must have been allocated with ber_init. This function
416 * can be called multiple times to decode data.
418 ULONG WINAPIV
ber_scanf( BerElement
*ber
, char *fmt
, ... )
425 __ms_va_start( list
, fmt
);
433 char *str
, **ptr
= va_arg( list
, char ** );
434 if ((ret
= ldap_funcs
->fn_ber_scanf( BER(ber
), new_fmt
, &str
)) == -1) break;
435 *ptr
= strdupU( str
);
436 ldap_funcs
->fn_ldap_memfree( str
);
443 int *i
= va_arg( list
, int * );
444 ret
= ldap_funcs
->fn_ber_scanf( BER(ber
), new_fmt
, i
);
449 unsigned int *tag
= va_arg( list
, unsigned int * );
450 ret
= ldap_funcs
->fn_ber_scanf( BER(ber
), new_fmt
, tag
);
455 char *str
, **arrayU
, **ptr
, ***array
= va_arg( list
, char *** );
456 if ((ret
= ldap_funcs
->fn_ber_scanf( BER(ber
), new_fmt
, &arrayU
)) == -1) break;
457 *array
= strarrayUtoU( arrayU
);
461 ldap_funcs
->fn_ldap_memfree( str
);
464 ldap_funcs
->fn_ldap_memfree( arrayU
);
469 char *strU
, **str
= va_arg( list
, char ** );
470 int *len
= va_arg( list
, int * );
471 if ((ret
= ldap_funcs
->fn_ber_scanf( BER(ber
), new_fmt
, &strU
, len
)) == -1) break;
472 *str
= malloc( *len
);
473 memcpy( *str
, strU
, *len
);
474 ldap_funcs
->fn_ldap_memfree( strU
);
479 struct berval
**berval
= va_arg( list
, struct berval
** );
480 struct bervalU
*bervalU
;
481 if ((ret
= ldap_funcs
->fn_ber_scanf( BER(ber
), new_fmt
, &bervalU
)) == -1) break;
482 *berval
= bervalUtoW( bervalU
);
483 ldap_funcs
->fn_ber_bvfree( bervalU
);
488 struct berval
***array
= va_arg( list
, struct berval
*** );
489 struct bervalU
**arrayU
;
490 if ((ret
= ldap_funcs
->fn_ber_scanf( BER(ber
), new_fmt
, &arrayU
)) == -1) break;
491 *array
= bvarrayUtoW( arrayU
);
492 ldap_funcs
->fn_ber_bvecfree( arrayU
);
501 ret
= ldap_funcs
->fn_ber_scanf( BER(ber
), new_fmt
);
505 FIXME( "Unknown format '%c'\n", new_fmt
[0] );
509 if (ret
== -1) break;