]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/sortctrl.c
Fix typo in comment
[openldap] / libraries / libldap / sortctrl.c
index 910bf0459dac8a64043e40a793005e5718964cec..50bed40b7d53998a27d0e1b22c6138f45ecd979c 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2004 The OpenLDAP Foundation.
+ * Copyright 1998-2010 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -27,9 +27,6 @@
  * can be found in the file "build/LICENSE-2.0.1" in this distribution
  * of OpenLDAP Software.
  */
  * can be found in the file "build/LICENSE-2.0.1" in this distribution
  * of OpenLDAP Software.
  */
-/* Portions Copyright (C) The Internet Society (1997)
- * ASN.1 fragments are from RFC 2251; see RFC for full legal notices.
- */
 
 #include "portable.h"
 
 
 #include "portable.h"
 
@@ -260,9 +257,9 @@ ldap_free_sort_keylist ( LDAPSortKey **keyList )
 
 
 /* ---------------------------------------------------------------------------
 
 
 /* ---------------------------------------------------------------------------
-   ldap_create_sort_control
+   ldap_create_sort_control_value
    
    
-   Create and encode the server-side sort control.
+   Create and encode the value of the server-side sort control.
    
    ld          (IN) An LDAP session handle, as obtained from a call to
                                        ldap_init().
    
    ld          (IN) An LDAP session handle, as obtained from a call to
                                        ldap_init().
@@ -273,11 +270,8 @@ ldap_free_sort_keylist ( LDAPSortKey **keyList )
                                        consists of an attribute name, ascending/descending flag,
                                        and an optional matching rule (OID) to use.
                           
                                        consists of an attribute name, ascending/descending flag,
                                        and an optional matching rule (OID) to use.
                           
-   isCritical  (IN) 0 - Indicates the control is not critical to the operation.
-                                       non-zero - The control is critical to the operation.
-                                        
-   ctrlp      (OUT) Returns a pointer to the LDAPControl created.  This control
-                                       SHOULD be freed by calling ldap_control_free() when done.
+   value      (OUT) Contains the control value; the bv_val member of the berval structure
+                                       SHOULD be freed by calling ldap_memfree() when done.
    
    
    Ber encoding
    
    
    Ber encoding
@@ -290,82 +284,166 @@ ldap_free_sort_keylist ( LDAPSortKey **keyList )
    ---------------------------------------------------------------------------*/
 
 int
    ---------------------------------------------------------------------------*/
 
 int
-ldap_create_sort_control (
+ldap_create_sort_control_value(
        LDAP *ld,
        LDAPSortKey **keyList,
        LDAP *ld,
        LDAPSortKey **keyList,
-       int isCritical,
-       LDAPControl **ctrlp )
+       struct berval *value )
 {
 {
-       int         i;
-       BerElement  *ber;
-       ber_tag_t tag;
+       int             i;
+       BerElement      *ber = NULL;
+       ber_tag_t       tag;
 
 
+       assert( ld != NULL );
+       assert( LDAP_VALID( ld ) );
 
 
-       if ( (ld == NULL) || (keyList == NULL) || (ctrlp == NULL) ) {
+       if ( ld == NULL ) return LDAP_PARAM_ERROR;
+       if ( keyList == NULL || value == NULL ) {
                ld->ld_errno = LDAP_PARAM_ERROR;
                ld->ld_errno = LDAP_PARAM_ERROR;
-               return(ld->ld_errno);
+               return LDAP_PARAM_ERROR;
        }
 
        }
 
-       if ((ber = ldap_alloc_ber_with_options(ld)) == NULL) {
+       value->bv_val = NULL;
+       value->bv_len = 0;
+       ld->ld_errno = LDAP_SUCCESS;
+
+       ber = ldap_alloc_ber_with_options( ld );
+       if ( ber == NULL) {
                ld->ld_errno = LDAP_NO_MEMORY;
                ld->ld_errno = LDAP_NO_MEMORY;
-               return( ld->ld_errno );
+               return ld->ld_errno;
        }
 
        }
 
-       tag = ber_printf(ber, "{" /*}*/);
-       if (tag == LBER_ERROR) goto exit;
+       tag = ber_printf( ber, "{" /*}*/ );
+       if ( tag == LBER_ERROR ) {
+               goto error_return;
+       }
 
 
-       for (i = 0; keyList[i] != NULL; i++) {
-               tag = ber_printf(ber, "{s" /*}*/, (keyList[i])->attributeType);
-               if (tag == LBER_ERROR) goto exit;
+       for ( i = 0; keyList[i] != NULL; i++ ) {
+               tag = ber_printf( ber, "{s" /*}*/, keyList[i]->attributeType );
+               if ( tag == LBER_ERROR ) {
+                       goto error_return;
+               }
 
 
-               if ((keyList[i])->orderingRule != NULL) {
+               if ( keyList[i]->orderingRule != NULL ) {
                        tag = ber_printf( ber, "ts",
                                LDAP_MATCHRULE_IDENTIFIER,
                        tag = ber_printf( ber, "ts",
                                LDAP_MATCHRULE_IDENTIFIER,
-                               (keyList[i])->orderingRule );
+                               keyList[i]->orderingRule );
 
 
-                       if( tag == LBER_ERROR ) goto exit;
+                       if ( tag == LBER_ERROR ) {
+                               goto error_return;
+                       }
                }
 
                }
 
-               if ((keyList[i])->reverseOrder) {
-                       tag = ber_printf(ber, "tb",
+               if ( keyList[i]->reverseOrder ) {
+                       tag = ber_printf( ber, "tb",
                                LDAP_REVERSEORDER_IDENTIFIER,
                                LDAP_REVERSEORDER_IDENTIFIER,
-                               (keyList[i])->reverseOrder );
+                               keyList[i]->reverseOrder );
 
 
-                       if( tag == LBER_ERROR ) goto exit;
+                       if ( tag == LBER_ERROR ) {
+                               goto error_return;
+                       }
                }
 
                }
 
-               tag = ber_printf(ber, /*{*/ "N}");
-               if( tag == LBER_ERROR ) goto exit;
+               tag = ber_printf( ber, /*{*/ "N}" );
+               if ( tag == LBER_ERROR ) {
+                       goto error_return;
+               }
        }
 
        }
 
-       tag = ber_printf(ber, /*{*/ "N}");
-       if( tag == LBER_ERROR ) goto exit;
+       tag = ber_printf( ber, /*{*/ "N}" );
+       if ( tag == LBER_ERROR ) {
+               goto error_return;
+       }
 
 
-       ld->ld_errno = ldap_create_control( LDAP_CONTROL_SORTREQUEST,
-               ber, isCritical, ctrlp);
+       if ( ber_flatten2( ber, value, 1 ) == -1 ) {
+               ld->ld_errno = LDAP_NO_MEMORY;
+       }
 
 
-       ber_free(ber, 1);
+       if ( 0 ) {
+error_return:;
+               ld->ld_errno =  LDAP_ENCODING_ERROR;
+       }
 
 
-       return(ld->ld_errno);
+       if ( ber != NULL ) {
+               ber_free( ber, 1 );
+       }
 
 
-exit:
-       ber_free(ber, 1);
-       ld->ld_errno =  LDAP_ENCODING_ERROR;
-       return(ld->ld_errno);
+       return ld->ld_errno;
 }
 
 
 /* ---------------------------------------------------------------------------
 }
 
 
 /* ---------------------------------------------------------------------------
-   ldap_parse_sort_control
+   ldap_create_sort_control
+   
+   Create and encode the server-side sort control.
+   
+   ld          (IN) An LDAP session handle, as obtained from a call to
+                                       ldap_init().
+
+   keyList     (IN) Points to a null-terminated array of pointers to
+                                       LDAPSortKey structures, containing a description of
+                                       each of the sort keys to be used.  The description
+                                       consists of an attribute name, ascending/descending flag,
+                                       and an optional matching rule (OID) to use.
+                          
+   isCritical  (IN) 0 - Indicates the control is not critical to the operation.
+                                       non-zero - The control is critical to the operation.
+                                        
+   ctrlp      (OUT) Returns a pointer to the LDAPControl created.  This control
+                                       SHOULD be freed by calling ldap_control_free() when done.
+   
+   
+   Ber encoding
+   
+   SortKeyList ::= SEQUENCE OF SEQUENCE {
+                  attributeType   AttributeDescription,
+                  orderingRule    [0] MatchingRuleId OPTIONAL,
+                  reverseOrder    [1] BOOLEAN DEFAULT FALSE }
+   
+   ---------------------------------------------------------------------------*/
+
+int
+ldap_create_sort_control(
+       LDAP *ld,
+       LDAPSortKey **keyList,
+       int isCritical,
+       LDAPControl **ctrlp )
+{
+       struct berval   value;
+
+       assert( ld != NULL );
+       assert( LDAP_VALID( ld ) );
+
+       if ( ld == NULL ) {
+               return LDAP_PARAM_ERROR;
+       }
+
+       if ( ctrlp == NULL ) {
+               ld->ld_errno = LDAP_PARAM_ERROR;
+               return ld->ld_errno;
+       }
+
+       ld->ld_errno = ldap_create_sort_control_value( ld, keyList, &value );
+       if ( ld->ld_errno == LDAP_SUCCESS ) {
+               ld->ld_errno = ldap_control_create( LDAP_CONTROL_SORTREQUEST,
+                       isCritical, &value, 0, ctrlp );
+               if ( ld->ld_errno != LDAP_SUCCESS ) {
+                       LDAP_FREE( value.bv_val );
+               }
+       }
+
+       return ld->ld_errno;
+}
+
+
+/* ---------------------------------------------------------------------------
+   ldap_parse_sortedresult_control
    
    Decode the server-side sort control return information.
 
    ld          (IN) An LDAP session handle, as obtained from a call to
                                        ldap_init().
 
    
    Decode the server-side sort control return information.
 
    ld          (IN) An LDAP session handle, as obtained from a call to
                                        ldap_init().
 
-   ctrls       (IN) The address of a NULL-terminated array of LDAPControl
-                                       structures, typically obtained by a call to
-                                       ldap_parse_result().
+   ctrl        (IN) The address of the LDAP Control Structure.
                                  
    returnCode (OUT) This result parameter is filled in with the sort control
                                        result code.  This parameter MUST not be NULL.
                                  
    returnCode (OUT) This result parameter is filled in with the sort control
                                        result code.  This parameter MUST not be NULL.
@@ -405,25 +483,25 @@ exit:
    ---------------------------------------------------------------------------*/
 
 int
    ---------------------------------------------------------------------------*/
 
 int
-ldap_parse_sort_control(
-       LDAP           *ld,
-       LDAPControl    **ctrls,
-       unsigned long  *returnCode,
-       char           **attribute )
+ldap_parse_sortresponse_control(
+       LDAP *ld,
+       LDAPControl *ctrl,
+       ber_int_t *returnCode,
+       char **attribute )
 {
        BerElement *ber;
 {
        BerElement *ber;
-       LDAPControl *pControl;
-       int i;
        ber_tag_t tag, berTag;
        ber_len_t berLen;
 
        ber_tag_t tag, berTag;
        ber_len_t berLen;
 
+       assert( ld != NULL );
+       assert( LDAP_VALID( ld ) );
+
        if (ld == NULL) {
        if (ld == NULL) {
-               ld->ld_errno = LDAP_PARAM_ERROR;
-               return(ld->ld_errno);
+               return LDAP_PARAM_ERROR;
        }
 
        }
 
-       if (ctrls == NULL) {
-               ld->ld_errno =  LDAP_CONTROL_NOT_FOUND;
+       if (ctrl == NULL) {
+               ld->ld_errno =  LDAP_PARAM_ERROR;
                return(ld->ld_errno);
        }
 
                return(ld->ld_errno);
        }
 
@@ -431,20 +509,14 @@ ldap_parse_sort_control(
                *attribute = NULL;
        }
 
                *attribute = NULL;
        }
 
-       /* Search the list of control responses for a sort control. */
-       for (i=0; ctrls[i]; i++) {
-               pControl = ctrls[i];
-               if (!strcmp(LDAP_CONTROL_SORTRESPONSE, pControl->ldctl_oid))
-                       goto foundSortControl;
+       if ( strcmp(LDAP_CONTROL_SORTRESPONSE, ctrl->ldctl_oid) != 0 ) {
+               /* Not sort result control */
+               ld->ld_errno = LDAP_CONTROL_NOT_FOUND;
+               return(ld->ld_errno);
        }
 
        }
 
-       /* No sort control was found. */
-       ld->ld_errno = LDAP_CONTROL_NOT_FOUND;
-       return(ld->ld_errno);
-
-foundSortControl:
        /* Create a BerElement from the berval returned in the control. */
        /* Create a BerElement from the berval returned in the control. */
-       ber = ber_init(&pControl->ldctl_value);
+       ber = ber_init(&ctrl->ldctl_value);
 
        if (ber == NULL) {
                ld->ld_errno = LDAP_NO_MEMORY;
 
        if (ber == NULL) {
                ld->ld_errno = LDAP_NO_MEMORY;