111695Sache/*-
211695Sache * Copyright (c) 1993
311695Sache *	The Regents of the University of California.  All rights reserved.
411695Sache *
511695Sache * This code is derived from software contributed to Berkeley by
611695Sache * Paul Borman at Krystal Technologies.
711695Sache *
8227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
9227753Stheraven * All rights reserved.
10227753Stheraven * Portions of this software were developed by David Chisnall
11227753Stheraven * under sponsorship from the FreeBSD Foundation.
12227753Stheraven *
1311695Sache * Redistribution and use in source and binary forms, with or without
1411695Sache * modification, are permitted provided that the following conditions
1511695Sache * are met:
1611695Sache * 1. Redistributions of source code must retain the above copyright
1711695Sache *    notice, this list of conditions and the following disclaimer.
1811695Sache * 2. Redistributions in binary form must reproduce the above copyright
1911695Sache *    notice, this list of conditions and the following disclaimer in the
2011695Sache *    documentation and/or other materials provided with the distribution.
2111695Sache * 4. Neither the name of the University nor the names of its contributors
2211695Sache *    may be used to endorse or promote products derived from this software
2311695Sache *    without specific prior written permission.
2411695Sache *
2511695Sache * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2611695Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2711695Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2811695Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2911695Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3011695Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3111695Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3211695Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3311695Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3411695Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3511695Sache * SUCH DAMAGE.
3611695Sache */
3711695Sache
3892986Sobrien#include <sys/cdefs.h>
3992986Sobrien__FBSDID("$FreeBSD$");
4092986Sobrien
41150065Sstefanf#include <ctype.h>
423050Sache#include <stdio.h>
43132820Stjr#include <runetype.h>
44227753Stheraven#include <wchar.h>
45227753Stheraven#include "mblocal.h"
463050Sache
47102227Smike__ct_rune_t
48227753Stheraven___tolower_l(c, l)
49102227Smike	__ct_rune_t c;
50227753Stheraven	locale_t l;
513050Sache{
52129065Stjr	size_t lim;
53227753Stheraven	FIX_LOCALE(l);
54227753Stheraven	_RuneRange *rr = &XLOCALE_CTYPE(l)->runes->__maplower_ext;
55129065Stjr	_RuneEntry *base, *re;
563050Sache
5714812Sache	if (c < 0 || c == EOF)
5814812Sache		return(c);
5914812Sache
60129065Stjr	/* Binary search -- see bsearch.c for explanation. */
61130961Stjr	base = rr->__ranges;
62130961Stjr	for (lim = rr->__nranges; lim != 0; lim >>= 1) {
63129065Stjr		re = base + (lim >> 1);
64130961Stjr		if (re->__min <= c && c <= re->__max)
65130961Stjr			return (re->__map + c - re->__min);
66130961Stjr		else if (c > re->__max) {
67129065Stjr			base = re + 1;
68129065Stjr			lim--;
69129065Stjr		}
703050Sache	}
7161218Sache
723050Sache	return(c);
733050Sache}
74227753Stheraven__ct_rune_t
75227753Stheraven___tolower(c)
76227753Stheraven	__ct_rune_t c;
77227753Stheraven{
78227753Stheraven	return ___tolower_l(c, __get_locale());
79227753Stheraven}
80