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 /* A set of helper functions to convert LDAP data structures
22 * to and from ansi (A), wide character (W) and utf8 (U) encodings.
25 static inline char *strdupU( const char *src )
29 if (!src) return NULL;
30 dst = HeapAlloc( GetProcessHeap(), 0, (strlen( src ) + 1) * sizeof(char) );
36 static inline LPWSTR strAtoW( LPCSTR str )
41 DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
42 if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
43 MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
48 static inline LPSTR strWtoA( LPCWSTR str )
53 DWORD len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
54 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
55 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
60 static inline char *strWtoU( LPCWSTR str )
65 DWORD len = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
66 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
67 WideCharToMultiByte( CP_UTF8, 0, str, -1, ret, len, NULL, NULL );
72 static inline LPWSTR strUtoW( char *str )
77 DWORD len = MultiByteToWideChar( CP_UTF8, 0, str, -1, NULL, 0 );
78 if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
79 MultiByteToWideChar( CP_UTF8, 0, str, -1, ret, len );
84 static inline void strfreeA( LPSTR str )
86 HeapFree( GetProcessHeap(), 0, str );
89 static inline void strfreeW( LPWSTR str )
91 HeapFree( GetProcessHeap(), 0, str );
94 static inline void strfreeU( char *str )
96 HeapFree( GetProcessHeap(), 0, str );
99 static inline DWORD strarraylenA( LPSTR *strarray )
106 static inline DWORD strarraylenW( LPWSTR *strarray )
108 LPWSTR *p = strarray;
113 static inline DWORD strarraylenU( char **strarray )
120 static inline LPWSTR *strarrayAtoW( LPSTR *strarray )
122 LPWSTR *strarrayW = NULL;
127 size = sizeof(WCHAR*) * (strarraylenA( strarray ) + 1);
128 strarrayW = HeapAlloc( GetProcessHeap(), 0, size );
133 LPWSTR *q = strarrayW;
135 while (*p) *q++ = strAtoW( *p++ );
142 static inline LPSTR *strarrayWtoA( LPWSTR *strarray )
144 LPSTR *strarrayA = NULL;
149 size = sizeof(LPSTR) * (strarraylenW( strarray ) + 1);
150 strarrayA = HeapAlloc( GetProcessHeap(), 0, size );
154 LPWSTR *p = strarray;
155 LPSTR *q = strarrayA;
157 while (*p) *q++ = strWtoA( *p++ );
164 static inline char **strarrayWtoU( LPWSTR *strarray )
166 char **strarrayU = NULL;
171 size = sizeof(char*) * (strarraylenW( strarray ) + 1);
172 strarrayU = HeapAlloc( GetProcessHeap(), 0, size );
176 LPWSTR *p = strarray;
177 char **q = strarrayU;
179 while (*p) *q++ = strWtoU( *p++ );
186 static inline LPWSTR *strarrayUtoW( char **strarray )
188 LPWSTR *strarrayW = NULL;
193 size = sizeof(WCHAR*) * (strarraylenU( strarray ) + 1);
194 strarrayW = HeapAlloc( GetProcessHeap(), 0, size );
199 LPWSTR *q = strarrayW;
201 while (*p) *q++ = strUtoW( *p++ );
208 static inline void strarrayfreeA( LPSTR *strarray )
213 while (*p) strfreeA( *p++ );
214 HeapFree( GetProcessHeap(), 0, strarray );
218 static inline void strarrayfreeW( LPWSTR *strarray )
222 LPWSTR *p = strarray;
223 while (*p) strfreeW( *p++ );
224 HeapFree( GetProcessHeap(), 0, strarray );
228 static inline void strarrayfreeU( char **strarray )
233 while (*p) strfreeU( *p++ );
234 HeapFree( GetProcessHeap(), 0, strarray );
240 static inline struct berval *bvdup( struct berval *bv )
242 struct berval *berval;
243 DWORD size = sizeof(struct berval) + bv->bv_len;
245 berval = HeapAlloc( GetProcessHeap(), 0, size );
248 char *val = (char *)berval + sizeof(struct berval);
250 berval->bv_len = bv->bv_len;
251 berval->bv_val = val;
252 memcpy( val, bv->bv_val, bv->bv_len );
257 static inline DWORD bvarraylen( struct berval **bv )
259 struct berval **p = bv;
264 static inline struct berval **bvarraydup( struct berval **bv )
266 struct berval **berval = NULL;
271 size = sizeof(struct berval *) * (bvarraylen( bv ) + 1);
272 berval = HeapAlloc( GetProcessHeap(), 0, size );
276 struct berval **p = bv;
277 struct berval **q = berval;
279 while (*p) *q++ = bvdup( *p++ );
286 static inline void bvarrayfree( struct berval **bv )
288 struct berval **p = bv;
289 while (*p) HeapFree( GetProcessHeap(), 0, *p++ );
290 HeapFree( GetProcessHeap(), 0, bv );
293 static inline LDAPModW *modAtoW( LDAPModA *mod )
297 modW = HeapAlloc( GetProcessHeap(), 0, 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 modU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPMod) );
318 modU->mod_op = mod->mod_op;
319 modU->mod_type = strWtoU( mod->mod_type );
321 if (mod->mod_op & LDAP_MOD_BVALUES)
322 modU->mod_vals.modv_bvals = bvarraydup( mod->mod_vals.modv_bvals );
324 modU->mod_vals.modv_strvals = strarrayWtoU( mod->mod_vals.modv_strvals );
329 static inline void modfreeW( LDAPModW *mod )
331 if (mod->mod_op & LDAP_MOD_BVALUES)
332 bvarrayfree( mod->mod_vals.modv_bvals );
334 strarrayfreeW( mod->mod_vals.modv_strvals );
335 HeapFree( GetProcessHeap(), 0, mod );
338 static inline void modfreeU( LDAPMod *mod )
340 if (mod->mod_op & LDAP_MOD_BVALUES)
341 bvarrayfree( mod->mod_vals.modv_bvals );
343 strarrayfreeU( mod->mod_vals.modv_strvals );
344 HeapFree( GetProcessHeap(), 0, mod );
347 static inline DWORD modarraylenA( LDAPModA **modarray )
349 LDAPModA **p = modarray;
354 static inline DWORD modarraylenW( LDAPModW **modarray )
356 LDAPModW **p = modarray;
361 static inline LDAPModW **modarrayAtoW( LDAPModA **modarray )
363 LDAPModW **modarrayW = NULL;
368 size = sizeof(LDAPModW*) * (modarraylenA( modarray ) + 1);
369 modarrayW = HeapAlloc( GetProcessHeap(), 0, size );
373 LDAPModA **p = modarray;
374 LDAPModW **q = modarrayW;
376 while (*p) *q++ = modAtoW( *p++ );
383 static inline LDAPMod **modarrayWtoU( LDAPModW **modarray )
385 LDAPMod **modarrayU = NULL;
390 size = sizeof(LDAPMod*) * (modarraylenW( modarray ) + 1);
391 modarrayU = HeapAlloc( GetProcessHeap(), 0, size );
395 LDAPModW **p = modarray;
396 LDAPMod **q = modarrayU;
398 while (*p) *q++ = modWtoU( *p++ );
405 static inline void modarrayfreeW( LDAPModW **modarray )
409 LDAPModW **p = modarray;
410 while (*p) modfreeW( *p++ );
411 HeapFree( GetProcessHeap(), 0, modarray );
415 static inline void modarrayfreeU( LDAPMod **modarray )
419 LDAPMod **p = modarray;
420 while (*p) modfreeU( *p++ );
421 HeapFree( GetProcessHeap(), 0, modarray );
425 static inline LDAPControlW *controlAtoW( LDAPControlA *control )
427 LDAPControlW *controlW;
428 DWORD len = control->ldctl_value.bv_len;
431 if (control->ldctl_value.bv_val)
433 val = HeapAlloc( GetProcessHeap(), 0, len );
434 if (!val) return NULL;
435 memcpy( val, control->ldctl_value.bv_val, len );
438 controlW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW) );
441 HeapFree( GetProcessHeap(), 0, val );
445 controlW->ldctl_oid = strAtoW( control->ldctl_oid );
446 controlW->ldctl_value.bv_len = len;
447 controlW->ldctl_value.bv_val = val;
448 controlW->ldctl_iscritical = control->ldctl_iscritical;
453 static inline LDAPControlA *controlWtoA( LDAPControlW *control )
455 LDAPControlA *controlA;
456 DWORD len = control->ldctl_value.bv_len;
459 if (control->ldctl_value.bv_val)
461 val = HeapAlloc( GetProcessHeap(), 0, len );
462 if (!val) return NULL;
463 memcpy( val, control->ldctl_value.bv_val, len );
466 controlA = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlA) );
469 HeapFree( GetProcessHeap(), 0, val );
473 controlA->ldctl_oid = strWtoA( control->ldctl_oid );
474 controlA->ldctl_value.bv_len = len;
475 controlA->ldctl_value.bv_val = val;
476 controlA->ldctl_iscritical = control->ldctl_iscritical;
481 static inline LDAPControl *controlWtoU( LDAPControlW *control )
483 LDAPControl *controlU;
484 DWORD len = control->ldctl_value.bv_len;
487 if (control->ldctl_value.bv_val)
489 val = HeapAlloc( GetProcessHeap(), 0, len );
490 if (!val) return NULL;
491 memcpy( val, control->ldctl_value.bv_val, len );
494 controlU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControl) );
497 HeapFree( GetProcessHeap(), 0, val );
501 controlU->ldctl_oid = strWtoU( control->ldctl_oid );
502 controlU->ldctl_value.bv_len = len;
503 controlU->ldctl_value.bv_val = val;
504 controlU->ldctl_iscritical = control->ldctl_iscritical;
509 static inline LDAPControlW *controlUtoW( LDAPControl *control )
511 LDAPControlW *controlW;
512 DWORD len = control->ldctl_value.bv_len;
515 if (control->ldctl_value.bv_val)
517 val = HeapAlloc( GetProcessHeap(), 0, len );
518 if (!val) return NULL;
519 memcpy( val, control->ldctl_value.bv_val, len );
522 controlW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW) );
525 HeapFree( GetProcessHeap(), 0, val );
529 controlW->ldctl_oid = strUtoW( control->ldctl_oid );
530 controlW->ldctl_value.bv_len = len;
531 controlW->ldctl_value.bv_val = val;
532 controlW->ldctl_iscritical = control->ldctl_iscritical;
537 static inline void controlfreeA( LDAPControlA *control )
541 strfreeA( control->ldctl_oid );
542 HeapFree( GetProcessHeap(), 0, control->ldctl_value.bv_val );
543 HeapFree( GetProcessHeap(), 0, control );
547 static inline void controlfreeW( LDAPControlW *control )
551 strfreeW( control->ldctl_oid );
552 HeapFree( GetProcessHeap(), 0, control->ldctl_value.bv_val );
553 HeapFree( GetProcessHeap(), 0, control );
557 static inline void controlfreeU( LDAPControl *control )
561 strfreeU( control->ldctl_oid );
562 HeapFree( GetProcessHeap(), 0, control->ldctl_value.bv_val );
563 HeapFree( GetProcessHeap(), 0, control );
567 static inline DWORD controlarraylenA( LDAPControlA **controlarray )
569 LDAPControlA **p = controlarray;
571 return p - controlarray;
574 static inline DWORD controlarraylenW( LDAPControlW **controlarray )
576 LDAPControlW **p = controlarray;
578 return p - controlarray;
581 static inline DWORD controlarraylenU( LDAPControl **controlarray )
583 LDAPControl **p = controlarray;
585 return p - controlarray;
588 static inline LDAPControlW **controlarrayAtoW( LDAPControlA **controlarray )
590 LDAPControlW **controlarrayW = NULL;
595 size = sizeof(LDAPControlW*) * (controlarraylenA( controlarray ) + 1);
596 controlarrayW = HeapAlloc( GetProcessHeap(), 0, size );
600 LDAPControlA **p = controlarray;
601 LDAPControlW **q = controlarrayW;
603 while (*p) *q++ = controlAtoW( *p++ );
607 return controlarrayW;
610 static inline LDAPControlA **controlarrayWtoA( LDAPControlW **controlarray )
612 LDAPControlA **controlarrayA = NULL;
617 size = sizeof(LDAPControl*) * (controlarraylenW( controlarray ) + 1);
618 controlarrayA = HeapAlloc( GetProcessHeap(), 0, size );
622 LDAPControlW **p = controlarray;
623 LDAPControlA **q = controlarrayA;
625 while (*p) *q++ = controlWtoA( *p++ );
629 return controlarrayA;
632 static inline LDAPControl **controlarrayWtoU( LDAPControlW **controlarray )
634 LDAPControl **controlarrayU = NULL;
639 size = sizeof(LDAPControl*) * (controlarraylenW( controlarray ) + 1);
640 controlarrayU = HeapAlloc( GetProcessHeap(), 0, size );
644 LDAPControlW **p = controlarray;
645 LDAPControl **q = controlarrayU;
647 while (*p) *q++ = controlWtoU( *p++ );
651 return controlarrayU;
654 static inline LDAPControlW **controlarrayUtoW( LDAPControl **controlarray )
656 LDAPControlW **controlarrayW = NULL;
661 size = sizeof(LDAPControlW*) * (controlarraylenU( controlarray ) + 1);
662 controlarrayW = HeapAlloc( GetProcessHeap(), 0, size );
666 LDAPControl **p = controlarray;
667 LDAPControlW **q = controlarrayW;
669 while (*p) *q++ = controlUtoW( *p++ );
673 return controlarrayW;
676 static inline void controlarrayfreeA( LDAPControlA **controlarray )
680 LDAPControlA **p = controlarray;
681 while (*p) controlfreeA( *p++ );
682 HeapFree( GetProcessHeap(), 0, controlarray );
686 static inline void controlarrayfreeW( LDAPControlW **controlarray )
690 LDAPControlW **p = controlarray;
691 while (*p) controlfreeW( *p++ );
692 HeapFree( GetProcessHeap(), 0, controlarray );
696 static inline void controlarrayfreeU( LDAPControl **controlarray )
700 LDAPControl **p = controlarray;
701 while (*p) controlfreeU( *p++ );
702 HeapFree( GetProcessHeap(), 0, controlarray );
706 static inline LDAPSortKeyW *sortkeyAtoW( LDAPSortKeyA *sortkey )
708 LDAPSortKeyW *sortkeyW;
710 sortkeyW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyW) );
713 sortkeyW->sk_attrtype = strAtoW( sortkey->sk_attrtype );
714 sortkeyW->sk_matchruleoid = strAtoW( sortkey->sk_matchruleoid );
715 sortkeyW->sk_reverseorder = sortkey->sk_reverseorder;
720 static inline LDAPSortKeyA *sortkeyWtoA( LDAPSortKeyW *sortkey )
722 LDAPSortKeyA *sortkeyA;
724 sortkeyA = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyA) );
727 sortkeyA->sk_attrtype = strWtoA( sortkey->sk_attrtype );
728 sortkeyA->sk_matchruleoid = strWtoA( sortkey->sk_matchruleoid );
729 sortkeyA->sk_reverseorder = sortkey->sk_reverseorder;
734 static inline LDAPSortKey *sortkeyWtoU( LDAPSortKeyW *sortkey )
736 LDAPSortKey *sortkeyU;
738 sortkeyU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKey) );
741 sortkeyU->attributeType = strWtoU( sortkey->sk_attrtype );
742 sortkeyU->orderingRule = strWtoU( sortkey->sk_matchruleoid );
743 sortkeyU->reverseOrder = sortkey->sk_reverseorder;
748 static inline void sortkeyfreeA( LDAPSortKeyA *sortkey )
752 strfreeA( sortkey->sk_attrtype );
753 strfreeA( sortkey->sk_matchruleoid );
754 HeapFree( GetProcessHeap(), 0, sortkey );
758 static inline void sortkeyfreeW( LDAPSortKeyW *sortkey )
762 strfreeW( sortkey->sk_attrtype );
763 strfreeW( sortkey->sk_matchruleoid );
764 HeapFree( GetProcessHeap(), 0, sortkey );
768 static inline void sortkeyfreeU( LDAPSortKey *sortkey )
772 strfreeU( sortkey->attributeType );
773 strfreeU( sortkey->orderingRule );
774 HeapFree( GetProcessHeap(), 0, sortkey );
778 static inline DWORD sortkeyarraylenA( LDAPSortKeyA **sortkeyarray )
780 LDAPSortKeyA **p = sortkeyarray;
782 return p - sortkeyarray;
785 static inline DWORD sortkeyarraylenW( LDAPSortKeyW **sortkeyarray )
787 LDAPSortKeyW **p = sortkeyarray;
789 return p - sortkeyarray;
792 static inline LDAPSortKeyW **sortkeyarrayAtoW( LDAPSortKeyA **sortkeyarray )
794 LDAPSortKeyW **sortkeyarrayW = NULL;
799 size = sizeof(LDAPSortKeyW*) * (sortkeyarraylenA( sortkeyarray ) + 1);
800 sortkeyarrayW = HeapAlloc( GetProcessHeap(), 0, size );
804 LDAPSortKeyA **p = sortkeyarray;
805 LDAPSortKeyW **q = sortkeyarrayW;
807 while (*p) *q++ = sortkeyAtoW( *p++ );
811 return sortkeyarrayW;
814 static inline LDAPSortKey **sortkeyarrayWtoU( LDAPSortKeyW **sortkeyarray )
816 LDAPSortKey **sortkeyarrayU = NULL;
821 size = sizeof(LDAPSortKey*) * (sortkeyarraylenW( sortkeyarray ) + 1);
822 sortkeyarrayU = HeapAlloc( GetProcessHeap(), 0, size );
826 LDAPSortKeyW **p = sortkeyarray;
827 LDAPSortKey **q = sortkeyarrayU;
829 while (*p) *q++ = sortkeyWtoU( *p++ );
833 return sortkeyarrayU;
836 static inline void sortkeyarrayfreeW( LDAPSortKeyW **sortkeyarray )
840 LDAPSortKeyW **p = sortkeyarray;
841 while (*p) sortkeyfreeW( *p++ );
842 HeapFree( GetProcessHeap(), 0, sortkeyarray );
846 static inline void sortkeyarrayfreeU( LDAPSortKey **sortkeyarray )
850 LDAPSortKey **p = sortkeyarray;
851 while (*p) sortkeyfreeU( *p++ );
852 HeapFree( GetProcessHeap(), 0, sortkeyarray );
856 #endif /* HAVE_LDAP */