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/unicode.h"
23 extern HINSTANCE hwldap32 DECLSPEC_HIDDEN
;
25 ULONG
map_error( int ) DECLSPEC_HIDDEN
;
27 /* A set of helper functions to convert LDAP data structures
28 * to and from ansi (A), wide character (W) and utf8 (U) encodings.
31 static inline char *strdupU( const char *src
)
35 if (!src
) return NULL
;
36 dst
= HeapAlloc( GetProcessHeap(), 0, (strlen( src
) + 1) * sizeof(char) );
42 static inline WCHAR
*strdupW( const WCHAR
*src
)
46 if (!src
) return NULL
;
47 dst
= HeapAlloc( GetProcessHeap(), 0, (strlenW( src
) + 1) * sizeof(WCHAR
) );
53 static inline LPWSTR
strAtoW( LPCSTR str
)
58 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
59 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
60 MultiByteToWideChar( CP_ACP
, 0, str
, -1, ret
, len
);
65 static inline LPSTR
strWtoA( LPCWSTR str
)
70 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
71 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
)))
72 WideCharToMultiByte( CP_ACP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
77 static inline char *strWtoU( LPCWSTR str
)
82 DWORD len
= WideCharToMultiByte( CP_UTF8
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
83 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
)))
84 WideCharToMultiByte( CP_UTF8
, 0, str
, -1, ret
, len
, NULL
, NULL
);
89 static inline LPWSTR
strUtoW( char *str
)
94 DWORD len
= MultiByteToWideChar( CP_UTF8
, 0, str
, -1, NULL
, 0 );
95 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
96 MultiByteToWideChar( CP_UTF8
, 0, str
, -1, ret
, len
);
101 static inline void strfreeA( LPSTR str
)
103 HeapFree( GetProcessHeap(), 0, str
);
106 static inline void strfreeW( LPWSTR str
)
108 HeapFree( GetProcessHeap(), 0, str
);
111 static inline void strfreeU( char *str
)
113 HeapFree( GetProcessHeap(), 0, str
);
116 static inline DWORD
strarraylenA( LPSTR
*strarray
)
123 static inline DWORD
strarraylenW( LPWSTR
*strarray
)
125 LPWSTR
*p
= strarray
;
130 static inline DWORD
strarraylenU( char **strarray
)
137 static inline LPWSTR
*strarrayAtoW( LPSTR
*strarray
)
139 LPWSTR
*strarrayW
= NULL
;
144 size
= sizeof(WCHAR
*) * (strarraylenA( strarray
) + 1);
145 strarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
150 LPWSTR
*q
= strarrayW
;
152 while (*p
) *q
++ = strAtoW( *p
++ );
159 static inline LPSTR
*strarrayWtoA( LPWSTR
*strarray
)
161 LPSTR
*strarrayA
= NULL
;
166 size
= sizeof(LPSTR
) * (strarraylenW( strarray
) + 1);
167 strarrayA
= HeapAlloc( GetProcessHeap(), 0, size
);
171 LPWSTR
*p
= strarray
;
172 LPSTR
*q
= strarrayA
;
174 while (*p
) *q
++ = strWtoA( *p
++ );
181 static inline char **strarrayWtoU( LPWSTR
*strarray
)
183 char **strarrayU
= NULL
;
188 size
= sizeof(char*) * (strarraylenW( strarray
) + 1);
189 strarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
193 LPWSTR
*p
= strarray
;
194 char **q
= strarrayU
;
196 while (*p
) *q
++ = strWtoU( *p
++ );
203 static inline LPWSTR
*strarrayUtoW( char **strarray
)
205 LPWSTR
*strarrayW
= NULL
;
210 size
= sizeof(WCHAR
*) * (strarraylenU( strarray
) + 1);
211 strarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
216 LPWSTR
*q
= strarrayW
;
218 while (*p
) *q
++ = strUtoW( *p
++ );
225 static inline void strarrayfreeA( LPSTR
*strarray
)
230 while (*p
) strfreeA( *p
++ );
231 HeapFree( GetProcessHeap(), 0, strarray
);
235 static inline void strarrayfreeW( LPWSTR
*strarray
)
239 LPWSTR
*p
= strarray
;
240 while (*p
) strfreeW( *p
++ );
241 HeapFree( GetProcessHeap(), 0, strarray
);
245 static inline void strarrayfreeU( char **strarray
)
250 while (*p
) strfreeU( *p
++ );
251 HeapFree( GetProcessHeap(), 0, strarray
);
257 static inline struct berval
*bvdup( struct berval
*bv
)
259 struct berval
*berval
;
260 DWORD size
= sizeof(struct berval
) + bv
->bv_len
;
262 berval
= HeapAlloc( GetProcessHeap(), 0, size
);
265 char *val
= (char *)berval
+ sizeof(struct berval
);
267 berval
->bv_len
= bv
->bv_len
;
268 berval
->bv_val
= val
;
269 memcpy( val
, bv
->bv_val
, bv
->bv_len
);
274 static inline DWORD
bvarraylen( struct berval
**bv
)
276 struct berval
**p
= bv
;
281 static inline struct berval
**bvarraydup( struct berval
**bv
)
283 struct berval
**berval
= NULL
;
288 size
= sizeof(struct berval
*) * (bvarraylen( bv
) + 1);
289 berval
= HeapAlloc( GetProcessHeap(), 0, size
);
293 struct berval
**p
= bv
;
294 struct berval
**q
= berval
;
296 while (*p
) *q
++ = bvdup( *p
++ );
303 static inline void bvarrayfree( struct berval
**bv
)
305 struct berval
**p
= bv
;
306 while (*p
) HeapFree( GetProcessHeap(), 0, *p
++ );
307 HeapFree( GetProcessHeap(), 0, bv
);
310 static inline LDAPModW
*modAtoW( LDAPModA
*mod
)
314 modW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPModW
) );
317 modW
->mod_op
= mod
->mod_op
;
318 modW
->mod_type
= strAtoW( mod
->mod_type
);
320 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
321 modW
->mod_vals
.modv_bvals
= bvarraydup( mod
->mod_vals
.modv_bvals
);
323 modW
->mod_vals
.modv_strvals
= strarrayAtoW( mod
->mod_vals
.modv_strvals
);
328 static inline LDAPMod
*modWtoU( LDAPModW
*mod
)
332 modU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPMod
) );
335 modU
->mod_op
= mod
->mod_op
;
336 modU
->mod_type
= strWtoU( mod
->mod_type
);
338 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
339 modU
->mod_vals
.modv_bvals
= bvarraydup( mod
->mod_vals
.modv_bvals
);
341 modU
->mod_vals
.modv_strvals
= strarrayWtoU( mod
->mod_vals
.modv_strvals
);
346 static inline void modfreeW( LDAPModW
*mod
)
348 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
349 bvarrayfree( mod
->mod_vals
.modv_bvals
);
351 strarrayfreeW( mod
->mod_vals
.modv_strvals
);
352 HeapFree( GetProcessHeap(), 0, mod
);
355 static inline void modfreeU( LDAPMod
*mod
)
357 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
358 bvarrayfree( mod
->mod_vals
.modv_bvals
);
360 strarrayfreeU( mod
->mod_vals
.modv_strvals
);
361 HeapFree( GetProcessHeap(), 0, mod
);
364 static inline DWORD
modarraylenA( LDAPModA
**modarray
)
366 LDAPModA
**p
= modarray
;
371 static inline DWORD
modarraylenW( LDAPModW
**modarray
)
373 LDAPModW
**p
= modarray
;
378 static inline LDAPModW
**modarrayAtoW( LDAPModA
**modarray
)
380 LDAPModW
**modarrayW
= NULL
;
385 size
= sizeof(LDAPModW
*) * (modarraylenA( modarray
) + 1);
386 modarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
390 LDAPModA
**p
= modarray
;
391 LDAPModW
**q
= modarrayW
;
393 while (*p
) *q
++ = modAtoW( *p
++ );
400 static inline LDAPMod
**modarrayWtoU( LDAPModW
**modarray
)
402 LDAPMod
**modarrayU
= NULL
;
407 size
= sizeof(LDAPMod
*) * (modarraylenW( modarray
) + 1);
408 modarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
412 LDAPModW
**p
= modarray
;
413 LDAPMod
**q
= modarrayU
;
415 while (*p
) *q
++ = modWtoU( *p
++ );
422 static inline void modarrayfreeW( LDAPModW
**modarray
)
426 LDAPModW
**p
= modarray
;
427 while (*p
) modfreeW( *p
++ );
428 HeapFree( GetProcessHeap(), 0, modarray
);
432 static inline void modarrayfreeU( LDAPMod
**modarray
)
436 LDAPMod
**p
= modarray
;
437 while (*p
) modfreeU( *p
++ );
438 HeapFree( GetProcessHeap(), 0, modarray
);
442 static inline LDAPControlW
*controlAtoW( LDAPControlA
*control
)
444 LDAPControlW
*controlW
;
445 DWORD len
= control
->ldctl_value
.bv_len
;
448 if (control
->ldctl_value
.bv_val
)
450 val
= HeapAlloc( GetProcessHeap(), 0, len
);
451 if (!val
) return NULL
;
452 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
455 controlW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW
) );
458 HeapFree( GetProcessHeap(), 0, val
);
462 controlW
->ldctl_oid
= strAtoW( control
->ldctl_oid
);
463 controlW
->ldctl_value
.bv_len
= len
;
464 controlW
->ldctl_value
.bv_val
= val
;
465 controlW
->ldctl_iscritical
= control
->ldctl_iscritical
;
470 static inline LDAPControlA
*controlWtoA( LDAPControlW
*control
)
472 LDAPControlA
*controlA
;
473 DWORD len
= control
->ldctl_value
.bv_len
;
476 if (control
->ldctl_value
.bv_val
)
478 val
= HeapAlloc( GetProcessHeap(), 0, len
);
479 if (!val
) return NULL
;
480 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
483 controlA
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlA
) );
486 HeapFree( GetProcessHeap(), 0, val
);
490 controlA
->ldctl_oid
= strWtoA( control
->ldctl_oid
);
491 controlA
->ldctl_value
.bv_len
= len
;
492 controlA
->ldctl_value
.bv_val
= val
;
493 controlA
->ldctl_iscritical
= control
->ldctl_iscritical
;
498 static inline LDAPControl
*controlWtoU( LDAPControlW
*control
)
500 LDAPControl
*controlU
;
501 DWORD len
= control
->ldctl_value
.bv_len
;
504 if (control
->ldctl_value
.bv_val
)
506 val
= HeapAlloc( GetProcessHeap(), 0, len
);
507 if (!val
) return NULL
;
508 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
511 controlU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControl
) );
514 HeapFree( GetProcessHeap(), 0, val
);
518 controlU
->ldctl_oid
= strWtoU( control
->ldctl_oid
);
519 controlU
->ldctl_value
.bv_len
= len
;
520 controlU
->ldctl_value
.bv_val
= val
;
521 controlU
->ldctl_iscritical
= control
->ldctl_iscritical
;
526 static inline LDAPControlW
*controlUtoW( LDAPControl
*control
)
528 LDAPControlW
*controlW
;
529 DWORD len
= control
->ldctl_value
.bv_len
;
532 if (control
->ldctl_value
.bv_val
)
534 val
= HeapAlloc( GetProcessHeap(), 0, len
);
535 if (!val
) return NULL
;
536 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
539 controlW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW
) );
542 HeapFree( GetProcessHeap(), 0, val
);
546 controlW
->ldctl_oid
= strUtoW( control
->ldctl_oid
);
547 controlW
->ldctl_value
.bv_len
= len
;
548 controlW
->ldctl_value
.bv_val
= val
;
549 controlW
->ldctl_iscritical
= control
->ldctl_iscritical
;
554 static inline void controlfreeA( LDAPControlA
*control
)
558 strfreeA( control
->ldctl_oid
);
559 HeapFree( GetProcessHeap(), 0, control
->ldctl_value
.bv_val
);
560 HeapFree( GetProcessHeap(), 0, control
);
564 static inline void controlfreeW( LDAPControlW
*control
)
568 strfreeW( control
->ldctl_oid
);
569 HeapFree( GetProcessHeap(), 0, control
->ldctl_value
.bv_val
);
570 HeapFree( GetProcessHeap(), 0, control
);
574 static inline void controlfreeU( LDAPControl
*control
)
578 strfreeU( control
->ldctl_oid
);
579 HeapFree( GetProcessHeap(), 0, control
->ldctl_value
.bv_val
);
580 HeapFree( GetProcessHeap(), 0, control
);
584 static inline DWORD
controlarraylenA( LDAPControlA
**controlarray
)
586 LDAPControlA
**p
= controlarray
;
588 return p
- controlarray
;
591 static inline DWORD
controlarraylenW( LDAPControlW
**controlarray
)
593 LDAPControlW
**p
= controlarray
;
595 return p
- controlarray
;
598 static inline DWORD
controlarraylenU( LDAPControl
**controlarray
)
600 LDAPControl
**p
= controlarray
;
602 return p
- controlarray
;
605 static inline LDAPControlW
**controlarrayAtoW( LDAPControlA
**controlarray
)
607 LDAPControlW
**controlarrayW
= NULL
;
612 size
= sizeof(LDAPControlW
*) * (controlarraylenA( controlarray
) + 1);
613 controlarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
617 LDAPControlA
**p
= controlarray
;
618 LDAPControlW
**q
= controlarrayW
;
620 while (*p
) *q
++ = controlAtoW( *p
++ );
624 return controlarrayW
;
627 static inline LDAPControlA
**controlarrayWtoA( LDAPControlW
**controlarray
)
629 LDAPControlA
**controlarrayA
= NULL
;
634 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
635 controlarrayA
= HeapAlloc( GetProcessHeap(), 0, size
);
639 LDAPControlW
**p
= controlarray
;
640 LDAPControlA
**q
= controlarrayA
;
642 while (*p
) *q
++ = controlWtoA( *p
++ );
646 return controlarrayA
;
649 static inline LDAPControl
**controlarrayWtoU( LDAPControlW
**controlarray
)
651 LDAPControl
**controlarrayU
= NULL
;
656 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
657 controlarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
661 LDAPControlW
**p
= controlarray
;
662 LDAPControl
**q
= controlarrayU
;
664 while (*p
) *q
++ = controlWtoU( *p
++ );
668 return controlarrayU
;
671 static inline LDAPControlW
**controlarrayUtoW( LDAPControl
**controlarray
)
673 LDAPControlW
**controlarrayW
= NULL
;
678 size
= sizeof(LDAPControlW
*) * (controlarraylenU( controlarray
) + 1);
679 controlarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
683 LDAPControl
**p
= controlarray
;
684 LDAPControlW
**q
= controlarrayW
;
686 while (*p
) *q
++ = controlUtoW( *p
++ );
690 return controlarrayW
;
693 static inline void controlarrayfreeA( LDAPControlA
**controlarray
)
697 LDAPControlA
**p
= controlarray
;
698 while (*p
) controlfreeA( *p
++ );
699 HeapFree( GetProcessHeap(), 0, controlarray
);
703 static inline void controlarrayfreeW( LDAPControlW
**controlarray
)
707 LDAPControlW
**p
= controlarray
;
708 while (*p
) controlfreeW( *p
++ );
709 HeapFree( GetProcessHeap(), 0, controlarray
);
713 static inline void controlarrayfreeU( LDAPControl
**controlarray
)
717 LDAPControl
**p
= controlarray
;
718 while (*p
) controlfreeU( *p
++ );
719 HeapFree( GetProcessHeap(), 0, controlarray
);
723 static inline LDAPSortKeyW
*sortkeyAtoW( LDAPSortKeyA
*sortkey
)
725 LDAPSortKeyW
*sortkeyW
;
727 sortkeyW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyW
) );
730 sortkeyW
->sk_attrtype
= strAtoW( sortkey
->sk_attrtype
);
731 sortkeyW
->sk_matchruleoid
= strAtoW( sortkey
->sk_matchruleoid
);
732 sortkeyW
->sk_reverseorder
= sortkey
->sk_reverseorder
;
737 static inline LDAPSortKeyA
*sortkeyWtoA( LDAPSortKeyW
*sortkey
)
739 LDAPSortKeyA
*sortkeyA
;
741 sortkeyA
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyA
) );
744 sortkeyA
->sk_attrtype
= strWtoA( sortkey
->sk_attrtype
);
745 sortkeyA
->sk_matchruleoid
= strWtoA( sortkey
->sk_matchruleoid
);
746 sortkeyA
->sk_reverseorder
= sortkey
->sk_reverseorder
;
751 static inline LDAPSortKey
*sortkeyWtoU( LDAPSortKeyW
*sortkey
)
753 LDAPSortKey
*sortkeyU
;
755 sortkeyU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKey
) );
758 sortkeyU
->attributeType
= strWtoU( sortkey
->sk_attrtype
);
759 sortkeyU
->orderingRule
= strWtoU( sortkey
->sk_matchruleoid
);
760 sortkeyU
->reverseOrder
= sortkey
->sk_reverseorder
;
765 static inline void sortkeyfreeA( LDAPSortKeyA
*sortkey
)
769 strfreeA( sortkey
->sk_attrtype
);
770 strfreeA( sortkey
->sk_matchruleoid
);
771 HeapFree( GetProcessHeap(), 0, sortkey
);
775 static inline void sortkeyfreeW( LDAPSortKeyW
*sortkey
)
779 strfreeW( sortkey
->sk_attrtype
);
780 strfreeW( sortkey
->sk_matchruleoid
);
781 HeapFree( GetProcessHeap(), 0, sortkey
);
785 static inline void sortkeyfreeU( LDAPSortKey
*sortkey
)
789 strfreeU( sortkey
->attributeType
);
790 strfreeU( sortkey
->orderingRule
);
791 HeapFree( GetProcessHeap(), 0, sortkey
);
795 static inline DWORD
sortkeyarraylenA( LDAPSortKeyA
**sortkeyarray
)
797 LDAPSortKeyA
**p
= sortkeyarray
;
799 return p
- sortkeyarray
;
802 static inline DWORD
sortkeyarraylenW( LDAPSortKeyW
**sortkeyarray
)
804 LDAPSortKeyW
**p
= sortkeyarray
;
806 return p
- sortkeyarray
;
809 static inline LDAPSortKeyW
**sortkeyarrayAtoW( LDAPSortKeyA
**sortkeyarray
)
811 LDAPSortKeyW
**sortkeyarrayW
= NULL
;
816 size
= sizeof(LDAPSortKeyW
*) * (sortkeyarraylenA( sortkeyarray
) + 1);
817 sortkeyarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
821 LDAPSortKeyA
**p
= sortkeyarray
;
822 LDAPSortKeyW
**q
= sortkeyarrayW
;
824 while (*p
) *q
++ = sortkeyAtoW( *p
++ );
828 return sortkeyarrayW
;
831 static inline LDAPSortKey
**sortkeyarrayWtoU( LDAPSortKeyW
**sortkeyarray
)
833 LDAPSortKey
**sortkeyarrayU
= NULL
;
838 size
= sizeof(LDAPSortKey
*) * (sortkeyarraylenW( sortkeyarray
) + 1);
839 sortkeyarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
843 LDAPSortKeyW
**p
= sortkeyarray
;
844 LDAPSortKey
**q
= sortkeyarrayU
;
846 while (*p
) *q
++ = sortkeyWtoU( *p
++ );
850 return sortkeyarrayU
;
853 static inline void sortkeyarrayfreeW( LDAPSortKeyW
**sortkeyarray
)
857 LDAPSortKeyW
**p
= sortkeyarray
;
858 while (*p
) sortkeyfreeW( *p
++ );
859 HeapFree( GetProcessHeap(), 0, sortkeyarray
);
863 static inline void sortkeyarrayfreeU( LDAPSortKey
**sortkeyarray
)
867 LDAPSortKey
**p
= sortkeyarray
;
868 while (*p
) sortkeyfreeU( *p
++ );
869 HeapFree( GetProcessHeap(), 0, sortkeyarray
);
872 #endif /* HAVE_LDAP */