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/heap.h"
22 #include "wine/unicode.h"
24 extern HINSTANCE hwldap32 DECLSPEC_HIDDEN
;
26 ULONG
map_error( int ) DECLSPEC_HIDDEN
;
28 /* A set of helper functions to convert LDAP data structures
29 * to and from ansi (A), wide character (W) and utf8 (U) encodings.
32 static inline char *strdupU( const char *src
)
35 if (!src
) return NULL
;
36 if ((dst
= heap_alloc( (strlen( src
) + 1) * sizeof(char) ))) strcpy( dst
, src
);
40 static inline WCHAR
*strdupW( const WCHAR
*src
)
43 if (!src
) return NULL
;
44 if ((dst
= heap_alloc( (strlenW( src
) + 1) * sizeof(WCHAR
) ))) strcpyW( dst
, src
);
48 static inline LPWSTR
strAtoW( LPCSTR str
)
53 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, str
, -1, NULL
, 0 );
54 if ((ret
= heap_alloc( len
* sizeof(WCHAR
) )))
55 MultiByteToWideChar( CP_ACP
, 0, str
, -1, ret
, len
);
60 static inline LPSTR
strWtoA( LPCWSTR str
)
65 DWORD len
= WideCharToMultiByte( CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
66 if ((ret
= heap_alloc( len
)))
67 WideCharToMultiByte( CP_ACP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
72 static inline char *strWtoU( LPCWSTR str
)
77 DWORD len
= WideCharToMultiByte( CP_UTF8
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
78 if ((ret
= heap_alloc( len
)))
79 WideCharToMultiByte( CP_UTF8
, 0, str
, -1, ret
, len
, NULL
, NULL
);
84 static inline LPWSTR
strUtoW( char *str
)
89 DWORD len
= MultiByteToWideChar( CP_UTF8
, 0, str
, -1, NULL
, 0 );
90 if ((ret
= heap_alloc( len
* sizeof(WCHAR
) )))
91 MultiByteToWideChar( CP_UTF8
, 0, str
, -1, ret
, len
);
96 static inline void strfreeA( LPSTR str
)
101 static inline void strfreeW( LPWSTR str
)
106 static inline void strfreeU( char *str
)
111 static inline DWORD
strarraylenA( LPSTR
*strarray
)
118 static inline DWORD
strarraylenW( LPWSTR
*strarray
)
120 LPWSTR
*p
= strarray
;
125 static inline DWORD
strarraylenU( char **strarray
)
132 static inline LPWSTR
*strarrayAtoW( LPSTR
*strarray
)
134 LPWSTR
*strarrayW
= NULL
;
139 size
= sizeof(WCHAR
*) * (strarraylenA( strarray
) + 1);
140 if ((strarrayW
= heap_alloc( size
)))
143 LPWSTR
*q
= strarrayW
;
145 while (*p
) *q
++ = strAtoW( *p
++ );
152 static inline LPSTR
*strarrayWtoA( LPWSTR
*strarray
)
154 LPSTR
*strarrayA
= NULL
;
159 size
= sizeof(LPSTR
) * (strarraylenW( strarray
) + 1);
160 if ((strarrayA
= heap_alloc( size
)))
162 LPWSTR
*p
= strarray
;
163 LPSTR
*q
= strarrayA
;
165 while (*p
) *q
++ = strWtoA( *p
++ );
172 static inline char **strarrayWtoU( LPWSTR
*strarray
)
174 char **strarrayU
= NULL
;
179 size
= sizeof(char*) * (strarraylenW( strarray
) + 1);
180 if ((strarrayU
= heap_alloc( 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 if ((strarrayW
= heap_alloc( size
)))
203 LPWSTR
*q
= strarrayW
;
205 while (*p
) *q
++ = strUtoW( *p
++ );
212 static inline void strarrayfreeA( LPSTR
*strarray
)
217 while (*p
) strfreeA( *p
++ );
218 heap_free( strarray
);
222 static inline void strarrayfreeW( LPWSTR
*strarray
)
226 LPWSTR
*p
= strarray
;
227 while (*p
) strfreeW( *p
++ );
228 heap_free( strarray
);
232 static inline void strarrayfreeU( char **strarray
)
237 while (*p
) strfreeU( *p
++ );
238 heap_free( strarray
);
244 static inline struct berval
*bvdup( struct berval
*bv
)
246 struct berval
*berval
;
247 DWORD size
= sizeof(struct berval
) + bv
->bv_len
;
249 if ((berval
= heap_alloc( size
)))
251 char *val
= (char *)berval
+ sizeof(struct berval
);
253 berval
->bv_len
= bv
->bv_len
;
254 berval
->bv_val
= val
;
255 memcpy( val
, bv
->bv_val
, bv
->bv_len
);
260 static inline DWORD
bvarraylen( struct berval
**bv
)
262 struct berval
**p
= bv
;
267 static inline struct berval
**bvarraydup( struct berval
**bv
)
269 struct berval
**berval
= NULL
;
274 size
= sizeof(struct berval
*) * (bvarraylen( bv
) + 1);
275 if ((berval
= heap_alloc( size
)))
277 struct berval
**p
= bv
;
278 struct berval
**q
= berval
;
280 while (*p
) *q
++ = bvdup( *p
++ );
287 static inline void bvarrayfree( struct berval
**bv
)
289 struct berval
**p
= bv
;
290 while (*p
) heap_free( *p
++ );
294 static inline LDAPModW
*modAtoW( LDAPModA
*mod
)
298 if ((modW
= heap_alloc( sizeof(LDAPModW
) )))
300 modW
->mod_op
= mod
->mod_op
;
301 modW
->mod_type
= strAtoW( mod
->mod_type
);
303 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
304 modW
->mod_vals
.modv_bvals
= bvarraydup( mod
->mod_vals
.modv_bvals
);
306 modW
->mod_vals
.modv_strvals
= strarrayAtoW( mod
->mod_vals
.modv_strvals
);
311 static inline LDAPMod
*modWtoU( LDAPModW
*mod
)
315 if ((modU
= heap_alloc( sizeof(LDAPMod
) )))
317 modU
->mod_op
= mod
->mod_op
;
318 modU
->mod_type
= strWtoU( mod
->mod_type
);
320 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
321 modU
->mod_vals
.modv_bvals
= bvarraydup( mod
->mod_vals
.modv_bvals
);
323 modU
->mod_vals
.modv_strvals
= strarrayWtoU( mod
->mod_vals
.modv_strvals
);
328 static inline void modfreeW( LDAPModW
*mod
)
330 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
331 bvarrayfree( mod
->mod_vals
.modv_bvals
);
333 strarrayfreeW( mod
->mod_vals
.modv_strvals
);
337 static inline void modfreeU( LDAPMod
*mod
)
339 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
340 bvarrayfree( mod
->mod_vals
.modv_bvals
);
342 strarrayfreeU( mod
->mod_vals
.modv_strvals
);
346 static inline DWORD
modarraylenA( LDAPModA
**modarray
)
348 LDAPModA
**p
= modarray
;
353 static inline DWORD
modarraylenW( LDAPModW
**modarray
)
355 LDAPModW
**p
= modarray
;
360 static inline LDAPModW
**modarrayAtoW( LDAPModA
**modarray
)
362 LDAPModW
**modarrayW
= NULL
;
367 size
= sizeof(LDAPModW
*) * (modarraylenA( modarray
) + 1);
368 if ((modarrayW
= heap_alloc( size
)))
370 LDAPModA
**p
= modarray
;
371 LDAPModW
**q
= modarrayW
;
373 while (*p
) *q
++ = modAtoW( *p
++ );
380 static inline LDAPMod
**modarrayWtoU( LDAPModW
**modarray
)
382 LDAPMod
**modarrayU
= NULL
;
387 size
= sizeof(LDAPMod
*) * (modarraylenW( modarray
) + 1);
388 if ((modarrayU
= heap_alloc( size
)))
390 LDAPModW
**p
= modarray
;
391 LDAPMod
**q
= modarrayU
;
393 while (*p
) *q
++ = modWtoU( *p
++ );
400 static inline void modarrayfreeW( LDAPModW
**modarray
)
404 LDAPModW
**p
= modarray
;
405 while (*p
) modfreeW( *p
++ );
406 heap_free( modarray
);
410 static inline void modarrayfreeU( LDAPMod
**modarray
)
414 LDAPMod
**p
= modarray
;
415 while (*p
) modfreeU( *p
++ );
416 heap_free( modarray
);
420 static inline LDAPControlW
*controlAtoW( LDAPControlA
*control
)
422 LDAPControlW
*controlW
;
423 DWORD len
= control
->ldctl_value
.bv_len
;
426 if (control
->ldctl_value
.bv_val
)
428 if (!(val
= heap_alloc( len
))) return NULL
;
429 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
432 if (!(controlW
= heap_alloc( sizeof(LDAPControlW
) )))
438 controlW
->ldctl_oid
= strAtoW( control
->ldctl_oid
);
439 controlW
->ldctl_value
.bv_len
= len
;
440 controlW
->ldctl_value
.bv_val
= val
;
441 controlW
->ldctl_iscritical
= control
->ldctl_iscritical
;
446 static inline LDAPControlA
*controlWtoA( LDAPControlW
*control
)
448 LDAPControlA
*controlA
;
449 DWORD len
= control
->ldctl_value
.bv_len
;
452 if (control
->ldctl_value
.bv_val
)
454 if (!(val
= heap_alloc( len
))) return NULL
;
455 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
458 if (!(controlA
= heap_alloc( sizeof(LDAPControlA
) )))
464 controlA
->ldctl_oid
= strWtoA( control
->ldctl_oid
);
465 controlA
->ldctl_value
.bv_len
= len
;
466 controlA
->ldctl_value
.bv_val
= val
;
467 controlA
->ldctl_iscritical
= control
->ldctl_iscritical
;
472 static inline LDAPControl
*controlWtoU( LDAPControlW
*control
)
474 LDAPControl
*controlU
;
475 DWORD len
= control
->ldctl_value
.bv_len
;
478 if (control
->ldctl_value
.bv_val
)
480 if (!(val
= heap_alloc( len
))) return NULL
;
481 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
484 if (!(controlU
= heap_alloc( sizeof(LDAPControl
) )))
490 controlU
->ldctl_oid
= strWtoU( control
->ldctl_oid
);
491 controlU
->ldctl_value
.bv_len
= len
;
492 controlU
->ldctl_value
.bv_val
= val
;
493 controlU
->ldctl_iscritical
= control
->ldctl_iscritical
;
498 static inline LDAPControlW
*controlUtoW( LDAPControl
*control
)
500 LDAPControlW
*controlW
;
501 DWORD len
= control
->ldctl_value
.bv_len
;
504 if (control
->ldctl_value
.bv_val
)
506 if (!(val
= heap_alloc( len
))) return NULL
;
507 memcpy( val
, control
->ldctl_value
.bv_val
, len
);
510 if (!(controlW
= heap_alloc( sizeof(LDAPControlW
) )))
516 controlW
->ldctl_oid
= strUtoW( control
->ldctl_oid
);
517 controlW
->ldctl_value
.bv_len
= len
;
518 controlW
->ldctl_value
.bv_val
= val
;
519 controlW
->ldctl_iscritical
= control
->ldctl_iscritical
;
524 static inline void controlfreeA( LDAPControlA
*control
)
528 strfreeA( control
->ldctl_oid
);
529 heap_free( control
->ldctl_value
.bv_val
);
530 heap_free( control
);
534 static inline void controlfreeW( LDAPControlW
*control
)
538 strfreeW( control
->ldctl_oid
);
539 heap_free( control
->ldctl_value
.bv_val
);
540 heap_free( control
);
544 static inline void controlfreeU( LDAPControl
*control
)
548 strfreeU( control
->ldctl_oid
);
549 heap_free( control
->ldctl_value
.bv_val
);
550 heap_free( control
);
554 static inline DWORD
controlarraylenA( LDAPControlA
**controlarray
)
556 LDAPControlA
**p
= controlarray
;
558 return p
- controlarray
;
561 static inline DWORD
controlarraylenW( LDAPControlW
**controlarray
)
563 LDAPControlW
**p
= controlarray
;
565 return p
- controlarray
;
568 static inline DWORD
controlarraylenU( LDAPControl
**controlarray
)
570 LDAPControl
**p
= controlarray
;
572 return p
- controlarray
;
575 static inline LDAPControlW
**controlarrayAtoW( LDAPControlA
**controlarray
)
577 LDAPControlW
**controlarrayW
= NULL
;
582 size
= sizeof(LDAPControlW
*) * (controlarraylenA( controlarray
) + 1);
583 if ((controlarrayW
= heap_alloc( size
)))
585 LDAPControlA
**p
= controlarray
;
586 LDAPControlW
**q
= controlarrayW
;
588 while (*p
) *q
++ = controlAtoW( *p
++ );
592 return controlarrayW
;
595 static inline LDAPControlA
**controlarrayWtoA( LDAPControlW
**controlarray
)
597 LDAPControlA
**controlarrayA
= NULL
;
602 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
603 if ((controlarrayA
= heap_alloc( size
)))
605 LDAPControlW
**p
= controlarray
;
606 LDAPControlA
**q
= controlarrayA
;
608 while (*p
) *q
++ = controlWtoA( *p
++ );
612 return controlarrayA
;
615 static inline LDAPControl
**controlarrayWtoU( LDAPControlW
**controlarray
)
617 LDAPControl
**controlarrayU
= NULL
;
622 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
623 if ((controlarrayU
= heap_alloc( size
)))
625 LDAPControlW
**p
= controlarray
;
626 LDAPControl
**q
= controlarrayU
;
628 while (*p
) *q
++ = controlWtoU( *p
++ );
632 return controlarrayU
;
635 static inline LDAPControlW
**controlarrayUtoW( LDAPControl
**controlarray
)
637 LDAPControlW
**controlarrayW
= NULL
;
642 size
= sizeof(LDAPControlW
*) * (controlarraylenU( controlarray
) + 1);
643 if ((controlarrayW
= heap_alloc( size
)))
645 LDAPControl
**p
= controlarray
;
646 LDAPControlW
**q
= controlarrayW
;
648 while (*p
) *q
++ = controlUtoW( *p
++ );
652 return controlarrayW
;
655 static inline void controlarrayfreeA( LDAPControlA
**controlarray
)
659 LDAPControlA
**p
= controlarray
;
660 while (*p
) controlfreeA( *p
++ );
661 heap_free( controlarray
);
665 static inline void controlarrayfreeW( LDAPControlW
**controlarray
)
669 LDAPControlW
**p
= controlarray
;
670 while (*p
) controlfreeW( *p
++ );
671 heap_free( controlarray
);
675 static inline void controlarrayfreeU( LDAPControl
**controlarray
)
679 LDAPControl
**p
= controlarray
;
680 while (*p
) controlfreeU( *p
++ );
681 heap_free( controlarray
);
685 static inline LDAPSortKeyW
*sortkeyAtoW( LDAPSortKeyA
*sortkey
)
687 LDAPSortKeyW
*sortkeyW
;
689 if ((sortkeyW
= heap_alloc( sizeof(LDAPSortKeyW
) )))
691 sortkeyW
->sk_attrtype
= strAtoW( sortkey
->sk_attrtype
);
692 sortkeyW
->sk_matchruleoid
= strAtoW( sortkey
->sk_matchruleoid
);
693 sortkeyW
->sk_reverseorder
= sortkey
->sk_reverseorder
;
698 static inline LDAPSortKeyA
*sortkeyWtoA( LDAPSortKeyW
*sortkey
)
700 LDAPSortKeyA
*sortkeyA
;
702 if ((sortkeyA
= heap_alloc( sizeof(LDAPSortKeyA
) )))
704 sortkeyA
->sk_attrtype
= strWtoA( sortkey
->sk_attrtype
);
705 sortkeyA
->sk_matchruleoid
= strWtoA( sortkey
->sk_matchruleoid
);
706 sortkeyA
->sk_reverseorder
= sortkey
->sk_reverseorder
;
711 static inline LDAPSortKey
*sortkeyWtoU( LDAPSortKeyW
*sortkey
)
713 LDAPSortKey
*sortkeyU
;
715 if ((sortkeyU
= heap_alloc( sizeof(LDAPSortKey
) )))
717 sortkeyU
->attributeType
= strWtoU( sortkey
->sk_attrtype
);
718 sortkeyU
->orderingRule
= strWtoU( sortkey
->sk_matchruleoid
);
719 sortkeyU
->reverseOrder
= sortkey
->sk_reverseorder
;
724 static inline void sortkeyfreeA( LDAPSortKeyA
*sortkey
)
728 strfreeA( sortkey
->sk_attrtype
);
729 strfreeA( sortkey
->sk_matchruleoid
);
730 heap_free( sortkey
);
734 static inline void sortkeyfreeW( LDAPSortKeyW
*sortkey
)
738 strfreeW( sortkey
->sk_attrtype
);
739 strfreeW( sortkey
->sk_matchruleoid
);
740 heap_free( sortkey
);
744 static inline void sortkeyfreeU( LDAPSortKey
*sortkey
)
748 strfreeU( sortkey
->attributeType
);
749 strfreeU( sortkey
->orderingRule
);
750 heap_free( sortkey
);
754 static inline DWORD
sortkeyarraylenA( LDAPSortKeyA
**sortkeyarray
)
756 LDAPSortKeyA
**p
= sortkeyarray
;
758 return p
- sortkeyarray
;
761 static inline DWORD
sortkeyarraylenW( LDAPSortKeyW
**sortkeyarray
)
763 LDAPSortKeyW
**p
= sortkeyarray
;
765 return p
- sortkeyarray
;
768 static inline LDAPSortKeyW
**sortkeyarrayAtoW( LDAPSortKeyA
**sortkeyarray
)
770 LDAPSortKeyW
**sortkeyarrayW
= NULL
;
775 size
= sizeof(LDAPSortKeyW
*) * (sortkeyarraylenA( sortkeyarray
) + 1);
776 if ((sortkeyarrayW
= heap_alloc( size
)))
778 LDAPSortKeyA
**p
= sortkeyarray
;
779 LDAPSortKeyW
**q
= sortkeyarrayW
;
781 while (*p
) *q
++ = sortkeyAtoW( *p
++ );
785 return sortkeyarrayW
;
788 static inline LDAPSortKey
**sortkeyarrayWtoU( LDAPSortKeyW
**sortkeyarray
)
790 LDAPSortKey
**sortkeyarrayU
= NULL
;
795 size
= sizeof(LDAPSortKey
*) * (sortkeyarraylenW( sortkeyarray
) + 1);
796 if ((sortkeyarrayU
= heap_alloc( size
)))
798 LDAPSortKeyW
**p
= sortkeyarray
;
799 LDAPSortKey
**q
= sortkeyarrayU
;
801 while (*p
) *q
++ = sortkeyWtoU( *p
++ );
805 return sortkeyarrayU
;
808 static inline void sortkeyarrayfreeW( LDAPSortKeyW
**sortkeyarray
)
812 LDAPSortKeyW
**p
= sortkeyarray
;
813 while (*p
) sortkeyfreeW( *p
++ );
814 heap_free( sortkeyarray
);
818 static inline void sortkeyarrayfreeU( LDAPSortKey
**sortkeyarray
)
822 LDAPSortKey
**p
= sortkeyarray
;
823 while (*p
) sortkeyfreeU( *p
++ );
824 heap_free( sortkeyarray
);
827 #endif /* HAVE_LDAP */