#include "ldap-int.h"
int
LDAP_CALL
ldap_create_sort_control (
LDAP *ld,
LDAPsortkey **sortKeyList,
const char ctl_iscritical,
LDAPControl **ctrlp
)
{
BerElement *ber;
int i, rc;
if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
return( LDAP_PARAM_ERROR );
}
if ( sortKeyList == NULL || ctrlp == NULL ) {
LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
return ( LDAP_PARAM_ERROR );
}
if ( ( nsldapi_alloc_ber_with_options( ld, &ber ) ) != LDAP_SUCCESS ) {
LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
return( LDAP_NO_MEMORY );
}
if ( ber_printf( ber, "{" ) == -1 ) {
goto encoding_error_exit;
}
for ( i = 0; sortKeyList[i] != NULL; i++ ) {
if ( ber_printf( ber, "{s", (sortKeyList[i])->sk_attrtype )
== -1 ) {
goto encoding_error_exit;
}
if ( (sortKeyList[i])->sk_matchruleoid != NULL ) {
if ( ber_printf( ber, "ts", LDAP_TAG_SK_MATCHRULE,
(sortKeyList[i])->sk_matchruleoid )
== -1 ) {
goto encoding_error_exit;
}
}
if ( (sortKeyList[i])->sk_reverseorder ) {
if ( ber_printf( ber, "tb}", LDAP_TAG_SK_REVERSE,
(sortKeyList[i])->sk_reverseorder ) == -1 ) {
goto encoding_error_exit;
}
} else {
if ( ber_printf( ber, "}" ) == -1 ) {
goto encoding_error_exit;
}
}
}
if ( ber_printf( ber, "}" ) == -1 ) {
goto encoding_error_exit;
}
rc = nsldapi_build_control( LDAP_CONTROL_SORTREQUEST, ber, 1,
ctl_iscritical, ctrlp );
LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
return( rc );
encoding_error_exit:
LDAP_SET_LDERRNO( ld, LDAP_ENCODING_ERROR, NULL, NULL );
ber_free( ber, 1 );
return( LDAP_ENCODING_ERROR );
}
int
LDAP_CALL
ldap_parse_sort_control (
LDAP *ld,
LDAPControl **ctrlp,
unsigned long *result,
char **attribute
)
{
BerElement *ber;
int i, foundSortControl;
LDAPControl *sortCtrlp;
ber_len_t len;
ber_tag_t tag;
char *attr;
if ( !NSLDAPI_VALID_LDAP_POINTER( ld ) || result == NULL ||
attribute == NULL ) {
return( LDAP_PARAM_ERROR );
}
if ( ctrlp == NULL ) {
LDAP_SET_LDERRNO( ld, LDAP_CONTROL_NOT_FOUND, NULL, NULL );
return ( LDAP_CONTROL_NOT_FOUND );
}
foundSortControl = 0;
for ( i = 0; (( ctrlp[i] != NULL ) && ( !foundSortControl )); i++ ) {
foundSortControl = !strcmp( ctrlp[i]->ldctl_oid, LDAP_CONTROL_SORTRESPONSE );
}
if ( !foundSortControl ) {
LDAP_SET_LDERRNO( ld, LDAP_CONTROL_NOT_FOUND, NULL, NULL );
return ( LDAP_CONTROL_NOT_FOUND );
} else {
sortCtrlp = ctrlp[i-1];
}
if ( ( ber = ber_init( &sortCtrlp->ldctl_value ) ) == NULL ) {
LDAP_SET_LDERRNO( ld, LDAP_NO_MEMORY, NULL, NULL );
return( LDAP_NO_MEMORY );
}
if ( ber_scanf( ber, "{i", result ) == LBER_ERROR ) {
LDAP_SET_LDERRNO( ld, LDAP_DECODING_ERROR, NULL, NULL );
ber_free( ber, 1 );
return( LDAP_DECODING_ERROR );
}
if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SR_ATTRTYPE ) {
if ( ber_scanf( ber, "ta", &tag, &attr ) == LBER_ERROR ) {
LDAP_SET_LDERRNO( ld, LDAP_DECODING_ERROR, NULL, NULL );
ber_free( ber, 1 );
return( LDAP_DECODING_ERROR );
}
*attribute = attr;
} else {
*attribute = NULL;
}
if ( ber_scanf( ber, "}" ) == LBER_ERROR ) {
LDAP_SET_LDERRNO( ld, LDAP_DECODING_ERROR, NULL, NULL );
ber_free( ber, 1 );
return( LDAP_DECODING_ERROR );
}
ber_free(ber,1);
return( LDAP_SUCCESS );
}
static int count_tokens(const char *s)
{
int count = 0;
const char *p = s;
int whitespace = 1;
while (*p != '\0') {
if (whitespace) {
if (!isspace(*p)) {
whitespace = 0;
count++;
}
} else {
if (isspace(*p)) {
whitespace = 1;
}
}
p++;
}
return count;
}
static int read_next_token(const char **s,LDAPsortkey **key)
{
char c = 0;
const char *pos = *s;
int retval = 0;
LDAPsortkey *new_key = NULL;
const char *matchrule_source = NULL;
int matchrule_size = 0;
const char *attrdesc_source = NULL;
int attrdesc_size = 0;
int reverse = 0;
int state = 0;
while ( ((c = *pos++) != '\0') && (state != 4) ) {
switch (state) {
case 0:
if (!isspace(c)) {
if ('-' == c) {
reverse = 1;
} else {
attrdesc_source = pos - 1;
state = 1;
}
}
break;
case 1:
if ( isspace(c) || (':' == c)) {
attrdesc_size = (pos - attrdesc_source) - 1;
if (':' == c) {
state = 2;
} else {
state = 4;
}
}
break;
case 2:
if (!isspace(c)) {
matchrule_source = pos - 1;
state = 3;
} else {
state = 4;
}
break;
case 3:
if (isspace(c)) {
matchrule_size = (pos - matchrule_source) - 1;
state = 4;
}
break;
default:
break;
}
}
if (3 == state) {
matchrule_size = (pos - matchrule_source) - 1;
}
if (1 == state) {
attrdesc_size = (pos - attrdesc_source) - 1;
}
if (NULL == attrdesc_source) {
return -1;
}
new_key = (LDAPsortkey*)NSLDAPI_MALLOC(sizeof(LDAPsortkey));
if (0 == new_key) {
return LDAP_NO_MEMORY;
}
new_key->sk_attrtype = (char *)NSLDAPI_MALLOC(attrdesc_size + 1);
if (NULL != matchrule_source) {
new_key->sk_matchruleoid = (char *)NSLDAPI_MALLOC(
matchrule_size + 1);
} else {
new_key->sk_matchruleoid = NULL;
}
memcpy(new_key->sk_attrtype,attrdesc_source,attrdesc_size);
*(new_key->sk_attrtype + attrdesc_size) = '\0';
if (NULL != matchrule_source) {
memcpy(new_key->sk_matchruleoid,matchrule_source,matchrule_size);
*(new_key->sk_matchruleoid + matchrule_size) = '\0';
}
new_key->sk_reverseorder = reverse;
*s = pos - 1;
*key = new_key;
return retval;
}
int
LDAP_CALL
ldap_create_sort_keylist (
LDAPsortkey ***sortKeyList,
const char *string_rep
)
{
int count = 0;
LDAPsortkey **pointer_array = NULL;
const char *current_position = NULL;
int retval = 0;
int i = 0;
if (NULL == string_rep) {
return LDAP_PARAM_ERROR;
}
if (NULL == sortKeyList) {
return LDAP_PARAM_ERROR;
}
count = count_tokens(string_rep);
if (0 == count) {
*sortKeyList = NULL;
return LDAP_PARAM_ERROR;
}
pointer_array = (LDAPsortkey**)NSLDAPI_MALLOC(sizeof(LDAPsortkey*)
* (count + 1) );
if (NULL == pointer_array) {
return LDAP_NO_MEMORY;
}
current_position = string_rep;
for (i = 0; i < count; i++) {
if (0 != (retval = read_next_token(¤t_position,&(pointer_array[i])))) {
pointer_array[count] = NULL;
ldap_free_sort_keylist(pointer_array);
*sortKeyList = NULL;
return retval;
}
}
pointer_array[count] = NULL;
*sortKeyList = pointer_array;
return LDAP_SUCCESS;
}
void
LDAP_CALL
ldap_free_sort_keylist (
LDAPsortkey **sortKeyList
)
{
LDAPsortkey *this_one = NULL;
int i = 0;
if ( NULL == sortKeyList ) {
return;
}
for (this_one = sortKeyList[0]; this_one ; this_one = sortKeyList[++i]) {
if (NULL != this_one->sk_attrtype) {
NSLDAPI_FREE(this_one->sk_attrtype);
}
if (NULL != this_one->sk_matchruleoid) {
NSLDAPI_FREE(this_one->sk_matchruleoid);
}
NSLDAPI_FREE(this_one);
}
NSLDAPI_FREE(sortKeyList);
}