wininet: Make sure to use return values (LLVM/Clang).
[wine.git] / dlls / wldap32 / misc.c
blobfa9ac7ac555d44eff73f4bda7e57aff76b995e9b
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 #endif
37 #include "winldap_private.h"
38 #include "wldap32.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
42 /***********************************************************************
43 * ldap_abandon (WLDAP32.@)
45 * Cancel an asynchronous operation.
47 * PARAMS
48 * ld [I] Pointer to an LDAP context.
49 * msgid [I] ID of the operation to cancel.
51 * RETURNS
52 * Success: LDAP_SUCCESS
53 * Failure: An LDAP error code.
55 ULONG CDECL WLDAP32_ldap_abandon( WLDAP32_LDAP *ld, ULONG msgid )
57 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
58 #ifdef HAVE_LDAP
60 TRACE( "(%p, 0x%08x)\n", ld, msgid );
62 if (!ld) return ~0u;
63 ret = map_error( ldap_abandon_ext( ld, msgid, NULL, NULL ));
65 #endif
66 return ret;
69 /***********************************************************************
70 * ldap_check_filterA (WLDAP32.@)
72 * See ldap_check_filterW.
74 ULONG CDECL ldap_check_filterA( WLDAP32_LDAP *ld, PCHAR filter )
76 ULONG ret;
77 WCHAR *filterW = NULL;
79 TRACE( "(%p, %s)\n", ld, debugstr_a(filter) );
81 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
83 if (filter) {
84 filterW = strAtoW( filter );
85 if (!filterW) return WLDAP32_LDAP_NO_MEMORY;
88 ret = ldap_check_filterW( ld, filterW );
90 strfreeW( filterW );
91 return ret;
94 /***********************************************************************
95 * ldap_check_filterW (WLDAP32.@)
97 * Check filter syntax.
99 * PARAMS
100 * ld [I] Pointer to an LDAP context.
101 * filter [I] Filter string.
103 * RETURNS
104 * Success: LDAP_SUCCESS
105 * Failure: An LDAP error code.
107 ULONG CDECL ldap_check_filterW( WLDAP32_LDAP *ld, PWCHAR filter )
109 TRACE( "(%p, %s)\n", ld, debugstr_w(filter) );
111 if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
112 return WLDAP32_LDAP_SUCCESS; /* FIXME: do some checks */
115 /***********************************************************************
116 * ldap_cleanup (WLDAP32.@)
118 ULONG CDECL ldap_cleanup( HANDLE instance )
120 TRACE( "(%p)\n", instance );
121 return WLDAP32_LDAP_SUCCESS;
124 /***********************************************************************
125 * ldap_conn_from_msg (WLDAP32.@)
127 * Get the LDAP context for a given message.
129 * PARAMS
130 * ld [I] Pointer to an LDAP context.
131 * res [I] LDAP message.
133 * RETURNS
134 * Success: Pointer to an LDAP context.
135 * Failure: NULL
137 WLDAP32_LDAP * CDECL ldap_conn_from_msg( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
139 TRACE( "(%p, %p)\n", ld, res );
141 if (!ld || !res) return NULL;
142 return ld; /* FIXME: not always correct */
145 /***********************************************************************
146 * ldap_count_entries (WLDAP32.@)
148 * Count the number of entries returned from a search.
150 * PARAMS
151 * ld [I] Pointer to an LDAP context.
152 * res [I] LDAP message.
154 * RETURNS
155 * Success: The number of entries.
156 * Failure: ~0u
158 ULONG CDECL WLDAP32_ldap_count_entries( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
160 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
161 #ifdef HAVE_LDAP
163 TRACE( "(%p, %p)\n", ld, res );
165 if (!ld) return ~0u;
166 ret = ldap_count_entries( ld, res );
168 #endif
169 return ret;
172 /***********************************************************************
173 * ldap_count_references (WLDAP32.@)
175 * Count the number of references returned from a search.
177 * PARAMS
178 * ld [I] Pointer to an LDAP context.
179 * res [I] LDAP message.
181 * RETURNS
182 * Success: The number of references.
183 * Failure: ~0u
185 ULONG CDECL WLDAP32_ldap_count_references( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
187 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
188 #ifdef HAVE_LDAP_COUNT_REFERENCES
190 TRACE( "(%p, %p)\n", ld, res );
192 if (!ld) return 0;
193 ret = ldap_count_references( ld, res );
195 #endif
196 return ret;
199 static ULONG get_escape_size( PCHAR src, ULONG srclen )
201 ULONG i, size = 0;
203 if (src)
205 for (i = 0; i < srclen; i++)
207 if ((src[i] >= '0' && src[i] <= '9') ||
208 (src[i] >= 'A' && src[i] <= 'Z') ||
209 (src[i] >= 'a' && src[i] <= 'z'))
210 size++;
211 else
212 size += 3;
215 return size + 1;
218 static void escape_filter_element( PCHAR src, ULONG srclen, PCHAR dst )
220 ULONG i;
221 static const char fmt[] = "\\%02X";
222 char *d = dst;
224 for (i = 0; i < srclen; i++)
226 if ((src[i] >= '0' && src[i] <= '9') ||
227 (src[i] >= 'A' && src[i] <= 'Z') ||
228 (src[i] >= 'a' && src[i] <= 'z'))
229 *d++ = src[i];
230 else
232 sprintf( d, fmt, (unsigned char)src[i] );
233 d += 3;
236 *++d = 0;
239 /***********************************************************************
240 * ldap_escape_filter_elementA (WLDAP32.@)
242 * See ldap_escape_filter_elementW.
244 ULONG CDECL ldap_escape_filter_elementA( PCHAR src, ULONG srclen, PCHAR dst, ULONG dstlen )
246 ULONG len;
248 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src, srclen, dst, dstlen );
250 len = get_escape_size( src, srclen );
251 if (!dst) return len;
253 if (!src || dstlen < len)
254 return WLDAP32_LDAP_PARAM_ERROR;
255 else
257 escape_filter_element( src, srclen, dst );
258 return WLDAP32_LDAP_SUCCESS;
262 /***********************************************************************
263 * ldap_escape_filter_elementW (WLDAP32.@)
265 * Escape binary data for safe passing in filters.
267 * PARAMS
268 * src [I] Filter element to be escaped.
269 * srclen [I] Length in bytes of the filter element.
270 * dst [O] Destination buffer for the escaped filter element.
271 * dstlen [I] Length in bytes of the destination buffer.
273 * RETURNS
274 * Success: LDAP_SUCCESS
275 * Failure: An LDAP error code.
277 ULONG CDECL ldap_escape_filter_elementW( PCHAR src, ULONG srclen, PWCHAR dst, ULONG dstlen )
279 ULONG len;
281 TRACE( "(%p, 0x%08x, %p, 0x%08x)\n", src, srclen, dst, dstlen );
283 len = get_escape_size( src, srclen );
284 if (!dst) return len;
286 /* no matter what you throw at it, this is what native returns */
287 return WLDAP32_LDAP_PARAM_ERROR;
290 /***********************************************************************
291 * ldap_first_attributeA (WLDAP32.@)
293 * See ldap_first_attributeW.
295 PCHAR CDECL ldap_first_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
296 WLDAP32_BerElement** ptr )
298 PCHAR ret = NULL;
299 #ifdef HAVE_LDAP
300 WCHAR *retW;
302 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
304 if (!ld || !entry) return NULL;
305 retW = ldap_first_attributeW( ld, entry, ptr );
307 ret = strWtoA( retW );
308 ldap_memfreeW( retW );
310 #endif
311 return ret;
314 /***********************************************************************
315 * ldap_first_attributeW (WLDAP32.@)
317 * Get the first attribute for a given entry.
319 * PARAMS
320 * ld [I] Pointer to an LDAP context.
321 * entry [I] Entry to retrieve attribute for.
322 * ptr [O] Position pointer.
324 * RETURNS
325 * Success: Name of the first attribute.
326 * Failure: NULL
328 * NOTES
329 * Use ldap_memfree to free the returned string.
331 PWCHAR CDECL ldap_first_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
332 WLDAP32_BerElement** ptr )
334 PWCHAR ret = NULL;
335 #ifdef HAVE_LDAP
336 char *retU;
338 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
340 if (!ld || !entry) return NULL;
341 retU = ldap_first_attribute( ld, entry, ptr );
343 ret = strUtoW( retU );
344 ldap_memfree( retU );
346 #endif
347 return ret;
350 /***********************************************************************
351 * ldap_first_entry (WLDAP32.@)
353 * Get the first entry from a result message.
355 * PARAMS
356 * ld [I] Pointer to an LDAP context.
357 * res [I] Search result message.
359 * RETURNS
360 * Success: The first entry.
361 * Failure: NULL
363 * NOTES
364 * The returned entry will be freed when the message is freed.
366 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
368 #ifdef HAVE_LDAP
370 TRACE( "(%p, %p)\n", ld, res );
372 if (!ld || !res) return NULL;
373 return ldap_first_entry( ld, res );
375 #else
376 return NULL;
377 #endif
380 /***********************************************************************
381 * ldap_first_reference (WLDAP32.@)
383 * Get the first reference from a result message.
385 * PARAMS
386 * ld [I] Pointer to an LDAP context.
387 * res [I] Search result message.
389 * RETURNS
390 * Success: The first reference.
391 * Failure: NULL
393 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_first_reference( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *res )
395 #ifdef HAVE_LDAP_FIRST_REFERENCE
397 TRACE( "(%p, %p)\n", ld, res );
399 if (!ld) return NULL;
400 return ldap_first_reference( ld, res );
402 #else
403 return NULL;
404 #endif
407 /***********************************************************************
408 * ldap_memfreeA (WLDAP32.@)
410 * See ldap_memfreeW.
412 void CDECL ldap_memfreeA( PCHAR block )
414 TRACE( "(%p)\n", block );
415 strfreeA( block );
418 /***********************************************************************
419 * ldap_memfreeW (WLDAP32.@)
421 * Free a block of memory.
423 * PARAMS
424 * block [I] Pointer to memory block to be freed.
426 void CDECL ldap_memfreeW( PWCHAR block )
428 TRACE( "(%p)\n", block );
429 strfreeW( block );
432 /***********************************************************************
433 * ldap_msgfree (WLDAP32.@)
435 * Free a message.
437 * PARAMS
438 * res [I] Message to be freed.
440 ULONG CDECL WLDAP32_ldap_msgfree( WLDAP32_LDAPMessage *res )
442 ULONG ret = WLDAP32_LDAP_SUCCESS;
443 #ifdef HAVE_LDAP
445 TRACE( "(%p)\n", res );
446 ldap_msgfree( res );
448 #endif
449 return ret;
452 /***********************************************************************
453 * ldap_next_attributeA (WLDAP32.@)
455 * See ldap_next_attributeW.
457 PCHAR CDECL ldap_next_attributeA( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
458 WLDAP32_BerElement *ptr )
460 PCHAR ret = NULL;
461 #ifdef HAVE_LDAP
462 WCHAR *retW;
464 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
466 if (!ld || !entry || !ptr) return NULL;
467 retW = ldap_next_attributeW( ld, entry, ptr );
469 ret = strWtoA( retW );
470 ldap_memfreeW( retW );
472 #endif
473 return ret;
476 /***********************************************************************
477 * ldap_next_attributeW (WLDAP32.@)
479 * Get the next attribute for a given entry.
481 * PARAMS
482 * ld [I] Pointer to an LDAP context.
483 * entry [I] Entry to retrieve attribute for.
484 * ptr [I/O] Position pointer.
486 * RETURNS
487 * Success: The name of the next attribute.
488 * Failure: NULL
490 * NOTES
491 * Free the returned string after each iteration with ldap_memfree.
492 * When done iterating and when ptr != NULL, call ber_free( ptr, 0 ).
494 PWCHAR CDECL ldap_next_attributeW( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry,
495 WLDAP32_BerElement *ptr )
497 PWCHAR ret = NULL;
498 #ifdef HAVE_LDAP
499 char *retU;
501 TRACE( "(%p, %p, %p)\n", ld, entry, ptr );
503 if (!ld || !entry || !ptr) return NULL;
504 retU = ldap_next_attribute( ld, entry, ptr );
506 ret = strUtoW( retU );
507 ldap_memfree( retU );
509 #endif
510 return ret;
513 /***********************************************************************
514 * ldap_next_entry (WLDAP32.@)
516 * Get the next entry from a result message.
518 * PARAMS
519 * ld [I] Pointer to an LDAP context.
520 * entry [I] Entry returned by a previous call.
522 * RETURNS
523 * Success: The next entry.
524 * Failure: NULL
526 * NOTES
527 * The returned entry will be freed when the message is freed.
529 WLDAP32_LDAPMessage * CDECL WLDAP32_ldap_next_entry( WLDAP32_LDAP *ld, WLDAP32_LDAPMessage *entry )
531 #ifdef HAVE_LDAP
533 TRACE( "(%p, %p)\n", ld, entry );
535 if (!ld || !entry) return NULL;
536 return ldap_next_entry( ld, entry );
538 #else
539 return NULL;
540 #endif
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 #else
569 return NULL;
570 #endif
573 /***********************************************************************
574 * ldap_result (WLDAP32.@)
576 * Get the result of an asynchronous operation.
578 * PARAMS
579 * ld [I] Pointer to an LDAP context.
580 * msgid [I] Message ID of the operation.
581 * all [I] How many results should be returned?
582 * timeout [I] How long to wait for the results?
583 * res [O] Result message for the operation.
585 * RETURNS
586 * Success: One of the following values:
588 * LDAP_RES_ADD
589 * LDAP_RES_BIND
590 * LDAP_RES_COMPARE
591 * LDAP_RES_DELETE
592 * LDAP_RES_EXTENDED
593 * LDAP_RES_MODIFY
594 * LDAP_RES_MODRDN
595 * LDAP_RES_REFERRAL
596 * LDAP_RES_SEARCH_ENTRY
597 * LDAP_RES_SEARCH_RESULT
599 * Failure: ~0u
601 * This function returns 0 when the timeout has expired.
603 * NOTES
604 * A NULL timeout pointer causes the function to block waiting
605 * for results to arrive. A timeout value of 0 causes the function
606 * to immediately return any available results. Free returned results
607 * with ldap_msgfree.
609 ULONG CDECL WLDAP32_ldap_result( WLDAP32_LDAP *ld, ULONG msgid, ULONG all,
610 struct l_timeval *timeout, WLDAP32_LDAPMessage **res )
612 ULONG ret = WLDAP32_LDAP_NOT_SUPPORTED;
613 #ifdef HAVE_LDAP
615 TRACE( "(%p, 0x%08x, 0x%08x, %p, %p)\n", ld, msgid, all, timeout, res );
617 if (!ld || !res || msgid == ~0u) return ~0u;
618 ret = ldap_result( ld, msgid, all, (struct timeval *)timeout, res );
620 #endif
621 return ret;
624 /***********************************************************************
625 * LdapUnicodeToUTF8 (WLDAP32.@)
627 * Convert a wide character string to a UTF8 string.
629 * PARAMS
630 * src [I] Wide character string to convert.
631 * srclen [I] Size of string to convert, in characters.
632 * dst [O] Pointer to a buffer that receives the converted string.
633 * dstlen [I] Size of the destination buffer in characters.
635 * RETURNS
636 * The number of characters written into the destination buffer.
638 * NOTES
639 * Set dstlen to zero to ask for the required buffer size.
641 int CDECL LdapUnicodeToUTF8( LPCWSTR src, int srclen, LPSTR dst, int dstlen )
643 return WideCharToMultiByte( CP_UTF8, 0, src, srclen, dst, dstlen, NULL, NULL );
646 /***********************************************************************
647 * LdapUTF8ToUnicode (WLDAP32.@)
649 * Convert a UTF8 string to a wide character string.
651 * PARAMS
652 * src [I] UTF8 string to convert.
653 * srclen [I] Size of string to convert, in characters.
654 * dst [O] Pointer to a buffer that receives the converted string.
655 * dstlen [I] Size of the destination buffer in characters.
657 * RETURNS
658 * The number of characters written into the destination buffer.
660 * NOTES
661 * Set dstlen to zero to ask for the required buffer size.
663 int CDECL LdapUTF8ToUnicode( LPCSTR src, int srclen, LPWSTR dst, int dstlen )
665 return MultiByteToWideChar( CP_UTF8, 0, src, srclen, dst, dstlen );