1 /* $NetBSD: multibyte.h,v 1.8 2024/06/07 13:53:23 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c)2002 Citrus Project, 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 _MULTIBYTE_H_ 30 #define _MULTIBYTE_H_ 31 32 #include <stdalign.h> 33 34 /* mbstate_t private */ 35 36 #ifdef _BSD_MBSTATE_T_ 37 typedef _BSD_MBSTATE_T_ mbstate_t; 38 #undef _BSD_MBSTATE_T_ 39 #endif 40 41 typedef struct _RuneStatePriv { 42 _RuneLocale *__runelocale; 43 char __private[]; 44 } _RuneStatePriv; 45 46 __CTASSERT(alignof(struct _RuneStatePriv) >= alignof(void *)); 47 __CTASSERT(sizeof(_RuneStatePriv) % alignof(void *) == 0); 48 __CTASSERT(alignof(struct _RuneStatePriv) >= alignof(int)); 49 __CTASSERT(sizeof(_RuneStatePriv) % alignof(int) == 0); 50 51 typedef union _RuneState { 52 mbstate_t __pad; 53 struct _RuneStatePriv __priv; 54 #define rs_runelocale __priv.__runelocale 55 #define rs_private __priv.__private 56 } _RuneState; 57 #define _PRIVSIZE (sizeof(mbstate_t)-offsetof(_RuneStatePriv, __private)) 58 59 #define _RUNE_LOCALE(loc) \ 60 ((_RuneLocale *)((loc)->part_impl[LC_CTYPE])) 61 62 #define _CITRUS_CTYPE(loc) \ 63 (((_RuneLocale *)((loc)->part_impl[LC_CTYPE]))->rl_citrus_ctype) 64 65 /* */ 66 67 static __inline _RuneState * 68 _ps_to_runestate(mbstate_t *ps) 69 { 70 return (_RuneState *)(void *)ps; 71 } 72 73 static __inline _RuneState const * 74 _ps_to_runestate_const(mbstate_t const *ps) 75 { 76 return (_RuneState const *)(void const *)ps; 77 } 78 79 static __inline _RuneLocale * 80 _ps_to_runelocale(mbstate_t const *ps) 81 { 82 return _ps_to_runestate_const(ps)->rs_runelocale; 83 } 84 85 static __inline _citrus_ctype_t 86 _ps_to_ctype(mbstate_t const *ps, locale_t loc) 87 { 88 if (!ps) 89 return _CITRUS_CTYPE(loc); 90 91 _DIAGASSERT(_ps_to_runelocale(ps) != NULL); 92 93 return _ps_to_runelocale(ps)->rl_citrus_ctype; 94 } 95 96 static __inline void * 97 _ps_to_private(mbstate_t *ps) 98 { 99 if (ps == NULL) 100 return NULL; 101 return _ps_to_runestate(ps)->rs_private; 102 } 103 104 static __inline void const * 105 _ps_to_private_const(mbstate_t const *ps) 106 { 107 if (ps == NULL) 108 return NULL; 109 return _ps_to_runestate_const(ps)->rs_private; 110 } 111 112 static __inline void 113 _init_ps(_RuneLocale *rl, mbstate_t *ps) 114 { 115 size_t dum; 116 _ps_to_runestate(ps)->rs_runelocale = rl; 117 _citrus_ctype_mbrtowc(rl->rl_citrus_ctype, NULL, NULL, 0, 118 _ps_to_private(ps), &dum); 119 } 120 121 static __inline void 122 _fixup_ps(_RuneLocale *rl, mbstate_t *ps, int forceinit) 123 { 124 /* for future multi-locale facility */ 125 _DIAGASSERT(rl != NULL); 126 127 if (ps != NULL && (_ps_to_runelocale(ps) == NULL || forceinit)) { 128 _init_ps(rl, ps); 129 } 130 } 131 132 #endif /*_MULTIBYTE_H_*/ 133