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 #include <wine/unicode.h>
25 extern HINSTANCE hwldap32 DECLSPEC_HIDDEN
;
27 ULONG
map_error( int ) DECLSPEC_HIDDEN
;
29 /* A set of helper functions to convert LDAP data structures
30 * to and from ansi (A), wide character (W) and utf8 (U) encodings.
33 static inline char *strdupU( const char *src
)
37 if (!src
) return NULL
;
38 dst
= HeapAlloc( GetProcessHeap(), 0, (strlen( src
) + 1) * sizeof(char) );
44 static inline WCHAR
*strdupW( const WCHAR
*src
)
48 if (!src
) return NULL
;
49 dst
= HeapAlloc( GetProcessHeap(), 0, (strlenW( src
) + 1) * sizeof(WCHAR
) );
55 static inline LPWSTR
strAtoW( LPCSTR str
)
60 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
61 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
62 MultiByteToWideChar( CP_ACP
, 0, str
, -1, ret
, len
);
67 static inline LPSTR
strWtoA( LPCWSTR str
)
72 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
73 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
)))
74 WideCharToMultiByte( CP_ACP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
79 static inline char *strWtoU( LPCWSTR str
)
84 DWORD len
= WideCharToMultiByte( CP_UTF8
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
85 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
)))
86 WideCharToMultiByte( CP_UTF8
, 0, str
, -1, ret
, len
, NULL
, NULL
);
91 static inline LPWSTR
strUtoW( char *str
)
96 DWORD len
= MultiByteToWideChar( CP_UTF8
, 0, str
, -1, NULL
, 0 );
97 if ((ret
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
98 MultiByteToWideChar( CP_UTF8
, 0, str
, -1, ret
, len
);
103 static inline void strfreeA( LPSTR str
)
105 HeapFree( GetProcessHeap(), 0, str
);
108 static inline void strfreeW( LPWSTR str
)
110 HeapFree( GetProcessHeap(), 0, str
);
113 static inline void strfreeU( char *str
)
115 HeapFree( GetProcessHeap(), 0, str
);
118 static inline DWORD
strarraylenA( LPSTR
*strarray
)
125 static inline DWORD
strarraylenW( LPWSTR
*strarray
)
127 LPWSTR
*p
= strarray
;
132 static inline DWORD
strarraylenU( char **strarray
)
139 static inline LPWSTR
*strarrayAtoW( LPSTR
*strarray
)
141 LPWSTR
*strarrayW
= NULL
;
146 size
= sizeof(WCHAR
*) * (strarraylenA( strarray
) + 1);
147 strarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
152 LPWSTR
*q
= strarrayW
;
154 while (*p
) *q
++ = strAtoW( *p
++ );
161 static inline LPSTR
*strarrayWtoA( LPWSTR
*strarray
)
163 LPSTR
*strarrayA
= NULL
;
168 size
= sizeof(LPSTR
) * (strarraylenW( strarray
) + 1);
169 strarrayA
= HeapAlloc( GetProcessHeap(), 0, size
);
173 LPWSTR
*p
= strarray
;
174 LPSTR
*q
= strarrayA
;
176 while (*p
) *q
++ = strWtoA( *p
++ );
183 static inline char **strarrayWtoU( LPWSTR
*strarray
)
185 char **strarrayU
= NULL
;
190 size
= sizeof(char*) * (strarraylenW( strarray
) + 1);
191 strarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
195 LPWSTR
*p
= strarray
;
196 char **q
= strarrayU
;
198 while (*p
) *q
++ = strWtoU( *p
++ );
205 static inline LPWSTR
*strarrayUtoW( char **strarray
)
207 LPWSTR
*strarrayW
= NULL
;
212 size
= sizeof(WCHAR
*) * (strarraylenU( strarray
) + 1);
213 strarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
218 LPWSTR
*q
= strarrayW
;
220 while (*p
) *q
++ = strUtoW( *p
++ );
227 static inline void strarrayfreeA( LPSTR
*strarray
)
232 while (*p
) strfreeA( *p
++ );
233 HeapFree( GetProcessHeap(), 0, strarray
);
237 static inline void strarrayfreeW( LPWSTR
*strarray
)
241 LPWSTR
*p
= strarray
;
242 while (*p
) strfreeW( *p
++ );
243 HeapFree( GetProcessHeap(), 0, strarray
);
247 static inline void strarrayfreeU( char **strarray
)
252 while (*p
) strfreeU( *p
++ );
253 HeapFree( GetProcessHeap(), 0, strarray
);
259 static inline struct berval
*bvdup( struct berval
*bv
)
261 struct berval
*berval
;
262 DWORD size
= sizeof(struct berval
) + bv
->bv_len
;
264 berval
= HeapAlloc( GetProcessHeap(), 0, size
);
267 char *val
= (char *)berval
+ sizeof(struct berval
);
269 berval
->bv_len
= bv
->bv_len
;
270 berval
->bv_val
= val
;
271 memcpy( val
, bv
->bv_val
, bv
->bv_len
);
276 static inline DWORD
bvarraylen( struct berval
**bv
)
278 struct berval
**p
= bv
;
283 static inline struct berval
**bvarraydup( struct berval
**bv
)
285 struct berval
**berval
= NULL
;
290 size
= sizeof(struct berval
*) * (bvarraylen( bv
) + 1);
291 berval
= HeapAlloc( GetProcessHeap(), 0, size
);
295 struct berval
**p
= bv
;
296 struct berval
**q
= berval
;
298 while (*p
) *q
++ = bvdup( *p
++ );
305 static inline void bvarrayfree( struct berval
**bv
)
307 struct berval
**p
= bv
;
308 while (*p
) HeapFree( GetProcessHeap(), 0, *p
++ );
309 HeapFree( GetProcessHeap(), 0, bv
);
312 static inline LDAPModW
*modAtoW( LDAPModA
*mod
)
316 modW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPModW
) );
319 modW
->mod_op
= mod
->mod_op
;
320 modW
->mod_type
= strAtoW( mod
->mod_type
);
322 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
323 modW
->mod_vals
.modv_bvals
= bvarraydup( mod
->mod_vals
.modv_bvals
);
325 modW
->mod_vals
.modv_strvals
= strarrayAtoW( mod
->mod_vals
.modv_strvals
);
330 static inline LDAPMod
*modWtoU( LDAPModW
*mod
)
334 modU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPMod
) );
337 modU
->mod_op
= mod
->mod_op
;
338 modU
->mod_type
= strWtoU( mod
->mod_type
);
340 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
341 modU
->mod_vals
.modv_bvals
= bvarraydup( mod
->mod_vals
.modv_bvals
);
343 modU
->mod_vals
.modv_strvals
= strarrayWtoU( mod
->mod_vals
.modv_strvals
);
348 static inline void modfreeW( LDAPModW
*mod
)
350 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
351 bvarrayfree( mod
->mod_vals
.modv_bvals
);
353 strarrayfreeW( mod
->mod_vals
.modv_strvals
);
354 HeapFree( GetProcessHeap(), 0, mod
);
357 static inline void modfreeU( LDAPMod
*mod
)
359 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
360 bvarrayfree( mod
->mod_vals
.modv_bvals
);
362 strarrayfreeU( mod
->mod_vals
.modv_strvals
);
363 HeapFree( GetProcessHeap(), 0, mod
);
366 static inline DWORD
modarraylenA( LDAPModA
**modarray
)
368 LDAPModA
**p
= modarray
;
373 static inline DWORD
modarraylenW( LDAPModW
**modarray
)
375 LDAPModW
**p
= modarray
;
380 static inline LDAPModW
**modarrayAtoW( LDAPModA
**modarray
)
382 LDAPModW
**modarrayW
= NULL
;
387 size
= sizeof(LDAPModW
*) * (modarraylenA( modarray
) + 1);
388 modarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
392 LDAPModA
**p
= modarray
;
393 LDAPModW
**q
= modarrayW
;
395 while (*p
) *q
++ = modAtoW( *p
++ );
402 static inline LDAPMod
**modarrayWtoU( LDAPModW
**modarray
)
404 LDAPMod
**modarrayU
= NULL
;
409 size
= sizeof(LDAPMod
*) * (modarraylenW( modarray
) + 1);
410 modarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
414 LDAPModW
**p
= modarray
;
415 LDAPMod
**q
= modarrayU
;
417 while (*p
) *q
++ = modWtoU( *p
++ );
424 static inline void modarrayfreeW( LDAPModW
**modarray
)
428 LDAPModW
**p
= modarray
;
429 while (*p
) modfreeW( *p
++ );
430 HeapFree( GetProcessHeap(), 0, modarray
);
434 static inline void modarrayfreeU( LDAPMod
**modarray
)
438 LDAPMod
**p
= modarray
;
439 while (*p
) modfreeU( *p
++ );
440 HeapFree( GetProcessHeap(), 0, modarray
);
444 static inline LDAPControlW
*controlAtoW( LDAPControlA
*control
)
446 LDAPControlW
*controlW
;
447 DWORD len
= control
->ldctl_value
.bv_len
;
450 if (control
->ldctl_value
.bv_val
)
452 val
= HeapAlloc( GetProcessHeap(), 0, len
);
453 if (!val
) return NULL
;
454 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
457 controlW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW
) );
460 HeapFree( GetProcessHeap(), 0, val
);
464 controlW
->ldctl_oid
= strAtoW( control
->ldctl_oid
);
465 controlW
->ldctl_value
.bv_len
= len
;
466 controlW
->ldctl_value
.bv_val
= val
;
467 controlW
->ldctl_iscritical
= control
->ldctl_iscritical
;
472 static inline LDAPControlA
*controlWtoA( LDAPControlW
*control
)
474 LDAPControlA
*controlA
;
475 DWORD len
= control
->ldctl_value
.bv_len
;
478 if (control
->ldctl_value
.bv_val
)
480 val
= HeapAlloc( GetProcessHeap(), 0, len
);
481 if (!val
) return NULL
;
482 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
485 controlA
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlA
) );
488 HeapFree( GetProcessHeap(), 0, val
);
492 controlA
->ldctl_oid
= strWtoA( control
->ldctl_oid
);
493 controlA
->ldctl_value
.bv_len
= len
;
494 controlA
->ldctl_value
.bv_val
= val
;
495 controlA
->ldctl_iscritical
= control
->ldctl_iscritical
;
500 static inline LDAPControl
*controlWtoU( LDAPControlW
*control
)
502 LDAPControl
*controlU
;
503 DWORD len
= control
->ldctl_value
.bv_len
;
506 if (control
->ldctl_value
.bv_val
)
508 val
= HeapAlloc( GetProcessHeap(), 0, len
);
509 if (!val
) return NULL
;
510 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
513 controlU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControl
) );
516 HeapFree( GetProcessHeap(), 0, val
);
520 controlU
->ldctl_oid
= strWtoU( control
->ldctl_oid
);
521 controlU
->ldctl_value
.bv_len
= len
;
522 controlU
->ldctl_value
.bv_val
= val
;
523 controlU
->ldctl_iscritical
= control
->ldctl_iscritical
;
528 static inline LDAPControlW
*controlUtoW( LDAPControl
*control
)
530 LDAPControlW
*controlW
;
531 DWORD len
= control
->ldctl_value
.bv_len
;
534 if (control
->ldctl_value
.bv_val
)
536 val
= HeapAlloc( GetProcessHeap(), 0, len
);
537 if (!val
) return NULL
;
538 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
541 controlW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW
) );
544 HeapFree( GetProcessHeap(), 0, val
);
548 controlW
->ldctl_oid
= strUtoW( control
->ldctl_oid
);
549 controlW
->ldctl_value
.bv_len
= len
;
550 controlW
->ldctl_value
.bv_val
= val
;
551 controlW
->ldctl_iscritical
= control
->ldctl_iscritical
;
556 static inline void controlfreeA( LDAPControlA
*control
)
560 strfreeA( control
->ldctl_oid
);
561 HeapFree( GetProcessHeap(), 0, control
->ldctl_value
.bv_val
);
562 HeapFree( GetProcessHeap(), 0, control
);
566 static inline void controlfreeW( LDAPControlW
*control
)
570 strfreeW( control
->ldctl_oid
);
571 HeapFree( GetProcessHeap(), 0, control
->ldctl_value
.bv_val
);
572 HeapFree( GetProcessHeap(), 0, control
);
576 static inline void controlfreeU( LDAPControl
*control
)
580 strfreeU( control
->ldctl_oid
);
581 HeapFree( GetProcessHeap(), 0, control
->ldctl_value
.bv_val
);
582 HeapFree( GetProcessHeap(), 0, control
);
586 static inline DWORD
controlarraylenA( LDAPControlA
**controlarray
)
588 LDAPControlA
**p
= controlarray
;
590 return p
- controlarray
;
593 static inline DWORD
controlarraylenW( LDAPControlW
**controlarray
)
595 LDAPControlW
**p
= controlarray
;
597 return p
- controlarray
;
600 static inline DWORD
controlarraylenU( LDAPControl
**controlarray
)
602 LDAPControl
**p
= controlarray
;
604 return p
- controlarray
;
607 static inline LDAPControlW
**controlarrayAtoW( LDAPControlA
**controlarray
)
609 LDAPControlW
**controlarrayW
= NULL
;
614 size
= sizeof(LDAPControlW
*) * (controlarraylenA( controlarray
) + 1);
615 controlarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
619 LDAPControlA
**p
= controlarray
;
620 LDAPControlW
**q
= controlarrayW
;
622 while (*p
) *q
++ = controlAtoW( *p
++ );
626 return controlarrayW
;
629 static inline LDAPControlA
**controlarrayWtoA( LDAPControlW
**controlarray
)
631 LDAPControlA
**controlarrayA
= NULL
;
636 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
637 controlarrayA
= HeapAlloc( GetProcessHeap(), 0, size
);
641 LDAPControlW
**p
= controlarray
;
642 LDAPControlA
**q
= controlarrayA
;
644 while (*p
) *q
++ = controlWtoA( *p
++ );
648 return controlarrayA
;
651 static inline LDAPControl
**controlarrayWtoU( LDAPControlW
**controlarray
)
653 LDAPControl
**controlarrayU
= NULL
;
658 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
659 controlarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
663 LDAPControlW
**p
= controlarray
;
664 LDAPControl
**q
= controlarrayU
;
666 while (*p
) *q
++ = controlWtoU( *p
++ );
670 return controlarrayU
;
673 static inline LDAPControlW
**controlarrayUtoW( LDAPControl
**controlarray
)
675 LDAPControlW
**controlarrayW
= NULL
;
680 size
= sizeof(LDAPControlW
*) * (controlarraylenU( controlarray
) + 1);
681 controlarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
685 LDAPControl
**p
= controlarray
;
686 LDAPControlW
**q
= controlarrayW
;
688 while (*p
) *q
++ = controlUtoW( *p
++ );
692 return controlarrayW
;
695 static inline void controlarrayfreeA( LDAPControlA
**controlarray
)
699 LDAPControlA
**p
= controlarray
;
700 while (*p
) controlfreeA( *p
++ );
701 HeapFree( GetProcessHeap(), 0, controlarray
);
705 static inline void controlarrayfreeW( LDAPControlW
**controlarray
)
709 LDAPControlW
**p
= controlarray
;
710 while (*p
) controlfreeW( *p
++ );
711 HeapFree( GetProcessHeap(), 0, controlarray
);
715 static inline void controlarrayfreeU( LDAPControl
**controlarray
)
719 LDAPControl
**p
= controlarray
;
720 while (*p
) controlfreeU( *p
++ );
721 HeapFree( GetProcessHeap(), 0, controlarray
);
725 static inline LDAPSortKeyW
*sortkeyAtoW( LDAPSortKeyA
*sortkey
)
727 LDAPSortKeyW
*sortkeyW
;
729 sortkeyW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyW
) );
732 sortkeyW
->sk_attrtype
= strAtoW( sortkey
->sk_attrtype
);
733 sortkeyW
->sk_matchruleoid
= strAtoW( sortkey
->sk_matchruleoid
);
734 sortkeyW
->sk_reverseorder
= sortkey
->sk_reverseorder
;
739 static inline LDAPSortKeyA
*sortkeyWtoA( LDAPSortKeyW
*sortkey
)
741 LDAPSortKeyA
*sortkeyA
;
743 sortkeyA
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyA
) );
746 sortkeyA
->sk_attrtype
= strWtoA( sortkey
->sk_attrtype
);
747 sortkeyA
->sk_matchruleoid
= strWtoA( sortkey
->sk_matchruleoid
);
748 sortkeyA
->sk_reverseorder
= sortkey
->sk_reverseorder
;
753 static inline LDAPSortKey
*sortkeyWtoU( LDAPSortKeyW
*sortkey
)
755 LDAPSortKey
*sortkeyU
;
757 sortkeyU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKey
) );
760 sortkeyU
->attributeType
= strWtoU( sortkey
->sk_attrtype
);
761 sortkeyU
->orderingRule
= strWtoU( sortkey
->sk_matchruleoid
);
762 sortkeyU
->reverseOrder
= sortkey
->sk_reverseorder
;
767 static inline void sortkeyfreeA( LDAPSortKeyA
*sortkey
)
771 strfreeA( sortkey
->sk_attrtype
);
772 strfreeA( sortkey
->sk_matchruleoid
);
773 HeapFree( GetProcessHeap(), 0, sortkey
);
777 static inline void sortkeyfreeW( LDAPSortKeyW
*sortkey
)
781 strfreeW( sortkey
->sk_attrtype
);
782 strfreeW( sortkey
->sk_matchruleoid
);
783 HeapFree( GetProcessHeap(), 0, sortkey
);
787 static inline void sortkeyfreeU( LDAPSortKey
*sortkey
)
791 strfreeU( sortkey
->attributeType
);
792 strfreeU( sortkey
->orderingRule
);
793 HeapFree( GetProcessHeap(), 0, sortkey
);
797 static inline DWORD
sortkeyarraylenA( LDAPSortKeyA
**sortkeyarray
)
799 LDAPSortKeyA
**p
= sortkeyarray
;
801 return p
- sortkeyarray
;
804 static inline DWORD
sortkeyarraylenW( LDAPSortKeyW
**sortkeyarray
)
806 LDAPSortKeyW
**p
= sortkeyarray
;
808 return p
- sortkeyarray
;
811 static inline LDAPSortKeyW
**sortkeyarrayAtoW( LDAPSortKeyA
**sortkeyarray
)
813 LDAPSortKeyW
**sortkeyarrayW
= NULL
;
818 size
= sizeof(LDAPSortKeyW
*) * (sortkeyarraylenA( sortkeyarray
) + 1);
819 sortkeyarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
823 LDAPSortKeyA
**p
= sortkeyarray
;
824 LDAPSortKeyW
**q
= sortkeyarrayW
;
826 while (*p
) *q
++ = sortkeyAtoW( *p
++ );
830 return sortkeyarrayW
;
833 static inline LDAPSortKey
**sortkeyarrayWtoU( LDAPSortKeyW
**sortkeyarray
)
835 LDAPSortKey
**sortkeyarrayU
= NULL
;
840 size
= sizeof(LDAPSortKey
*) * (sortkeyarraylenW( sortkeyarray
) + 1);
841 sortkeyarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
845 LDAPSortKeyW
**p
= sortkeyarray
;
846 LDAPSortKey
**q
= sortkeyarrayU
;
848 while (*p
) *q
++ = sortkeyWtoU( *p
++ );
852 return sortkeyarrayU
;
855 static inline void sortkeyarrayfreeW( LDAPSortKeyW
**sortkeyarray
)
859 LDAPSortKeyW
**p
= sortkeyarray
;
860 while (*p
) sortkeyfreeW( *p
++ );
861 HeapFree( GetProcessHeap(), 0, sortkeyarray
);
865 static inline void sortkeyarrayfreeU( LDAPSortKey
**sortkeyarray
)
869 LDAPSortKey
**p
= sortkeyarray
;
870 while (*p
) sortkeyfreeU( *p
++ );
871 HeapFree( GetProcessHeap(), 0, sortkeyarray
);
874 #endif /* HAVE_LDAP */