[WLDAP32] Sync with Wine Staging 1.9.4. CORE-10912
[reactos.git] / reactos / dll / win32 / wldap32 / wldap32.h
1 /*
2 * WLDAP32 - LDAP support for Wine
3 *
4 * Copyright 2005 Hans Leidekker
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #pragma once
22
23 #include <wine/unicode.h>
24
25 extern HINSTANCE hwldap32 DECLSPEC_HIDDEN;
26
27 ULONG map_error( int ) DECLSPEC_HIDDEN;
28
29 /* A set of helper functions to convert LDAP data structures
30 * to and from ansi (A), wide character (W) and utf8 (U) encodings.
31 */
32
33 static inline char *strdupU( const char *src )
34 {
35 char *dst;
36
37 if (!src) return NULL;
38 dst = HeapAlloc( GetProcessHeap(), 0, (strlen( src ) + 1) * sizeof(char) );
39 if (dst)
40 strcpy( dst, src );
41 return dst;
42 }
43
44 static inline WCHAR *strdupW( const WCHAR *src )
45 {
46 WCHAR *dst;
47
48 if (!src) return NULL;
49 dst = HeapAlloc( GetProcessHeap(), 0, (strlenW( src ) + 1) * sizeof(WCHAR) );
50 if (dst)
51 strcpyW( dst, src );
52 return dst;
53 }
54
55 static inline LPWSTR strAtoW( LPCSTR str )
56 {
57 LPWSTR ret = NULL;
58 if (str)
59 {
60 DWORD len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
61 if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
62 MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
63 }
64 return ret;
65 }
66
67 static inline LPSTR strWtoA( LPCWSTR str )
68 {
69 LPSTR ret = NULL;
70 if (str)
71 {
72 DWORD len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
73 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
74 WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
75 }
76 return ret;
77 }
78
79 static inline char *strWtoU( LPCWSTR str )
80 {
81 LPSTR ret = NULL;
82 if (str)
83 {
84 DWORD len = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
85 if ((ret = HeapAlloc( GetProcessHeap(), 0, len )))
86 WideCharToMultiByte( CP_UTF8, 0, str, -1, ret, len, NULL, NULL );
87 }
88 return ret;
89 }
90
91 static inline LPWSTR strUtoW( char *str )
92 {
93 LPWSTR ret = NULL;
94 if (str)
95 {
96 DWORD len = MultiByteToWideChar( CP_UTF8, 0, str, -1, NULL, 0 );
97 if ((ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
98 MultiByteToWideChar( CP_UTF8, 0, str, -1, ret, len );
99 }
100 return ret;
101 }
102
103 static inline void strfreeA( LPSTR str )
104 {
105 HeapFree( GetProcessHeap(), 0, str );
106 }
107
108 static inline void strfreeW( LPWSTR str )
109 {
110 HeapFree( GetProcessHeap(), 0, str );
111 }
112
113 static inline void strfreeU( char *str )
114 {
115 HeapFree( GetProcessHeap(), 0, str );
116 }
117
118 static inline DWORD strarraylenA( LPSTR *strarray )
119 {
120 LPSTR *p = strarray;
121 while (*p) p++;
122 return p - strarray;
123 }
124
125 static inline DWORD strarraylenW( LPWSTR *strarray )
126 {
127 LPWSTR *p = strarray;
128 while (*p) p++;
129 return p - strarray;
130 }
131
132 static inline DWORD strarraylenU( char **strarray )
133 {
134 char **p = strarray;
135 while (*p) p++;
136 return p - strarray;
137 }
138
139 static inline LPWSTR *strarrayAtoW( LPSTR *strarray )
140 {
141 LPWSTR *strarrayW = NULL;
142 DWORD size;
143
144 if (strarray)
145 {
146 size = sizeof(WCHAR*) * (strarraylenA( strarray ) + 1);
147 strarrayW = HeapAlloc( GetProcessHeap(), 0, size );
148
149 if (strarrayW)
150 {
151 LPSTR *p = strarray;
152 LPWSTR *q = strarrayW;
153
154 while (*p) *q++ = strAtoW( *p++ );
155 *q = NULL;
156 }
157 }
158 return strarrayW;
159 }
160
161 static inline LPSTR *strarrayWtoA( LPWSTR *strarray )
162 {
163 LPSTR *strarrayA = NULL;
164 DWORD size;
165
166 if (strarray)
167 {
168 size = sizeof(LPSTR) * (strarraylenW( strarray ) + 1);
169 strarrayA = HeapAlloc( GetProcessHeap(), 0, size );
170
171 if (strarrayA)
172 {
173 LPWSTR *p = strarray;
174 LPSTR *q = strarrayA;
175
176 while (*p) *q++ = strWtoA( *p++ );
177 *q = NULL;
178 }
179 }
180 return strarrayA;
181 }
182
183 static inline char **strarrayWtoU( LPWSTR *strarray )
184 {
185 char **strarrayU = NULL;
186 DWORD size;
187
188 if (strarray)
189 {
190 size = sizeof(char*) * (strarraylenW( strarray ) + 1);
191 strarrayU = HeapAlloc( GetProcessHeap(), 0, size );
192
193 if (strarrayU)
194 {
195 LPWSTR *p = strarray;
196 char **q = strarrayU;
197
198 while (*p) *q++ = strWtoU( *p++ );
199 *q = NULL;
200 }
201 }
202 return strarrayU;
203 }
204
205 static inline LPWSTR *strarrayUtoW( char **strarray )
206 {
207 LPWSTR *strarrayW = NULL;
208 DWORD size;
209
210 if (strarray)
211 {
212 size = sizeof(WCHAR*) * (strarraylenU( strarray ) + 1);
213 strarrayW = HeapAlloc( GetProcessHeap(), 0, size );
214
215 if (strarrayW)
216 {
217 char **p = strarray;
218 LPWSTR *q = strarrayW;
219
220 while (*p) *q++ = strUtoW( *p++ );
221 *q = NULL;
222 }
223 }
224 return strarrayW;
225 }
226
227 static inline void strarrayfreeA( LPSTR *strarray )
228 {
229 if (strarray)
230 {
231 LPSTR *p = strarray;
232 while (*p) strfreeA( *p++ );
233 HeapFree( GetProcessHeap(), 0, strarray );
234 }
235 }
236
237 static inline void strarrayfreeW( LPWSTR *strarray )
238 {
239 if (strarray)
240 {
241 LPWSTR *p = strarray;
242 while (*p) strfreeW( *p++ );
243 HeapFree( GetProcessHeap(), 0, strarray );
244 }
245 }
246
247 static inline void strarrayfreeU( char **strarray )
248 {
249 if (strarray)
250 {
251 char **p = strarray;
252 while (*p) strfreeU( *p++ );
253 HeapFree( GetProcessHeap(), 0, strarray );
254 }
255 }
256
257 #ifdef HAVE_LDAP
258
259 static inline struct berval *bvdup( struct berval *bv )
260 {
261 struct berval *berval;
262 DWORD size = sizeof(struct berval) + bv->bv_len;
263
264 berval = HeapAlloc( GetProcessHeap(), 0, size );
265 if (berval)
266 {
267 char *val = (char *)berval + sizeof(struct berval);
268
269 berval->bv_len = bv->bv_len;
270 berval->bv_val = val;
271 memcpy( val, bv->bv_val, bv->bv_len );
272 }
273 return berval;
274 }
275
276 static inline DWORD bvarraylen( struct berval **bv )
277 {
278 struct berval **p = bv;
279 while (*p) p++;
280 return p - bv;
281 }
282
283 static inline struct berval **bvarraydup( struct berval **bv )
284 {
285 struct berval **berval = NULL;
286 DWORD size;
287
288 if (bv)
289 {
290 size = sizeof(struct berval *) * (bvarraylen( bv ) + 1);
291 berval = HeapAlloc( GetProcessHeap(), 0, size );
292
293 if (berval)
294 {
295 struct berval **p = bv;
296 struct berval **q = berval;
297
298 while (*p) *q++ = bvdup( *p++ );
299 *q = NULL;
300 }
301 }
302 return berval;
303 }
304
305 static inline void bvarrayfree( struct berval **bv )
306 {
307 struct berval **p = bv;
308 while (*p) HeapFree( GetProcessHeap(), 0, *p++ );
309 HeapFree( GetProcessHeap(), 0, bv );
310 }
311
312 static inline LDAPModW *modAtoW( LDAPModA *mod )
313 {
314 LDAPModW *modW;
315
316 modW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPModW) );
317 if (modW)
318 {
319 modW->mod_op = mod->mod_op;
320 modW->mod_type = strAtoW( mod->mod_type );
321
322 if (mod->mod_op & LDAP_MOD_BVALUES)
323 modW->mod_vals.modv_bvals = bvarraydup( mod->mod_vals.modv_bvals );
324 else
325 modW->mod_vals.modv_strvals = strarrayAtoW( mod->mod_vals.modv_strvals );
326 }
327 return modW;
328 }
329
330 static inline LDAPMod *modWtoU( LDAPModW *mod )
331 {
332 LDAPMod *modU;
333
334 modU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPMod) );
335 if (modU)
336 {
337 modU->mod_op = mod->mod_op;
338 modU->mod_type = strWtoU( mod->mod_type );
339
340 if (mod->mod_op & LDAP_MOD_BVALUES)
341 modU->mod_vals.modv_bvals = bvarraydup( mod->mod_vals.modv_bvals );
342 else
343 modU->mod_vals.modv_strvals = strarrayWtoU( mod->mod_vals.modv_strvals );
344 }
345 return modU;
346 }
347
348 static inline void modfreeW( LDAPModW *mod )
349 {
350 if (mod->mod_op & LDAP_MOD_BVALUES)
351 bvarrayfree( mod->mod_vals.modv_bvals );
352 else
353 strarrayfreeW( mod->mod_vals.modv_strvals );
354 HeapFree( GetProcessHeap(), 0, mod );
355 }
356
357 static inline void modfreeU( LDAPMod *mod )
358 {
359 if (mod->mod_op & LDAP_MOD_BVALUES)
360 bvarrayfree( mod->mod_vals.modv_bvals );
361 else
362 strarrayfreeU( mod->mod_vals.modv_strvals );
363 HeapFree( GetProcessHeap(), 0, mod );
364 }
365
366 static inline DWORD modarraylenA( LDAPModA **modarray )
367 {
368 LDAPModA **p = modarray;
369 while (*p) p++;
370 return p - modarray;
371 }
372
373 static inline DWORD modarraylenW( LDAPModW **modarray )
374 {
375 LDAPModW **p = modarray;
376 while (*p) p++;
377 return p - modarray;
378 }
379
380 static inline LDAPModW **modarrayAtoW( LDAPModA **modarray )
381 {
382 LDAPModW **modarrayW = NULL;
383 DWORD size;
384
385 if (modarray)
386 {
387 size = sizeof(LDAPModW*) * (modarraylenA( modarray ) + 1);
388 modarrayW = HeapAlloc( GetProcessHeap(), 0, size );
389
390 if (modarrayW)
391 {
392 LDAPModA **p = modarray;
393 LDAPModW **q = modarrayW;
394
395 while (*p) *q++ = modAtoW( *p++ );
396 *q = NULL;
397 }
398 }
399 return modarrayW;
400 }
401
402 static inline LDAPMod **modarrayWtoU( LDAPModW **modarray )
403 {
404 LDAPMod **modarrayU = NULL;
405 DWORD size;
406
407 if (modarray)
408 {
409 size = sizeof(LDAPMod*) * (modarraylenW( modarray ) + 1);
410 modarrayU = HeapAlloc( GetProcessHeap(), 0, size );
411
412 if (modarrayU)
413 {
414 LDAPModW **p = modarray;
415 LDAPMod **q = modarrayU;
416
417 while (*p) *q++ = modWtoU( *p++ );
418 *q = NULL;
419 }
420 }
421 return modarrayU;
422 }
423
424 static inline void modarrayfreeW( LDAPModW **modarray )
425 {
426 if (modarray)
427 {
428 LDAPModW **p = modarray;
429 while (*p) modfreeW( *p++ );
430 HeapFree( GetProcessHeap(), 0, modarray );
431 }
432 }
433
434 static inline void modarrayfreeU( LDAPMod **modarray )
435 {
436 if (modarray)
437 {
438 LDAPMod **p = modarray;
439 while (*p) modfreeU( *p++ );
440 HeapFree( GetProcessHeap(), 0, modarray );
441 }
442 }
443
444 static inline LDAPControlW *controlAtoW( LDAPControlA *control )
445 {
446 LDAPControlW *controlW;
447 DWORD len = control->ldctl_value.bv_len;
448 char *val = NULL;
449
450 if (control->ldctl_value.bv_val)
451 {
452 val = HeapAlloc( GetProcessHeap(), 0, len );
453 if (!val) return NULL;
454 memcpy( val, control->ldctl_value.bv_val, len );
455 }
456
457 controlW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW) );
458 if (!controlW)
459 {
460 HeapFree( GetProcessHeap(), 0, val );
461 return NULL;
462 }
463
464 controlW->ldctl_oid = strAtoW( control->ldctl_oid );
465 controlW->ldctl_value.bv_len = len;
466 controlW->ldctl_value.bv_val = val;
467 controlW->ldctl_iscritical = control->ldctl_iscritical;
468
469 return controlW;
470 }
471
472 static inline LDAPControlA *controlWtoA( LDAPControlW *control )
473 {
474 LDAPControlA *controlA;
475 DWORD len = control->ldctl_value.bv_len;
476 char *val = NULL;
477
478 if (control->ldctl_value.bv_val)
479 {
480 val = HeapAlloc( GetProcessHeap(), 0, len );
481 if (!val) return NULL;
482 memcpy( val, control->ldctl_value.bv_val, len );
483 }
484
485 controlA = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlA) );
486 if (!controlA)
487 {
488 HeapFree( GetProcessHeap(), 0, val );
489 return NULL;
490 }
491
492 controlA->ldctl_oid = strWtoA( control->ldctl_oid );
493 controlA->ldctl_value.bv_len = len;
494 controlA->ldctl_value.bv_val = val;
495 controlA->ldctl_iscritical = control->ldctl_iscritical;
496
497 return controlA;
498 }
499
500 static inline LDAPControl *controlWtoU( LDAPControlW *control )
501 {
502 LDAPControl *controlU;
503 DWORD len = control->ldctl_value.bv_len;
504 char *val = NULL;
505
506 if (control->ldctl_value.bv_val)
507 {
508 val = HeapAlloc( GetProcessHeap(), 0, len );
509 if (!val) return NULL;
510 memcpy( val, control->ldctl_value.bv_val, len );
511 }
512
513 controlU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControl) );
514 if (!controlU)
515 {
516 HeapFree( GetProcessHeap(), 0, val );
517 return NULL;
518 }
519
520 controlU->ldctl_oid = strWtoU( control->ldctl_oid );
521 controlU->ldctl_value.bv_len = len;
522 controlU->ldctl_value.bv_val = val;
523 controlU->ldctl_iscritical = control->ldctl_iscritical;
524
525 return controlU;
526 }
527
528 static inline LDAPControlW *controlUtoW( LDAPControl *control )
529 {
530 LDAPControlW *controlW;
531 DWORD len = control->ldctl_value.bv_len;
532 char *val = NULL;
533
534 if (control->ldctl_value.bv_val)
535 {
536 val = HeapAlloc( GetProcessHeap(), 0, len );
537 if (!val) return NULL;
538 memcpy( val, control->ldctl_value.bv_val, len );
539 }
540
541 controlW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW) );
542 if (!controlW)
543 {
544 HeapFree( GetProcessHeap(), 0, val );
545 return NULL;
546 }
547
548 controlW->ldctl_oid = strUtoW( control->ldctl_oid );
549 controlW->ldctl_value.bv_len = len;
550 controlW->ldctl_value.bv_val = val;
551 controlW->ldctl_iscritical = control->ldctl_iscritical;
552
553 return controlW;
554 }
555
556 static inline void controlfreeA( LDAPControlA *control )
557 {
558 if (control)
559 {
560 strfreeA( control->ldctl_oid );
561 HeapFree( GetProcessHeap(), 0, control->ldctl_value.bv_val );
562 HeapFree( GetProcessHeap(), 0, control );
563 }
564 }
565
566 static inline void controlfreeW( LDAPControlW *control )
567 {
568 if (control)
569 {
570 strfreeW( control->ldctl_oid );
571 HeapFree( GetProcessHeap(), 0, control->ldctl_value.bv_val );
572 HeapFree( GetProcessHeap(), 0, control );
573 }
574 }
575
576 static inline void controlfreeU( LDAPControl *control )
577 {
578 if (control)
579 {
580 strfreeU( control->ldctl_oid );
581 HeapFree( GetProcessHeap(), 0, control->ldctl_value.bv_val );
582 HeapFree( GetProcessHeap(), 0, control );
583 }
584 }
585
586 static inline DWORD controlarraylenA( LDAPControlA **controlarray )
587 {
588 LDAPControlA **p = controlarray;
589 while (*p) p++;
590 return p - controlarray;
591 }
592
593 static inline DWORD controlarraylenW( LDAPControlW **controlarray )
594 {
595 LDAPControlW **p = controlarray;
596 while (*p) p++;
597 return p - controlarray;
598 }
599
600 static inline DWORD controlarraylenU( LDAPControl **controlarray )
601 {
602 LDAPControl **p = controlarray;
603 while (*p) p++;
604 return p - controlarray;
605 }
606
607 static inline LDAPControlW **controlarrayAtoW( LDAPControlA **controlarray )
608 {
609 LDAPControlW **controlarrayW = NULL;
610 DWORD size;
611
612 if (controlarray)
613 {
614 size = sizeof(LDAPControlW*) * (controlarraylenA( controlarray ) + 1);
615 controlarrayW = HeapAlloc( GetProcessHeap(), 0, size );
616
617 if (controlarrayW)
618 {
619 LDAPControlA **p = controlarray;
620 LDAPControlW **q = controlarrayW;
621
622 while (*p) *q++ = controlAtoW( *p++ );
623 *q = NULL;
624 }
625 }
626 return controlarrayW;
627 }
628
629 static inline LDAPControlA **controlarrayWtoA( LDAPControlW **controlarray )
630 {
631 LDAPControlA **controlarrayA = NULL;
632 DWORD size;
633
634 if (controlarray)
635 {
636 size = sizeof(LDAPControl*) * (controlarraylenW( controlarray ) + 1);
637 controlarrayA = HeapAlloc( GetProcessHeap(), 0, size );
638
639 if (controlarrayA)
640 {
641 LDAPControlW **p = controlarray;
642 LDAPControlA **q = controlarrayA;
643
644 while (*p) *q++ = controlWtoA( *p++ );
645 *q = NULL;
646 }
647 }
648 return controlarrayA;
649 }
650
651 static inline LDAPControl **controlarrayWtoU( LDAPControlW **controlarray )
652 {
653 LDAPControl **controlarrayU = NULL;
654 DWORD size;
655
656 if (controlarray)
657 {
658 size = sizeof(LDAPControl*) * (controlarraylenW( controlarray ) + 1);
659 controlarrayU = HeapAlloc( GetProcessHeap(), 0, size );
660
661 if (controlarrayU)
662 {
663 LDAPControlW **p = controlarray;
664 LDAPControl **q = controlarrayU;
665
666 while (*p) *q++ = controlWtoU( *p++ );
667 *q = NULL;
668 }
669 }
670 return controlarrayU;
671 }
672
673 static inline LDAPControlW **controlarrayUtoW( LDAPControl **controlarray )
674 {
675 LDAPControlW **controlarrayW = NULL;
676 DWORD size;
677
678 if (controlarray)
679 {
680 size = sizeof(LDAPControlW*) * (controlarraylenU( controlarray ) + 1);
681 controlarrayW = HeapAlloc( GetProcessHeap(), 0, size );
682
683 if (controlarrayW)
684 {
685 LDAPControl **p = controlarray;
686 LDAPControlW **q = controlarrayW;
687
688 while (*p) *q++ = controlUtoW( *p++ );
689 *q = NULL;
690 }
691 }
692 return controlarrayW;
693 }
694
695 static inline void controlarrayfreeA( LDAPControlA **controlarray )
696 {
697 if (controlarray)
698 {
699 LDAPControlA **p = controlarray;
700 while (*p) controlfreeA( *p++ );
701 HeapFree( GetProcessHeap(), 0, controlarray );
702 }
703 }
704
705 static inline void controlarrayfreeW( LDAPControlW **controlarray )
706 {
707 if (controlarray)
708 {
709 LDAPControlW **p = controlarray;
710 while (*p) controlfreeW( *p++ );
711 HeapFree( GetProcessHeap(), 0, controlarray );
712 }
713 }
714
715 static inline void controlarrayfreeU( LDAPControl **controlarray )
716 {
717 if (controlarray)
718 {
719 LDAPControl **p = controlarray;
720 while (*p) controlfreeU( *p++ );
721 HeapFree( GetProcessHeap(), 0, controlarray );
722 }
723 }
724
725 static inline LDAPSortKeyW *sortkeyAtoW( LDAPSortKeyA *sortkey )
726 {
727 LDAPSortKeyW *sortkeyW;
728
729 sortkeyW = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyW) );
730 if (sortkeyW)
731 {
732 sortkeyW->sk_attrtype = strAtoW( sortkey->sk_attrtype );
733 sortkeyW->sk_matchruleoid = strAtoW( sortkey->sk_matchruleoid );
734 sortkeyW->sk_reverseorder = sortkey->sk_reverseorder;
735 }
736 return sortkeyW;
737 }
738
739 static inline LDAPSortKeyA *sortkeyWtoA( LDAPSortKeyW *sortkey )
740 {
741 LDAPSortKeyA *sortkeyA;
742
743 sortkeyA = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKeyA) );
744 if (sortkeyA)
745 {
746 sortkeyA->sk_attrtype = strWtoA( sortkey->sk_attrtype );
747 sortkeyA->sk_matchruleoid = strWtoA( sortkey->sk_matchruleoid );
748 sortkeyA->sk_reverseorder = sortkey->sk_reverseorder;
749 }
750 return sortkeyA;
751 }
752
753 static inline LDAPSortKey *sortkeyWtoU( LDAPSortKeyW *sortkey )
754 {
755 LDAPSortKey *sortkeyU;
756
757 sortkeyU = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPSortKey) );
758 if (sortkeyU)
759 {
760 sortkeyU->attributeType = strWtoU( sortkey->sk_attrtype );
761 sortkeyU->orderingRule = strWtoU( sortkey->sk_matchruleoid );
762 sortkeyU->reverseOrder = sortkey->sk_reverseorder;
763 }
764 return sortkeyU;
765 }
766
767 static inline void sortkeyfreeA( LDAPSortKeyA *sortkey )
768 {
769 if (sortkey)
770 {
771 strfreeA( sortkey->sk_attrtype );
772 strfreeA( sortkey->sk_matchruleoid );
773 HeapFree( GetProcessHeap(), 0, sortkey );
774 }
775 }
776
777 static inline void sortkeyfreeW( LDAPSortKeyW *sortkey )
778 {
779 if (sortkey)
780 {
781 strfreeW( sortkey->sk_attrtype );
782 strfreeW( sortkey->sk_matchruleoid );
783 HeapFree( GetProcessHeap(), 0, sortkey );
784 }
785 }
786
787 static inline void sortkeyfreeU( LDAPSortKey *sortkey )
788 {
789 if (sortkey)
790 {
791 strfreeU( sortkey->attributeType );
792 strfreeU( sortkey->orderingRule );
793 HeapFree( GetProcessHeap(), 0, sortkey );
794 }
795 }
796
797 static inline DWORD sortkeyarraylenA( LDAPSortKeyA **sortkeyarray )
798 {
799 LDAPSortKeyA **p = sortkeyarray;
800 while (*p) p++;
801 return p - sortkeyarray;
802 }
803
804 static inline DWORD sortkeyarraylenW( LDAPSortKeyW **sortkeyarray )
805 {
806 LDAPSortKeyW **p = sortkeyarray;
807 while (*p) p++;
808 return p - sortkeyarray;
809 }
810
811 static inline LDAPSortKeyW **sortkeyarrayAtoW( LDAPSortKeyA **sortkeyarray )
812 {
813 LDAPSortKeyW **sortkeyarrayW = NULL;
814 DWORD size;
815
816 if (sortkeyarray)
817 {
818 size = sizeof(LDAPSortKeyW*) * (sortkeyarraylenA( sortkeyarray ) + 1);
819 sortkeyarrayW = HeapAlloc( GetProcessHeap(), 0, size );
820
821 if (sortkeyarrayW)
822 {
823 LDAPSortKeyA **p = sortkeyarray;
824 LDAPSortKeyW **q = sortkeyarrayW;
825
826 while (*p) *q++ = sortkeyAtoW( *p++ );
827 *q = NULL;
828 }
829 }
830 return sortkeyarrayW;
831 }
832
833 static inline LDAPSortKey **sortkeyarrayWtoU( LDAPSortKeyW **sortkeyarray )
834 {
835 LDAPSortKey **sortkeyarrayU = NULL;
836 DWORD size;
837
838 if (sortkeyarray)
839 {
840 size = sizeof(LDAPSortKey*) * (sortkeyarraylenW( sortkeyarray ) + 1);
841 sortkeyarrayU = HeapAlloc( GetProcessHeap(), 0, size );
842
843 if (sortkeyarrayU)
844 {
845 LDAPSortKeyW **p = sortkeyarray;
846 LDAPSortKey **q = sortkeyarrayU;
847
848 while (*p) *q++ = sortkeyWtoU( *p++ );
849 *q = NULL;
850 }
851 }
852 return sortkeyarrayU;
853 }
854
855 static inline void sortkeyarrayfreeW( LDAPSortKeyW **sortkeyarray )
856 {
857 if (sortkeyarray)
858 {
859 LDAPSortKeyW **p = sortkeyarray;
860 while (*p) sortkeyfreeW( *p++ );
861 HeapFree( GetProcessHeap(), 0, sortkeyarray );
862 }
863 }
864
865 static inline void sortkeyarrayfreeU( LDAPSortKey **sortkeyarray )
866 {
867 if (sortkeyarray)
868 {
869 LDAPSortKey **p = sortkeyarray;
870 while (*p) sortkeyfreeU( *p++ );
871 HeapFree( GetProcessHeap(), 0, sortkeyarray );
872 }
873 }
874 #endif /* HAVE_LDAP */