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