xref: /netbsd-src/external/bsd/openldap/dist/libraries/libldap/string.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: string.c,v 1.1.1.2 2010/03/08 02:14:20 lukem Exp $	*/
2 
3 /* OpenLDAP: pkg/ldap/libraries/libldap/string.c,v 1.23.2.4 2009/01/22 00:00:56 kurt Exp */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2009 The OpenLDAP Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 
18 /*
19  * Locale-specific 1-byte character versions
20  * See utf-8.c for UTF-8 versions
21  */
22 
23 #include "portable.h"
24 
25 #include <ac/stdlib.h>
26 #include <ac/string.h>
27 #include <ac/time.h>
28 #include <ac/ctype.h>
29 
30 #include "ldap-int.h"
31 
32 
33 #if defined ( HAVE_STRSPN )
34 #define int_strspn strspn
35 #else
36 static int int_strspn( const char *str, const char *delim )
37 {
38 	int pos;
39 	const char *p=delim;
40 
41 	for( pos=0; (*str) ; pos++,str++) {
42 		if (*str!=*p) {
43 			for( p=delim; (*p) ; p++ ) {
44 				if (*str==*p) {
45 					break;
46 				}
47 		  	}
48 		}
49 
50 		if (*p=='\0') {
51 			return pos;
52 		}
53 	}
54 	return pos;
55 }
56 #endif
57 
58 #if defined( HAVE_STRPBRK )
59 #define int_strpbrk strpbrk
60 #else
61 static char *(int_strpbrk)( const char *str, const char *accept )
62 {
63 	const char *p;
64 
65 	for( ; (*str) ; str++ ) {
66 		for( p=accept; (*p) ; p++) {
67 			if (*str==*p) {
68 				return str;
69 			}
70 		}
71 	}
72 
73 	return NULL;
74 }
75 #endif
76 
77 char *(ldap_pvt_strtok)( char *str, const char *delim, char **pos )
78 {
79 	char *p;
80 
81 	if (pos==NULL) {
82 		return NULL;
83 	}
84 
85 	if (str==NULL) {
86 		if (*pos==NULL) {
87 			return NULL;
88 		}
89 
90 		str=*pos;
91 	}
92 
93 	/* skip any initial delimiters */
94 	str += int_strspn( str, delim );
95 	if (*str == '\0') {
96 		return NULL;
97 	}
98 
99 	p = int_strpbrk( str, delim );
100 	if (p==NULL) {
101 		*pos = NULL;
102 
103 	} else {
104 		*p ='\0';
105 		*pos = p+1;
106 	}
107 
108 	return str;
109 }
110 
111 char *
112 ldap_pvt_str2upper( char *str )
113 {
114 	char    *s;
115 
116 	/* to upper */
117 	if ( str ) {
118 		for ( s = str; *s; s++ ) {
119 			*s = TOUPPER( (unsigned char) *s );
120 		}
121 	}
122 
123 	return( str );
124 }
125 
126 struct berval *
127 ldap_pvt_str2upperbv( char *str, struct berval *bv )
128 {
129 	char    *s = NULL;
130 
131 	assert( bv != NULL );
132 
133 	/* to upper */
134 	if ( str ) {
135 		for ( s = str; *s; s++ ) {
136 			*s = TOUPPER( (unsigned char) *s );
137 		}
138 	}
139 
140 	bv->bv_val = str;
141 	bv->bv_len = (ber_len_t)(s - str);
142 
143 	return( bv );
144 }
145 
146 char *
147 ldap_pvt_str2lower( char *str )
148 {
149 	char    *s;
150 
151 	/* to lower */
152 	if ( str ) {
153 		for ( s = str; *s; s++ ) {
154 			*s = TOLOWER( (unsigned char) *s );
155 		}
156 	}
157 
158 	return( str );
159 }
160 
161 struct berval *
162 ldap_pvt_str2lowerbv( char *str, struct berval *bv )
163 {
164 	char    *s = NULL;
165 
166 	assert( bv != NULL );
167 
168 	/* to lower */
169 	if ( str ) {
170 		for ( s = str; *s; s++ ) {
171 			*s = TOLOWER( (unsigned char) *s );
172 		}
173 	}
174 
175 	bv->bv_val = str;
176 	bv->bv_len = (ber_len_t)(s - str);
177 
178 	return( bv );
179 }
180