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 extern HINSTANCE hwldap32 DECLSPEC_HIDDEN
;
23 ULONG
map_error( int ) DECLSPEC_HIDDEN
;
25 /* A set of helper functions to convert LDAP data structures
26 * to and from ansi (A), wide character (W) and utf8 (U) encodings.
29 static inline char *strdupU( const char *src
)
33 if (!src
) return NULL
;
34 dst
= HeapAlloc( GetProcessHeap(), 0, (strlen( src
) + 1) * sizeof(char) );
40 static inline LPWSTR
strAtoW( LPCSTR str
)
45 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
46 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
47 MultiByteToWideChar( CP_ACP
, 0, str
, -1, ret
, len
);
52 static inline LPSTR
strWtoA( LPCWSTR str
)
57 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
58 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
)))
59 WideCharToMultiByte( CP_ACP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
64 static inline char *strWtoU( LPCWSTR str
)
69 DWORD len
= WideCharToMultiByte( CP_UTF8
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
70 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
)))
71 WideCharToMultiByte( CP_UTF8
, 0, str
, -1, ret
, len
, NULL
, NULL
);
76 static inline LPWSTR
strUtoW( char *str
)
81 DWORD len
= MultiByteToWideChar( CP_UTF8
, 0, str
, -1, NULL
, 0 );
82 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
83 MultiByteToWideChar( CP_UTF8
, 0, str
, -1, ret
, len
);
88 static inline void strfreeA( LPSTR str
)
90 HeapFree( GetProcessHeap(), 0, str
);
93 static inline void strfreeW( LPWSTR str
)
95 HeapFree( GetProcessHeap(), 0, str
);
98 static inline void strfreeU( char *str
)
100 HeapFree( GetProcessHeap(), 0, str
);
103 static inline DWORD
strarraylenA( LPSTR
*strarray
)
110 static inline DWORD
strarraylenW( LPWSTR
*strarray
)
112 LPWSTR
*p
= strarray
;
117 static inline DWORD
strarraylenU( char **strarray
)
124 static inline LPWSTR
*strarrayAtoW( LPSTR
*strarray
)
126 LPWSTR
*strarrayW
= NULL
;
131 size
= sizeof(WCHAR
*) * (strarraylenA( strarray
) + 1);
132 strarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
137 LPWSTR
*q
= strarrayW
;
139 while (*p
) *q
++ = strAtoW( *p
++ );
146 static inline LPSTR
*strarrayWtoA( LPWSTR
*strarray
)
148 LPSTR
*strarrayA
= NULL
;
153 size
= sizeof(LPSTR
) * (strarraylenW( strarray
) + 1);
154 strarrayA
= HeapAlloc( GetProcessHeap(), 0, size
);
158 LPWSTR
*p
= strarray
;
159 LPSTR
*q
= strarrayA
;
161 while (*p
) *q
++ = strWtoA( *p
++ );
168 static inline char **strarrayWtoU( LPWSTR
*strarray
)
170 char **strarrayU
= NULL
;
175 size
= sizeof(char*) * (strarraylenW( strarray
) + 1);
176 strarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
180 LPWSTR
*p
= strarray
;
181 char **q
= strarrayU
;
183 while (*p
) *q
++ = strWtoU( *p
++ );
190 static inline LPWSTR
*strarrayUtoW( char **strarray
)
192 LPWSTR
*strarrayW
= NULL
;
197 size
= sizeof(WCHAR
*) * (strarraylenU( strarray
) + 1);
198 strarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
203 LPWSTR
*q
= strarrayW
;
205 while (*p
) *q
++ = strUtoW( *p
++ );
212 static inline void strarrayfreeA( LPSTR
*strarray
)
217 while (*p
) strfreeA( *p
++ );
218 HeapFree( GetProcessHeap(), 0, strarray
);
222 static inline void strarrayfreeW( LPWSTR
*strarray
)
226 LPWSTR
*p
= strarray
;
227 while (*p
) strfreeW( *p
++ );
228 HeapFree( GetProcessHeap(), 0, strarray
);
232 static inline void strarrayfreeU( char **strarray
)
237 while (*p
) strfreeU( *p
++ );
238 HeapFree( GetProcessHeap(), 0, strarray
);
244 static inline struct berval
*bvdup( struct berval
*bv
)
246 struct berval
*berval
;
247 DWORD size
= sizeof(struct berval
) + bv
->bv_len
;
249 berval
= HeapAlloc( GetProcessHeap(), 0, size
);
252 char *val
= (char *)berval
+ sizeof(struct berval
);
254 berval
->bv_len
= bv
->bv_len
;
255 berval
->bv_val
= val
;
256 memcpy( val
, bv
->bv_val
, bv
->bv_len
);
261 static inline DWORD
bvarraylen( struct berval
**bv
)
263 struct berval
**p
= bv
;
268 static inline struct berval
**bvarraydup( struct berval
**bv
)
270 struct berval
**berval
= NULL
;
275 size
= sizeof(struct berval
*) * (bvarraylen( bv
) + 1);
276 berval
= HeapAlloc( GetProcessHeap(), 0, size
);
280 struct berval
**p
= bv
;
281 struct berval
**q
= berval
;
283 while (*p
) *q
++ = bvdup( *p
++ );
290 static inline void bvarrayfree( struct berval
**bv
)
292 struct berval
**p
= bv
;
293 while (*p
) HeapFree( GetProcessHeap(), 0, *p
++ );
294 HeapFree( GetProcessHeap(), 0, bv
);
297 static inline LDAPModW
*modAtoW( LDAPModA
*mod
)
301 modW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPModW
) );
304 modW
->mod_op
= mod
->mod_op
;
305 modW
->mod_type
= strAtoW( mod
->mod_type
);
307 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
308 modW
->mod_vals
.modv_bvals
= bvarraydup( mod
->mod_vals
.modv_bvals
);
310 modW
->mod_vals
.modv_strvals
= strarrayAtoW( mod
->mod_vals
.modv_strvals
);
315 static inline LDAPMod
*modWtoU( LDAPModW
*mod
)
319 modU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPMod
) );
322 modU
->mod_op
= mod
->mod_op
;
323 modU
->mod_type
= strWtoU( mod
->mod_type
);
325 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
326 modU
->mod_vals
.modv_bvals
= bvarraydup( mod
->mod_vals
.modv_bvals
);
328 modU
->mod_vals
.modv_strvals
= strarrayWtoU( mod
->mod_vals
.modv_strvals
);
333 static inline void modfreeW( LDAPModW
*mod
)
335 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
336 bvarrayfree( mod
->mod_vals
.modv_bvals
);
338 strarrayfreeW( mod
->mod_vals
.modv_strvals
);
339 HeapFree( GetProcessHeap(), 0, mod
);
342 static inline void modfreeU( LDAPMod
*mod
)
344 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
345 bvarrayfree( mod
->mod_vals
.modv_bvals
);
347 strarrayfreeU( mod
->mod_vals
.modv_strvals
);
348 HeapFree( GetProcessHeap(), 0, mod
);
351 static inline DWORD
modarraylenA( LDAPModA
**modarray
)
353 LDAPModA
**p
= modarray
;
358 static inline DWORD
modarraylenW( LDAPModW
**modarray
)
360 LDAPModW
**p
= modarray
;
365 static inline LDAPModW
**modarrayAtoW( LDAPModA
**modarray
)
367 LDAPModW
**modarrayW
= NULL
;
372 size
= sizeof(LDAPModW
*) * (modarraylenA( modarray
) + 1);
373 modarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
377 LDAPModA
**p
= modarray
;
378 LDAPModW
**q
= modarrayW
;
380 while (*p
) *q
++ = modAtoW( *p
++ );
387 static inline LDAPMod
**modarrayWtoU( LDAPModW
**modarray
)
389 LDAPMod
**modarrayU
= NULL
;
394 size
= sizeof(LDAPMod
*) * (modarraylenW( modarray
) + 1);
395 modarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
399 LDAPModW
**p
= modarray
;
400 LDAPMod
**q
= modarrayU
;
402 while (*p
) *q
++ = modWtoU( *p
++ );
409 static inline void modarrayfreeW( LDAPModW
**modarray
)
413 LDAPModW
**p
= modarray
;
414 while (*p
) modfreeW( *p
++ );
415 HeapFree( GetProcessHeap(), 0, modarray
);
419 static inline void modarrayfreeU( LDAPMod
**modarray
)
423 LDAPMod
**p
= modarray
;
424 while (*p
) modfreeU( *p
++ );
425 HeapFree( GetProcessHeap(), 0, modarray
);
429 static inline LDAPControlW
*controlAtoW( LDAPControlA
*control
)
431 LDAPControlW
*controlW
;
432 DWORD len
= control
->ldctl_value
.bv_len
;
435 if (control
->ldctl_value
.bv_val
)
437 val
= HeapAlloc( GetProcessHeap(), 0, len
);
438 if (!val
) return NULL
;
439 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
442 controlW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW
) );
445 HeapFree( GetProcessHeap(), 0, val
);
449 controlW
->ldctl_oid
= strAtoW( control
->ldctl_oid
);
450 controlW
->ldctl_value
.bv_len
= len
;
451 controlW
->ldctl_value
.bv_val
= val
;
452 controlW
->ldctl_iscritical
= control
->ldctl_iscritical
;
457 static inline LDAPControlA
*controlWtoA( LDAPControlW
*control
)
459 LDAPControlA
*controlA
;
460 DWORD len
= control
->ldctl_value
.bv_len
;
463 if (control
->ldctl_value
.bv_val
)
465 val
= HeapAlloc( GetProcessHeap(), 0, len
);
466 if (!val
) return NULL
;
467 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
470 controlA
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlA
) );
473 HeapFree( GetProcessHeap(), 0, val
);
477 controlA
->ldctl_oid
= strWtoA( control
->ldctl_oid
);
478 controlA
->ldctl_value
.bv_len
= len
;
479 controlA
->ldctl_value
.bv_val
= val
;
480 controlA
->ldctl_iscritical
= control
->ldctl_iscritical
;
485 static inline LDAPControl
*controlWtoU( LDAPControlW
*control
)
487 LDAPControl
*controlU
;
488 DWORD len
= control
->ldctl_value
.bv_len
;
491 if (control
->ldctl_value
.bv_val
)
493 val
= HeapAlloc( GetProcessHeap(), 0, len
);
494 if (!val
) return NULL
;
495 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
498 controlU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControl
) );
501 HeapFree( GetProcessHeap(), 0, val
);
505 controlU
->ldctl_oid
= strWtoU( control
->ldctl_oid
);
506 controlU
->ldctl_value
.bv_len
= len
;
507 controlU
->ldctl_value
.bv_val
= val
;
508 controlU
->ldctl_iscritical
= control
->ldctl_iscritical
;
513 static inline LDAPControlW
*controlUtoW( LDAPControl
*control
)
515 LDAPControlW
*controlW
;
516 DWORD len
= control
->ldctl_value
.bv_len
;
519 if (control
->ldctl_value
.bv_val
)
521 val
= HeapAlloc( GetProcessHeap(), 0, len
);
522 if (!val
) return NULL
;
523 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
526 controlW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW
) );
529 HeapFree( GetProcessHeap(), 0, val
);
533 controlW
->ldctl_oid
= strUtoW( control
->ldctl_oid
);
534 controlW
->ldctl_value
.bv_len
= len
;
535 controlW
->ldctl_value
.bv_val
= val
;
536 controlW
->ldctl_iscritical
= control
->ldctl_iscritical
;
541 static inline void controlfreeA( LDAPControlA
*control
)
545 strfreeA( control
->ldctl_oid
);
546 HeapFree( GetProcessHeap(), 0, control
->ldctl_value
.bv_val
);
547 HeapFree( GetProcessHeap(), 0, control
);
551 static inline void controlfreeW( LDAPControlW
*control
)
555 strfreeW( control
->ldctl_oid
);
556 HeapFree( GetProcessHeap(), 0, control
->ldctl_value
.bv_val
);
557 HeapFree( GetProcessHeap(), 0, control
);
561 static inline void controlfreeU( LDAPControl
*control
)
565 strfreeU( control
->ldctl_oid
);
566 HeapFree( GetProcessHeap(), 0, control
->ldctl_value
.bv_val
);
567 HeapFree( GetProcessHeap(), 0, control
);
571 static inline DWORD
controlarraylenA( LDAPControlA
**controlarray
)
573 LDAPControlA
**p
= controlarray
;
575 return p
- controlarray
;
578 static inline DWORD
controlarraylenW( LDAPControlW
**controlarray
)
580 LDAPControlW
**p
= controlarray
;
582 return p
- controlarray
;
585 static inline DWORD
controlarraylenU( LDAPControl
**controlarray
)
587 LDAPControl
**p
= controlarray
;
589 return p
- controlarray
;
592 static inline LDAPControlW
**controlarrayAtoW( LDAPControlA
**controlarray
)
594 LDAPControlW
**controlarrayW
= NULL
;
599 size
= sizeof(LDAPControlW
*) * (controlarraylenA( controlarray
) + 1);
600 controlarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
604 LDAPControlA
**p
= controlarray
;
605 LDAPControlW
**q
= controlarrayW
;
607 while (*p
) *q
++ = controlAtoW( *p
++ );
611 return controlarrayW
;
614 static inline LDAPControlA
**controlarrayWtoA( LDAPControlW
**controlarray
)
616 LDAPControlA
**controlarrayA
= NULL
;
621 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
622 controlarrayA
= HeapAlloc( GetProcessHeap(), 0, size
);
626 LDAPControlW
**p
= controlarray
;
627 LDAPControlA
**q
= controlarrayA
;
629 while (*p
) *q
++ = controlWtoA( *p
++ );
633 return controlarrayA
;
636 static inline LDAPControl
**controlarrayWtoU( LDAPControlW
**controlarray
)
638 LDAPControl
**controlarrayU
= NULL
;
643 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
644 controlarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
648 LDAPControlW
**p
= controlarray
;
649 LDAPControl
**q
= controlarrayU
;
651 while (*p
) *q
++ = controlWtoU( *p
++ );
655 return controlarrayU
;
658 static inline LDAPControlW
**controlarrayUtoW( LDAPControl
**controlarray
)
660 LDAPControlW
**controlarrayW
= NULL
;
665 size
= sizeof(LDAPControlW
*) * (controlarraylenU( controlarray
) + 1);
666 controlarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
670 LDAPControl
**p
= controlarray
;
671 LDAPControlW
**q
= controlarrayW
;
673 while (*p
) *q
++ = controlUtoW( *p
++ );
677 return controlarrayW
;
680 static inline void controlarrayfreeA( LDAPControlA
**controlarray
)
684 LDAPControlA
**p
= controlarray
;
685 while (*p
) controlfreeA( *p
++ );
686 HeapFree( GetProcessHeap(), 0, controlarray
);
690 static inline void controlarrayfreeW( LDAPControlW
**controlarray
)
694 LDAPControlW
**p
= controlarray
;
695 while (*p
) controlfreeW( *p
++ );
696 HeapFree( GetProcessHeap(), 0, controlarray
);
700 static inline void controlarrayfreeU( LDAPControl
**controlarray
)
704 LDAPControl
**p
= controlarray
;
705 while (*p
) controlfreeU( *p
++ );
706 HeapFree( GetProcessHeap(), 0, controlarray
);
710 static inline LDAPSortKeyW
*sortkeyAtoW( LDAPSortKeyA
*sortkey
)
712 LDAPSortKeyW
*sortkeyW
;
714 sortkeyW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyW
) );
717 sortkeyW
->sk_attrtype
= strAtoW( sortkey
->sk_attrtype
);
718 sortkeyW
->sk_matchruleoid
= strAtoW( sortkey
->sk_matchruleoid
);
719 sortkeyW
->sk_reverseorder
= sortkey
->sk_reverseorder
;
724 static inline LDAPSortKeyA
*sortkeyWtoA( LDAPSortKeyW
*sortkey
)
726 LDAPSortKeyA
*sortkeyA
;
728 sortkeyA
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyA
) );
731 sortkeyA
->sk_attrtype
= strWtoA( sortkey
->sk_attrtype
);
732 sortkeyA
->sk_matchruleoid
= strWtoA( sortkey
->sk_matchruleoid
);
733 sortkeyA
->sk_reverseorder
= sortkey
->sk_reverseorder
;
738 static inline LDAPSortKey
*sortkeyWtoU( LDAPSortKeyW
*sortkey
)
740 LDAPSortKey
*sortkeyU
;
742 sortkeyU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKey
) );
745 sortkeyU
->attributeType
= strWtoU( sortkey
->sk_attrtype
);
746 sortkeyU
->orderingRule
= strWtoU( sortkey
->sk_matchruleoid
);
747 sortkeyU
->reverseOrder
= sortkey
->sk_reverseorder
;
752 static inline void sortkeyfreeA( LDAPSortKeyA
*sortkey
)
756 strfreeA( sortkey
->sk_attrtype
);
757 strfreeA( sortkey
->sk_matchruleoid
);
758 HeapFree( GetProcessHeap(), 0, sortkey
);
762 static inline void sortkeyfreeW( LDAPSortKeyW
*sortkey
)
766 strfreeW( sortkey
->sk_attrtype
);
767 strfreeW( sortkey
->sk_matchruleoid
);
768 HeapFree( GetProcessHeap(), 0, sortkey
);
772 static inline void sortkeyfreeU( LDAPSortKey
*sortkey
)
776 strfreeU( sortkey
->attributeType
);
777 strfreeU( sortkey
->orderingRule
);
778 HeapFree( GetProcessHeap(), 0, sortkey
);
782 static inline DWORD
sortkeyarraylenA( LDAPSortKeyA
**sortkeyarray
)
784 LDAPSortKeyA
**p
= sortkeyarray
;
786 return p
- sortkeyarray
;
789 static inline DWORD
sortkeyarraylenW( LDAPSortKeyW
**sortkeyarray
)
791 LDAPSortKeyW
**p
= sortkeyarray
;
793 return p
- sortkeyarray
;
796 static inline LDAPSortKeyW
**sortkeyarrayAtoW( LDAPSortKeyA
**sortkeyarray
)
798 LDAPSortKeyW
**sortkeyarrayW
= NULL
;
803 size
= sizeof(LDAPSortKeyW
*) * (sortkeyarraylenA( sortkeyarray
) + 1);
804 sortkeyarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
808 LDAPSortKeyA
**p
= sortkeyarray
;
809 LDAPSortKeyW
**q
= sortkeyarrayW
;
811 while (*p
) *q
++ = sortkeyAtoW( *p
++ );
815 return sortkeyarrayW
;
818 static inline LDAPSortKey
**sortkeyarrayWtoU( LDAPSortKeyW
**sortkeyarray
)
820 LDAPSortKey
**sortkeyarrayU
= NULL
;
825 size
= sizeof(LDAPSortKey
*) * (sortkeyarraylenW( sortkeyarray
) + 1);
826 sortkeyarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
830 LDAPSortKeyW
**p
= sortkeyarray
;
831 LDAPSortKey
**q
= sortkeyarrayU
;
833 while (*p
) *q
++ = sortkeyWtoU( *p
++ );
837 return sortkeyarrayU
;
840 static inline void sortkeyarrayfreeW( LDAPSortKeyW
**sortkeyarray
)
844 LDAPSortKeyW
**p
= sortkeyarray
;
845 while (*p
) sortkeyfreeW( *p
++ );
846 HeapFree( GetProcessHeap(), 0, sortkeyarray
);
850 static inline void sortkeyarrayfreeU( LDAPSortKey
**sortkeyarray
)
854 LDAPSortKey
**p
= sortkeyarray
;
855 while (*p
) sortkeyfreeU( *p
++ );
856 HeapFree( GetProcessHeap(), 0, sortkeyarray
);
859 #endif /* HAVE_LDAP */