1/*	$NetBSD: string.c,v 1.3 2021/08/14 16:14:56 christos Exp $	*/
2
3/* $OpenLDAP$ */
4/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 *
6 * Copyright 1998-2021 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 <sys/cdefs.h>
24__RCSID("$NetBSD: string.c,v 1.3 2021/08/14 16:14:56 christos Exp $");
25
26#include "portable.h"
27
28#include <ac/stdlib.h>
29#include <ac/string.h>
30#include <ac/time.h>
31#include <ac/ctype.h>
32
33#include "ldap-int.h"
34
35
36#if defined ( HAVE_STRSPN )
37#define int_strspn strspn
38#else
39static int int_strspn( const char *str, const char *delim )
40{
41	int pos;
42	const char *p=delim;
43
44	for( pos=0; (*str) ; pos++,str++) {
45		if (*str!=*p) {
46			for( p=delim; (*p) ; p++ ) {
47				if (*str==*p) {
48					break;
49				}
50		  	}
51		}
52
53		if (*p=='\0') {
54			return pos;
55		}
56	}
57	return pos;
58}
59#endif
60
61#if defined( HAVE_STRPBRK )
62#define int_strpbrk strpbrk
63#else
64static char *(int_strpbrk)( const char *str, const char *accept )
65{
66	const char *p;
67
68	for( ; (*str) ; str++ ) {
69		for( p=accept; (*p) ; p++) {
70			if (*str==*p) {
71				return str;
72			}
73		}
74	}
75
76	return NULL;
77}
78#endif
79
80char *(ldap_pvt_strtok)( char *str, const char *delim, char **pos )
81{
82	char *p;
83
84	if (pos==NULL) {
85		return NULL;
86	}
87
88	if (str==NULL) {
89		if (*pos==NULL) {
90			return NULL;
91		}
92
93		str=*pos;
94	}
95
96	/* skip any initial delimiters */
97	str += int_strspn( str, delim );
98	if (*str == '\0') {
99		return NULL;
100	}
101
102	p = int_strpbrk( str, delim );
103	if (p==NULL) {
104		*pos = NULL;
105
106	} else {
107		*p ='\0';
108		*pos = p+1;
109	}
110
111	return str;
112}
113
114char *
115ldap_pvt_str2upper( char *str )
116{
117	char    *s;
118
119	/* to upper */
120	if ( str ) {
121		for ( s = str; *s; s++ ) {
122			*s = TOUPPER( (unsigned char) *s );
123		}
124	}
125
126	return( str );
127}
128
129struct berval *
130ldap_pvt_str2upperbv( char *str, struct berval *bv )
131{
132	char    *s = NULL;
133
134	assert( bv != NULL );
135
136	/* to upper */
137	if ( str ) {
138		for ( s = str; *s; s++ ) {
139			*s = TOUPPER( (unsigned char) *s );
140		}
141	}
142
143	bv->bv_val = str;
144	bv->bv_len = (ber_len_t)(s - str);
145
146	return( bv );
147}
148
149char *
150ldap_pvt_str2lower( char *str )
151{
152	char    *s;
153
154	/* to lower */
155	if ( str ) {
156		for ( s = str; *s; s++ ) {
157			*s = TOLOWER( (unsigned char) *s );
158		}
159	}
160
161	return( str );
162}
163
164struct berval *
165ldap_pvt_str2lowerbv( char *str, struct berval *bv )
166{
167	char    *s = NULL;
168
169	assert( bv != NULL );
170
171	/* to lower */
172	if ( str ) {
173		for ( s = str; *s; s++ ) {
174			*s = TOLOWER( (unsigned char) *s );
175		}
176	}
177
178	bv->bv_val = str;
179	bv->bv_len = (ber_len_t)(s - str);
180
181	return( bv );
182}
183