1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 * Copyright 1998-2007 The OpenLDAP Foundation.
4 * Copyright 2006 Hans Leidekker
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
19 #include <ac/stdlib.h>
20 #include <ac/string.h>
25 /* ---------------------------------------------------------------------------
26 ldap_create_page_control_value
28 Create and encode the value of the paged results control (RFC 2696).
30 ld (IN) An LDAP session handle
31 pagesize (IN) Page size requested
32 cookie (IN) Opaque structure used by the server to track its
33 location in the search results. NULL on the
35 value (OUT) Control value, SHOULD be freed by calling
36 ldap_memfree() when done.
38 pagedResultsControl ::= SEQUENCE {
39 controlType 1.2.840.113556.1.4.319,
40 criticality BOOLEAN DEFAULT FALSE,
41 controlValue searchControlValue }
43 searchControlValue ::= SEQUENCE {
44 size INTEGER (0..maxInt),
45 -- requested page size from client
46 -- result set size estimate from server
49 ---------------------------------------------------------------------------*/
52 ldap_create_page_control_value(
55 struct berval *cookie,
56 struct berval *value )
58 BerElement *ber = NULL;
60 struct berval null_cookie = { 0, NULL };
62 if ( ld == NULL || value == NULL ||
63 pagesize < 1 || pagesize > LDAP_MAXINT )
66 ld->ld_errno = LDAP_PARAM_ERROR;
67 return LDAP_PARAM_ERROR;
70 assert( LDAP_VALID( ld ) );
75 if ( cookie == NULL ) {
76 cookie = &null_cookie;
79 ber = ldap_alloc_ber_with_options( ld );
81 ld->ld_errno = LDAP_NO_MEMORY;
85 tag = ber_printf( ber, "{iO}", pagesize, cookie );
86 if ( tag == LBER_ERROR ) {
87 ld->ld_errno = LDAP_ENCODING_ERROR;
91 if ( ber_flatten2( ber, value, 1 ) == -1 ) {
92 ld->ld_errno = LDAP_NO_MEMORY;
104 /* ---------------------------------------------------------------------------
105 ldap_create_page_control
107 Create and encode a page control.
109 ld (IN) An LDAP session handle
110 pagesize (IN) Page size requested
111 cookie (IN) Opaque structure used by the server to track its
112 location in the search results. NULL on the
114 value (OUT) Control value, SHOULD be freed by calling
115 ldap_memfree() when done.
116 iscritical (IN) Criticality
117 ctrlp (OUT) LDAP control, SHOULD be freed by calling
118 ldap_control_free() when done.
120 pagedResultsControl ::= SEQUENCE {
121 controlType 1.2.840.113556.1.4.319,
122 criticality BOOLEAN DEFAULT FALSE,
123 controlValue searchControlValue }
125 searchControlValue ::= SEQUENCE {
126 size INTEGER (0..maxInt),
127 -- requested page size from client
128 -- result set size estimate from server
129 cookie OCTET STRING }
131 ---------------------------------------------------------------------------*/
134 ldap_create_page_control(
137 struct berval *cookie,
139 LDAPControl **ctrlp )
144 if ( ctrlp == NULL ) {
145 ld->ld_errno = LDAP_PARAM_ERROR;
149 ld->ld_errno = ldap_create_page_control_value( ld,
150 pagesize, cookie, &value );
151 if ( ld->ld_errno == LDAP_SUCCESS ) {
152 if ((ber = ldap_alloc_ber_with_options(ld)) == NULL) {
153 ld->ld_errno = LDAP_NO_MEMORY;
154 return LDAP_NO_MEMORY;
157 ld->ld_errno = ldap_create_control( LDAP_CONTROL_PAGEDRESULTS,
158 ber, iscritical, ctrlp );
159 if ( ld->ld_errno == LDAP_SUCCESS ) {
160 (*ctrlp)->ldctl_value = value;
162 LDAP_FREE( value.bv_val );
171 /* ---------------------------------------------------------------------------
172 ldap_parse_pageresponse_control
174 Decode a page control.
176 ld (IN) An LDAP session handle
177 ctrl (IN) The page response control
178 count (OUT) The number of entries in the page.
179 cookie (OUT) Opaque cookie. Use ldap_memfree() to
180 free the bv_val member of this structure.
182 ---------------------------------------------------------------------------*/
185 ldap_parse_pageresponse_control(
189 struct berval *cookie )
195 if ( ld == NULL || ctrl == NULL || cookie == NULL ) {
197 ld->ld_errno = LDAP_PARAM_ERROR;
198 return LDAP_PARAM_ERROR;
201 /* Create a BerElement from the berval returned in the control. */
202 ber = ber_init( &ctrl->ldctl_value );
205 ld->ld_errno = LDAP_NO_MEMORY;
209 /* Extract the count and cookie from the control. */
210 tag = ber_scanf( ber, "{io}", &count, cookie );
213 if ( tag == LBER_ERROR ) {
214 ld->ld_errno = LDAP_DECODING_ERROR;
216 ld->ld_errno = LDAP_SUCCESS;
218 if ( countp != NULL ) {
219 *countp = (unsigned long)count;
226 /* ---------------------------------------------------------------------------
227 ldap_parse_page_control
229 Decode a page control.
231 ld (IN) An LDAP session handle
232 ctrls (IN) Response controls
233 count (OUT) The number of entries in the page.
234 cookie (OUT) Opaque cookie. Use ldap_memfree() to
235 free the bv_val member of this structure.
237 ---------------------------------------------------------------------------*/
240 ldap_parse_page_control(
244 struct berval **cookiep )
247 struct berval cookie;
249 if ( cookiep == NULL ) {
250 ld->ld_errno = LDAP_PARAM_ERROR;
254 if ( ctrls == NULL ) {
255 ld->ld_errno = LDAP_CONTROL_NOT_FOUND;
259 c = ldap_find_control( LDAP_CONTROL_PAGEDRESULTS, ctrls );
261 /* No page control was found. */
262 ld->ld_errno = LDAP_CONTROL_NOT_FOUND;
266 ld->ld_errno = ldap_parse_pageresponse_control( ld, c, countp, &cookie );
267 if ( ld->ld_errno == LDAP_SUCCESS ) {
268 *cookiep = LDAP_MALLOC( sizeof( struct berval * ) );
269 if ( *cookiep == NULL ) {
270 ld->ld_errno = LDAP_NO_MEMORY;