wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / wldap32 / ber.c
blob7f39b9cfe17d76a5183daf6bbd160a3ccfd9917d
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 "config.h"
23 #include <stdarg.h>
24 #ifdef HAVE_LDAP_H
25 #include <ldap.h>
26 #endif
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winldap_private.h"
31 #include "wldap32.h"
32 #include "wine/debug.h"
34 #ifdef HAVE_LDAP
35 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
36 #endif
38 #define WLDAP32_LBER_ERROR (~0U)
40 /***********************************************************************
41 * ber_alloc_t (WLDAP32.@)
43 * Allocate a berelement structure.
45 * PARAMS
46 * options [I] Must be LBER_USE_DER.
48 * RETURNS
49 * Success: Pointer to an allocated berelement structure.
50 * Failure: NULL
52 * NOTES
53 * Free the berelement structure with ber_free.
55 WLDAP32_BerElement * CDECL WLDAP32_ber_alloc_t( INT options )
57 #ifdef HAVE_LDAP
58 return ber_alloc_t( options );
59 #else
60 return NULL;
61 #endif
65 /***********************************************************************
66 * ber_bvdup (WLDAP32.@)
68 * Copy a berval structure.
70 * PARAMS
71 * berval [I] Pointer to the berval structure to be copied.
73 * RETURNS
74 * Success: Pointer to a copy of the berval structure.
75 * Failure: NULL
77 * NOTES
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.
91 * PARAMS
92 * berval [I] Pointer to an array of berval structures.
94 * RETURNS
95 * Nothing.
97 * NOTES
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.
112 * PARAMS
113 * berval [I] Pointer to a berval structure.
115 * RETURNS
116 * Nothing.
118 * NOTES
119 * Use this function only to free berval structures allocated by
120 * an LDAP API.
122 void CDECL WLDAP32_ber_bvfree( BERVAL *berval )
124 heap_free( berval );
128 /***********************************************************************
129 * ber_first_element (WLDAP32.@)
131 * Return the tag of the first element in a set or sequence.
133 * PARAMS
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.
138 * RETURNS
139 * Success: Tag of the first element.
140 * Failure: LBER_DEFAULT (no more data).
142 * NOTES
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 )
147 #ifdef HAVE_LDAP
148 ber_len_t len;
149 ber_tag_t ret;
151 if ((ret = ber_first_element( berelement, &len, opaque )) != LBER_ERROR)
153 if (len > ~0u)
155 ERR( "len too large\n" );
156 return WLDAP32_LBER_ERROR;
158 *ret_len = len;
160 return ret;
162 #else
163 return WLDAP32_LBER_ERROR;
164 #endif
168 /***********************************************************************
169 * ber_flatten (WLDAP32.@)
171 * Flatten a berelement structure into a berval structure.
173 * PARAMS
174 * berelement [I] Pointer to a berelement structure.
175 * berval [O] Pointer to a berval structure.
177 * RETURNS
178 * Success: 0
179 * Failure: LBER_ERROR
181 * NOTES
182 * Free the berval structure with ber_bvfree.
184 INT CDECL WLDAP32_ber_flatten( WLDAP32_BerElement *berelement, PBERVAL *berval )
186 #ifdef HAVE_LDAP
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;
195 *berval = bervalW;
196 return 0;
198 #else
199 return WLDAP32_LBER_ERROR;
200 #endif
204 /***********************************************************************
205 * ber_free (WLDAP32.@)
207 * Free a berelement structure.
209 * PARAMS
210 * berelement [I] Pointer to the berelement structure to be freed.
211 * buf [I] Flag.
213 * RETURNS
214 * Nothing.
216 * NOTES
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 )
222 #ifdef HAVE_LDAP
223 ber_free( berelement, buf );
224 #endif
228 /***********************************************************************
229 * ber_init (WLDAP32.@)
231 * Initialise a berelement structure from a berval structure.
233 * PARAMS
234 * berval [I] Pointer to a berval structure.
236 * RETURNS
237 * Success: Pointer to a berelement structure.
238 * Failure: NULL
240 * NOTES
241 * Call ber_free to free the returned berelement structure.
243 WLDAP32_BerElement * CDECL WLDAP32_ber_init( BERVAL *berval )
245 #ifdef HAVE_LDAP
246 struct berval *bervalU;
247 WLDAP32_BerElement *ret;
249 if (!(bervalU = bervalWtoU( berval ))) return NULL;
250 ret = ber_init( bervalU );
251 heap_free( bervalU );
252 return ret;
253 #else
254 return NULL;
255 #endif
259 /***********************************************************************
260 * ber_next_element (WLDAP32.@)
262 * Return the tag of the next element in a set or sequence.
264 * PARAMS
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.
269 * RETURNS
270 * Success: Tag of the next element.
271 * Failure: LBER_DEFAULT (no more data).
273 * NOTES
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 )
279 #ifdef HAVE_LDAP
280 ber_len_t len;
281 ber_tag_t ret;
283 if ((ret = ber_next_element( berelement, &len, opaque )) != LBER_ERROR)
285 if (len > ~0u)
287 ERR( "len too large\n" );
288 return WLDAP32_LBER_ERROR;
290 *ret_len = len;
292 return ret;
294 #else
295 return WLDAP32_LBER_ERROR;
296 #endif
300 /***********************************************************************
301 * ber_peek_tag (WLDAP32.@)
303 * Return the tag of the next element.
305 * PARAMS
306 * berelement [I] Pointer to a berelement structure.
307 * len [O] Receives the length of the next element.
309 * RETURNS
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 )
315 #ifdef HAVE_LDAP
316 ber_len_t len;
317 ber_tag_t ret;
319 if ((ret = ber_peek_tag( berelement, &len )) != LBER_ERROR)
321 if (len > ~0u)
323 ERR( "len too large\n" );
324 return WLDAP32_LBER_ERROR;
326 *ret_len = len;
328 return ret;
330 #else
331 return WLDAP32_LBER_ERROR;
332 #endif
336 /***********************************************************************
337 * ber_skip_tag (WLDAP32.@)
339 * Skip the current tag and return the tag of the next element.
341 * PARAMS
342 * berelement [I] Pointer to a berelement structure.
343 * len [O] Receives the length of the skipped element.
345 * RETURNS
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 )
351 #ifdef HAVE_LDAP
352 ber_len_t len;
353 ber_tag_t ret;
355 if ((ret = ber_skip_tag( berelement, &len )) != LBER_ERROR)
357 if (len > ~0u)
359 ERR( "len too large\n" );
360 return WLDAP32_LBER_ERROR;
362 *ret_len = len;
364 return ret;
366 #else
367 return WLDAP32_LBER_ERROR;
368 #endif
372 /***********************************************************************
373 * ber_printf (WLDAP32.@)
375 * Encode a berelement structure.
377 * PARAMS
378 * berelement [I/O] Pointer to a berelement structure.
379 * fmt [I] Format string.
380 * ... [I] Values to encode.
382 * RETURNS
383 * Success: Non-negative number.
384 * Failure: LBER_ERROR
386 * NOTES
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, ... )
392 #ifdef HAVE_LDAP
393 __ms_va_list list;
394 int ret = 0;
395 char new_fmt[2];
397 new_fmt[1] = 0;
398 __ms_va_start( list, fmt );
399 while (*fmt)
401 new_fmt[0] = *fmt++;
402 switch(new_fmt[0])
404 case 'b':
405 case 'e':
406 case 'i':
408 int i = va_arg( list, int );
409 ret = ber_printf( berelement, new_fmt, i );
410 break;
412 case 'o':
413 case 's':
415 char *str = va_arg( list, char * );
416 ret = ber_printf( berelement, new_fmt, str );
417 break;
419 case 't':
421 unsigned int tag = va_arg( list, unsigned int );
422 ret = ber_printf( berelement, new_fmt, tag );
423 break;
425 case 'v':
427 char **array = va_arg( list, char ** );
428 ret = ber_printf( berelement, new_fmt, array );
429 break;
431 case 'V':
433 struct WLDAP32_berval **array = va_arg( list, struct WLDAP32_berval ** );
434 struct berval **arrayU;
435 if (!(arrayU = bvarrayWtoU( array )))
437 ret = -1;
438 break;
440 ret = ber_printf( berelement, new_fmt, arrayU );
441 bvarrayfreeU( arrayU );
442 break;
444 case 'X':
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 );
450 break;
452 case 'n':
453 case '{':
454 case '}':
455 case '[':
456 case ']':
457 ret = ber_printf( berelement, new_fmt );
458 break;
459 default:
460 FIXME( "Unknown format '%c'\n", new_fmt[0] );
461 ret = -1;
462 break;
464 if (ret == -1) break;
466 __ms_va_end( list );
467 return ret;
468 #else
469 return WLDAP32_LBER_ERROR;
470 #endif
474 /***********************************************************************
475 * ber_scanf (WLDAP32.@)
477 * Decode a berelement structure.
479 * PARAMS
480 * berelement [I/O] Pointer to a berelement structure.
481 * fmt [I] Format string.
482 * ... [I] Pointers to values to be decoded.
484 * RETURNS
485 * Success: Non-negative number.
486 * Failure: LBER_ERROR
488 * NOTES
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, ... )
494 #ifdef HAVE_LDAP
495 __ms_va_list list;
496 int ret = 0;
497 char new_fmt[2];
499 new_fmt[1] = 0;
500 __ms_va_start( list, fmt );
501 while (*fmt)
503 new_fmt[0] = *fmt++;
504 switch(new_fmt[0])
506 case 'a':
508 char **ptr = va_arg( list, char ** );
509 ret = ber_scanf( berelement, new_fmt, ptr );
510 break;
512 case 'b':
513 case 'e':
514 case 'i':
516 int *i = va_arg( list, int * );
517 ret = ber_scanf( berelement, new_fmt, i );
518 break;
520 case 't':
522 unsigned int *tag = va_arg( list, unsigned int * );
523 ret = ber_scanf( berelement, new_fmt, tag );
524 break;
526 case 'v':
528 char ***array = va_arg( list, char *** );
529 ret = ber_scanf( berelement, new_fmt, array );
530 break;
532 case 'B':
534 char **str = va_arg( list, char ** );
535 int *len = va_arg( list, int * );
536 ret = ber_scanf( berelement, new_fmt, str, len );
537 break;
539 case 'O':
541 struct berval **ptr = va_arg( list, struct berval ** );
542 ret = ber_scanf( berelement, new_fmt, ptr );
543 break;
545 case 'V':
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;
551 else ret = -1;
552 bvarrayfreeU( arrayU );
553 break;
555 case 'n':
556 case 'x':
557 case '{':
558 case '}':
559 case '[':
560 case ']':
561 ret = ber_scanf( berelement, new_fmt );
562 break;
563 default:
564 FIXME( "Unknown format '%c'\n", new_fmt[0] );
565 ret = -1;
566 break;
568 if (ret == -1) break;
570 __ms_va_end( list );
571 return ret;
572 #else
573 return WLDAP32_LBER_ERROR;
574 #endif