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 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 LDAPModW *modAtoW( LDAPModA *mod )
244 modW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPModW) );
247 modW->mod_op = mod->mod_op;
248 modW->mod_type = strAtoW( mod->mod_type );
250 if (mod->mod_op & LDAP_MOD_BVALUES)
251 modW->mod_vals.modv_bvals = mod->mod_vals.modv_bvals;
253 modW->mod_vals.modv_strvals = strarrayAtoW( mod->mod_vals.modv_strvals );
258 static inline LDAPMod *modWtoU( LDAPModW *mod )
262 modU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPMod) );
265 modU->mod_op = mod->mod_op;
266 modU->mod_type = strWtoU( mod->mod_type );
268 if (mod->mod_op & LDAP_MOD_BVALUES)
269 modU->mod_vals.modv_bvals = mod->mod_vals.modv_bvals;
271 modU->mod_vals.modv_strvals = strarrayWtoU( mod->mod_vals.modv_strvals );
276 static inline void modfreeW( LDAPModW *mod )
278 if (!(mod->mod_op & LDAP_MOD_BVALUES))
279 strarrayfreeW( mod->mod_vals.modv_strvals );
280 HeapFree( GetProcessHeap(), 0, mod );
283 static inline void modfreeU( LDAPMod *mod )
285 if (!(mod->mod_op & LDAP_MOD_BVALUES))
286 strarrayfreeU( mod->mod_vals.modv_strvals );
287 HeapFree( GetProcessHeap(), 0, mod );
290 static inline DWORD modarraylenA( LDAPModA **modarray )
292 LDAPModA **p = modarray;
297 static inline DWORD modarraylenW( LDAPModW **modarray )
299 LDAPModW **p = modarray;
304 static inline LDAPModW **modarrayAtoW( LDAPModA **modarray )
306 LDAPModW **modarrayW = NULL;
311 size = sizeof(LDAPModW*) * (modarraylenA( modarray ) + 1);
312 modarrayW = HeapAlloc( GetProcessHeap(), 0, size );
316 LDAPModA **p = modarray;
317 LDAPModW **q = modarrayW;
319 while (*p) *q++ = modAtoW( *p++ );
326 static inline LDAPMod **modarrayWtoU( LDAPModW **modarray )
328 LDAPMod **modarrayU = NULL;
333 size = sizeof(LDAPMod*) * (modarraylenW( modarray ) + 1);
334 modarrayU = HeapAlloc( GetProcessHeap(), 0, size );
338 LDAPModW **p = modarray;
339 LDAPMod **q = modarrayU;
341 while (*p) *q++ = modWtoU( *p++ );
348 static inline void modarrayfreeW( LDAPModW **modarray )
352 LDAPModW **p = modarray;
353 while (*p) modfreeW( *p++ );
354 HeapFree( GetProcessHeap(), 0, modarray );
358 static inline void modarrayfreeU( LDAPMod **modarray )
362 LDAPMod **p = modarray;
363 while (*p) modfreeU( *p++ );
364 HeapFree( GetProcessHeap(), 0, modarray );
368 static inline LDAPControlW *controlAtoW( LDAPControlA *control )
370 LDAPControlW *controlW;
372 controlW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW) );
375 memcpy( controlW, control, sizeof(LDAPControlW) );
376 controlW->ldctl_oid = strAtoW( control->ldctl_oid );
381 static inline LDAPControlA *controlWtoA( LDAPControlW *control )
383 LDAPControlA *controlA;
385 controlA = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControl) );
388 memcpy( controlA, control, sizeof(LDAPControlA) );
389 controlA->ldctl_oid = strWtoA( control->ldctl_oid );
394 static inline LDAPControl *controlWtoU( LDAPControlW *control )
396 LDAPControl *controlU;
398 controlU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControl) );
401 memcpy( controlU, control, sizeof(LDAPControl) );
402 controlU->ldctl_oid = strWtoU( control->ldctl_oid );
407 static inline LDAPControlW *controlUtoW( LDAPControl *control )
409 LDAPControlW *controlW;
411 controlW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW) );
414 memcpy( controlW, control, sizeof(LDAPControlW) );
415 controlW->ldctl_oid = strUtoW( control->ldctl_oid );
420 static inline void controlfreeA( LDAPControlA *control )
424 strfreeA( control->ldctl_oid );
425 HeapFree( GetProcessHeap(), 0, control );
429 static inline void controlfreeW( LDAPControlW *control )
433 strfreeW( control->ldctl_oid );
434 HeapFree( GetProcessHeap(), 0, control );
438 static inline void controlfreeU( LDAPControl *control )
442 strfreeU( control->ldctl_oid );
443 HeapFree( GetProcessHeap(), 0, control );
447 static inline DWORD controlarraylenA( LDAPControlA **controlarray )
449 LDAPControlA **p = controlarray;
451 return p - controlarray;
454 static inline DWORD controlarraylenW( LDAPControlW **controlarray )
456 LDAPControlW **p = controlarray;
458 return p - controlarray;
461 static inline DWORD controlarraylenU( LDAPControl **controlarray )
463 LDAPControl **p = controlarray;
465 return p - controlarray;
468 static inline LDAPControlW **controlarrayAtoW( LDAPControlA **controlarray )
470 LDAPControlW **controlarrayW = NULL;
475 size = sizeof(LDAPControlW*) * (controlarraylenA( controlarray ) + 1);
476 controlarrayW = HeapAlloc( GetProcessHeap(), 0, size );
480 LDAPControlA **p = controlarray;
481 LDAPControlW **q = controlarrayW;
483 while (*p) *q++ = controlAtoW( *p++ );
487 return controlarrayW;
490 static inline LDAPControlA **controlarrayWtoA( LDAPControlW **controlarray )
492 LDAPControlA **controlarrayA = NULL;
497 size = sizeof(LDAPControl*) * (controlarraylenW( controlarray ) + 1);
498 controlarrayA = HeapAlloc( GetProcessHeap(), 0, size );
502 LDAPControlW **p = controlarray;
503 LDAPControlA **q = controlarrayA;
505 while (*p) *q++ = controlWtoA( *p++ );
509 return controlarrayA;
512 static inline LDAPControl **controlarrayWtoU( LDAPControlW **controlarray )
514 LDAPControl **controlarrayU = NULL;
519 size = sizeof(LDAPControl*) * (controlarraylenW( controlarray ) + 1);
520 controlarrayU = HeapAlloc( GetProcessHeap(), 0, size );
524 LDAPControlW **p = controlarray;
525 LDAPControl **q = controlarrayU;
527 while (*p) *q++ = controlWtoU( *p++ );
531 return controlarrayU;
534 static inline LDAPControlW **controlarrayUtoW( LDAPControl **controlarray )
536 LDAPControlW **controlarrayW = NULL;
541 size = sizeof(LDAPControlW*) * (controlarraylenU( controlarray ) + 1);
542 controlarrayW = HeapAlloc( GetProcessHeap(), 0, size );
546 LDAPControl **p = controlarray;
547 LDAPControlW **q = controlarrayW;
549 while (*p) *q++ = controlUtoW( *p++ );
553 return controlarrayW;
556 static inline void controlarrayfreeA( LDAPControlA **controlarray )
560 LDAPControlA **p = controlarray;
561 while (*p) controlfreeA( *p++ );
562 HeapFree( GetProcessHeap(), 0, controlarray );
566 static inline void controlarrayfreeW( LDAPControlW **controlarray )
570 LDAPControlW **p = controlarray;
571 while (*p) controlfreeW( *p++ );
572 HeapFree( GetProcessHeap(), 0, controlarray );
576 static inline void controlarrayfreeU( LDAPControl **controlarray )
580 LDAPControl **p = controlarray;
581 while (*p) controlfreeU( *p++ );
582 HeapFree( GetProcessHeap(), 0, controlarray );
586 static inline LDAPSortKeyW *sortkeyAtoW( LDAPSortKeyA *sortkey )
588 LDAPSortKeyW *sortkeyW;
590 sortkeyW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyW) );
593 sortkeyW->sk_attrtype = strAtoW( sortkey->sk_attrtype );
594 sortkeyW->sk_matchruleoid = strAtoW( sortkey->sk_matchruleoid );
595 sortkeyW->sk_reverseorder = sortkey->sk_reverseorder;
600 static inline LDAPSortKeyA *sortkeyWtoA( LDAPSortKeyW *sortkey )
602 LDAPSortKeyA *sortkeyA;
604 sortkeyA = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyA) );
607 sortkeyA->sk_attrtype = strWtoA( sortkey->sk_attrtype );
608 sortkeyA->sk_matchruleoid = strWtoA( sortkey->sk_matchruleoid );
609 sortkeyA->sk_reverseorder = sortkey->sk_reverseorder;
614 static inline LDAPSortKey *sortkeyWtoU( LDAPSortKeyW *sortkey )
616 LDAPSortKey *sortkeyU;
618 sortkeyU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKey) );
621 sortkeyU->attributeType = strWtoU( sortkey->sk_attrtype );
622 sortkeyU->orderingRule = strWtoU( sortkey->sk_matchruleoid );
623 sortkeyU->reverseOrder = sortkey->sk_reverseorder;
628 static inline void sortkeyfreeA( LDAPSortKeyA *sortkey )
632 strfreeA( sortkey->sk_attrtype );
633 strfreeA( sortkey->sk_matchruleoid );
634 HeapFree( GetProcessHeap(), 0, sortkey );
638 static inline void sortkeyfreeW( LDAPSortKeyW *sortkey )
642 strfreeW( sortkey->sk_attrtype );
643 strfreeW( sortkey->sk_matchruleoid );
644 HeapFree( GetProcessHeap(), 0, sortkey );
648 static inline void sortkeyfreeU( LDAPSortKey *sortkey )
652 strfreeU( sortkey->attributeType );
653 strfreeU( sortkey->orderingRule );
654 HeapFree( GetProcessHeap(), 0, sortkey );
658 static inline DWORD sortkeyarraylenA( LDAPSortKeyA **sortkeyarray )
660 LDAPSortKeyA **p = sortkeyarray;
662 return p - sortkeyarray;
665 static inline DWORD sortkeyarraylenW( LDAPSortKeyW **sortkeyarray )
667 LDAPSortKeyW **p = sortkeyarray;
669 return p - sortkeyarray;
672 static inline LDAPSortKeyW **sortkeyarrayAtoW( LDAPSortKeyA **sortkeyarray )
674 LDAPSortKeyW **sortkeyarrayW = NULL;
679 size = sizeof(LDAPSortKeyW*) * (sortkeyarraylenA( sortkeyarray ) + 1);
680 sortkeyarrayW = HeapAlloc( GetProcessHeap(), 0, size );
684 LDAPSortKeyA **p = sortkeyarray;
685 LDAPSortKeyW **q = sortkeyarrayW;
687 while (*p) *q++ = sortkeyAtoW( *p++ );
691 return sortkeyarrayW;
694 static inline LDAPSortKey **sortkeyarrayWtoU( LDAPSortKeyW **sortkeyarray )
696 LDAPSortKey **sortkeyarrayU = NULL;
701 size = sizeof(LDAPSortKey*) * (sortkeyarraylenW( sortkeyarray ) + 1);
702 sortkeyarrayU = HeapAlloc( GetProcessHeap(), 0, size );
706 LDAPSortKeyW **p = sortkeyarray;
707 LDAPSortKey **q = sortkeyarrayU;
709 while (*p) *q++ = sortkeyWtoU( *p++ );
713 return sortkeyarrayU;
716 static inline void sortkeyarrayfreeW( LDAPSortKeyW **sortkeyarray )
720 LDAPSortKeyW **p = sortkeyarray;
721 while (*p) sortkeyfreeW( *p++ );
722 HeapFree( GetProcessHeap(), 0, sortkeyarray );
726 static inline void sortkeyarrayfreeU( LDAPSortKey **sortkeyarray )
730 LDAPSortKey **p = sortkeyarray;
731 while (*p) sortkeyfreeU( *p++ );
732 HeapFree( GetProcessHeap(), 0, sortkeyarray );
736 #endif /* HAVE_LDAP */