VLC 4.0.0-dev
vlc_charset.h
Go to the documentation of this file.
1/*****************************************************************************
2 * vlc_charset.h: Unicode UTF-8 wrappers function
3 *****************************************************************************
4 * Copyright (C) 2003-2005 VLC authors and VideoLAN
5 * Copyright © 2005-2010 Rémi Denis-Courmont
6 *
7 * Author: Rémi Denis-Courmont
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
23
24#ifndef VLC_CHARSET_H
25#define VLC_CHARSET_H 1
26
27/**
28 * \file vlc_charset.h
29 * \ingroup charset
30 * \defgroup charset Character sets
31 * \ingroup strings
32 * @{
33 */
34
35/**
36 * Decodes a code point from UTF-8.
37 *
38 * Converts the first character in a UTF-8 sequence into a Unicode code point.
39 *
40 * \param str an UTF-8 bytes sequence [IN]
41 * \param pwc address of a location to store the code point [OUT]
42 *
43 * \return the number of bytes occupied by the decoded code point
44 *
45 * \retval -1 not a valid UTF-8 sequence
46 * \retval 0 null character (i.e. str points to an empty string)
47 * \retval 1 (non-null) ASCII character
48 * \retval 2-4 non-ASCII character
49 */
50VLC_API ssize_t vlc_towc(const char *str, uint32_t *restrict pwc);
51
52/**
53 * Checks UTF-8 validity.
54 *
55 * Checks whether a null-terminated string is a valid UTF-8 bytes sequence.
56 *
57 * \param str string to check
58 *
59 * \retval str the string is a valid null-terminated UTF-8 sequence
60 * \retval NULL the string is not an UTF-8 sequence
61 */
62VLC_USED static inline const char *IsUTF8(const char *str)
64 ssize_t n;
65 uint32_t cp;
66
67 while ((n = vlc_towc(str, &cp)) != 0)
68 if (likely(n != -1))
69 str += n;
70 else
71 return NULL;
72 return str;
73}
74
75/**
76 * Checks ASCII validity.
77 *
78 * Checks whether a null-terminated string is a valid ASCII bytes sequence
79 * (non-printable ASCII characters 1-31 are permitted).
80 *
81 * \param str string to check
82 *
83 * \retval str the string is a valid null-terminated ASCII sequence
84 * \retval NULL the string is not an ASCII sequence
85 */
86VLC_USED static inline const char *IsASCII(const char *str)
88 unsigned char c;
89
90 for (const char *p = str; (c = *p) != '\0'; p++)
91 if (c >= 0x80)
92 return NULL;
93 return str;
94}
95
96/**
97 * Removes non-UTF-8 sequences.
98 *
99 * Replaces invalid or <i>over-long</i> UTF-8 bytes sequences within a
100 * null-terminated string with question marks. This is so that the string can
101 * be printed at least partially.
102 *
103 * \warning Do not use this were correctness is critical. use IsUTF8() and
104 * handle the error case instead. This function is mainly for display or debug.
105 *
106 * \note Converting from Latin-1 to UTF-8 in place is not possible (the string
107 * size would be increased). So it is not attempted even if it would otherwise
108 * be less disruptive.
109 *
110 * \retval str the string is a valid null-terminated UTF-8 sequence
111 * (i.e. no changes were made)
112 * \retval NULL the string is not an UTF-8 sequence
113 */
114static inline char *EnsureUTF8(char *str)
116 char *ret = str;
117 ssize_t n;
118 uint32_t cp;
119
120 while ((n = vlc_towc(str, &cp)) != 0)
121 if (likely(n != -1))
122 str += n;
123 else
124 {
125 *str++ = '?';
126 ret = NULL;
127 }
128 return ret;
129}
130
131/**
132 * \defgroup iconv iconv wrappers
133 *
134 * (defined in src/extras/libc.c)
135 * @{
136 */
137
138#define VLC_ICONV_ERR ((size_t) -1)
139typedef void *vlc_iconv_t;
140VLC_API vlc_iconv_t vlc_iconv_open( const char *, const char * ) VLC_USED;
141VLC_API size_t vlc_iconv( vlc_iconv_t, const char **, size_t *, char **, size_t * ) VLC_USED;
144/** @} */
145
146#include <stdarg.h>
147
148VLC_API int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap );
149VLC_API int utf8_fprintf( FILE *, const char *, ... ) VLC_FORMAT( 2, 3 );
150VLC_API char * vlc_strcasestr(const char *, const char *) VLC_USED;
151
152VLC_API char * FromCharset( const char *charset, const void *data, size_t data_size ) VLC_USED;
153VLC_API void * ToCharset( const char *charset, const char *in, size_t *outsize ) VLC_USED;
154
155#ifdef __APPLE__
156# include <CoreFoundation/CoreFoundation.h>
157
158/* Obtains a copy of the contents of a CFString in specified encoding.
159 * Returns char* (must be freed by caller) or NULL on failure.
160 */
161VLC_USED static inline char *FromCFString(const CFStringRef cfString,
162 const CFStringEncoding cfStringEncoding)
163{
164 // Try the quick way to obtain the buffer
165 const char *tmpBuffer = CFStringGetCStringPtr(cfString, cfStringEncoding);
166
167 if (tmpBuffer != NULL) {
168 return strdup(tmpBuffer);
169 }
170
171 // The quick way did not work, try the long way
172 CFIndex length = CFStringGetLength(cfString);
173 CFIndex maxSize =
174 CFStringGetMaximumSizeForEncoding(length, cfStringEncoding);
175
176 // If result would exceed LONG_MAX, kCFNotFound is returned
177 if (unlikely(maxSize == kCFNotFound)) {
178 return NULL;
179 }
180
181 // Account for the null terminator
182 maxSize++;
183
184 char *buffer = (char *)malloc(maxSize);
185
186 if (unlikely(buffer == NULL)) {
187 return NULL;
188 }
189
190 // Copy CFString in requested encoding to buffer
191 Boolean success = CFStringGetCString(cfString, buffer, maxSize, cfStringEncoding);
192
193 if (!success)
194 FREENULL(buffer);
195 return buffer;
196}
197#endif
198
199#ifdef _WIN32
201static inline char *FromWide (const wchar_t *wide)
202{
203 size_t len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
204 if (len == 0)
205 return NULL;
206
207 char *out = (char *)malloc (len);
208
209 if (likely(out))
210 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
211 return out;
212}
213
215static inline wchar_t *ToWide (const char *utf8)
216{
217 int len = MultiByteToWideChar (CP_UTF8, 0, utf8, -1, NULL, 0);
218 if (len == 0)
219 return NULL;
220
221 wchar_t *out = (wchar_t *)malloc (len * sizeof (wchar_t));
222
223 if (likely(out))
224 MultiByteToWideChar (CP_UTF8, 0, utf8, -1, out, len);
225 return out;
226}
227
229static inline char *ToCodePage (unsigned cp, const char *utf8)
230{
231 wchar_t *wide = ToWide (utf8);
232 if (wide == NULL)
233 return NULL;
234
235 size_t len = WideCharToMultiByte (cp, 0, wide, -1, NULL, 0, NULL, NULL);
236 if (len == 0) {
237 free(wide);
238 return NULL;
239 }
240
241 char *out = (char *)malloc (len);
242 if (likely(out != NULL))
243 WideCharToMultiByte (cp, 0, wide, -1, out, len, NULL, NULL);
244 free (wide);
245 return out;
246}
247
249static inline char *FromCodePage (unsigned cp, const char *mb)
250{
251 int len = MultiByteToWideChar (cp, 0, mb, -1, NULL, 0);
252 if (len == 0)
253 return NULL;
254
255 wchar_t *wide = (wchar_t *)malloc (len * sizeof (wchar_t));
256 if (unlikely(wide == NULL))
257 return NULL;
258 MultiByteToWideChar (cp, 0, mb, -1, wide, len);
259
260 char *utf8 = FromWide (wide);
261 free (wide);
262 return utf8;
263}
264
266static inline char *FromANSI (const char *ansi)
267{
268 return FromCodePage (GetACP (), ansi);
269}
270
272static inline char *ToANSI (const char *utf8)
273{
274 return ToCodePage (GetACP (), utf8);
275}
276
277# define FromLocale FromANSI
278# define ToLocale ToANSI
279# define LocaleFree(s) free((char *)(s))
280# define FromLocaleDup FromANSI
281# define ToLocaleDup ToANSI
282
283#elif defined(__OS2__)
284
285VLC_USED static inline char *FromLocale (const char *locale)
286{
287 return locale ? FromCharset ((char *)"", locale, strlen(locale)) : NULL;
288}
289
290VLC_USED static inline char *ToLocale (const char *utf8)
291{
292 size_t outsize;
293 return utf8 ? (char *)ToCharset ("", utf8, &outsize) : NULL;
294}
295
296VLC_USED static inline void LocaleFree (const char *str)
297{
298 free ((char *)str);
299}
300
301VLC_USED static inline char *FromLocaleDup (const char *locale)
302{
303 return FromCharset ("", locale, strlen(locale));
304}
305
306VLC_USED static inline char *ToLocaleDup (const char *utf8)
307{
308 size_t outsize;
309 return (char *)ToCharset ("", utf8, &outsize);
310}
311
312#else
313
314# define FromLocale(l) (l)
315# define ToLocale(u) (u)
316# define LocaleFree(s) ((void)(s))
317# define FromLocaleDup strdup
318# define ToLocaleDup strdup
319#endif
320
321/**
322 * Converts a nul-terminated string from ISO-8859-1 to UTF-8.
323 */
324static inline char *FromLatin1 (const char *latin)
326 char *str = (char *)malloc (2 * strlen (latin) + 1), *utf8 = str;
327 unsigned char c;
328
329 if (str == NULL)
330 return NULL;
331
332 while ((c = *(latin++)) != '\0')
333 {
334 if (c >= 0x80)
335 {
336 *(utf8++) = 0xC0 | (c >> 6);
337 *(utf8++) = 0x80 | (c & 0x3F);
338 }
339 else
340 *(utf8++) = c;
341 }
342 *(utf8++) = '\0';
343
344 utf8 = (char *)realloc (str, utf8 - str);
345 return utf8 ? utf8 : str;
346}
347
348/**
349 * \defgroup c_locale C/POSIX locale functions
350 * @{
351 */
352
353/**
354 * Parses a double in C locale.
355 *
356 * This function parses a double-precision floating point number from a string
357 * just like the standard strtod() but it uses the C locale. In other words, it
358 * expects the POSIX/C/American decimal format regardless of the current
359 * numeric locale.
360 *
361 * \param str nul-terminated string to parse
362 * \param[out] end storage space for a pointer to the first unparsed byte
363 * (or NULL to discard it)
364 * \return the parsed double value (zero if no character could be parsed)
365 */
366VLC_API double vlc_strtod_c(const char *restrict str, char **restrict end)
368
369/**
370 * Parses a float in C locale.
371 *
372 * This function parses a single-precision floating point number from a string
373 * just like the standard strtof() but it uses the C locale. In other words, it
374 * expects the POSIX/C/American decimal format regardless of the current
375 * numeric locale.
376 *
377 * \param str nul-terminated string to parse
378 * \param[out] end storage space for a pointer to the first unparsed byte
379 * (or NULL to discard it)
380 * \return the parsed double value (zero if no character could be parsed)
381 */
382VLC_API float vlc_strtof_c(const char *restrict str, char **restrict end)
384
385/**
386 * Parses a double in C locale.
387 *
388 * This function parses a double-precision floating point number from a string
389 * just like the standard atof() but it uses the C locale. In other words, it
390 * expects the POSIX/C/American decimal format regardless of the current
391 * numeric locale.
392 *
393 * \param str nul-terminated string to parse
394 * \return the parsed double value (zero if no character could be parsed)
395 */
396VLC_USED static inline double vlc_atof_c(const char *str)
398 return vlc_strtod_c(str, NULL);
399}
400
401/**
402 * Formats a string using the C locale.
403 *
404 * This function formats a string from a format string and a variable argument
405 * list, just like the standard vasprintf() but using the C locale for the
406 * formatting of numerals.
407 *
408 * \param[out] p storage space for a pointer to the heap-allocated formatted
409 * string (undefined on error)
410 * \param fmt format string
411 * \param ap variable argument list
412 * \return number of bytes formatted (excluding the nul terminator)
413 * or -1 on error
414 */
415VLC_API int vlc_vasprintf_c(char **restrict p, const char *restrict fmt,
416 va_list ap) VLC_USED;
417
418/**
419 * Formats a string using the C locale.
420 *
421 * This function formats a string from a format string and a variable argument
422 * list, just like the standard vasprintf() but using the C locale for the
423 * formatting of numerals.
424 *
425 * \param[out] p storage space for a pointer to the heap-allocated formatted
426 * string (undefined on error)
427 * \param fmt format string
428 * \return number of bytes formatted (excluding the nul terminator)
429 * or -1 on error
430 */
431VLC_API int vlc_asprintf_c( char **, const char *, ... ) VLC_USED;
433int vlc_vsscanf_c(const char *, const char *, va_list) VLC_USED;
434int vlc_sscanf_c(const char*, const char*, ...) VLC_USED
435#ifdef __GNUC__
436__attribute__((format(scanf, 2, 3)))
437#endif
438;
439
440/** @} */
441/** @} */
442
443#endif
#define VLC_USED
Definition: fourcc_gen.c:32
#define VLC_API
Definition: fourcc_gen.c:31
#define p(t)
int vlc_asprintf_c(char **, const char *,...)
Formats a string using the C locale.
double vlc_strtod_c(const char *restrict str, char **restrict end)
Parses a double in C locale.
Definition: charset.c:46
static double vlc_atof_c(const char *str)
Parses a double in C locale.
Definition: vlc_charset.h:397
int vlc_sscanf_c(const char *, const char *,...)
int vlc_vasprintf_c(char **restrict p, const char *restrict fmt, va_list ap)
Formats a string using the C locale.
Definition: charset.c:74
float vlc_strtof_c(const char *restrict str, char **restrict end)
Parses a float in C locale.
Definition: charset.c:60
int vlc_vsscanf_c(const char *, const char *, va_list)
#define unlikely(p)
Predicted false condition.
Definition: vlc_common.h:257
#define VLC_MALLOC
Definition: vlc_common.h:164
#define likely(p)
Predicted true condition.
Definition: vlc_common.h:248
#define VLC_FORMAT(x, y)
String format function annotation.
Definition: vlc_common.h:204
char * vlc_strcasestr(const char *, const char *)
Look for an UTF-8 string within another one in a case-insensitive fashion.
Definition: unicode.c:191
void * ToCharset(const char *charset, const char *in, size_t *outsize)
Converts a nul-terminated UTF-8 string to a given character encoding.
Definition: unicode.c:274
static char * EnsureUTF8(char *str)
Removes non-UTF-8 sequences.
Definition: vlc_charset.h:115
char * FromCharset(const char *charset, const void *data, size_t data_size)
Converts a string from the given character encoding to utf-8.
Definition: unicode.c:232
static const char * IsUTF8(const char *str)
Checks UTF-8 validity.
Definition: vlc_charset.h:63
#define ToLocaleDup
Definition: vlc_charset.h:319
#define ToLocale(u)
Definition: vlc_charset.h:316
#define LocaleFree(s)
Definition: vlc_charset.h:317
ssize_t vlc_towc(const char *str, uint32_t *restrict pwc)
Decodes a code point from UTF-8.
Definition: unicode.c:115
static char * FromLatin1(const char *latin)
Converts a nul-terminated string from ISO-8859-1 to UTF-8.
Definition: vlc_charset.h:325
int utf8_vfprintf(FILE *stream, const char *fmt, va_list ap)
Formats an UTF-8 string as vfprintf(), then print it, with appropriate conversion to local encoding.
Definition: unicode.c:52
#define FromLocaleDup
Definition: vlc_charset.h:318
int utf8_fprintf(FILE *, const char *,...)
Formats an UTF-8 string as fprintf(), then print it, with appropriate conversion to local encoding.
Definition: unicode.c:104
static const char * IsASCII(const char *str)
Checks ASCII validity.
Definition: vlc_charset.h:87
#define FromLocale(l)
Definition: vlc_charset.h:315
void * vlc_iconv_t
Definition: vlc_charset.h:140
size_t vlc_iconv(vlc_iconv_t, const char **, size_t *, char **, size_t *)
int vlc_iconv_close(vlc_iconv_t)
vlc_iconv_t vlc_iconv_open(const char *, const char *)
This file is a collection of common definitions and types.
#define FREENULL(a)
Definition: vlc_common.h:987
char * strdup(const char *)