diff --git a/src/common/classes/Aligner.h b/src/common/classes/Aligner.h new file mode 100644 index 00000000000..3a0427220cb --- /dev/null +++ b/src/common/classes/Aligner.h @@ -0,0 +1,115 @@ +/* + * PROGRAM: JRD Access Method + * MODULE: Aligner.h + * DESCRIPTION: Aligner, OutAligner - templates to help + * with alignment on RISC machines. + * Should be used ONLY as temporary on-stack buffers! + * + * The contents of this file are subject to the Initial + * Developer's Public License Version 1.0 (the "License"); + * you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl. + * + * Software distributed under the License is distributed AS IS, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. + * See the License for the specific language governing rights + * and limitations under the License. + * + * The Original Code was created by Alexander Peshkoff + * for the Firebird Open Source RDBMS project. + * + * Copyright (c) 2007 Alexander Peshkoff + * and all contributors signed below. + * + * All Rights Reserved. + * Contributor(s): ______________________________________. + */ + +#ifndef CLASSES_ALIGN_H +#define CLASSES_ALIGN_H + +#include "../common/classes/array.h" + +namespace Firebird { + +// Aligns output parameter (i.e. transfers data in destructor). +template +class OutAligner { +private: + UCHAR* userBuffer; +#ifdef RISC_ALIGNMENT + Firebird::HalfStaticArray localBuffer; + ULONG bSize; + C* bPointer; +#endif + +public: + OutAligner(UCHAR* buf, ULONG len) : userBuffer(buf) +#ifdef RISC_ALIGNMENT + , bSize(len), bPointer(0) +#endif + { + fb_assert(len % sizeof(C) == 0); +#ifdef RISC_ALIGNMENT + if ((IPTR) userBuffer & (sizeof(C) - 1)) + { + bPointer = localBuffer.getBuffer(len / sizeof(C) + (bSize % sizeof(C) ? 1 : 0)); + } +#endif + } + + operator C*() + { +#ifdef RISC_ALIGNMENT + return bPointer ? bPointer : reinterpret_cast(userBuffer); +#else + return reinterpret_cast(userBuffer); +#endif + } + + ~OutAligner() + { +#ifdef RISC_ALIGNMENT + if (bPointer) + { + memcpy(userBuffer, bPointer, bSize); + } +#endif + } +}; + +// Aligns input parameter. +template +class Aligner { +private: +#ifdef RISC_ALIGNMENT + Firebird::HalfStaticArray localBuffer; +#endif + const C* bPointer; + +public: + Aligner(const UCHAR* buf, ULONG len) + { + fb_assert(len % sizeof(C) == 0); +#ifdef RISC_ALIGNMENT + if ((IPTR) buf & (sizeof(C) - 1)) + { + C* tempPointer = localBuffer.getBuffer(len / sizeof(C) + (len % sizeof(C) ? 1 : 0)); + memcpy(tempPointer, buf, len); + bPointer = tempPointer; + } + else +#endif + bPointer = reinterpret_cast(buf); + } + + operator const C*() + { + return bPointer; + } +}; + +} // namespace Firebird + +#endif // CLASSES_ALIGN_H diff --git a/src/intl/cs_big5.cpp b/src/intl/cs_big5.cpp index 5de3a77590d..4d5f2d3a004 100644 --- a/src/intl/cs_big5.cpp +++ b/src/intl/cs_big5.cpp @@ -41,11 +41,11 @@ CHARSET_ENTRY(CS_big_5) csptr->charset_fn_well_formed = CVBIG5_check_big5; CV_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(CVBIG5_big5_to_unicode), + CVBIG5_big5_to_unicode, to_unicode_mapping_array, to_unicode_map); CV_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(CVBIG5_unicode_to_big5), + CVBIG5_unicode_to_big5, from_unicode_mapping_array, from_unicode_map); diff --git a/src/intl/cs_gb2312.cpp b/src/intl/cs_gb2312.cpp index 7c121f9c235..5e2c7f81222 100644 --- a/src/intl/cs_gb2312.cpp +++ b/src/intl/cs_gb2312.cpp @@ -41,11 +41,11 @@ CHARSET_ENTRY(CS_gb_2312) csptr->charset_fn_well_formed = CVGB_check_gb2312; CV_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(CVGB_gb2312_to_unicode), + CVGB_gb2312_to_unicode, to_unicode_mapping_array, to_unicode_map); CV_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(CVGB_unicode_to_gb2312), + CVGB_unicode_to_gb2312, from_unicode_mapping_array, from_unicode_map); diff --git a/src/intl/cs_jis.cpp b/src/intl/cs_jis.cpp index 67e38c991a9..63a0478f407 100644 --- a/src/intl/cs_jis.cpp +++ b/src/intl/cs_jis.cpp @@ -42,10 +42,10 @@ CHARSET_ENTRY(CS_jis_0208_1990) csptr->charset_fn_well_formed = NULL; CV_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(CV_wc_to_wc), + CV_wc_to_wc, to_unicode_mapping_array, to_unicode_map); CV_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(CV_wc_to_wc), + CV_wc_to_wc, from_unicode_mapping_array, from_unicode_map); CHARSET_RETURN; @@ -60,10 +60,8 @@ CHARSET_ENTRY(CS_sjis) csptr->charset_min_bytes_per_char = 1; csptr->charset_space_length = 1; csptr->charset_space_character = (const BYTE*) " "; /* 0x20 */ - csptr->charset_to_unicode.csconvert_fn_convert = - reinterpret_cast(CVJIS_sjis_to_unicode); - csptr->charset_from_unicode.csconvert_fn_convert = - reinterpret_cast(CVJIS_unicode_to_sjis); + csptr->charset_to_unicode.csconvert_fn_convert = CVJIS_sjis_to_unicode; + csptr->charset_from_unicode.csconvert_fn_convert = CVJIS_unicode_to_sjis; CHARSET_RETURN; } @@ -76,9 +74,7 @@ CHARSET_ENTRY(CS_euc_j) csptr->charset_min_bytes_per_char = 1; csptr->charset_space_length = 1; csptr->charset_space_character = (const BYTE*) " "; /* 0x20 */ - csptr->charset_to_unicode.csconvert_fn_convert = - reinterpret_cast(CVJIS_eucj_to_unicode); - csptr->charset_from_unicode.csconvert_fn_convert = - reinterpret_cast(CVJIS_unicode_to_eucj); + csptr->charset_to_unicode.csconvert_fn_convert = CVJIS_eucj_to_unicode; + csptr->charset_from_unicode.csconvert_fn_convert = CVJIS_unicode_to_eucj; CHARSET_RETURN; } diff --git a/src/intl/cs_ksc.cpp b/src/intl/cs_ksc.cpp index 20ab0989cd4..19c5de60441 100644 --- a/src/intl/cs_ksc.cpp +++ b/src/intl/cs_ksc.cpp @@ -42,11 +42,11 @@ CHARSET_ENTRY(CS_ksc_5601) csptr->charset_fn_well_formed = CVKSC_check_ksc; CV_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(CVKSC_ksc_to_unicode), + CVKSC_ksc_to_unicode, to_unicode_mapping_array, to_unicode_map); CV_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(CVKSC_unicode_to_ksc), + CVKSC_unicode_to_ksc, from_unicode_mapping_array, from_unicode_map); diff --git a/src/intl/cs_narrow.cpp b/src/intl/cs_narrow.cpp index 30cc0e63822..928da4baa17 100644 --- a/src/intl/cs_narrow.cpp +++ b/src/intl/cs_narrow.cpp @@ -40,10 +40,10 @@ static void common_8bit_init(charset* csptr, csptr->charset_space_character = (const BYTE*) " "; csptr->charset_fn_well_formed = NULL; CV_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(CV_nc_to_unicode), + CV_nc_to_unicode, to_unicode_tbl, NULL); CV_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(CV_unicode_to_nc), + CV_unicode_to_nc, from_unicode_tbl1, from_unicode_tbl2); } diff --git a/src/intl/cs_unicode_fss.cpp b/src/intl/cs_unicode_fss.cpp index d34a0ca1503..acad8cc2de7 100644 --- a/src/intl/cs_unicode_fss.cpp +++ b/src/intl/cs_unicode_fss.cpp @@ -39,10 +39,10 @@ CHARSET_ENTRY(CS_unicode_fss) csptr->charset_space_character = (const BYTE*) " "; /* 0x20 */ csptr->charset_fn_well_formed = NULL; CV_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(CS_UTFFSS_fss_to_unicode_cc), + CS_UTFFSS_fss_to_unicode_cc, NULL, NULL); CV_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(CS_UTFFSS_unicode_to_fss), + CS_UTFFSS_unicode_to_fss, NULL, NULL); CHARSET_RETURN; } diff --git a/src/intl/cs_unicode_ucs2.cpp b/src/intl/cs_unicode_ucs2.cpp index 77f98c69378..d0ffe05f6ed 100644 --- a/src/intl/cs_unicode_ucs2.cpp +++ b/src/intl/cs_unicode_ucs2.cpp @@ -38,10 +38,10 @@ CHARSET_ENTRY(CS_unicode_ucs2) csptr->charset_space_character = (const BYTE*) & space; /* 0x0020 */ csptr->charset_fn_well_formed = NULL; CV_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(CV_wc_copy), + CV_wc_copy, NULL, NULL); CV_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(CV_wc_copy), + CV_wc_copy, NULL, NULL); CHARSET_RETURN; } diff --git a/src/intl/cv_big5.cpp b/src/intl/cv_big5.cpp index 62149eae121..c894ccbda97 100644 --- a/src/intl/cv_big5.cpp +++ b/src/intl/cv_big5.cpp @@ -31,15 +31,15 @@ ULONG CVBIG5_big5_to_unicode(csconvert* obj, ULONG src_len, const UCHAR* src_ptr, ULONG dest_len, - USHORT *dest_ptr, - USHORT *err_code, - ULONG *err_position) + UCHAR* p_dest_ptr, + USHORT* err_code, + ULONG* err_position) { - fb_assert(src_ptr != NULL || dest_ptr == NULL); + fb_assert(src_ptr != NULL || p_dest_ptr == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == reinterpret_cast(CVBIG5_big5_to_unicode)); + fb_assert(obj->csconvert_fn_convert == CVBIG5_big5_to_unicode); fb_assert(obj->csconvert_impl->csconvert_datatable != NULL); fb_assert(obj->csconvert_impl->csconvert_misc != NULL); @@ -47,9 +47,12 @@ ULONG CVBIG5_big5_to_unicode(csconvert* obj, *err_code = 0; /* See if we're only after a length estimate */ - if (dest_ptr == NULL) + if (p_dest_ptr == NULL) return (src_len * sizeof(USHORT)); + Firebird::OutAligner d(p_dest_ptr, dest_len); + USHORT* dest_ptr = d; + USHORT wide; USHORT this_len; const USHORT* const start = dest_ptr; @@ -104,17 +107,17 @@ ULONG CVBIG5_big5_to_unicode(csconvert* obj, ULONG CVBIG5_unicode_to_big5(csconvert* obj, ULONG unicode_len, - const USHORT* unicode_str, + const UCHAR* p_unicode_str, ULONG big5_len, - UCHAR *big5_str, - USHORT *err_code, - ULONG *err_position) + UCHAR* big5_str, + USHORT* err_code, + ULONG* err_position) { - fb_assert(unicode_str != NULL || big5_str == NULL); + fb_assert(p_unicode_str != NULL || big5_str == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == reinterpret_cast(CVBIG5_unicode_to_big5)); + fb_assert(obj->csconvert_fn_convert == CVBIG5_unicode_to_big5); fb_assert(obj->csconvert_impl->csconvert_datatable != NULL); fb_assert(obj->csconvert_impl->csconvert_misc != NULL); @@ -125,6 +128,9 @@ ULONG CVBIG5_unicode_to_big5(csconvert* obj, if (big5_str == NULL) return (unicode_len); /* worst case - all han character input */ + Firebird::Aligner s(p_unicode_str, unicode_len); + const USHORT* unicode_str = s; + const UCHAR* const start = big5_str; while ((big5_len) && (unicode_len > 1)) { /* Convert from UNICODE to BIG5 code */ diff --git a/src/intl/cv_big5.h b/src/intl/cv_big5.h index fd9780d5106..6db06636054 100644 --- a/src/intl/cv_big5.h +++ b/src/intl/cv_big5.h @@ -30,9 +30,9 @@ (UCHAR)((uc)&0xff)<=0xfe) /* BIG-5 2nd-byte */ ULONG CVBIG5_big5_to_unicode(csconvert* obj, ULONG src_len, const UCHAR* src_ptr, - ULONG dest_len, USHORT *dest_ptr, + ULONG dest_len, UCHAR *dest_ptr, USHORT *err_code, ULONG *err_position); -ULONG CVBIG5_unicode_to_big5(csconvert* obj, ULONG unicode_len, const USHORT* unicode_str, +ULONG CVBIG5_unicode_to_big5(csconvert* obj, ULONG unicode_len, const UCHAR* unicode_str, ULONG big5_len, UCHAR *big5_str, USHORT *err_code, ULONG *err_position); USHORT CVBIG5_check_big5(charset* cs, ULONG big5_len, const UCHAR* big5_str, ULONG* offending_position); diff --git a/src/intl/cv_gb2312.cpp b/src/intl/cv_gb2312.cpp index 3d2f916f643..712ace7e726 100644 --- a/src/intl/cv_gb2312.cpp +++ b/src/intl/cv_gb2312.cpp @@ -31,15 +31,15 @@ ULONG CVGB_gb2312_to_unicode(csconvert* obj, ULONG src_len, const UCHAR* src_ptr, ULONG dest_len, - USHORT *dest_ptr, - USHORT *err_code, - ULONG *err_position) + UCHAR* p_dest_ptr, + USHORT* err_code, + ULONG* err_position) { - fb_assert(src_ptr != NULL || dest_ptr == NULL); + fb_assert(src_ptr != NULL || p_dest_ptr == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == reinterpret_cast(CVGB_gb2312_to_unicode)); + fb_assert(obj->csconvert_fn_convert == CVGB_gb2312_to_unicode); fb_assert(obj->csconvert_impl->csconvert_datatable != NULL); fb_assert(obj->csconvert_impl->csconvert_misc != NULL); @@ -47,9 +47,12 @@ ULONG CVGB_gb2312_to_unicode(csconvert* obj, *err_code = 0; /* See if we're only after a length estimate */ - if (dest_ptr == NULL) + if (p_dest_ptr == NULL) return (src_len * sizeof(USHORT)); + Firebird::OutAligner d(p_dest_ptr, dest_len); + USHORT* dest_ptr = d; + USHORT wide; USHORT this_len; const USHORT* const start = dest_ptr; @@ -105,17 +108,17 @@ ULONG CVGB_gb2312_to_unicode(csconvert* obj, ULONG CVGB_unicode_to_gb2312(csconvert* obj, ULONG unicode_len, - const USHORT* unicode_str, + const UCHAR* p_unicode_str, ULONG gb_len, - UCHAR *gb_str, - USHORT *err_code, - ULONG *err_position) + UCHAR* gb_str, + USHORT* err_code, + ULONG* err_position) { - fb_assert(unicode_str != NULL || gb_str == NULL); + fb_assert(p_unicode_str != NULL || gb_str == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == reinterpret_cast(CVGB_unicode_to_gb2312)); + fb_assert(obj->csconvert_fn_convert == CVGB_unicode_to_gb2312); fb_assert(obj->csconvert_impl->csconvert_datatable != NULL); fb_assert(obj->csconvert_impl->csconvert_misc != NULL); @@ -126,6 +129,9 @@ ULONG CVGB_unicode_to_gb2312(csconvert* obj, if (gb_str == NULL) return (unicode_len); /* worst case - all han character input */ + Firebird::Aligner s(p_unicode_str, unicode_len); + const USHORT* unicode_str = s; + const UCHAR* const start = gb_str; while ((gb_len) && (unicode_len > 1)) { /* Convert from UNICODE to GB2312 code */ diff --git a/src/intl/cv_gb2312.h b/src/intl/cv_gb2312.h index 35e16ddc536..ac92a34779a 100644 --- a/src/intl/cv_gb2312.h +++ b/src/intl/cv_gb2312.h @@ -28,11 +28,11 @@ (UCHAR)((uc)&0xff)<=0xfe) /* GB2312 2nd-byte */ ULONG CVGB_gb2312_to_unicode(csconvert* obj, ULONG src_len, const UCHAR* src_ptr, - ULONG dest_len, USHORT *dest_ptr, + ULONG dest_len, UCHAR *dest_ptr, USHORT *err_code, ULONG *err_position); -ULONG CVGB_unicode_to_gb2312(csconvert* obj, ULONG unicode_len, const USHORT* unicode_str, - ULONG gb_len,UCHAR *gb_str, +ULONG CVGB_unicode_to_gb2312(csconvert* obj, ULONG unicode_len, const UCHAR* unicode_str, + ULONG gb_len, UCHAR *gb_str, USHORT *err_code, ULONG *err_position); INTL_BOOL CVGB_check_gb2312(charset* cs, ULONG gb_len, const UCHAR* gb_str, ULONG* offending_position); diff --git a/src/intl/cv_jis.cpp b/src/intl/cv_jis.cpp index 3d7f29b8b78..856f32a68e9 100644 --- a/src/intl/cv_jis.cpp +++ b/src/intl/cv_jis.cpp @@ -33,15 +33,15 @@ ULONG CVJIS_eucj_to_unicode(csconvert* obj, ULONG src_len, const UCHAR* src_ptr, ULONG dest_len, - USHORT *dest_ptr, + UCHAR *p_dest_ptr, USHORT *err_code, ULONG *err_position) { - fb_assert(src_ptr != NULL || dest_ptr == NULL); + fb_assert(src_ptr != NULL || p_dest_ptr == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == reinterpret_cast(CVJIS_eucj_to_unicode)); + fb_assert(obj->csconvert_fn_convert == CVJIS_eucj_to_unicode); fb_assert(obj->csconvert_impl->csconvert_datatable != NULL); fb_assert(obj->csconvert_impl->csconvert_misc != NULL); @@ -49,9 +49,12 @@ ULONG CVJIS_eucj_to_unicode(csconvert* obj, *err_code = 0; /* See if we're only after a length estimate */ - if (dest_ptr == NULL) + if (p_dest_ptr == NULL) return (src_len); + Firebird::OutAligner d(p_dest_ptr, dest_len); + USHORT* dest_ptr = d; + USHORT ch; USHORT wide; USHORT this_len; @@ -128,15 +131,15 @@ ULONG CVJIS_sjis_to_unicode(csconvert* obj, ULONG sjis_len, const UCHAR* sjis_str, ULONG dest_len, - USHORT *dest_ptr, + UCHAR *p_dest_ptr, USHORT *err_code, ULONG *err_position) { - fb_assert(sjis_str != NULL || dest_ptr == NULL); + fb_assert(sjis_str != NULL || p_dest_ptr == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == reinterpret_cast(CVJIS_sjis_to_unicode)); + fb_assert(obj->csconvert_fn_convert == CVJIS_sjis_to_unicode); fb_assert(obj->csconvert_impl->csconvert_datatable != NULL); fb_assert(obj->csconvert_impl->csconvert_misc != NULL); @@ -144,9 +147,12 @@ ULONG CVJIS_sjis_to_unicode(csconvert* obj, *err_code = 0; /* See if we're only after a length estimate */ - if (dest_ptr == NULL) + if (p_dest_ptr == NULL) return (sjis_len * 2); /* worst case - all ascii input */ + Firebird::OutAligner d(p_dest_ptr, dest_len); + USHORT* dest_ptr = d; + USHORT table; USHORT this_len; USHORT wide; @@ -381,17 +387,17 @@ I hope this helps in the discussion. ULONG CVJIS_unicode_to_sjis(csconvert* obj, ULONG unicode_len, - const USHORT* unicode_str, + const UCHAR* p_unicode_str, ULONG sjis_len, UCHAR* sjis_str, USHORT *err_code, ULONG *err_position) { - fb_assert(unicode_str != NULL || sjis_str == NULL); + fb_assert(p_unicode_str != NULL || sjis_str == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == reinterpret_cast(CVJIS_unicode_to_sjis)); + fb_assert(obj->csconvert_fn_convert == CVJIS_unicode_to_sjis); fb_assert(obj->csconvert_impl->csconvert_datatable != NULL); fb_assert(obj->csconvert_impl->csconvert_misc != NULL); @@ -402,6 +408,9 @@ ULONG CVJIS_unicode_to_sjis(csconvert* obj, if (sjis_str == NULL) return (unicode_len); /* worst case - all han character input */ + Firebird::Aligner s(p_unicode_str, unicode_len); + const USHORT* unicode_str = s; + const UCHAR* const start = sjis_str; while ((sjis_len) && (unicode_len > 1)) { /* Step 1: Convert from UNICODE to JIS code */ @@ -458,15 +467,15 @@ ULONG CVJIS_unicode_to_sjis(csconvert* obj, } -ULONG CVJIS_unicode_to_eucj(csconvert* obj, ULONG unicode_len, const USHORT* unicode_str, +ULONG CVJIS_unicode_to_eucj(csconvert* obj, ULONG unicode_len, const UCHAR* p_unicode_str, ULONG eucj_len, UCHAR *eucj_str, USHORT *err_code, ULONG *err_position) { - fb_assert(unicode_str != NULL || eucj_str == NULL); + fb_assert(p_unicode_str != NULL || eucj_str == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == reinterpret_cast(CVJIS_unicode_to_eucj)); + fb_assert(obj->csconvert_fn_convert == CVJIS_unicode_to_eucj); fb_assert(obj->csconvert_impl->csconvert_datatable != NULL); fb_assert(obj->csconvert_impl->csconvert_misc != NULL); @@ -477,6 +486,9 @@ ULONG CVJIS_unicode_to_eucj(csconvert* obj, ULONG unicode_len, const USHORT* uni if (eucj_str == NULL) return (unicode_len); /* worst case - all han character input */ + Firebird::Aligner s(p_unicode_str, unicode_len); + const USHORT* unicode_str = s; + const UCHAR* const start = eucj_str; while ((eucj_len) && (unicode_len > 1)) { /* Step 1: Convert from UNICODE to JIS code */ @@ -770,11 +782,11 @@ CONVERT_ENTRY(CS_SJIS, CS_EUCJ, CVJIS_sjis_x_eucj) { if (dest_cs == CS_EUCJ) CV_convert_init(csptr, dest_cs, source_cs, - reinterpret_cast(CVJIS_sjis2euc), + CVJIS_sjis2euc, NULL, NULL); else CV_convert_init(csptr, dest_cs, source_cs, - reinterpret_cast(CVJIS_euc2sjis), + CVJIS_euc2sjis, NULL, NULL); CONVERT_RETURN; diff --git a/src/intl/cv_jis.h b/src/intl/cv_jis.h index 4f7fbfbc5ce..6c6149146e1 100644 --- a/src/intl/cv_jis.h +++ b/src/intl/cv_jis.h @@ -22,18 +22,18 @@ */ ULONG CVJIS_sjis_to_unicode(csconvert* obj, ULONG sjis_len, const UCHAR* sjis_str, - ULONG dest_len, USHORT *dest_ptr, + ULONG dest_len, UCHAR *dest_ptr, USHORT *err_code, ULONG *err_position); -ULONG CVJIS_unicode_to_sjis(csconvert* obj, ULONG unicode_len, const USHORT* unicode_str, +ULONG CVJIS_unicode_to_sjis(csconvert* obj, ULONG unicode_len, const UCHAR* unicode_str, ULONG sjis_len, UCHAR *sjis_str, USHORT *err_code, ULONG *err_position); ULONG CVJIS_eucj_to_unicode(csconvert* obj, ULONG src_len, const UCHAR* src_ptr, - ULONG dest_len, USHORT *dest_ptr, + ULONG dest_len, UCHAR *dest_ptr, USHORT *err_code, ULONG *err_position); -ULONG CVJIS_unicode_to_eucj(csconvert* obj, ULONG unicode_len, const USHORT* unicode_str, +ULONG CVJIS_unicode_to_eucj(csconvert* obj, ULONG unicode_len, const UCHAR* unicode_str, ULONG eucj_len, UCHAR *eucj_str, USHORT *err_code, ULONG *err_position); diff --git a/src/intl/cv_ksc.cpp b/src/intl/cv_ksc.cpp index 7eb0739c607..e36d789b499 100644 --- a/src/intl/cv_ksc.cpp +++ b/src/intl/cv_ksc.cpp @@ -37,24 +37,27 @@ ULONG CVKSC_ksc_to_unicode(csconvert* obj, ULONG ksc_len, const UCHAR* ksc_str, ULONG dest_len, - USHORT *dest_ptr, - USHORT *err_code, - ULONG *err_position) + UCHAR* p_dest_ptr, + USHORT* err_code, + ULONG* err_position) { - fb_assert(ksc_str != NULL || dest_ptr == NULL); + fb_assert(ksc_str != NULL || p_dest_ptr == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == reinterpret_cast(CVKSC_ksc_to_unicode)); + fb_assert(obj->csconvert_fn_convert == CVKSC_ksc_to_unicode); fb_assert(obj->csconvert_impl->csconvert_datatable != NULL); fb_assert(obj->csconvert_impl->csconvert_misc != NULL); const ULONG src_start = ksc_len; *err_code = 0; - if (dest_ptr == NULL) + if (p_dest_ptr == NULL) return (ksc_len * sizeof(USHORT)); + Firebird::OutAligner d(p_dest_ptr, dest_len); + USHORT* dest_ptr = d; + USHORT this_len; USHORT wide; const USHORT* const start = dest_ptr; @@ -107,17 +110,17 @@ ULONG CVKSC_ksc_to_unicode(csconvert* obj, ULONG CVKSC_unicode_to_ksc(csconvert* obj, ULONG unicode_len, - const USHORT* unicode_str, + const UCHAR* p_unicode_str, ULONG ksc_len, - UCHAR *ksc_str, - USHORT *err_code, - ULONG *err_position) + UCHAR* ksc_str, + USHORT* err_code, + ULONG* err_position) { - fb_assert(unicode_str != NULL || ksc_str == NULL); + fb_assert(p_unicode_str != NULL || ksc_str == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == reinterpret_cast(CVKSC_unicode_to_ksc)); + fb_assert(obj->csconvert_fn_convert == CVKSC_unicode_to_ksc); fb_assert(obj->csconvert_impl->csconvert_datatable != NULL); fb_assert(obj->csconvert_impl->csconvert_misc != NULL); @@ -127,6 +130,9 @@ ULONG CVKSC_unicode_to_ksc(csconvert* obj, if (ksc_str == NULL) return (unicode_len); + Firebird::Aligner s(p_unicode_str, unicode_len); + const USHORT* unicode_str = s; + const UCHAR* const start = ksc_str; while (ksc_len && unicode_len > 1) { const USHORT wide = *unicode_str++; diff --git a/src/intl/cv_ksc.h b/src/intl/cv_ksc.h index 8efb2d6f31a..8a010a5788e 100644 --- a/src/intl/cv_ksc.h +++ b/src/intl/cv_ksc.h @@ -36,10 +36,10 @@ #define SPE_HAN(b1, b2) (((b1) == 0xa4) && (((b2) == 0xa2) || ((b2) == 0xa4) || ((b2) == 0xa7) || ((b2) == 0xa8) || ((b2) == 0xa9) || ((b2) == 0xb1) || ((b2) == 0xb2) || ((b2) == 0xb3) || ((b2) == 0xb5) || ((b2) == 0xb6) || ((b2) == 0xb7) || ((b2) == 0xb8) || ((b2) == 0xb9) || ((b2) == 0xba) || ((b2) == 0xbb) || ((b2) == 0xbc) || ((b2) == 0xbd) || ((b2) == 0xbe))) ULONG CVKSC_ksc_to_unicode(csconvert* obj, ULONG ksc_len, const UCHAR* ksc_str, - ULONG dest_len, USHORT *dest_ptr, + ULONG dest_len, UCHAR *dest_ptr, USHORT *err_code, ULONG *err_position); -ULONG CVKSC_unicode_to_ksc(csconvert* obj, ULONG unicode_len, const USHORT* unicode_str, +ULONG CVKSC_unicode_to_ksc(csconvert* obj, ULONG unicode_len, const UCHAR* unicode_str, ULONG ksc_len, UCHAR *ksc_str, USHORT *err_code, ULONG *err_position); diff --git a/src/intl/cv_narrow.cpp b/src/intl/cv_narrow.cpp index 268d69bde3f..094fe0957f1 100644 --- a/src/intl/cv_narrow.cpp +++ b/src/intl/cv_narrow.cpp @@ -96,17 +96,17 @@ ULONG CV_unicode_to_nc(csconvert* obj, ULONG CV_wc_to_wc(csconvert* obj, ULONG src_len, - const USHORT* src_ptr, + const UCHAR* p_src_ptr, ULONG dest_len, - USHORT* dest_ptr, + UCHAR* p_dest_ptr, USHORT *err_code, ULONG *err_position) { - fb_assert(src_ptr != NULL || dest_ptr == NULL); + fb_assert(p_src_ptr != NULL || p_dest_ptr == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == reinterpret_cast(CV_wc_to_wc)); + fb_assert(obj->csconvert_fn_convert == CV_wc_to_wc); fb_assert(obj->csconvert_impl->csconvert_datatable != NULL); fb_assert(obj->csconvert_impl->csconvert_misc != NULL); @@ -114,9 +114,14 @@ ULONG CV_wc_to_wc(csconvert* obj, *err_code = 0; /* See if we're only after a length estimate */ - if (dest_ptr == NULL) + if (p_dest_ptr == NULL) return (src_len); + Firebird::Aligner s(p_src_ptr, src_len); + const USHORT* src_ptr = s; + Firebird::OutAligner d(p_dest_ptr, dest_len); + USHORT* dest_ptr = d; + const USHORT* const start = dest_ptr; while ((src_len > 1) && (dest_len > 1)) { const UNICODE uni = *((const UNICODE*) src_ptr); @@ -157,7 +162,7 @@ ULONG CV_nc_to_unicode(csconvert* obj, fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == reinterpret_cast(CV_nc_to_unicode)); + fb_assert(obj->csconvert_fn_convert == CV_nc_to_unicode); fb_assert(obj->csconvert_impl->csconvert_datatable != NULL); fb_assert(sizeof(UNICODE) == 2); @@ -282,11 +287,11 @@ CONVERT_ENTRY(CS_ISO8859_1, CS_DOS_865, CV_dos_865_x_iso8859_1) #include "../intl/conversions/tx865_lat1.h" if (dest_cs == CS_ISO8859_1) CV_convert_init(csptr, dest_cs, source_cs, - reinterpret_cast(eight_bit_convert), + eight_bit_convert, cvt_865_to_iso88591, NULL); else CV_convert_init(csptr, dest_cs, source_cs, - reinterpret_cast(eight_bit_convert), + eight_bit_convert, cvt_iso88591_to_865, NULL); CONVERT_RETURN; } @@ -298,11 +303,11 @@ CONVERT_ENTRY(CS_ISO8859_1, CS_DOS_437, CV_dos_437_x_dos_865) #include "../intl/conversions/tx437_865.h" if (dest_cs == CS_DOS_865) CV_convert_init(csptr, dest_cs, source_cs, - reinterpret_cast(eight_bit_convert), + eight_bit_convert, cvt_437_to_865, NULL); else CV_convert_init(csptr, dest_cs, source_cs, - reinterpret_cast(eight_bit_convert), + eight_bit_convert, cvt_865_to_437, NULL); CONVERT_RETURN; @@ -315,11 +320,11 @@ CONVERT_ENTRY(CS_ISO8859_1, CS_DOS_437, CV_dos_437_x_iso8859_1) #include "../intl/conversions/tx437_lat1.h" if (dest_cs == CS_ISO8859_1) CV_convert_init(csptr, dest_cs, source_cs, - reinterpret_cast(eight_bit_convert), + eight_bit_convert, cvt_437_to_iso88591, NULL); else CV_convert_init(csptr, dest_cs, source_cs, - reinterpret_cast(eight_bit_convert), + eight_bit_convert, cvt_iso88591_to_437, NULL); CONVERT_RETURN; diff --git a/src/intl/cv_narrow.h b/src/intl/cv_narrow.h index 7d817f6529f..bb7957acf41 100644 --- a/src/intl/cv_narrow.h +++ b/src/intl/cv_narrow.h @@ -30,8 +30,8 @@ struct CsConvertImpl void CV_convert_init(csconvert* csptr, pfn_INTL_convert cvt_fn, const void *datatable, const void *datatable2); -ULONG CV_wc_to_wc(csconvert* obj, ULONG src_len, const USHORT* src_ptr, - ULONG dest_len, USHORT *dest_ptr, +ULONG CV_wc_to_wc(csconvert* obj, ULONG src_len, const UCHAR* src_ptr, + ULONG dest_len, UCHAR *dest_ptr, USHORT *err_code, ULONG *err_position); ULONG CV_unicode_to_nc(csconvert* obj, ULONG src_len, const BYTE* src_ptr, diff --git a/src/intl/cv_unicode_fss.cpp b/src/intl/cv_unicode_fss.cpp index bd018318850..96f3b0ef3cf 100644 --- a/src/intl/cv_unicode_fss.cpp +++ b/src/intl/cv_unicode_fss.cpp @@ -255,7 +255,7 @@ ULONG CS_UTFFSS_fss_to_unicode_cc(csconvert* obj, ULONG src_len, const UCHAR* src_ptr, ULONG dest_len, - UNICODE *dest_ptr, + UCHAR *dest_ptr, USHORT *err_code, ULONG *err_position) { @@ -263,26 +263,26 @@ ULONG CS_UTFFSS_fss_to_unicode_cc(csconvert* obj, fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == - reinterpret_cast(CS_UTFFSS_fss_to_unicode_cc)); + fb_assert(obj->csconvert_fn_convert == CS_UTFFSS_fss_to_unicode_cc); - return fss_to_unicode(src_len, src_ptr, dest_len, dest_ptr, err_code, err_position); + return fss_to_unicode(src_len, src_ptr, + dest_len, Firebird::OutAligner(dest_ptr, dest_len), err_code, err_position); } ULONG CS_UTFFSS_unicode_to_fss(csconvert* obj, ULONG unicode_len, - const UNICODE* unicode_str, + const UCHAR* p_unicode_str, ULONG fss_len, - UCHAR *fss_str, - USHORT *err_code, - ULONG *err_position) + UCHAR* fss_str, + USHORT* err_code, + ULONG* err_position) { - fb_assert(unicode_str != NULL || fss_str == NULL); + fb_assert(p_unicode_str != NULL || fss_str == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == reinterpret_cast(CS_UTFFSS_unicode_to_fss)); + fb_assert(obj->csconvert_fn_convert == CS_UTFFSS_unicode_to_fss); const ULONG src_start = unicode_len; *err_code = 0; @@ -291,6 +291,9 @@ ULONG CS_UTFFSS_unicode_to_fss(csconvert* obj, if (fss_str == NULL) return ((ULONG) (unicode_len + 1) / 2 * 3); /* worst case - all han character input */ + Firebird::Aligner s(p_unicode_str, unicode_len); + const UNICODE* unicode_str = s; + UCHAR tmp_buffer[6]; const UCHAR* const start = fss_str; while ((fss_len) && (unicode_len >= sizeof(*unicode_str))) { diff --git a/src/intl/cv_unicode_fss.h b/src/intl/cv_unicode_fss.h index 3c92ef41b8c..a1c27fae927 100644 --- a/src/intl/cv_unicode_fss.h +++ b/src/intl/cv_unicode_fss.h @@ -24,10 +24,10 @@ /* Note: all routines have cousins in jrd/intl.cpp */ ULONG CS_UTFFSS_fss_to_unicode_cc(csconvert* obj, ULONG src_len, const UCHAR* src_ptr, - ULONG dest_len, UNICODE *dest_ptr, + ULONG dest_len, UCHAR *dest_ptr, USHORT *err_code, ULONG *err_position); -ULONG CS_UTFFSS_unicode_to_fss(csconvert* obj, ULONG unicode_len, const UNICODE* unicode_str, +ULONG CS_UTFFSS_unicode_to_fss(csconvert* obj, ULONG unicode_len, const UCHAR* unicode_str, ULONG fss_len, UCHAR *fss_str, USHORT *err_code, ULONG *err_position); diff --git a/src/intl/lc_narrow.cpp b/src/intl/lc_narrow.cpp index e3f675345f8..7b4fbd55d71 100644 --- a/src/intl/lc_narrow.cpp +++ b/src/intl/lc_narrow.cpp @@ -688,18 +688,15 @@ ULONG LC_NARROW_canonical(TEXTTYPE obj, ULONG srcLen, const UCHAR* src, ULONG ds if ((obj->texttype_impl->texttype_flags & (TEXTTYPE_secondary_insensitive | TEXTTYPE_tertiary_insensitive)) == 0) { - *reinterpret_cast(dst) = (coll->Primary << 8) | (coll->Secondary << 4) | coll->Tertiary; - dst += sizeof(USHORT); + put(dst, (USHORT) ((coll->Primary << 8) | (coll->Secondary << 4) | coll->Tertiary)); } else if ((obj->texttype_impl->texttype_flags & TEXTTYPE_secondary_insensitive) == 0) { - *reinterpret_cast(dst) = (coll->Primary << 8) | coll->Secondary; - dst += sizeof(USHORT); + put(dst, (USHORT) ((coll->Primary << 8) | coll->Secondary)); } else if ((obj->texttype_impl->texttype_flags & TEXTTYPE_tertiary_insensitive) == 0) { - *reinterpret_cast(dst) = (coll->Primary << 8) | coll->Tertiary; - dst += sizeof(USHORT); + put(dst, (USHORT) ((coll->Primary << 8) | coll->Tertiary)); } else *dst++ = coll->Primary; diff --git a/src/intl/ldcommon.h b/src/intl/ldcommon.h index 866f531d26c..a2ad69e7e0c 100644 --- a/src/intl/ldcommon.h +++ b/src/intl/ldcommon.h @@ -33,6 +33,7 @@ #include "../intl/charsets.h" #include "../intl/country_codes.h" #include "../intl/ld.h" +#include "../common/classes/Aligner.h" #undef DEBUG @@ -81,5 +82,13 @@ typedef USHORT UNICODE; void CV_convert_init(csconvert*, pfn_INTL_convert, const void*, const void*); +// Helpers to avoid alignment problems in INTL +template +inline void put(UCHAR*& dest, const C src) +{ + memcpy(dest, &src, sizeof(C)); + dest += sizeof(C); +} + #endif /* INTL_LDCOMMON_H */ diff --git a/src/jrd/common.h b/src/jrd/common.h index f6b2aa44af1..d1776d34979 100644 --- a/src/jrd/common.h +++ b/src/jrd/common.h @@ -136,6 +136,7 @@ #ifdef sparc #define IMPLEMENTATION isc_info_db_impl_linux_sparc /* 65 */ +#define RISC_ALIGNMENT #endif /* sparc */ #define MEMMOVE(from, to, length) memmove ((void *)to, (void *)from, (size_t) length) @@ -457,6 +458,7 @@ static inline int sinixz_sigaction(int sig, const struct sinixz_sigaction *act, //#define DOUBLE_ALIGN 8 #define IMPLEMENTATION isc_info_db_impl_isc_sun4 /* 30 */ +#define RISC_ALIGNMENT #else /* sparc */ #ifdef i386 @@ -512,6 +514,8 @@ static inline int sinixz_sigaction(int sig, const struct sinixz_sigaction *act, #define MOVE_FASTER(from, to, length) memcpy (to, from, (int) (length)) #define MOVE_CLEAR(to, length) memset (to, 0, (int) (length)) +#define RISC_ALIGNMENT + #endif /* hpux */ diff --git a/src/jrd/intl.cpp b/src/jrd/intl.cpp index 7d7a067300c..2925226f534 100644 --- a/src/jrd/intl.cpp +++ b/src/jrd/intl.cpp @@ -471,15 +471,15 @@ class MultiByteCharSet : public CharSet ULONG errPos; // convert to UTF16 - Firebird::HalfStaticArray str; + Firebird::HalfStaticArray str; ULONG unilength = getConvToUnicode().convertLength(srcLen); unilength = getConvToUnicode().convert(srcLen, src, unilength, - reinterpret_cast(str.getBuffer(unilength)), &errCode, &errPos); + str.getBuffer(unilength / sizeof(USHORT) + 1), &errCode, &errPos); // generate substring of UTF16 - Firebird::HalfStaticArray substr; - unilength = UnicodeUtil::utf16Substring(unilength, reinterpret_cast(str.begin()), - unilength, reinterpret_cast(substr.getBuffer(unilength)), startPos, len); + Firebird::HalfStaticArray substr; + unilength = UnicodeUtil::utf16Substring(unilength, str.begin(), + unilength, substr.getBuffer(unilength / sizeof(USHORT) + 1), startPos, len); // convert generated substring to original charset return getConvFromUnicode().convert(unilength, substr.begin(), dstLen, dst, &errCode, &errPos); diff --git a/src/jrd/intl_builtin.cpp b/src/jrd/intl_builtin.cpp index 4864968567a..f98b690a315 100644 --- a/src/jrd/intl_builtin.cpp +++ b/src/jrd/intl_builtin.cpp @@ -29,6 +29,20 @@ struct TextTypeImpl }; +namespace { +template +void padUtf16(const USHORT* text, U& len) +{ + for (; len > 0; len -= sizeof(USHORT)) + { + if (text[len / sizeof(USHORT) - 1] != 32) + break; + } +} +} //namespace + + + static inline bool FAMILY_INTERNAL(TEXTTYPE cache, SSHORT country, const ASCII* POSIX, @@ -203,34 +217,37 @@ static SSHORT internal_fss_mbtowc(TEXTTYPE obj, } #endif -static ULONG internal_fss_to_unicode(TEXTTYPE obj, +static ULONG internal_fss_to_unicode(csconvert* obj, ULONG src_len, const UCHAR* src_ptr, ULONG dest_len, - UNICODE* dest_ptr, + UCHAR* p_dest_ptr, USHORT* err_code, ULONG* err_position) { - fb_assert(src_ptr != NULL || dest_ptr == NULL); + fb_assert(src_ptr != NULL || p_dest_ptr == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); *err_code = 0; -/* See if we're only after a length estimate */ - if (dest_ptr == NULL) + // See if we're only after a length estimate + if (p_dest_ptr == NULL) return (src_len * 2); /* All single byte narrow characters */ + Firebird::OutAligner d(p_dest_ptr, dest_len); + UNICODE* dest_ptr = d; + const UNICODE* const start = dest_ptr; const ULONG src_start = src_len; while ((src_len) && (dest_len >= sizeof(*dest_ptr))) { const fss_size_t res = fss_mbtowc(dest_ptr, src_ptr, src_len); - if (res == -1) { + if (res < 0) { *err_code = CS_BAD_INPUT; break; } - fb_assert(res <= src_len); + fb_assert(static_cast(res) <= src_len); dest_ptr++; dest_len -= sizeof(*dest_ptr); src_ptr += res; @@ -245,7 +262,7 @@ static ULONG internal_fss_to_unicode(TEXTTYPE obj, ULONG internal_unicode_to_fss(csconvert* obj, ULONG unicode_len, /* BYTE count */ - const UNICODE* unicode_str, + const UCHAR* p_unicode_str, ULONG fss_len, UCHAR* fss_str, USHORT* err_code, @@ -254,11 +271,11 @@ ULONG internal_unicode_to_fss(csconvert* obj, const ULONG src_start = unicode_len; UCHAR tmp_buffer[6]; - fb_assert(unicode_str != NULL || fss_str == NULL); + fb_assert(p_unicode_str != NULL || fss_str == NULL); fb_assert(err_code != NULL); fb_assert(err_position != NULL); fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == (pfn_INTL_convert) internal_unicode_to_fss); + fb_assert(obj->csconvert_fn_convert == internal_unicode_to_fss); *err_code = 0; @@ -266,16 +283,19 @@ ULONG internal_unicode_to_fss(csconvert* obj, if (fss_str == NULL) return ((unicode_len + 1) / 2 * 3); /* worst case - all han character input */ + Firebird::Aligner s(p_unicode_str, unicode_len); + const UNICODE* unicode_str = s; + const UCHAR* const start = fss_str; while ((fss_len) && (unicode_len >= sizeof(*unicode_str))) { /* Convert the wide character into temp buffer */ fss_size_t res = fss_wctomb(tmp_buffer, *unicode_str); - if (res == -1) { + if (res < 0) { *err_code = CS_BAD_INPUT; break; } /* will the mb sequence fit into space left? */ - if (res > fss_len) { + if (static_cast(res) > fss_len) { *err_code = CS_TRUNCATION_ERROR; break; } @@ -314,10 +334,10 @@ static ULONG internal_fss_length(charset* obj, ULONG srcLen, const UCHAR* src) USHORT c; const fss_size_t res = fss_mbtowc(&c, src, srcLen); - if (res == -1) + if (res < 0) break; - fb_assert(res <= srcLen); + fb_assert(static_cast(res) <= srcLen); src += res; srcLen -= res; @@ -363,7 +383,7 @@ static ULONG internal_fss_substring(charset* obj, ULONG srcLen, const UCHAR* src { res = fss_mbtowc(&c, src, srcLen); - if (res == -1) + if (res < 0) { wellFormed = false; continue; @@ -375,7 +395,7 @@ static ULONG internal_fss_substring(charset* obj, ULONG srcLen, const UCHAR* src res = 1; } - fb_assert(res <= srcLen); + fb_assert(static_cast(res) <= srcLen); src += res; srcLen -= res; @@ -391,7 +411,7 @@ static ULONG internal_fss_substring(charset* obj, ULONG srcLen, const UCHAR* src { res = fss_mbtowc(&c, src, srcLen); - if (res == -1) + if (res < 0) { wellFormed = false; continue; @@ -403,7 +423,7 @@ static ULONG internal_fss_substring(charset* obj, ULONG srcLen, const UCHAR* src res = 1; } - fb_assert(res <= srcLen); + fb_assert(static_cast(res) <= srcLen); src += res; srcLen -= res; @@ -669,29 +689,21 @@ static USHORT unicode8_string_to_key( USHORT err_code; ULONG err_position; - Firebird::HalfStaticArray utf16Str; + Firebird::HalfStaticArray utf16Str; ULONG utf16Len = UnicodeUtil::utf8ToUtf16(srcLen, src, 0, NULL, &err_code, &err_position); utf16Len = UnicodeUtil::utf8ToUtf16(srcLen, src, - utf16Len, reinterpret_cast(utf16Str.getBuffer(utf16Len)), + utf16Len, utf16Str.getBuffer(utf16Len / sizeof(USHORT) + 1), &err_code, &err_position); if (obj->texttype_pad_option) { - const UCHAR* pad; - - for (pad = utf16Str.begin() + utf16Len - sizeof(USHORT); pad >= utf16Str.begin(); pad -= sizeof(USHORT)) - { - if (*reinterpret_cast(pad) != 32) - break; - } - - utf16Len = pad - utf16Str.begin() + sizeof(USHORT); + padUtf16(utf16Str.begin(), utf16Len); } return ((UnicodeUtil::Utf16Collation*)obj->reserved_for_driver[0])->stringToKey( - utf16Len, (USHORT*)utf16Str.begin(), dstLen, dst, key_type); + utf16Len, utf16Str.begin(), dstLen, dst, key_type); } @@ -712,45 +724,29 @@ static SSHORT unicode8_compare(TEXTTYPE obj, ULONG length1, const UCHAR* p1, USHORT err_code; ULONG err_position; - Firebird::HalfStaticArray utf16Str1; - Firebird::HalfStaticArray utf16Str2; + Firebird::HalfStaticArray utf16Str1; + Firebird::HalfStaticArray utf16Str2; ULONG utf16Len1 = UnicodeUtil::utf8ToUtf16(length1, p1, 0, NULL, &err_code, &err_position); ULONG utf16Len2 = UnicodeUtil::utf8ToUtf16(length2, p2, 0, NULL, &err_code, &err_position); utf16Len1 = UnicodeUtil::utf8ToUtf16(length1, p1, - utf16Len1, reinterpret_cast(utf16Str1.getBuffer(utf16Len1)), + utf16Len1, utf16Str1.getBuffer(utf16Len1 / sizeof(USHORT) + 1), &err_code, &err_position); utf16Len2 = UnicodeUtil::utf8ToUtf16(length2, p2, - utf16Len2, reinterpret_cast(utf16Str2.getBuffer(utf16Len2)), + utf16Len2, utf16Str2.getBuffer(utf16Len2 / sizeof(USHORT) + 1), &err_code, &err_position); if (obj->texttype_pad_option) { - const UCHAR* pad; - - for (pad = utf16Str1.begin() + utf16Len1 - sizeof(USHORT); pad >= utf16Str1.begin(); pad -= sizeof(USHORT)) - { - if (*reinterpret_cast(pad) != 32) - break; - } - - utf16Len1 = pad - utf16Str1.begin() + sizeof(USHORT); - - for (pad = utf16Str2.begin() + utf16Len2 - sizeof(USHORT); pad >= utf16Str2.begin(); pad -= sizeof(USHORT)) - { - if (*reinterpret_cast(pad) != 32) - break; - } - - utf16Len2 = pad - utf16Str2.begin() + sizeof(USHORT); + padUtf16(utf16Str1.begin(), utf16Len1); + padUtf16(utf16Str2.begin(), utf16Len2); } return ((UnicodeUtil::Utf16Collation*)obj->reserved_for_driver[0])->compare( - utf16Len1, (USHORT*)utf16Str1.begin(), - utf16Len2, (USHORT*)utf16Str2.begin(), error_flag); + utf16Len1, utf16Str1.begin(), utf16Len2, utf16Str2.begin(), error_flag); } @@ -771,7 +767,7 @@ static USHORT utf16_keylength(TEXTTYPE obj, USHORT len) static USHORT utf16_string_to_key( TEXTTYPE obj, USHORT srcLen, - const UCHAR* src, + const UCHAR* psrc, USHORT dstLen, UCHAR* dst, USHORT key_type) @@ -788,28 +784,22 @@ static USHORT utf16_string_to_key( fb_assert(obj != NULL); fb_assert(srcLen % 2 == 0); + const USHORT* src = Firebird::Aligner(psrc, srcLen); + if (obj->texttype_pad_option) { - const UCHAR* pad; - - for (pad = src + srcLen - sizeof(USHORT); pad >= src; pad -= sizeof(USHORT)) - { - if (*reinterpret_cast(pad) != 32) - break; - } - - srcLen = pad - src + sizeof(USHORT); + padUtf16(src, srcLen); } - return UnicodeUtil::utf16ToKey(srcLen, reinterpret_cast(src), dstLen, dst, key_type); + return UnicodeUtil::utf16ToKey(srcLen, src, dstLen, dst, key_type); } static SSHORT utf16_compare( TEXTTYPE obj, ULONG len1, - const UCHAR* str1, + const UCHAR* pstr1, ULONG len2, - const UCHAR* str2, + const UCHAR* pstr2, INTL_BOOL* error_flag) { /************************************** @@ -823,31 +813,17 @@ static SSHORT utf16_compare( **************************************/ fb_assert(obj != NULL); fb_assert(len1 % 2 == 0 && len2 % 2 == 0); - fb_assert(str1 != NULL && str2 != NULL); + fb_assert(pstr1 != NULL && pstr2 != NULL); + const USHORT* str1 = Firebird::Aligner(pstr1, len1); + const USHORT* str2 = Firebird::Aligner(pstr2, len2); if (obj->texttype_pad_option) { - const UCHAR* pad; - - for (pad = str1 + len1 - sizeof(USHORT); pad >= str1; pad -= sizeof(USHORT)) - { - if (*reinterpret_cast(pad) != 32) - break; - } - - len1 = pad - str1 + sizeof(USHORT); - - for (pad = str2 + len2 - sizeof(USHORT); pad >= str2; pad -= sizeof(USHORT)) - { - if (*reinterpret_cast(pad) != 32) - break; - } - - len2 = pad - str2 + sizeof(USHORT); + padUtf16(str1, len1); + padUtf16(str2, len2); } - return UnicodeUtil::utf16Compare(len1, reinterpret_cast(str1), - len2, reinterpret_cast(str2), error_flag); + return UnicodeUtil::utf16Compare(len1, str1, len2, str2, error_flag); } static ULONG utf16_upper( @@ -870,8 +846,8 @@ static ULONG utf16_upper( fb_assert(srcLen % 2 == 0); fb_assert(src != NULL && dst != NULL); - return UnicodeUtil::utf16UpperCase(srcLen, reinterpret_cast(src), - dstLen, reinterpret_cast(dst)); + return UnicodeUtil::utf16UpperCase(srcLen, Firebird::Aligner(src, srcLen), + dstLen, Firebird::OutAligner(dst, dstLen)); } static ULONG utf16_lower( @@ -894,8 +870,8 @@ static ULONG utf16_lower( fb_assert(srcLen % 2 == 0); fb_assert(src != NULL && dst != NULL); - return UnicodeUtil::utf16LowerCase(srcLen, reinterpret_cast(src), - dstLen, reinterpret_cast(dst)); + return UnicodeUtil::utf16LowerCase(srcLen, Firebird::Aligner(src, srcLen), + dstLen, Firebird::OutAligner(dst, dstLen)); } @@ -916,7 +892,7 @@ static USHORT utf32_keylength(TEXTTYPE obj, USHORT len) static USHORT utf32_string_to_key( TEXTTYPE obj, USHORT srcLen, - const UCHAR* src, + const UCHAR* psrc, USHORT dstLen, UCHAR* dst, USHORT key_type) @@ -936,29 +912,21 @@ static USHORT utf32_string_to_key( USHORT err_code; ULONG err_position; - Firebird::HalfStaticArray utf16Str; - srcLen = UnicodeUtil::utf32ToUtf16(srcLen, reinterpret_cast(src), - dstLen, reinterpret_cast(utf16Str.getBuffer(dstLen)), &err_code, &err_position); - src = utf16Str.begin(); + Firebird::HalfStaticArray utf16Str; + srcLen = UnicodeUtil::utf32ToUtf16(srcLen, Firebird::Aligner(psrc, srcLen), + dstLen, utf16Str.getBuffer(dstLen / sizeof(USHORT) + 1), &err_code, &err_position); + USHORT* src = utf16Str.begin(); if (obj->texttype_pad_option) { - const UCHAR* pad; - - for (pad = src + srcLen - sizeof(USHORT); pad >= src; pad -= sizeof(USHORT)) - { - if (*reinterpret_cast(pad) != 32) - break; - } - - srcLen = pad - src + sizeof(USHORT); + padUtf16(src, srcLen); } - return UnicodeUtil::utf16ToKey(srcLen, reinterpret_cast(src), dstLen, dst, key_type); + return UnicodeUtil::utf16ToKey(srcLen, src, dstLen, dst, key_type); } -static ULONG wc_to_nc(csconvert* obj, ULONG nSrc, const USHORT* pSrc, +static ULONG wc_to_nc(csconvert* obj, ULONG nSrc, const UCHAR* ppSrc, ULONG nDest, UCHAR* pDest, USHORT* err_code, ULONG* err_position) { @@ -972,7 +940,7 @@ static ULONG wc_to_nc(csconvert* obj, ULONG nSrc, const USHORT* pSrc, * **************************************/ fb_assert(obj != NULL); - fb_assert((pSrc != NULL) || (pDest == NULL)); + fb_assert((ppSrc != NULL) || (pDest == NULL)); fb_assert(err_code != NULL); fb_assert(err_position != NULL); @@ -980,6 +948,9 @@ static ULONG wc_to_nc(csconvert* obj, ULONG nSrc, const USHORT* pSrc, if (pDest == NULL) /* length estimate needed? */ return ((nSrc + 1) / 2); + Firebird::Aligner s(ppSrc, nSrc); + const USHORT* pSrc = s; + const UCHAR* const pStart = pDest; const USHORT* const pStart_src = pSrc; @@ -1002,7 +973,7 @@ static ULONG wc_to_nc(csconvert* obj, ULONG nSrc, const USHORT* pSrc, static ULONG mb_to_wc(csconvert* obj, ULONG nSrc, const UCHAR* pSrc, - ULONG nDest, USHORT* pDest, + ULONG nDest, UCHAR* ppDest, USHORT* err_code, ULONG* err_position) { /************************************** @@ -1017,14 +988,17 @@ static ULONG mb_to_wc(csconvert* obj, ULONG nSrc, const UCHAR* pSrc, * *************************************/ fb_assert(obj != NULL); - fb_assert((pSrc != NULL) || (pDest == NULL)); + fb_assert((pSrc != NULL) || (ppDest == NULL)); fb_assert(err_code != NULL); fb_assert(err_position != NULL); *err_code = 0; - if (pDest == NULL) /* length estimate needed? */ + if (ppDest == NULL) /* length estimate needed? */ return (nSrc); + Firebird::OutAligner d(ppDest, nDest); + USHORT* pDest = d; + const USHORT* const pStart = pDest; const UCHAR* const pStart_src = pSrc; while (nDest > 1 && nSrc > 1) { @@ -1042,7 +1016,7 @@ static ULONG mb_to_wc(csconvert* obj, ULONG nSrc, const UCHAR* pSrc, } -static ULONG wc_to_mb(csconvert* obj, ULONG nSrc, const USHORT* pSrc, +static ULONG wc_to_mb(csconvert* obj, ULONG nSrc, const UCHAR* ppSrc, ULONG nDest, UCHAR* pDest, USHORT* err_code, ULONG* err_position) { @@ -1058,7 +1032,7 @@ static ULONG wc_to_mb(csconvert* obj, ULONG nSrc, const USHORT* pSrc, * *************************************/ fb_assert(obj != NULL); - fb_assert((pSrc != NULL) || (pDest == NULL)); + fb_assert((ppSrc != NULL) || (pDest == NULL)); fb_assert(err_code != NULL); fb_assert(err_position != NULL); @@ -1066,6 +1040,9 @@ static ULONG wc_to_mb(csconvert* obj, ULONG nSrc, const USHORT* pSrc, if (pDest == NULL) /* length estimate needed? */ return (nSrc); + Firebird::Aligner s(ppSrc, nSrc); + const USHORT* pSrc = s; + const UCHAR* const pStart = pDest; const USHORT* const pStart_src = pSrc; while (nDest > 1 && nSrc > 1) { @@ -1346,7 +1323,7 @@ static INTL_BOOL cs_utf16_well_formed(charset* cs, *************************************/ fb_assert(cs != NULL); - return UnicodeUtil::utf16WellFormed(len, reinterpret_cast(str), offending_position); + return UnicodeUtil::utf16WellFormed(len, Firebird::Aligner(str, len), offending_position); } @@ -1365,7 +1342,7 @@ static ULONG cs_utf16_length(charset* cs, * *************************************/ fb_assert(cs != NULL); - return UnicodeUtil::utf16Length(srcLen, reinterpret_cast(src)); + return UnicodeUtil::utf16Length(srcLen, Firebird::Aligner(src, srcLen)); } @@ -1389,8 +1366,8 @@ static ULONG cs_utf16_substring(charset* cs, *************************************/ fb_assert(cs != NULL); - return UnicodeUtil::utf16Substring(srcLen, reinterpret_cast(src), - dstLen, reinterpret_cast(dst), startPos, length); + return UnicodeUtil::utf16Substring(srcLen, Firebird::Aligner(src, srcLen), + dstLen, Firebird::OutAligner(dst, dstLen), startPos, length); } @@ -1411,7 +1388,7 @@ static INTL_BOOL cs_utf32_well_formed(charset* cs, *************************************/ fb_assert(cs != NULL); - return UnicodeUtil::utf32WellFormed(len, reinterpret_cast(str), offending_position); + return UnicodeUtil::utf32WellFormed(len, Firebird::Aligner(str, len), offending_position); } @@ -1436,7 +1413,7 @@ static void common_convert_init( static ULONG cvt_ascii_to_unicode(csconvert* obj, ULONG nSrc, const UCHAR* pSrc, - ULONG nDest, USHORT* pDest, + ULONG nDest, UCHAR* ppDest, USHORT* err_code, ULONG* err_position) { /************************************** @@ -1452,13 +1429,16 @@ static ULONG cvt_ascii_to_unicode(csconvert* obj, ULONG nSrc, const UCHAR* pSrc, * *************************************/ fb_assert(obj != NULL); - fb_assert((pSrc != NULL) || (pDest == NULL)); + fb_assert((pSrc != NULL) || (ppDest == NULL)); fb_assert(err_code != NULL); *err_code = 0; - if (pDest == NULL) /* length estimate needed? */ + if (ppDest == NULL) /* length estimate needed? */ return (2 * nSrc); + Firebird::OutAligner d(ppDest, nDest); + USHORT* pDest = d; + const USHORT* const pStart = pDest; const UCHAR* const pStart_src = pSrc; while (nDest >= sizeof(*pDest) && nSrc >= sizeof(*pSrc)) { @@ -1479,7 +1459,7 @@ static ULONG cvt_ascii_to_unicode(csconvert* obj, ULONG nSrc, const UCHAR* pSrc, } -static ULONG cvt_unicode_to_ascii(csconvert* obj, ULONG nSrc, const USHORT* pSrc, +static ULONG cvt_unicode_to_ascii(csconvert* obj, ULONG nSrc, const UCHAR* ppSrc, ULONG nDest, UCHAR* pDest, USHORT* err_code, ULONG* err_position) { @@ -1496,9 +1476,12 @@ static ULONG cvt_unicode_to_ascii(csconvert* obj, ULONG nSrc, const USHORT* pSrc * *************************************/ fb_assert(obj != NULL); - fb_assert((pSrc != NULL) || (pDest == NULL)); + fb_assert((ppSrc != NULL) || (pDest == NULL)); fb_assert(err_code != NULL); + Firebird::Aligner s(ppSrc, nSrc); + const USHORT* pSrc = s; + *err_code = 0; if (pDest == NULL) /* length estimate needed? */ return (nSrc / 2); @@ -1524,7 +1507,7 @@ static ULONG cvt_unicode_to_ascii(csconvert* obj, ULONG nSrc, const USHORT* pSrc static ULONG cvt_none_to_unicode(csconvert* obj, ULONG nSrc, const UCHAR* pSrc, - ULONG nDest, USHORT* pDest, + ULONG nDest, UCHAR* ppDest, USHORT* err_code, ULONG* err_position) { /************************************** @@ -1540,9 +1523,12 @@ static ULONG cvt_none_to_unicode(csconvert* obj, ULONG nSrc, const UCHAR* pSrc, * *************************************/ fb_assert(obj != NULL); - fb_assert((pSrc != NULL) || (pDest == NULL)); + fb_assert((pSrc != NULL) || (ppDest == NULL)); fb_assert(err_code != NULL); + Firebird::OutAligner d(ppDest, nDest); + USHORT* pDest = d; + *err_code = 0; if (pDest == NULL) /* length estimate needed? */ return (2 * nSrc); @@ -1567,8 +1553,8 @@ static ULONG cvt_none_to_unicode(csconvert* obj, ULONG nSrc, const UCHAR* pSrc, } -static ULONG cvt_unicode_to_unicode(csconvert* obj, ULONG nSrc, const USHORT* pSrc, - ULONG nDest, USHORT* pDest, +static ULONG cvt_unicode_to_unicode(csconvert* obj, ULONG nSrc, const UCHAR* ppSrc, + ULONG nDest, UCHAR* ppDest, USHORT* err_code, ULONG* err_position) { /************************************** @@ -1581,13 +1567,18 @@ static ULONG cvt_unicode_to_unicode(csconvert* obj, ULONG nSrc, const USHORT* pS * *************************************/ fb_assert(obj != NULL); - fb_assert((pSrc != NULL) || (pDest == NULL)); + fb_assert((ppSrc != NULL) || (ppDest == NULL)); fb_assert(err_code != NULL); *err_code = 0; - if (pDest == NULL) /* length estimate needed? */ + if (ppDest == NULL) /* length estimate needed? */ return nSrc; + Firebird::Aligner s(ppSrc, nSrc); + const USHORT* pSrc = s; + Firebird::OutAligner d(ppDest, nDest); + USHORT* pDest = d; + const USHORT* const pStart = pDest; const USHORT* const pStart_src = pSrc; while (nDest >= sizeof(*pDest) && nSrc >= sizeof(*pSrc)) { @@ -1659,15 +1650,16 @@ static ULONG cvt_utffss_to_ascii(csconvert* obj, ULONG nSrc, const UCHAR* pSrc, static ULONG cvt_unicode_to_utf8(csconvert* obj, ULONG unicode_len, - const USHORT* unicode_str, + const UCHAR* unicode_str, ULONG utf8_len, UCHAR* utf8_str, USHORT* err_code, ULONG* err_position) { fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == (pfn_INTL_convert) cvt_unicode_to_utf8); - return UnicodeUtil::utf16ToUtf8(unicode_len, unicode_str, utf8_len, utf8_str, err_code, err_position); + fb_assert(obj->csconvert_fn_convert == cvt_unicode_to_utf8); + return UnicodeUtil::utf16ToUtf8(unicode_len, Firebird::Aligner(unicode_str, unicode_len), + utf8_len, utf8_str, err_code, err_position); } @@ -1675,41 +1667,45 @@ static ULONG cvt_utf8_to_unicode(csconvert* obj, ULONG utf8_len, const UCHAR* utf8_str, ULONG unicode_len, - USHORT* unicode_str, + UCHAR* unicode_str, USHORT* err_code, ULONG* err_position) { fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == (pfn_INTL_convert) cvt_utf8_to_unicode); - return UnicodeUtil::utf8ToUtf16(utf8_len, utf8_str, unicode_len, unicode_str, err_code, err_position); + fb_assert(obj->csconvert_fn_convert == cvt_utf8_to_unicode); + return UnicodeUtil::utf8ToUtf16(utf8_len, utf8_str, + unicode_len, Firebird::OutAligner(unicode_str, unicode_len), err_code, err_position); } static ULONG cvt_unicode_to_utf32(csconvert* obj, ULONG unicode_len, - const USHORT* unicode_str, + const UCHAR* unicode_str, ULONG utf32_len, - ULONG* utf32_str, + UCHAR* utf32_str, USHORT* err_code, ULONG* err_position) { fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == (pfn_INTL_convert) cvt_unicode_to_utf32); - return UnicodeUtil::utf16ToUtf32(unicode_len, unicode_str, utf32_len, utf32_str, err_code, err_position); + fb_assert(obj->csconvert_fn_convert == cvt_unicode_to_utf32); + return UnicodeUtil::utf16ToUtf32(unicode_len, Firebird::Aligner(unicode_str, unicode_len), + utf32_len, Firebird::OutAligner(utf32_str, utf32_len), err_code, err_position); } static ULONG cvt_utf32_to_unicode(csconvert* obj, ULONG utf32_len, - const ULONG* utf32_str, + const UCHAR* utf32_str, ULONG unicode_len, - USHORT* unicode_str, + UCHAR* unicode_str, USHORT* err_code, ULONG* err_position) { fb_assert(obj != NULL); - fb_assert(obj->csconvert_fn_convert == (pfn_INTL_convert) cvt_utf32_to_unicode); - return UnicodeUtil::utf32ToUtf16(utf32_len, utf32_str, unicode_len, unicode_str, err_code, err_position); + fb_assert(obj->csconvert_fn_convert == cvt_utf32_to_unicode); + + return UnicodeUtil::utf32ToUtf16(utf32_len, Firebird::Aligner(utf32_str, utf32_len), + unicode_len, Firebird::OutAligner(unicode_str, unicode_len), err_code, err_position); } @@ -1726,10 +1722,8 @@ static INTL_BOOL cs_ascii_init(charset* csptr, const ASCII* charset_name) *************************************/ common_8bit_init(csptr, (const ASCII*) "ASCII", NULL, NULL, NULL); - common_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(cvt_ascii_to_unicode)); - common_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(cvt_unicode_to_ascii)); + common_convert_init(&csptr->charset_to_unicode, cvt_ascii_to_unicode); + common_convert_init(&csptr->charset_from_unicode, cvt_unicode_to_ascii); return true; } @@ -1751,10 +1745,8 @@ static INTL_BOOL cs_none_init(charset* csptr, const ASCII* charset_name) common_convert_init (&csptr->charset_to_unicode, CS_UTF16, id, nc_to_wc, to_unicode_tbl, NULL); */ - common_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(cvt_none_to_unicode)); - common_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(wc_to_nc)); + common_convert_init(&csptr->charset_to_unicode, cvt_none_to_unicode); + common_convert_init(&csptr->charset_from_unicode, wc_to_nc); return true; } @@ -1776,10 +1768,8 @@ static INTL_BOOL cs_unicode_fss_init(charset* csptr, const ASCII* charset_name) csptr->charset_max_bytes_per_char = 3; csptr->charset_flags |= CHARSET_LEGACY_SEMANTICS; - common_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(internal_fss_to_unicode)); - common_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(internal_unicode_to_fss)); + common_convert_init(&csptr->charset_to_unicode, internal_fss_to_unicode); + common_convert_init(&csptr->charset_from_unicode, internal_unicode_to_fss); csptr->charset_fn_length = internal_fss_length; csptr->charset_fn_substring = internal_fss_substring; @@ -1809,10 +1799,8 @@ static INTL_BOOL cs_unicode_ucs2_init(charset* csptr, const ASCII* charset_name) csptr->charset_space_character = (const BYTE*) & space; /* 0x0020 */ csptr->charset_fn_well_formed = NULL; - common_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(cvt_unicode_to_unicode)); - common_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(cvt_unicode_to_unicode)); + common_convert_init(&csptr->charset_to_unicode, cvt_unicode_to_unicode); + common_convert_init(&csptr->charset_from_unicode, cvt_unicode_to_unicode); return true; } @@ -1832,10 +1820,8 @@ static INTL_BOOL cs_binary_init(charset* csptr, const ASCII* charset_name) common_8bit_init(csptr, (const ASCII*) "BINARY", NULL, NULL, NULL); csptr->charset_space_character = (const BYTE*) "\0"; - common_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(mb_to_wc)); - common_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(wc_to_mb)); + common_convert_init(&csptr->charset_to_unicode, mb_to_wc); + common_convert_init(&csptr->charset_from_unicode, wc_to_mb); return true; } @@ -1863,10 +1849,8 @@ static INTL_BOOL cs_utf8_init(charset* csptr, const ASCII* charset_name) csptr->charset_space_character = (const BYTE*)&space; csptr->charset_fn_well_formed = cs_utf8_well_formed; - common_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(cvt_utf8_to_unicode)); - common_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(cvt_unicode_to_utf8)); + common_convert_init(&csptr->charset_to_unicode, cvt_utf8_to_unicode); + common_convert_init(&csptr->charset_from_unicode, cvt_unicode_to_utf8); return true; } @@ -1896,10 +1880,8 @@ static INTL_BOOL cs_utf16_init(charset* csptr, const ASCII* charset_name) csptr->charset_fn_length = cs_utf16_length; csptr->charset_fn_substring = cs_utf16_substring; - common_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(cvt_unicode_to_unicode)); - common_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(cvt_unicode_to_unicode)); + common_convert_init(&csptr->charset_to_unicode, cvt_unicode_to_unicode); + common_convert_init(&csptr->charset_from_unicode, cvt_unicode_to_unicode); return true; } @@ -1927,10 +1909,8 @@ static INTL_BOOL cs_utf32_init(charset* csptr, const ASCII* charset_name) csptr->charset_space_character = (const BYTE*)&space; csptr->charset_fn_well_formed = cs_utf32_well_formed; - common_convert_init(&csptr->charset_to_unicode, - reinterpret_cast(cvt_utf32_to_unicode)); - common_convert_init(&csptr->charset_from_unicode, - reinterpret_cast(cvt_unicode_to_utf32)); + common_convert_init(&csptr->charset_to_unicode, cvt_utf32_to_unicode); + common_convert_init(&csptr->charset_from_unicode, cvt_unicode_to_utf32); return true; } diff --git a/src/jrd/intl_classes.h b/src/jrd/intl_classes.h index 04461cc0ee8..2afba0b2962 100644 --- a/src/jrd/intl_classes.h +++ b/src/jrd/intl_classes.h @@ -33,6 +33,7 @@ #include "../jrd/intlobj_new.h" #include "../jrd/constants.h" #include "../jrd/unicode_util.h" +#include "../common/classes/Aligner.h" typedef SSHORT CHARSET_ID; typedef SSHORT COLLATE_ID; @@ -297,7 +298,7 @@ class TextType { const UCHAR* space = getCharSet()->getSpace(); BYTE spaceLength = getCharSet()->getSpaceLength(); - Firebird::HalfStaticArray utf16Str; + Firebird::HalfStaticArray utf16Str; UCHAR utf16Space[sizeof(ULONG)]; if (getCharSet()->isMultiByte()) @@ -309,8 +310,8 @@ class TextType ULONG utf16Length = getCharSet()->getConvToUnicode().convertLength(srcLen); srcLen = getCharSet()->getConvToUnicode().convert(srcLen, src, - utf16Length, utf16Str.getBuffer(utf16Length), &err_code, &err_position); - src = utf16Str.begin(); + utf16Length, utf16Str.getBuffer(utf16Length / sizeof(USHORT) + 1), &err_code, &err_position); + src = reinterpret_cast(utf16Str.begin()); // convert charset space to UTF-16 spaceLength = getCharSet()->getConvToUnicode().convert(spaceLength, space, @@ -334,7 +335,7 @@ class TextType if (getCharSet()->isMultiByte()) { - dstLen = UnicodeUtil::utf16ToKey(srcLen, reinterpret_cast(src), + dstLen = UnicodeUtil::utf16ToKey(srcLen, Firebird::Aligner(src, srcLen), dstLen, dst, key_type); } else @@ -365,8 +366,8 @@ class TextType { const UCHAR* space = getCharSet()->getSpace(); BYTE spaceLength = getCharSet()->getSpaceLength(); - Firebird::HalfStaticArray utf16Str1; - Firebird::HalfStaticArray utf16Str2; + Firebird::HalfStaticArray utf16Str1; + Firebird::HalfStaticArray utf16Str2; UCHAR utf16Space[sizeof(ULONG)]; if (getCharSet()->isMultiByte()) @@ -378,15 +379,15 @@ class TextType ULONG utf16Length = getCharSet()->getConvToUnicode().convertLength(len1); len1 = getCharSet()->getConvToUnicode().convert(len1, str1, - utf16Length, utf16Str1.getBuffer(utf16Length), &err_code, &err_position); - str1 = utf16Str1.begin(); + utf16Length, utf16Str1.getBuffer(utf16Length / sizeof(USHORT) + 1), &err_code, &err_position); + str1 = reinterpret_cast(utf16Str1.begin()); // convert str2 to UTF-16 utf16Length = getCharSet()->getConvToUnicode().convertLength(len2); len2 = getCharSet()->getConvToUnicode().convert(len2, str2, - utf16Length, utf16Str2.getBuffer(utf16Length), &err_code, &err_position); - str2 = utf16Str2.begin(); + utf16Length, utf16Str2.getBuffer(utf16Length / sizeof(USHORT) + 1), &err_code, &err_position); + str2 = reinterpret_cast(utf16Str2.begin()); // convert charset space to UTF-16 spaceLength = getCharSet()->getConvToUnicode().convert(spaceLength, space, @@ -419,8 +420,8 @@ class TextType if (getCharSet()->isMultiByte()) { INTL_BOOL error_flag; - return UnicodeUtil::utf16Compare(len1, reinterpret_cast(str1), - len2, reinterpret_cast(str2), &error_flag); + return UnicodeUtil::utf16Compare(len1, Firebird::Aligner(str1, len1), + len2, Firebird::Aligner(str2, len2), &error_flag); } else { @@ -448,22 +449,17 @@ class TextType ULONG err_position; ULONG utf16_length = getCharSet()->getConvToUnicode().convertLength(srcLen); - Firebird::HalfStaticArray utf16_str; - UCHAR* utf16_ptr; - - if (dstLen >= utf16_length) // if dst buffer is sufficient large, use it as intermediate - utf16_ptr = dst; - else - utf16_ptr = utf16_str.getBuffer(utf16_length); + Firebird::HalfStaticArray utf16_str; + USHORT* utf16_ptr = utf16_str.getBuffer(utf16_length / sizeof(USHORT) + 1); // convert to UTF-16 srcLen = getCharSet()->getConvToUnicode().convert(srcLen, src, utf16_length, utf16_ptr, &err_code, &err_position); // convert to uppercase - Firebird::HalfStaticArray upper_str; - srcLen = UnicodeUtil::utf16UpperCase(srcLen, reinterpret_cast(utf16_ptr), - utf16_length, reinterpret_cast(upper_str.getBuffer(utf16_length))); + Firebird::HalfStaticArray upper_str; + srcLen = UnicodeUtil::utf16UpperCase(srcLen, utf16_ptr, + utf16_length, upper_str.getBuffer(utf16_length / sizeof(USHORT) + 1)); // convert to original character set return getCharSet()->getConvFromUnicode().convert(srcLen, upper_str.begin(), @@ -485,22 +481,17 @@ class TextType ULONG err_position; ULONG utf16_length = getCharSet()->getConvToUnicode().convertLength(srcLen); - Firebird::HalfStaticArray utf16_str; - UCHAR* utf16_ptr; - - if (dstLen >= utf16_length) // if dst buffer is sufficient large, use it as intermediate - utf16_ptr = dst; - else - utf16_ptr = utf16_str.getBuffer(utf16_length); + Firebird::HalfStaticArray utf16_str; + USHORT* utf16_ptr = utf16_str.getBuffer(utf16_length / sizeof(USHORT) + 1); // convert to UTF-16 srcLen = getCharSet()->getConvToUnicode().convert(srcLen, src, utf16_length, utf16_ptr, &err_code, &err_position); // convert to lowercase - Firebird::HalfStaticArray lower_str; - srcLen = UnicodeUtil::utf16LowerCase(srcLen, reinterpret_cast(utf16_ptr), - utf16_length, reinterpret_cast(lower_str.getBuffer(utf16_length))); + Firebird::HalfStaticArray lower_str; + srcLen = UnicodeUtil::utf16LowerCase(srcLen, utf16_ptr, + utf16_length, lower_str.getBuffer(utf16_length / sizeof(USHORT) + 1)); // convert to original character set return getCharSet()->getConvFromUnicode().convert(srcLen, lower_str.begin(), @@ -523,17 +514,17 @@ class TextType USHORT err_code; ULONG err_position; - Firebird::HalfStaticArray utf16_str; + Firebird::HalfStaticArray utf16_str; ULONG utf16_len = getCharSet()->getConvToUnicode().convertLength(srcLen); // convert to UTF-16 utf16_len = getCharSet()->getConvToUnicode().convert(srcLen, src, - utf16_len, utf16_str.getBuffer(utf16_len), &err_code, &err_position); + utf16_len, utf16_str.getBuffer(utf16_len / sizeof(USHORT) + 1), &err_code, &err_position); // convert UTF-16 to UTF-32 - return UnicodeUtil::utf16ToUtf32(utf16_len, reinterpret_cast(utf16_str.begin()), - dstLen, reinterpret_cast(dst), &err_code, &err_position) / sizeof(ULONG); + return UnicodeUtil::utf16ToUtf32(utf16_len, utf16_str.begin(), + dstLen, Firebird::OutAligner(dst, dstLen), &err_code, &err_position) / sizeof(ULONG); } else { diff --git a/src/jrd/unicode_util.cpp b/src/jrd/unicode_util.cpp index d6c283a20db..e94469038e2 100644 --- a/src/jrd/unicode_util.cpp +++ b/src/jrd/unicode_util.cpp @@ -70,6 +70,7 @@ USHORT UnicodeUtil::utf16ToKey(USHORT srcLen, const USHORT* src, USHORT dstLen, ULONG UnicodeUtil::utf16LowerCase(ULONG srcLen, const USHORT* src, ULONG dstLen, USHORT* dst) { // this is more correct but we don't support completely yet + // will need avoiding reinterpret_cast /*** fb_assert(srcLen % sizeof(*src) == 0); fb_assert(src != NULL && dst != NULL); @@ -112,6 +113,7 @@ ULONG UnicodeUtil::utf16LowerCase(ULONG srcLen, const USHORT* src, ULONG dstLen, ULONG UnicodeUtil::utf16UpperCase(ULONG srcLen, const USHORT* src, ULONG dstLen, USHORT* dst) { // this is more correct but we don't support completely yet + // will need avoiding reinterpret_cast /*** fb_assert(srcLen % sizeof(*src) == 0); fb_assert(src != NULL && dst != NULL);