2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2000-07-10 20:51:54 +00:00
|
|
|
/*
|
2000-02-07 17:35:21 +00:00
|
|
|
**********************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2000-2015, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2000-02-07 17:35:21 +00:00
|
|
|
**********************************************************************
|
|
|
|
* file name: ucnvlat1.cpp
|
2017-02-03 18:57:23 +00:00
|
|
|
* encoding: UTF-8
|
2000-02-07 17:35:21 +00:00
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 2000feb07
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2004-08-26 22:51:40 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_CONVERSION
|
|
|
|
|
2000-12-20 01:22:02 +00:00
|
|
|
#include "unicode/ucnv.h"
|
2003-04-05 01:33:02 +00:00
|
|
|
#include "unicode/uset.h"
|
2011-07-27 05:53:56 +00:00
|
|
|
#include "unicode/utf8.h"
|
2000-06-27 20:47:56 +00:00
|
|
|
#include "ucnv_bld.h"
|
2000-02-07 17:35:21 +00:00
|
|
|
#include "ucnv_cnv.h"
|
2017-09-23 06:34:53 +00:00
|
|
|
#include "ustr_imp.h"
|
2000-02-07 17:35:21 +00:00
|
|
|
|
2001-01-17 19:10:28 +00:00
|
|
|
/* control optimizations according to the platform */
|
|
|
|
#define LATIN1_UNROLL_FROM_UNICODE 1
|
|
|
|
|
2000-02-07 17:35:21 +00:00
|
|
|
/* ISO 8859-1 --------------------------------------------------------------- */
|
|
|
|
|
2004-09-16 04:00:12 +00:00
|
|
|
/* This is a table-less and callback-less version of ucnv_MBCSSingleToBMPWithOffsets(). */
|
2017-02-11 02:43:09 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
static void U_CALLCONV
|
2000-12-20 01:22:02 +00:00
|
|
|
_Latin1ToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
const uint8_t *source;
|
|
|
|
UChar *target;
|
|
|
|
int32_t targetCapacity, length;
|
|
|
|
int32_t *offsets;
|
|
|
|
|
2001-01-17 19:10:28 +00:00
|
|
|
int32_t sourceIndex;
|
|
|
|
|
2000-12-20 01:22:02 +00:00
|
|
|
/* set up the local pointers */
|
|
|
|
source=(const uint8_t *)pArgs->source;
|
|
|
|
target=pArgs->target;
|
2004-12-08 01:05:40 +00:00
|
|
|
targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
|
2001-01-17 19:10:28 +00:00
|
|
|
offsets=pArgs->offsets;
|
|
|
|
|
|
|
|
sourceIndex=0;
|
2000-12-20 01:22:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* since the conversion here is 1:1 UChar:uint8_t, we need only one counter
|
|
|
|
* for the minimum of the sourceLength and targetCapacity
|
|
|
|
*/
|
2004-12-08 01:05:40 +00:00
|
|
|
length=(int32_t)((const uint8_t *)pArgs->sourceLimit-source);
|
2000-12-20 01:22:02 +00:00
|
|
|
if(length<=targetCapacity) {
|
|
|
|
targetCapacity=length;
|
|
|
|
} else {
|
|
|
|
/* target will be full */
|
|
|
|
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
length=targetCapacity;
|
|
|
|
}
|
|
|
|
|
2007-06-10 06:36:08 +00:00
|
|
|
if(targetCapacity>=8) {
|
|
|
|
/* This loop is unrolled for speed and improved pipelining. */
|
2001-01-17 19:10:28 +00:00
|
|
|
int32_t count, loops;
|
|
|
|
|
2007-06-10 06:36:08 +00:00
|
|
|
loops=count=targetCapacity>>3;
|
|
|
|
length=targetCapacity&=0x7;
|
2001-01-17 19:10:28 +00:00
|
|
|
do {
|
2007-06-10 06:36:08 +00:00
|
|
|
target[0]=source[0];
|
|
|
|
target[1]=source[1];
|
|
|
|
target[2]=source[2];
|
|
|
|
target[3]=source[3];
|
|
|
|
target[4]=source[4];
|
|
|
|
target[5]=source[5];
|
|
|
|
target[6]=source[6];
|
|
|
|
target[7]=source[7];
|
|
|
|
target+=8;
|
|
|
|
source+=8;
|
2001-01-17 19:10:28 +00:00
|
|
|
} while(--count>0);
|
|
|
|
|
|
|
|
if(offsets!=NULL) {
|
|
|
|
do {
|
2007-06-10 06:36:08 +00:00
|
|
|
offsets[0]=sourceIndex++;
|
|
|
|
offsets[1]=sourceIndex++;
|
|
|
|
offsets[2]=sourceIndex++;
|
|
|
|
offsets[3]=sourceIndex++;
|
|
|
|
offsets[4]=sourceIndex++;
|
|
|
|
offsets[5]=sourceIndex++;
|
|
|
|
offsets[6]=sourceIndex++;
|
|
|
|
offsets[7]=sourceIndex++;
|
|
|
|
offsets+=8;
|
2001-01-17 19:10:28 +00:00
|
|
|
} while(--loops>0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-20 01:22:02 +00:00
|
|
|
/* conversion loop */
|
|
|
|
while(targetCapacity>0) {
|
|
|
|
*target++=*source++;
|
|
|
|
--targetCapacity;
|
2000-02-07 17:35:21 +00:00
|
|
|
}
|
2000-12-20 01:22:02 +00:00
|
|
|
|
|
|
|
/* write back the updated pointers */
|
|
|
|
pArgs->source=(const char *)source;
|
|
|
|
pArgs->target=target;
|
|
|
|
|
|
|
|
/* set offsets */
|
|
|
|
if(offsets!=NULL) {
|
|
|
|
while(length>0) {
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
--length;
|
|
|
|
}
|
|
|
|
pArgs->offsets=offsets;
|
2000-02-07 17:35:21 +00:00
|
|
|
}
|
2000-12-20 01:22:02 +00:00
|
|
|
}
|
2000-02-07 17:35:21 +00:00
|
|
|
|
2004-09-16 04:00:12 +00:00
|
|
|
/* This is a table-less and callback-less version of ucnv_MBCSSingleGetNextUChar(). */
|
2017-02-11 02:43:09 +00:00
|
|
|
static UChar32 U_CALLCONV
|
2000-12-20 01:22:02 +00:00
|
|
|
_Latin1GetNextUChar(UConverterToUnicodeArgs *pArgs,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
const uint8_t *source=(const uint8_t *)pArgs->source;
|
|
|
|
if(source<(const uint8_t *)pArgs->sourceLimit) {
|
|
|
|
pArgs->source=(const char *)(source+1);
|
|
|
|
return *source;
|
|
|
|
}
|
2000-02-07 17:35:21 +00:00
|
|
|
|
2000-12-20 01:22:02 +00:00
|
|
|
/* no output because of empty input */
|
|
|
|
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
return 0xffff;
|
2000-02-07 17:35:21 +00:00
|
|
|
}
|
|
|
|
|
2004-09-16 04:00:12 +00:00
|
|
|
/* This is a table-less version of ucnv_MBCSSingleFromBMPWithOffsets(). */
|
2017-02-11 02:43:09 +00:00
|
|
|
static void U_CALLCONV
|
2000-12-20 01:22:02 +00:00
|
|
|
_Latin1FromUnicodeWithOffsets(UConverterFromUnicodeArgs *pArgs,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
UConverter *cnv;
|
2003-07-22 04:22:57 +00:00
|
|
|
const UChar *source, *sourceLimit;
|
|
|
|
uint8_t *target, *oldTarget;
|
2000-12-20 01:22:02 +00:00
|
|
|
int32_t targetCapacity, length;
|
|
|
|
int32_t *offsets;
|
|
|
|
|
2003-07-22 04:22:57 +00:00
|
|
|
UChar32 cp;
|
|
|
|
UChar c, max;
|
2000-12-20 01:22:02 +00:00
|
|
|
|
|
|
|
int32_t sourceIndex;
|
|
|
|
|
|
|
|
/* set up the local pointers */
|
|
|
|
cnv=pArgs->converter;
|
|
|
|
source=pArgs->source;
|
|
|
|
sourceLimit=pArgs->sourceLimit;
|
2003-07-22 04:22:57 +00:00
|
|
|
target=oldTarget=(uint8_t *)pArgs->target;
|
2004-12-08 01:05:40 +00:00
|
|
|
targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
|
2000-12-20 01:22:02 +00:00
|
|
|
offsets=pArgs->offsets;
|
|
|
|
|
2000-12-20 02:08:39 +00:00
|
|
|
if(cnv->sharedData==&_Latin1Data) {
|
|
|
|
max=0xff; /* Latin-1 */
|
|
|
|
} else {
|
|
|
|
max=0x7f; /* US-ASCII */
|
|
|
|
}
|
2000-12-20 01:22:02 +00:00
|
|
|
|
|
|
|
/* get the converter state from UConverter */
|
2003-07-24 00:28:47 +00:00
|
|
|
cp=cnv->fromUChar32;
|
2000-12-20 01:22:02 +00:00
|
|
|
|
|
|
|
/* sourceIndex=-1 if the current character began in the previous buffer */
|
2003-07-22 04:22:57 +00:00
|
|
|
sourceIndex= cp==0 ? 0 : -1;
|
2000-12-20 01:22:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* since the conversion here is 1:1 UChar:uint8_t, we need only one counter
|
|
|
|
* for the minimum of the sourceLength and targetCapacity
|
|
|
|
*/
|
2004-12-08 01:05:40 +00:00
|
|
|
length=(int32_t)(sourceLimit-source);
|
2000-12-20 01:22:02 +00:00
|
|
|
if(length<targetCapacity) {
|
|
|
|
targetCapacity=length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* conversion loop */
|
2003-07-22 04:22:57 +00:00
|
|
|
if(cp!=0 && targetCapacity>0) {
|
2000-12-20 01:22:02 +00:00
|
|
|
goto getTrail;
|
|
|
|
}
|
|
|
|
|
2001-01-17 19:10:28 +00:00
|
|
|
#if LATIN1_UNROLL_FROM_UNICODE
|
|
|
|
/* unroll the loop with the most common case */
|
|
|
|
if(targetCapacity>=16) {
|
|
|
|
int32_t count, loops;
|
|
|
|
UChar u, oredChars;
|
|
|
|
|
|
|
|
loops=count=targetCapacity>>4;
|
|
|
|
do {
|
|
|
|
oredChars=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
oredChars|=u=*source++;
|
|
|
|
*target++=(uint8_t)u;
|
|
|
|
|
|
|
|
/* were all 16 entries really valid? */
|
|
|
|
if(oredChars>max) {
|
|
|
|
/* no, return to the first of these 16 */
|
|
|
|
source-=16;
|
|
|
|
target-=16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(--count>0);
|
|
|
|
count=loops-count;
|
|
|
|
targetCapacity-=16*count;
|
|
|
|
|
|
|
|
if(offsets!=NULL) {
|
2003-07-22 04:22:57 +00:00
|
|
|
oldTarget+=16*count;
|
2001-01-17 19:10:28 +00:00
|
|
|
while(count>0) {
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
--count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-07-22 04:22:57 +00:00
|
|
|
/* conversion loop */
|
|
|
|
c=0;
|
|
|
|
while(targetCapacity>0 && (c=*source++)<=max) {
|
|
|
|
/* convert the Unicode code point */
|
|
|
|
*target++=(uint8_t)c;
|
|
|
|
--targetCapacity;
|
|
|
|
}
|
|
|
|
|
2003-10-15 21:55:06 +00:00
|
|
|
if(c>max) {
|
2003-07-22 04:22:57 +00:00
|
|
|
cp=c;
|
|
|
|
if(!U_IS_SURROGATE(cp)) {
|
|
|
|
/* callback(unassigned) */
|
|
|
|
} else if(U_IS_SURROGATE_LEAD(cp)) {
|
2000-12-20 01:22:02 +00:00
|
|
|
getTrail:
|
2003-07-22 04:22:57 +00:00
|
|
|
if(source<sourceLimit) {
|
|
|
|
/* test the following code unit */
|
|
|
|
UChar trail=*source;
|
|
|
|
if(U16_IS_TRAIL(trail)) {
|
|
|
|
++source;
|
|
|
|
cp=U16_GET_SUPPLEMENTARY(cp, trail);
|
|
|
|
/* this codepage does not map supplementary code points */
|
|
|
|
/* callback(unassigned) */
|
2000-12-20 01:22:02 +00:00
|
|
|
} else {
|
2003-07-22 04:22:57 +00:00
|
|
|
/* this is an unmatched lead code unit (1st surrogate) */
|
|
|
|
/* callback(illegal) */
|
2000-12-20 01:22:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2003-07-22 04:22:57 +00:00
|
|
|
/* no more input */
|
2003-07-24 00:28:47 +00:00
|
|
|
cnv->fromUChar32=cp;
|
2003-10-15 21:55:06 +00:00
|
|
|
goto noMoreInput;
|
2000-12-20 01:22:02 +00:00
|
|
|
}
|
2003-07-22 04:22:57 +00:00
|
|
|
} else {
|
|
|
|
/* this is an unmatched trail code unit (2nd surrogate) */
|
|
|
|
/* callback(illegal) */
|
|
|
|
}
|
2000-12-20 01:22:02 +00:00
|
|
|
|
2003-07-22 04:22:57 +00:00
|
|
|
*pErrorCode= U_IS_SURROGATE(cp) ? U_ILLEGAL_CHAR_FOUND : U_INVALID_CHAR_FOUND;
|
2003-07-24 00:28:47 +00:00
|
|
|
cnv->fromUChar32=cp;
|
2000-12-20 01:22:02 +00:00
|
|
|
}
|
2003-10-15 21:55:06 +00:00
|
|
|
noMoreInput:
|
2000-02-07 17:35:21 +00:00
|
|
|
|
2003-07-22 04:22:57 +00:00
|
|
|
/* set offsets since the start */
|
2000-12-20 01:22:02 +00:00
|
|
|
if(offsets!=NULL) {
|
2003-07-22 04:22:57 +00:00
|
|
|
size_t count=target-oldTarget;
|
2000-12-20 01:22:02 +00:00
|
|
|
while(count>0) {
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
--count;
|
|
|
|
}
|
|
|
|
}
|
2000-02-07 17:35:21 +00:00
|
|
|
|
2003-07-22 04:22:57 +00:00
|
|
|
if(U_SUCCESS(*pErrorCode) && source<sourceLimit && target>=(uint8_t *)pArgs->targetLimit) {
|
|
|
|
/* target is full */
|
|
|
|
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
|
2000-02-07 17:35:21 +00:00
|
|
|
}
|
2000-06-02 00:05:22 +00:00
|
|
|
|
2000-12-20 01:22:02 +00:00
|
|
|
/* write back the updated pointers */
|
|
|
|
pArgs->source=source;
|
|
|
|
pArgs->target=(char *)target;
|
|
|
|
pArgs->offsets=offsets;
|
2000-02-07 17:35:21 +00:00
|
|
|
}
|
|
|
|
|
2007-02-06 05:24:14 +00:00
|
|
|
/* Convert UTF-8 to Latin-1. Adapted from ucnv_SBCSFromUTF8(). */
|
2017-02-11 02:43:09 +00:00
|
|
|
static void U_CALLCONV
|
2007-02-06 05:24:14 +00:00
|
|
|
ucnv_Latin1FromUTF8(UConverterFromUnicodeArgs *pFromUArgs,
|
|
|
|
UConverterToUnicodeArgs *pToUArgs,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
UConverter *utf8;
|
|
|
|
const uint8_t *source, *sourceLimit;
|
|
|
|
uint8_t *target;
|
|
|
|
int32_t targetCapacity;
|
|
|
|
|
|
|
|
UChar32 c;
|
|
|
|
uint8_t b, t1;
|
|
|
|
|
|
|
|
/* set up the local pointers */
|
|
|
|
utf8=pToUArgs->converter;
|
|
|
|
source=(uint8_t *)pToUArgs->source;
|
|
|
|
sourceLimit=(uint8_t *)pToUArgs->sourceLimit;
|
|
|
|
target=(uint8_t *)pFromUArgs->target;
|
|
|
|
targetCapacity=(int32_t)(pFromUArgs->targetLimit-pFromUArgs->target);
|
|
|
|
|
|
|
|
/* get the converter state from the UTF-8 UConverter */
|
2018-01-23 21:32:36 +00:00
|
|
|
if (utf8->toULength > 0) {
|
|
|
|
c=(UChar32)utf8->toUnicodeStatus;
|
|
|
|
} else {
|
|
|
|
c = 0;
|
|
|
|
}
|
2007-02-06 05:24:14 +00:00
|
|
|
if(c!=0 && source<sourceLimit) {
|
|
|
|
if(targetCapacity==0) {
|
|
|
|
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
return;
|
|
|
|
} else if(c>=0xc2 && c<=0xc3 && (t1=(uint8_t)(*source-0x80)) <= 0x3f) {
|
|
|
|
++source;
|
|
|
|
*target++=(uint8_t)(((c&3)<<6)|t1);
|
|
|
|
--targetCapacity;
|
|
|
|
|
|
|
|
utf8->toUnicodeStatus=0;
|
|
|
|
utf8->toULength=0;
|
|
|
|
} else {
|
|
|
|
/* complicated, illegal or unmappable input: fall back to the pivoting implementation */
|
|
|
|
*pErrorCode=U_USING_DEFAULT_WARNING;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure that the last byte sequence before sourceLimit is complete
|
|
|
|
* or runs into a lead byte.
|
|
|
|
* In the conversion loop compare source with sourceLimit only once
|
|
|
|
* per multi-byte character.
|
|
|
|
* For Latin-1, adjust sourceLimit only for 1 trail byte because
|
|
|
|
* the conversion loop handles at most 2-byte sequences.
|
|
|
|
*/
|
|
|
|
if(source<sourceLimit && U8_IS_LEAD(*(sourceLimit-1))) {
|
|
|
|
--sourceLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* conversion loop */
|
|
|
|
while(source<sourceLimit) {
|
|
|
|
if(targetCapacity>0) {
|
|
|
|
b=*source++;
|
2017-09-23 06:34:53 +00:00
|
|
|
if(U8_IS_SINGLE(b)) {
|
2007-02-06 05:24:14 +00:00
|
|
|
/* convert ASCII */
|
|
|
|
*target++=(uint8_t)b;
|
|
|
|
--targetCapacity;
|
|
|
|
} else if( /* handle U+0080..U+00FF inline */
|
|
|
|
b>=0xc2 && b<=0xc3 &&
|
|
|
|
(t1=(uint8_t)(*source-0x80)) <= 0x3f
|
|
|
|
) {
|
|
|
|
++source;
|
|
|
|
*target++=(uint8_t)(((b&3)<<6)|t1);
|
|
|
|
--targetCapacity;
|
|
|
|
} else {
|
|
|
|
/* complicated, illegal or unmappable input: fall back to the pivoting implementation */
|
|
|
|
pToUArgs->source=(char *)(source-1);
|
|
|
|
pFromUArgs->target=(char *)target;
|
|
|
|
*pErrorCode=U_USING_DEFAULT_WARNING;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* target is full */
|
|
|
|
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The sourceLimit may have been adjusted before the conversion loop
|
|
|
|
* to stop before a truncated sequence.
|
|
|
|
* If so, then collect the truncated sequence now.
|
|
|
|
* For Latin-1, there is at most exactly one lead byte because of the
|
|
|
|
* smaller sourceLimit adjustment logic.
|
|
|
|
*/
|
|
|
|
if(U_SUCCESS(*pErrorCode) && source<(sourceLimit=(uint8_t *)pToUArgs->sourceLimit)) {
|
|
|
|
utf8->toUnicodeStatus=utf8->toUBytes[0]=b=*source++;
|
|
|
|
utf8->toULength=1;
|
2017-09-23 06:34:53 +00:00
|
|
|
utf8->mode=U8_COUNT_BYTES(b);
|
2007-02-06 05:24:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* write back the updated pointers */
|
|
|
|
pToUArgs->source=(char *)source;
|
|
|
|
pFromUArgs->target=(char *)target;
|
|
|
|
}
|
|
|
|
|
2017-02-11 02:43:09 +00:00
|
|
|
static void U_CALLCONV
|
2003-04-05 01:33:02 +00:00
|
|
|
_Latin1GetUnicodeSet(const UConverter *cnv,
|
2004-12-02 04:18:35 +00:00
|
|
|
const USetAdder *sa,
|
2003-04-05 01:33:02 +00:00
|
|
|
UConverterUnicodeSet which,
|
|
|
|
UErrorCode *pErrorCode) {
|
2016-12-07 21:14:27 +00:00
|
|
|
(void)cnv;
|
|
|
|
(void)which;
|
|
|
|
(void)pErrorCode;
|
2004-09-07 17:59:53 +00:00
|
|
|
sa->addRange(sa->set, 0, 0xff);
|
2003-04-05 01:33:02 +00:00
|
|
|
}
|
2017-02-11 02:43:09 +00:00
|
|
|
U_CDECL_END
|
|
|
|
|
2003-04-05 01:33:02 +00:00
|
|
|
|
2000-02-09 19:15:17 +00:00
|
|
|
static const UConverterImpl _Latin1Impl={
|
2000-02-07 17:35:21 +00:00
|
|
|
UCNV_LATIN_1,
|
|
|
|
|
2000-02-08 23:41:16 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
2000-12-20 01:22:02 +00:00
|
|
|
_Latin1ToUnicodeWithOffsets,
|
|
|
|
_Latin1ToUnicodeWithOffsets,
|
|
|
|
_Latin1FromUnicodeWithOffsets,
|
|
|
|
_Latin1FromUnicodeWithOffsets,
|
|
|
|
_Latin1GetNextUChar,
|
2000-02-09 19:15:17 +00:00
|
|
|
|
2000-11-27 17:40:41 +00:00
|
|
|
NULL,
|
2003-04-05 01:33:02 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2007-02-06 05:24:14 +00:00
|
|
|
_Latin1GetUnicodeSet,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
ucnv_Latin1FromUTF8
|
2000-02-07 17:35:21 +00:00
|
|
|
};
|
|
|
|
|
2000-12-20 02:08:39 +00:00
|
|
|
static const UConverterStaticData _Latin1StaticData={
|
2000-12-19 23:07:50 +00:00
|
|
|
sizeof(UConverterStaticData),
|
2001-09-28 00:29:40 +00:00
|
|
|
"ISO-8859-1",
|
2000-04-19 23:05:27 +00:00
|
|
|
819, UCNV_IBM, UCNV_LATIN_1, 1, 1,
|
2000-12-20 02:08:39 +00:00
|
|
|
{ 0x1a, 0, 0, 0 }, 1, FALSE, FALSE,
|
2000-12-19 23:07:50 +00:00
|
|
|
0,
|
2001-02-26 19:51:20 +00:00
|
|
|
0,
|
|
|
|
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */
|
2000-04-19 23:05:27 +00:00
|
|
|
};
|
|
|
|
|
2015-05-29 19:33:24 +00:00
|
|
|
const UConverterSharedData _Latin1Data=
|
|
|
|
UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_Latin1StaticData, &_Latin1Impl);
|
2000-12-20 02:08:39 +00:00
|
|
|
|
|
|
|
/* US-ASCII ----------------------------------------------------------------- */
|
|
|
|
|
2017-02-11 02:43:09 +00:00
|
|
|
U_CDECL_BEGIN
|
2004-09-16 04:00:12 +00:00
|
|
|
/* This is a table-less version of ucnv_MBCSSingleToBMPWithOffsets(). */
|
2017-02-11 02:43:09 +00:00
|
|
|
static void U_CALLCONV
|
2000-12-20 02:08:39 +00:00
|
|
|
_ASCIIToUnicodeWithOffsets(UConverterToUnicodeArgs *pArgs,
|
|
|
|
UErrorCode *pErrorCode) {
|
2003-07-22 04:22:57 +00:00
|
|
|
const uint8_t *source, *sourceLimit;
|
|
|
|
UChar *target, *oldTarget;
|
2000-12-20 02:08:39 +00:00
|
|
|
int32_t targetCapacity, length;
|
|
|
|
int32_t *offsets;
|
|
|
|
|
|
|
|
int32_t sourceIndex;
|
|
|
|
|
2003-07-22 04:22:57 +00:00
|
|
|
uint8_t c;
|
|
|
|
|
2000-12-20 02:08:39 +00:00
|
|
|
/* set up the local pointers */
|
|
|
|
source=(const uint8_t *)pArgs->source;
|
|
|
|
sourceLimit=(const uint8_t *)pArgs->sourceLimit;
|
2003-07-22 04:22:57 +00:00
|
|
|
target=oldTarget=pArgs->target;
|
2004-12-08 01:05:40 +00:00
|
|
|
targetCapacity=(int32_t)(pArgs->targetLimit-pArgs->target);
|
2000-12-20 02:08:39 +00:00
|
|
|
offsets=pArgs->offsets;
|
|
|
|
|
|
|
|
/* sourceIndex=-1 if the current character began in the previous buffer */
|
|
|
|
sourceIndex=0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* since the conversion here is 1:1 UChar:uint8_t, we need only one counter
|
|
|
|
* for the minimum of the sourceLength and targetCapacity
|
|
|
|
*/
|
2004-12-08 01:05:40 +00:00
|
|
|
length=(int32_t)(sourceLimit-source);
|
2000-12-20 02:08:39 +00:00
|
|
|
if(length<targetCapacity) {
|
|
|
|
targetCapacity=length;
|
|
|
|
}
|
|
|
|
|
2007-06-10 06:36:08 +00:00
|
|
|
if(targetCapacity>=8) {
|
|
|
|
/* This loop is unrolled for speed and improved pipelining. */
|
2001-01-17 19:10:28 +00:00
|
|
|
int32_t count, loops;
|
|
|
|
UChar oredChars;
|
|
|
|
|
2007-06-10 06:36:08 +00:00
|
|
|
loops=count=targetCapacity>>3;
|
2001-01-17 19:10:28 +00:00
|
|
|
do {
|
2007-06-10 06:36:08 +00:00
|
|
|
oredChars=target[0]=source[0];
|
|
|
|
oredChars|=target[1]=source[1];
|
|
|
|
oredChars|=target[2]=source[2];
|
|
|
|
oredChars|=target[3]=source[3];
|
|
|
|
oredChars|=target[4]=source[4];
|
|
|
|
oredChars|=target[5]=source[5];
|
|
|
|
oredChars|=target[6]=source[6];
|
|
|
|
oredChars|=target[7]=source[7];
|
2001-01-17 19:10:28 +00:00
|
|
|
|
|
|
|
/* were all 16 entries really valid? */
|
|
|
|
if(oredChars>0x7f) {
|
|
|
|
/* no, return to the first of these 16 */
|
|
|
|
break;
|
|
|
|
}
|
2007-06-10 06:36:08 +00:00
|
|
|
source+=8;
|
|
|
|
target+=8;
|
2001-01-17 19:10:28 +00:00
|
|
|
} while(--count>0);
|
|
|
|
count=loops-count;
|
2007-06-10 06:36:08 +00:00
|
|
|
targetCapacity-=count*8;
|
2001-01-17 19:10:28 +00:00
|
|
|
|
|
|
|
if(offsets!=NULL) {
|
2007-06-10 06:36:08 +00:00
|
|
|
oldTarget+=count*8;
|
2001-01-17 19:10:28 +00:00
|
|
|
while(count>0) {
|
2007-06-10 06:36:08 +00:00
|
|
|
offsets[0]=sourceIndex++;
|
|
|
|
offsets[1]=sourceIndex++;
|
|
|
|
offsets[2]=sourceIndex++;
|
|
|
|
offsets[3]=sourceIndex++;
|
|
|
|
offsets[4]=sourceIndex++;
|
|
|
|
offsets[5]=sourceIndex++;
|
|
|
|
offsets[6]=sourceIndex++;
|
|
|
|
offsets[7]=sourceIndex++;
|
|
|
|
offsets+=8;
|
2001-01-17 19:10:28 +00:00
|
|
|
--count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-20 02:08:39 +00:00
|
|
|
/* conversion loop */
|
2003-07-22 04:22:57 +00:00
|
|
|
c=0;
|
|
|
|
while(targetCapacity>0 && (c=*source++)<=0x7f) {
|
|
|
|
*target++=c;
|
|
|
|
--targetCapacity;
|
2000-12-20 02:08:39 +00:00
|
|
|
}
|
|
|
|
|
2003-07-22 04:22:57 +00:00
|
|
|
if(c>0x7f) {
|
2003-08-01 14:58:43 +00:00
|
|
|
/* callback(illegal); copy the current bytes to toUBytes[] */
|
2003-07-22 04:22:57 +00:00
|
|
|
UConverter *cnv=pArgs->converter;
|
2003-08-01 14:58:43 +00:00
|
|
|
cnv->toUBytes[0]=c;
|
|
|
|
cnv->toULength=1;
|
2003-07-22 04:22:57 +00:00
|
|
|
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
|
|
|
|
} else if(source<sourceLimit && target>=pArgs->targetLimit) {
|
2000-12-20 02:08:39 +00:00
|
|
|
/* target is full */
|
|
|
|
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-07-22 04:22:57 +00:00
|
|
|
/* set offsets since the start */
|
2000-12-20 02:08:39 +00:00
|
|
|
if(offsets!=NULL) {
|
2003-07-22 04:22:57 +00:00
|
|
|
size_t count=target-oldTarget;
|
2000-12-20 02:08:39 +00:00
|
|
|
while(count>0) {
|
|
|
|
*offsets++=sourceIndex++;
|
|
|
|
--count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write back the updated pointers */
|
|
|
|
pArgs->source=(const char *)source;
|
|
|
|
pArgs->target=target;
|
|
|
|
pArgs->offsets=offsets;
|
|
|
|
}
|
|
|
|
|
2004-09-16 04:00:12 +00:00
|
|
|
/* This is a table-less version of ucnv_MBCSSingleGetNextUChar(). */
|
2017-02-11 02:43:09 +00:00
|
|
|
static UChar32 U_CALLCONV
|
2000-12-20 02:08:39 +00:00
|
|
|
_ASCIIGetNextUChar(UConverterToUnicodeArgs *pArgs,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
const uint8_t *source;
|
|
|
|
uint8_t b;
|
|
|
|
|
|
|
|
source=(const uint8_t *)pArgs->source;
|
2003-08-01 14:58:43 +00:00
|
|
|
if(source<(const uint8_t *)pArgs->sourceLimit) {
|
2000-12-20 02:08:39 +00:00
|
|
|
b=*source++;
|
|
|
|
pArgs->source=(const char *)source;
|
|
|
|
if(b<=0x7f) {
|
|
|
|
return b;
|
|
|
|
} else {
|
|
|
|
UConverter *cnv=pArgs->converter;
|
2003-08-01 14:58:43 +00:00
|
|
|
cnv->toUBytes[0]=b;
|
|
|
|
cnv->toULength=1;
|
2000-12-20 02:08:39 +00:00
|
|
|
*pErrorCode=U_ILLEGAL_CHAR_FOUND;
|
2003-08-01 14:58:43 +00:00
|
|
|
return 0xffff;
|
2000-12-20 02:08:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-01 14:58:43 +00:00
|
|
|
/* no output because of empty input */
|
2000-12-20 02:08:39 +00:00
|
|
|
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
|
|
|
|
return 0xffff;
|
|
|
|
}
|
|
|
|
|
2007-02-06 05:24:14 +00:00
|
|
|
/* "Convert" UTF-8 to US-ASCII: Validate and copy. */
|
2017-02-11 02:43:09 +00:00
|
|
|
static void U_CALLCONV
|
2007-02-06 05:24:14 +00:00
|
|
|
ucnv_ASCIIFromUTF8(UConverterFromUnicodeArgs *pFromUArgs,
|
|
|
|
UConverterToUnicodeArgs *pToUArgs,
|
|
|
|
UErrorCode *pErrorCode) {
|
|
|
|
const uint8_t *source, *sourceLimit;
|
|
|
|
uint8_t *target;
|
|
|
|
int32_t targetCapacity, length;
|
|
|
|
|
|
|
|
uint8_t c;
|
|
|
|
|
2018-01-23 21:32:36 +00:00
|
|
|
if(pToUArgs->converter->toULength > 0) {
|
2007-02-06 05:24:14 +00:00
|
|
|
/* no handling of partial UTF-8 characters here, fall back to pivoting */
|
|
|
|
*pErrorCode=U_USING_DEFAULT_WARNING;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set up the local pointers */
|
|
|
|
source=(const uint8_t *)pToUArgs->source;
|
|
|
|
sourceLimit=(const uint8_t *)pToUArgs->sourceLimit;
|
|
|
|
target=(uint8_t *)pFromUArgs->target;
|
|
|
|
targetCapacity=(int32_t)(pFromUArgs->targetLimit-pFromUArgs->target);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* since the conversion here is 1:1 uint8_t:uint8_t, we need only one counter
|
|
|
|
* for the minimum of the sourceLength and targetCapacity
|
|
|
|
*/
|
|
|
|
length=(int32_t)(sourceLimit-source);
|
|
|
|
if(length<targetCapacity) {
|
|
|
|
targetCapacity=length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unroll the loop with the most common case */
|
|
|
|
if(targetCapacity>=16) {
|
|
|
|
int32_t count, loops;
|
|
|
|
uint8_t oredChars;
|
|
|
|
|
|
|
|
loops=count=targetCapacity>>4;
|
|
|
|
do {
|
|
|
|
oredChars=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
oredChars|=*target++=*source++;
|
|
|
|
|
|
|
|
/* were all 16 entries really valid? */
|
|
|
|
if(oredChars>0x7f) {
|
|
|
|
/* no, return to the first of these 16 */
|
|
|
|
source-=16;
|
|
|
|
target-=16;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(--count>0);
|
|
|
|
count=loops-count;
|
|
|
|
targetCapacity-=16*count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* conversion loop */
|
|
|
|
c=0;
|
|
|
|
while(targetCapacity>0 && (c=*source)<=0x7f) {
|
|
|
|
++source;
|
|
|
|
*target++=c;
|
|
|
|
--targetCapacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(c>0x7f) {
|
|
|
|
/* non-ASCII character, handle in standard converter */
|
|
|
|
*pErrorCode=U_USING_DEFAULT_WARNING;
|
|
|
|
} else if(source<sourceLimit && target>=(const uint8_t *)pFromUArgs->targetLimit) {
|
|
|
|
/* target is full */
|
|
|
|
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write back the updated pointers */
|
|
|
|
pToUArgs->source=(const char *)source;
|
|
|
|
pFromUArgs->target=(char *)target;
|
|
|
|
}
|
|
|
|
|
2017-02-11 02:43:09 +00:00
|
|
|
static void U_CALLCONV
|
2003-04-05 01:33:02 +00:00
|
|
|
_ASCIIGetUnicodeSet(const UConverter *cnv,
|
2004-12-02 04:18:35 +00:00
|
|
|
const USetAdder *sa,
|
2003-04-05 01:33:02 +00:00
|
|
|
UConverterUnicodeSet which,
|
|
|
|
UErrorCode *pErrorCode) {
|
2016-12-07 21:14:27 +00:00
|
|
|
(void)cnv;
|
|
|
|
(void)which;
|
|
|
|
(void)pErrorCode;
|
2004-09-07 17:59:53 +00:00
|
|
|
sa->addRange(sa->set, 0, 0x7f);
|
2003-04-05 01:33:02 +00:00
|
|
|
}
|
2017-02-11 02:43:09 +00:00
|
|
|
U_CDECL_END
|
2003-04-05 01:33:02 +00:00
|
|
|
|
2000-12-20 02:08:39 +00:00
|
|
|
static const UConverterImpl _ASCIIImpl={
|
|
|
|
UCNV_US_ASCII,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
|
|
|
|
_ASCIIToUnicodeWithOffsets,
|
|
|
|
_ASCIIToUnicodeWithOffsets,
|
|
|
|
_Latin1FromUnicodeWithOffsets,
|
|
|
|
_Latin1FromUnicodeWithOffsets,
|
|
|
|
_ASCIIGetNextUChar,
|
|
|
|
|
|
|
|
NULL,
|
2003-04-05 01:33:02 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2007-02-06 05:24:14 +00:00
|
|
|
_ASCIIGetUnicodeSet,
|
|
|
|
|
|
|
|
NULL,
|
|
|
|
ucnv_ASCIIFromUTF8
|
2000-12-20 02:08:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const UConverterStaticData _ASCIIStaticData={
|
|
|
|
sizeof(UConverterStaticData),
|
|
|
|
"US-ASCII",
|
|
|
|
367, UCNV_IBM, UCNV_US_ASCII, 1, 1,
|
|
|
|
{ 0x1a, 0, 0, 0 }, 1, FALSE, FALSE,
|
|
|
|
0,
|
2001-02-26 19:51:20 +00:00
|
|
|
0,
|
|
|
|
{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } /* reserved */
|
2000-12-20 02:08:39 +00:00
|
|
|
};
|
|
|
|
|
2015-05-29 19:33:24 +00:00
|
|
|
const UConverterSharedData _ASCIIData=
|
|
|
|
UCNV_IMMUTABLE_SHARED_DATA_INITIALIZER(&_ASCIIStaticData, &_ASCIIImpl);
|
2004-08-26 22:51:40 +00:00
|
|
|
|
|
|
|
#endif
|