d3d: Don't crash when SetRenderTarget() is called with a NULL pSurface.
[wine/hacks.git] / dlls / wldap32 / misc.c
blob04df65901f1e1166866a194affdd434814aff6db
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 "wine/port.h"
24 #include "wine/debug.h"
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winnls.h"
33 #ifdef HAVE_LDAP_H
34 #include <ldap.h>
35 #else
36 #define LDAP_SUCCESS 0x00
37 #define LDAP_NOT_SUPPORTED 0x5c
38 #endif
40 #include "winldap_private.h"
41 #include "wldap32.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
45 /***********************************************************************
46 * ldap_abandon (WLDAP32.@)
48 * Cancel an asynchronous operation.
50 * PARAMS
51 * ld [I] Pointer to an LDAP context.
52 * msgid [I] ID of the operation to cancel.
54 * RETURNS
55 * Success: LDAP_SUCCESS
56 * Failure: An LDAP error code.
58 ULONG CDECL WLDAP32_ldap_abandon( WLDAP32_LDAP *ld, ULONG msgid )
60 ULONG ret = LDAP_NOT_SUPPORTED;
61 #ifdef HAVE_LDAP
63 TRACE( "(%p, 0x%08lx)\n", ld, msgid );
65 if (!ld) return ~0UL;
66 ret = ldap_abandon_ext( ld, msgid, NULL, NULL );
68 #endif
69 return ret;
72 /***********************************************************************
73 * ldap_check_filterA (WLDAP32.@)
75 * See ldap_check_filterW.
77 ULONG CDECL ldap_check_filterA( WLDAP32_LDAP *ld, PCHAR filter )
79 ULONG ret;
80 WCHAR *filterW = NULL;
82 TRACE( "(%p, %s)\n", ld, debugstr_a(filter) );
84 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
86 if (filter) {
87 filterW = strAtoW( filter );
88 if (!filterW) return WLDAP32_LDAP_NO_MEMORY;
91 ret = ldap_check_filterW( ld, filterW );
93 strfreeW( filterW );
94 return ret;
97 /***********************************************************************
98 * ldap_check_filterW (WLDAP32.@)
100 * Check filter syntax.
102 * PARAMS
103 * ld [I] Pointer to an LDAP context.
104 * filter [I] Filter string.
106 * RETURNS
107 * Success: LDAP_SUCCESS
108 * Failure: An LDAP error code.
110 ULONG CDECL ldap_check_filterW( WLDAP32_LDAP *ld, PWCHAR filter )
112 TRACE( "(%p, %s)\n", ld, debugstr_w(filter) );
114 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
115 return LDAP_SUCCESS; /* FIXME: do some checks */
118 /***********************************************************************
119 * ldap_cleanup (WLDAP32.@)
121 ULONG CDECL ldap_cleanup( HANDLE instance )
123 TRACE( "(%p)\n", instance );
124 return LDAP_SUCCESS;
127 /***********************************************************************
128 * ldap_conn_from_msg (WLDAP32.@)
130 * Get the LDAP context for a given message.
132 * PARAMS
133 * ld [I] Pointer to an LDAP context.
134 * res [I] LDAP message.
136 * RETURNS
137 * Success: Pointer to an LDAP context.
138 * Failure: NULL
140 WLDAP32_LDAP * CDECL ldap_conn_from_msg( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
142 TRACE( "(%p, %p)\n", ld, res );
144 if (!ld || !res) return NULL;
145 return ld; /* FIXME: not always correct */
148 /***********************************************************************
149 * ldap_count_entries (WLDAP32.@)
151 * Count the number of entries returned from a search.
153 * PARAMS
154 * ld [I] Pointer to an LDAP context.
155 * res [I] LDAP message.
157 * RETURNS
158 * Success: The number of entries.
159 * Failure: ~0UL
161 ULONG CDECL WLDAP32_ldap_count_entries( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
163 ULONG ret = LDAP_NOT_SUPPORTED;
164 #ifdef HAVE_LDAP
166 TRACE( "(%p, %p)\n", ld, res );
168 if (!ld) return ~0UL;
169 ret = ldap_count_entries( ld, res );
171 #endif
172 return ret;
175 /***********************************************************************
176 * ldap_count_references (WLDAP32.@)
178 * Count the number of references returned from a search.
180 * PARAMS
181 * ld [I] Pointer to an LDAP context.
182 * res [I] LDAP message.
184 * RETURNS
185 * Success: The number of references.
186 * Failure: ~0UL
188 ULONG CDECL WLDAP32_ldap_count_references( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
190 ULONG ret = LDAP_NOT_SUPPORTED;
191 #ifdef HAVE_LDAP_COUNT_REFERENCES
193 TRACE( "(%p, %p)\n", ld, res );
195 if (!ld) return 0;
196 ret = ldap_count_references( ld, res );
198 #endif
199 return ret;
202 static ULONG get_escape_size( PCHAR src, ULONG srclen )
204 ULONG i, size = 0;
206 if (src)
208 for (i = 0; i < srclen; i++)
210 if ((src[i] >= '0' && src[i] <= '9') ||
211 (src[i] >= 'A' && src[i] <= 'Z') ||
212 (src[i] >= 'a' && src[i] <= 'z'))
213 size++;
214 else
215 size += 3;
218 return size + 1;
221 static void escape_filter_element( PCHAR src, ULONG srclen, PCHAR dst )
223 ULONG i;
224 static const char fmt[] = "\\%02X";
225 char *d = dst;
227 for (i = 0; i < srclen; i++)
229 if ((src[i] >= '0' && src[i] <= '9') ||
230 (src[i] >= 'A' && src[i] <= 'Z') ||
231 (src[i] >= 'a' && src[i] <= 'z'))
232 *d++ = src[i];
233 else
235 sprintf( d, fmt, (unsigned char)src[i] );
236 d += 3;
239 *++d = 0;
242 /***********************************************************************
243 * ldap_escape_filter_elementA (WLDAP32.@)
245 * See ldap_escape_filter_elementW.
247 ULONG CDECL ldap_escape_filter_elementA( PCHAR src, ULONG srclen, PCHAR dst, ULONG dstlen )
249 ULONG len;
251 TRACE( "(%p, 0x%08lx, %p, 0x%08lx)\n", src, srclen, dst, dstlen );
253 len = get_escape_size( src, srclen );
254 if (!dst) return len;
256 if (!src || dstlen < len)
257 return WLDAP32_LDAP_PARAM_ERROR;
258 else
260 escape_filter_element( src, srclen, dst );
261 return LDAP_SUCCESS;
265 /***********************************************************************
266 * ldap_escape_filter_elementW (WLDAP32.@)
268 * Escape binary data for safe passing in filters.
270 * PARAMS
271 * src [I] Filter element to be escaped.
272 * srclen [I] Length in bytes of the filter element.
273 * dst [O] Destination buffer for the escaped filter element.
274 * dstlen [I] Length in bytes of the destination buffer.
276 * RETURNS
277 * Success: LDAP_SUCCESS
278 * Failure: An LDAP error code.
280 ULONG CDECL ldap_escape_filter_elementW( PCHAR src, ULONG srclen, PWCHAR dst, ULONG dstlen )
282 ULONG len;
284 TRACE( "(%p, 0x%08lx, %p, 0x%08lx)\n", src, srclen, dst, dstlen );
286 len = get_escape_size( src, srclen );
287 if (!dst) return len;
289 /* no matter what you throw at it, this is what native returns */
290 return WLDAP32_LDAP_PARAM_ERROR;
293 /***********************************************************************
294 * ldap_first_attributeA (WLDAP32.@)
296 * See ldap_first_attributeW.
298 PCHAR CDECL ldap_first_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
299 WLDAP32_BerElement** ptr )
301 PCHAR ret = NULL;
302 #ifdef HAVE_LDAP
303 WCHAR *retW;
305 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
307 if (!ld || !entry) return NULL;
308 retW = ldap_first_attributeW( ld, entry, ptr );
310 ret = strWtoA( retW );
311 ldap_memfreeW( retW );
313 #endif
314 return ret;
317 /***********************************************************************
318 * ldap_first_attributeW (WLDAP32.@)
320 * Get the first attribute for a given entry.
322 * PARAMS
323 * ld [I] Pointer to an LDAP context.
324 * entry [I] Entry to retrieve attribute for.
325 * ptr [O] Position pointer.
327 * RETURNS
328 * Success: Name of the first attribute.
329 * Failure: NULL
331 * NOTES
332 * Use ldap_memfree to free the returned string.
334 PWCHAR CDECL ldap_first_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
335 WLDAP32_BerElement** ptr )
337 PWCHAR ret = NULL;
338 #ifdef HAVE_LDAP
339 char *retU;
341 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
343 if (!ld || !entry) return NULL;
344 retU = ldap_first_attribute( ld, entry, ptr );
346 ret = strUtoW( retU );
347 ldap_memfree( retU );
349 #endif
350 return ret;
353 /***********************************************************************
354 * ldap_first_entry (WLDAP32.@)
356 * Get the first entry from a result message.
358 * PARAMS
359 * ld [I] Pointer to an LDAP context.
360 * res [I] Search result message.
362 * RETURNS
363 * Success: The first entry.
364 * Failure: NULL
366 * NOTES
367 * The returned entry will be freed when the message is freed.
369 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
371 #ifdef HAVE_LDAP
373 TRACE( "(%p, %p)\n", ld, res );
375 if (!ld || !res) return NULL;
376 return ldap_first_entry( ld, res );
378 #endif
379 return NULL;
382 /***********************************************************************
383 * ldap_first_reference (WLDAP32.@)
385 * Get the first reference from a result message.
387 * PARAMS
388 * ld [I] Pointer to an LDAP context.
389 * res [I] Search result message.
391 * RETURNS
392 * Success: The first reference.
393 * Failure: NULL
395 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
397 #ifdef HAVE_LDAP_FIRST_REFERENCE
399 TRACE( "(%p, %p)\n", ld, res );
401 if (!ld) return NULL;
402 return ldap_first_reference( ld, res );
404 #endif
405 return NULL;
408 /***********************************************************************
409 * ldap_memfreeA (WLDAP32.@)
411 * See ldap_memfreeW.
413 void CDECL ldap_memfreeA( PCHAR block )
415 TRACE( "(%p)\n", block );
416 strfreeA( block );
419 /***********************************************************************
420 * ldap_memfreeW (WLDAP32.@)
422 * Free a block of memory.
424 * PARAMS
425 * block [I] Pointer to memory block to be freed.
427 void CDECL ldap_memfreeW( PWCHAR block )
429 TRACE( "(%p)\n", block );
430 strfreeW( block );
433 /***********************************************************************
434 * ldap_msgfree (WLDAP32.@)
436 * Free a message.
438 * PARAMS
439 * res [I] Message to be freed.
441 ULONG CDECL WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage *res )
443 ULONG ret = LDAP_SUCCESS;
444 #ifdef HAVE_LDAP
446 TRACE( "(%p)\n", res );
447 ldap_msgfree( res );
449 #endif
450 return ret;
453 /***********************************************************************
454 * ldap_next_attributeA (WLDAP32.@)
456 * See ldap_next_attributeW.
458 PCHAR CDECL ldap_next_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
459 WLDAP32_BerElement *ptr )
461 PCHAR ret = NULL;
462 #ifdef HAVE_LDAP
463 WCHAR *retW;
465 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
467 if (!ld || !entry || !ptr) return NULL;
468 retW = ldap_next_attributeW( ld, entry, ptr );
470 ret = strWtoA( retW );
471 ldap_memfreeW( retW );
473 #endif
474 return ret;
477 /***********************************************************************
478 * ldap_next_attributeW (WLDAP32.@)
480 * Get the next attribute for a given entry.
482 * PARAMS
483 * ld [I] Pointer to an LDAP context.
484 * entry [I] Entry to retrieve attribute for.
485 * ptr [I/O] Position pointer.
487 * RETURNS
488 * Success: The name of the next attribute.
489 * Failure: NULL
491 * NOTES
492 * Free the returned string after each iteration with ldap_memfree.
493 * When done iterating and when ptr != NULL, call ber_free( ptr, 0 ).
495 PWCHAR CDECL ldap_next_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
496 WLDAP32_BerElement *ptr )
498 PWCHAR ret = NULL;
499 #ifdef HAVE_LDAP
500 char *retU;
502 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
504 if (!ld || !entry || !ptr) return NULL;
505 retU = ldap_next_attribute( ld, entry, ptr );
507 ret = strUtoW( retU );
508 ldap_memfree( retU );
510 #endif
511 return ret;
514 /***********************************************************************
515 * ldap_next_entry (WLDAP32.@)
517 * Get the next entry from a result message.
519 * PARAMS
520 * ld [I] Pointer to an LDAP context.
521 * entry [I] Entry returned by a previous call.
523 * RETURNS
524 * Success: The next entry.
525 * Failure: NULL
527 * NOTES
528 * The returned entry will be freed when the message is freed.
530 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
532 #ifdef HAVE_LDAP
534 TRACE( "(%p, %p)\n", ld, entry );
536 if (!ld || !entry) return NULL;
537 return ldap_next_entry( ld, entry );
539 #endif
540 return NULL;
543 /***********************************************************************
544 * ldap_next_reference (WLDAP32.@)
546 * Get the next reference from a result message.
548 * PARAMS
549 * ld [I] Pointer to an LDAP context.
550 * entry [I] Entry returned by a previous call.
552 * RETURNS
553 * Success: The next reference.
554 * Failure: NULL
556 * NOTES
557 * The returned entry will be freed when the message is freed.
559 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
561 #ifdef HAVE_LDAP_NEXT_REFERENCE
563 TRACE( "(%p, %p)\n", ld, entry );
565 if (!ld || !entry) return NULL;
566 return ldap_next_reference( ld, entry );
568 #endif
569 return NULL;
572 /***********************************************************************
573 * ldap_result (WLDAP32.@)
575 * Get the result of an asynchronous operation.
577 * PARAMS
578 * ld [I] Pointer to an LDAP context.
579 * msgid [I] Message ID of the operation.
580 * all [I] How many results should be returned?
581 * timeout [I] How long to wait for the results?
582 * res [O] Result message for the operation.
584 * RETURNS
585 * Success: One of the following values:
587 * LDAP_RES_ADD
588 * LDAP_RES_BIND
589 * LDAP_RES_COMPARE
590 * LDAP_RES_DELETE
591 * LDAP_RES_EXTENDED
592 * LDAP_RES_MODIFY
593 * LDAP_RES_MODRDN
594 * LDAP_RES_REFERRAL
595 * LDAP_RES_SEARCH_ENTRY
596 * LDAP_RES_SEARCH_RESULT
598 * Failure: ~0UL
600 * This function returns 0 when the timeout has expired.
602 * NOTES
603 * A NULL timeout pointer causes the function to block waiting
604 * for results to arrive. A timeout value of 0 causes the function
605 * to immediately return any available results. Free returned results
606 * with ldap_msgfree.
608 ULONG CDECL WLDAP32_ldap_result( WLDAP32_LDAP *ld, ULONG msgid, ULONG all,
609 struct l_timeval *timeout, WLDAP32_LDAPMessage **res )
611 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
612 #ifdef HAVE_LDAP
614 TRACE( "(%p, 0x%08lx, 0x%08lx, %p, %p)\n", ld, msgid, all, timeout, res );
616 if (!ld || !res || msgid == ~0UL) return ~0UL;
617 ret = ldap_result( ld, msgid, all, (struct timeval *)timeout, res );
619 #endif
620 return ret;
623 /***********************************************************************
624 * LdapUnicodeToUTF8 (WLDAP32.@)
626 * Convert a wide character string to a UTF8 string.
628 * PARAMS
629 * src [I] Wide character string to convert.
630 * srclen [I] Size of string to convert, in characters.
631 * dst [O] Pointer to a buffer that receives the converted string.
632 * dstlen [I] Size of the destination buffer in characters.
634 * RETURNS
635 * The number of characters written into the destination buffer.
637 * NOTES
638 * Set dstlen to zero to ask for the required buffer size.
640 int CDECL LdapUnicodeToUTF8( LPCWSTR src, int srclen, LPSTR dst, int dstlen )
642 return WideCharToMultiByte( CP_UTF8, 0, src, srclen, dst, dstlen, NULL, NULL );
645 /***********************************************************************
646 * LdapUTF8ToUnicode (WLDAP32.@)
648 * Convert a UTF8 string to a wide character string.
650 * PARAMS
651 * src [I] UTF8 string to convert.
652 * srclen [I] Size of string to convert, in characters.
653 * dst [O] Pointer to a buffer that receives the converted string.
654 * dstlen [I] Size of the destination buffer in characters.
656 * RETURNS
657 * The number of characters written into the destination buffer.
659 * NOTES
660 * Set dstlen to zero to ask for the required buffer size.
662 int CDECL LdapUTF8ToUnicode( LPCSTR src, int srclen, LPWSTR dst, int dstlen )
664 return MultiByteToWideChar( CP_UTF8, 0, src, srclen, dst, dstlen );