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
)
52 struct ber_alloc_t_params params
;
54 if (!(ret
= malloc( sizeof(*ret
) ))) return NULL
;
55 params
.options
= options
;
56 params
.ret
= (void **)&BER(ret
);
57 if (LDAP_CALL( ber_alloc_t
, ¶ms
))
66 /***********************************************************************
67 * ber_bvdup (WLDAP32.@)
69 * Copy a berval structure.
72 * berval [I] Pointer to the berval structure to be copied.
75 * Success: Pointer to a copy of the berval structure.
79 * Free the copy with ber_bvfree.
81 BERVAL
* CDECL
ber_bvdup( BERVAL
*berval
)
83 return bervalWtoW( berval
);
87 /***********************************************************************
88 * ber_bvecfree (WLDAP32.@)
90 * Free an array of berval structures.
93 * berval [I] Pointer to an array of berval structures.
99 * Use this function only to free an array of berval structures
100 * returned by a call to ber_scanf with a 'V' in the format string.
102 void CDECL
ber_bvecfree( BERVAL
**berval
)
104 bvarrayfreeW( berval
);
108 /***********************************************************************
109 * ber_bvfree (WLDAP32.@)
111 * Free a berval structure.
114 * berval [I] Pointer to a berval structure.
120 * Use this function only to free berval structures allocated by
123 void CDECL
ber_bvfree( BERVAL
*berval
)
129 /***********************************************************************
130 * ber_first_element (WLDAP32.@)
132 * Return the tag of the first element in a set or sequence.
135 * berelement [I] Pointer to a berelement structure.
136 * len [O] Receives the length of the first element.
137 * opaque [O] Receives a pointer to a cookie.
140 * Success: Tag of the first element.
141 * Failure: LBER_DEFAULT (no more data).
144 * len and cookie should be passed to ber_next_element.
146 ULONG CDECL
ber_first_element( BerElement
*ber
, ULONG
*len
, char **opaque
)
148 struct ber_first_element_params params
= { BER(ber
), (unsigned int *)len
, opaque
};
149 return LDAP_CALL( ber_first_element
, ¶ms
);
153 /***********************************************************************
154 * ber_flatten (WLDAP32.@)
156 * Flatten a berelement structure into a berval structure.
159 * berelement [I] Pointer to a berelement structure.
160 * berval [O] Pointer to a berval structure.
164 * Failure: LBER_ERROR
167 * Free the berval structure with ber_bvfree.
169 int CDECL
ber_flatten( BerElement
*ber
, BERVAL
**berval
)
171 struct bervalU
*bervalU
;
172 struct berval
*bervalW
;
173 struct ber_flatten_params params
= { BER(ber
), &bervalU
};
175 if (LDAP_CALL( ber_flatten
, ¶ms
)) return LBER_ERROR
;
177 if (!(bervalW
= bervalUtoW( bervalU
))) return LBER_ERROR
;
178 LDAP_CALL( ber_bvfree
, bervalU
);
179 if (!bervalW
) return LBER_ERROR
;
185 /***********************************************************************
186 * ber_free (WLDAP32.@)
188 * Free a berelement structure.
191 * berelement [I] Pointer to the berelement structure to be freed.
198 * Set buf to 0 if the berelement was allocated with ldap_first_attribute
199 * or ldap_next_attribute, otherwise set it to 1.
201 void CDECL
ber_free( BerElement
*ber
, int freebuf
)
203 struct ber_free_params params
= { BER(ber
), freebuf
};
204 LDAP_CALL( ber_free
, ¶ms
);
209 /***********************************************************************
210 * ber_init (WLDAP32.@)
212 * Initialise a berelement structure from a berval structure.
215 * berval [I] Pointer to a berval structure.
218 * Success: Pointer to a berelement structure.
222 * Call ber_free to free the returned berelement structure.
224 BerElement
* CDECL
ber_init( BERVAL
*berval
)
226 struct bervalU
*bervalU
;
228 struct ber_init_params params
;
230 if (!(ret
= malloc( sizeof(*ret
) ))) return NULL
;
231 if (!(bervalU
= bervalWtoU( berval
)))
236 params
.berval
= bervalU
;
237 params
.ret
= (void **)&BER(ret
);
238 if (LDAP_CALL( ber_init
, ¶ms
))
248 /***********************************************************************
249 * ber_next_element (WLDAP32.@)
251 * Return the tag of the next element in a set or sequence.
254 * berelement [I] Pointer to a berelement structure.
255 * len [I/O] Receives the length of the next element.
256 * opaque [I/O] Pointer to a cookie.
259 * Success: Tag of the next element.
260 * Failure: LBER_DEFAULT (no more data).
263 * len and cookie are initialized by ber_first_element and should
264 * be passed on in subsequent calls to ber_next_element.
266 ULONG CDECL
ber_next_element( BerElement
*ber
, ULONG
*len
, char *opaque
)
268 struct ber_next_element_params params
= { BER(ber
), (unsigned int *)len
, opaque
};
269 return LDAP_CALL( ber_next_element
, ¶ms
);
273 /***********************************************************************
274 * ber_peek_tag (WLDAP32.@)
276 * Return the tag of the next element.
279 * berelement [I] Pointer to a berelement structure.
280 * len [O] Receives the length of the next element.
283 * Success: Tag of the next element.
284 * Failure: LBER_DEFAULT (no more data).
286 ULONG CDECL
ber_peek_tag( BerElement
*ber
, ULONG
*len
)
288 struct ber_peek_tag_params params
= { BER(ber
), (unsigned int *)len
};
289 return LDAP_CALL( ber_peek_tag
, ¶ms
);
293 /***********************************************************************
294 * ber_skip_tag (WLDAP32.@)
296 * Skip the current tag and return the tag of the next element.
299 * berelement [I] Pointer to a berelement structure.
300 * len [O] Receives the length of the skipped element.
303 * Success: Tag of the next element.
304 * Failure: LBER_DEFAULT (no more data).
306 ULONG CDECL
ber_skip_tag( BerElement
*ber
, ULONG
*len
)
308 struct ber_skip_tag_params params
= { BER(ber
), (unsigned int *)len
};
309 return LDAP_CALL( ber_skip_tag
, ¶ms
);
313 /***********************************************************************
314 * ber_printf (WLDAP32.@)
316 * Encode a berelement structure.
319 * berelement [I/O] Pointer to a berelement structure.
320 * fmt [I] Format string.
321 * ... [I] Values to encode.
324 * Success: Non-negative number.
325 * Failure: LBER_ERROR
328 * berelement must have been allocated with ber_alloc_t. This function
329 * can be called multiple times to append data.
331 int WINAPIV
ber_printf( BerElement
*ber
, char *fmt
, ... )
338 va_start( list
, fmt
);
341 struct ber_printf_params params
= { BER(ber
), new_fmt
};
348 params
.arg1
= va_arg( list
, int );
349 ret
= LDAP_CALL( ber_printf
, ¶ms
);
353 params
.arg1
= (ULONG_PTR
)va_arg( list
, char * );
354 ret
= LDAP_CALL( ber_printf
, ¶ms
);
357 params
.arg1
= va_arg( list
, unsigned int );
358 ret
= LDAP_CALL( ber_printf
, ¶ms
);
361 params
.arg1
= (ULONG_PTR
)va_arg( list
, char ** );
362 ret
= LDAP_CALL( ber_printf
, ¶ms
);
366 struct berval
**array
= va_arg( list
, struct berval
** );
367 struct bervalU
**arrayU
;
368 if (!(arrayU
= bvarrayWtoU( array
)))
373 params
.arg1
= (ULONG_PTR
)arrayU
;
374 ret
= LDAP_CALL( ber_printf
, ¶ms
);
375 bvarrayfreeU( arrayU
);
380 params
.arg1
= (ULONG_PTR
)va_arg( list
, char * );
381 params
.arg2
= va_arg( list
, int );
382 new_fmt
[0] = 'B'; /* 'X' is deprecated */
383 ret
= LDAP_CALL( ber_printf
, ¶ms
);
391 ret
= LDAP_CALL( ber_printf
, ¶ms
);
395 FIXME( "Unknown format '%c'\n", new_fmt
[0] );
399 if (ret
== -1) break;
406 /***********************************************************************
407 * ber_scanf (WLDAP32.@)
409 * Decode a berelement structure.
412 * berelement [I/O] Pointer to a berelement structure.
413 * fmt [I] Format string.
414 * ... [I] Pointers to values to be decoded.
417 * Success: Non-negative number.
418 * Failure: LBER_ERROR
421 * berelement must have been allocated with ber_init. This function
422 * can be called multiple times to decode data.
424 ULONG WINAPIV
ber_scanf( BerElement
*ber
, char *fmt
, ... )
431 va_start( list
, fmt
);
434 struct ber_scanf_params params
= { BER(ber
), new_fmt
};
440 char *str
, **ptr
= va_arg( list
, char ** );
442 if ((ret
= LDAP_CALL( ber_scanf
, ¶ms
)) == -1) break;
443 *ptr
= strdupU( str
);
444 LDAP_CALL( ldap_memfree
, str
);
450 params
.arg1
= va_arg( list
, int * );
451 ret
= LDAP_CALL( ber_scanf
, ¶ms
);
454 params
.arg1
= va_arg( list
, unsigned int * );
455 ret
= LDAP_CALL( ber_scanf
, ¶ms
);
459 char *str
, **arrayU
, **ptr
, ***array
= va_arg( list
, char *** );
460 params
.arg1
= &arrayU
;
461 if ((ret
= LDAP_CALL( ber_scanf
, ¶ms
)) == -1) break;
462 *array
= strarrayUtoU( arrayU
);
466 LDAP_CALL( ldap_memfree
, str
);
469 LDAP_CALL( ldap_memfree
, arrayU
);
474 char *strU
, **str
= va_arg( list
, char ** );
475 int *len
= va_arg( list
, int * );
478 if ((ret
= LDAP_CALL( ber_scanf
, ¶ms
)) == -1) break;
479 *str
= malloc( *len
);
480 memcpy( *str
, strU
, *len
);
481 LDAP_CALL( ldap_memfree
, strU
);
486 struct berval
**berval
= va_arg( list
, struct berval
** );
487 struct bervalU
*bervalU
;
488 params
.arg1
= &bervalU
;
489 if ((ret
= LDAP_CALL( ber_scanf
, ¶ms
)) == -1) break;
490 *berval
= bervalUtoW( bervalU
);
491 LDAP_CALL( ber_bvfree
, bervalU
);
496 struct berval
***array
= va_arg( list
, struct berval
*** );
497 struct bervalU
**arrayU
;
498 params
.arg1
= &arrayU
;
499 if ((ret
= LDAP_CALL( ber_scanf
, ¶ms
)) == -1) break;
500 *array
= bvarrayUtoW( arrayU
);
501 LDAP_CALL( ber_bvecfree
, arrayU
);
510 ret
= LDAP_CALL( ber_scanf
, ¶ms
);
514 FIXME( "Unknown format '%c'\n", new_fmt
[0] );
518 if (ret
== -1) break;