xref: /titanic_52/usr/src/lib/libc/port/locale/mblocal.h (revision 4d40e39c66a331aefef24083480939f0e78a9045)
1 /*
2  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
4  * Copyright (c) 2004 Tim J. Robbins.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _MBLOCAL_H_
30 #define	_MBLOCAL_H_
31 
32 #include "runetype.h"
33 #include "lctype.h"
34 #include <uchar.h>
35 
36 /*
37  * Actual implementation structures for mbstate_t data.
38  *
39  * All of the conversion states are independent of one another, with the
40  * exception of that used for mbrtoc16(). That needs to encode data not as a
41  * wide-character but as UTF-16 data, which means handling surrogate pairs. To
42  * minimize the amount of state in each locale, we instead have a conversion
43  * state for this which includes all the other conversion states, plus extra
44  * data to accomodate this.
45  */
46 typedef struct {
47 	wchar_t	ch;
48 } _BIG5State;
49 
50 typedef struct {
51 	wchar_t	ch;
52 	int	set;
53 	int	want;
54 } _EucState;
55 
56 typedef struct {
57 	int	count;
58 	uchar_t	bytes[4];
59 } _GB18030State;
60 
61 typedef struct {
62 	int	count;
63 	uchar_t	bytes[2];
64 } _GB2312State;
65 
66 typedef struct {
67 	wchar_t	ch;
68 } _GBKState;
69 
70 typedef struct {
71 	wchar_t	ch;
72 } _MSKanjiState;
73 
74 typedef struct {
75 	wchar_t	ch;
76 	int	want;
77 	wchar_t	lbound;
78 } _UTF8State;
79 
80 typedef struct {
81 	union {
82 		_BIG5State	c16_big5;
83 		_EucState	c16_euc;
84 		_GB18030State	c16_gb18030;
85 		_GB2312State	c16_gb2312;
86 		_GBKState	c16_gbk;
87 		_MSKanjiState	c16_mskanji;
88 		_UTF8State	c16_utf8;
89 	} c16_state;
90 	char16_t c16_surrogate;
91 } _CHAR16State;
92 
93 /*
94  * Rune initialization function prototypes.
95  */
96 void	_none_init(struct lc_ctype *);
97 void	_UTF8_init(struct lc_ctype *);
98 void	_EUC_CN_init(struct lc_ctype *);
99 void	_EUC_JP_init(struct lc_ctype *);
100 void	_EUC_KR_init(struct lc_ctype *);
101 void	_EUC_TW_init(struct lc_ctype *);
102 void	_GB18030_init(struct lc_ctype *);
103 void	_GB2312_init(struct lc_ctype *);
104 void	_GBK_init(struct lc_ctype *);
105 void	_BIG5_init(struct lc_ctype *);
106 void	_MSKanji_init(struct lc_ctype *);
107 
108 typedef size_t (*mbrtowc_pfn_t)(wchar_t *_RESTRICT_KYWD,
109     const char *_RESTRICT_KYWD, size_t, mbstate_t *_RESTRICT_KYWD, boolean_t);
110 typedef size_t (*wcrtomb_pfn_t)(char *_RESTRICT_KYWD, wchar_t,
111     mbstate_t *_RESTRICT_KYWD);
112 size_t __mbsnrtowcs_std(wchar_t *_RESTRICT_KYWD, const char **_RESTRICT_KYWD,
113     size_t, size_t, mbstate_t *_RESTRICT_KYWD, mbrtowc_pfn_t);
114 size_t __wcsnrtombs_std(char *_RESTRICT_KYWD, const wchar_t **_RESTRICT_KYWD,
115     size_t, size_t, mbstate_t *_RESTRICT_KYWD, wcrtomb_pfn_t);
116 
117 #define	MIN(a, b)	((a) < (b) ? (a) : (b))
118 
119 #endif	/* _MBLOCAL_H_ */
120