bug 1083: Fix infinite loop in decompress_data
[elinks.git] / src / intl / charsets.h
blob7baf3cf34dcb100cdeb1b137fb202a448f7e58ef
1 #ifndef EL__INTL_CHARSETS_H
2 #define EL__INTL_CHARSETS_H
4 /* The TRE check in configure.in assumes unicode_val_T is uint32_t. */
5 typedef uint32_t unicode_val_T;
7 /* U+0020 SPACE. Normally the same as ' ' or L' ' but perhaps ELinks
8 * shouldn't rely on that. */
9 #define UCS_SPACE ((unicode_val_T) 0x0020)
11 /* U+00A0 NO-BREAK SPACE. */
12 #define UCS_NO_BREAK_SPACE ((unicode_val_T) 0x00A0)
14 /* U+00AD SOFT HYPHEN. */
15 #define UCS_SOFT_HYPHEN ((unicode_val_T) 0x00AD)
17 /* U+FFFD REPLACEMENT CHARACTER. Used when no Unicode mapping is
18 * known for a byte in a codepage, or when invalid UTF-8 is received
19 * from a terminal. After generating the character, ELinks then
20 * treats it like any other Unicode character. The user can also type
21 * this character directly, and it can occur in documents. */
22 #define UCS_REPLACEMENT_CHARACTER ((unicode_val_T) 0xFFFD)
24 /* A special value that fits in unicode_val_T but is outside the range
25 * of Unicode characters. utf8_to_unicode and cp_to_unicode return
26 * this if the input is too short. This is also used as a placeholder
27 * for the second cell of a double-cell character. */
28 #define UCS_NO_CHAR ((unicode_val_T) 0xFFFFFFFD)
30 /* If ELinks should display a double-cell character but there is only
31 * one cell available, it displays this character instead. This must
32 * be a single-cell character but need not be unique. Possible values
33 * might be U+0020 SPACE or U+303F IDEOGRAPHIC HALF FILL SPACE.
35 * Some BFU widgets (at least input fields and list boxes) currently
36 * ignore this setting and use U+0020 instead. (They first draw spaces
37 * and then overwrite with text; look for utf8_cells2bytes calls.)
38 * We should fix that if we ever change the value. */
39 #define UCS_ORPHAN_CELL ((unicode_val_T) 0x20)
41 /*   replacement character. See u2cp().
42 * UTF-8 strings should use the encoding of U+00A0 instead. */
43 #define NBSP_CHAR ((unsigned char) 1)
44 #define NBSP_CHAR_STRING "\001"
46 /* How to convert a byte from a source charset. This is used in an
47 * array (struct conv_table[256]) indexed by the byte value. */
48 struct conv_table {
49 /* 0 if this is the final byte of a character, or 1 if more
50 * bytes are needed. */
51 int t;
52 union {
53 /* If @t==0: a null-terminated string that is the
54 * corresponding character in the target charset.
55 * Normally, the string is statically allocated.
56 * However, if the conversion table is to UTF-8, then
57 * the strings in elements 0x80 to 0xFF are allocated
58 * with @mem_alloc and owned by the table. */
59 const unsigned char *str;
60 /* If @t==1: a pointer to a nested conversion table
61 * (with 256 elements) that describes how to convert
62 * each possible subsequent byte. The conversion
63 * table owns the nested conversion table. */
64 struct conv_table *tbl;
65 } u;
68 enum convert_string_mode {
69 CSM_DEFAULT, /* Convert any char. */
70 CSM_QUERY, /* Special handling of '&' and '=' chars. */
71 CSM_FORM, /* Special handling of '&' and '=' chars in forms. */
72 CSM_NONE, /* Convert nothing. */
75 /* How to translate U+00A0 NO-BREAK SPACE. If u2cp_ is converting to
76 * UTF-8, it ignores this choice and just encodes the U+00A0. */
77 enum nbsp_mode {
78 /* Convert to NBSP_CHAR. This lets the HTML renderer
79 * recognize nbsp even if the codepage doesn't support
80 * nbsp. (VISCII doesn't.) */
81 NBSP_MODE_HACK = 0,
83 /* Convert to normal ASCII space. */
84 NBSP_MODE_ASCII = 1
87 struct conv_table *get_translation_table(int, int);
88 const unsigned char *get_entity_string(const unsigned char *str,
89 const int strlen, int encoding);
91 /* The convert_string() name is also used by Samba (version 3.0.3), which
92 * provides libnss_wins.so.2, which is called somewhere inside
93 * _nss_wins_gethostbyname_r(). This name clash causes the elinks hostname
94 * lookup thread to crash so we need to rename the symbol. */
95 /* Reported by Derek Poon and filed as bug 453 */
96 #undef convert_string
97 #define convert_string convert_string_elinks
99 /* This routine converts a string from one charset to another according to the
100 * passed @convert_table, potentially also decoding SGML (HTML) entities along
101 * the way (according to @mode). It either returns dynamically allocated
102 * converted string of length @length, or if the @callback is non-NULL it calls
103 * it each few bytes instead and always returns NULL (@length is undefined).
104 * Note that it's ok not to care and pass NULL as @length. */
105 unsigned char *convert_string(struct conv_table *convert_table,
106 unsigned char *chars, int charslen, int cp,
107 enum convert_string_mode mode, int *length,
108 void (*callback)(void *data, unsigned char *buf, int buflen),
109 void *callback_data);
111 int get_cp_index(const unsigned char *);
112 unsigned char *get_cp_name(int);
113 unsigned char *get_cp_config_name(int);
114 unsigned char *get_cp_mime_name(int);
115 int is_cp_utf8(int);
116 void free_conv_table(void);
117 unsigned char *encode_utf8(unicode_val_T);
118 #ifdef CONFIG_UTF8
119 unsigned char *utf8_prevchar(unsigned char *, int, unsigned char *);
120 int utf8charlen(const unsigned char *);
121 int utf8_char2cells(unsigned char *, unsigned char *);
122 int utf8_ptr2cells(unsigned char *, unsigned char *);
123 int utf8_ptr2chars(unsigned char *, unsigned char *);
124 int utf8_cells2bytes(unsigned char *, int, unsigned char *);
125 /* How utf8_step_forward and utf8_step_backward count steps. */
126 enum utf8_step {
127 /* Each step is one character, even if it is a combining or
128 * double-width character. */
129 UTF8_STEP_CHARACTERS,
131 /* Each step is one cell. If the specified number of steps
132 * would end in the middle of a double-width character, do not
133 * include the character. */
134 UTF8_STEP_CELLS_FEWER,
136 /* Each step is one cell. If the specified number of steps
137 * would end in the middle of a double-width character,
138 * include the whole character. */
139 UTF8_STEP_CELLS_MORE
141 unsigned char *utf8_step_forward(unsigned char *, unsigned char *,
142 int, enum utf8_step, int *);
143 unsigned char *utf8_step_backward(unsigned char *, unsigned char *,
144 int, enum utf8_step, int *);
145 int unicode_to_cell(unicode_val_T);
146 unicode_val_T unicode_fold_label_case(unicode_val_T);
147 int strlen_utf8(unsigned char **);
148 #endif /* CONFIG_UTF8 */
149 unicode_val_T utf8_to_unicode(unsigned char **, const unsigned char *);
150 unicode_val_T cp_to_unicode(int, unsigned char **, const unsigned char *);
152 unicode_val_T cp2u(int, unsigned char);
153 const unsigned char *cp2utf8(int, int);
155 const unsigned char *u2cp_(unicode_val_T, int, enum nbsp_mode);
156 #define u2cp(u, to) u2cp_(u, to, NBSP_MODE_HACK)
157 #define u2cp_no_nbsp(u, to) u2cp_(u, to, NBSP_MODE_ASCII)
159 void init_charsets_lookup(void);
160 void free_charsets_lookup(void);
162 /* UTF-16 encodes each Unicode character U+0000...U+FFFF as a single
163 * 16-bit code unit, and each character U+10000...U+10FFFF as a pair
164 * of two code units: a high surrogate followed by a low surrogate.
165 * The range U+D800...U+DFFF is reserved for these surrogates. */
166 #define is_utf16_surrogate(u) (((u) & 0xFFFFF800) == 0xD800)
167 #define is_utf16_high_surrogate(u) (((u) & 0xFFFFFC00) == 0xD800)
168 #define is_utf16_low_surrogate(u) (((u) & 0xFFFFFC00) == 0xDC00)
169 #define join_utf16_surrogates(high,low) (0x10000 + (((high) - 0xD800L) << 10) + ((low) - 0xDC00))
170 #define needs_utf16_surrogates(u) ((uint32_t) ((u) - 0x10000) < 0x100000)
171 #define get_utf16_high_surrogate(u) (0xD800 + (((u) - 0x10000) >> 10))
172 #define get_utf16_low_surrogate(u) (0xDC00 + ((u) & 0x3FF))
174 #endif