2
* PROGRAM: Firebird International support
4
* DESCRIPTION: Codeset conversion for ICU character sets.
6
* The contents of this file are subject to the Initial
7
* Developer's Public License Version 1.0 (the "License");
8
* you may not use this file except in compliance with the
9
* License. You may obtain a copy of the License at
10
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
12
* Software distributed under the License is distributed AS IS,
13
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
14
* See the License for the specific language governing rights
15
* and limitations under the License.
17
* The Original Code was created by Adriano dos Santos Fernandes
18
* for the Firebird Open Source RDBMS project.
20
* Copyright (c) 2004 Adriano dos Santos Fernandes <adrianosf@uol.com.br>
21
* and all contributors signed below.
23
* All Rights Reserved.
24
* Contributor(s): ______________________________________.
28
#include "../intl/ldcommon.h"
31
#include "unicode/ucnv.h"
43
static UConverter* create_converter(csconvert* cv, UErrorCode* status)
45
UConverter* conv = ucnv_open(cv->csconvert_impl->cs->charset_name, status);
46
const void* oldContext;
48
UConverterFromUCallback oldFromAction;
49
ucnv_setFromUCallBack(
51
UCNV_FROM_U_CALLBACK_STOP,
57
UConverterToUCallback oldToAction;
60
UCNV_TO_U_CALLBACK_STOP,
66
fb_assert(U_SUCCESS(*status));
72
static void convert_destroy(csconvert* cv)
74
delete cv->csconvert_impl;
78
static ULONG unicode_to_icu(csconvert* cv,
90
return srcLen / sizeof(UChar) * cv->csconvert_impl->cs->charset_max_bytes_per_char;
92
UErrorCode status = U_ZERO_ERROR;
93
UConverter* conv = create_converter(cv, &status);
95
ULONG len = ucnv_fromUChars(conv, reinterpret_cast<char*>(dst), dstLen,
96
Firebird::Aligner<UChar>(src, srcLen), srcLen / sizeof(UChar), &status);
98
if (!U_SUCCESS(status))
100
len = INTL_BAD_STR_LENGTH;
102
if (status == U_INVALID_CHAR_FOUND)
103
*errCode = CS_CONVERT_ERROR;
104
else if (status == U_TRUNCATED_CHAR_FOUND)
105
*errCode = CS_TRUNCATION_ERROR;
109
*errCode = CS_CONVERT_ERROR;
119
static ULONG icu_to_unicode(csconvert* cv,
131
return srcLen / cv->csconvert_impl->cs->charset_min_bytes_per_char * sizeof(UChar);
133
UErrorCode status = U_ZERO_ERROR;
134
UConverter* conv = create_converter(cv, &status);
136
ULONG len = ucnv_toUChars(conv, Firebird::OutAligner<UChar>(dst, dstLen), dstLen / sizeof(UChar),
137
reinterpret_cast<const char*>(src), srcLen, &status);
139
if (!U_SUCCESS(status))
141
len = INTL_BAD_STR_LENGTH;
143
if (status == U_INVALID_CHAR_FOUND)
144
*errCode = CS_BAD_INPUT;
145
else if (status == U_TRUNCATED_CHAR_FOUND)
146
*errCode = CS_TRUNCATION_ERROR;
150
*errCode = CS_BAD_INPUT;
154
len *= sizeof(UChar);
162
void CVICU_convert_init(charset* cs)
164
cs->charset_to_unicode.csconvert_version = CSCONVERT_VERSION_1;
165
cs->charset_to_unicode.csconvert_name = "ICU->UNICODE";
166
cs->charset_to_unicode.csconvert_fn_convert = icu_to_unicode;
167
cs->charset_to_unicode.csconvert_fn_destroy = convert_destroy;
168
cs->charset_to_unicode.csconvert_impl = new CsConvertImpl();
169
cs->charset_to_unicode.csconvert_impl->cs = cs;
171
cs->charset_from_unicode.csconvert_version = CSCONVERT_VERSION_1;
172
cs->charset_from_unicode.csconvert_name = "UNICODE->ICU";
173
cs->charset_from_unicode.csconvert_fn_convert = unicode_to_icu;
174
cs->charset_from_unicode.csconvert_fn_destroy = convert_destroy;
175
cs->charset_from_unicode.csconvert_impl = new CsConvertImpl();
176
cs->charset_from_unicode.csconvert_impl->cs = cs;