]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconv/gconv_int.h
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / iconv / gconv_int.h
CommitLineData
581c785b 1/* Copyright (C) 1997-2022 Free Software Foundation, Inc.
e62c19f1 2 This file is part of the GNU C Library.
e62c19f1
UD
3
4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
e62c19f1
UD
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
e62c19f1 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
e62c19f1
UD
17
18#ifndef _GCONV_INT_H
19#define _GCONV_INT_H 1
20
21#include "gconv.h"
c5738211 22#include <stdlib.h> /* For alloca used in macro below. */
6111babe
RM
23#include <ctype.h> /* For __toupper_l used in macro below. */
24#include <string.h> /* For strlen et al used in macro below. */
ec999b8e 25#include <libc-lock.h>
e62c19f1
UD
26
27__BEGIN_DECLS
28
29
9a321276 30/* Structure for alias definition. Simply two strings. */
e62c19f1
UD
31struct gconv_alias
32{
17427edd
UD
33 char *fromname;
34 char *toname;
e62c19f1
UD
35};
36
37
0d9f6793
UD
38/* Structure describing one loaded shared object. This normally are
39 objects to perform conversation but as a special case the db shared
40 object is also handled. */
d64b6ad0 41struct __gconv_loaded_object
0d9f6793 42{
b79f74cd 43 /* Name of the object. It must be the first structure element. */
0d9f6793
UD
44 const char *name;
45
46 /* Reference counter for the db functionality. If no conversion is
47 needed we unload the db library. */
48 int counter;
49
50 /* The handle for the shared object. */
b3fc5f84 51 void *handle;
0d9f6793
UD
52
53 /* Pointer to the functions the module defines. */
d64b6ad0
UD
54 __gconv_fct fct;
55 __gconv_init_fct init_fct;
56 __gconv_end_fct end_fct;
0d9f6793
UD
57};
58
59
e62c19f1
UD
60/* Description for an available conversion module. */
61struct gconv_module
62{
d2dfc5de 63 const char *from_string;
0d9f6793 64 const char *to_string;
e62c19f1 65
fab6d621
UD
66 int cost_hi;
67 int cost_lo;
e62c19f1 68
0d9f6793 69 const char *module_name;
2bd60880
UD
70
71 struct gconv_module *left; /* Prefix smaller. */
72 struct gconv_module *same; /* List of entries with identical prefix. */
2bd60880 73 struct gconv_module *right; /* Prefix larger. */
e62c19f1
UD
74};
75
76
91927b7c
AS
77/* The specification of the conversion that needs to be performed. */
78struct gconv_spec
79{
80 char *fromcode;
81 char *tocode;
82 bool translit;
83 bool ignore;
84};
85
c90a2db6
UD
86/* Flags for `gconv_open'. */
87enum
88{
89 GCONV_AVOID_NOCONV = 1 << 0
90};
91
7b503bcc
UD
92/* When GCONV_AVOID_NOCONV is set and no conversion is needed,
93 __GCONV_NULCONV should be returned. */
94enum
95{
96 __GCONV_NULCONV = -1
97};
c90a2db6 98
e62c19f1
UD
99/* Global variables. */
100
101/* Database of alias names. */
230491f0 102extern void *__gconv_alias_db attribute_hidden;
e62c19f1
UD
103
104/* Array with available modules. */
230491f0 105extern struct gconv_module *__gconv_modules_db attribute_hidden;
e62c19f1 106
6b98979f 107/* Value of the GCONV_PATH environment variable. */
aa32f798 108extern const char *__gconv_path_envvar attribute_hidden;
6b98979f 109
9e26f129 110/* Lock for the conversion database content. */
6f8a7dff 111__libc_lock_define (extern, __gconv_lock attribute_hidden)
9e26f129 112
e62c19f1 113
17c389fc
UD
114/* The gconv functions expects the name to be in upper case and complete,
115 including the trailing slashes if necessary. */
323fb88d 116#define norm_add_slashes(str,suffix) \
17c389fc
UD
117 ({ \
118 const char *cp = (str); \
119 char *result; \
120 char *tmp; \
121 size_t cnt = 0; \
db2f05ba 122 const size_t suffix_len = strlen (suffix); \
17c389fc
UD
123 \
124 while (*cp != '\0') \
125 if (*cp++ == '/') \
126 ++cnt; \
127 \
c5738211 128 tmp = result = __alloca (cp - (str) + 3 + suffix_len); \
17c389fc
UD
129 cp = (str); \
130 while (*cp != '\0') \
4b5b009c 131 *tmp++ = __toupper_l (*cp++, _nl_C_locobj_ptr); \
17c389fc
UD
132 if (cnt < 2) \
133 { \
134 *tmp++ = '/'; \
135 if (cnt < 1) \
323fb88d
UD
136 { \
137 *tmp++ = '/'; \
db2f05ba 138 if (suffix_len != 0) \
323fb88d
UD
139 tmp = __mempcpy (tmp, suffix, suffix_len); \
140 } \
17c389fc
UD
141 } \
142 *tmp = '\0'; \
143 result; \
144 })
145
146
91927b7c
AS
147/* Return in *HANDLE, a decriptor for the transformation. The function expects
148 the specification of the transformation in the structure pointed to by
149 CONV_SPEC. It only reads *CONV_SPEC and does not take ownership of it. */
150extern int __gconv_open (struct gconv_spec *conv_spec,
151 __gconv_t *handle, int flags);
152libc_hidden_proto (__gconv_open)
e62c19f1 153
7d4ec75e
AS
154/* This function accepts the charset names of the source and destination of the
155 conversion and populates *conv_spec with an equivalent conversion
156 specification that may later be used by __gconv_open. The charset names
157 might contain options in the form of suffixes that alter the conversion,
158 e.g. "ISO-10646/UTF-8/TRANSLIT". It processes the charset names, ignoring
159 and truncating any suffix options in fromcode, and processing and truncating
160 any suffix options in tocode. Supported suffix options ("TRANSLIT" or
161 "IGNORE") when found in tocode lead to the corresponding flag in *conv_spec
162 to be set to true. Unrecognized suffix options are silently discarded. If
163 the function succeeds, it returns conv_spec back to the caller. It returns
164 NULL upon failure. */
165extern struct gconv_spec *
166__gconv_create_spec (struct gconv_spec *conv_spec, const char *fromcode,
167 const char *tocode);
168libc_hidden_proto (__gconv_create_spec)
169
170/* This function frees all heap memory allocated by __gconv_create_spec. */
171extern void
172__gconv_destroy_spec (struct gconv_spec *conv_spec);
173libc_hidden_proto (__gconv_destroy_spec)
174
e62c19f1 175/* Free resources associated with transformation descriptor CD. */
8bcdb7e0
L
176extern int __gconv_close (__gconv_t cd)
177 attribute_hidden;
e62c19f1
UD
178
179/* Transform at most *INBYTESLEFT bytes from buffer starting at *INBUF
180 according to rules described by CD and place up to *OUTBYTESLEFT
63e04088 181 bytes in buffer starting at *OUTBUF. Return number of non-identical
38677ace 182 conversions in *IRREVERSIBLE if this pointer is not null. */
55985355
UD
183extern int __gconv (__gconv_t cd, const unsigned char **inbuf,
184 const unsigned char *inbufend, unsigned char **outbuf,
8bcdb7e0
L
185 unsigned char *outbufend, size_t *irreversible)
186 attribute_hidden;
e62c19f1
UD
187
188/* Return in *HANDLE a pointer to an array with *NSTEPS elements describing
189 the single steps necessary for transformation from FROMSET to TOSET. */
55985355
UD
190extern int __gconv_find_transform (const char *toset, const char *fromset,
191 struct __gconv_step **handle,
8bcdb7e0
L
192 size_t *nsteps, int flags)
193 attribute_hidden;
e62c19f1 194
6b98979f
UD
195/* Search for transformation in cache data. */
196extern int __gconv_lookup_cache (const char *toset, const char *fromset,
197 struct __gconv_step **handle, size_t *nsteps,
8bcdb7e0
L
198 int flags)
199 attribute_hidden;
6b98979f 200
9a018f6c
UD
201/* Compare the two name for whether they are after alias expansion the
202 same. This function uses the cache and fails if none is
203 loaded. */
204extern int __gconv_compare_alias_cache (const char *name1, const char *name2,
8bcdb7e0
L
205 int *result)
206 attribute_hidden;
9a018f6c 207
6b98979f 208/* Free data associated with a step's structure. */
8bcdb7e0
L
209extern void __gconv_release_step (struct __gconv_step *step)
210 attribute_hidden;
6b98979f 211
c5288d37
AS
212/* Read all the configuration data and cache it if not done so already. */
213extern void __gconv_load_conf (void) attribute_hidden;
e62c19f1 214
6b98979f 215/* Try to read module cache file. */
8bcdb7e0 216extern int __gconv_load_cache (void) attribute_hidden;
6b98979f 217
230491f0
UD
218/* Retrieve pointer to internal cache. */
219extern void *__gconv_get_cache (void);
220
221/* Retrieve pointer to internal module database. */
222extern struct gconv_module *__gconv_get_modules_db (void);
223
224/* Retrieve pointer to internal alias database. */
225extern void *__gconv_get_alias_db (void);
226
e62c19f1 227/* Comparison function to search alias. */
dff07c4b
UD
228extern int __gconv_alias_compare (const void *p1, const void *p2)
229 attribute_hidden;
e62c19f1
UD
230
231/* Clear reference to transformation step implementations which might
232 cause the code to be unloaded. */
55985355 233extern int __gconv_close_transform (struct __gconv_step *steps,
8bcdb7e0
L
234 size_t nsteps)
235 attribute_hidden;
e62c19f1 236
0db59742
UD
237/* Free all resources allocated for the transformation record when
238 using the cache. */
8bcdb7e0
L
239extern void __gconv_release_cache (struct __gconv_step *steps, size_t nsteps)
240 attribute_hidden;
0db59742 241
e62c19f1
UD
242/* Load shared object named by NAME. If already loaded increment reference
243 count. */
8bcdb7e0
L
244extern struct __gconv_loaded_object *__gconv_find_shlib (const char *name)
245 attribute_hidden;
e62c19f1
UD
246
247/* Release shared object. If no further reference is available unload
248 the object. */
8bcdb7e0
L
249extern void __gconv_release_shlib (struct __gconv_loaded_object *handle)
250 attribute_hidden;
e62c19f1
UD
251
252/* Fill STEP with information about builtin module with NAME. */
55985355 253extern void __gconv_get_builtin_trans (const char *name,
8bcdb7e0
L
254 struct __gconv_step *step)
255 attribute_hidden;
e62c19f1 256
7ac6fad9
FW
257/* Transliteration using the locale's data. */
258extern int __gconv_transliterate (struct __gconv_step *step,
259 struct __gconv_step_data *step_data,
260 const unsigned char *inbufstart,
261 const unsigned char **inbufp,
262 const unsigned char *inbufend,
263 unsigned char **outbufstart,
264 size_t *irreversible);
ba7b4d29 265libc_hidden_proto (__gconv_transliterate)
e62c19f1 266
dd9423a6 267/* If NAME is an codeset alias expand it. */
8bcdb7e0
L
268extern int __gconv_compare_alias (const char *name1, const char *name2)
269 attribute_hidden;
dd9423a6
UD
270
271
e62c19f1
UD
272/* Builtin transformations. */
273#ifdef _LIBC
f9ad060c 274# define __BUILTIN_TRANSFORM(Name) \
55985355
UD
275 extern int Name (struct __gconv_step *step, \
276 struct __gconv_step_data *data, \
277 const unsigned char **inbuf, \
f1d5c60d
UD
278 const unsigned char *inbufend, \
279 unsigned char **outbufstart, size_t *irreversible, \
280 int do_flush, int consume_incomplete)
e62c19f1 281
f9ad060c
UD
282__BUILTIN_TRANSFORM (__gconv_transform_ascii_internal);
283__BUILTIN_TRANSFORM (__gconv_transform_internal_ascii);
284__BUILTIN_TRANSFORM (__gconv_transform_utf8_internal);
285__BUILTIN_TRANSFORM (__gconv_transform_internal_utf8);
286__BUILTIN_TRANSFORM (__gconv_transform_ucs2_internal);
287__BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2);
288__BUILTIN_TRANSFORM (__gconv_transform_ucs2reverse_internal);
289__BUILTIN_TRANSFORM (__gconv_transform_internal_ucs2reverse);
290__BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4);
291__BUILTIN_TRANSFORM (__gconv_transform_ucs4_internal);
292__BUILTIN_TRANSFORM (__gconv_transform_internal_ucs4le);
293__BUILTIN_TRANSFORM (__gconv_transform_ucs4le_internal);
294__BUILTIN_TRANSFORM (__gconv_transform_internal_utf16);
295__BUILTIN_TRANSFORM (__gconv_transform_utf16_internal);
296# undef __BUITLIN_TRANSFORM
297
298/* Specialized conversion function for a single byte to INTERNAL, recognizing
299 only ASCII characters. */
300extern wint_t __gconv_btwoc_ascii (struct __gconv_step *step, unsigned char c);
e62c19f1 301
e62c19f1
UD
302#endif
303
304__END_DECLS
305
306#endif /* gconv_int.h */