wined3d: UBYTE4 data shouldn't be normalized.
[wine/hacks.git] / dlls / wldap32 / misc.c
blob9d938c89c1ce38893c33fd4342bfc9ee354739d2
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%08x)\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%08x, %p, 0x%08x)\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%08x, %p, 0x%08x)\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 #else
379 return NULL;
380 #endif
383 /***********************************************************************
384 * ldap_first_reference (WLDAP32.@)
386 * Get the first reference from a result message.
388 * PARAMS
389 * ld [I] Pointer to an LDAP context.
390 * res [I] Search result message.
392 * RETURNS
393 * Success: The first reference.
394 * Failure: NULL
396 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
398 #ifdef HAVE_LDAP_FIRST_REFERENCE
400 TRACE( "(%p, %p)\n", ld, res );
402 if (!ld) return NULL;
403 return ldap_first_reference( ld, res );
405 #else
406 return NULL;
407 #endif
410 /***********************************************************************
411 * ldap_memfreeA (WLDAP32.@)
413 * See ldap_memfreeW.
415 void CDECL ldap_memfreeA( PCHAR block )
417 TRACE( "(%p)\n", block );
418 strfreeA( block );
421 /***********************************************************************
422 * ldap_memfreeW (WLDAP32.@)
424 * Free a block of memory.
426 * PARAMS
427 * block [I] Pointer to memory block to be freed.
429 void CDECL ldap_memfreeW( PWCHAR block )
431 TRACE( "(%p)\n", block );
432 strfreeW( block );
435 /***********************************************************************
436 * ldap_msgfree (WLDAP32.@)
438 * Free a message.
440 * PARAMS
441 * res [I] Message to be freed.
443 ULONG CDECL WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage *res )
445 ULONG ret = LDAP_SUCCESS;
446 #ifdef HAVE_LDAP
448 TRACE( "(%p)\n", res );
449 ldap_msgfree( res );
451 #endif
452 return ret;
455 /***********************************************************************
456 * ldap_next_attributeA (WLDAP32.@)
458 * See ldap_next_attributeW.
460 PCHAR CDECL ldap_next_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
461 WLDAP32_BerElement *ptr )
463 PCHAR ret = NULL;
464 #ifdef HAVE_LDAP
465 WCHAR *retW;
467 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
469 if (!ld || !entry || !ptr) return NULL;
470 retW = ldap_next_attributeW( ld, entry, ptr );
472 ret = strWtoA( retW );
473 ldap_memfreeW( retW );
475 #endif
476 return ret;
479 /***********************************************************************
480 * ldap_next_attributeW (WLDAP32.@)
482 * Get the next attribute for a given entry.
484 * PARAMS
485 * ld [I] Pointer to an LDAP context.
486 * entry [I] Entry to retrieve attribute for.
487 * ptr [I/O] Position pointer.
489 * RETURNS
490 * Success: The name of the next attribute.
491 * Failure: NULL
493 * NOTES
494 * Free the returned string after each iteration with ldap_memfree.
495 * When done iterating and when ptr != NULL, call ber_free( ptr, 0 ).
497 PWCHAR CDECL ldap_next_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
498 WLDAP32_BerElement *ptr )
500 PWCHAR ret = NULL;
501 #ifdef HAVE_LDAP
502 char *retU;
504 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
506 if (!ld || !entry || !ptr) return NULL;
507 retU = ldap_next_attribute( ld, entry, ptr );
509 ret = strUtoW( retU );
510 ldap_memfree( retU );
512 #endif
513 return ret;
516 /***********************************************************************
517 * ldap_next_entry (WLDAP32.@)
519 * Get the next entry from a result message.
521 * PARAMS
522 * ld [I] Pointer to an LDAP context.
523 * entry [I] Entry returned by a previous call.
525 * RETURNS
526 * Success: The next entry.
527 * Failure: NULL
529 * NOTES
530 * The returned entry will be freed when the message is freed.
532 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
534 #ifdef HAVE_LDAP
536 TRACE( "(%p, %p)\n", ld, entry );
538 if (!ld || !entry) return NULL;
539 return ldap_next_entry( ld, entry );
541 #else
542 return NULL;
543 #endif
546 /***********************************************************************
547 * ldap_next_reference (WLDAP32.@)
549 * Get the next reference from a result message.
551 * PARAMS
552 * ld [I] Pointer to an LDAP context.
553 * entry [I] Entry returned by a previous call.
555 * RETURNS
556 * Success: The next reference.
557 * Failure: NULL
559 * NOTES
560 * The returned entry will be freed when the message is freed.
562 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
564 #ifdef HAVE_LDAP_NEXT_REFERENCE
566 TRACE( "(%p, %p)\n", ld, entry );
568 if (!ld || !entry) return NULL;
569 return ldap_next_reference( ld, entry );
571 #else
572 return NULL;
573 #endif
576 /***********************************************************************
577 * ldap_result (WLDAP32.@)
579 * Get the result of an asynchronous operation.
581 * PARAMS
582 * ld [I] Pointer to an LDAP context.
583 * msgid [I] Message ID of the operation.
584 * all [I] How many results should be returned?
585 * timeout [I] How long to wait for the results?
586 * res [O] Result message for the operation.
588 * RETURNS
589 * Success: One of the following values:
591 * LDAP_RES_ADD
592 * LDAP_RES_BIND
593 * LDAP_RES_COMPARE
594 * LDAP_RES_DELETE
595 * LDAP_RES_EXTENDED
596 * LDAP_RES_MODIFY
597 * LDAP_RES_MODRDN
598 * LDAP_RES_REFERRAL
599 * LDAP_RES_SEARCH_ENTRY
600 * LDAP_RES_SEARCH_RESULT
602 * Failure: ~0UL
604 * This function returns 0 when the timeout has expired.
606 * NOTES
607 * A NULL timeout pointer causes the function to block waiting
608 * for results to arrive. A timeout value of 0 causes the function
609 * to immediately return any available results. Free returned results
610 * with ldap_msgfree.
612 ULONG CDECL WLDAP32_ldap_result( WLDAP32_LDAP *ld, ULONG msgid, ULONG all,
613 struct l_timeval *timeout, WLDAP32_LDAPMessage **res )
615 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
616 #ifdef HAVE_LDAP
618 TRACE( "(%p, 0x%08x, 0x%08x, %p, %p)\n", ld, msgid, all, timeout, res );
620 if (!ld || !res || msgid == ~0UL) return ~0UL;
621 ret = ldap_result( ld, msgid, all, (struct timeval *)timeout, res );
623 #endif
624 return ret;
627 /***********************************************************************
628 * LdapUnicodeToUTF8 (WLDAP32.@)
630 * Convert a wide character string to a UTF8 string.
632 * PARAMS
633 * src [I] Wide character string to convert.
634 * srclen [I] Size of string to convert, in characters.
635 * dst [O] Pointer to a buffer that receives the converted string.
636 * dstlen [I] Size of the destination buffer in characters.
638 * RETURNS
639 * The number of characters written into the destination buffer.
641 * NOTES
642 * Set dstlen to zero to ask for the required buffer size.
644 int CDECL LdapUnicodeToUTF8( LPCWSTR src, int srclen, LPSTR dst, int dstlen )
646 return WideCharToMultiByte( CP_UTF8, 0, src, srclen, dst, dstlen, NULL, NULL );
649 /***********************************************************************
650 * LdapUTF8ToUnicode (WLDAP32.@)
652 * Convert a UTF8 string to a wide character string.
654 * PARAMS
655 * src [I] UTF8 string to convert.
656 * srclen [I] Size of string to convert, in characters.
657 * dst [O] Pointer to a buffer that receives the converted string.
658 * dstlen [I] Size of the destination buffer in characters.
660 * RETURNS
661 * The number of characters written into the destination buffer.
663 * NOTES
664 * Set dstlen to zero to ask for the required buffer size.
666 int CDECL LdapUTF8ToUnicode( LPCSTR src, int srclen, LPWSTR dst, int dstlen )
668 return MultiByteToWideChar( CP_UTF8, 0, src, srclen, dst, dstlen );