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 LDAPModW
*modAtoW( LDAPModA
*mod
)
233 modW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPModW
) );
236 modW
->mod_op
= mod
->mod_op
;
237 modW
->mod_type
= strAtoW( mod
->mod_type
);
239 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
240 modW
->mod_vals
.modv_bvals
= mod
->mod_vals
.modv_bvals
;
242 modW
->mod_vals
.modv_strvals
= strarrayAtoW( mod
->mod_vals
.modv_strvals
);
247 static inline LDAPMod
*modWtoU( LDAPModW
*mod
)
251 modU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPMod
) );
254 modU
->mod_op
= mod
->mod_op
;
255 modU
->mod_type
= strWtoU( mod
->mod_type
);
257 if (mod
->mod_op
& LDAP_MOD_BVALUES
)
258 modU
->mod_vals
.modv_bvals
= mod
->mod_vals
.modv_bvals
;
260 modU
->mod_vals
.modv_strvals
= strarrayWtoU( mod
->mod_vals
.modv_strvals
);
265 static inline void modfreeW( LDAPModW
*mod
)
267 if (!(mod
->mod_op
& LDAP_MOD_BVALUES
))
268 strarrayfreeW( mod
->mod_vals
.modv_strvals
);
269 HeapFree( GetProcessHeap(), 0, mod
);
272 static inline void modfreeU( LDAPMod
*mod
)
274 if (!(mod
->mod_op
& LDAP_MOD_BVALUES
))
275 strarrayfreeU( mod
->mod_vals
.modv_strvals
);
276 HeapFree( GetProcessHeap(), 0, mod
);
279 static inline DWORD
modarraylenA( LDAPModA
**modarray
)
281 LDAPModA
**p
= modarray
;
286 static inline DWORD
modarraylenW( LDAPModW
**modarray
)
288 LDAPModW
**p
= modarray
;
293 static inline LDAPModW
**modarrayAtoW( LDAPModA
**modarray
)
295 LDAPModW
**modarrayW
= NULL
;
300 size
= sizeof(LDAPModW
*) * (modarraylenA( modarray
) + 1);
301 modarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
305 LDAPModA
**p
= modarray
;
306 LDAPModW
**q
= modarrayW
;
308 while (*p
) *q
++ = modAtoW( *p
++ );
315 static inline LDAPMod
**modarrayWtoU( LDAPModW
**modarray
)
317 LDAPMod
**modarrayU
= NULL
;
322 size
= sizeof(LDAPMod
*) * (modarraylenW( modarray
) + 1);
323 modarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
327 LDAPModW
**p
= modarray
;
328 LDAPMod
**q
= modarrayU
;
330 while (*p
) *q
++ = modWtoU( *p
++ );
337 static inline void modarrayfreeW( LDAPModW
**modarray
)
341 LDAPModW
**p
= modarray
;
342 while (*p
) modfreeW( *p
++ );
343 HeapFree( GetProcessHeap(), 0, modarray
);
347 static inline void modarrayfreeU( LDAPMod
**modarray
)
351 LDAPMod
**p
= modarray
;
352 while (*p
) modfreeU( *p
++ );
353 HeapFree( GetProcessHeap(), 0, modarray
);
357 static inline LDAPControlW
*controlAtoW( LDAPControlA
*control
)
359 LDAPControlW
*controlW
;
361 controlW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW
) );
364 memcpy( controlW
, control
, sizeof(LDAPControlW
) );
365 controlW
->ldctl_oid
= strAtoW( control
->ldctl_oid
);
370 static inline LDAPControlA
*controlWtoA( LDAPControlW
*control
)
372 LDAPControlA
*controlA
;
374 controlA
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControl
) );
377 memcpy( controlA
, control
, sizeof(LDAPControlA
) );
378 controlA
->ldctl_oid
= strWtoA( control
->ldctl_oid
);
383 static inline LDAPControl
*controlWtoU( LDAPControlW
*control
)
385 LDAPControl
*controlU
;
387 controlU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControl
) );
390 memcpy( controlU
, control
, sizeof(LDAPControl
) );
391 controlU
->ldctl_oid
= strWtoU( control
->ldctl_oid
);
396 static inline LDAPControlW
*controlUtoW( LDAPControl
*control
)
398 LDAPControlW
*controlW
;
400 controlW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW
) );
403 memcpy( controlW
, control
, sizeof(LDAPControlW
) );
404 controlW
->ldctl_oid
= strUtoW( control
->ldctl_oid
);
409 static inline void controlfreeA( LDAPControlA
*control
)
413 strfreeA( control
->ldctl_oid
);
414 HeapFree( GetProcessHeap(), 0, control
);
418 static inline void controlfreeW( LDAPControlW
*control
)
422 strfreeW( control
->ldctl_oid
);
423 HeapFree( GetProcessHeap(), 0, control
);
427 static inline void controlfreeU( LDAPControl
*control
)
431 strfreeU( control
->ldctl_oid
);
432 HeapFree( GetProcessHeap(), 0, control
);
436 static inline DWORD
controlarraylenA( LDAPControlA
**controlarray
)
438 LDAPControlA
**p
= controlarray
;
440 return p
- controlarray
;
443 static inline DWORD
controlarraylenW( LDAPControlW
**controlarray
)
445 LDAPControlW
**p
= controlarray
;
447 return p
- controlarray
;
450 static inline DWORD
controlarraylenU( LDAPControl
**controlarray
)
452 LDAPControl
**p
= controlarray
;
454 return p
- controlarray
;
457 static inline LDAPControlW
**controlarrayAtoW( LDAPControlA
**controlarray
)
459 LDAPControlW
**controlarrayW
= NULL
;
464 size
= sizeof(LDAPControlW
*) * (controlarraylenA( controlarray
) + 1);
465 controlarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
469 LDAPControlA
**p
= controlarray
;
470 LDAPControlW
**q
= controlarrayW
;
472 while (*p
) *q
++ = controlAtoW( *p
++ );
476 return controlarrayW
;
479 static inline LDAPControlA
**controlarrayWtoA( LDAPControlW
**controlarray
)
481 LDAPControlA
**controlarrayA
= NULL
;
486 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
487 controlarrayA
= HeapAlloc( GetProcessHeap(), 0, size
);
491 LDAPControlW
**p
= controlarray
;
492 LDAPControlA
**q
= controlarrayA
;
494 while (*p
) *q
++ = controlWtoA( *p
++ );
498 return controlarrayA
;
501 static inline LDAPControl
**controlarrayWtoU( LDAPControlW
**controlarray
)
503 LDAPControl
**controlarrayU
= NULL
;
508 size
= sizeof(LDAPControl
*) * (controlarraylenW( controlarray
) + 1);
509 controlarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
513 LDAPControlW
**p
= controlarray
;
514 LDAPControl
**q
= controlarrayU
;
516 while (*p
) *q
++ = controlWtoU( *p
++ );
520 return controlarrayU
;
523 static inline LDAPControlW
**controlarrayUtoW( LDAPControl
**controlarray
)
525 LDAPControlW
**controlarrayW
= NULL
;
530 size
= sizeof(LDAPControlW
*) * (controlarraylenU( controlarray
) + 1);
531 controlarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
535 LDAPControl
**p
= controlarray
;
536 LDAPControlW
**q
= controlarrayW
;
538 while (*p
) *q
++ = controlUtoW( *p
++ );
542 return controlarrayW
;
545 static inline void controlarrayfreeA( LDAPControlA
**controlarray
)
549 LDAPControlA
**p
= controlarray
;
550 while (*p
) controlfreeA( *p
++ );
551 HeapFree( GetProcessHeap(), 0, controlarray
);
555 static inline void controlarrayfreeW( LDAPControlW
**controlarray
)
559 LDAPControlW
**p
= controlarray
;
560 while (*p
) controlfreeW( *p
++ );
561 HeapFree( GetProcessHeap(), 0, controlarray
);
565 static inline void controlarrayfreeU( LDAPControl
**controlarray
)
569 LDAPControl
**p
= controlarray
;
570 while (*p
) controlfreeU( *p
++ );
571 HeapFree( GetProcessHeap(), 0, controlarray
);
575 static inline LDAPSortKeyW
*sortkeyAtoW( LDAPSortKeyA
*sortkey
)
577 LDAPSortKeyW
*sortkeyW
;
579 sortkeyW
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyW
) );
582 sortkeyW
->sk_attrtype
= strAtoW( sortkey
->sk_attrtype
);
583 sortkeyW
->sk_matchruleoid
= strAtoW( sortkey
->sk_matchruleoid
);
584 sortkeyW
->sk_reverseorder
= sortkey
->sk_reverseorder
;
589 static inline LDAPSortKeyA
*sortkeyWtoA( LDAPSortKeyW
*sortkey
)
591 LDAPSortKeyA
*sortkeyA
;
593 sortkeyA
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyA
) );
596 sortkeyA
->sk_attrtype
= strWtoA( sortkey
->sk_attrtype
);
597 sortkeyA
->sk_matchruleoid
= strWtoA( sortkey
->sk_matchruleoid
);
598 sortkeyA
->sk_reverseorder
= sortkey
->sk_reverseorder
;
603 static inline LDAPSortKey
*sortkeyWtoU( LDAPSortKeyW
*sortkey
)
605 LDAPSortKey
*sortkeyU
;
607 sortkeyU
= HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKey
) );
610 sortkeyU
->attributeType
= strWtoU( sortkey
->sk_attrtype
);
611 sortkeyU
->orderingRule
= strWtoU( sortkey
->sk_matchruleoid
);
612 sortkeyU
->reverseOrder
= sortkey
->sk_reverseorder
;
617 static inline void sortkeyfreeA( LDAPSortKeyA
*sortkey
)
621 strfreeA( sortkey
->sk_attrtype
);
622 strfreeA( sortkey
->sk_matchruleoid
);
623 HeapFree( GetProcessHeap(), 0, sortkey
);
627 static inline void sortkeyfreeW( LDAPSortKeyW
*sortkey
)
631 strfreeW( sortkey
->sk_attrtype
);
632 strfreeW( sortkey
->sk_matchruleoid
);
633 HeapFree( GetProcessHeap(), 0, sortkey
);
637 static inline void sortkeyfreeU( LDAPSortKey
*sortkey
)
641 strfreeU( sortkey
->attributeType
);
642 strfreeU( sortkey
->orderingRule
);
643 HeapFree( GetProcessHeap(), 0, sortkey
);
647 static inline DWORD
sortkeyarraylenA( LDAPSortKeyA
**sortkeyarray
)
649 LDAPSortKeyA
**p
= sortkeyarray
;
651 return p
- sortkeyarray
;
654 static inline DWORD
sortkeyarraylenW( LDAPSortKeyW
**sortkeyarray
)
656 LDAPSortKeyW
**p
= sortkeyarray
;
658 return p
- sortkeyarray
;
661 static inline LDAPSortKeyW
**sortkeyarrayAtoW( LDAPSortKeyA
**sortkeyarray
)
663 LDAPSortKeyW
**sortkeyarrayW
= NULL
;
668 size
= sizeof(LDAPSortKeyW
*) * (sortkeyarraylenA( sortkeyarray
) + 1);
669 sortkeyarrayW
= HeapAlloc( GetProcessHeap(), 0, size
);
673 LDAPSortKeyA
**p
= sortkeyarray
;
674 LDAPSortKeyW
**q
= sortkeyarrayW
;
676 while (*p
) *q
++ = sortkeyAtoW( *p
++ );
680 return sortkeyarrayW
;
683 static inline LDAPSortKey
**sortkeyarrayWtoU( LDAPSortKeyW
**sortkeyarray
)
685 LDAPSortKey
**sortkeyarrayU
= NULL
;
690 size
= sizeof(LDAPSortKey
*) * (sortkeyarraylenW( sortkeyarray
) + 1);
691 sortkeyarrayU
= HeapAlloc( GetProcessHeap(), 0, size
);
695 LDAPSortKeyW
**p
= sortkeyarray
;
696 LDAPSortKey
**q
= sortkeyarrayU
;
698 while (*p
) *q
++ = sortkeyWtoU( *p
++ );
702 return sortkeyarrayU
;
705 static inline void sortkeyarrayfreeW( LDAPSortKeyW
**sortkeyarray
)
709 LDAPSortKeyW
**p
= sortkeyarray
;
710 while (*p
) sortkeyfreeW( *p
++ );
711 HeapFree( GetProcessHeap(), 0, sortkeyarray
);
715 static inline void sortkeyarrayfreeU( LDAPSortKey
**sortkeyarray
)
719 LDAPSortKey
**p
= sortkeyarray
;
720 while (*p
) sortkeyfreeU( *p
++ );
721 HeapFree( GetProcessHeap(), 0, sortkeyarray
);
725 #endif /* HAVE_LDAP */