mfmediaengine: Remove vertical flipping of video frames.
[wine.git] / dlls / wldap32 / ber.c
blobf3b18e530b4c744408712d37be54d74bcd97e8cb
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 <stdlib.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnls.h"
26 #include "winldap.h"
27 #include "winber.h"
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.
39 * PARAMS
40 * options [I] Must be LBER_USE_DER.
42 * RETURNS
43 * Success: Pointer to an allocated berelement structure.
44 * Failure: NULL
46 * NOTES
47 * Free the berelement structure with ber_free.
49 BerElement * CDECL ber_alloc_t( int options )
51 BerElement *ret;
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, &params ))
59 free( ret );
60 return NULL;
62 return ret;
66 /***********************************************************************
67 * ber_bvdup (WLDAP32.@)
69 * Copy a berval structure.
71 * PARAMS
72 * berval [I] Pointer to the berval structure to be copied.
74 * RETURNS
75 * Success: Pointer to a copy of the berval structure.
76 * Failure: NULL
78 * NOTES
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.
92 * PARAMS
93 * berval [I] Pointer to an array of berval structures.
95 * RETURNS
96 * Nothing.
98 * NOTES
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.
113 * PARAMS
114 * berval [I] Pointer to a berval structure.
116 * RETURNS
117 * Nothing.
119 * NOTES
120 * Use this function only to free berval structures allocated by
121 * an LDAP API.
123 void CDECL ber_bvfree( BERVAL *berval )
125 free( berval );
129 /***********************************************************************
130 * ber_first_element (WLDAP32.@)
132 * Return the tag of the first element in a set or sequence.
134 * PARAMS
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.
139 * RETURNS
140 * Success: Tag of the first element.
141 * Failure: LBER_DEFAULT (no more data).
143 * NOTES
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, &params );
153 /***********************************************************************
154 * ber_flatten (WLDAP32.@)
156 * Flatten a berelement structure into a berval structure.
158 * PARAMS
159 * berelement [I] Pointer to a berelement structure.
160 * berval [O] Pointer to a berval structure.
162 * RETURNS
163 * Success: 0
164 * Failure: LBER_ERROR
166 * NOTES
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, &params )) return LBER_ERROR;
177 if (!(bervalW = bervalUtoW( bervalU ))) return LBER_ERROR;
178 LDAP_CALL( ber_bvfree, bervalU );
179 if (!bervalW) return LBER_ERROR;
180 *berval = bervalW;
181 return 0;
185 /***********************************************************************
186 * ber_free (WLDAP32.@)
188 * Free a berelement structure.
190 * PARAMS
191 * berelement [I] Pointer to the berelement structure to be freed.
192 * buf [I] Flag.
194 * RETURNS
195 * Nothing.
197 * NOTES
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, &params );
205 free( ber );
209 /***********************************************************************
210 * ber_init (WLDAP32.@)
212 * Initialise a berelement structure from a berval structure.
214 * PARAMS
215 * berval [I] Pointer to a berval structure.
217 * RETURNS
218 * Success: Pointer to a berelement structure.
219 * Failure: NULL
221 * NOTES
222 * Call ber_free to free the returned berelement structure.
224 BerElement * CDECL ber_init( BERVAL *berval )
226 struct bervalU *bervalU;
227 BerElement *ret;
228 struct ber_init_params params;
230 if (!(ret = malloc( sizeof(*ret) ))) return NULL;
231 if (!(bervalU = bervalWtoU( berval )))
233 free( ret );
234 return NULL;
236 params.berval = bervalU;
237 params.ret = (void **)&BER(ret);
238 if (LDAP_CALL( ber_init, &params ))
240 free( ret );
241 ret = NULL;
243 free( bervalU );
244 return ret;
248 /***********************************************************************
249 * ber_next_element (WLDAP32.@)
251 * Return the tag of the next element in a set or sequence.
253 * PARAMS
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.
258 * RETURNS
259 * Success: Tag of the next element.
260 * Failure: LBER_DEFAULT (no more data).
262 * NOTES
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, &params );
273 /***********************************************************************
274 * ber_peek_tag (WLDAP32.@)
276 * Return the tag of the next element.
278 * PARAMS
279 * berelement [I] Pointer to a berelement structure.
280 * len [O] Receives the length of the next element.
282 * RETURNS
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, &params );
293 /***********************************************************************
294 * ber_skip_tag (WLDAP32.@)
296 * Skip the current tag and return the tag of the next element.
298 * PARAMS
299 * berelement [I] Pointer to a berelement structure.
300 * len [O] Receives the length of the skipped element.
302 * RETURNS
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, &params );
313 /***********************************************************************
314 * ber_printf (WLDAP32.@)
316 * Encode a berelement structure.
318 * PARAMS
319 * berelement [I/O] Pointer to a berelement structure.
320 * fmt [I] Format string.
321 * ... [I] Values to encode.
323 * RETURNS
324 * Success: Non-negative number.
325 * Failure: LBER_ERROR
327 * NOTES
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, ... )
333 va_list list;
334 int ret = 0;
335 char new_fmt[2];
337 new_fmt[1] = 0;
338 va_start( list, fmt );
339 while (*fmt)
341 struct ber_printf_params params = { BER(ber), new_fmt };
342 new_fmt[0] = *fmt++;
343 switch (new_fmt[0])
345 case 'b':
346 case 'e':
347 case 'i':
348 params.arg1 = va_arg( list, int );
349 ret = LDAP_CALL( ber_printf, &params );
350 break;
351 case 'o':
352 case 's':
353 params.arg1 = (ULONG_PTR)va_arg( list, char * );
354 ret = LDAP_CALL( ber_printf, &params );
355 break;
356 case 't':
357 params.arg1 = va_arg( list, unsigned int );
358 ret = LDAP_CALL( ber_printf, &params );
359 break;
360 case 'v':
361 params.arg1 = (ULONG_PTR)va_arg( list, char ** );
362 ret = LDAP_CALL( ber_printf, &params );
363 break;
364 case 'V':
366 struct berval **array = va_arg( list, struct berval ** );
367 struct bervalU **arrayU;
368 if (!(arrayU = bvarrayWtoU( array )))
370 ret = -1;
371 break;
373 params.arg1 = (ULONG_PTR)arrayU;
374 ret = LDAP_CALL( ber_printf, &params );
375 bvarrayfreeU( arrayU );
376 break;
378 case 'X':
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, &params );
384 break;
386 case 'n':
387 case '{':
388 case '}':
389 case '[':
390 case ']':
391 ret = LDAP_CALL( ber_printf, &params );
392 break;
394 default:
395 FIXME( "Unknown format '%c'\n", new_fmt[0] );
396 ret = -1;
397 break;
399 if (ret == -1) break;
401 va_end( list );
402 return ret;
406 /***********************************************************************
407 * ber_scanf (WLDAP32.@)
409 * Decode a berelement structure.
411 * PARAMS
412 * berelement [I/O] Pointer to a berelement structure.
413 * fmt [I] Format string.
414 * ... [I] Pointers to values to be decoded.
416 * RETURNS
417 * Success: Non-negative number.
418 * Failure: LBER_ERROR
420 * NOTES
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, ... )
426 va_list list;
427 int ret = 0;
428 char new_fmt[2];
430 new_fmt[1] = 0;
431 va_start( list, fmt );
432 while (*fmt)
434 struct ber_scanf_params params = { BER(ber), new_fmt };
435 new_fmt[0] = *fmt++;
436 switch (new_fmt[0])
438 case 'a':
440 char *str, **ptr = va_arg( list, char ** );
441 params.arg1 = &str;
442 if ((ret = LDAP_CALL( ber_scanf, &params )) == -1) break;
443 *ptr = strdupU( str );
444 LDAP_CALL( ldap_memfree, str );
445 break;
447 case 'b':
448 case 'e':
449 case 'i':
450 params.arg1 = va_arg( list, int * );
451 ret = LDAP_CALL( ber_scanf, &params );
452 break;
453 case 't':
454 params.arg1 = va_arg( list, unsigned int * );
455 ret = LDAP_CALL( ber_scanf, &params );
456 break;
457 case 'v':
459 char *str, **arrayU, **ptr, ***array = va_arg( list, char *** );
460 params.arg1 = &arrayU;
461 if ((ret = LDAP_CALL( ber_scanf, &params )) == -1) break;
462 *array = strarrayUtoU( arrayU );
463 ptr = arrayU;
464 while ((str = *ptr))
466 LDAP_CALL( ldap_memfree, str );
467 ptr++;
469 LDAP_CALL( ldap_memfree, arrayU );
470 break;
472 case 'B':
474 char *strU, **str = va_arg( list, char ** );
475 int *len = va_arg( list, int * );
476 params.arg1 = &strU;
477 params.arg2 = len;
478 if ((ret = LDAP_CALL( ber_scanf, &params )) == -1) break;
479 *str = malloc( *len );
480 memcpy( *str, strU, *len );
481 LDAP_CALL( ldap_memfree, strU );
482 break;
484 case 'O':
486 struct berval **berval = va_arg( list, struct berval ** );
487 struct bervalU *bervalU;
488 params.arg1 = &bervalU;
489 if ((ret = LDAP_CALL( ber_scanf, &params )) == -1) break;
490 *berval = bervalUtoW( bervalU );
491 LDAP_CALL( ber_bvfree, bervalU );
492 break;
494 case 'V':
496 struct berval ***array = va_arg( list, struct berval *** );
497 struct bervalU **arrayU;
498 params.arg1 = &arrayU;
499 if ((ret = LDAP_CALL( ber_scanf, &params )) == -1) break;
500 *array = bvarrayUtoW( arrayU );
501 LDAP_CALL( ber_bvecfree, arrayU );
502 break;
504 case 'n':
505 case 'x':
506 case '{':
507 case '}':
508 case '[':
509 case ']':
510 ret = LDAP_CALL( ber_scanf, &params );
511 break;
513 default:
514 FIXME( "Unknown format '%c'\n", new_fmt[0] );
515 ret = -1;
516 break;
518 if (ret == -1) break;
520 va_end( list );
521 return ret;