8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-31 04:03:03 +01:00
firebird-mirror/src/intl/cv_big5.cpp

221 lines
5.6 KiB
C++
Raw Normal View History

2001-05-23 15:26:42 +02:00
/*
* PROGRAM: InterBase International support
* MODULE: cv_big5.cpp
2001-05-23 15:26:42 +02:00
* DESCRIPTION: Codeset conversion for BIG5 family codesets
*
* The contents of this file are subject to the Interbase 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.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, 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 Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
2003-02-17 11:37:42 +01:00
#include "firebird.h"
2001-05-23 15:26:42 +02:00
#include "../intl/ldcommon.h"
#include "../intl/cv_big5.h"
2005-05-28 00:45:31 +02:00
#include "../intl/cv_narrow.h"
2003-09-21 01:33:36 +02:00
#include "ld_proto.h"
2001-05-23 15:26:42 +02:00
2005-05-28 00:45:31 +02:00
ULONG CVBIG5_big5_to_unicode(csconvert* obj,
ULONG src_len,
const UCHAR* src_ptr,
ULONG dest_len,
UCHAR* p_dest_ptr,
USHORT* err_code,
ULONG* err_position)
2001-05-23 15:26:42 +02:00
{
fb_assert(obj != NULL);
CsConvertImpl* impl = static_cast<CsConvertImpl*>(obj->csconvert_impl);
fb_assert(src_ptr != NULL || p_dest_ptr == NULL);
2003-11-04 00:59:24 +01:00
fb_assert(err_code != NULL);
fb_assert(err_position != NULL);
fb_assert(obj->csconvert_fn_convert == CVBIG5_big5_to_unicode);
fb_assert(impl->csconvert_datatable != NULL);
fb_assert(impl->csconvert_misc != NULL);
2001-05-23 15:26:42 +02:00
const ULONG src_start = src_len;
2001-05-23 15:26:42 +02:00
*err_code = 0;
2007-10-06 18:07:31 +02:00
// See if we're only after a length estimate
if (p_dest_ptr == NULL)
2005-05-28 00:45:31 +02:00
return (src_len * sizeof(USHORT));
2001-05-23 15:26:42 +02:00
Firebird::OutAligner<USHORT> d(p_dest_ptr, dest_len);
USHORT* dest_ptr = d;
2005-05-28 00:45:31 +02:00
USHORT wide;
2004-03-07 08:58:55 +01:00
USHORT this_len;
2005-05-28 00:45:31 +02:00
const USHORT* const start = dest_ptr;
2001-05-23 15:26:42 +02:00
while ((src_len) && (dest_len > 1)) {
if (*src_ptr & 0x80) {
2004-03-07 08:58:55 +01:00
const UCHAR c1 = *src_ptr++;
2001-05-23 15:26:42 +02:00
if (BIG51(c1)) { /* first byte is Big5 */
if (src_len == 1) {
*err_code = CS_BAD_INPUT;
break;
}
2004-03-07 08:58:55 +01:00
const UCHAR c2 = *src_ptr++;
2001-05-23 15:26:42 +02:00
if (!(BIG52(c2))) { /* Bad second byte */
*err_code = CS_BAD_INPUT;
break;
}
wide = (c1 << 8) + c2;
this_len = 2;
}
else {
*err_code = CS_BAD_INPUT;
break;
}
}
else { /* it is ASCII */
wide = *src_ptr++;
this_len = 1;
}
/* Convert from BIG5 to UNICODE */
const USHORT ch = ((const USHORT*) impl->csconvert_datatable)
[((const USHORT*) impl->csconvert_misc)[(USHORT) wide / 256] + (wide % 256)];
2001-05-23 15:26:42 +02:00
if ((ch == CS_CANT_MAP) && !(wide == CS_CANT_MAP)) {
*err_code = CS_CONVERT_ERROR;
break;
}
*dest_ptr++ = ch;
dest_len -= sizeof(*dest_ptr);
2001-05-23 15:26:42 +02:00
src_len -= this_len;
}
2001-05-23 15:26:42 +02:00
if (src_len && !*err_code) {
*err_code = CS_TRUNCATION_ERROR;
}
2001-05-23 15:26:42 +02:00
*err_position = src_start - src_len;
return ((dest_ptr - start) * sizeof(*dest_ptr));
}
2005-05-28 00:45:31 +02:00
ULONG CVBIG5_unicode_to_big5(csconvert* obj,
ULONG unicode_len,
const UCHAR* p_unicode_str,
2005-05-28 00:45:31 +02:00
ULONG big5_len,
UCHAR* big5_str,
USHORT* err_code,
ULONG* err_position)
2001-05-23 15:26:42 +02:00
{
fb_assert(obj != NULL);
CsConvertImpl* impl = static_cast<CsConvertImpl*>(obj->csconvert_impl);
fb_assert(p_unicode_str != NULL || big5_str == NULL);
2003-11-04 00:59:24 +01:00
fb_assert(err_code != NULL);
fb_assert(err_position != NULL);
fb_assert(obj->csconvert_fn_convert == CVBIG5_unicode_to_big5);
fb_assert(impl->csconvert_datatable != NULL);
fb_assert(impl->csconvert_misc != NULL);
2001-05-23 15:26:42 +02:00
2005-05-28 00:45:31 +02:00
const ULONG src_start = unicode_len;
2001-05-23 15:26:42 +02:00
*err_code = 0;
/* See if we're only after a length estimate */
if (big5_str == NULL)
return (unicode_len); /* worst case - all han character input */
Firebird::Aligner<USHORT> s(p_unicode_str, unicode_len);
const USHORT* unicode_str = s;
2004-03-07 08:58:55 +01:00
const UCHAR* const start = big5_str;
2001-05-23 15:26:42 +02:00
while ((big5_len) && (unicode_len > 1)) {
/* Convert from UNICODE to BIG5 code */
2005-05-28 00:45:31 +02:00
const USHORT wide = *unicode_str++;
2001-05-23 15:26:42 +02:00
const USHORT big5_ch = ((const USHORT*) impl->csconvert_datatable)
[((const USHORT*) impl->csconvert_misc)[(USHORT)wide / 256] + (wide % 256)];
2001-05-23 15:26:42 +02:00
if ((big5_ch == CS_CANT_MAP) && !(wide == CS_CANT_MAP)) {
*err_code = CS_CONVERT_ERROR;
break;
}
2001-05-23 15:26:42 +02:00
2004-03-07 08:58:55 +01:00
// int ???
const int tmp1 = big5_ch / 256;
const int tmp2 = big5_ch % 256;
2001-05-23 15:26:42 +02:00
if (tmp1 == 0) { /* ASCII character */
fb_assert((UCHAR(tmp2)&0x80)==0);
2001-05-23 15:26:42 +02:00
*big5_str++ = tmp2;
big5_len--;
unicode_len -= sizeof(*unicode_str);
continue;
}
2001-05-23 15:26:42 +02:00
if (big5_len < 2) {
*err_code = CS_TRUNCATION_ERROR;
break;
}
else {
2003-11-04 00:59:24 +01:00
fb_assert(BIG51(tmp1));
fb_assert(BIG52(tmp2));
2001-05-23 15:26:42 +02:00
*big5_str++ = tmp1;
*big5_str++ = tmp2;
unicode_len -= sizeof(*unicode_str);
big5_len -= 2;
}
2001-05-23 15:26:42 +02:00
}
if (unicode_len && !*err_code) {
*err_code = CS_TRUNCATION_ERROR;
}
*err_position = src_start - unicode_len;
return ((big5_str - start) * sizeof(*big5_str));
}
2005-05-28 00:45:31 +02:00
INTL_BOOL CVBIG5_check_big5(charset* cs,
ULONG big5_len,
const UCHAR* big5_str,
ULONG* offending_position)
2001-05-23 15:26:42 +02:00
{
/**************************************
* Functional description
* Make sure that the big5 string does not have any truncated 2 byte
* character at the end.
* If we have a truncated character then,
2005-05-28 00:45:31 +02:00
* return false.
* else return(true);
2001-05-23 15:26:42 +02:00
**************************************/
2005-05-28 00:45:31 +02:00
const UCHAR* big5_str_start = big5_str;
2008-02-28 03:43:23 +01:00
while (big5_len--)
{
2004-03-07 08:58:55 +01:00
const UCHAR c1 = *big5_str;
2008-02-28 03:43:23 +01:00
if (BIG51(c1)) // Is it BIG-5
{
2001-05-23 15:26:42 +02:00
if (big5_len == 0) /* truncated BIG-5 */
2005-05-28 00:45:31 +02:00
{
if (offending_position)
*offending_position = big5_str - big5_str_start;
return (false);
}
2008-02-28 03:43:23 +01:00
2008-02-26 08:23:32 +01:00
big5_str += 2;
big5_len -= 1;
2001-05-23 15:26:42 +02:00
}
2008-02-28 03:43:23 +01:00
else // it is a ASCII
2001-05-23 15:26:42 +02:00
big5_str++;
}
2005-05-28 00:45:31 +02:00
return (true);
2001-05-23 15:26:42 +02:00
}