2008-11-13 07:39:08 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2001-2004 Unicode, Inc.
|
2018-03-22 17:01:38 +01:00
|
|
|
*
|
2008-11-13 07:39:08 +01:00
|
|
|
* Disclaimer
|
2018-03-22 17:01:38 +01:00
|
|
|
*
|
2008-11-13 07:39:08 +01:00
|
|
|
* This source code is provided as is by Unicode, Inc. No claims are
|
|
|
|
* made as to fitness for any particular purpose. No warranties of any
|
|
|
|
* kind are expressed or implied. The recipient agrees to determine
|
|
|
|
* applicability of information provided. If this file has been
|
|
|
|
* purchased on magnetic or optical media from Unicode, Inc., the
|
|
|
|
* sole remedy for any claim will be exchange of defective media
|
|
|
|
* within 90 days of receipt.
|
2018-03-22 17:01:38 +01:00
|
|
|
*
|
2008-11-13 07:39:08 +01:00
|
|
|
* Limitations on Rights to Redistribute This Code
|
2018-03-22 17:01:38 +01:00
|
|
|
*
|
2008-11-13 07:39:08 +01:00
|
|
|
* Unicode, Inc. hereby grants the right to freely use the information
|
|
|
|
* supplied in this file in the creation of products supporting the
|
|
|
|
* Unicode Standard, and to make copies of this file in any form
|
|
|
|
* for internal or external distribution as long as this notice
|
|
|
|
* remains attached.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
Conversions between UTF32, UTF-16, and UTF-8. Header file.
|
|
|
|
|
|
|
|
Several funtions are included here, forming a complete set of
|
|
|
|
conversions between the three formats. UTF-7 is not included
|
|
|
|
here, but is handled in a separate source file.
|
|
|
|
|
|
|
|
Each of these routines takes pointers to input buffers and output
|
|
|
|
buffers. The input buffers are const.
|
|
|
|
|
|
|
|
Each routine converts the text between *sourceStart and sourceEnd,
|
|
|
|
putting the result into the buffer between *targetStart and
|
2018-03-22 17:01:38 +01:00
|
|
|
targetEnd. Note: the end pointers are *after* the last item: e.g.
|
2008-11-13 07:39:08 +01:00
|
|
|
*(sourceEnd - 1) is the last item.
|
|
|
|
|
|
|
|
The return result indicates whether the conversion was successful,
|
|
|
|
and if not, whether the problem was in the source or target buffers.
|
|
|
|
(Only the first encountered problem is indicated.)
|
|
|
|
|
|
|
|
After the conversion, *sourceStart and *targetStart are both
|
|
|
|
updated to point to the end of last text successfully converted in
|
|
|
|
the respective buffers.
|
|
|
|
|
|
|
|
Input parameters:
|
|
|
|
sourceStart - pointer to a pointer to the source buffer.
|
|
|
|
The contents of this are modified on return so that
|
|
|
|
it points at the next thing to be converted.
|
|
|
|
targetStart - similarly, pointer to pointer to the target buffer.
|
|
|
|
sourceEnd, targetEnd - respectively pointers to the ends of the
|
|
|
|
two buffers, for overflow checking only.
|
|
|
|
|
|
|
|
These conversion functions take a ConversionFlags argument. When this
|
|
|
|
flag is set to strict, both irregular sequences and isolated surrogates
|
|
|
|
will cause an error. When the flag is set to lenient, both irregular
|
|
|
|
sequences and isolated surrogates are converted.
|
|
|
|
|
|
|
|
Whether the flag is strict or lenient, all illegal sequences will cause
|
|
|
|
an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
|
|
|
|
or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code
|
|
|
|
must check for illegal sequences.
|
|
|
|
|
|
|
|
When the flag is set to lenient, characters over 0x10FFFF are converted
|
|
|
|
to the replacement character; otherwise (when the flag is set to strict)
|
|
|
|
they constitute an error.
|
|
|
|
|
|
|
|
Output parameters:
|
|
|
|
The value "sourceIllegal" is returned from some routines if the input
|
|
|
|
sequence is malformed. When "sourceIllegal" is returned, the source
|
|
|
|
value will point to the illegal value that caused the problem. E.g.,
|
|
|
|
in UTF-8 when a sequence is malformed, it points to the start of the
|
2018-03-22 17:01:38 +01:00
|
|
|
malformed sequence.
|
2008-11-13 07:39:08 +01:00
|
|
|
|
|
|
|
Author: Mark E. Davis, 1994.
|
|
|
|
Rev History: Rick McGowan, fixes & updates May 2001.
|
|
|
|
Fixes & updates, Sept 2001.
|
|
|
|
|
|
|
|
------------------------------------------------------------------------ */
|
|
|
|
|
2018-11-12 01:11:26 +01:00
|
|
|
#ifndef CONVERT_UTF_H_INCLUDED
|
|
|
|
#define CONVERT_UTF_H_INCLUDED
|
|
|
|
|
2008-11-14 06:25:01 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2016-07-10 13:31:58 +02:00
|
|
|
#include <cstdint>
|
2018-03-22 17:01:38 +01:00
|
|
|
using UTF32 = std::uint32_t;
|
|
|
|
using UTF16 = std::uint16_t;
|
|
|
|
using UTF8 = std::uint8_t;
|
2008-12-22 10:45:54 +01:00
|
|
|
extern "C" {
|
2016-07-10 13:31:58 +02:00
|
|
|
|
2018-03-22 17:01:38 +01:00
|
|
|
using Boolean = unsigned char; /* 0 or 1 */
|
2008-11-13 07:39:08 +01:00
|
|
|
|
|
|
|
/* Some fundamental constants */
|
2015-08-14 05:52:25 +02:00
|
|
|
#define UNI_REPLACEMENT_CHAR UTF32(0x0000FFFD)
|
|
|
|
#define UNI_MAX_BMP UTF32(0x0000FFFF)
|
|
|
|
#define UNI_MAX_UTF16 UTF32(0x0010FFFF)
|
|
|
|
#define UNI_MAX_UTF32 UTF32(0x7FFFFFFF)
|
|
|
|
#define UNI_MAX_LEGAL_UTF32 UTF32(0x0010FFFF)
|
2008-11-13 07:39:08 +01:00
|
|
|
|
2018-03-22 17:01:38 +01:00
|
|
|
enum ConversionResult {
|
|
|
|
conversionOK, /* conversion successful */
|
|
|
|
sourceExhausted, /* partial character in source, but hit end */
|
|
|
|
targetExhausted, /* insuff. room in target for conversion */
|
|
|
|
sourceIllegal /* source sequence is illegal/malformed */
|
|
|
|
};
|
2008-11-13 07:39:08 +01:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
strictConversion = 0,
|
|
|
|
lenientConversion
|
|
|
|
} ConversionFlags;
|
|
|
|
|
2014-05-03 08:31:42 +02:00
|
|
|
TORRENT_EXTRA_EXPORT ConversionResult ConvertUTF8toUTF16 (
|
2018-03-22 17:01:38 +01:00
|
|
|
const UTF8** sourceStart, const UTF8* sourceEnd,
|
|
|
|
UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
|
2008-11-13 07:39:08 +01:00
|
|
|
|
2014-05-03 08:31:42 +02:00
|
|
|
TORRENT_EXTRA_EXPORT ConversionResult ConvertUTF16toUTF8 (
|
2018-03-22 17:01:38 +01:00
|
|
|
const UTF16** sourceStart, const UTF16* sourceEnd,
|
|
|
|
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
|
|
|
|
|
2014-05-03 08:31:42 +02:00
|
|
|
TORRENT_EXTRA_EXPORT ConversionResult ConvertUTF8toUTF32 (
|
2018-03-22 17:01:38 +01:00
|
|
|
const UTF8** sourceStart, const UTF8* sourceEnd,
|
|
|
|
UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
|
2008-11-13 07:39:08 +01:00
|
|
|
|
2014-05-03 08:31:42 +02:00
|
|
|
TORRENT_EXTRA_EXPORT ConversionResult ConvertUTF32toUTF8 (
|
2018-03-22 17:01:38 +01:00
|
|
|
const UTF32** sourceStart, const UTF32* sourceEnd,
|
|
|
|
UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
|
|
|
|
|
2014-05-03 08:31:42 +02:00
|
|
|
TORRENT_EXTRA_EXPORT ConversionResult ConvertUTF16toUTF32 (
|
2018-03-22 17:01:38 +01:00
|
|
|
const UTF16** sourceStart, const UTF16* sourceEnd,
|
|
|
|
UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
|
2008-11-13 07:39:08 +01:00
|
|
|
|
2014-05-03 08:31:42 +02:00
|
|
|
TORRENT_EXTRA_EXPORT ConversionResult ConvertUTF32toUTF16 (
|
2018-03-22 17:01:38 +01:00
|
|
|
const UTF32** sourceStart, const UTF32* sourceEnd,
|
|
|
|
UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
|
2008-11-13 07:39:08 +01:00
|
|
|
|
2014-05-03 08:31:42 +02:00
|
|
|
TORRENT_EXTRA_EXPORT Boolean isLegalUTF8Sequence(const UTF8 *source,
|
2018-03-22 17:01:38 +01:00
|
|
|
const UTF8 *sourceEnd);
|
2008-11-13 07:39:08 +01:00
|
|
|
|
2017-05-15 02:58:51 +02:00
|
|
|
TORRENT_EXTRA_EXPORT Boolean isLegalUTF8(const UTF8 *source, int length);
|
|
|
|
|
|
|
|
extern const char trailingBytesForUTF8[256];
|
|
|
|
extern const UTF32 offsetsFromUTF8[6];
|
|
|
|
|
2008-12-22 10:45:54 +01:00
|
|
|
}
|
2008-11-13 07:39:08 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
2018-11-12 01:11:26 +01:00
|
|
|
|
|
|
|
#endif // CONVERT_UTF_H_INCLUDED
|