1350a3d3eSAndrey A. Chernov /*- 2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*8a16b7a1SPedro F. Giffuni * 4350a3d3eSAndrey A. Chernov * Copyright (c) 1993 5350a3d3eSAndrey A. Chernov * The Regents of the University of California. All rights reserved. 6350a3d3eSAndrey A. Chernov * 7350a3d3eSAndrey A. Chernov * This code is derived from software contributed to Berkeley by 8350a3d3eSAndrey A. Chernov * Paul Borman at Krystal Technologies. 9350a3d3eSAndrey A. Chernov * 103c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation 113c87aa1dSDavid Chisnall * All rights reserved. 123c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall 133c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation. 143c87aa1dSDavid Chisnall * 15350a3d3eSAndrey A. Chernov * Redistribution and use in source and binary forms, with or without 16350a3d3eSAndrey A. Chernov * modification, are permitted provided that the following conditions 17350a3d3eSAndrey A. Chernov * are met: 18350a3d3eSAndrey A. Chernov * 1. Redistributions of source code must retain the above copyright 19350a3d3eSAndrey A. Chernov * notice, this list of conditions and the following disclaimer. 20350a3d3eSAndrey A. Chernov * 2. Redistributions in binary form must reproduce the above copyright 21350a3d3eSAndrey A. Chernov * notice, this list of conditions and the following disclaimer in the 22350a3d3eSAndrey A. Chernov * documentation and/or other materials provided with the distribution. 23fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 24350a3d3eSAndrey A. Chernov * may be used to endorse or promote products derived from this software 25350a3d3eSAndrey A. Chernov * without specific prior written permission. 26350a3d3eSAndrey A. Chernov * 27350a3d3eSAndrey A. Chernov * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28350a3d3eSAndrey A. Chernov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29350a3d3eSAndrey A. Chernov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30350a3d3eSAndrey A. Chernov * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31350a3d3eSAndrey A. Chernov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32350a3d3eSAndrey A. Chernov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33350a3d3eSAndrey A. Chernov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34350a3d3eSAndrey A. Chernov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35350a3d3eSAndrey A. Chernov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36350a3d3eSAndrey A. Chernov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37350a3d3eSAndrey A. Chernov * SUCH DAMAGE. 38350a3d3eSAndrey A. Chernov */ 39350a3d3eSAndrey A. Chernov 40333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 41333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 42333fc21eSDavid E. O'Brien 4361310091SStefan Farfeleder #include <ctype.h> 442f04ec53SAndrey A. Chernov #include <stdio.h> 45a0998ce6STim J. Robbins #include <runetype.h> 463c87aa1dSDavid Chisnall #include <wchar.h> 473c87aa1dSDavid Chisnall #include "mblocal.h" 482f04ec53SAndrey A. Chernov 49abbd8902SMike Barcroft __ct_rune_t 50c83f3fc4SCraig Rodrigues ___tolower_l(__ct_rune_t c, locale_t l) 512f04ec53SAndrey A. Chernov { 5245a11576STim J. Robbins size_t lim; 533c87aa1dSDavid Chisnall FIX_LOCALE(l); 543c87aa1dSDavid Chisnall _RuneRange *rr = &XLOCALE_CTYPE(l)->runes->__maplower_ext; 5545a11576STim J. Robbins _RuneEntry *base, *re; 562f04ec53SAndrey A. Chernov 577871e368SAndrey A. Chernov if (c < 0 || c == EOF) 58f9cbdf41SAndrey A. Chernov return(c); 597871e368SAndrey A. Chernov 6045a11576STim J. Robbins /* Binary search -- see bsearch.c for explanation. */ 61ddc1ededSTim J. Robbins base = rr->__ranges; 62ddc1ededSTim J. Robbins for (lim = rr->__nranges; lim != 0; lim >>= 1) { 6345a11576STim J. Robbins re = base + (lim >> 1); 64ddc1ededSTim J. Robbins if (re->__min <= c && c <= re->__max) 65ddc1ededSTim J. Robbins return (re->__map + c - re->__min); 66ddc1ededSTim J. Robbins else if (c > re->__max) { 6745a11576STim J. Robbins base = re + 1; 6845a11576STim J. Robbins lim--; 6945a11576STim J. Robbins } 702f04ec53SAndrey A. Chernov } 718b96e6c9SAndrey A. Chernov 722f04ec53SAndrey A. Chernov return(c); 732f04ec53SAndrey A. Chernov } 743c87aa1dSDavid Chisnall __ct_rune_t 75c83f3fc4SCraig Rodrigues ___tolower(__ct_rune_t c) 763c87aa1dSDavid Chisnall { 773c87aa1dSDavid Chisnall return ___tolower_l(c, __get_locale()); 783c87aa1dSDavid Chisnall } 79