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
30 #include "winldap_private.h"
32 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(wldap32
);
38 #define WLDAP32_LBER_ERROR (~0U)
40 /***********************************************************************
41 * ber_alloc_t (WLDAP32.@)
43 * Allocate a berelement structure.
46 * options [I] Must be LBER_USE_DER.
49 * Success: Pointer to an allocated berelement structure.
53 * Free the berelement structure with ber_free.
55 WLDAP32_BerElement
* CDECL
WLDAP32_ber_alloc_t( INT options
)
58 return ber_alloc_t( options
);
65 /***********************************************************************
66 * ber_bvdup (WLDAP32.@)
68 * Copy a berval structure.
71 * berval [I] Pointer to the berval structure to be copied.
74 * Success: Pointer to a copy of the berval structure.
78 * Free the copy with ber_bvfree.
80 BERVAL
* CDECL
WLDAP32_ber_bvdup( BERVAL
*berval
)
82 return bervalWtoW( berval
);
86 /***********************************************************************
87 * ber_bvecfree (WLDAP32.@)
89 * Free an array of berval structures.
92 * berval [I] Pointer to an array of berval structures.
98 * Use this function only to free an array of berval structures
99 * returned by a call to ber_scanf with a 'V' in the format string.
101 void CDECL
WLDAP32_ber_bvecfree( PBERVAL
*berval
)
103 bvarrayfreeW( berval
);
107 /***********************************************************************
108 * ber_bvfree (WLDAP32.@)
110 * Free a berval structure.
113 * berval [I] Pointer to a berval structure.
119 * Use this function only to free berval structures allocated by
122 void CDECL
WLDAP32_ber_bvfree( BERVAL
*berval
)
128 /***********************************************************************
129 * ber_first_element (WLDAP32.@)
131 * Return the tag of the first element in a set or sequence.
134 * berelement [I] Pointer to a berelement structure.
135 * len [O] Receives the length of the first element.
136 * opaque [O] Receives a pointer to a cookie.
139 * Success: Tag of the first element.
140 * Failure: LBER_DEFAULT (no more data).
143 * len and cookie should be passed to ber_next_element.
145 ULONG CDECL
WLDAP32_ber_first_element( WLDAP32_BerElement
*berelement
, ULONG
*ret_len
, CHAR
**opaque
)
151 if ((ret
= ber_first_element( berelement
, &len
, opaque
)) != LBER_ERROR
)
155 ERR( "len too large\n" );
156 return WLDAP32_LBER_ERROR
;
163 return WLDAP32_LBER_ERROR
;
168 /***********************************************************************
169 * ber_flatten (WLDAP32.@)
171 * Flatten a berelement structure into a berval structure.
174 * berelement [I] Pointer to a berelement structure.
175 * berval [O] Pointer to a berval structure.
179 * Failure: LBER_ERROR
182 * Free the berval structure with ber_bvfree.
184 INT CDECL
WLDAP32_ber_flatten( WLDAP32_BerElement
*berelement
, PBERVAL
*berval
)
187 struct berval
*bervalU
;
188 struct WLDAP32_berval
*bervalW
;
190 if (ber_flatten( berelement
, &bervalU
)) return WLDAP32_LBER_ERROR
;
192 bervalW
= bervalUtoW( bervalU
);
193 ber_bvfree( bervalU
);
194 if (!bervalW
) return WLDAP32_LBER_ERROR
;
199 return WLDAP32_LBER_ERROR
;
204 /***********************************************************************
205 * ber_free (WLDAP32.@)
207 * Free a berelement structure.
210 * berelement [I] Pointer to the berelement structure to be freed.
217 * Set buf to 0 if the berelement was allocated with ldap_first_attribute
218 * or ldap_next_attribute, otherwise set it to 1.
220 void CDECL
WLDAP32_ber_free( WLDAP32_BerElement
*berelement
, INT buf
)
223 ber_free( berelement
, buf
);
228 /***********************************************************************
229 * ber_init (WLDAP32.@)
231 * Initialise a berelement structure from a berval structure.
234 * berval [I] Pointer to a berval structure.
237 * Success: Pointer to a berelement structure.
241 * Call ber_free to free the returned berelement structure.
243 WLDAP32_BerElement
* CDECL
WLDAP32_ber_init( BERVAL
*berval
)
246 struct berval
*bervalU
;
247 WLDAP32_BerElement
*ret
;
249 if (!(bervalU
= bervalWtoU( berval
))) return NULL
;
250 ret
= ber_init( bervalU
);
251 heap_free( bervalU
);
259 /***********************************************************************
260 * ber_next_element (WLDAP32.@)
262 * Return the tag of the next element in a set or sequence.
265 * berelement [I] Pointer to a berelement structure.
266 * len [I/O] Receives the length of the next element.
267 * opaque [I/O] Pointer to a cookie.
270 * Success: Tag of the next element.
271 * Failure: LBER_DEFAULT (no more data).
274 * len and cookie are initialized by ber_first_element and should
275 * be passed on in subsequent calls to ber_next_element.
277 ULONG CDECL
WLDAP32_ber_next_element( WLDAP32_BerElement
*berelement
, ULONG
*ret_len
, CHAR
*opaque
)
283 if ((ret
= ber_next_element( berelement
, &len
, opaque
)) != LBER_ERROR
)
287 ERR( "len too large\n" );
288 return WLDAP32_LBER_ERROR
;
295 return WLDAP32_LBER_ERROR
;
300 /***********************************************************************
301 * ber_peek_tag (WLDAP32.@)
303 * Return the tag of the next element.
306 * berelement [I] Pointer to a berelement structure.
307 * len [O] Receives the length of the next element.
310 * Success: Tag of the next element.
311 * Failure: LBER_DEFAULT (no more data).
313 ULONG CDECL
WLDAP32_ber_peek_tag( WLDAP32_BerElement
*berelement
, ULONG
*ret_len
)
319 if ((ret
= ber_peek_tag( berelement
, &len
)) != LBER_ERROR
)
323 ERR( "len too large\n" );
324 return WLDAP32_LBER_ERROR
;
331 return WLDAP32_LBER_ERROR
;
336 /***********************************************************************
337 * ber_skip_tag (WLDAP32.@)
339 * Skip the current tag and return the tag of the next element.
342 * berelement [I] Pointer to a berelement structure.
343 * len [O] Receives the length of the skipped element.
346 * Success: Tag of the next element.
347 * Failure: LBER_DEFAULT (no more data).
349 ULONG CDECL
WLDAP32_ber_skip_tag( WLDAP32_BerElement
*berelement
, ULONG
*ret_len
)
355 if ((ret
= ber_skip_tag( berelement
, &len
)) != LBER_ERROR
)
359 ERR( "len too large\n" );
360 return WLDAP32_LBER_ERROR
;
367 return WLDAP32_LBER_ERROR
;
372 /***********************************************************************
373 * ber_printf (WLDAP32.@)
375 * Encode a berelement structure.
378 * berelement [I/O] Pointer to a berelement structure.
379 * fmt [I] Format string.
380 * ... [I] Values to encode.
383 * Success: Non-negative number.
384 * Failure: LBER_ERROR
387 * berelement must have been allocated with ber_alloc_t. This function
388 * can be called multiple times to append data.
390 INT WINAPIV
WLDAP32_ber_printf( WLDAP32_BerElement
*berelement
, PCHAR fmt
, ... )
398 __ms_va_start( list
, fmt
);
408 int i
= va_arg( list
, int );
409 ret
= ber_printf( berelement
, new_fmt
, i
);
415 char *str
= va_arg( list
, char * );
416 ret
= ber_printf( berelement
, new_fmt
, str
);
421 unsigned int tag
= va_arg( list
, unsigned int );
422 ret
= ber_printf( berelement
, new_fmt
, tag
);
427 char **array
= va_arg( list
, char ** );
428 ret
= ber_printf( berelement
, new_fmt
, array
);
433 struct WLDAP32_berval
**array
= va_arg( list
, struct WLDAP32_berval
** );
434 struct berval
**arrayU
;
435 if (!(arrayU
= bvarrayWtoU( array
)))
440 ret
= ber_printf( berelement
, new_fmt
, arrayU
);
441 bvarrayfreeU( arrayU
);
446 char *str
= va_arg( list
, char * );
447 int len
= va_arg( list
, int );
448 new_fmt
[0] = 'B'; /* 'X' is deprecated */
449 ret
= ber_printf( berelement
, new_fmt
, str
, len
);
457 ret
= ber_printf( berelement
, new_fmt
);
460 FIXME( "Unknown format '%c'\n", new_fmt
[0] );
464 if (ret
== -1) break;
469 return WLDAP32_LBER_ERROR
;
474 /***********************************************************************
475 * ber_scanf (WLDAP32.@)
477 * Decode a berelement structure.
480 * berelement [I/O] Pointer to a berelement structure.
481 * fmt [I] Format string.
482 * ... [I] Pointers to values to be decoded.
485 * Success: Non-negative number.
486 * Failure: LBER_ERROR
489 * berelement must have been allocated with ber_init. This function
490 * can be called multiple times to decode data.
492 INT WINAPIV
WLDAP32_ber_scanf( WLDAP32_BerElement
*berelement
, PCHAR fmt
, ... )
500 __ms_va_start( list
, fmt
);
508 char **ptr
= va_arg( list
, char ** );
509 ret
= ber_scanf( berelement
, new_fmt
, ptr
);
516 int *i
= va_arg( list
, int * );
517 ret
= ber_scanf( berelement
, new_fmt
, i
);
522 unsigned int *tag
= va_arg( list
, unsigned int * );
523 ret
= ber_scanf( berelement
, new_fmt
, tag
);
528 char ***array
= va_arg( list
, char *** );
529 ret
= ber_scanf( berelement
, new_fmt
, array
);
534 char **str
= va_arg( list
, char ** );
535 int *len
= va_arg( list
, int * );
536 ret
= ber_scanf( berelement
, new_fmt
, str
, len
);
541 struct berval
**ptr
= va_arg( list
, struct berval
** );
542 ret
= ber_scanf( berelement
, new_fmt
, ptr
);
547 struct WLDAP32_berval
**arrayW
, ***array
= va_arg( list
, struct WLDAP32_berval
*** );
548 struct berval
**arrayU
;
549 if ((ret
= ber_scanf( berelement
, new_fmt
, &arrayU
)) == -1) break;
550 if ((arrayW
= bvarrayUtoW( arrayU
))) *array
= arrayW
;
552 bvarrayfreeU( arrayU
);
561 ret
= ber_scanf( berelement
, new_fmt
);
564 FIXME( "Unknown format '%c'\n", new_fmt
[0] );
568 if (ret
== -1) break;
573 return WLDAP32_LBER_ERROR
;