xref: /freebsd/lib/libc/locale/collate.h (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1c3d0cca4SAndrey A. Chernov /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
4d0a68f8dSBaptiste Daroussin  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
5c3d0cca4SAndrey A. Chernov  * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
6c3d0cca4SAndrey A. Chernov  *		at Electronni Visti IA, Kiev, Ukraine.
7c3d0cca4SAndrey A. Chernov  *			All rights reserved.
8c3d0cca4SAndrey A. Chernov  *
93c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
105b5fa75aSEd Maste  *
113c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
123c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
133c87aa1dSDavid Chisnall  *
14c3d0cca4SAndrey A. Chernov  * Redistribution and use in source and binary forms, with or without
15c3d0cca4SAndrey A. Chernov  * modification, are permitted provided that the following conditions
16c3d0cca4SAndrey A. Chernov  * are met:
17c3d0cca4SAndrey A. Chernov  * 1. Redistributions of source code must retain the above copyright
18c3d0cca4SAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer.
19c3d0cca4SAndrey A. Chernov  * 2. Redistributions in binary form must reproduce the above copyright
20c3d0cca4SAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer in the
21c3d0cca4SAndrey A. Chernov  *    documentation and/or other materials provided with the distribution.
22c3d0cca4SAndrey A. Chernov  *
23c3d0cca4SAndrey A. Chernov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
24c3d0cca4SAndrey A. Chernov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25c3d0cca4SAndrey A. Chernov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26c3d0cca4SAndrey A. Chernov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
27c3d0cca4SAndrey A. Chernov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28c3d0cca4SAndrey A. Chernov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29c3d0cca4SAndrey A. Chernov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30c3d0cca4SAndrey A. Chernov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31c3d0cca4SAndrey A. Chernov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32c3d0cca4SAndrey A. Chernov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33c3d0cca4SAndrey A. Chernov  * SUCH DAMAGE.
34c3d0cca4SAndrey A. Chernov  */
35c3d0cca4SAndrey A. Chernov 
36f43a321bSAlexey Zelkin #ifndef _COLLATE_H_
37f43a321bSAlexey Zelkin #define	_COLLATE_H_
38c3d0cca4SAndrey A. Chernov 
39c3d0cca4SAndrey A. Chernov #include <sys/types.h>
40c3d0cca4SAndrey A. Chernov #include <limits.h>
413c87aa1dSDavid Chisnall #include "xlocale_private.h"
42c3d0cca4SAndrey A. Chernov 
432a6abeebSBaptiste Daroussin /*
44536451f9SBaptiste Daroussin  * Work around buildworld bootstrapping from older systems whose limits.h
452a6abeebSBaptiste Daroussin  * sets COLL_WEIGHTS_MAX to 0.
462a6abeebSBaptiste Daroussin  */
472a6abeebSBaptiste Daroussin #if COLL_WEIGHTS_MAX == 0
482a6abeebSBaptiste Daroussin #undef COLL_WEIGHTS_MAX
492a6abeebSBaptiste Daroussin #define COLL_WEIGHTS_MAX 10
502a6abeebSBaptiste Daroussin #endif
51c3d0cca4SAndrey A. Chernov 
522a6abeebSBaptiste Daroussin #define	COLLATE_STR_LEN		24		/* should be 64-bit multiple */
53cc7edd25SThomas Munro 
54cc7edd25SThomas Munro #define	COLLATE_FMT_VERSION_LEN	12
55cc7edd25SThomas Munro #define	COLLATE_FMT_VERSION	"BSD 1.0\n"
56c3d0cca4SAndrey A. Chernov 
572a6abeebSBaptiste Daroussin #define	COLLATE_MAX_PRIORITY	(0x7fffffff)	/* max signed value */
582a6abeebSBaptiste Daroussin #define	COLLATE_SUBST_PRIORITY	(0x40000000)	/* bit indicates subst table */
592a6abeebSBaptiste Daroussin 
602a6abeebSBaptiste Daroussin #define	DIRECTIVE_UNDEF		0x00
612a6abeebSBaptiste Daroussin #define	DIRECTIVE_FORWARD	0x01
622a6abeebSBaptiste Daroussin #define	DIRECTIVE_BACKWARD	0x02
632a6abeebSBaptiste Daroussin #define	DIRECTIVE_POSITION	0x04
642a6abeebSBaptiste Daroussin #define	DIRECTIVE_UNDEFINED	0x08	/* special last weight for UNDEFINED */
652a6abeebSBaptiste Daroussin 
662a6abeebSBaptiste Daroussin #define	DIRECTIVE_DIRECTION_MASK (DIRECTIVE_FORWARD | DIRECTIVE_BACKWARD)
672a6abeebSBaptiste Daroussin 
682a6abeebSBaptiste Daroussin /*
692a6abeebSBaptiste Daroussin  * The collate file format is as follows:
702a6abeebSBaptiste Daroussin  *
71cc7edd25SThomas Munro  * char	fmt_version[COLLATE_FMT_VERSION_LEN];	// must be COLLATE_FMT_VERSION
72cc7edd25SThomas Munro  * char	def_version[XLOCALE_DEF_VERSION_LEN];	// NUL-terminated, may be empty
732a6abeebSBaptiste Daroussin  * collate_info_t	info;			// see below, includes padding
742a6abeebSBaptiste Daroussin  * collate_char_pri_t	char_data[256];		// 8 bit char values
752a6abeebSBaptiste Daroussin  * collate_subst_t	subst[*];		// 0 or more substitutions
762a6abeebSBaptiste Daroussin  * collate_chain_pri_t	chains[*];		// 0 or more chains
772a6abeebSBaptiste Daroussin  * collate_large_pri_t	large[*];		// extended char priorities
782a6abeebSBaptiste Daroussin  *
792a6abeebSBaptiste Daroussin  * Note that all structures must be 32-bit aligned, as each structure
802a6abeebSBaptiste Daroussin  * contains 32-bit member fields.  The entire file is mmap'd, so its
812a6abeebSBaptiste Daroussin  * critical that alignment be observed.  It is not generally safe to
822a6abeebSBaptiste Daroussin  * use any 64-bit values in the structures.
832a6abeebSBaptiste Daroussin  */
842a6abeebSBaptiste Daroussin 
852a6abeebSBaptiste Daroussin typedef struct collate_info {
862a6abeebSBaptiste Daroussin 	uint8_t directive_count;
872a6abeebSBaptiste Daroussin 	uint8_t directive[COLL_WEIGHTS_MAX];
882a6abeebSBaptiste Daroussin 	int32_t pri_count[COLL_WEIGHTS_MAX];
892a6abeebSBaptiste Daroussin 	int32_t flags;
902a6abeebSBaptiste Daroussin 	int32_t chain_count;
912a6abeebSBaptiste Daroussin 	int32_t large_count;
922a6abeebSBaptiste Daroussin 	int32_t subst_count[COLL_WEIGHTS_MAX];
932a6abeebSBaptiste Daroussin 	int32_t undef_pri[COLL_WEIGHTS_MAX];
942a6abeebSBaptiste Daroussin } collate_info_t;
952a6abeebSBaptiste Daroussin 
962a6abeebSBaptiste Daroussin typedef struct collate_char {
972a6abeebSBaptiste Daroussin 	int32_t pri[COLL_WEIGHTS_MAX];
982a6abeebSBaptiste Daroussin } collate_char_t;
992a6abeebSBaptiste Daroussin 
1002a6abeebSBaptiste Daroussin typedef struct collate_chain {
1012a6abeebSBaptiste Daroussin 	wchar_t str[COLLATE_STR_LEN];
1022a6abeebSBaptiste Daroussin 	int32_t pri[COLL_WEIGHTS_MAX];
1032a6abeebSBaptiste Daroussin } collate_chain_t;
1042a6abeebSBaptiste Daroussin 
1052a6abeebSBaptiste Daroussin typedef struct collate_large {
1062a6abeebSBaptiste Daroussin 	int32_t val;
1072a6abeebSBaptiste Daroussin 	collate_char_t pri;
1082a6abeebSBaptiste Daroussin } collate_large_t;
1092a6abeebSBaptiste Daroussin 
1102a6abeebSBaptiste Daroussin typedef struct collate_subst {
1112a6abeebSBaptiste Daroussin 	int32_t key;
1122a6abeebSBaptiste Daroussin 	int32_t pri[COLLATE_STR_LEN];
1132a6abeebSBaptiste Daroussin } collate_subst_t;
1143c87aa1dSDavid Chisnall 
1153c87aa1dSDavid Chisnall struct xlocale_collate {
1163c87aa1dSDavid Chisnall 	struct xlocale_component header;
1173c87aa1dSDavid Chisnall 	int __collate_load_error;
1182a6abeebSBaptiste Daroussin 	char * map;
1192a6abeebSBaptiste Daroussin 	size_t maplen;
1203c87aa1dSDavid Chisnall 
1212a6abeebSBaptiste Daroussin 	collate_info_t	*info;
1222a6abeebSBaptiste Daroussin 	collate_char_t	*char_pri_table;
1232a6abeebSBaptiste Daroussin 	collate_large_t	*large_pri_table;
1242a6abeebSBaptiste Daroussin 	collate_chain_t	*chain_pri_table;
1252a6abeebSBaptiste Daroussin 	collate_subst_t	*subst_table[COLL_WEIGHTS_MAX];
1263c87aa1dSDavid Chisnall };
1273c87aa1dSDavid Chisnall 
128c3d0cca4SAndrey A. Chernov __BEGIN_DECLS
12976692b80SAndrey A. Chernov int	__collate_load_tables(const char *);
1302a6abeebSBaptiste Daroussin int	__collate_equiv_value(locale_t, const wchar_t *, size_t);
1312a6abeebSBaptiste Daroussin void	_collate_lookup(struct xlocale_collate *,const wchar_t *, int *, int *,
1322a6abeebSBaptiste Daroussin 	int, const int **);
13312eae8c8SAndrey A. Chernov int	__collate_range_cmp(char, char);
13412eae8c8SAndrey A. Chernov int	__wcollate_range_cmp(wchar_t, wchar_t);
1352a6abeebSBaptiste Daroussin size_t	_collate_wxfrm(struct xlocale_collate *, const wchar_t *, wchar_t *,
1362a6abeebSBaptiste Daroussin 	size_t);
1372a6abeebSBaptiste Daroussin size_t	_collate_sxfrm(struct xlocale_collate *, const wchar_t *, char *,
1382a6abeebSBaptiste Daroussin 	size_t);
139c3d0cca4SAndrey A. Chernov __END_DECLS
140c3d0cca4SAndrey A. Chernov 
141f43a321bSAlexey Zelkin #endif /* !_COLLATE_H_ */
142