1 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 * Copyright 1998-2008 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
)
143 if ( ctrlp
== NULL
) {
144 ld
->ld_errno
= LDAP_PARAM_ERROR
;
148 ld
->ld_errno
= ldap_create_page_control_value( ld
,
149 pagesize
, cookie
, &value
);
150 if ( ld
->ld_errno
== LDAP_SUCCESS
) {
151 ld
->ld_errno
= ldap_control_create( LDAP_CONTROL_PAGEDRESULTS
,
152 iscritical
, &value
, 0, ctrlp
);
153 if ( ld
->ld_errno
!= LDAP_SUCCESS
) {
154 LDAP_FREE( value
.bv_val
);
162 /* ---------------------------------------------------------------------------
163 ldap_parse_pageresponse_control
165 Decode a page control.
167 ld (IN) An LDAP session handle
168 ctrl (IN) The page response control
169 count (OUT) The number of entries in the page.
170 cookie (OUT) Opaque cookie. Use ldap_memfree() to
171 free the bv_val member of this structure.
173 ---------------------------------------------------------------------------*/
176 ldap_parse_pageresponse_control(
180 struct berval
*cookie
)
186 if ( ld
== NULL
|| ctrl
== NULL
|| cookie
== NULL
) {
188 ld
->ld_errno
= LDAP_PARAM_ERROR
;
189 return LDAP_PARAM_ERROR
;
192 /* Create a BerElement from the berval returned in the control. */
193 ber
= ber_init( &ctrl
->ldctl_value
);
196 ld
->ld_errno
= LDAP_NO_MEMORY
;
200 /* Extract the count and cookie from the control. */
201 tag
= ber_scanf( ber
, "{io}", &count
, cookie
);
204 if ( tag
== LBER_ERROR
) {
205 ld
->ld_errno
= LDAP_DECODING_ERROR
;
207 ld
->ld_errno
= LDAP_SUCCESS
;
209 if ( countp
!= NULL
) {
210 *countp
= (unsigned long)count
;
217 /* ---------------------------------------------------------------------------
218 ldap_parse_page_control
220 Decode a page control.
222 ld (IN) An LDAP session handle
223 ctrls (IN) Response controls
224 count (OUT) The number of entries in the page.
225 cookie (OUT) Opaque cookie. Use ldap_memfree() to
226 free the bv_val member of this structure.
228 ---------------------------------------------------------------------------*/
231 ldap_parse_page_control(
235 struct berval
**cookiep
)
238 struct berval cookie
;
240 if ( cookiep
== NULL
) {
241 ld
->ld_errno
= LDAP_PARAM_ERROR
;
245 if ( ctrls
== NULL
) {
246 ld
->ld_errno
= LDAP_CONTROL_NOT_FOUND
;
250 c
= ldap_control_find( LDAP_CONTROL_PAGEDRESULTS
, ctrls
, NULL
);
252 /* No page control was found. */
253 ld
->ld_errno
= LDAP_CONTROL_NOT_FOUND
;
257 ld
->ld_errno
= ldap_parse_pageresponse_control( ld
, c
, countp
, &cookie
);
258 if ( ld
->ld_errno
== LDAP_SUCCESS
) {
259 *cookiep
= LDAP_MALLOC( sizeof( struct berval
) );
260 if ( *cookiep
== NULL
) {
261 ld
->ld_errno
= LDAP_NO_MEMORY
;