.
[glibc/history.git] / iconvdata / iso-ir-165.h
blob80d1a1c44fdc25bf22d7ee141d564972f8dfc59a
1 /* Tables for conversion to and from ISO-IR-165.
2 converting from UCS using gaps.
3 Copyright (C) 2000, 2003, 2007 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #ifndef _ISO_IR_165_H
23 #define _ISO_IR_165_H 1
25 #include <gconv.h>
26 #include <stdint.h>
28 struct gap
30 uint16_t start;
31 uint16_t end;
32 int32_t idx;
35 /* Table for ISO-IR-165 (CCITT Chinese) to UCS4 conversion. */
36 #define ISOIR165_FROMSIZE 0x2284
37 extern const uint16_t __isoir165_to_tab[ISOIR165_FROMSIZE];
40 /* XXX If we at some point need an offset value to decode the byte
41 sequences another parameter can be added. */
42 static inline uint32_t
43 __attribute ((always_inline))
44 isoir165_to_ucs4 (const unsigned char **s, size_t avail)
46 unsigned char ch = *(*s);
47 unsigned char ch2;
48 uint32_t res;
50 if (ch <= 0x20 || ch >= 0x7f)
51 return __UNKNOWN_10646_CHAR;
53 if (avail < 2)
54 return 0;
56 ch2 = (*s)[1];
57 if (ch2 <= 0x20 || ch2 >= 0x7f)
58 return __UNKNOWN_10646_CHAR;
60 res = __isoir165_to_tab[(ch - 0x21) * 94 + (ch2 - 0x21)];
61 if (res == 0)
62 return __UNKNOWN_10646_CHAR;
64 *s += 2;
65 return res;
69 /* Tables for ISO-IR-165 (CCITT Chinese) from UCS4 conversion. */
70 extern const struct gap __isoir165_from_idx[];
71 extern const char __isoir165_from_tab[];
73 static inline size_t
74 __attribute ((always_inline))
75 ucs4_to_isoir165 (uint32_t wch, unsigned char *s, size_t avail)
77 unsigned int ch = (unsigned int) wch;
78 const char *cp;
79 const struct gap *rp = __isoir165_from_idx;
81 if (ch > 0xffe5)
82 /* This is an illegal character. */
83 return __UNKNOWN_10646_CHAR;
85 while (ch > rp->end)
86 ++rp;
87 if (ch < rp->start)
88 /* This is an illegal character. */
89 return __UNKNOWN_10646_CHAR;
91 /* The two bytes following the index given in this record give the
92 encoding in ISO-IR-165. Unless the bytes are zero. */
93 cp = &__isoir165_from_tab[(ch + rp->idx) * 2];
94 if (*cp == '\0')
95 /* This is an illegal character. */
96 return __UNKNOWN_10646_CHAR;
98 if (avail < 2)
99 return 0;
101 s[0] = cp[0];
102 s[1] = cp[1];
104 return 2;
107 #endif /* iso-ir-165.h */