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
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 LPWSTR
strAtoW( LPCSTR str
)
47 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
48 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
49 MultiByteToWideChar( CP_ACP
, 0, str
, -1, ret
, len
);
54 static inline LPSTR
strWtoA( LPCWSTR str
)
59 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
60 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
)))
61 WideCharToMultiByte( CP_ACP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
66 static inline char *strWtoU( LPCWSTR str
)
71 DWORD len
= WideCharToMultiByte( CP_UTF8
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
72 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
)))
73 WideCharToMultiByte( CP_UTF8
, 0, str
, -1, ret
, len
, NULL
, NULL
);
78 static inline LPWSTR
strUtoW( char *str
)
83 DWORD len
= MultiByteToWideChar( CP_UTF8
, 0, str
, -1, NULL
, 0 );
84 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
85 MultiByteToWideChar( CP_UTF8
, 0, str
, -1, ret
, len
);
90 static inline void strfreeA( LPSTR str
)
92 HeapFree( GetProcessHeap(), 0, str
);
95 static inline void strfreeW( LPWSTR str
)
97 HeapFree( GetProcessHeap(), 0, str
);
100 static inline void strfreeU( char *str
)
102 HeapFree( GetProcessHeap(), 0, str
);
105 static inline DWORD
strarraylenA( LPSTR
*strarray
)
112 static inline DWORD
strarraylenW( LPWSTR
*strarray
)
114 LPWSTR
*p
= strarray
;
119 static inline DWORD
strarraylenU( char **strarray
)
126 static inline LPWSTR
*strarrayAtoW( LPSTR
*strarray
)
128 LPWSTR
*strarrayW
= NULL
;
133 size
= sizeof(WCHAR
*) * (strarraylenA( strarray
) + 1);
134 strarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
139 LPWSTR
*q
= strarrayW
;
141 while (*p
) *q
++ = strAtoW( *p
++ );
148 static inline LPSTR
*strarrayWtoA( LPWSTR
*strarray
)
150 LPSTR
*strarrayA
= NULL
;
155 size
= sizeof(LPSTR
) * (strarraylenW( strarray
) + 1);
156 strarrayA
= HeapAlloc( GetProcessHeap(), 0, size
);
160 LPWSTR
*p
= strarray
;
161 LPSTR
*q
= strarrayA
;
163 while (*p
) *q
++ = strWtoA( *p
++ );
170 static inline char **strarrayWtoU( LPWSTR
*strarray
)
172 char **strarrayU
= NULL
;
177 size
= sizeof(char*) * (strarraylenW( strarray
) + 1);
178 strarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
182 LPWSTR
*p
= strarray
;
183 char **q
= strarrayU
;
185 while (*p
) *q
++ = strWtoU( *p
++ );
192 static inline LPWSTR
*strarrayUtoW( char **strarray
)
194 LPWSTR
*strarrayW
= NULL
;
199 size
= sizeof(WCHAR
*) * (strarraylenU( strarray
) + 1);
200 strarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
205 LPWSTR
*q
= strarrayW
;
207 while (*p
) *q
++ = strUtoW( *p
++ );
214 static inline void strarrayfreeA( LPSTR
*strarray
)
219 while (*p
) strfreeA( *p
++ );
220 HeapFree( GetProcessHeap(), 0, strarray
);
224 static inline void strarrayfreeW( LPWSTR
*strarray
)
228 LPWSTR
*p
= strarray
;
229 while (*p
) strfreeW( *p
++ );
230 HeapFree( GetProcessHeap(), 0, strarray
);
234 static inline void strarrayfreeU( char **strarray
)
239 while (*p
) strfreeU( *p
++ );
240 HeapFree( GetProcessHeap(), 0, strarray
);
246 static inline struct berval
*bvdup( struct berval
*bv
)
248 struct berval
*berval
;
249 DWORD size
= sizeof(struct berval
) + bv
->bv_len
;
251 berval
= HeapAlloc( GetProcessHeap(), 0, size
);
254 char *val
= (char *)berval
+ sizeof(struct berval
);
256 berval
->bv_len
= bv
->bv_len
;
257 berval
->bv_val
= val
;
258 memcpy( val
, bv
->bv_val
, bv
->bv_len
);
263 static inline DWORD
bvarraylen( struct berval
**bv
)
265 struct berval
**p
= bv
;
270 static inline struct berval
**bvarraydup( struct berval
**bv
)
272 struct berval
**berval
= NULL
;
277 size
= sizeof(struct berval
*) * (bvarraylen( bv
) + 1);
278 berval
= HeapAlloc( GetProcessHeap(), 0, size
);
282 struct berval
**p
= bv
;
283 struct berval
**q
= berval
;
285 while (*p
) *q
++ = bvdup( *p
++ );
292 static inline void bvarrayfree( struct berval
**bv
)
294 struct berval
**p
= bv
;
295 while (*p
) HeapFree( GetProcessHeap(), 0, *p
++ );
296 HeapFree( GetProcessHeap(), 0, bv
);
299 static inline LDAPModW
*modAtoW( LDAPModA
*mod
)
303 modW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPModW
) );
306 modW
->mod_op
= mod
->mod_op
;
307 modW
->mod_type
= strAtoW( mod
->mod_type
);
309 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
310 modW
->mod_vals
.modv_bvals
= bvarraydup( mod
->mod_vals
.modv_bvals
);
312 modW
->mod_vals
.modv_strvals
= strarrayAtoW( mod
->mod_vals
.modv_strvals
);
317 static inline LDAPMod
*modWtoU( LDAPModW
*mod
)
321 modU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPMod
) );
324 modU
->mod_op
= mod
->mod_op
;
325 modU
->mod_type
= strWtoU( mod
->mod_type
);
327 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
328 modU
->mod_vals
.modv_bvals
= bvarraydup( mod
->mod_vals
.modv_bvals
);
330 modU
->mod_vals
.modv_strvals
= strarrayWtoU( mod
->mod_vals
.modv_strvals
);
335 static inline void modfreeW( LDAPModW
*mod
)
337 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
338 bvarrayfree( mod
->mod_vals
.modv_bvals
);
340 strarrayfreeW( mod
->mod_vals
.modv_strvals
);
341 HeapFree( GetProcessHeap(), 0, mod
);
344 static inline void modfreeU( LDAPMod
*mod
)
346 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
347 bvarrayfree( mod
->mod_vals
.modv_bvals
);
349 strarrayfreeU( mod
->mod_vals
.modv_strvals
);
350 HeapFree( GetProcessHeap(), 0, mod
);
353 static inline DWORD
modarraylenA( LDAPModA
**modarray
)
355 LDAPModA
**p
= modarray
;
360 static inline DWORD
modarraylenW( LDAPModW
**modarray
)
362 LDAPModW
**p
= modarray
;
367 static inline LDAPModW
**modarrayAtoW( LDAPModA
**modarray
)
369 LDAPModW
**modarrayW
= NULL
;
374 size
= sizeof(LDAPModW
*) * (modarraylenA( modarray
) + 1);
375 modarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
379 LDAPModA
**p
= modarray
;
380 LDAPModW
**q
= modarrayW
;
382 while (*p
) *q
++ = modAtoW( *p
++ );
389 static inline LDAPMod
**modarrayWtoU( LDAPModW
**modarray
)
391 LDAPMod
**modarrayU
= NULL
;
396 size
= sizeof(LDAPMod
*) * (modarraylenW( modarray
) + 1);
397 modarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
401 LDAPModW
**p
= modarray
;
402 LDAPMod
**q
= modarrayU
;
404 while (*p
) *q
++ = modWtoU( *p
++ );
411 static inline void modarrayfreeW( LDAPModW
**modarray
)
415 LDAPModW
**p
= modarray
;
416 while (*p
) modfreeW( *p
++ );
417 HeapFree( GetProcessHeap(), 0, modarray
);
421 static inline void modarrayfreeU( LDAPMod
**modarray
)
425 LDAPMod
**p
= modarray
;
426 while (*p
) modfreeU( *p
++ );
427 HeapFree( GetProcessHeap(), 0, modarray
);
431 static inline LDAPControlW
*controlAtoW( LDAPControlA
*control
)
433 LDAPControlW
*controlW
;
434 DWORD len
= control
->ldctl_value
.bv_len
;
437 if (control
->ldctl_value
.bv_val
)
439 val
= HeapAlloc( GetProcessHeap(), 0, len
);
440 if (!val
) return NULL
;
441 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
444 controlW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW
) );
447 HeapFree( GetProcessHeap(), 0, val
);
451 controlW
->ldctl_oid
= strAtoW( control
->ldctl_oid
);
452 controlW
->ldctl_value
.bv_len
= len
;
453 controlW
->ldctl_value
.bv_val
= val
;
454 controlW
->ldctl_iscritical
= control
->ldctl_iscritical
;
459 static inline LDAPControlA
*controlWtoA( LDAPControlW
*control
)
461 LDAPControlA
*controlA
;
462 DWORD len
= control
->ldctl_value
.bv_len
;
465 if (control
->ldctl_value
.bv_val
)
467 val
= HeapAlloc( GetProcessHeap(), 0, len
);
468 if (!val
) return NULL
;
469 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
472 controlA
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlA
) );
475 HeapFree( GetProcessHeap(), 0, val
);
479 controlA
->ldctl_oid
= strWtoA( control
->ldctl_oid
);
480 controlA
->ldctl_value
.bv_len
= len
;
481 controlA
->ldctl_value
.bv_val
= val
;
482 controlA
->ldctl_iscritical
= control
->ldctl_iscritical
;
487 static inline LDAPControl
*controlWtoU( LDAPControlW
*control
)
489 LDAPControl
*controlU
;
490 DWORD len
= control
->ldctl_value
.bv_len
;
493 if (control
->ldctl_value
.bv_val
)
495 val
= HeapAlloc( GetProcessHeap(), 0, len
);
496 if (!val
) return NULL
;
497 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
500 controlU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControl
) );
503 HeapFree( GetProcessHeap(), 0, val
);
507 controlU
->ldctl_oid
= strWtoU( control
->ldctl_oid
);
508 controlU
->ldctl_value
.bv_len
= len
;
509 controlU
->ldctl_value
.bv_val
= val
;
510 controlU
->ldctl_iscritical
= control
->ldctl_iscritical
;
515 static inline LDAPControlW
*controlUtoW( LDAPControl
*control
)
517 LDAPControlW
*controlW
;
518 DWORD len
= control
->ldctl_value
.bv_len
;
521 if (control
->ldctl_value
.bv_val
)
523 val
= HeapAlloc( GetProcessHeap(), 0, len
);
524 if (!val
) return NULL
;
525 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
528 controlW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW
) );
531 HeapFree( GetProcessHeap(), 0, val
);
535 controlW
->ldctl_oid
= strUtoW( control
->ldctl_oid
);
536 controlW
->ldctl_value
.bv_len
= len
;
537 controlW
->ldctl_value
.bv_val
= val
;
538 controlW
->ldctl_iscritical
= control
->ldctl_iscritical
;
543 static inline void controlfreeA( LDAPControlA
*control
)
547 strfreeA( control
->ldctl_oid
);
548 HeapFree( GetProcessHeap(), 0, control
->ldctl_value
.bv_val
);
549 HeapFree( GetProcessHeap(), 0, control
);
553 static inline void controlfreeW( LDAPControlW
*control
)
557 strfreeW( control
->ldctl_oid
);
558 HeapFree( GetProcessHeap(), 0, control
->ldctl_value
.bv_val
);
559 HeapFree( GetProcessHeap(), 0, control
);
563 static inline void controlfreeU( LDAPControl
*control
)
567 strfreeU( control
->ldctl_oid
);
568 HeapFree( GetProcessHeap(), 0, control
->ldctl_value
.bv_val
);
569 HeapFree( GetProcessHeap(), 0, control
);
573 static inline DWORD
controlarraylenA( LDAPControlA
**controlarray
)
575 LDAPControlA
**p
= controlarray
;
577 return p
- controlarray
;
580 static inline DWORD
controlarraylenW( LDAPControlW
**controlarray
)
582 LDAPControlW
**p
= controlarray
;
584 return p
- controlarray
;
587 static inline DWORD
controlarraylenU( LDAPControl
**controlarray
)
589 LDAPControl
**p
= controlarray
;
591 return p
- controlarray
;
594 static inline LDAPControlW
**controlarrayAtoW( LDAPControlA
**controlarray
)
596 LDAPControlW
**controlarrayW
= NULL
;
601 size
= sizeof(LDAPControlW
*) * (controlarraylenA( controlarray
) + 1);
602 controlarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
606 LDAPControlA
**p
= controlarray
;
607 LDAPControlW
**q
= controlarrayW
;
609 while (*p
) *q
++ = controlAtoW( *p
++ );
613 return controlarrayW
;
616 static inline LDAPControlA
**controlarrayWtoA( LDAPControlW
**controlarray
)
618 LDAPControlA
**controlarrayA
= NULL
;
623 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
624 controlarrayA
= HeapAlloc( GetProcessHeap(), 0, size
);
628 LDAPControlW
**p
= controlarray
;
629 LDAPControlA
**q
= controlarrayA
;
631 while (*p
) *q
++ = controlWtoA( *p
++ );
635 return controlarrayA
;
638 static inline LDAPControl
**controlarrayWtoU( LDAPControlW
**controlarray
)
640 LDAPControl
**controlarrayU
= NULL
;
645 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
646 controlarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
650 LDAPControlW
**p
= controlarray
;
651 LDAPControl
**q
= controlarrayU
;
653 while (*p
) *q
++ = controlWtoU( *p
++ );
657 return controlarrayU
;
660 static inline LDAPControlW
**controlarrayUtoW( LDAPControl
**controlarray
)
662 LDAPControlW
**controlarrayW
= NULL
;
667 size
= sizeof(LDAPControlW
*) * (controlarraylenU( controlarray
) + 1);
668 controlarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
672 LDAPControl
**p
= controlarray
;
673 LDAPControlW
**q
= controlarrayW
;
675 while (*p
) *q
++ = controlUtoW( *p
++ );
679 return controlarrayW
;
682 static inline void controlarrayfreeA( LDAPControlA
**controlarray
)
686 LDAPControlA
**p
= controlarray
;
687 while (*p
) controlfreeA( *p
++ );
688 HeapFree( GetProcessHeap(), 0, controlarray
);
692 static inline void controlarrayfreeW( LDAPControlW
**controlarray
)
696 LDAPControlW
**p
= controlarray
;
697 while (*p
) controlfreeW( *p
++ );
698 HeapFree( GetProcessHeap(), 0, controlarray
);
702 static inline void controlarrayfreeU( LDAPControl
**controlarray
)
706 LDAPControl
**p
= controlarray
;
707 while (*p
) controlfreeU( *p
++ );
708 HeapFree( GetProcessHeap(), 0, controlarray
);
712 static inline LDAPSortKeyW
*sortkeyAtoW( LDAPSortKeyA
*sortkey
)
714 LDAPSortKeyW
*sortkeyW
;
716 sortkeyW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyW
) );
719 sortkeyW
->sk_attrtype
= strAtoW( sortkey
->sk_attrtype
);
720 sortkeyW
->sk_matchruleoid
= strAtoW( sortkey
->sk_matchruleoid
);
721 sortkeyW
->sk_reverseorder
= sortkey
->sk_reverseorder
;
726 static inline LDAPSortKeyA
*sortkeyWtoA( LDAPSortKeyW
*sortkey
)
728 LDAPSortKeyA
*sortkeyA
;
730 sortkeyA
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyA
) );
733 sortkeyA
->sk_attrtype
= strWtoA( sortkey
->sk_attrtype
);
734 sortkeyA
->sk_matchruleoid
= strWtoA( sortkey
->sk_matchruleoid
);
735 sortkeyA
->sk_reverseorder
= sortkey
->sk_reverseorder
;
740 static inline LDAPSortKey
*sortkeyWtoU( LDAPSortKeyW
*sortkey
)
742 LDAPSortKey
*sortkeyU
;
744 sortkeyU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKey
) );
747 sortkeyU
->attributeType
= strWtoU( sortkey
->sk_attrtype
);
748 sortkeyU
->orderingRule
= strWtoU( sortkey
->sk_matchruleoid
);
749 sortkeyU
->reverseOrder
= sortkey
->sk_reverseorder
;
754 static inline void sortkeyfreeA( LDAPSortKeyA
*sortkey
)
758 strfreeA( sortkey
->sk_attrtype
);
759 strfreeA( sortkey
->sk_matchruleoid
);
760 HeapFree( GetProcessHeap(), 0, sortkey
);
764 static inline void sortkeyfreeW( LDAPSortKeyW
*sortkey
)
768 strfreeW( sortkey
->sk_attrtype
);
769 strfreeW( sortkey
->sk_matchruleoid
);
770 HeapFree( GetProcessHeap(), 0, sortkey
);
774 static inline void sortkeyfreeU( LDAPSortKey
*sortkey
)
778 strfreeU( sortkey
->attributeType
);
779 strfreeU( sortkey
->orderingRule
);
780 HeapFree( GetProcessHeap(), 0, sortkey
);
784 static inline DWORD
sortkeyarraylenA( LDAPSortKeyA
**sortkeyarray
)
786 LDAPSortKeyA
**p
= sortkeyarray
;
788 return p
- sortkeyarray
;
791 static inline DWORD
sortkeyarraylenW( LDAPSortKeyW
**sortkeyarray
)
793 LDAPSortKeyW
**p
= sortkeyarray
;
795 return p
- sortkeyarray
;
798 static inline LDAPSortKeyW
**sortkeyarrayAtoW( LDAPSortKeyA
**sortkeyarray
)
800 LDAPSortKeyW
**sortkeyarrayW
= NULL
;
805 size
= sizeof(LDAPSortKeyW
*) * (sortkeyarraylenA( sortkeyarray
) + 1);
806 sortkeyarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
810 LDAPSortKeyA
**p
= sortkeyarray
;
811 LDAPSortKeyW
**q
= sortkeyarrayW
;
813 while (*p
) *q
++ = sortkeyAtoW( *p
++ );
817 return sortkeyarrayW
;
820 static inline LDAPSortKey
**sortkeyarrayWtoU( LDAPSortKeyW
**sortkeyarray
)
822 LDAPSortKey
**sortkeyarrayU
= NULL
;
827 size
= sizeof(LDAPSortKey
*) * (sortkeyarraylenW( sortkeyarray
) + 1);
828 sortkeyarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
832 LDAPSortKeyW
**p
= sortkeyarray
;
833 LDAPSortKey
**q
= sortkeyarrayU
;
835 while (*p
) *q
++ = sortkeyWtoU( *p
++ );
839 return sortkeyarrayU
;
842 static inline void sortkeyarrayfreeW( LDAPSortKeyW
**sortkeyarray
)
846 LDAPSortKeyW
**p
= sortkeyarray
;
847 while (*p
) sortkeyfreeW( *p
++ );
848 HeapFree( GetProcessHeap(), 0, sortkeyarray
);
852 static inline void sortkeyarrayfreeU( LDAPSortKey
**sortkeyarray
)
856 LDAPSortKey
**p
= sortkeyarray
;
857 while (*p
) sortkeyfreeU( *p
++ );
858 HeapFree( GetProcessHeap(), 0, sortkeyarray
);
861 #endif /* HAVE_LDAP */