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 ULONG map_error( int );
23 /* A set of helper functions to convert LDAP data structures
24 * to and from ansi (A), wide character (W) and utf8 (U) encodings.
27 static inline char *strdupU( const char *src )
31 if (!src) return NULL;
32 dst = HeapAlloc( GetProcessHeap(), 0, (strlen( src ) + 1) * sizeof(char) );
38 static inline LPWSTR strAtoW( LPCSTR str )
43 DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
44 if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
45 MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
50 static inline LPSTR strWtoA( LPCWSTR str )
55 DWORD len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
56 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
57 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
62 static inline char *strWtoU( LPCWSTR str )
67 DWORD len = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
68 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
69 WideCharToMultiByte( CP_UTF8, 0, str, -1, ret, len, NULL, NULL );
74 static inline LPWSTR strUtoW( char *str )
79 DWORD len = MultiByteToWideChar( CP_UTF8, 0, str, -1, NULL, 0 );
80 if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
81 MultiByteToWideChar( CP_UTF8, 0, str, -1, ret, len );
86 static inline void strfreeA( LPSTR str )
88 HeapFree( GetProcessHeap(), 0, str );
91 static inline void strfreeW( LPWSTR str )
93 HeapFree( GetProcessHeap(), 0, str );
96 static inline void strfreeU( char *str )
98 HeapFree( GetProcessHeap(), 0, str );
101 static inline DWORD strarraylenA( LPSTR *strarray )
108 static inline DWORD strarraylenW( LPWSTR *strarray )
110 LPWSTR *p = strarray;
115 static inline DWORD strarraylenU( char **strarray )
122 static inline LPWSTR *strarrayAtoW( LPSTR *strarray )
124 LPWSTR *strarrayW = NULL;
129 size = sizeof(WCHAR*) * (strarraylenA( strarray ) + 1);
130 strarrayW = HeapAlloc( GetProcessHeap(), 0, size );
135 LPWSTR *q = strarrayW;
137 while (*p) *q++ = strAtoW( *p++ );
144 static inline LPSTR *strarrayWtoA( LPWSTR *strarray )
146 LPSTR *strarrayA = NULL;
151 size = sizeof(LPSTR) * (strarraylenW( strarray ) + 1);
152 strarrayA = HeapAlloc( GetProcessHeap(), 0, size );
156 LPWSTR *p = strarray;
157 LPSTR *q = strarrayA;
159 while (*p) *q++ = strWtoA( *p++ );
166 static inline char **strarrayWtoU( LPWSTR *strarray )
168 char **strarrayU = NULL;
173 size = sizeof(char*) * (strarraylenW( strarray ) + 1);
174 strarrayU = HeapAlloc( GetProcessHeap(), 0, size );
178 LPWSTR *p = strarray;
179 char **q = strarrayU;
181 while (*p) *q++ = strWtoU( *p++ );
188 static inline LPWSTR *strarrayUtoW( char **strarray )
190 LPWSTR *strarrayW = NULL;
195 size = sizeof(WCHAR*) * (strarraylenU( strarray ) + 1);
196 strarrayW = HeapAlloc( GetProcessHeap(), 0, size );
201 LPWSTR *q = strarrayW;
203 while (*p) *q++ = strUtoW( *p++ );
210 static inline void strarrayfreeA( LPSTR *strarray )
215 while (*p) strfreeA( *p++ );
216 HeapFree( GetProcessHeap(), 0, strarray );
220 static inline void strarrayfreeW( LPWSTR *strarray )
224 LPWSTR *p = strarray;
225 while (*p) strfreeW( *p++ );
226 HeapFree( GetProcessHeap(), 0, strarray );
230 static inline void strarrayfreeU( char **strarray )
235 while (*p) strfreeU( *p++ );
236 HeapFree( GetProcessHeap(), 0, strarray );
242 static inline struct berval *bvdup( struct berval *bv )
244 struct berval *berval;
245 DWORD size = sizeof(struct berval) + bv->bv_len;
247 berval = HeapAlloc( GetProcessHeap(), 0, size );
250 char *val = (char *)berval + sizeof(struct berval);
252 berval->bv_len = bv->bv_len;
253 berval->bv_val = val;
254 memcpy( val, bv->bv_val, bv->bv_len );
259 static inline DWORD bvarraylen( struct berval **bv )
261 struct berval **p = bv;
266 static inline struct berval **bvarraydup( struct berval **bv )
268 struct berval **berval = NULL;
273 size = sizeof(struct berval *) * (bvarraylen( bv ) + 1);
274 berval = HeapAlloc( GetProcessHeap(), 0, size );
278 struct berval **p = bv;
279 struct berval **q = berval;
281 while (*p) *q++ = bvdup( *p++ );
288 static inline void bvarrayfree( struct berval **bv )
290 struct berval **p = bv;
291 while (*p) HeapFree( GetProcessHeap(), 0, *p++ );
292 HeapFree( GetProcessHeap(), 0, bv );
295 static inline LDAPModW *modAtoW( LDAPModA *mod )
299 modW = HeapAlloc( GetProcessHeap(), 0, 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 modU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPMod) );
320 modU->mod_op = mod->mod_op;
321 modU->mod_type = strWtoU( mod->mod_type );
323 if (mod->mod_op & LDAP_MOD_BVALUES)
324 modU->mod_vals.modv_bvals = bvarraydup( mod->mod_vals.modv_bvals );
326 modU->mod_vals.modv_strvals = strarrayWtoU( mod->mod_vals.modv_strvals );
331 static inline void modfreeW( LDAPModW *mod )
333 if (mod->mod_op & LDAP_MOD_BVALUES)
334 bvarrayfree( mod->mod_vals.modv_bvals );
336 strarrayfreeW( mod->mod_vals.modv_strvals );
337 HeapFree( GetProcessHeap(), 0, mod );
340 static inline void modfreeU( LDAPMod *mod )
342 if (mod->mod_op & LDAP_MOD_BVALUES)
343 bvarrayfree( mod->mod_vals.modv_bvals );
345 strarrayfreeU( mod->mod_vals.modv_strvals );
346 HeapFree( GetProcessHeap(), 0, mod );
349 static inline DWORD modarraylenA( LDAPModA **modarray )
351 LDAPModA **p = modarray;
356 static inline DWORD modarraylenW( LDAPModW **modarray )
358 LDAPModW **p = modarray;
363 static inline LDAPModW **modarrayAtoW( LDAPModA **modarray )
365 LDAPModW **modarrayW = NULL;
370 size = sizeof(LDAPModW*) * (modarraylenA( modarray ) + 1);
371 modarrayW = HeapAlloc( GetProcessHeap(), 0, size );
375 LDAPModA **p = modarray;
376 LDAPModW **q = modarrayW;
378 while (*p) *q++ = modAtoW( *p++ );
385 static inline LDAPMod **modarrayWtoU( LDAPModW **modarray )
387 LDAPMod **modarrayU = NULL;
392 size = sizeof(LDAPMod*) * (modarraylenW( modarray ) + 1);
393 modarrayU = HeapAlloc( GetProcessHeap(), 0, size );
397 LDAPModW **p = modarray;
398 LDAPMod **q = modarrayU;
400 while (*p) *q++ = modWtoU( *p++ );
407 static inline void modarrayfreeW( LDAPModW **modarray )
411 LDAPModW **p = modarray;
412 while (*p) modfreeW( *p++ );
413 HeapFree( GetProcessHeap(), 0, modarray );
417 static inline void modarrayfreeU( LDAPMod **modarray )
421 LDAPMod **p = modarray;
422 while (*p) modfreeU( *p++ );
423 HeapFree( GetProcessHeap(), 0, modarray );
427 static inline LDAPControlW *controlAtoW( LDAPControlA *control )
429 LDAPControlW *controlW;
430 DWORD len = control->ldctl_value.bv_len;
433 if (control->ldctl_value.bv_val)
435 val = HeapAlloc( GetProcessHeap(), 0, len );
436 if (!val) return NULL;
437 memcpy( val, control->ldctl_value.bv_val, len );
440 controlW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW) );
443 HeapFree( GetProcessHeap(), 0, val );
447 controlW->ldctl_oid = strAtoW( control->ldctl_oid );
448 controlW->ldctl_value.bv_len = len;
449 controlW->ldctl_value.bv_val = val;
450 controlW->ldctl_iscritical = control->ldctl_iscritical;
455 static inline LDAPControlA *controlWtoA( LDAPControlW *control )
457 LDAPControlA *controlA;
458 DWORD len = control->ldctl_value.bv_len;
461 if (control->ldctl_value.bv_val)
463 val = HeapAlloc( GetProcessHeap(), 0, len );
464 if (!val) return NULL;
465 memcpy( val, control->ldctl_value.bv_val, len );
468 controlA = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlA) );
471 HeapFree( GetProcessHeap(), 0, val );
475 controlA->ldctl_oid = strWtoA( control->ldctl_oid );
476 controlA->ldctl_value.bv_len = len;
477 controlA->ldctl_value.bv_val = val;
478 controlA->ldctl_iscritical = control->ldctl_iscritical;
483 static inline LDAPControl *controlWtoU( LDAPControlW *control )
485 LDAPControl *controlU;
486 DWORD len = control->ldctl_value.bv_len;
489 if (control->ldctl_value.bv_val)
491 val = HeapAlloc( GetProcessHeap(), 0, len );
492 if (!val) return NULL;
493 memcpy( val, control->ldctl_value.bv_val, len );
496 controlU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControl) );
499 HeapFree( GetProcessHeap(), 0, val );
503 controlU->ldctl_oid = strWtoU( control->ldctl_oid );
504 controlU->ldctl_value.bv_len = len;
505 controlU->ldctl_value.bv_val = val;
506 controlU->ldctl_iscritical = control->ldctl_iscritical;
511 static inline LDAPControlW *controlUtoW( LDAPControl *control )
513 LDAPControlW *controlW;
514 DWORD len = control->ldctl_value.bv_len;
517 if (control->ldctl_value.bv_val)
519 val = HeapAlloc( GetProcessHeap(), 0, len );
520 if (!val) return NULL;
521 memcpy( val, control->ldctl_value.bv_val, len );
524 controlW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW) );
527 HeapFree( GetProcessHeap(), 0, val );
531 controlW->ldctl_oid = strUtoW( control->ldctl_oid );
532 controlW->ldctl_value.bv_len = len;
533 controlW->ldctl_value.bv_val = val;
534 controlW->ldctl_iscritical = control->ldctl_iscritical;
539 static inline void controlfreeA( LDAPControlA *control )
543 strfreeA( control->ldctl_oid );
544 HeapFree( GetProcessHeap(), 0, control->ldctl_value.bv_val );
545 HeapFree( GetProcessHeap(), 0, control );
549 static inline void controlfreeW( LDAPControlW *control )
553 strfreeW( control->ldctl_oid );
554 HeapFree( GetProcessHeap(), 0, control->ldctl_value.bv_val );
555 HeapFree( GetProcessHeap(), 0, control );
559 static inline void controlfreeU( LDAPControl *control )
563 strfreeU( control->ldctl_oid );
564 HeapFree( GetProcessHeap(), 0, control->ldctl_value.bv_val );
565 HeapFree( GetProcessHeap(), 0, control );
569 static inline DWORD controlarraylenA( LDAPControlA **controlarray )
571 LDAPControlA **p = controlarray;
573 return p - controlarray;
576 static inline DWORD controlarraylenW( LDAPControlW **controlarray )
578 LDAPControlW **p = controlarray;
580 return p - controlarray;
583 static inline DWORD controlarraylenU( LDAPControl **controlarray )
585 LDAPControl **p = controlarray;
587 return p - controlarray;
590 static inline LDAPControlW **controlarrayAtoW( LDAPControlA **controlarray )
592 LDAPControlW **controlarrayW = NULL;
597 size = sizeof(LDAPControlW*) * (controlarraylenA( controlarray ) + 1);
598 controlarrayW = HeapAlloc( GetProcessHeap(), 0, size );
602 LDAPControlA **p = controlarray;
603 LDAPControlW **q = controlarrayW;
605 while (*p) *q++ = controlAtoW( *p++ );
609 return controlarrayW;
612 static inline LDAPControlA **controlarrayWtoA( LDAPControlW **controlarray )
614 LDAPControlA **controlarrayA = NULL;
619 size = sizeof(LDAPControl*) * (controlarraylenW( controlarray ) + 1);
620 controlarrayA = HeapAlloc( GetProcessHeap(), 0, size );
624 LDAPControlW **p = controlarray;
625 LDAPControlA **q = controlarrayA;
627 while (*p) *q++ = controlWtoA( *p++ );
631 return controlarrayA;
634 static inline LDAPControl **controlarrayWtoU( LDAPControlW **controlarray )
636 LDAPControl **controlarrayU = NULL;
641 size = sizeof(LDAPControl*) * (controlarraylenW( controlarray ) + 1);
642 controlarrayU = HeapAlloc( GetProcessHeap(), 0, size );
646 LDAPControlW **p = controlarray;
647 LDAPControl **q = controlarrayU;
649 while (*p) *q++ = controlWtoU( *p++ );
653 return controlarrayU;
656 static inline LDAPControlW **controlarrayUtoW( LDAPControl **controlarray )
658 LDAPControlW **controlarrayW = NULL;
663 size = sizeof(LDAPControlW*) * (controlarraylenU( controlarray ) + 1);
664 controlarrayW = HeapAlloc( GetProcessHeap(), 0, size );
668 LDAPControl **p = controlarray;
669 LDAPControlW **q = controlarrayW;
671 while (*p) *q++ = controlUtoW( *p++ );
675 return controlarrayW;
678 static inline void controlarrayfreeA( LDAPControlA **controlarray )
682 LDAPControlA **p = controlarray;
683 while (*p) controlfreeA( *p++ );
684 HeapFree( GetProcessHeap(), 0, controlarray );
688 static inline void controlarrayfreeW( LDAPControlW **controlarray )
692 LDAPControlW **p = controlarray;
693 while (*p) controlfreeW( *p++ );
694 HeapFree( GetProcessHeap(), 0, controlarray );
698 static inline void controlarrayfreeU( LDAPControl **controlarray )
702 LDAPControl **p = controlarray;
703 while (*p) controlfreeU( *p++ );
704 HeapFree( GetProcessHeap(), 0, controlarray );
708 static inline LDAPSortKeyW *sortkeyAtoW( LDAPSortKeyA *sortkey )
710 LDAPSortKeyW *sortkeyW;
712 sortkeyW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyW) );
715 sortkeyW->sk_attrtype = strAtoW( sortkey->sk_attrtype );
716 sortkeyW->sk_matchruleoid = strAtoW( sortkey->sk_matchruleoid );
717 sortkeyW->sk_reverseorder = sortkey->sk_reverseorder;
722 static inline LDAPSortKeyA *sortkeyWtoA( LDAPSortKeyW *sortkey )
724 LDAPSortKeyA *sortkeyA;
726 sortkeyA = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyA) );
729 sortkeyA->sk_attrtype = strWtoA( sortkey->sk_attrtype );
730 sortkeyA->sk_matchruleoid = strWtoA( sortkey->sk_matchruleoid );
731 sortkeyA->sk_reverseorder = sortkey->sk_reverseorder;
736 static inline LDAPSortKey *sortkeyWtoU( LDAPSortKeyW *sortkey )
738 LDAPSortKey *sortkeyU;
740 sortkeyU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKey) );
743 sortkeyU->attributeType = strWtoU( sortkey->sk_attrtype );
744 sortkeyU->orderingRule = strWtoU( sortkey->sk_matchruleoid );
745 sortkeyU->reverseOrder = sortkey->sk_reverseorder;
750 static inline void sortkeyfreeA( LDAPSortKeyA *sortkey )
754 strfreeA( sortkey->sk_attrtype );
755 strfreeA( sortkey->sk_matchruleoid );
756 HeapFree( GetProcessHeap(), 0, sortkey );
760 static inline void sortkeyfreeW( LDAPSortKeyW *sortkey )
764 strfreeW( sortkey->sk_attrtype );
765 strfreeW( sortkey->sk_matchruleoid );
766 HeapFree( GetProcessHeap(), 0, sortkey );
770 static inline void sortkeyfreeU( LDAPSortKey *sortkey )
774 strfreeU( sortkey->attributeType );
775 strfreeU( sortkey->orderingRule );
776 HeapFree( GetProcessHeap(), 0, sortkey );
780 static inline DWORD sortkeyarraylenA( LDAPSortKeyA **sortkeyarray )
782 LDAPSortKeyA **p = sortkeyarray;
784 return p - sortkeyarray;
787 static inline DWORD sortkeyarraylenW( LDAPSortKeyW **sortkeyarray )
789 LDAPSortKeyW **p = sortkeyarray;
791 return p - sortkeyarray;
794 static inline LDAPSortKeyW **sortkeyarrayAtoW( LDAPSortKeyA **sortkeyarray )
796 LDAPSortKeyW **sortkeyarrayW = NULL;
801 size = sizeof(LDAPSortKeyW*) * (sortkeyarraylenA( sortkeyarray ) + 1);
802 sortkeyarrayW = HeapAlloc( GetProcessHeap(), 0, size );
806 LDAPSortKeyA **p = sortkeyarray;
807 LDAPSortKeyW **q = sortkeyarrayW;
809 while (*p) *q++ = sortkeyAtoW( *p++ );
813 return sortkeyarrayW;
816 static inline LDAPSortKey **sortkeyarrayWtoU( LDAPSortKeyW **sortkeyarray )
818 LDAPSortKey **sortkeyarrayU = NULL;
823 size = sizeof(LDAPSortKey*) * (sortkeyarraylenW( sortkeyarray ) + 1);
824 sortkeyarrayU = HeapAlloc( GetProcessHeap(), 0, size );
828 LDAPSortKeyW **p = sortkeyarray;
829 LDAPSortKey **q = sortkeyarrayU;
831 while (*p) *q++ = sortkeyWtoU( *p++ );
835 return sortkeyarrayU;
838 static inline void sortkeyarrayfreeW( LDAPSortKeyW **sortkeyarray )
842 LDAPSortKeyW **p = sortkeyarray;
843 while (*p) sortkeyfreeW( *p++ );
844 HeapFree( GetProcessHeap(), 0, sortkeyarray );
848 static inline void sortkeyarrayfreeU( LDAPSortKey **sortkeyarray )
852 LDAPSortKey **p = sortkeyarray;
853 while (*p) sortkeyfreeU( *p++ );
854 HeapFree( GetProcessHeap(), 0, sortkeyarray );
858 #endif /* HAVE_LDAP */