wldap32: Don't produce unreachable code during conditional compilation. Found bySmatch.
[wine] / dlls / wldap32 / page.c
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 #include "config.h"
22
23 #include "wine/port.h"
24 #include "wine/debug.h"
25
26 #include <stdarg.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winnls.h"
31
32 #ifdef HAVE_LDAP_H
33 #include <ldap.h>
34 #else
35 #define LDAP_SUCCESS        0x00
36 #define LDAP_NOT_SUPPORTED  0x5c
37 #endif
38
39 #include "winldap_private.h"
40 #include "wldap32.h"
41
42 #ifndef LDAP_MAXINT
43 #define LDAP_MAXINT  2147483647
44 #endif
45
46 WINE_DEFAULT_DEBUG_CHANNEL(wldap32);
47
48 /***********************************************************************
49  *      ldap_create_page_controlA     (WLDAP32.@)
50  *
51  * See ldap_create_page_controlW.
52  */
53 ULONG CDECL ldap_create_page_controlA( WLDAP32_LDAP *ld, ULONG pagesize,
54     struct WLDAP32_berval *cookie, UCHAR critical, PLDAPControlA *control )
55 {
56     ULONG ret = LDAP_NOT_SUPPORTED;
57 #ifdef HAVE_LDAP
58     LDAPControlW *controlW = NULL;
59
60     TRACE( "(%p, 0x%08x, %p, 0x%02x, %p)\n", ld, pagesize, cookie,
61            critical, control );
62
63     if (!ld || !control || pagesize > LDAP_MAXINT)
64         return WLDAP32_LDAP_PARAM_ERROR;
65
66     ret = ldap_create_page_controlW( ld, pagesize, cookie, critical, &controlW );
67     if (ret == LDAP_SUCCESS)
68     {
69         *control = controlWtoA( controlW );
70         ldap_control_freeW( controlW );
71     }
72
73 #endif
74     return ret;
75 }
76
77 #ifdef HAVE_LDAP
78
79 /* create a page control by hand */
80 static ULONG create_page_control( ULONG pagesize, struct WLDAP32_berval *cookie,
81     UCHAR critical, PLDAPControlW *control )
82 {
83     LDAPControlW *ctrl;
84     BerElement *ber;
85     ber_tag_t tag;
86     struct berval *berval, null_cookie = { 0, NULL };
87     INT ret, len;
88     char *val;
89
90     ber = ber_alloc_t( LBER_USE_DER );
91     if (!ber) return WLDAP32_LDAP_NO_MEMORY;
92
93     if (cookie)
94         tag = ber_printf( ber, "{iO}", (ber_int_t)pagesize, cookie );
95     else
96         tag = ber_printf( ber, "{iO}", (ber_int_t)pagesize, &null_cookie );
97
98     ret = ber_flatten( ber, &berval );
99     ber_free( ber, 1 );
100
101     if (tag == LBER_ERROR)
102         return WLDAP32_LDAP_ENCODING_ERROR;
103
104     if (ret == -1)
105         return WLDAP32_LDAP_NO_MEMORY;
106
107     /* copy the berval so it can be properly freed by the caller */
108     val = HeapAlloc( GetProcessHeap(), 0, berval->bv_len );
109     if (!val) return WLDAP32_LDAP_NO_MEMORY;
110
111     len = berval->bv_len;
112     memcpy( val, berval->bv_val, len );
113     ber_bvfree( berval );
114
115     ctrl = HeapAlloc( GetProcessHeap(), 0, sizeof(LDAPControlW) );
116     if (!ctrl)
117     {
118         HeapFree( GetProcessHeap(), 0, val );
119         return WLDAP32_LDAP_NO_MEMORY;
120     }
121
122     ctrl->ldctl_oid = strAtoW( LDAP_PAGED_RESULT_OID_STRING );
123     ctrl->ldctl_value.bv_len = len;
124     ctrl->ldctl_value.bv_val = val;
125     ctrl->ldctl_iscritical = critical;
126
127     *control = ctrl;
128
129     return LDAP_SUCCESS;
130 }
131
132 #endif /* HAVE_LDAP */
133
134 /***********************************************************************
135  *      ldap_create_page_controlW     (WLDAP32.@)
136  *
137  * Create a control for paged search results.
138  *
139  * PARAMS
140  *  ld        [I] Pointer to an LDAP context.
141  *  pagesize  [I] Number of entries to return per page.
142  *  cookie    [I] Used by the server to track its location in the
143  *                search results.
144  *  critical  [I] Tells the server this control is critical to the
145  *                search operation.
146  *  control   [O] LDAPControl created.
147  *
148  * RETURNS
149  *  Success: LDAP_SUCCESS
150  *  Failure: An LDAP error code.
151  */
152 ULONG CDECL ldap_create_page_controlW( WLDAP32_LDAP *ld, ULONG pagesize,
153     struct WLDAP32_berval *cookie, UCHAR critical, PLDAPControlW *control )
154 {
155 #ifdef HAVE_LDAP
156     TRACE( "(%p, 0x%08x, %p, 0x%02x, %p)\n", ld, pagesize, cookie,
157            critical, control );
158
159     if (!ld || !control || pagesize > LDAP_MAXINT)
160         return WLDAP32_LDAP_PARAM_ERROR;
161
162     return create_page_control( pagesize, cookie, critical, control );
163
164 #else
165     return LDAP_NOT_SUPPORTED;
166 #endif
167 }
168
169 ULONG CDECL ldap_get_next_page( WLDAP32_LDAP *ld, PLDAPSearch search, ULONG pagesize,
170     ULONG *message )
171 {
172     FIXME( "(%p, %p, 0x%08x, %p)\n", ld, search, pagesize, message );
173
174     if (!ld) return ~0UL;
175     return LDAP_NOT_SUPPORTED;
176 }
177
178 ULONG CDECL ldap_get_next_page_s( WLDAP32_LDAP *ld, PLDAPSearch search,
179     struct l_timeval *timeout, ULONG pagesize, ULONG *count,
180     WLDAP32_LDAPMessage **results )
181 {
182     FIXME( "(%p, %p, %p, 0x%08x, %p, %p)\n", ld, search, timeout,
183            pagesize, count, results );
184
185     if (!ld) return ~0UL;
186     return LDAP_NOT_SUPPORTED;
187 }
188
189 ULONG CDECL ldap_get_paged_count( WLDAP32_LDAP *ld, PLDAPSearch search,
190     ULONG *count, WLDAP32_LDAPMessage *results )
191 {
192     ULONG ret = LDAP_NOT_SUPPORTED;
193 #ifdef HAVE_LDAP
194     FIXME( "(%p, %p, %p, %p)\n", ld, search, count, results );
195
196     if (!ld) return WLDAP32_LDAP_PARAM_ERROR;
197     /* FIXME: save the cookie from the server here */
198
199 #endif
200     return ret;
201 }
202
203 /***********************************************************************
204  *      ldap_parse_page_controlA      (WLDAP32.@)
205  */
206 ULONG CDECL ldap_parse_page_controlA( WLDAP32_LDAP *ld, PLDAPControlA *ctrls,
207     ULONG *count, struct WLDAP32_berval **cookie )
208 {
209     ULONG ret = LDAP_NOT_SUPPORTED;
210 #ifdef HAVE_LDAP
211     LDAPControlW **ctrlsW = NULL;
212
213     TRACE( "(%p, %p, %p, %p)\n", ld, ctrls, count, cookie );
214
215     if (!ld || !ctrls || !count || !cookie)
216         return WLDAP32_LDAP_PARAM_ERROR;
217
218     ctrlsW = controlarrayAtoW( ctrls );
219     if (!ctrlsW) return WLDAP32_LDAP_NO_MEMORY;
220
221     ret = ldap_parse_page_controlW( ld, ctrlsW, count, cookie );
222     controlarrayfreeW( ctrlsW );
223  
224 #endif
225     return ret;
226 }
227
228 /***********************************************************************
229  *      ldap_parse_page_controlW      (WLDAP32.@)
230  */
231 ULONG CDECL ldap_parse_page_controlW( WLDAP32_LDAP *ld, PLDAPControlW *ctrls,
232     ULONG *count, struct WLDAP32_berval **cookie )
233 {
234     ULONG ret = LDAP_NOT_SUPPORTED;
235 #ifdef HAVE_LDAP
236     LDAPControlW *control = NULL;
237     BerElement *ber;
238     ber_tag_t tag;
239     ULONG i;
240
241     TRACE( "(%p, %p, %p, %p)\n", ld, ctrls, count, cookie );
242
243     if (!ld || !ctrls || !count || !cookie)
244         return WLDAP32_LDAP_PARAM_ERROR;
245
246     for (i = 0; ctrls[i]; i++)
247     {
248         if (!lstrcmpW( LDAP_PAGED_RESULT_OID_STRING_W, ctrls[i]->ldctl_oid ))
249             control = ctrls[i];
250     }
251
252     if (!control)
253         return WLDAP32_LDAP_CONTROL_NOT_FOUND; 
254             
255     ber = ber_init( &((LDAPControl *)control)->ldctl_value );
256     if (!ber)
257         return WLDAP32_LDAP_NO_MEMORY;
258
259     tag = ber_scanf( ber, "{iO}", count, cookie );
260     if ( tag == LBER_ERROR )
261         ret = WLDAP32_LDAP_DECODING_ERROR;
262     else
263         ret = LDAP_SUCCESS;
264
265     ber_free( ber, 1 );
266     
267 #endif
268     return ret;
269 }
270
271 ULONG CDECL ldap_search_abandon_page( WLDAP32_LDAP *ld, PLDAPSearch search )
272 {
273     FIXME( "(%p, %p)\n", ld, search );
274
275     if (!ld) return ~0UL;
276     return LDAP_SUCCESS;
277 }
278
279 PLDAPSearch CDECL ldap_search_init_pageA( WLDAP32_LDAP *ld, PCHAR dn, ULONG scope,
280     PCHAR filter, PCHAR attrs[], ULONG attrsonly, PLDAPControlA *serverctrls,
281     PLDAPControlA *clientctrls, ULONG timelimit, ULONG sizelimit, PLDAPSortKeyA *sortkeys )
282 {
283     FIXME( "(%p, %s, 0x%08x, %s, %p, 0x%08x)\n", ld, debugstr_a(dn),
284            scope, debugstr_a(filter), attrs, attrsonly );
285     return NULL;
286 }
287
288 PLDAPSearch CDECL ldap_search_init_pageW( WLDAP32_LDAP *ld, PWCHAR dn, ULONG scope,
289     PWCHAR filter, PWCHAR attrs[], ULONG attrsonly, PLDAPControlW *serverctrls,
290     PLDAPControlW *clientctrls, ULONG timelimit, ULONG sizelimit, PLDAPSortKeyW *sortkeys )
291 {
292     FIXME( "(%p, %s, 0x%08x, %s, %p, 0x%08x)\n", ld, debugstr_w(dn),
293            scope, debugstr_w(filter), attrs, attrsonly );
294     return NULL;
295 }