]> git.sur5r.net Git - openldap/blob - libraries/libldap/getattr.c
stdlib.h should be included as <ac/stdlib.h>
[openldap] / libraries / libldap / getattr.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1990 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  getattr.c
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16 #include <ac/stdlib.h>
17
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/time.h>
21
22 #include "ldap-int.h"
23
24 char *
25 ldap_first_attribute( LDAP *ld, LDAPMessage *entry, BerElement **berout )
26 {
27         int rc;
28         ber_tag_t tag;
29         ber_len_t len = 0;
30         char *attr;
31         BerElement *ber;
32
33         Debug( LDAP_DEBUG_TRACE, "ldap_first_attribute\n", 0, 0, 0 );
34
35         assert( ld != NULL );
36         assert( LDAP_VALID( ld ) );
37         assert( entry != NULL );
38         assert( berout != NULL );
39
40         *berout = NULL;
41
42         ber = ldap_alloc_ber_with_options( ld );
43         if( ber == NULL ) {
44                 return NULL;
45         }
46
47         *ber = *entry->lm_ber;
48
49         /* 
50          * Skip past the sequence, dn, sequence of sequence leaving
51          * us at the first attribute.
52          */
53
54         tag = ber_scanf( ber, "{xl{" /*}}*/, &len );
55         if( tag == LBER_ERROR ) {
56                 ld->ld_errno = LDAP_DECODING_ERROR;
57                 ber_free( ber, 0 );
58                 return NULL;
59         }
60
61         /* set the length to avoid overrun */
62         rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
63         if( rc != LBER_OPT_SUCCESS ) {
64                 ld->ld_errno = LDAP_LOCAL_ERROR;
65                 ber_free( ber, 0 );
66                 return NULL;
67         }
68
69         if ( ber_pvt_ber_remaining( ber ) == 0 ) {
70                 assert( len == 0 );
71                 ber_free( ber, 0 );
72                 return NULL;
73         }
74         assert( len != 0 );
75
76         /* snatch the first attribute */
77         tag = ber_scanf( ber, "{ax}", &attr );
78         if( tag == LBER_ERROR ) {
79                 ld->ld_errno = LDAP_DECODING_ERROR;
80                 ber_free( ber, 0 );
81                 return NULL;
82         }
83
84         *berout = ber;
85         return attr;
86 }
87
88 /* ARGSUSED */
89 char *
90 ldap_next_attribute( LDAP *ld, LDAPMessage *entry, BerElement *ber )
91 {
92         ber_tag_t tag;
93         char *attr;
94
95         Debug( LDAP_DEBUG_TRACE, "ldap_next_attribute\n", 0, 0, 0 );
96
97         assert( ld != NULL );
98         assert( LDAP_VALID( ld ) );
99         assert( entry != NULL );
100         assert( ber != NULL );
101
102         if ( ber_pvt_ber_remaining( ber ) == 0 ) {
103                 return NULL;
104         }
105
106         /* skip sequence, snarf attribute type, skip values */
107         tag = ber_scanf( ber, "{ax}", &attr ); 
108         if( tag == LBER_ERROR ) {
109                 ld->ld_errno = LDAP_DECODING_ERROR;
110                 return NULL;
111         }
112
113         return attr;
114 }