user32: Add stub for SetThreadDpiAwarenessContext.
[wine.git] / dlls / wldap32 / wldap32.h
blob1d4052002abbf8494cfc7a047f91220945d56ebc
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 "wine/heap.h"
22 #include "wine/unicode.h"
24 extern HINSTANCE hwldap32 DECLSPEC_HIDDEN;
26 ULONG map_error( int ) DECLSPEC_HIDDEN;
28 /* A set of helper functions to convert LDAP data structures
29 * to and from ansi (A), wide character (W) and utf8 (U) encodings.
32 static inline char *strdupU( const char *src )
34 char *dst;
35 if (!src) return NULL;
36 if ((dst = heap_alloc( (strlen( src ) + 1) * sizeof(char) ))) strcpy( dst, src );
37 return dst;
40 static inline WCHAR *strdupW( const WCHAR *src )
42 WCHAR *dst;
43 if (!src) return NULL;
44 if ((dst = heap_alloc( (strlenW( src ) + 1) * sizeof(WCHAR) ))) strcpyW( dst, src );
45 return dst;
48 static inline LPWSTR strAtoW( LPCSTR str )
50 LPWSTR ret = NULL;
51 if (str)
53 DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
54 if ((ret = heap_alloc( len * sizeof(WCHAR) )))
55 MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
57 return ret;
60 static inline LPSTR strWtoA( LPCWSTR str )
62 LPSTR ret = NULL;
63 if (str)
65 DWORD len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
66 if ((ret = heap_alloc( len )))
67 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
69 return ret;
72 static inline char *strWtoU( LPCWSTR str )
74 LPSTR ret = NULL;
75 if (str)
77 DWORD len = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
78 if ((ret = heap_alloc( len )))
79 WideCharToMultiByte( CP_UTF8, 0, str, -1, ret, len, NULL, NULL );
81 return ret;
84 static inline LPWSTR strUtoW( char *str )
86 LPWSTR ret = NULL;
87 if (str)
89 DWORD len = MultiByteToWideChar( CP_UTF8, 0, str, -1, NULL, 0 );
90 if ((ret = heap_alloc( len * sizeof(WCHAR) )))
91 MultiByteToWideChar( CP_UTF8, 0, str, -1, ret, len );
93 return ret;
96 static inline void strfreeA( LPSTR str )
98 heap_free( str );
101 static inline void strfreeW( LPWSTR str )
103 heap_free( str );
106 static inline void strfreeU( char *str )
108 heap_free( str );
111 static inline DWORD strarraylenA( LPSTR *strarray )
113 LPSTR *p = strarray;
114 while (*p) p++;
115 return p - strarray;
118 static inline DWORD strarraylenW( LPWSTR *strarray )
120 LPWSTR *p = strarray;
121 while (*p) p++;
122 return p - strarray;
125 static inline DWORD strarraylenU( char **strarray )
127 char **p = strarray;
128 while (*p) p++;
129 return p - strarray;
132 static inline LPWSTR *strarrayAtoW( LPSTR *strarray )
134 LPWSTR *strarrayW = NULL;
135 DWORD size;
137 if (strarray)
139 size = sizeof(WCHAR*) * (strarraylenA( strarray ) + 1);
140 if ((strarrayW = heap_alloc( size )))
142 LPSTR *p = strarray;
143 LPWSTR *q = strarrayW;
145 while (*p) *q++ = strAtoW( *p++ );
146 *q = NULL;
149 return strarrayW;
152 static inline LPSTR *strarrayWtoA( LPWSTR *strarray )
154 LPSTR *strarrayA = NULL;
155 DWORD size;
157 if (strarray)
159 size = sizeof(LPSTR) * (strarraylenW( strarray ) + 1);
160 if ((strarrayA = heap_alloc( size )))
162 LPWSTR *p = strarray;
163 LPSTR *q = strarrayA;
165 while (*p) *q++ = strWtoA( *p++ );
166 *q = NULL;
169 return strarrayA;
172 static inline char **strarrayWtoU( LPWSTR *strarray )
174 char **strarrayU = NULL;
175 DWORD size;
177 if (strarray)
179 size = sizeof(char*) * (strarraylenW( strarray ) + 1);
180 if ((strarrayU = heap_alloc( size )))
182 LPWSTR *p = strarray;
183 char **q = strarrayU;
185 while (*p) *q++ = strWtoU( *p++ );
186 *q = NULL;
189 return strarrayU;
192 static inline LPWSTR *strarrayUtoW( char **strarray )
194 LPWSTR *strarrayW = NULL;
195 DWORD size;
197 if (strarray)
199 size = sizeof(WCHAR*) * (strarraylenU( strarray ) + 1);
200 if ((strarrayW = heap_alloc( size )))
202 char **p = strarray;
203 LPWSTR *q = strarrayW;
205 while (*p) *q++ = strUtoW( *p++ );
206 *q = NULL;
209 return strarrayW;
212 static inline void strarrayfreeA( LPSTR *strarray )
214 if (strarray)
216 LPSTR *p = strarray;
217 while (*p) strfreeA( *p++ );
218 heap_free( strarray );
222 static inline void strarrayfreeW( LPWSTR *strarray )
224 if (strarray)
226 LPWSTR *p = strarray;
227 while (*p) strfreeW( *p++ );
228 heap_free( strarray );
232 static inline void strarrayfreeU( char **strarray )
234 if (strarray)
236 char **p = strarray;
237 while (*p) strfreeU( *p++ );
238 heap_free( strarray );
242 #ifdef HAVE_LDAP
244 static inline struct berval *bvdup( struct berval *bv )
246 struct berval *berval;
247 DWORD size = sizeof(struct berval) + bv->bv_len;
249 if ((berval = heap_alloc( size )))
251 char *val = (char *)berval + sizeof(struct berval);
253 berval->bv_len = bv->bv_len;
254 berval->bv_val = val;
255 memcpy( val, bv->bv_val, bv->bv_len );
257 return berval;
260 static inline DWORD bvarraylen( struct berval **bv )
262 struct berval **p = bv;
263 while (*p) p++;
264 return p - bv;
267 static inline struct berval **bvarraydup( struct berval **bv )
269 struct berval **berval = NULL;
270 DWORD size;
272 if (bv)
274 size = sizeof(struct berval *) * (bvarraylen( bv ) + 1);
275 if ((berval = heap_alloc( size )))
277 struct berval **p = bv;
278 struct berval **q = berval;
280 while (*p) *q++ = bvdup( *p++ );
281 *q = NULL;
284 return berval;
287 static inline void bvarrayfree( struct berval **bv )
289 struct berval **p = bv;
290 while (*p) heap_free( *p++ );
291 heap_free( bv );
294 static inline LDAPModW *modAtoW( LDAPModA *mod )
296 LDAPModW *modW;
298 if ((modW = heap_alloc( sizeof(LDAPModW) )))
300 modW->mod_op = mod->mod_op;
301 modW->mod_type = strAtoW( mod->mod_type );
303 if (mod->mod_op & LDAP_MOD_BVALUES)
304 modW->mod_vals.modv_bvals = bvarraydup( mod->mod_vals.modv_bvals );
305 else
306 modW->mod_vals.modv_strvals = strarrayAtoW( mod->mod_vals.modv_strvals );
308 return modW;
311 static inline LDAPMod *modWtoU( LDAPModW *mod )
313 LDAPMod *modU;
315 if ((modU = heap_alloc( sizeof(LDAPMod) )))
317 modU->mod_op = mod->mod_op;
318 modU->mod_type = strWtoU( mod->mod_type );
320 if (mod->mod_op & LDAP_MOD_BVALUES)
321 modU->mod_vals.modv_bvals = bvarraydup( mod->mod_vals.modv_bvals );
322 else
323 modU->mod_vals.modv_strvals = strarrayWtoU( mod->mod_vals.modv_strvals );
325 return modU;
328 static inline void modfreeW( LDAPModW *mod )
330 if (mod->mod_op & LDAP_MOD_BVALUES)
331 bvarrayfree( mod->mod_vals.modv_bvals );
332 else
333 strarrayfreeW( mod->mod_vals.modv_strvals );
334 heap_free( mod );
337 static inline void modfreeU( LDAPMod *mod )
339 if (mod->mod_op & LDAP_MOD_BVALUES)
340 bvarrayfree( mod->mod_vals.modv_bvals );
341 else
342 strarrayfreeU( mod->mod_vals.modv_strvals );
343 heap_free( mod );
346 static inline DWORD modarraylenA( LDAPModA **modarray )
348 LDAPModA **p = modarray;
349 while (*p) p++;
350 return p - modarray;
353 static inline DWORD modarraylenW( LDAPModW **modarray )
355 LDAPModW **p = modarray;
356 while (*p) p++;
357 return p - modarray;
360 static inline LDAPModW **modarrayAtoW( LDAPModA **modarray )
362 LDAPModW **modarrayW = NULL;
363 DWORD size;
365 if (modarray)
367 size = sizeof(LDAPModW*) * (modarraylenA( modarray ) + 1);
368 if ((modarrayW = heap_alloc( size )))
370 LDAPModA **p = modarray;
371 LDAPModW **q = modarrayW;
373 while (*p) *q++ = modAtoW( *p++ );
374 *q = NULL;
377 return modarrayW;
380 static inline LDAPMod **modarrayWtoU( LDAPModW **modarray )
382 LDAPMod **modarrayU = NULL;
383 DWORD size;
385 if (modarray)
387 size = sizeof(LDAPMod*) * (modarraylenW( modarray ) + 1);
388 if ((modarrayU = heap_alloc( size )))
390 LDAPModW **p = modarray;
391 LDAPMod **q = modarrayU;
393 while (*p) *q++ = modWtoU( *p++ );
394 *q = NULL;
397 return modarrayU;
400 static inline void modarrayfreeW( LDAPModW **modarray )
402 if (modarray)
404 LDAPModW **p = modarray;
405 while (*p) modfreeW( *p++ );
406 heap_free( modarray );
410 static inline void modarrayfreeU( LDAPMod **modarray )
412 if (modarray)
414 LDAPMod **p = modarray;
415 while (*p) modfreeU( *p++ );
416 heap_free( modarray );
420 static inline LDAPControlW *controlAtoW( LDAPControlA *control )
422 LDAPControlW *controlW;
423 DWORD len = control->ldctl_value.bv_len;
424 char *val = NULL;
426 if (control->ldctl_value.bv_val)
428 if (!(val = heap_alloc( len ))) return NULL;
429 memcpy( val, control->ldctl_value.bv_val, len );
432 if (!(controlW = heap_alloc( sizeof(LDAPControlW) )))
434 heap_free( val );
435 return NULL;
438 controlW->ldctl_oid = strAtoW( control->ldctl_oid );
439 controlW->ldctl_value.bv_len = len;
440 controlW->ldctl_value.bv_val = val;
441 controlW->ldctl_iscritical = control->ldctl_iscritical;
443 return controlW;
446 static inline LDAPControlA *controlWtoA( LDAPControlW *control )
448 LDAPControlA *controlA;
449 DWORD len = control->ldctl_value.bv_len;
450 char *val = NULL;
452 if (control->ldctl_value.bv_val)
454 if (!(val = heap_alloc( len ))) return NULL;
455 memcpy( val, control->ldctl_value.bv_val, len );
458 if (!(controlA = heap_alloc( sizeof(LDAPControlA) )))
460 heap_free( val );
461 return NULL;
464 controlA->ldctl_oid = strWtoA( control->ldctl_oid );
465 controlA->ldctl_value.bv_len = len;
466 controlA->ldctl_value.bv_val = val;
467 controlA->ldctl_iscritical = control->ldctl_iscritical;
469 return controlA;
472 static inline LDAPControl *controlWtoU( LDAPControlW *control )
474 LDAPControl *controlU;
475 DWORD len = control->ldctl_value.bv_len;
476 char *val = NULL;
478 if (control->ldctl_value.bv_val)
480 if (!(val = heap_alloc( len ))) return NULL;
481 memcpy( val, control->ldctl_value.bv_val, len );
484 if (!(controlU = heap_alloc( sizeof(LDAPControl) )))
486 heap_free( val );
487 return NULL;
490 controlU->ldctl_oid = strWtoU( control->ldctl_oid );
491 controlU->ldctl_value.bv_len = len;
492 controlU->ldctl_value.bv_val = val;
493 controlU->ldctl_iscritical = control->ldctl_iscritical;
495 return controlU;
498 static inline LDAPControlW *controlUtoW( LDAPControl *control )
500 LDAPControlW *controlW;
501 DWORD len = control->ldctl_value.bv_len;
502 char *val = NULL;
504 if (control->ldctl_value.bv_val)
506 if (!(val = heap_alloc( len ))) return NULL;
507 memcpy( val, control->ldctl_value.bv_val, len );
510 if (!(controlW = heap_alloc( sizeof(LDAPControlW) )))
512 heap_free( val );
513 return NULL;
516 controlW->ldctl_oid = strUtoW( control->ldctl_oid );
517 controlW->ldctl_value.bv_len = len;
518 controlW->ldctl_value.bv_val = val;
519 controlW->ldctl_iscritical = control->ldctl_iscritical;
521 return controlW;
524 static inline void controlfreeA( LDAPControlA *control )
526 if (control)
528 strfreeA( control->ldctl_oid );
529 heap_free( control->ldctl_value.bv_val );
530 heap_free( control );
534 static inline void controlfreeW( LDAPControlW *control )
536 if (control)
538 strfreeW( control->ldctl_oid );
539 heap_free( control->ldctl_value.bv_val );
540 heap_free( control );
544 static inline void controlfreeU( LDAPControl *control )
546 if (control)
548 strfreeU( control->ldctl_oid );
549 heap_free( control->ldctl_value.bv_val );
550 heap_free( control );
554 static inline DWORD controlarraylenA( LDAPControlA **controlarray )
556 LDAPControlA **p = controlarray;
557 while (*p) p++;
558 return p - controlarray;
561 static inline DWORD controlarraylenW( LDAPControlW **controlarray )
563 LDAPControlW **p = controlarray;
564 while (*p) p++;
565 return p - controlarray;
568 static inline DWORD controlarraylenU( LDAPControl **controlarray )
570 LDAPControl **p = controlarray;
571 while (*p) p++;
572 return p - controlarray;
575 static inline LDAPControlW **controlarrayAtoW( LDAPControlA **controlarray )
577 LDAPControlW **controlarrayW = NULL;
578 DWORD size;
580 if (controlarray)
582 size = sizeof(LDAPControlW*) * (controlarraylenA( controlarray ) + 1);
583 if ((controlarrayW = heap_alloc( size )))
585 LDAPControlA **p = controlarray;
586 LDAPControlW **q = controlarrayW;
588 while (*p) *q++ = controlAtoW( *p++ );
589 *q = NULL;
592 return controlarrayW;
595 static inline LDAPControlA **controlarrayWtoA( LDAPControlW **controlarray )
597 LDAPControlA **controlarrayA = NULL;
598 DWORD size;
600 if (controlarray)
602 size = sizeof(LDAPControl*) * (controlarraylenW( controlarray ) + 1);
603 if ((controlarrayA = heap_alloc( size )))
605 LDAPControlW **p = controlarray;
606 LDAPControlA **q = controlarrayA;
608 while (*p) *q++ = controlWtoA( *p++ );
609 *q = NULL;
612 return controlarrayA;
615 static inline LDAPControl **controlarrayWtoU( LDAPControlW **controlarray )
617 LDAPControl **controlarrayU = NULL;
618 DWORD size;
620 if (controlarray)
622 size = sizeof(LDAPControl*) * (controlarraylenW( controlarray ) + 1);
623 if ((controlarrayU = heap_alloc( size )))
625 LDAPControlW **p = controlarray;
626 LDAPControl **q = controlarrayU;
628 while (*p) *q++ = controlWtoU( *p++ );
629 *q = NULL;
632 return controlarrayU;
635 static inline LDAPControlW **controlarrayUtoW( LDAPControl **controlarray )
637 LDAPControlW **controlarrayW = NULL;
638 DWORD size;
640 if (controlarray)
642 size = sizeof(LDAPControlW*) * (controlarraylenU( controlarray ) + 1);
643 if ((controlarrayW = heap_alloc( size )))
645 LDAPControl **p = controlarray;
646 LDAPControlW **q = controlarrayW;
648 while (*p) *q++ = controlUtoW( *p++ );
649 *q = NULL;
652 return controlarrayW;
655 static inline void controlarrayfreeA( LDAPControlA **controlarray )
657 if (controlarray)
659 LDAPControlA **p = controlarray;
660 while (*p) controlfreeA( *p++ );
661 heap_free( controlarray );
665 static inline void controlarrayfreeW( LDAPControlW **controlarray )
667 if (controlarray)
669 LDAPControlW **p = controlarray;
670 while (*p) controlfreeW( *p++ );
671 heap_free( controlarray );
675 static inline void controlarrayfreeU( LDAPControl **controlarray )
677 if (controlarray)
679 LDAPControl **p = controlarray;
680 while (*p) controlfreeU( *p++ );
681 heap_free( controlarray );
685 static inline LDAPSortKeyW *sortkeyAtoW( LDAPSortKeyA *sortkey )
687 LDAPSortKeyW *sortkeyW;
689 if ((sortkeyW = heap_alloc( sizeof(LDAPSortKeyW) )))
691 sortkeyW->sk_attrtype = strAtoW( sortkey->sk_attrtype );
692 sortkeyW->sk_matchruleoid = strAtoW( sortkey->sk_matchruleoid );
693 sortkeyW->sk_reverseorder = sortkey->sk_reverseorder;
695 return sortkeyW;
698 static inline LDAPSortKeyA *sortkeyWtoA( LDAPSortKeyW *sortkey )
700 LDAPSortKeyA *sortkeyA;
702 if ((sortkeyA = heap_alloc( sizeof(LDAPSortKeyA) )))
704 sortkeyA->sk_attrtype = strWtoA( sortkey->sk_attrtype );
705 sortkeyA->sk_matchruleoid = strWtoA( sortkey->sk_matchruleoid );
706 sortkeyA->sk_reverseorder = sortkey->sk_reverseorder;
708 return sortkeyA;
711 static inline LDAPSortKey *sortkeyWtoU( LDAPSortKeyW *sortkey )
713 LDAPSortKey *sortkeyU;
715 if ((sortkeyU = heap_alloc( sizeof(LDAPSortKey) )))
717 sortkeyU->attributeType = strWtoU( sortkey->sk_attrtype );
718 sortkeyU->orderingRule = strWtoU( sortkey->sk_matchruleoid );
719 sortkeyU->reverseOrder = sortkey->sk_reverseorder;
721 return sortkeyU;
724 static inline void sortkeyfreeA( LDAPSortKeyA *sortkey )
726 if (sortkey)
728 strfreeA( sortkey->sk_attrtype );
729 strfreeA( sortkey->sk_matchruleoid );
730 heap_free( sortkey );
734 static inline void sortkeyfreeW( LDAPSortKeyW *sortkey )
736 if (sortkey)
738 strfreeW( sortkey->sk_attrtype );
739 strfreeW( sortkey->sk_matchruleoid );
740 heap_free( sortkey );
744 static inline void sortkeyfreeU( LDAPSortKey *sortkey )
746 if (sortkey)
748 strfreeU( sortkey->attributeType );
749 strfreeU( sortkey->orderingRule );
750 heap_free( sortkey );
754 static inline DWORD sortkeyarraylenA( LDAPSortKeyA **sortkeyarray )
756 LDAPSortKeyA **p = sortkeyarray;
757 while (*p) p++;
758 return p - sortkeyarray;
761 static inline DWORD sortkeyarraylenW( LDAPSortKeyW **sortkeyarray )
763 LDAPSortKeyW **p = sortkeyarray;
764 while (*p) p++;
765 return p - sortkeyarray;
768 static inline LDAPSortKeyW **sortkeyarrayAtoW( LDAPSortKeyA **sortkeyarray )
770 LDAPSortKeyW **sortkeyarrayW = NULL;
771 DWORD size;
773 if (sortkeyarray)
775 size = sizeof(LDAPSortKeyW*) * (sortkeyarraylenA( sortkeyarray ) + 1);
776 if ((sortkeyarrayW = heap_alloc( size )))
778 LDAPSortKeyA **p = sortkeyarray;
779 LDAPSortKeyW **q = sortkeyarrayW;
781 while (*p) *q++ = sortkeyAtoW( *p++ );
782 *q = NULL;
785 return sortkeyarrayW;
788 static inline LDAPSortKey **sortkeyarrayWtoU( LDAPSortKeyW **sortkeyarray )
790 LDAPSortKey **sortkeyarrayU = NULL;
791 DWORD size;
793 if (sortkeyarray)
795 size = sizeof(LDAPSortKey*) * (sortkeyarraylenW( sortkeyarray ) + 1);
796 if ((sortkeyarrayU = heap_alloc( size )))
798 LDAPSortKeyW **p = sortkeyarray;
799 LDAPSortKey **q = sortkeyarrayU;
801 while (*p) *q++ = sortkeyWtoU( *p++ );
802 *q = NULL;
805 return sortkeyarrayU;
808 static inline void sortkeyarrayfreeW( LDAPSortKeyW **sortkeyarray )
810 if (sortkeyarray)
812 LDAPSortKeyW **p = sortkeyarray;
813 while (*p) sortkeyfreeW( *p++ );
814 heap_free( sortkeyarray );
818 static inline void sortkeyarrayfreeU( LDAPSortKey **sortkeyarray )
820 if (sortkeyarray)
822 LDAPSortKey **p = sortkeyarray;
823 while (*p) sortkeyfreeU( *p++ );
824 heap_free( sortkeyarray );
827 #endif /* HAVE_LDAP */