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/heap.h"
24 #include "wine/unicode.h"
26 extern HINSTANCE hwldap32 DECLSPEC_HIDDEN
;
28 ULONG
map_error( int ) DECLSPEC_HIDDEN
;
30 /* A set of helper functions to convert LDAP data structures
31 * to and from ansi (A), wide character (W) and utf8 (U) encodings.
34 static inline char *strdupU( const char *src
)
37 if (!src
) return NULL
;
38 if ((dst
= heap_alloc( (strlen( src
) + 1) * sizeof(char) ))) strcpy( dst
, src
);
42 static inline WCHAR
*strdupW( const WCHAR
*src
)
45 if (!src
) return NULL
;
46 if ((dst
= heap_alloc( (strlenW( src
) + 1) * sizeof(WCHAR
) ))) strcpyW( dst
, src
);
50 static inline LPWSTR
strAtoW( LPCSTR str
)
55 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
56 if ((ret
= heap_alloc( len
* sizeof(WCHAR
) )))
57 MultiByteToWideChar( CP_ACP
, 0, str
, -1, ret
, len
);
62 static inline LPSTR
strWtoA( LPCWSTR str
)
67 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
68 if ((ret
= heap_alloc( len
)))
69 WideCharToMultiByte( CP_ACP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
74 static inline char *strWtoU( LPCWSTR str
)
79 DWORD len
= WideCharToMultiByte( CP_UTF8
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
80 if ((ret
= heap_alloc( len
)))
81 WideCharToMultiByte( CP_UTF8
, 0, str
, -1, ret
, len
, NULL
, NULL
);
86 static inline LPWSTR
strUtoW( char *str
)
91 DWORD len
= MultiByteToWideChar( CP_UTF8
, 0, str
, -1, NULL
, 0 );
92 if ((ret
= heap_alloc( len
* sizeof(WCHAR
) )))
93 MultiByteToWideChar( CP_UTF8
, 0, str
, -1, ret
, len
);
98 static inline void strfreeA( LPSTR str
)
103 static inline void strfreeW( LPWSTR str
)
108 static inline void strfreeU( char *str
)
113 static inline DWORD
strarraylenA( LPSTR
*strarray
)
120 static inline DWORD
strarraylenW( LPWSTR
*strarray
)
122 LPWSTR
*p
= strarray
;
127 static inline DWORD
strarraylenU( char **strarray
)
134 static inline LPWSTR
*strarrayAtoW( LPSTR
*strarray
)
136 LPWSTR
*strarrayW
= NULL
;
141 size
= sizeof(WCHAR
*) * (strarraylenA( strarray
) + 1);
142 if ((strarrayW
= heap_alloc( size
)))
145 LPWSTR
*q
= strarrayW
;
147 while (*p
) *q
++ = strAtoW( *p
++ );
154 static inline LPSTR
*strarrayWtoA( LPWSTR
*strarray
)
156 LPSTR
*strarrayA
= NULL
;
161 size
= sizeof(LPSTR
) * (strarraylenW( strarray
) + 1);
162 if ((strarrayA
= heap_alloc( size
)))
164 LPWSTR
*p
= strarray
;
165 LPSTR
*q
= strarrayA
;
167 while (*p
) *q
++ = strWtoA( *p
++ );
174 static inline char **strarrayWtoU( LPWSTR
*strarray
)
176 char **strarrayU
= NULL
;
181 size
= sizeof(char*) * (strarraylenW( strarray
) + 1);
182 if ((strarrayU
= heap_alloc( size
)))
184 LPWSTR
*p
= strarray
;
185 char **q
= strarrayU
;
187 while (*p
) *q
++ = strWtoU( *p
++ );
194 static inline LPWSTR
*strarrayUtoW( char **strarray
)
196 LPWSTR
*strarrayW
= NULL
;
201 size
= sizeof(WCHAR
*) * (strarraylenU( strarray
) + 1);
202 if ((strarrayW
= heap_alloc( size
)))
205 LPWSTR
*q
= strarrayW
;
207 while (*p
) *q
++ = strUtoW( *p
++ );
214 static inline void strarrayfreeA( LPSTR
*strarray
)
219 while (*p
) strfreeA( *p
++ );
220 heap_free( strarray
);
224 static inline void strarrayfreeW( LPWSTR
*strarray
)
228 LPWSTR
*p
= strarray
;
229 while (*p
) strfreeW( *p
++ );
230 heap_free( strarray
);
234 static inline void strarrayfreeU( char **strarray
)
239 while (*p
) strfreeU( *p
++ );
240 heap_free( strarray
);
246 static inline struct berval
*bvdup( struct berval
*bv
)
248 struct berval
*berval
;
249 DWORD size
= sizeof(struct berval
) + bv
->bv_len
;
251 if ((berval
= heap_alloc( size
)))
253 char *val
= (char *)berval
+ sizeof(struct berval
);
255 berval
->bv_len
= bv
->bv_len
;
256 berval
->bv_val
= val
;
257 memcpy( val
, bv
->bv_val
, bv
->bv_len
);
262 static inline DWORD
bvarraylen( struct berval
**bv
)
264 struct berval
**p
= bv
;
269 static inline struct berval
**bvarraydup( struct berval
**bv
)
271 struct berval
**berval
= NULL
;
276 size
= sizeof(struct berval
*) * (bvarraylen( bv
) + 1);
277 if ((berval
= heap_alloc( size
)))
279 struct berval
**p
= bv
;
280 struct berval
**q
= berval
;
282 while (*p
) *q
++ = bvdup( *p
++ );
289 static inline void bvarrayfree( struct berval
**bv
)
291 struct berval
**p
= bv
;
292 while (*p
) heap_free( *p
++ );
296 static inline LDAPModW
*modAtoW( LDAPModA
*mod
)
300 if ((modW
= heap_alloc( sizeof(LDAPModW
) )))
302 modW
->mod_op
= mod
->mod_op
;
303 modW
->mod_type
= strAtoW( mod
->mod_type
);
305 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
306 modW
->mod_vals
.modv_bvals
= bvarraydup( mod
->mod_vals
.modv_bvals
);
308 modW
->mod_vals
.modv_strvals
= strarrayAtoW( mod
->mod_vals
.modv_strvals
);
313 static inline LDAPMod
*modWtoU( LDAPModW
*mod
)
317 if ((modU
= heap_alloc( sizeof(LDAPMod
) )))
319 modU
->mod_op
= mod
->mod_op
;
320 modU
->mod_type
= strWtoU( mod
->mod_type
);
322 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
323 modU
->mod_vals
.modv_bvals
= bvarraydup( mod
->mod_vals
.modv_bvals
);
325 modU
->mod_vals
.modv_strvals
= strarrayWtoU( mod
->mod_vals
.modv_strvals
);
330 static inline void modfreeW( LDAPModW
*mod
)
332 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
333 bvarrayfree( mod
->mod_vals
.modv_bvals
);
335 strarrayfreeW( mod
->mod_vals
.modv_strvals
);
339 static inline void modfreeU( LDAPMod
*mod
)
341 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
342 bvarrayfree( mod
->mod_vals
.modv_bvals
);
344 strarrayfreeU( mod
->mod_vals
.modv_strvals
);
348 static inline DWORD
modarraylenA( LDAPModA
**modarray
)
350 LDAPModA
**p
= modarray
;
355 static inline DWORD
modarraylenW( LDAPModW
**modarray
)
357 LDAPModW
**p
= modarray
;
362 static inline LDAPModW
**modarrayAtoW( LDAPModA
**modarray
)
364 LDAPModW
**modarrayW
= NULL
;
369 size
= sizeof(LDAPModW
*) * (modarraylenA( modarray
) + 1);
370 if ((modarrayW
= heap_alloc( size
)))
372 LDAPModA
**p
= modarray
;
373 LDAPModW
**q
= modarrayW
;
375 while (*p
) *q
++ = modAtoW( *p
++ );
382 static inline LDAPMod
**modarrayWtoU( LDAPModW
**modarray
)
384 LDAPMod
**modarrayU
= NULL
;
389 size
= sizeof(LDAPMod
*) * (modarraylenW( modarray
) + 1);
390 if ((modarrayU
= heap_alloc( size
)))
392 LDAPModW
**p
= modarray
;
393 LDAPMod
**q
= modarrayU
;
395 while (*p
) *q
++ = modWtoU( *p
++ );
402 static inline void modarrayfreeW( LDAPModW
**modarray
)
406 LDAPModW
**p
= modarray
;
407 while (*p
) modfreeW( *p
++ );
408 heap_free( modarray
);
412 static inline void modarrayfreeU( LDAPMod
**modarray
)
416 LDAPMod
**p
= modarray
;
417 while (*p
) modfreeU( *p
++ );
418 heap_free( modarray
);
422 static inline LDAPControlW
*controlAtoW( LDAPControlA
*control
)
424 LDAPControlW
*controlW
;
425 DWORD len
= control
->ldctl_value
.bv_len
;
428 if (control
->ldctl_value
.bv_val
)
430 if (!(val
= heap_alloc( len
))) return NULL
;
431 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
434 if (!(controlW
= heap_alloc( sizeof(LDAPControlW
) )))
440 controlW
->ldctl_oid
= strAtoW( control
->ldctl_oid
);
441 controlW
->ldctl_value
.bv_len
= len
;
442 controlW
->ldctl_value
.bv_val
= val
;
443 controlW
->ldctl_iscritical
= control
->ldctl_iscritical
;
448 static inline LDAPControlA
*controlWtoA( LDAPControlW
*control
)
450 LDAPControlA
*controlA
;
451 DWORD len
= control
->ldctl_value
.bv_len
;
454 if (control
->ldctl_value
.bv_val
)
456 if (!(val
= heap_alloc( len
))) return NULL
;
457 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
460 if (!(controlA
= heap_alloc( sizeof(LDAPControlA
) )))
466 controlA
->ldctl_oid
= strWtoA( control
->ldctl_oid
);
467 controlA
->ldctl_value
.bv_len
= len
;
468 controlA
->ldctl_value
.bv_val
= val
;
469 controlA
->ldctl_iscritical
= control
->ldctl_iscritical
;
474 static inline LDAPControl
*controlWtoU( LDAPControlW
*control
)
476 LDAPControl
*controlU
;
477 DWORD len
= control
->ldctl_value
.bv_len
;
480 if (control
->ldctl_value
.bv_val
)
482 if (!(val
= heap_alloc( len
))) return NULL
;
483 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
486 if (!(controlU
= heap_alloc( sizeof(LDAPControl
) )))
492 controlU
->ldctl_oid
= strWtoU( control
->ldctl_oid
);
493 controlU
->ldctl_value
.bv_len
= len
;
494 controlU
->ldctl_value
.bv_val
= val
;
495 controlU
->ldctl_iscritical
= control
->ldctl_iscritical
;
500 static inline LDAPControlW
*controlUtoW( LDAPControl
*control
)
502 LDAPControlW
*controlW
;
503 DWORD len
= control
->ldctl_value
.bv_len
;
506 if (control
->ldctl_value
.bv_val
)
508 if (!(val
= heap_alloc( len
))) return NULL
;
509 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
512 if (!(controlW
= heap_alloc( sizeof(LDAPControlW
) )))
518 controlW
->ldctl_oid
= strUtoW( control
->ldctl_oid
);
519 controlW
->ldctl_value
.bv_len
= len
;
520 controlW
->ldctl_value
.bv_val
= val
;
521 controlW
->ldctl_iscritical
= control
->ldctl_iscritical
;
526 static inline void controlfreeA( LDAPControlA
*control
)
530 strfreeA( control
->ldctl_oid
);
531 heap_free( control
->ldctl_value
.bv_val
);
532 heap_free( control
);
536 static inline void controlfreeW( LDAPControlW
*control
)
540 strfreeW( control
->ldctl_oid
);
541 heap_free( control
->ldctl_value
.bv_val
);
542 heap_free( control
);
546 static inline void controlfreeU( LDAPControl
*control
)
550 strfreeU( control
->ldctl_oid
);
551 heap_free( control
->ldctl_value
.bv_val
);
552 heap_free( control
);
556 static inline DWORD
controlarraylenA( LDAPControlA
**controlarray
)
558 LDAPControlA
**p
= controlarray
;
560 return p
- controlarray
;
563 static inline DWORD
controlarraylenW( LDAPControlW
**controlarray
)
565 LDAPControlW
**p
= controlarray
;
567 return p
- controlarray
;
570 static inline DWORD
controlarraylenU( LDAPControl
**controlarray
)
572 LDAPControl
**p
= controlarray
;
574 return p
- controlarray
;
577 static inline LDAPControlW
**controlarrayAtoW( LDAPControlA
**controlarray
)
579 LDAPControlW
**controlarrayW
= NULL
;
584 size
= sizeof(LDAPControlW
*) * (controlarraylenA( controlarray
) + 1);
585 if ((controlarrayW
= heap_alloc( size
)))
587 LDAPControlA
**p
= controlarray
;
588 LDAPControlW
**q
= controlarrayW
;
590 while (*p
) *q
++ = controlAtoW( *p
++ );
594 return controlarrayW
;
597 static inline LDAPControlA
**controlarrayWtoA( LDAPControlW
**controlarray
)
599 LDAPControlA
**controlarrayA
= NULL
;
604 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
605 if ((controlarrayA
= heap_alloc( size
)))
607 LDAPControlW
**p
= controlarray
;
608 LDAPControlA
**q
= controlarrayA
;
610 while (*p
) *q
++ = controlWtoA( *p
++ );
614 return controlarrayA
;
617 static inline LDAPControl
**controlarrayWtoU( LDAPControlW
**controlarray
)
619 LDAPControl
**controlarrayU
= NULL
;
624 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
625 if ((controlarrayU
= heap_alloc( size
)))
627 LDAPControlW
**p
= controlarray
;
628 LDAPControl
**q
= controlarrayU
;
630 while (*p
) *q
++ = controlWtoU( *p
++ );
634 return controlarrayU
;
637 static inline LDAPControlW
**controlarrayUtoW( LDAPControl
**controlarray
)
639 LDAPControlW
**controlarrayW
= NULL
;
644 size
= sizeof(LDAPControlW
*) * (controlarraylenU( controlarray
) + 1);
645 if ((controlarrayW
= heap_alloc( size
)))
647 LDAPControl
**p
= controlarray
;
648 LDAPControlW
**q
= controlarrayW
;
650 while (*p
) *q
++ = controlUtoW( *p
++ );
654 return controlarrayW
;
657 static inline void controlarrayfreeA( LDAPControlA
**controlarray
)
661 LDAPControlA
**p
= controlarray
;
662 while (*p
) controlfreeA( *p
++ );
663 heap_free( controlarray
);
667 static inline void controlarrayfreeW( LDAPControlW
**controlarray
)
671 LDAPControlW
**p
= controlarray
;
672 while (*p
) controlfreeW( *p
++ );
673 heap_free( controlarray
);
677 static inline void controlarrayfreeU( LDAPControl
**controlarray
)
681 LDAPControl
**p
= controlarray
;
682 while (*p
) controlfreeU( *p
++ );
683 heap_free( controlarray
);
687 static inline LDAPSortKeyW
*sortkeyAtoW( LDAPSortKeyA
*sortkey
)
689 LDAPSortKeyW
*sortkeyW
;
691 if ((sortkeyW
= heap_alloc( sizeof(LDAPSortKeyW
) )))
693 sortkeyW
->sk_attrtype
= strAtoW( sortkey
->sk_attrtype
);
694 sortkeyW
->sk_matchruleoid
= strAtoW( sortkey
->sk_matchruleoid
);
695 sortkeyW
->sk_reverseorder
= sortkey
->sk_reverseorder
;
700 static inline LDAPSortKeyA
*sortkeyWtoA( LDAPSortKeyW
*sortkey
)
702 LDAPSortKeyA
*sortkeyA
;
704 if ((sortkeyA
= heap_alloc( sizeof(LDAPSortKeyA
) )))
706 sortkeyA
->sk_attrtype
= strWtoA( sortkey
->sk_attrtype
);
707 sortkeyA
->sk_matchruleoid
= strWtoA( sortkey
->sk_matchruleoid
);
708 sortkeyA
->sk_reverseorder
= sortkey
->sk_reverseorder
;
713 static inline LDAPSortKey
*sortkeyWtoU( LDAPSortKeyW
*sortkey
)
715 LDAPSortKey
*sortkeyU
;
717 if ((sortkeyU
= heap_alloc( sizeof(LDAPSortKey
) )))
719 sortkeyU
->attributeType
= strWtoU( sortkey
->sk_attrtype
);
720 sortkeyU
->orderingRule
= strWtoU( sortkey
->sk_matchruleoid
);
721 sortkeyU
->reverseOrder
= sortkey
->sk_reverseorder
;
726 static inline void sortkeyfreeA( LDAPSortKeyA
*sortkey
)
730 strfreeA( sortkey
->sk_attrtype
);
731 strfreeA( sortkey
->sk_matchruleoid
);
732 heap_free( sortkey
);
736 static inline void sortkeyfreeW( LDAPSortKeyW
*sortkey
)
740 strfreeW( sortkey
->sk_attrtype
);
741 strfreeW( sortkey
->sk_matchruleoid
);
742 heap_free( sortkey
);
746 static inline void sortkeyfreeU( LDAPSortKey
*sortkey
)
750 strfreeU( sortkey
->attributeType
);
751 strfreeU( sortkey
->orderingRule
);
752 heap_free( sortkey
);
756 static inline DWORD
sortkeyarraylenA( LDAPSortKeyA
**sortkeyarray
)
758 LDAPSortKeyA
**p
= sortkeyarray
;
760 return p
- sortkeyarray
;
763 static inline DWORD
sortkeyarraylenW( LDAPSortKeyW
**sortkeyarray
)
765 LDAPSortKeyW
**p
= sortkeyarray
;
767 return p
- sortkeyarray
;
770 static inline LDAPSortKeyW
**sortkeyarrayAtoW( LDAPSortKeyA
**sortkeyarray
)
772 LDAPSortKeyW
**sortkeyarrayW
= NULL
;
777 size
= sizeof(LDAPSortKeyW
*) * (sortkeyarraylenA( sortkeyarray
) + 1);
778 if ((sortkeyarrayW
= heap_alloc( size
)))
780 LDAPSortKeyA
**p
= sortkeyarray
;
781 LDAPSortKeyW
**q
= sortkeyarrayW
;
783 while (*p
) *q
++ = sortkeyAtoW( *p
++ );
787 return sortkeyarrayW
;
790 static inline LDAPSortKey
**sortkeyarrayWtoU( LDAPSortKeyW
**sortkeyarray
)
792 LDAPSortKey
**sortkeyarrayU
= NULL
;
797 size
= sizeof(LDAPSortKey
*) * (sortkeyarraylenW( sortkeyarray
) + 1);
798 if ((sortkeyarrayU
= heap_alloc( size
)))
800 LDAPSortKeyW
**p
= sortkeyarray
;
801 LDAPSortKey
**q
= sortkeyarrayU
;
803 while (*p
) *q
++ = sortkeyWtoU( *p
++ );
807 return sortkeyarrayU
;
810 static inline void sortkeyarrayfreeW( LDAPSortKeyW
**sortkeyarray
)
814 LDAPSortKeyW
**p
= sortkeyarray
;
815 while (*p
) sortkeyfreeW( *p
++ );
816 heap_free( sortkeyarray
);
820 static inline void sortkeyarrayfreeU( LDAPSortKey
**sortkeyarray
)
824 LDAPSortKey
**p
= sortkeyarray
;
825 while (*p
) sortkeyfreeU( *p
++ );
826 heap_free( sortkeyarray
);
829 #endif /* HAVE_LDAP */