xref: /freebsd/lib/libc/locale/collate.h (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
2  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
3  * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
4  *		at Electronni Visti IA, Kiev, Ukraine.
5  *			All rights reserved.
6  *
7  * Copyright (c) 2011 The FreeBSD Foundation
8  * All rights reserved.
9  * Portions of this software were developed by David Chisnall
10  * under sponsorship from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35 
36 #ifndef _COLLATE_H_
37 #define	_COLLATE_H_
38 
39 #include <sys/cdefs.h>
40 #include <sys/types.h>
41 #include <limits.h>
42 #include "xlocale_private.h"
43 
44 /*
45  * Work around buildworld bootstrapping from older systems whose limits.h
46  * sets COLL_WEIGHTS_MAX to 0.
47  */
48 #if COLL_WEIGHTS_MAX == 0
49 #undef COLL_WEIGHTS_MAX
50 #define COLL_WEIGHTS_MAX 10
51 #endif
52 
53 #define	COLLATE_STR_LEN		24		/* should be 64-bit multiple */
54 #define	COLLATE_VERSION		"BSD 1.0\n"
55 
56 #define	COLLATE_MAX_PRIORITY	(0x7fffffff)	/* max signed value */
57 #define	COLLATE_SUBST_PRIORITY	(0x40000000)	/* bit indicates subst table */
58 
59 #define	DIRECTIVE_UNDEF		0x00
60 #define	DIRECTIVE_FORWARD	0x01
61 #define	DIRECTIVE_BACKWARD	0x02
62 #define	DIRECTIVE_POSITION	0x04
63 #define	DIRECTIVE_UNDEFINED	0x08	/* special last weight for UNDEFINED */
64 
65 #define	DIRECTIVE_DIRECTION_MASK (DIRECTIVE_FORWARD | DIRECTIVE_BACKWARD)
66 
67 /*
68  * The collate file format is as follows:
69  *
70  * char		version[COLLATE_STR_LEN];	// must be COLLATE_VERSION
71  * collate_info_t	info;			// see below, includes padding
72  * collate_char_pri_t	char_data[256];		// 8 bit char values
73  * collate_subst_t	subst[*];		// 0 or more substitutions
74  * collate_chain_pri_t	chains[*];		// 0 or more chains
75  * collate_large_pri_t	large[*];		// extended char priorities
76  *
77  * Note that all structures must be 32-bit aligned, as each structure
78  * contains 32-bit member fields.  The entire file is mmap'd, so its
79  * critical that alignment be observed.  It is not generally safe to
80  * use any 64-bit values in the structures.
81  */
82 
83 typedef struct collate_info {
84 	uint8_t directive_count;
85 	uint8_t directive[COLL_WEIGHTS_MAX];
86 	int32_t pri_count[COLL_WEIGHTS_MAX];
87 	int32_t flags;
88 	int32_t chain_count;
89 	int32_t large_count;
90 	int32_t subst_count[COLL_WEIGHTS_MAX];
91 	int32_t undef_pri[COLL_WEIGHTS_MAX];
92 } collate_info_t;
93 
94 typedef struct collate_char {
95 	int32_t pri[COLL_WEIGHTS_MAX];
96 } collate_char_t;
97 
98 typedef struct collate_chain {
99 	wchar_t str[COLLATE_STR_LEN];
100 	int32_t pri[COLL_WEIGHTS_MAX];
101 } collate_chain_t;
102 
103 typedef struct collate_large {
104 	int32_t val;
105 	collate_char_t pri;
106 } collate_large_t;
107 
108 typedef struct collate_subst {
109 	int32_t key;
110 	int32_t pri[COLLATE_STR_LEN];
111 } collate_subst_t;
112 
113 struct xlocale_collate {
114 	struct xlocale_component header;
115 	int __collate_load_error;
116 	char * map;
117 	size_t maplen;
118 
119 	collate_info_t	*info;
120 	collate_char_t	*char_pri_table;
121 	collate_large_t	*large_pri_table;
122 	collate_chain_t	*chain_pri_table;
123 	collate_subst_t	*subst_table[COLL_WEIGHTS_MAX];
124 };
125 
126 __BEGIN_DECLS
127 int	__collate_load_tables(const char *);
128 int	__collate_equiv_value(locale_t, const wchar_t *, size_t);
129 void	_collate_lookup(struct xlocale_collate *,const wchar_t *, int *, int *,
130 	int, const int **);
131 int	__collate_range_cmp(struct xlocale_collate *, wchar_t, wchar_t);
132 size_t	_collate_wxfrm(struct xlocale_collate *, const wchar_t *, wchar_t *,
133 	size_t);
134 size_t	_collate_sxfrm(struct xlocale_collate *, const wchar_t *, char *,
135 	size_t);
136 __END_DECLS
137 
138 #endif /* !_COLLATE_H_ */
139