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