/* Determine length of this Standard UTF-8 in Modified UTF-8. *ValidationisdoneofthebasicUTFencodingrules,returns *length(nochange)whenerrorsaredetectedintheUTFencoding. * *Note:AcceptsModifiedUTF-8also,noverificationonthe *correctnessofStandardUTF-8isdone.e,g,0xC080inputisok.
*/ int JNICALL utf8sToUtf8mLength(jbyte *string, int length) { int newLength; int i;
newLength = 0; for ( i = 0 ; i < length ; i++ ) { unsigned byte;
byte = (unsignedchar)string[i]; if ( (byte & 0x80) == 0 ) { /* 1byte encoding */
newLength++; if ( byte == 0 ) {
newLength++; /* We gain one byte in length on NULL bytes */
}
} elseif ( (byte & 0xE0) == 0xC0 ) { /* 2byte encoding */ /* Check encoding of following bytes */ if ( (i+1) >= length || (string[i+1] & 0xC0) != 0x80 ) { break; /* Error condition */
}
i++; /* Skip next byte */
newLength += 2;
} elseif ( (byte & 0xF0) == 0xE0 ) { /* 3byte encoding */ /* Check encoding of following bytes */ if ( (i+2) >= length || (string[i+1] & 0xC0) != 0x80
|| (string[i+2] & 0xC0) != 0x80 ) { break; /* Error condition */
}
i += 2; /* Skip next two bytes */
newLength += 3;
} elseif ( (byte & 0xF8) == 0xF0 ) { /* 4byte encoding */ /* Check encoding of following bytes */ if ( (i+3) >= length || (string[i+1] & 0xC0) != 0x80
|| (string[i+2] & 0xC0) != 0x80
|| (string[i+3] & 0xC0) != 0x80 ) { break; /* Error condition */
}
i += 3; /* Skip next 3 bytes */
newLength += 6; /* 4byte encoding turns into 2 3byte ones */
} else { break; /* Error condition */
}
} if ( i != length ) { /* Error in finding new length, return old length so no conversion */ /* FIXUP: ERROR_MESSAGE? */ return length;
} return newLength;
}
/* Convert Standard UTF-8 to Modified UTF-8. *AssumestheUTF-8encodingwasvalidatedbyutf8mLength()above. * *Note:AcceptsModifiedUTF-8also,noverificationonthe *correctnessofStandardUTF-8isdone.e,g,0xC080inputisok.
*/ void JNICALL utf8sToUtf8m(jbyte *string, int length, jbyte *newString, int newLength) { int i; int j;
j = 0; for ( i = 0 ; i < length ; i++ ) { unsigned byte1;
/* Given a Modified UTF-8 string, calculate the Standard UTF-8 length. *BasicvalidationoftheUTFencodingrulesisdone,andlengthis *returned(nochange)whenerrorsaredetected. * *Note:NovalidationismadethatthisisindeedModifiedUTF-8comingin. *
*/ int JNICALL utf8mToUtf8sLength(jbyte *string, int length) { int newLength; int i;
newLength = 0; for ( i = 0 ; i < length ; i++ ) { unsigned byte1, byte2, byte3, byte4, byte5, byte6;
byte1 = (unsignedchar)string[i]; if ( (byte1 & 0x80) == 0 ) { /* 1byte encoding */
newLength++;
} elseif ( (byte1 & 0xE0) == 0xC0 ) { /* 2byte encoding */ /* Check encoding of following bytes */ if ( (i+1) >= length || (string[i+1] & 0xC0) != 0x80 ) { break; /* Error condition */
}
byte2 = (unsignedchar)string[++i]; if ( byte1 != 0xC0 || byte2 != 0x80 ) {
newLength += 2; /* Normal 2byte encoding, not 0xC080 */
} else {
newLength++; /* We will turn 0xC080 into 0 */
}
} elseif ( (byte1 & 0xF0) == 0xE0 ) { /* 3byte encoding */ /* Check encoding of following bytes */ if ( (i+2) >= length || (string[i+1] & 0xC0) != 0x80
|| (string[i+2] & 0xC0) != 0x80 ) { break; /* Error condition */
}
byte2 = (unsignedchar)string[++i];
byte3 = (unsignedchar)string[++i];
newLength += 3; /* Possible process a second 3byte encoding */ if ( (i+3) < length && byte1 == 0xED && (byte2 & 0xF0) == 0xA0 ) { /* See if this is a pair of 3byte encodings */
byte4 = (unsignedchar)string[i+1];
byte5 = (unsignedchar)string[i+2];
byte6 = (unsignedchar)string[i+3]; if ( byte4 == 0xED && (byte5 & 0xF0) == 0xB0 ) { /* Check encoding of 3rd byte */ if ( (byte6 & 0xC0) != 0x80 ) { break; /* Error condition */
}
newLength++; /* New string will have 4byte encoding */
i += 3; /* Skip next 3 bytes */
}
}
} else { break; /* Error condition */
}
} if ( i != length ) { /* Error in UTF encoding */ /* FIXUP: ERROR_MESSAGE()? */ return length;
} return newLength;
}
/* Convert a Modified UTF-8 string into a Standard UTF-8 string *Itisassumedthatthisstringhasbeenvalidatedintermsofthe *basicUTFencodingrulesbyutf8Length()above. * *Note:NovalidationismadethatthisisindeedModifiedUTF-8comingin. *
*/ void JNICALL utf8mToUtf8s(jbyte *string, int length, jbyte *newString, int newLength) { int i; int j;
j = 0; for ( i = 0 ; i < length ; i++ ) { unsigned byte1, byte2, byte3, byte4, byte5, byte6;
if (intCodePage == -1) { // First call, get codepage from the os
langID = LANGIDFROMLCID(GetUserDefaultLCID());
localeID = MAKELCID(langID, SORT_DEFAULT); if (GetLocaleInfo(localeID, LOCALE_IDEFAULTANSICODEPAGE,
strCodePage, sizeof(strCodePage)/sizeof(TCHAR)) > 0 ) {
intCodePage = atoi(strCodePage);
} else {
intCodePage = GetACP();
}
}
return intCodePage;
}
/* *Getwidestring(assumeslen>0)
*/ static WCHAR* getWideString(UINT codePage, char* str, int len, int *pwlen) { int wlen;
WCHAR* wstr;
/* Convert the string to WIDE string */
wlen = MultiByteToWideChar(codePage, 0, str, len, NULL, 0);
*pwlen = wlen; if (wlen <= 0) {
UTF_ERROR(("Can't get WIDE string length")); return NULL;
}
wstr = (WCHAR*)malloc(wlen * sizeof(WCHAR)); if (wstr == NULL) {
UTF_ERROR(("Can't malloc() any space")); return NULL;
} if (MultiByteToWideChar(codePage, 0, str, len, wstr, wlen) == 0) {
UTF_ERROR(("Can't get WIDE string")); return NULL;
} return wstr;
}
/* *ConvertUTF-8toaplatformstring *NOTE:outputBufSizeincludesthespaceforthetrailing0.
*/ int JNICALL utf8ToPlatform(jbyte *utf8, int len, char* output, int outputBufSize) { int wlen; int plen;
WCHAR* wstr;
UINT codepage; int outputMaxLen;
UTF_ASSERT(utf8);
UTF_ASSERT(output);
UTF_ASSERT(len >= 0);
UTF_ASSERT(outputBufSize > len);
outputMaxLen = outputBufSize - 1; // leave space for trailing 0
/* Zero length is ok, but we don't need to do much */ if ( len == 0 ) {
output[0] = 0; return0;
}
/* Get WIDE string version (assumes len>0) */
wstr = getWideString(CP_UTF8, (char*)utf8, len, &wlen); if ( wstr == NULL ) { // Can't allocate WIDE string goto just_copy_bytes;
}
/* *ConvertPlatformEncodingtoUTF-8. *NOTE:outputBufSizeincludesthespaceforthetrailing0.
*/ int JNICALL utf8FromPlatform(char *str, int len, jbyte *output, int outputBufSize) { int wlen; int plen;
WCHAR* wstr;
UINT codepage; int outputMaxLen;
UTF_ASSERT(str);
UTF_ASSERT(output);
UTF_ASSERT(len >= 0);
UTF_ASSERT(outputBufSize > len);
outputMaxLen = outputBufSize - 1; // leave space for trailing 0
/* Zero length is ok, but we don't need to do much */ if ( len == 0 ) {
output[0] = 0; return0;
}
/* Get WIDE string version (assumes len>0) */
codepage = getCodepage();
wstr = getWideString(codepage, str, len, &wlen); if ( wstr == NULL ) { goto just_copy_bytes;
}
UTF_ASSERT(bytes);
UTF_ASSERT(output);
UTF_ASSERT(outputBufSize > len);
outputMaxLen = outputBufSize - 1; // leave space for trailing 0
/* Zero length is ok, but we don't need to do much */ if ( len == 0 ) {
output[0] = 0; return0;
}
if (codeset == NULL && codeset != (char *) -1) { // locale is not initialized, do it now if (setlocale(LC_ALL, "") != NULL) { // nl_langinfo returns ANSI_X3.4-1968 by default
codeset = (char*)nl_langinfo(CODESET);
}
if (codeset == NULL) { // Not able to initialize process locale from platform one.
codeset = (char *) -1;
}
}
if (codeset == (char *) -1) { // There was an error during initialization, so just bail out goto just_copy_bytes;
}
func = (drn == TO_UTF8) ? iconv_open(codeset, "UTF-8") : iconv_open("UTF-8", codeset); if (func == (iconv_t) -1) { // Requested charset combination is not supported, conversion couldn't be done. // make sure we will not try it again
codeset = (char *) -1; goto just_copy_bytes;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.