1 /* $NetBSD: bsdctype.c,v 1.9 2010/06/20 02:23:15 tnozaki Exp $ */
4 * Copyright (c)2008 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: bsdctype.c,v 1.9 2010/06/20 02:23:15 tnozaki Exp $");
32 #endif /* LIBC_SCCS and not lint */
34 #include <sys/endian.h>
43 #include "bsdctype_local.h"
44 #include "runetype_misc.h"
46 const _BSDCTypeLocale _DefaultBSDCTypeLocale
= {
52 const _BSDCTypeLocale
*_CurrentBSDCTypeLocale
= &_DefaultBSDCTypeLocale
;
56 unsigned char blp_ctype_tab
[_CTYPE_NUM_CHARS
+ 1];
57 short blp_tolower_tab
[_CTYPE_NUM_CHARS
+ 1];
58 short blp_toupper_tab
[_CTYPE_NUM_CHARS
+ 1];
59 } _BSDCTypeLocalePriv
;
62 _bsdctype_init_priv(_BSDCTypeLocalePriv
*blp
)
64 #if _CTYPE_CACHE_SIZE != _CTYPE_NUM_CHARS
67 for (i
= _CTYPE_CACHE_SIZE
; i
< _CTYPE_NUM_CHARS
; ++i
) {
68 blp
->blp_ctype_tab
[i
+ 1] = 0;
69 blp
->blp_tolower_tab
[i
+ 1] = i
;
70 blp
->blp_toupper_tab
[i
+ 1] = i
;
73 blp
->blp_ctype_tab
[0] = 0;
74 blp
->blp_tolower_tab
[0] = EOF
;
75 blp
->blp_toupper_tab
[0] = EOF
;
76 blp
->bl
.bl_ctype_tab
= &blp
->blp_ctype_tab
[0];
77 blp
->bl
.bl_tolower_tab
= &blp
->blp_tolower_tab
[0];
78 blp
->bl
.bl_toupper_tab
= &blp
->blp_toupper_tab
[0];
82 _bsdctype_read_file(const char * __restrict var
, size_t lenvar
,
83 _BSDCTypeLocalePriv
* __restrict blp
)
85 const _FileBSDCTypeLocale
*fbl
;
89 _DIAGASSERT(blp
!= NULL
);
91 if (lenvar
< sizeof(*fbl
))
93 fbl
= (const _FileBSDCTypeLocale
*)(const void *)var
;
94 if (memcmp(&fbl
->fbl_id
[0], _CTYPE_ID
, sizeof(fbl
->fbl_id
)))
96 value
= be32toh(fbl
->fbl_rev
);
97 if (value
!= _CTYPE_REV
)
99 value
= be32toh(fbl
->fbl_num_chars
);
100 if (value
!= _CTYPE_CACHE_SIZE
)
102 for (i
= 0; i
< _CTYPE_CACHE_SIZE
; ++i
) {
103 blp
->blp_ctype_tab
[i
+ 1] = fbl
->fbl_ctype_tab
[i
];
104 blp
->blp_tolower_tab
[i
+ 1] = be16toh(fbl
->fbl_tolower_tab
[i
]);
105 blp
->blp_toupper_tab
[i
+ 1] = be16toh(fbl
->fbl_toupper_tab
[i
]);
111 _bsdctype_read_runetype(const char * __restrict var
, size_t lenvar
,
112 _BSDCTypeLocalePriv
* __restrict blp
)
114 const _FileRuneLocale
*frl
;
117 _DIAGASSERT(blp
!= NULL
);
119 if (lenvar
< sizeof(*frl
))
121 lenvar
-= sizeof(*frl
);
122 frl
= (const _FileRuneLocale
*)(const void *)var
;
123 if (memcmp(_RUNECT10_MAGIC
, &frl
->frl_magic
[0], sizeof(frl
->frl_magic
)))
125 if (frl
->frl_encoding
[0] != 'N' || frl
->frl_encoding
[1] != 'O' ||
126 frl
->frl_encoding
[2] != 'N' || frl
->frl_encoding
[3] != 'E' ||
127 frl
->frl_encoding
[4] != '\0') /* XXX */
129 if (be32toh(frl
->frl_runetype_ext
.frr_nranges
) != 0 ||
130 be32toh(frl
->frl_maplower_ext
.frr_nranges
) != 0 ||
131 be32toh(frl
->frl_mapupper_ext
.frr_nranges
) != 0)
133 if (lenvar
< be32toh((uint32_t)frl
->frl_variable_len
))
135 for (i
= 0; i
< _CTYPE_CACHE_SIZE
; ++i
) {
136 blp
->blp_ctype_tab
[i
+ 1] = (unsigned char)
137 _runetype_to_ctype((_RuneType
)
138 be32toh(frl
->frl_runetype
[i
]));
139 blp
->blp_tolower_tab
[i
+ 1] = (short)
140 be32toh((uint32_t)frl
->frl_maplower
[i
]);
141 blp
->blp_toupper_tab
[i
+ 1] = (short)
142 be32toh((uint32_t)frl
->frl_mapupper
[i
]);
148 _bsdctype_load(const char * __restrict var
, size_t lenvar
,
149 _BSDCTypeLocale
** __restrict pbl
)
152 _BSDCTypeLocalePriv
*blp
;
154 _DIAGASSERT(var
!= NULL
|| lenvar
< 1);
155 _DIAGASSERT(pbl
!= NULL
);
159 blp
= malloc(sizeof(*blp
));
162 _bsdctype_init_priv(blp
);
165 _bsdctype_read_file(var
, lenvar
, blp
);
168 _bsdctype_read_runetype(var
, lenvar
, blp
);