You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by ab...@locus.apache.org on 2000/02/10 19:08:28 UTC
cvs commit: xml-xerces/c/src/util/Transcoders/Iconv400 iconv_cnv.c iconv_util.c iconv_cnv.h iconv_util.h pos400.h utypes.h Iconv400TransService.cpp Iconv400TransService.hpp Makefile.in
abagchi 00/02/10 10:08:28
Added: c/src/util/Transcoders/Iconv400 iconv_cnv.c iconv_util.c
iconv_cnv.h iconv_util.h pos400.h utypes.h
Iconv400TransService.cpp Iconv400TransService.hpp
Makefile.in
Log:
Initial checkin
Revision Changes Path
1.1 xml-xerces/c/src/util/Transcoders/Iconv400/iconv_cnv.c
Index: iconv_cnv.c
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 1999, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/**
* $Log: iconv_cnv.c,v $
* Revision 1.1 2000/02/10 18:08:28 abagchi
* Initial checkin
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <iconv_util.h>
#include <iconv_cnv.h>
#include <qmhrtvm.h>
#include <qusec.h>
#include <util/Platforms/OS400/OS400PlatformUtils.hpp>
#include <iconv.h>
#include <errno.h>
#define CHUNK_SIZE 5*1024
void Convert_toUnicode(UConverter *,
UChar **,
const UChar *,
const char **,
const char *,
int32_t* offsets,
int,
UErrorCode *);
void Convert_fromUnicode(UConverter *,
char **,
const char *,
const UChar **,
const UChar *,
int32_t* offsets,
int,
UErrorCode *);
UChar getNextUChar(UConverter* converter,
const char** source,
const char* sourceLimit,
UErrorCode* err);
void T_UConverter_fromCodepageToCodepage (UConverter * outConverter,
UConverter * inConverter,
char **target,
const char *targetLimit,
const char **source,
const char *sourceLimit,
int32_t* offsets,
int flush,
UErrorCode * err);
void Converter_fromUnicode(UConverter * _this,
char **target,
const char *targetLimit,
const UChar ** source,
const UChar * sourceLimit,
int32_t *offsets,
int flush,
UErrorCode * err);
/*Calls through createConverter */
UConverter* ucnv_open (const char *name,
UErrorCode * err)
{
if (U_FAILURE (*err))
return NULL;
/*In case "name" is NULL we want to open the default converter */
if (name != NULL)
return createConverter (name, err);
else
return createConverter (iconv_getDefaultCodepage(), err);
}
/*Extracts the UChar* to a char* and calls through createConverter */
UConverter* ucnv_openU (const UChar * name,
UErrorCode * err)
{
char asciiName[MAX_CONVERTER_NAME_LENGTH];
if (U_FAILURE (*err))
return NULL;
if (name == NULL)
return ucnv_open (NULL, err);
if (u_strlen (name) > MAX_CONVERTER_NAME_LENGTH)
{
*err = U_ILLEGAL_ARGUMENT_ERROR;
return NULL;
}
return ucnv_open (u_austrcpy (asciiName, name), err);
}
/*Decreases the reference counter in the shared immutable section of the object
*and frees the mutable part*/
void ucnv_close (UConverter * converter)
{
/* for iconv we will close the handles and free the converter storage*/
iconv_close(converter->sharedData->toiconv_handle);
iconv_close(converter->sharedData->fromiconv_handle);
if (converter == NULL)
return;
free (converter);
return;
}
/* currently required for iconv suuport */
/* XMLReader calls this and uses fact that it is different than min
to go thru a calculation otherwise if max and min same then there is
a calculation speed up - we will keep the two routines but have them
return different sizes - later will ifdef XMLreader for ICONV to remove the calls*/
int8_t ucnv_getMaxCharSize (const UConverter * converter)
{
return (4); /* dummy returns just need to be different in XMLParser - need something else for ICU replacement */
}
/* currently required for iconv support */
/* see note for ucnv_getMaxCharSize */
int8_t ucnv_getMinCharSize (const UConverter * converter)
{
return (1);
}
void ucnv_fromUnicode (UConverter * _this,
char **target,
const char *targetLimit,
const UChar ** source,
const UChar * sourceLimit,
int32_t* offsets,
int flush,
UErrorCode * err)
{
UCNV_TYPE myConvType;
/*
* Check parameters in for all conversions
*/
if (U_FAILURE (*err)) return;
if ((_this == NULL) || ((char *) targetLimit < *target) || (sourceLimit < *source))
{
*err = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
myConvType = _this->sharedData->conversionType;
/*calls the specific conversion routines */
Converter_fromUnicode(_this,target,targetLimit,source,sourceLimit,
offsets,flush,err);
return;
}
void ucnv_toUnicode (UConverter * _this,
UChar ** target,
const UChar * targetLimit,
const char **source,
const char *sourceLimit,
int32_t* offsets,
int flush,
UErrorCode * err)
{
/*
* Check parameters in for all conversions
*/
UCNV_TYPE myConvType;
if (U_FAILURE (*err)) return;
if ((_this == NULL) || ((UChar *) targetLimit < *target) || (sourceLimit < *source))
{
*err = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
myConvType = _this->sharedData->conversionType;
/*calls the specific conversion routines */
Convert_toUnicode(_this,target,targetLimit,source,sourceLimit,
offsets,flush,err);
return;
}
int32_t ucnv_fromUChars (const UConverter * converter,
char *target,
int32_t targetSize,
const UChar * source,
UErrorCode * err)
{
const UChar *mySource = source;
const UChar *mySource_limit;
int32_t mySourceLength = 0;
UConverter myConverter;
char *myTarget = target;
int32_t targetCapacity = 0;
if (U_FAILURE (*err))
return 0;
if ((converter == NULL) || (targetSize < 0))
{
*err = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
/*makes a local copy of the UConverter */
myConverter = *converter;
/*if the source is empty we return immediately */
mySourceLength = u_strlen (source);
if (mySourceLength == 0)
{
/*for consistency we still need to
*store 0 in the targetCapacity
*if the user requires it
*/
return 0;
}
mySource_limit = mySource + mySourceLength;
if (targetSize > 0)
{
ucnv_fromUnicode (&myConverter,
&myTarget,
target + targetSize,
&mySource,
mySource_limit,
NULL,
TRUE,
err);
targetCapacity = myTarget - target;
}
/*Updates targetCapacity to contain the number of bytes written to target */
if (targetSize == 0)
{
*err = U_INDEX_OUTOFBOUNDS_ERROR;
}
/* If the output buffer is exhausted, we need to stop writing
* to it but continue the conversion in order to store in targetSize
* the number of bytes that was required*/
if (*err == U_INDEX_OUTOFBOUNDS_ERROR)
{
char target2[CHUNK_SIZE];
char *target2_alias = target2;
const char *target2_limit = target2 + CHUNK_SIZE;
/*We use a stack allocated buffer around which we loop
*(in case the output is greater than CHUNK_SIZE)
*/
while (*err == U_INDEX_OUTOFBOUNDS_ERROR)
{
*err = U_ZERO_ERROR;
target2_alias = target2;
ucnv_fromUnicode (&myConverter,
&target2_alias,
target2_limit,
&mySource,
mySource_limit,
NULL,
TRUE,
err);
/*updates the output parameter to contain the number of char required */
targetCapacity += (target2_alias - target2) + 1;
}
/*We will set the erro code to BUFFER_OVERFLOW_ERROR only if
*nothing graver happened in the previous loop*/
(targetCapacity)--;
if (U_SUCCESS (*err))
*err = U_BUFFER_OVERFLOW_ERROR;
}
return targetCapacity;
}
int32_t ucnv_toUChars (const UConverter * converter,
UChar * target,
int32_t targetSize,
const char *source,
int32_t sourceSize,
UErrorCode * err)
{
const char *mySource = source;
const char *mySource_limit = source + sourceSize;
UConverter myConverter;
UChar *myTarget = target;
int32_t targetCapacity;
if (U_FAILURE (*err))
return 0;
if ((converter == NULL) || (targetSize < 0) || (sourceSize < 0))
{
*err = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
/*Means there is no work to be done */
if (sourceSize == 0)
{
/*for consistency we still need to
*store 0 in the targetCapacity
*if the user requires it
*/
if (targetSize >= 1)
{
target[0] = 0x0000;
return 1;
}
else
return 0;
}
/*makes a local copy of the UConverter */
myConverter = *converter;
/*Not in pure pre-flight mode */
if (targetSize > 0)
{
ucnv_toUnicode (&myConverter,
&myTarget,
target + (targetSize * 2) - 1, /*Save a spot for the Null terminator */
&mySource,
mySource_limit,
NULL,
TRUE,
err);
/*Null terminates the string */
*(myTarget) = 0x0000;
}
/*Rigs targetCapacity to have at least one cell for zero termination */
/*Updates targetCapacity to contain the number of bytes written to target */
targetCapacity = 1;
targetCapacity += myTarget - target;
if (targetSize == 0)
{
*err = U_INDEX_OUTOFBOUNDS_ERROR;
}
/* If the output buffer is exhausted, we need to stop writing
* to it but if the input buffer is not exhausted,
* we need to continue the conversion in order to store in targetSize
* the number of bytes that was required
*/
if (*err == U_INDEX_OUTOFBOUNDS_ERROR)
{
UChar target2[CHUNK_SIZE];
UChar *target2_alias = target2;
const UChar *target2_limit = target2 + CHUNK_SIZE;
/*We use a stack allocated buffer around which we loop
(in case the output is greater than CHUNK_SIZE) */
while (*err == U_INDEX_OUTOFBOUNDS_ERROR)
{
*err = U_ZERO_ERROR;
target2_alias = target2;
ucnv_toUnicode (&myConverter,
&target2_alias,
target2_limit,
&mySource,
mySource_limit,
NULL,
TRUE,
err);
/*updates the output parameter to contain the number of char required */
targetCapacity += target2_alias - target2 + 1;
}
(targetCapacity)--; /*adjust for last one */
if (U_SUCCESS (*err))
*err = U_BUFFER_OVERFLOW_ERROR;
}
return targetCapacity;
}
UChar ucnv_getNextUChar (UConverter * converter,
const char **source,
const char *sourceLimit,
UErrorCode * err)
{
/*calls the specific conversion routines */
/*as dictated in a code review, avoids a switch statement */
return getNextUChar(converter,source,sourceLimit,err);
}
/**************************
* Will convert a sequence of bytes from one codepage to another.
* @param toConverterName: The name of the converter that will be used to encode the output buffer
* @param fromConverterName: The name of the converter that will be used to decode the input buffer
* @param target: Pointer to the output buffer* written
* @param targetLength: on input contains the capacity of target, on output the number of bytes copied to target
* @param source: Pointer to the input buffer
* @param sourceLength: on input contains the capacity of source, on output the number of bytes processed in "source"
* @param internal: used internally to store store state data across calls
* @param err: fills in an error status
*/
void
T_UConverter_fromCodepageToCodepage (UConverter * outConverter,
UConverter * inConverter,
char **target,
const char *targetLimit,
const char **source,
const char *sourceLimit,
int32_t* offsets,
int flush,
UErrorCode * err)
{
UChar out_chunk[CHUNK_SIZE];
const UChar *out_chunk_limit = out_chunk + CHUNK_SIZE;
UChar *out_chunk_alias;
UChar const *out_chunk_alias2;
if (U_FAILURE (*err)) return;
/*loops until the input buffer is completely consumed
*or if an error has be encountered
*first we convert from inConverter codepage to Unicode
*then from Unicode to outConverter codepage
*/
while ((*source != sourceLimit) && U_SUCCESS (*err))
{
out_chunk_alias = out_chunk;
ucnv_toUnicode (inConverter,
&out_chunk_alias,
out_chunk_limit,
source,
sourceLimit,
NULL,
flush,
err);
/*INDEX_OUTOFBOUNDS_ERROR means that the output "CHUNK" is full
*we will require at least another loop (it's a recoverable error)
*/
if (U_SUCCESS (*err) || (*err == U_INDEX_OUTOFBOUNDS_ERROR))
{
*err = U_ZERO_ERROR;
out_chunk_alias2 = out_chunk;
while ((out_chunk_alias2 != out_chunk_alias) && U_SUCCESS (*err))
{
ucnv_fromUnicode (outConverter,
target,
targetLimit,
&out_chunk_alias2,
out_chunk_alias,
NULL,
TRUE,
err);
}
}
else
break;
}
return;
}
int32_t ucnv_convert(const char *toConverterName,
const char *fromConverterName,
char *target,
int32_t targetSize,
const char *source,
int32_t sourceSize,
UErrorCode * err)
{
const char *mySource = source;
const char *mySource_limit = source + sourceSize;
int32_t mySourceLength = 0;
UConverter *inConverter;
UConverter *outConverter;
char *myTarget = target;
int32_t targetCapacity = 0;
if (U_FAILURE (*err))
return 0;
if ((targetSize < 0) || (sourceSize < 0))
{
*err = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
/*if there is no input data, we're done */
if (sourceSize == 0)
{
/*in case the caller passed an output ptr
*we update it
*/
return 0;
}
/*create the converters */
inConverter = ucnv_open (fromConverterName, err);
if (U_FAILURE (*err)) return 0;
outConverter = ucnv_open (toConverterName, err);
if (U_FAILURE (*err))
{
ucnv_close (inConverter);
return 0;
}
if (targetSize > 0)
{
T_UConverter_fromCodepageToCodepage (outConverter,
inConverter,
&myTarget,
target + targetSize,
&mySource,
mySource_limit,
NULL,
TRUE,
err);
}
/*Updates targetCapacity to contain the number of bytes written to target */
targetCapacity = myTarget - target;
if (targetSize == 0)
{
*err = U_INDEX_OUTOFBOUNDS_ERROR;
}
/* If the output buffer is exhausted, we need to stop writing
* to it but continue the conversion in order to store in targetSize
* the number of bytes that was required*/
if (*err == U_INDEX_OUTOFBOUNDS_ERROR)
{
char target2[CHUNK_SIZE];
char *target2_alias = target2;
const char *target2_limit = target2 + CHUNK_SIZE;
/*We use a stack allocated buffer around which we loop
*(in case the output is greater than CHUNK_SIZE)
*/
while (*err == U_INDEX_OUTOFBOUNDS_ERROR)
{
*err = U_ZERO_ERROR;
target2_alias = target2;
T_UConverter_fromCodepageToCodepage (outConverter,
inConverter,
&target2_alias,
target2_limit,
&mySource,
mySource_limit,
NULL,
TRUE,
err);
/*updates the output parameter to contain the number of char required */
targetCapacity += (target2_alias - target2) + 1;
}
/*We will set the erro code to BUFFER_OVERFLOW_ERROR only if
*nothing graver happened in the previous loop*/
(targetCapacity)--;
if (U_SUCCESS (*err))
*err = U_BUFFER_OVERFLOW_ERROR;
}
ucnv_close (inConverter);
ucnv_close (outConverter);
return targetCapacity;
}
void Converter_fromUnicode(UConverter * _this,
char **target,
const char *targetLimit,
const UChar ** source,
const UChar * sourceLimit,
int32_t *offsets,
int flush,
UErrorCode * err)
{
int chardone;
const UChar *mySource = *source;
unsigned char *myTarget = (unsigned char *) *target;
int32_t mySourceIndex = 0;
int32_t myTargetIndex = 0;
int32_t targetLength = targetLimit - (char *) myTarget;
int32_t sourceLength = (sourceLimit - mySource) * 2;
unsigned char targetChar = 0x00;
/* pick up the iconv handle and perform the conversion */
errno = 0;
chardone =iconv(_this->sharedData->fromiconv_handle,(char**)source, (size_t*) &sourceLength,target,(size_t *)&targetLength);
if (errno!=0)
if (errno == E2BIG)
{
*err = U_INDEX_OUTOFBOUNDS_ERROR;
return;
}
else
if ((errno ==EBADDATA)|| (errno ==ECONVERT))
{
char errno_id[7];
send_message(NULL,ICONV_CONVERT_PROBLEM,'d');
convert_errno(errno_id,errno);
send_message(NULL,errno_id,'d');
*err = U_INVALID_CHAR_FOUND;
return;
}
*target += myTargetIndex;
*source += mySourceIndex;
return;
}
void Convert_toUnicode(UConverter * _this,
UChar ** target,
const UChar * targetLimit,
const char **source,
const char *sourceLimit,
int32_t *offsets,
int flush,
UErrorCode * err)
{
char *mySource = (char *) *source;
UChar *myTarget = *target;
int32_t mySourceIndex = 0;
int32_t myTargetIndex = 0;
int32_t targetLength = targetLimit - myTarget;
int32_t sourceLength = (sourceLimit - (char *) mySource);
UChar *myToUnicode = NULL;
UChar targetUniChar = 0x0000;
int chardone;
/* pick up the iconv handle */
errno = 0;
chardone =iconv(_this->sharedData->toiconv_handle,(char**)source, (size_t*) &sourceLength,(char **)target,(size_t *)&targetLength);
if (errno!=0)
{
if (errno == E2BIG)
{
*err = U_INDEX_OUTOFBOUNDS_ERROR;
return;
}
else
if ((errno ==EBADDATA)|| (errno ==ECONVERT))
{
char errno_id[7];
send_message(NULL,ICONV_CONVERT_PROBLEM,'d');
convert_errno(errno_id,errno);
send_message(NULL,errno_id,'d');
*err = U_INVALID_CHAR_FOUND;
return;
}
}
*target += myTargetIndex;
*source += mySourceIndex;
return;
}
UChar getNextUChar(UConverter* converter,
const char** source,
const char* sourceLimit,
UErrorCode* err)
{
UChar myUChar;
UChar* myUCharptr;
size_t numberibytes=sizeof(UChar);
size_t numberobytes=sizeof(UChar);
int chardone;
if ((*source)+1 > sourceLimit)
{
*err = U_INDEX_OUTOFBOUNDS_ERROR;
return 0xFFFD;
}
/*pick up the iconv handle */
/* convert the requested character - need to cache characters 6 will do - XMLReader is using this function to get header to process*/
myUCharptr = &myUChar;
chardone =iconv(converter->sharedData->toiconv_handle,(char**)source, (size_t*) &numberibytes,(char **)&myUCharptr,(size_t *)&numberobytes);
if (myUChar != 0xFFFD) return myUChar;
else
{
UChar* myUCharPtr = &myUChar;
const char* sourceFinal = *source;
*err = U_INVALID_CHAR_FOUND;
/*makes the internal caching transparent to the user*/
if (*err == U_INDEX_OUTOFBOUNDS_ERROR) *err = U_ZERO_ERROR;
return myUChar;
}
}
1.1 xml-xerces/c/src/util/Transcoders/Iconv400/iconv_util.c
Index: iconv_util.c
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 1999, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/**
* $Log: iconv_util.c,v $
* Revision 1.1 2000/02/10 18:08:28 abagchi
* Initial checkin
*
*/
#include <iconv.h>
#include <util/Platforms/OS400/OS400PlatformUtils.hpp>
#include <stdlib.h>
#include <unistd.h>
#include <iconv_util.h>
#include <iconv_cnv.h>
#include <ctype.h>
#define COPYRIGHT_STRING_LENGTH 200
#define MAX_STRLEN 0x00FFFFF
#define CHARCCSIDSIZE 5
static UConverter* _defaultConverter = NULL;
static UErrorCode gErr = U_ZERO_ERROR;
#include "utypes.h"
void shareConverterData (UConverterSharedData * data,char *InDescriptor) ;
UConverterSharedData *getSharedConverterData(char *Descriptor);
#define defaultConverter (_defaultConverter==NULL)?_defaultConverter=ucnv_open(NULL, &gErr):_defaultConverter
static char DEFAULT_CONVERTER_NAME[60] = "";
const char* iconv_getDefaultCodepage()
{
strcpy(DEFAULT_CONVERTER_NAME, "ibm037");
return DEFAULT_CONVERTER_NAME;
}
/* placeholder function to be deleted in 3_0 */
/* used by String.c */
int32_t
u_strlen(const UChar *s)
{
int32_t i = 0;
while(*s++)
i++;
return i;
}
/* note sure if needed - think that this is needed in cnv.c */
char* u_austrcpy(char *s1,
const UChar *ucs2 )
{
char * anchor = s1; /* save the start of result string */
UErrorCode err = U_ZERO_ERROR;
int32_t len;
len = ucnv_fromUChars(defaultConverter,
s1,
MAX_STRLEN,
ucs2,
&err);
s1[len] = '\0';
return s1;
}
/*Logic determines if the converter is Algorithmic AND/OR cached
*depending on that:
* -we either go to get data from disk and cache it (Data=TRUE, Cached=False)
* -Get it from a Hashphkramtable (Data=X, Cached=TRUE)
* -Call dataConverter initializer (Data=TRUE, Cached=TRUE)
* -Call AlgorithmicConverter initializer (Data=FALSE, Cached=TRUE)
*/
int QlgCvtTextDescToDesc(int,int,char *,int,char *,int,int);
UConverter *
createConverter (const char *converterName, UErrorCode * err)
{
char realName[MAX_CONVERTER_NAME_LENGTH];
char uppercased_converterName[MAX_CONVERTER_NAME_LENGTH];
UConverter *myUConverter = NULL;
UConverterSharedData *mySharedConverterData = NULL;
/* following parameters are passed to the convert text decscriptor
to descriptor */
int InType;
int OutType=1;
char *InDescriptor;
int InDescSize;
char OutDescriptor[CHARCCSIDSIZE]; /* 5 = size of a CCSID id on AS/400*/
int OutDescSize;
int JobCCSID=0;
char *inchar, *outchar; /* input and output conversion pointers*/
int tempchar;
if (U_FAILURE (*err))
return NULL;
/******************************************************************/
/* assume name is in EBCDIC. The convetername is assumed to be
/* either upper/lower case ebcdic and the text converter requires
/* upper case - Since we do not know we will convert to upper case
** just in case
/******************************************************************/
inchar = (char*)converterName;
outchar = &uppercased_converterName[0];
while (*inchar) /*except for null terminator uppercase inputs*/
{
tempchar = (int)(*inchar);
*outchar=(char)toupper(tempchar);
inchar ++;
outchar ++;
}
if (outchar -&uppercased_converterName[0]<=MAX_CONVERTER_NAME_LENGTH)
*outchar = '\0'; /* replace the terminator */
if(!strcmp(uppercased_converterName, "IBM-37")) /* We special-case this one */
{
memcpy(uppercased_converterName, "IBM-037", 7);
memset(uppercased_converterName + 7, '\0',1);
}
/******************************************************************/
/* convert name to AS/400 CCSID
** if CCSID is returned then we can complete building the converter
** otherwise we will return the same error as ICU converters
*/
/******************************************************************/
InType = 11; /*set for IANA initially */
if (QlgCvtTextDescToDesc(InType, OutType,(char *)uppercased_converterName, strlen(uppercased_converterName),&OutDescriptor[0],sizeof(OutDescriptor),JobCCSID)<0)
{
InType = 3; /* change to AIX 4.1 if we fail above */
if (QlgCvtTextDescToDesc(InType, OutType,(char *)uppercased_converterName, strlen(uppercased_converterName),&OutDescriptor[0],sizeof(OutDescriptor),JobCCSID)<0)
{
*err = U_INVALID_TABLE_FILE;
send_message((char *)converterName,ICONV_CCSID_PROBLEM,'d');
return NULL;
}
}
/*****************************************************************/
/* create a new converter */
/*****************************************************************/
myUConverter = createNewConverter(&OutDescriptor[0], err);
if (U_FAILURE (*err) || (myUConverter == NULL))
{
return NULL;
}
return myUConverter;
}
UConverter* createNewConverter(const char *name, UErrorCode *err)
{
char temp[33];
int32_t i = 0;
const int8_t *myByteArray = NULL;
const uint16_t *myIndexArray = NULL;
int32_t myValuesCount = 0;
int32_t myIndexCount = 0;
UConverter *myConverter = NULL;
int32_t myCheck;
int8_t errorLevel = 0;
char throwAway[COPYRIGHT_STRING_LENGTH];
char UNICODE_CCSID_ID_O[33]="IBMCCSID134880000000";
char UNICODE_CCSID_ID_I[33]="IBMCCSID13488";
char my_CCSID_ID[33]="\0";
char CONVERT_ID_START[9] ="IBMCCSID"; /* include null terminator for strcat */
char DEFAULTS[9] = "00000000";
if (U_FAILURE (*err))
return NULL;
/* create the base shell for the converter */
myConverter = (UConverter *) malloc (sizeof (UConverter));
if (myConverter == NULL)
{
*err = U_MEMORY_ALLOCATION_ERROR;
return NULL;
}
/* create the "shared area' and link into the shell */
myConverter->sharedData =
(UConverterSharedData *) malloc (sizeof (UConverterSharedData));
if (myConverter->sharedData == NULL)
{
free (myConverter);
*err = U_MEMORY_ALLOCATION_ERROR;
return NULL;
}
memset(my_CCSID_ID, '\0', 33); /* sizeof(my_CCSID_ID)); */
memcpy(my_CCSID_ID,&CONVERT_ID_START[0],strlen(CONVERT_ID_START)+1);
memcpy(&my_CCSID_ID[8],&name[0],5);
memcpy(&my_CCSID_ID[13],&DEFAULTS[0],7);
memset(temp, '\0', 33);
memcpy(temp, my_CCSID_ID, 13);
myConverter->sharedData->toiconv_handle = iconv_open(UNICODE_CCSID_ID_I,my_CCSID_ID);
/* memset(&my_CCSID_ID[13],'0',19); clear out the non necessary data to reverse */
my_CCSID_ID[13] = '\0';
myConverter->sharedData->fromiconv_handle = iconv_open(temp,UNICODE_CCSID_ID_O);
if ((myConverter->sharedData->toiconv_handle.return_value ==-1) ||(myConverter->sharedData->fromiconv_handle.return_value==-1))
{
/*If it isn't any of the above, the file is invalid */
*err = U_INVALID_TABLE_FILE;
free (myConverter->sharedData);
free (myConverter);
}
return myConverter;
}
1.1 xml-xerces/c/src/util/Transcoders/Iconv400/iconv_cnv.h
Index: iconv_cnv.h
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 1999, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/**
* $Log: iconv_cnv.h,v $
* Revision 1.1 2000/02/10 18:08:28 abagchi
* Initial checkin
*
*/
#ifndef UCNV_H
#define UCNV_H
#include "utypes.h"
/**
* Creates a UConverter object with the names specified as a C string.
* The actual name will be resolved with the alias file.
* if <TT>NULL</TT> is passed for the converter name, it will create one with the
* getDefaultName return value.
* @param converterName : name of the uconv table
* @param err outgoing error status <TT>MEMORY_ALLOCATION_ERROR, TABLE_NOT_FOUND</TT>
* @return the created Unicode converter object, or <TT>NULL</TT> if an error occured
* @see ucnv_openU
* @see ucnv_openCCSID
* @see ucnv_close
*/
U_CAPI
UConverter* U_EXPORT2 ucnv_open (const char *converterName,
UErrorCode * err);
/**
* Creates a Unicode converter with the names specified as unicode string. The name should be limited to
* the ASCII-7 alphanumerics range. The actual name will be resolved with the alias file.
* if <TT>NULL</TT> is passed for the converter name, it will create one with the
* getDefaultName return value.
* @param converterName : name of the uconv table in a zero terminated Unicode string
* @param err outgoing error status <TT>MEMORY_ALLOCATION_ERROR, TABLE_NOT_FOUND</TT>
* @return the created Unicode converter object, or <TT>NULL</TT> if an error occured
* @see ucnv_open
* @see ucnv_openCCSID
* @see ucnv_close
*/
U_CAPI UConverter* U_EXPORT2 ucnv_openU (const UChar * name,
UErrorCode * err);
/**
* Creates a UConverter object using a CCSID number.
* @param codepage : codepage # of the uconv table
* @param platform : codepage's platform (now only <TT>IBM</TT> supported)
* @param err error status <TT>MEMORY_ALLOCATION_ERROR, TABLE_NOT_FOUND</TT>
* @return the created Unicode converter object, or <TT>NULL</TT> if and error occured
* @see ucnv_open
* @see ucnv_openU
* @see ucnv_close
*/
U_CAPI void U_EXPORT2 ucnv_close (UConverter * converter);
/**
* Returns the maximum length of bytes used by a character. This varies between 1 and 4
* @param converter the Unicode converter
* @return the maximum number of bytes allowed by this particular converter
* @see ucnv_getMinCharSize
*/
U_CAPI int8_t U_EXPORT2
ucnv_getMaxCharSize (const UConverter * converter);
/**
* Returns the minimum byte length for characters in this codepage. This is either
* 1 or 2 for all supported codepages.
* @param converter the Unicode converter
* @return the minimum number of bytes allowed by this particular converter
* @see ucnv_getMaxCharSize
*/
U_CAPI int8_t U_EXPORT2
ucnv_getMinCharSize (const UConverter * converter);
/**
* Transcodes an array of unicode characters to an array of codepage characters.
* The source pointer is an I/O parameter, it starts out pointing where the function is
* to begin transcoding, and ends up pointing after the first sequence of the bytes
* that it encounters that are semantically invalid.
* if ucnv_setToUCallBack is called with an action other than <TT>STOP</TT>
* before a call is made to this API, <TT>consumed</TT> and <TT>source</TT> should point to the same place
* (unless <TT>target</TT> ends with an imcomplete sequence of bytes and <TT>flush</TT> is <TT>FALSE</TT>).
* the <TT>target</TT> buffer buffer needs to be a least the size of the maximum # of bytes per characters
* allowed by the target codepage.
* @param converter the Unicode converter
* @param converter the Unicode converter
* @param target : I/O parameter. Input : Points to the beginning of the buffer to copy
* codepage characters to. Output : points to after the last codepage character copied
* to <TT>target</TT>.
* @param targetLimit the pointer to the end of the <TT>target</TT> array
* @param source the source Unicode character array
* @param sourceLimit the pointer to the end of the source array
* @param offsets if NULL is passed, nothing will happen to it, otherwise it needs to have the same number
* of allocated cells as <TT>target</TT>. Will fill in offsets from target to source pointer
* e.g: <TT>offsets[3]</TT> is equal to 6, it means that the <TT>target[3]</TT> was a result of transcoding <TT>source[6]</TT>
* For output data carried across calls -1 will be placed for offsets.
* @param flush <TT>TRUE</TT> if the buffer is the last buffer of the conversion interation
* and the conversion will finish with this call, FALSE otherwise.
* @param err the error status. <TT>ILLEGAL_ARGUMENT_ERROR</TT> will be returned if the
* converter is <TT>NULL</TT>.
* @see ucnv_fromUChars
* @see ucnv_convert
* @see ucnv_getMinCharSize
* @see ucnv_setToUCallBack
*/
U_CAPI
void U_EXPORT2 ucnv_fromUnicode (UConverter * converter,
char **target,
const char *targetLimit,
const UChar ** source,
const UChar * sourceLimit,
int32_t* offsets,
int flush,
UErrorCode * err);
/**
* Converts an array of codepage characters into an array of unicode characters.
* The source pointer is an I/O parameter, it starts out pointing at the place
* to begin translating, and ends up pointing after the first sequence of the bytes
* that it encounters that are semantically invalid.
* if ucnv_setFromUCallBack is called with an action other than STOP
* before a call is made to this API, consumed and source should point to the same place
* (unless target ends with an imcomplete sequence of bytes and flush is FALSE).
* @param converter the Unicode converter
* @param target : I/O parameter. Input : Points to the beginning of the buffer to copy
* Unicode characters to. Output : points to after the last UChar copied to target.
* @param targetLimit the pointer to the end of the target array
* @param source the source codepage character array
* @param sourceLimit the pointer to the end of the source array
* @param offsets if NULL is passed, nothing will happen to it, otherwise it needs to have the same number
* of allocated cells as <TT>target</TT>. Will fill in offsets from target to source pointer
* e.g: <TT>offsets[3]</TT> is equal to 6, it means that the <TT>target[3]</TT> was a result of transcoding <TT>source[6]</TT>
* For output data carried across calls -1 will be placed for offsets.
* @param flush TRUE if the buffer is the last buffer and the conversion will finish
* in this call, FALSE otherwise.
* @param err the error code status <TT>ILLEGAL_ARGUMENT_ERROR</TT> will be returned if the
* converter is <TT>NULL</TT>, or if <TT>targetLimit</TT> and <TT>sourceLimit</TT> are misaligned.
* @see ucnv_toUChars
* @see ucnv_getNextUChar
* @see ucnv_convert
* @see ucnv_setFromUCallBack
*/
U_CAPI
void U_EXPORT2 ucnv_toUnicode (UConverter * converter,
UChar ** target,
const UChar * targetLimit,
const char **source,
const char *sourceLimit,
int32_t* offsets,
int flush,
UErrorCode * err);
/**
* Transcodes the source Unicode string to the target string in a codepage encoding
* with the specified Unicode converter. For example, if a Unicode to/from JIS
* converter is specified, the source string in Unicode will be transcoded to JIS
* encoding. The result will be stored in JIS encoding.
* if any problems during conversion are encountered it will SUBSTITUTE with the default (initial)
* substitute characters.
* This function is a more convenient but less efficient version of \Ref{ucnv_fromUnicode}.
* @param converter the Unicode converter
* @param source the <TT>source</TT> Unicode string (zero Terminated)
* @param target the <TT>target</TT> string in codepage encoding (<STRONG>not zero-terminated</STRONG> because some
* codepage do not use '\0' as a string terminator
* @param targetCapacity Input the number of bytes available in the <TT>target</TT> buffer
* @param err the error status code.
* <TT>INDEX_OUTOFBOUNDS_ERROR</TT> will be returned if the
* the # of bytes provided are not enough for transcoding.
* <TT>ILLEGAL_ARGUMENT_ERROR</TT> is returned if the converter is <TT>NULL</TT> or the source or target string is empty.
* <TT>BUFFER_OVERFLOW_ERROR</TT> when <TT>targetSize</TT> turns out to be bigger than <TT>targetCapacity</TT>
* @return number of bytes needed in target, regardless of <TT>targetCapacity</TT>
* @see ucnv_fromUnicode
* @see ucnv_convert
*/
U_CAPI
int32_t U_EXPORT2 ucnv_fromUChars (const UConverter * converter,
char *target,
int32_t targetCapacity,
const UChar * source,
UErrorCode * err);
/**
* Transcode the source string in codepage encoding to the target string in
* Unicode encoding. For example, if a Unicode to/from JIS
* converter is specified, the source string in JIS encoding will be transcoded
* to Unicode and placed into a provided target buffer.
* if any problems during conversion are encountered it will SUBSTITUTE with the Unicode REPLACEMENT char
* We recomment, the size of the target buffer needs to be at least as long as the maximum # of bytes per char
* in this character set.
* A zero-terminator will be placed at the end of the target buffer
* This function is a more convenient but less efficient version of \Ref{ucnv_toUnicode}.
* @param converter the Unicode converter
* @param source the source string in codepage encoding
* @param target the target string in Unicode encoding
* @param targetCapacity capacity of the target buffer
* @param sourceSize : Number of bytes in <TT>source</TT> to be transcoded
* @param err the error status code
* <TT>MEMORY_ALLOCATION_ERROR</TT> will be returned if the
* the internal process buffer cannot be allocated for transcoding.
* <TT>ILLEGAL_ARGUMENT_ERROR</TT> is returned if the converter is <TT>NULL</TT> or
* if the source or target string is empty.
* <TT>BUFFER_OVERFLOW_ERROR</TT> when the input buffer is prematurely exhausted and targetSize non-<TT>NULL</TT>.
* @return the number of UChar needed in target (including the zero terminator)
* @see ucnv_getNextUChar
* @see ucnv_toUnicode
* @see ucnv_convert
*/
U_CAPI
int32_t U_EXPORT2 ucnv_toUChars (const UConverter * converter,
UChar * target,
int32_t targetCapacity,
const char *source,
int32_t sourceSize,
UErrorCode * err);
/********************************
* Will convert a codepage buffer one character at a time.
* This function was written to be efficient when transcoding small amounts of data at a time.
* In that case it will be more efficient than \Ref{ucnv_toUnicode}.
* When converting large buffers use \Ref{ucnv_toUnicode}.
*@param converter an open UConverter
*@param source the address of a pointer to the codepage buffer, will be updated to point after
*the bytes consumed in the conversion call.
*@param points to the end of the input buffer
*@param err fills in error status (see ucnv_toUnicode)
*@return a UChar resulting from the partial conversion of source
*@see ucnv_toUnicode
*@see ucnv_toUChars
*@see ucnv_convert
*/
U_CAPI
UChar U_EXPORT2 ucnv_getNextUChar (UConverter * converter,
const char **source,
const char *sourceLimit,
UErrorCode * err);
/**************************
* Will convert a sequence of bytes from one codepage to another.
* This is <STRONG>NOT AN EFFICIENT</STRONG> way to transcode.
* use \Ref{ucnv_toUnicode} and \Ref{ucnv_fromUnicode} for efficiency
* @param toConverterName: The name of the converter that will be used to encode the output buffer
* @param fromConverterName: The name of the converter that will be used to decode the input buffer
* @param target: Pointer to the output buffer to write to
* @param targetCapacity: on input contains the capacity of target
* @param source: Pointer to the input buffer
* @param sourceLength: on input contains the capacity of source
* @param err: fills in an error status
* @return will be filled in with the number of bytes needed in target
* @see ucnv_fromUnicode
* @see ucnv_toUnicode
* @see ucnv_fromUChars
* @see ucnv_toUChars
* @see ucnv_getNextUChar
*/
U_CAPI
int32_t U_EXPORT2 ucnv_convert (const char *toConverterName,
const char *fromConverterName,
char *target,
int32_t targetCapacity,
const char *source,
int32_t sourceLength,
UErrorCode * err);
#endif
1.1 xml-xerces/c/src/util/Transcoders/Iconv400/iconv_util.h
Index: iconv_util.h
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 1999, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/**
* $Log: iconv_util.h,v $
* Revision 1.1 2000/02/10 18:08:28 abagchi
* Initial checkin
*
*/
const char* iconv_getDefaultCodepage(void);
#include "utypes.h"
#include <iconv.h>
int QlgCvtTextDescToDesc (int, int, char *, int, char*, int, int);
#define MAX_CONVERTER_NAME_LENGTH 60
#define MAX_SUBCHAR_LEN 4
#define ERROR_BUFFER_LENGTH 20
typedef enum {UNKNOWN = -1, IBM = 0}
UCNV_PLATFORM;
typedef enum
{
UNSUPPORTED_CONVERTER = -1,
SBCS = 0,
DBCS = 1,
MBCS = 2,
LATIN_1 = 3,
UTF8 = 4,
UTF16_BigEndian = 5,
UTF16_LittleEndian = 6,
EBCDIC_STATEFUL = 7,
ISO_2022 = 8,
JIS = 9,
EUC = 10,
GB = 11
}
UCNV_TYPE;
int32_t
u_strlen(const UChar *s);
/* note sure if needed - think that this is needed in cnv.c */
char* u_austrcpy(char *s1,
const UChar *ucs2 );
/*Defines the struct of a UConverterSharedData the immutable, shared part of
*UConverter
*/
typedef struct
{
uint32_t referenceCounter; /*used to count number of clients */
char name[MAX_CONVERTER_NAME_LENGTH]; /*internal name of the converter */
UCNV_PLATFORM platform; /*platform of the converter (only IBM now */
int32_t codepage; /*codepage # (now IBM-$codepage) */
UCNV_TYPE conversionType; /*conversion type */
int8_t minBytesPerChar; /*Minimum # bytes per char in this codepage */
int8_t maxBytesPerChar; /*Maximum # bytes per char in this codepage */
struct
{ /*initial values of some members of the mutable part of object */
int8_t subCharLen;
unsigned char subChar[MAX_SUBCHAR_LEN];
} defaultConverterValues ;
iconv_t toiconv_handle ; /* handle to convert to unicode*/
iconv_t fromiconv_handle; /* handle to convert from unicode*/
}
UConverterSharedData;
/*Defines a UConverter, the lightweight mutable part the user sees */
struct UConverter
{
int8_t pad;
int32_t mode;
int8_t subCharLen; /*length of the codepage specific character sequence */
unsigned char subChar[MAX_SUBCHAR_LEN]; /*codepage specific character sequence */
UConverterSharedData *sharedData; /*Pointer to the shared immutable part of the
*converter object
*/
};
typedef struct UConverter UConverter;
UConverter* createNewConverter(const char *name, UErrorCode *err);
/*Initializes the mutable lightweight portion of the object
*By copying data from UConverter->sharedData->defaultConverter
*/
static void initializeDataConverter (UConverter * myUConverter);
UConverter *createConverter (const char *converterName, UErrorCode * err);
1.1 xml-xerces/c/src/util/Transcoders/Iconv400/pos400.h
Index: pos400.h
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 1999, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/**
* $Log: pos400.h,v $
* Revision 1.1 2000/02/10 18:08:28 abagchi
* Initial checkin
*
*/
/* Define the platform we're on. */
#ifndef OS400
#define OS400
#endif
/* Define whether inttypes.h is available */
#define HAVE_INTTYPES_H 0
/* Determines whether specific types are available */
#define HAVE_INT8_T 0
#define HAVE_UINT8_T 0
#define HAVE_INT16_T 0
#define HAVE_UINT16_T 0
#define HAVE_INT32_T 0
#define HAVE_UINT32_T 0
#define HAVE_BOOL_T 0
/* Determines the endianness of the platform */
#define U_IS_BIG_ENDIAN 1
/*===========================================================================*/
/* Generic data types */
/*===========================================================================*/
/* If your platform does not have the <inttypes.h> header, you may
need to edit the typedefs below. */
#if HAVE_INTTYPES_H
#include <inttypes.h>
#else
#if ! HAVE_INT8_T
typedef signed char int8_t;
#endif
#if ! HAVE_UINT8_T
typedef unsigned char uint8_t;
#endif
#if ! HAVE_INT16_T
typedef signed short int16_t;
#endif
#if ! HAVE_UINT16_T
typedef unsigned short uint16_t;
#endif
#if ! HAVE_INT32_T
typedef signed long int32_t;
#endif
#if ! HAVE_UINT32_T
typedef unsigned long uint32_t;
#endif
#endif
#include <limits.h>
#define T_INT32_MAX (LONG_MAX)
/*===========================================================================*/
/* Symbol import-export control */
/*===========================================================================*/
#define U_EXPORT
#define U_EXPORT2
#define U_IMPORT
1.1 xml-xerces/c/src/util/Transcoders/Iconv400/utypes.h
Index: utypes.h
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 1999, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/**
* $Log: utypes.h,v $
* Revision 1.1 2000/02/10 18:08:28 abagchi
* Initial checkin
*
*/
#ifndef UTYPES_H
#define UTYPES_H
#include <wchar.h>
#include <stdlib.h>
/*===========================================================================*/
/* Include platform-dependent definitions */
/* which are contained in the platform-specific file platform.h */
/*===========================================================================*/
#include "pos400.h"
/* XP_CPLUSPLUS is a cross-platform symbol which should be defined when
using C++. It should not be defined when compiling under C. */
#ifdef __cplusplus
# ifndef XP_CPLUSPLUS
# define XP_CPLUSPLUS
# endif
#else
# undef XP_CPLUSPLUS
#endif
/*===========================================================================*/
/* Boolean data type */
/*===========================================================================*/
#if ! HAVE_BOOL_T
typedef int8_t bool_t;
#endif
#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif
/*===========================================================================*/
/* Unicode string offset */
/*===========================================================================*/
typedef int32_t UTextOffset;
/*===========================================================================*/
/* Unicode character */
/*===========================================================================*/
typedef uint16_t UChar;
/*===========================================================================*/
/* For C wrappers, we use the symbol U_CAPI. */
/* This works properly if the includer is C or C++. */
/* Functions are declared U_CAPI return-type U_EXPORT2 function-name() ... */
/*===========================================================================*/
#ifdef XP_CPLUSPLUS
# define U_CFUNC extern "C"
# define U_CDECL_BEGIN extern "C" {
# define U_CDECL_END }
#else
# define U_CFUNC
# define U_CDECL_BEGIN
# define U_CDECL_END
#endif
#define U_CAPI U_CFUNC U_EXPORT
/* Define NULL pointer value if it isn't already defined */
#ifndef NULL
#ifdef XP_CPLUSPLUS
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
/* Maximum value of a (void*) - use to indicate the limit of
an 'infinite' buffer. */
#define U_MAX_PTR ((void*)-1)
/*===========================================================================*/
/* UErrorCode */
/*===========================================================================*/
/** Error code to replace exception handling */
#ifdef __OS400__
enum UErrorCode1 {
#else
enum UErrorCode {
#endif
U_ERROR_INFO_START = -128, /* Start of information results (semantically successful) */
U_USING_FALLBACK_ERROR = -128,
U_USING_DEFAULT_ERROR = -127,
U_ERROR_INFO_LIMIT,
U_ZERO_ERROR = 0, /* success */
U_ILLEGAL_ARGUMENT_ERROR = 1, /* Start of codes indicating failure */
U_MISSING_RESOURCE_ERROR = 2,
U_INVALID_FORMAT_ERROR = 3,
U_FILE_ACCESS_ERROR = 4,
U_INTERNAL_PROGRAM_ERROR = 5, /* Indicates a bug in the library code */
U_MESSAGE_PARSE_ERROR = 6,
U_MEMORY_ALLOCATION_ERROR = 7, /* Memory allocation error */
U_INDEX_OUTOFBOUNDS_ERROR = 8,
U_PARSE_ERROR = 9, /* Equivalent to Java ParseException */
U_INVALID_CHAR_FOUND = 10, /* In the Character conversion routines: Invalid character or sequence was encountered*/
U_TRUNCATED_CHAR_FOUND = 11, /* In the Character conversion routines: More bytes are required to complete the conversion successfully*/
U_ILLEGAL_CHAR_FOUND = 12, /* In codeset conversion: a sequence that does NOT belong in the codepage has been encountered*/
U_INVALID_TABLE_FORMAT = 13, /* Conversion table file found, but corrupted*/
U_INVALID_TABLE_FILE = 14, /* Conversion table file not found*/
U_BUFFER_OVERFLOW_ERROR = 15, /* A result would not fit in the supplied buffer */
U_UNSUPPORTED_ERROR = 16, /* Requested operation not supported in current context */
U_ERROR_LIMIT
};
#ifdef __OS400__
typedef int UErrorCode;
#define U_SUCCESS(x) ((x)<=U_ZERO_ERROR)
#define U_FAILURE(x) ((x)>U_ZERO_ERROR)
#else
#ifndef XP_CPLUSPLUS
typedef enum UErrorCode UErrorCode;
#endif
#endif
/* Use the following to determine if an UErrorCode represents */
/* operational success or failure. */
#ifndef __OS400__
#ifdef XP_CPLUSPLUS
inline bool_t U_SUCCESS(UErrorCode code) { return (bool_t)(code<=U_ZERO_ERROR); }
inline bool_t U_FAILURE(UErrorCode code) { return (bool_t)(code>U_ZERO_ERROR); }
#else
#define U_SUCCESS(x) ((x)<=U_ZERO_ERROR)
#define U_FAILURE(x) ((x)>U_ZERO_ERROR)
#endif
#endif
/* Casting function for int32_t (backward compatibility version, here until
T_INT32 is replaced) */
#define T_INT32(i) ((int32_t)i)
/*===========================================================================*/
/* Debugging */
/*===========================================================================*/
/* remove this */
/* This function is useful for debugging; it returns the text name */
/* of an UErrorCode result. This is not the most efficient way of */
/* doing this but it's just for Debug builds anyway. */
/* Do not use these arrays directly: they will move to a .c file! */
static const char *
_uErrorInfoName[U_ERROR_INFO_LIMIT-U_ERROR_INFO_START]={
"U_USING_FALLBACK_ERROR",
"U_USING_DEFAULT_ERROR"
};
static const char *
_uErrorName[U_ERROR_LIMIT]={
"U_ZERO_ERROR",
"U_ILLEGAL_ARGUMENT_ERROR",
"U_MISSING_RESOURCE_ERROR",
"U_INVALID_FORMAT_ERROR",
"U_FILE_ACCESS_ERROR",
"U_INTERNAL_PROGRAM_ERROR",
"U_MESSAGE_PARSE_ERROR",
"U_MEMORY_ALLOCATION_ERROR",
"U_INDEX_OUTOFBOUNDS_ERROR",
"U_PARSE_ERROR",
"U_INVALID_CHAR_FOUND",
"U_TRUNCATED_CHAR_FOUND",
"U_ILLEGAL_CHAR_FOUND",
"U_INVALID_TABLE_FORMAT",
"U_INVALID_TABLE_FILE",
"U_BUFFER_OVERFLOW_ERROR",
"U_UNSUPPORTED_ERROR"
};
#ifdef XP_CPLUSPLUS
inline const char *
errorName(UErrorCode code)
{
if(code>=0 && code<U_ERROR_LIMIT) {
return _uErrorName[code];
} else if(code>=U_ERROR_INFO_START && code<U_ERROR_INFO_LIMIT) {
return _uErrorInfoName[code-U_ERROR_INFO_START];
} else {
return "[BOGUS UErrorCode]";
}
}
#else
# define errorName(code) \
((code)>=0 && (code)<U_ERROR_LIMIT) ? \
_uErrorName[code] : \
((code)>=U_ERROR_INFO_START && (code)<U_ERROR_INFO_LIMIT) ? \
_uErrorInfoName[code-U_ERROR_INFO_START] : \
"[BOGUS UErrorCode]"
#endif
#endif /* _UTYPES */
1.1 xml-xerces/c/src/util/Transcoders/Iconv400/Iconv400TransService.cpp
Index: Iconv400TransService.cpp
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 1999, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/**
* $Log: Iconv400TransService.cpp,v $
* Revision 1.1 2000/02/10 18:08:28 abagchi
* Initial checkin
*
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#include <util/TranscodingException.hpp>
#include "Iconv400TransService.hpp"
#include <string.h>
#include <qlgcase.h>
#include "iconv_cnv.h"
#include "iconv_util.h"
#include <qusec.h>
int32_t u_strlen(const XMLCh *s)
{
int32_t i=0;
while(*s++)
i++;
return i;
}
// ---------------------------------------------------------------------------
// Local, const data
// ---------------------------------------------------------------------------
static const XMLCh gMyServiceId[] =
{
chLatin_I, chLatin_C, chLatin_O, chLatin_V, chDigit_4, chDigit_0, chDigit_0, chNull
};
// ---------------------------------------------------------------------------
// IconvTransService: Constructors and Destructor
// ---------------------------------------------------------------------------
Iconv400TransService::Iconv400TransService()
{
memset((char*)&convertCtlblk,'\0',sizeof(convertCtlblk));
convertCtlblk.Type_of_Request = 1;
convertCtlblk.Case_Request = 0;
convertCtlblk.CCSID_of_Input_Data = 13488;
}
Iconv400TransService::~Iconv400TransService()
{
}
// ---------------------------------------------------------------------------
// Iconv400TransService: The virtual transcoding service API
// ---------------------------------------------------------------------------
int Iconv400TransService::compareIString(const XMLCh* const comp1
, const XMLCh* const comp2)
{
const XMLCh* psz1 = comp1;
const XMLCh* psz2 = comp2;
unsigned int curCount = 0;
while (true)
{
if (toUnicodeUpper(*psz1) != toUnicodeUpper(*psz2))
return int(*psz1) - int(*psz2);
// If either has ended, then they both ended, so equal
if (!*psz1 || !*psz2)
break;
// Move upwards for the next round
psz1++;
psz2++;
}
return 0;
}
int Iconv400TransService::compareNIString(const XMLCh* const comp1
, const XMLCh* const comp2
, const unsigned int maxChars)
{
const XMLCh* psz1 = comp1;
const XMLCh* psz2 = comp2;
unsigned int curCount = 0;
while (true)
{
// If an inequality, then return the difference
// If an inequality, then return difference
if (toUnicodeUpper(*psz1) != toUnicodeUpper(*psz2))
return int(*psz1) - int(*psz2);
// If either ended, then both ended, so equal
if (!*psz1 || !*psz2)
break;
// Move upwards to next chars
psz1++;
psz2++;
//
// Bump the count of chars done. If it equals the count then we
// are equal for the requested count, so break out and return
// equal.
//
curCount++;
if (maxChars == curCount)
break;
}
return 0;
}
const XMLCh* Iconv400TransService::getId() const
{
return gMyServiceId;
}
bool Iconv400TransService::isSpace(const XMLCh toCheck) const
{
// The following are Unicode Space characters
//
if ((toCheck == 0x09)
|| (toCheck == 0x0A)
|| (toCheck == 0x0D)
|| (toCheck == 0x20)
|| (toCheck == 0xA0)
|| ((toCheck >= 0x2000) && (toCheck <= 0x200B))
|| (toCheck == 0x3000)
|| (toCheck == 0xFEFF))
{
return true;
}
else return false;
}
XMLLCPTranscoder* Iconv400TransService::makeNewLCPTranscoder()
{
//
// Try to create a default converter. If it fails, return a null pointer
// which will basically cause the system to give up because we really can't
// do anything without one.
//
UErrorCode uerr = U_ZERO_ERROR;
UConverter* converter = ucnv_open(NULL, &uerr);
if (!converter)
return 0;
// That went ok, so create an Iconv LCP transcoder wrapper and return it
return new Iconv400LCPTranscoder(converter);
}
bool Iconv400TransService::supportsSrcOfs() const
{
// This implementation supports source offset information
return true;
}
void Iconv400TransService::upperCase(XMLCh* const toUpperCase) const
{
XMLCh* outPtr = toUpperCase;
while (*outPtr)
{
*outPtr = toUnicodeUpper(*outPtr);
outPtr++;
}
}
// ---------------------------------------------------------------------------
// Iconv400TransService: The virtual transcoding service API
// ---------------------------------------------------------------------------
XMLCh Iconv400TransService::toUnicodeUpper(XMLCh comp1) const
{
XMLCh chRet;
struct {
int bytes_available;
int bytes_used;
char exception_id[7];
char reserved;
char exception_data[15];
} error_code;
error_code.bytes_available = sizeof(error_code);
long charlen =2;
QlgConvertCase((char*)&convertCtlblk,
(char*)&comp1,
(char*)&chRet,
(long*)&charlen,
(char*)&error_code);
return chRet;
}
// ---------------------------------------------------------------------------
// Iconv400TransService: The protected virtual transcoding service API
// ---------------------------------------------------------------------------
XMLTranscoder*
Iconv400TransService::makeNewXMLTranscoder( const XMLCh* const encodingName
, XMLTransService::Codes& resValue
, const unsigned int blockSize)
{
UErrorCode uerr = U_ZERO_ERROR;
UConverter* converter = ucnv_openU(encodingName, &uerr);
if (!converter)
{
resValue = XMLTransService::UnsupportedEncoding;
return 0;
}
return new Iconv400Transcoder(encodingName, converter, blockSize);
}
// ---------------------------------------------------------------------------
// IconvTranscoder: Constructors and Destructor
// ---------------------------------------------------------------------------
Iconv400Transcoder::Iconv400Transcoder(const XMLCh* const encodingName
, UConverter* const toAdopt
, const unsigned int blockSize) :
XMLTranscoder(encodingName, blockSize)
, fConverter(toAdopt)
, fFixed(false)
, fSrcOffsets(0)
{
// If there is a block size, then allocate our source offset array
if (blockSize)
fSrcOffsets = new long[blockSize];
// Remember if its a fixed size encoding
fFixed = (ucnv_getMaxCharSize(fConverter) == ucnv_getMinCharSize(fConverter));
}
Iconv400Transcoder::~Iconv400Transcoder()
{
delete [] fSrcOffsets;
// If there is a converter, ask Iconv400 to clean it up
if (fConverter)
{
// <TBD> Does this actually delete the structure???
ucnv_close(fConverter);
fConverter = 0;
}
}
XMLCh Iconv400Transcoder::transcodeOne( const XMLByte* const srcData
, const unsigned int srcBytes
, unsigned int& bytesEaten)
{
// Check for stupid stuff
if (!srcBytes)
return 0;
UErrorCode err = U_ZERO_ERROR;
const XMLByte* startSrc = srcData;
const XMLCh chRet = ucnv_getNextUChar
(
fConverter
, (const char**)&startSrc
, (const char*)((srcData + srcBytes) - 1)
, &err
);
// Bail out if an error
if (U_FAILURE(err))
return 0;
// Calculate the bytes eaten and return the char
bytesEaten = startSrc - srcData;
return chRet;
}
unsigned int
Iconv400Transcoder::transcodeXML(const XMLByte* const srcData
, const unsigned int srcCount
, XMLCh* const toFill
, const unsigned int maxChars
, unsigned int& bytesEaten
, unsigned char* const charSizes)
{
// If debugging, insure the block size is legal
#if defined(XML4C_DEBUG)
checkBlockSize(maxChars);
#endif
// Set up pointers to the source and destination buffers.
UChar* startTarget = toFill;
const XMLByte* startSrc = srcData;
const XMLByte* endSrc = srcData + srcCount;
//
// Transoode the buffer. Buffer overflow errors are normal, occuring
// when the raw input buffer holds more characters than will fit in
// the Unicode output buffer.
//
UErrorCode err = U_ZERO_ERROR;
ucnv_toUnicode
(
fConverter
, &startTarget
, toFill + maxChars
, (const char**)&startSrc
, (const char*)endSrc
, (fFixed ? 0 : fSrcOffsets)
, false
, &err
);
if ((err != U_ZERO_ERROR) && (err != U_INDEX_OUTOFBOUNDS_ERROR))
ThrowXML(TranscodingException, XML4CExcepts::Trans_CouldNotXCodeXMLData);
// Calculate the bytes eaten and store in caller's param
bytesEaten = startSrc - srcData;
// And the characters decoded
const unsigned int charsDecoded = startTarget - toFill;
//
// Translate the array of char offsets into an array of character
// sizes, which is what the transcoder interface semantics requires.
// If its fixed, then we can optimize it.
//
if (fFixed)
{
const unsigned char fillSize = (unsigned char)ucnv_getMaxCharSize(fConverter);;
memset(charSizes, fillSize, maxChars);
}
else
{
//
// We have to convert the series of offsets into a series of
// sizes. If just one char was decoded, then its the total bytes
// eaten. Otherwise, do a loop and subtract out each element from
// its previous element.
//
if (charsDecoded == 1)
{
charSizes[0] = (unsigned char)bytesEaten;
}
else
{
// <TBD> Does Iconv return an extra element to allow us to figure
// out the last char size? It better!!
unsigned int index;
for (index = 0; index < charsDecoded; index++)
{
charSizes[index] = (unsigned char)(fSrcOffsets[index + 1]
- fSrcOffsets[index]);
}
}
}
// Return the chars we put into the target buffer
return charsDecoded;
}
// ---------------------------------------------------------------------------
// IconvLCPTranscoder: Constructors and Destructor
// ---------------------------------------------------------------------------
Iconv400LCPTranscoder::Iconv400LCPTranscoder(UConverter* const toAdopt) :
fConverter(toAdopt)
{
}
Iconv400LCPTranscoder::~Iconv400LCPTranscoder()
{
// If there is a converter, ask Iconv to clean it up
if (fConverter)
{
// <TBD> Does this actually delete the structure???
ucnv_close(fConverter);
fConverter = 0;
}
}
// ---------------------------------------------------------------------------
// Iconv400LCPTranscoder: Constructors and Destructor
// ---------------------------------------------------------------------------
unsigned int Iconv400LCPTranscoder::calcRequiredSize(const XMLCh* const srcText)
{
if (!srcText)
return 0;
// Lock and attempt the calculation
UErrorCode err = U_ZERO_ERROR;
int32_t targetCap;
{
XMLMutexLock lockConverter(&fMutex);
targetCap = ucnv_fromUChars
(
fConverter
, 0
, 0
, srcText
, &err
);
}
if (err != U_BUFFER_OVERFLOW_ERROR)
return 0;
return (unsigned int)targetCap;
}
unsigned int Iconv400LCPTranscoder::calcRequiredSize(const char* const srcText)
{
if (!srcText)
return 0;
int32_t targetCap;
UErrorCode err = U_ZERO_ERROR;
{
XMLMutexLock lockConverter(&fMutex);
targetCap = ucnv_toUChars
(
fConverter
, 0
, 0
, srcText
, strlen(srcText)
, &err
);
}
if (err != U_BUFFER_OVERFLOW_ERROR)
return 0;
// Subtract one since it includes the terminator space
return (unsigned int)(targetCap - 1);
}
char* Iconv400LCPTranscoder::transcode(const XMLCh* const toTranscode)
{
char* retBuf = 0;
// Check for a couple of special cases
if (!toTranscode)
return 0;
if (!*toTranscode)
{
retBuf = new char[1];
retBuf[0] = 0;
return retBuf;
}
// Caculate a return buffer size not too big, but less likely to overflow
int32_t targetLen = (int32_t)(u_strlen(toTranscode) * 1.25);
// Allocate the return buffer
retBuf = new char[targetLen + 1];
// Lock now while we call the converter.
UErrorCode err = U_ZERO_ERROR;
int32_t targetCap;
{
XMLMutexLock lockConverter(&fMutex);
//Convert the Unicode string to char*
targetCap = ucnv_fromUChars
(
fConverter
, retBuf
, targetLen + 1
, toTranscode
, &err
);
}
// If targetLen is not enough then buffer overflow might occur
if (err == U_BUFFER_OVERFLOW_ERROR)
{
// Reset the error, delete the old buffer, allocate a new one, and try again
err = U_ZERO_ERROR;
delete [] retBuf;
retBuf = new char[targetCap];
// Lock again before we retry
XMLMutexLock lockConverter(&fMutex);
targetCap = ucnv_fromUChars
(
fConverter
, retBuf
, targetCap
, toTranscode
, &err
);
}
if (U_FAILURE(err))
{
delete [] retBuf;
return 0;
}
// Cap it off and return
retBuf[targetCap] = 0;
return retBuf;
}
XMLCh* Iconv400LCPTranscoder::transcode(const char* const toTranscode)
{
// Watch for a few pyscho corner cases
if (!toTranscode)
return 0;
XMLCh* retVal = 0;
if (!*toTranscode)
{
retVal = new XMLCh[1];
retVal[0] = 0;
return retVal;
}
//
// Get the length of the string to transcode. The Unicode string will
// almost always be no more chars than were in the source, so this is
// the best guess as to the storage needed.
//
const int32_t srcLen = (int32_t)strlen(toTranscode);
// Now lock while we do these calculations
UErrorCode err = U_ZERO_ERROR;
{
XMLMutexLock lockConverter(&fMutex);
//
// Here we don't know what the target length will be so use 0 and
// expect an U_BUFFER_OVERFLOW_ERROR in which case it'd get resolved
// by the correct capacity value.
//
int32_t targetCap;
targetCap = ucnv_toUChars
(
fConverter
, 0
, 0
, toTranscode
, srcLen
, &err
);
if (err != U_BUFFER_OVERFLOW_ERROR)
return 0;
err = U_ZERO_ERROR;
retVal = new XMLCh[targetCap];
ucnv_toUChars
(
fConverter
, retVal
, targetCap
, toTranscode
, srcLen
, &err
);
}
if (U_FAILURE(err))
{
// Clean up if we got anything allocated
delete [] retVal;
return 0;
}
return retVal;
}
bool Iconv400LCPTranscoder::transcode(const char* const toTranscode
, XMLCh* const toFill
, const unsigned int maxChars)
{
// Check for a couple of psycho corner cases
if (!toTranscode || !maxChars)
{
toFill[0] = 0;
return true;
}
if (!*toTranscode)
{
toFill[0] = 0;
return true;
}
// Lock and do the transcode operation
UErrorCode err = U_ZERO_ERROR;
const int32_t srcLen = (int32_t)strlen(toTranscode);
{
XMLMutexLock lockConverter(&fMutex);
ucnv_toUChars
(
fConverter
, toFill
, maxChars + 1
, toTranscode
, srcLen
, &err
);
}
if (U_FAILURE(err))
return false;
return true;
}
bool Iconv400LCPTranscoder::transcode( const XMLCh* const toTranscode
, char* const toFill
, const unsigned int maxChars)
{
// Watch for a few psycho corner cases
if (!toTranscode || !maxChars)
{
toFill[0] = 0;
return true;
}
if (!*toTranscode)
{
toFill[0] = 0;
return true;
}
UErrorCode err = U_ZERO_ERROR;
int32_t targetCap;
{
XMLMutexLock lockConverter(&fMutex);
targetCap = ucnv_fromUChars
(
fConverter
, toFill
, maxChars + 1
, toTranscode
, &err
);
}
if (U_FAILURE(err))
return false;
toFill[targetCap] = 0;
return true;
}
1.1 xml-xerces/c/src/util/Transcoders/Iconv400/Iconv400TransService.hpp
Index: Iconv400TransService.hpp
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 1999, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/**
* $Log: Iconv400TransService.hpp,v $
* Revision 1.1 2000/02/10 18:08:28 abagchi
* Initial checkin
*
*/
#ifndef ICONV400TRANSSERVICE_HPP
#define ICONV400TRANSSERVICE_HPP
#include <util/Mutexes.hpp>
#include <util/TransService.hpp>
#include <qlg.h>
struct UConverter;
class XMLUTIL_EXPORT Iconv400TransService : public XMLTransService
{
public :
// -----------------------------------------------------------------------
// Public, static methods
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
Iconv400TransService();
~Iconv400TransService();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoding service API
// -----------------------------------------------------------------------
virtual int compareIString
(
const XMLCh* const comp1
, const XMLCh* const comp2
);
virtual int compareNIString
(
const XMLCh* const comp1
, const XMLCh* const comp2
, const unsigned int maxChars
);
virtual const XMLCh* getId() const;
virtual bool isSpace(const XMLCh toCheck) const;
virtual XMLLCPTranscoder* makeNewLCPTranscoder();
virtual bool supportsSrcOfs() const;
virtual void upperCase(XMLCh* const toUpperCase) const;
protected :
// -----------------------------------------------------------------------
// Protected virtual methods
// -----------------------------------------------------------------------
virtual XMLTranscoder* makeNewXMLTranscoder
(
const XMLCh* const encodingName
, XMLTransService::Codes& resValue
, const unsigned int blockSize
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
Iconv400TransService(const Iconv400TransService&);
void operator=(const Iconv400TransService&);
XMLCh toUnicodeUpper(XMLCh toupper) const;
Qlg_CCSID_ReqCtlBlk_T convertCtlblk;
};
class XMLUTIL_EXPORT Iconv400Transcoder : public XMLTranscoder
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
Iconv400Transcoder
(
const XMLCh* const encodingName
, UConverter* const toAdopt
, const unsigned int blockSize
);
~Iconv400Transcoder();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoder interface
// -----------------------------------------------------------------------
virtual XMLCh transcodeOne
(
const XMLByte* const srcData
, const unsigned int srcBytes
, unsigned int& bytesEaten
);
virtual unsigned int transcodeXML
(
const XMLByte* const srcData
, const unsigned int srcCount
, XMLCh* const toFill
, const unsigned int maxChars
, unsigned int& bytesEaten
, unsigned char* const charSizes
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
Iconv400Transcoder();
Iconv400Transcoder(const Iconv400Transcoder&);
void operator=(const Iconv400Transcoder&);
// -----------------------------------------------------------------------
// Private data members
//
// fConverter
// This is a pointer to the Iconv400 converter that this transcoder
// uses.
//
// fFixed
// This is set to true if the encoding is a fixed size one. This
// can be used to optimize some operations.
//
// fSrcOffsets
// This is an array of longs, which are allocated to the size of
// the trancoding block (if any) indicated in the ctor. It is used
// to get the character offsets from Iconv400, which are then translated
// into an array of char sizes for return.
// -----------------------------------------------------------------------
UConverter* fConverter;
bool fFixed;
long* fSrcOffsets;
};
class XMLUTIL_EXPORT Iconv400LCPTranscoder : public XMLLCPTranscoder
{
public :
// -----------------------------------------------------------------------
// Constructors and Destructor
// -----------------------------------------------------------------------
Iconv400LCPTranscoder(UConverter* const toAdopt);
~Iconv400LCPTranscoder();
// -----------------------------------------------------------------------
// Implementation of the virtual transcoder interface
// -----------------------------------------------------------------------
virtual unsigned int calcRequiredSize(const char* const srcText);
virtual unsigned int calcRequiredSize(const XMLCh* const srcText);
virtual char* transcode(const XMLCh* const toTranscode);
virtual XMLCh* transcode(const char* const toTranscode);
virtual bool transcode
(
const char* const toTranscode
, XMLCh* const toFill
, const unsigned int maxChars
);
virtual bool transcode
(
const XMLCh* const toTranscode
, char* const toFill
, const unsigned int maxChars
);
private :
// -----------------------------------------------------------------------
// Unimplemented constructors and operators
// -----------------------------------------------------------------------
Iconv400LCPTranscoder();
Iconv400LCPTranscoder(const Iconv400LCPTranscoder&);
void operator=(const Iconv400LCPTranscoder&);
// -----------------------------------------------------------------------
// Private data members
//
// fConverter
// This is a pointer to the Iconv converter that this transcoder
// uses.
//
// fMutex
// We have to synchronize threaded calls to the converter.
// -----------------------------------------------------------------------
UConverter* fConverter;
XMLMutex fMutex;
};
#endif
1.1 xml-xerces/c/src/util/Transcoders/Iconv400/Makefile.in
Index: Makefile.in
===================================================================
#
# The Apache Software License, Version 1.1
#
# Copyright (c) 1999-2000 The Apache Software Foundation. All rights
# reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. The end-user documentation included with the redistribution,
# if any, must include the following acknowledgment:
# "This product includes software developed by the
# Apache Software Foundation (http://www.apache.org/)."
# Alternately, this acknowledgment may appear in the software itself,
# if and wherever such third-party acknowledgments normally appear.
#
# 4. The names "Xerces" and "Apache Software Foundation" must
# not be used to endorse or promote products derived from this
# software without prior written permission. For written
# permission, please contact apache\@apache.org.
#
# 5. Products derived from this software may not be called "Apache",
# nor may "Apache" appear in their name, without prior written
# permission of the Apache Software Foundation.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
# ====================================================================
#
#
# This software consists of voluntary contributions made by many
# individuals on behalf of the Apache Software Foundation, and was
# originally based on software copyright (c) 1999, International
# Business Machines, Inc., http://www.ibm.com . For more information
# on the Apache Software Foundation, please see
# <http://www.apache.org/>.
#
#
# $Log: Makefile.in,v $
# Revision 1.1 2000/02/10 18:08:28 abagchi
# Initial checkin
#
#
PLATFORM = @platform@
COMPILER = @compiler@
CXXFLAGS = @cxxflags@
CFLAGS = @cflags@
PREFIX = @prefix@
LDFLAGS = @ldflags@
LIBS = @libs@
OSVER = @osver@
USELIBWWW = @uselibwww@
MESSAGELOADER = @messageloader@
TRANSCODER = @transcoder@
MODULE = util
SUBMODULE = Transcoders/Iconv400
include ../../../Makefile.incl
CPP_PUBHEADERS = Iconv400TransService.hpp
CPP_OBJECTS = Iconv400TransService.$(TO) \
iconv_util.$(TO) \
iconv_cnv.$(TO)
include ../../Makefile.util.submodule