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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 LPWSTR strAtoW( LPCSTR str )
30 DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
31 if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
32 MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
37 static inline LPSTR strWtoA( LPCWSTR str )
42 DWORD len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
43 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
44 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
49 static inline char *strWtoU( LPCWSTR str )
54 DWORD len = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
55 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
56 WideCharToMultiByte( CP_UTF8, 0, str, -1, ret, len, NULL, NULL );
61 static inline LPWSTR strUtoW( char *str )
66 DWORD len = MultiByteToWideChar( CP_UTF8, 0, str, -1, NULL, 0 );
67 if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
68 MultiByteToWideChar( CP_UTF8, 0, str, -1, ret, len );
73 static inline void strfreeA( LPSTR str )
75 HeapFree( GetProcessHeap(), 0, str );
78 static inline void strfreeW( LPWSTR str )
80 HeapFree( GetProcessHeap(), 0, str );
83 static inline void strfreeU( char *str )
85 HeapFree( GetProcessHeap(), 0, str );
88 static inline DWORD strarraylenA( LPSTR *strarray )
95 static inline DWORD strarraylenW( LPWSTR *strarray )
102 static inline DWORD strarraylenU( char **strarray )
109 static inline LPWSTR *strarrayAtoW( LPSTR *strarray )
111 LPWSTR *strarrayW = NULL;
116 size = sizeof(WCHAR*) * (strarraylenA( strarray ) + 1);
117 strarrayW = HeapAlloc( GetProcessHeap(), 0, size );
122 LPWSTR *q = strarrayW;
124 while (*p) *q++ = strAtoW( *p++ );
131 static inline LPSTR *strarrayWtoA( LPWSTR *strarray )
133 LPSTR *strarrayA = NULL;
138 size = sizeof(LPSTR) * (strarraylenW( strarray ) + 1);
139 strarrayA = HeapAlloc( GetProcessHeap(), 0, size );
143 LPWSTR *p = strarray;
144 LPSTR *q = strarrayA;
146 while (*p) *q++ = strWtoA( *p++ );
153 static inline char **strarrayWtoU( LPWSTR *strarray )
155 char **strarrayU = NULL;
160 size = sizeof(char*) * (strarraylenW( strarray ) + 1);
161 strarrayU = HeapAlloc( GetProcessHeap(), 0, size );
165 LPWSTR *p = strarray;
166 char **q = strarrayU;
168 while (*p) *q++ = strWtoU( *p++ );
175 static inline LPWSTR *strarrayUtoW( char **strarray )
177 LPWSTR *strarrayW = NULL;
182 size = sizeof(WCHAR*) * (strarraylenU( strarray ) + 1);
183 strarrayW = HeapAlloc( GetProcessHeap(), 0, size );
188 LPWSTR *q = strarrayW;
190 while (*p) *q++ = strUtoW( *p++ );
197 static inline void strarrayfreeA( LPSTR *strarray )
202 while (*p) strfreeA( *p++ );
203 HeapFree( GetProcessHeap(), 0, strarray );
207 static inline void strarrayfreeW( LPWSTR *strarray )
211 LPWSTR *p = strarray;
212 while (*p) strfreeW( *p++ );
213 HeapFree( GetProcessHeap(), 0, strarray );
217 static inline void strarrayfreeU( char **strarray )
222 while (*p) strfreeU( *p++ );
223 HeapFree( GetProcessHeap(), 0, strarray );
229 static inline LDAPControlW *controlAtoW( LDAPControlA *control )
231 LDAPControlW *controlW;
233 controlW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW) );
236 memcpy( controlW, control, sizeof(LDAPControlW) );
237 controlW->ldctl_oid = strAtoW( control->ldctl_oid );
242 static inline LDAPControlA *controlWtoA( LDAPControlW *control )
244 LDAPControlA *controlA;
246 controlA = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControl) );
249 memcpy( controlA, control, sizeof(LDAPControlA) );
250 controlA->ldctl_oid = strWtoA( control->ldctl_oid );
255 static inline LDAPControl *controlWtoU( LDAPControlW *control )
257 LDAPControl *controlU;
259 controlU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControl) );
262 memcpy( controlU, control, sizeof(LDAPControl) );
263 controlU->ldctl_oid = strWtoU( control->ldctl_oid );
268 static inline LDAPControlW *controlUtoW( LDAPControl *control )
270 LDAPControlW *controlW;
272 controlW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW) );
275 memcpy( controlW, control, sizeof(LDAPControlW) );
276 controlW->ldctl_oid = strUtoW( control->ldctl_oid );
281 static inline void controlfreeA( LDAPControlA *control )
285 strfreeA( control->ldctl_oid );
286 HeapFree( GetProcessHeap(), 0, control );
290 static inline void controlfreeW( LDAPControlW *control )
294 strfreeW( control->ldctl_oid );
295 HeapFree( GetProcessHeap(), 0, control );
299 static inline void controlfreeU( LDAPControl *control )
303 strfreeU( control->ldctl_oid );
304 HeapFree( GetProcessHeap(), 0, control );
308 static inline DWORD controlarraylenA( LDAPControlA **controlarray )
310 LDAPControlA **p = controlarray;
312 return p - controlarray;
315 static inline DWORD controlarraylenW( LDAPControlW **controlarray )
317 LDAPControlW **p = controlarray;
319 return p - controlarray;
322 static inline DWORD controlarraylenU( LDAPControl **controlarray )
324 LDAPControl **p = controlarray;
326 return p - controlarray;
329 static inline LDAPControlW **controlarrayAtoW( LDAPControlA **controlarray )
331 LDAPControlW **controlarrayW = NULL;
336 size = sizeof(LDAPControlW*) * (controlarraylenA( controlarray ) + 1);
337 controlarrayW = HeapAlloc( GetProcessHeap(), 0, size );
341 LDAPControlA **p = controlarray;
342 LDAPControlW **q = controlarrayW;
344 while (*p) *q++ = controlAtoW( *p++ );
348 return controlarrayW;
351 static inline LDAPControlA **controlarrayWtoA( LDAPControlW **controlarray )
353 LDAPControlA **controlarrayA = NULL;
358 size = sizeof(LDAPControl*) * (controlarraylenW( controlarray ) + 1);
359 controlarrayA = HeapAlloc( GetProcessHeap(), 0, size );
363 LDAPControlW **p = controlarray;
364 LDAPControlA **q = controlarrayA;
366 while (*p) *q++ = controlWtoA( *p++ );
370 return controlarrayA;
373 static inline LDAPControl **controlarrayWtoU( LDAPControlW **controlarray )
375 LDAPControl **controlarrayU = NULL;
380 size = sizeof(LDAPControl*) * (controlarraylenW( controlarray ) + 1);
381 controlarrayU = HeapAlloc( GetProcessHeap(), 0, size );
385 LDAPControlW **p = controlarray;
386 LDAPControl **q = controlarrayU;
388 while (*p) *q++ = controlWtoU( *p++ );
392 return controlarrayU;
395 static inline LDAPControlW **controlarrayUtoW( LDAPControl **controlarray )
397 LDAPControlW **controlarrayW = NULL;
402 size = sizeof(LDAPControlW*) * (controlarraylenU( controlarray ) + 1);
403 controlarrayW = HeapAlloc( GetProcessHeap(), 0, size );
407 LDAPControl **p = controlarray;
408 LDAPControlW **q = controlarrayW;
410 while (*p) *q++ = controlUtoW( *p++ );
414 return controlarrayW;
417 static inline void controlarrayfreeA( LDAPControlA **controlarray )
421 LDAPControlA **p = controlarray;
422 while (*p) controlfreeA( *p++ );
423 HeapFree( GetProcessHeap(), 0, controlarray );
427 static inline void controlarrayfreeW( LDAPControlW **controlarray )
431 LDAPControlW **p = controlarray;
432 while (*p) controlfreeW( *p++ );
433 HeapFree( GetProcessHeap(), 0, controlarray );
437 static inline void controlarrayfreeU( LDAPControl **controlarray )
441 LDAPControl **p = controlarray;
442 while (*p) controlfreeU( *p++ );
443 HeapFree( GetProcessHeap(), 0, controlarray );
447 static inline LDAPSortKeyW *sortkeyAtoW( LDAPSortKeyA *sortkey )
449 LDAPSortKeyW *sortkeyW;
451 sortkeyW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyW) );
454 sortkeyW->sk_attrtype = strAtoW( sortkey->sk_attrtype );
455 sortkeyW->sk_matchruleoid = strAtoW( sortkey->sk_matchruleoid );
456 sortkeyW->sk_reverseorder = sortkey->sk_reverseorder;
461 static inline LDAPSortKeyA *sortkeyWtoA( LDAPSortKeyW *sortkey )
463 LDAPSortKeyA *sortkeyA;
465 sortkeyA = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyA) );
468 sortkeyA->sk_attrtype = strWtoA( sortkey->sk_attrtype );
469 sortkeyA->sk_matchruleoid = strWtoA( sortkey->sk_matchruleoid );
470 sortkeyA->sk_reverseorder = sortkey->sk_reverseorder;
475 static inline LDAPSortKey *sortkeyWtoU( LDAPSortKeyW *sortkey )
477 LDAPSortKey *sortkeyU;
479 sortkeyU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKey) );
482 sortkeyU->attributeType = strWtoU( sortkey->sk_attrtype );
483 sortkeyU->orderingRule = strWtoU( sortkey->sk_matchruleoid );
484 sortkeyU->reverseOrder = sortkey->sk_reverseorder;
489 static inline void sortkeyfreeA( LDAPSortKeyA *sortkey )
493 strfreeA( sortkey->sk_attrtype );
494 strfreeA( sortkey->sk_matchruleoid );
495 HeapFree( GetProcessHeap(), 0, sortkey );
499 static inline void sortkeyfreeW( LDAPSortKeyW *sortkey )
503 strfreeW( sortkey->sk_attrtype );
504 strfreeW( sortkey->sk_matchruleoid );
505 HeapFree( GetProcessHeap(), 0, sortkey );
509 static inline void sortkeyfreeU( LDAPSortKey *sortkey )
513 strfreeU( sortkey->attributeType );
514 strfreeU( sortkey->orderingRule );
515 HeapFree( GetProcessHeap(), 0, sortkey );
519 static inline DWORD sortkeyarraylenA( LDAPSortKeyA **sortkeyarray )
521 LDAPSortKeyA **p = sortkeyarray;
523 return p - sortkeyarray;
526 static inline DWORD sortkeyarraylenW( LDAPSortKeyW **sortkeyarray )
528 LDAPSortKeyW **p = sortkeyarray;
530 return p - sortkeyarray;
533 static inline LDAPSortKeyW **sortkeyarrayAtoW( LDAPSortKeyA **sortkeyarray )
535 LDAPSortKeyW **sortkeyarrayW = NULL;
540 size = sizeof(LDAPSortKeyW*) * (sortkeyarraylenA( sortkeyarray ) + 1);
541 sortkeyarrayW = HeapAlloc( GetProcessHeap(), 0, size );
545 LDAPSortKeyA **p = sortkeyarray;
546 LDAPSortKeyW **q = sortkeyarrayW;
548 while (*p) *q++ = sortkeyAtoW( *p++ );
552 return sortkeyarrayW;
555 static inline LDAPSortKey **sortkeyarrayWtoU( LDAPSortKeyW **sortkeyarray )
557 LDAPSortKey **sortkeyarrayU = NULL;
562 size = sizeof(LDAPSortKey*) * (sortkeyarraylenW( sortkeyarray ) + 1);
563 sortkeyarrayU = HeapAlloc( GetProcessHeap(), 0, size );
567 LDAPSortKeyW **p = sortkeyarray;
568 LDAPSortKey **q = sortkeyarrayU;
570 while (*p) *q++ = sortkeyWtoU( *p++ );
574 return sortkeyarrayU;
577 static inline void sortkeyarrayfreeW( LDAPSortKeyW **sortkeyarray )
581 LDAPSortKeyW **p = sortkeyarray;
582 while (*p) sortkeyfreeW( *p++ );
583 HeapFree( GetProcessHeap(), 0, sortkeyarray );
587 static inline void sortkeyarrayfreeU( LDAPSortKey **sortkeyarray )
591 LDAPSortKey **p = sortkeyarray;
592 while (*p) sortkeyfreeU( *p++ );
593 HeapFree( GetProcessHeap(), 0, sortkeyarray );
597 static inline LDAPAPIInfoW *infoAtoW( LDAPAPIInfoA *info )
601 infoW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPAPIInfoW) );
604 memcpy( infoW, info, sizeof(LDAPAPIInfoA) );
605 infoW->ldapai_extensions = strarrayAtoW( info->ldapai_extensions );
606 infoW->ldapai_vendor_name = strAtoW( info->ldapai_vendor_name );
611 static inline LDAPAPIInfoA *infoWtoA( LDAPAPIInfoW *info )
615 infoA = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPAPIInfoA) );
618 memcpy( infoA, info, sizeof(LDAPAPIInfoW) );
619 infoA->ldapai_extensions = strarrayWtoA( info->ldapai_extensions );
620 infoA->ldapai_vendor_name = strWtoA( info->ldapai_vendor_name );
625 static inline LDAPAPIInfoW *infoUtoW( LDAPAPIInfo *info )
629 infoW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPAPIInfoW) );
632 memcpy( infoW, info, sizeof(LDAPAPIInfo) );
633 infoW->ldapai_extensions = strarrayUtoW( info->ldapai_extensions );
634 infoW->ldapai_vendor_name = strUtoW( info->ldapai_vendor_name );
639 static inline LDAPAPIInfo *infoWtoU( LDAPAPIInfoW *info )
643 infoU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPAPIInfo) );
646 memcpy( infoU, info, sizeof(LDAPAPIInfoW) );
647 infoU->ldapai_extensions = strarrayWtoU( info->ldapai_extensions );
648 infoU->ldapai_vendor_name = strWtoU( info->ldapai_vendor_name );
653 static inline void infofreeW( LDAPAPIInfoW *info )
657 strarrayfreeW( info->ldapai_extensions );
658 strfreeW( info->ldapai_vendor_name );
659 HeapFree( GetProcessHeap(), 0, info );
663 static inline void infofreeU( LDAPAPIInfo *info )
667 strarrayfreeU( info->ldapai_extensions );
668 strfreeU( info->ldapai_vendor_name );
669 HeapFree( GetProcessHeap(), 0, info );
673 static inline LDAPAPIFeatureInfoW *featureinfoAtoW( LDAPAPIFeatureInfoA *featureinfo )
675 LDAPAPIFeatureInfoW *featureinfoW;
677 featureinfoW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPAPIFeatureInfoW) );
680 featureinfoW->ldapaif_info_version = featureinfo->ldapaif_info_version;
681 featureinfoW->ldapaif_name = strAtoW( featureinfo->ldapaif_name );
682 featureinfoW->ldapaif_version = featureinfo->ldapaif_version;
687 static inline LDAPAPIFeatureInfoA *featureinfoWtoA( LDAPAPIFeatureInfoW *featureinfo )
689 LDAPAPIFeatureInfoA *featureinfoA;
691 featureinfoA = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPAPIFeatureInfoA) );
694 featureinfoA->ldapaif_info_version = featureinfo->ldapaif_info_version;
695 featureinfoA->ldapaif_name = strWtoA( featureinfo->ldapaif_name );
696 featureinfoA->ldapaif_version = featureinfo->ldapaif_version;
701 static inline LDAPAPIFeatureInfoW *featureinfoUtoW( LDAPAPIFeatureInfo *featureinfo )
703 LDAPAPIFeatureInfoW *featureinfoW;
705 featureinfoW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPAPIFeatureInfoW) );
708 featureinfoW->ldapaif_info_version = featureinfo->ldapaif_info_version;
709 featureinfoW->ldapaif_name = strUtoW( featureinfo->ldapaif_name );
710 featureinfoW->ldapaif_version = featureinfo->ldapaif_version;
715 static inline LDAPAPIFeatureInfo *featureinfoWtoU( LDAPAPIFeatureInfoW *featureinfo )
717 LDAPAPIFeatureInfo *featureinfoU;
719 featureinfoU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPAPIFeatureInfo) );
722 featureinfoU->ldapaif_info_version = featureinfo->ldapaif_info_version;
723 featureinfoU->ldapaif_name = strWtoU( featureinfo->ldapaif_name );
724 featureinfoU->ldapaif_version = featureinfo->ldapaif_version;
729 static inline void featureinfofreeW( LDAPAPIFeatureInfoW *featureinfo )
733 strfreeW( featureinfo->ldapaif_name );
734 HeapFree( GetProcessHeap(), 0, featureinfo );
738 static inline void featureinfofreeU( LDAPAPIFeatureInfo *featureinfo )
742 strfreeU( featureinfo->ldapaif_name );
743 HeapFree( GetProcessHeap(), 0, featureinfo );
747 #endif /* HAVE_LDAP */