1 /** 2 ******************************************************************************* 3 * Copyright (C) 1996-2006, International Business Machines Corporation and * 4 * others. All Rights Reserved. * 5 ******************************************************************************* 6 * 7 * 8 ******************************************************************************* 9 */ 10 /* 11 * (C) Copyright IBM Corp. 2000 - All Rights Reserved 12 * A JNI wrapper to ICU native converter Interface 13 * @author: Ram Viswanadha 14 */ 15 16 #define LOG_TAG "NativeConverter" 17 18 #include "IcuUtilities.h" 19 #include "JNIHelp.h" 20 #include "JniConstants.h" 21 #include "JniException.h" 22 #include "ScopedLocalRef.h" 23 #include "ScopedPrimitiveArray.h" 24 #include "ScopedStringChars.h" 25 #include "ScopedUtfChars.h" 26 #include "UniquePtr.h" 27 #include "cutils/log.h" 28 #include "toStringArray.h" 29 #include "unicode/ucnv.h" 30 #include "unicode/ucnv_cb.h" 31 #include "unicode/uniset.h" 32 #include "unicode/ustring.h" 33 #include "unicode/utypes.h" 34 35 #include <vector> 36 37 #include <stdlib.h> 38 #include <string.h> 39 40 #define NativeConverter_REPORT 0 41 #define NativeConverter_IGNORE 1 42 #define NativeConverter_REPLACE 2 43 44 #define MAX_REPLACEMENT_LENGTH 32 // equivalent to UCNV_ERROR_BUFFER_LENGTH 45 46 struct DecoderCallbackContext { 47 UChar replacementChars[MAX_REPLACEMENT_LENGTH]; 48 size_t replacementCharCount; 49 UConverterToUCallback onUnmappableInput; 50 UConverterToUCallback onMalformedInput; 51 }; 52 53 struct EncoderCallbackContext { 54 char replacementBytes[MAX_REPLACEMENT_LENGTH]; 55 size_t replacementByteCount; 56 UConverterFromUCallback onUnmappableInput; 57 UConverterFromUCallback onMalformedInput; 58 }; 59 60 static UConverter* toUConverter(jlong address) { 61 return reinterpret_cast<UConverter*>(static_cast<uintptr_t>(address)); 62 } 63 64 static bool collectStandardNames(JNIEnv* env, const char* canonicalName, const char* standard, 65 std::vector<std::string>& result) { 66 UErrorCode status = U_ZERO_ERROR; 67 UStringEnumeration e(ucnv_openStandardNames(canonicalName, standard, &status)); 68 if (maybeThrowIcuException(env, "ucnv_openStandardNames", status)) { 69 return false; 70 } 71 72 int32_t count = e.count(status); 73 if (maybeThrowIcuException(env, "StringEnumeration::count", status)) { 74 return false; 75 } 76 77 for (int32_t i = 0; i < count; ++i) { 78 const UnicodeString* string = e.snext(status); 79 if (maybeThrowIcuException(env, "StringEnumeration::snext", status)) { 80 return false; 81 } 82 std::string s; 83 string->toUTF8String(s); 84 if (s.find_first_of("+,") == std::string::npos) { 85 result.push_back(s); 86 } 87 } 88 89 return true; 90 } 91 92 static const char* getICUCanonicalName(const char* name) { 93 UErrorCode error = U_ZERO_ERROR; 94 const char* canonicalName = NULL; 95 if ((canonicalName = ucnv_getCanonicalName(name, "MIME", &error)) != NULL) { 96 return canonicalName; 97 } else if ((canonicalName = ucnv_getCanonicalName(name, "IANA", &error)) != NULL) { 98 return canonicalName; 99 } else if ((canonicalName = ucnv_getCanonicalName(name, "", &error)) != NULL) { 100 return canonicalName; 101 } else if ((canonicalName = ucnv_getAlias(name, 0, &error)) != NULL) { 102 // We have some aliases in the form x-blah .. match those first. 103 return canonicalName; 104 } else if (strstr(name, "x-") == name) { 105 // Check if the converter can be opened with the name given. 106 error = U_ZERO_ERROR; 107 LocalUConverterPointer cnv(ucnv_open(name + 2, &error)); 108 if (U_SUCCESS(error)) { 109 return name + 2; 110 } 111 } 112 return NULL; 113 } 114 115 // If a charset listed in the IANA Charset Registry is supported by an implementation 116 // of the Java platform then its canonical name must be the name listed in the registry. 117 // Many charsets are given more than one name in the registry, in which case the registry 118 // identifies one of the names as MIME-preferred. If a charset has more than one registry 119 // name then its canonical name must be the MIME-preferred name and the other names in 120 // the registry must be valid aliases. If a supported charset is not listed in the IANA 121 // registry then its canonical name must begin with one of the strings "X-" or "x-". 122 static jstring getJavaCanonicalName(JNIEnv* env, const char* icuCanonicalName) { 123 UErrorCode status = U_ZERO_ERROR; 124 125 // Check to see if this is a well-known MIME or IANA name. 126 const char* cName = NULL; 127 if ((cName = ucnv_getStandardName(icuCanonicalName, "MIME", &status)) != NULL) { 128 return env->NewStringUTF(cName); 129 } else if ((cName = ucnv_getStandardName(icuCanonicalName, "IANA", &status)) != NULL) { 130 return env->NewStringUTF(cName); 131 } 132 133 // Check to see if an alias already exists with "x-" prefix, if yes then 134 // make that the canonical name. 135 int32_t aliasCount = ucnv_countAliases(icuCanonicalName, &status); 136 for (int i = 0; i < aliasCount; ++i) { 137 const char* name = ucnv_getAlias(icuCanonicalName, i, &status); 138 if (name != NULL && name[0] == 'x' && name[1] == '-') { 139 return env->NewStringUTF(name); 140 } 141 } 142 143 // As a last resort, prepend "x-" to any alias and make that the canonical name. 144 status = U_ZERO_ERROR; 145 const char* name = ucnv_getStandardName(icuCanonicalName, "UTR22", &status); 146 if (name == NULL && strchr(icuCanonicalName, ',') != NULL) { 147 name = ucnv_getAlias(icuCanonicalName, 1, &status); 148 } 149 // If there is no UTR22 canonical name then just return the original name. 150 if (name == NULL) { 151 name = icuCanonicalName; 152 } 153 UniquePtr<char[]> result(new char[2 + strlen(name) + 1]); 154 strcpy(&result[0], "x-"); 155 strcat(&result[0], name); 156 return env->NewStringUTF(&result[0]); 157 } 158 159 static jlong NativeConverter_openConverter(JNIEnv* env, jclass, jstring converterName) { 160 ScopedUtfChars converterNameChars(env, converterName); 161 if (converterNameChars.c_str() == NULL) { 162 return 0; 163 } 164 UErrorCode status = U_ZERO_ERROR; 165 UConverter* cnv = ucnv_open(converterNameChars.c_str(), &status); 166 maybeThrowIcuException(env, "ucnv_open", status); 167 return reinterpret_cast<uintptr_t>(cnv); 168 } 169 170 static void NativeConverter_closeConverter(JNIEnv*, jclass, jlong address) { 171 ucnv_close(toUConverter(address)); 172 } 173 174 static bool shouldCodecThrow(jboolean flush, UErrorCode error) { 175 if (flush) { 176 return (error != U_BUFFER_OVERFLOW_ERROR && error != U_TRUNCATED_CHAR_FOUND); 177 } else { 178 return (error != U_BUFFER_OVERFLOW_ERROR && error != U_INVALID_CHAR_FOUND && error != U_ILLEGAL_CHAR_FOUND); 179 } 180 } 181 182 static jint NativeConverter_encode(JNIEnv* env, jclass, jlong address, 183 jcharArray source, jint sourceEnd, jbyteArray target, jint targetEnd, 184 jintArray data, jboolean flush) { 185 186 UConverter* cnv = toUConverter(address); 187 if (cnv == NULL) { 188 maybeThrowIcuException(env, "toUConverter", U_ILLEGAL_ARGUMENT_ERROR); 189 return U_ILLEGAL_ARGUMENT_ERROR; 190 } 191 ScopedCharArrayRO uSource(env, source); 192 if (uSource.get() == NULL) { 193 maybeThrowIcuException(env, "uSource", U_ILLEGAL_ARGUMENT_ERROR); 194 return U_ILLEGAL_ARGUMENT_ERROR; 195 } 196 ScopedByteArrayRW uTarget(env, target); 197 if (uTarget.get() == NULL) { 198 maybeThrowIcuException(env, "uTarget", U_ILLEGAL_ARGUMENT_ERROR); 199 return U_ILLEGAL_ARGUMENT_ERROR; 200 } 201 ScopedIntArrayRW myData(env, data); 202 if (myData.get() == NULL) { 203 maybeThrowIcuException(env, "myData", U_ILLEGAL_ARGUMENT_ERROR); 204 return U_ILLEGAL_ARGUMENT_ERROR; 205 } 206 207 // Do the conversion. 208 jint* sourceOffset = &myData[0]; 209 jint* targetOffset = &myData[1]; 210 const jchar* mySource = uSource.get() + *sourceOffset; 211 const UChar* mySourceLimit= uSource.get() + sourceEnd; 212 char* cTarget = reinterpret_cast<char*>(uTarget.get() + *targetOffset); 213 const char* cTargetLimit = reinterpret_cast<const char*>(uTarget.get() + targetEnd); 214 UErrorCode errorCode = U_ZERO_ERROR; 215 ucnv_fromUnicode(cnv , &cTarget, cTargetLimit, &mySource, mySourceLimit, NULL, (UBool) flush, &errorCode); 216 *sourceOffset = (mySource - uSource.get()) - *sourceOffset; 217 *targetOffset = (reinterpret_cast<jbyte*>(cTarget) - uTarget.get()) - *targetOffset; 218 219 // If there was an error, count the problematic characters. 220 if (errorCode == U_ILLEGAL_CHAR_FOUND || errorCode == U_INVALID_CHAR_FOUND) { 221 int8_t invalidUCharCount = 32; 222 UChar invalidUChars[32]; 223 UErrorCode minorErrorCode = U_ZERO_ERROR; 224 ucnv_getInvalidUChars(cnv, invalidUChars, &invalidUCharCount, &minorErrorCode); 225 if (U_SUCCESS(minorErrorCode)) { 226 myData[2] = invalidUCharCount; 227 } 228 } 229 230 // Managed code handles some cases; throw all other errors. 231 if (shouldCodecThrow(flush, errorCode)) { 232 maybeThrowIcuException(env, "ucnv_fromUnicode", errorCode); 233 } 234 return errorCode; 235 } 236 237 static jint NativeConverter_decode(JNIEnv* env, jclass, jlong address, 238 jbyteArray source, jint sourceEnd, jcharArray target, jint targetEnd, 239 jintArray data, jboolean flush) { 240 241 UConverter* cnv = toUConverter(address); 242 if (cnv == NULL) { 243 maybeThrowIcuException(env, "toUConverter", U_ILLEGAL_ARGUMENT_ERROR); 244 return U_ILLEGAL_ARGUMENT_ERROR; 245 } 246 ScopedByteArrayRO uSource(env, source); 247 if (uSource.get() == NULL) { 248 maybeThrowIcuException(env, "uSource", U_ILLEGAL_ARGUMENT_ERROR); 249 return U_ILLEGAL_ARGUMENT_ERROR; 250 } 251 ScopedCharArrayRW uTarget(env, target); 252 if (uTarget.get() == NULL) { 253 maybeThrowIcuException(env, "uTarget", U_ILLEGAL_ARGUMENT_ERROR); 254 return U_ILLEGAL_ARGUMENT_ERROR; 255 } 256 ScopedIntArrayRW myData(env, data); 257 if (myData.get() == NULL) { 258 maybeThrowIcuException(env, "myData", U_ILLEGAL_ARGUMENT_ERROR); 259 return U_ILLEGAL_ARGUMENT_ERROR; 260 } 261 262 // Do the conversion. 263 jint* sourceOffset = &myData[0]; 264 jint* targetOffset = &myData[1]; 265 const char* mySource = reinterpret_cast<const char*>(uSource.get() + *sourceOffset); 266 const char* mySourceLimit = reinterpret_cast<const char*>(uSource.get() + sourceEnd); 267 UChar* cTarget = uTarget.get() + *targetOffset; 268 const UChar* cTargetLimit = uTarget.get() + targetEnd; 269 UErrorCode errorCode = U_ZERO_ERROR; 270 ucnv_toUnicode(cnv, &cTarget, cTargetLimit, &mySource, mySourceLimit, NULL, flush, &errorCode); 271 *sourceOffset = mySource - reinterpret_cast<const char*>(uSource.get()) - *sourceOffset; 272 *targetOffset = cTarget - uTarget.get() - *targetOffset; 273 274 // If there was an error, count the problematic bytes. 275 if (errorCode == U_ILLEGAL_CHAR_FOUND || errorCode == U_INVALID_CHAR_FOUND) { 276 int8_t invalidByteCount = 32; 277 char invalidBytes[32] = {'\0'}; 278 UErrorCode minorErrorCode = U_ZERO_ERROR; 279 ucnv_getInvalidChars(cnv, invalidBytes, &invalidByteCount, &minorErrorCode); 280 if (U_SUCCESS(minorErrorCode)) { 281 myData[2] = invalidByteCount; 282 } 283 } 284 285 // Managed code handles some cases; throw all other errors. 286 if (shouldCodecThrow(flush, errorCode)) { 287 maybeThrowIcuException(env, "ucnv_toUnicode", errorCode); 288 } 289 return errorCode; 290 } 291 292 static void NativeConverter_resetByteToChar(JNIEnv*, jclass, jlong address) { 293 UConverter* cnv = toUConverter(address); 294 if (cnv) { 295 ucnv_resetToUnicode(cnv); 296 } 297 } 298 299 static void NativeConverter_resetCharToByte(JNIEnv*, jclass, jlong address) { 300 UConverter* cnv = toUConverter(address); 301 if (cnv) { 302 ucnv_resetFromUnicode(cnv); 303 } 304 } 305 306 static jint NativeConverter_getMaxBytesPerChar(JNIEnv*, jclass, jlong address) { 307 UConverter* cnv = toUConverter(address); 308 return (cnv != NULL) ? ucnv_getMaxCharSize(cnv) : -1; 309 } 310 311 static jint NativeConverter_getMinBytesPerChar(JNIEnv*, jclass, jlong address) { 312 UConverter* cnv = toUConverter(address); 313 return (cnv != NULL) ? ucnv_getMinCharSize(cnv) : -1; 314 } 315 316 static jfloat NativeConverter_getAveBytesPerChar(JNIEnv*, jclass, jlong address) { 317 UConverter* cnv = toUConverter(address); 318 return (cnv != NULL) ? ((ucnv_getMaxCharSize(cnv) + ucnv_getMinCharSize(cnv)) / 2.0) : -1; 319 } 320 321 static jobjectArray NativeConverter_getAvailableCharsetNames(JNIEnv* env, jclass) { 322 int32_t num = ucnv_countAvailable(); 323 jobjectArray result = env->NewObjectArray(num, JniConstants::stringClass, NULL); 324 if (result == NULL) { 325 return NULL; 326 } 327 for (int i = 0; i < num; ++i) { 328 const char* name = ucnv_getAvailableName(i); 329 ScopedLocalRef<jstring> javaCanonicalName(env, getJavaCanonicalName(env, name)); 330 if (javaCanonicalName.get() == NULL) { 331 return NULL; 332 } 333 env->SetObjectArrayElement(result, i, javaCanonicalName.get()); 334 if (env->ExceptionCheck()) { 335 return NULL; 336 } 337 } 338 return result; 339 } 340 341 static void CHARSET_ENCODER_CALLBACK(const void* rawContext, UConverterFromUnicodeArgs* args, 342 const UChar* codeUnits, int32_t length, UChar32 codePoint, UConverterCallbackReason reason, 343 UErrorCode* status) { 344 if (!rawContext) { 345 return; 346 } 347 const EncoderCallbackContext* ctx = reinterpret_cast<const EncoderCallbackContext*>(rawContext); 348 switch(reason) { 349 case UCNV_UNASSIGNED: 350 ctx->onUnmappableInput(ctx, args, codeUnits, length, codePoint, reason, status); 351 return; 352 case UCNV_ILLEGAL: 353 case UCNV_IRREGULAR: 354 ctx->onMalformedInput(ctx, args, codeUnits, length, codePoint, reason, status); 355 return; 356 case UCNV_CLOSE: 357 delete ctx; 358 return; 359 default: 360 *status = U_ILLEGAL_ARGUMENT_ERROR; 361 return; 362 } 363 } 364 365 static void encoderReplaceCallback(const void* rawContext, 366 UConverterFromUnicodeArgs* fromArgs, const UChar*, int32_t, UChar32, 367 UConverterCallbackReason, UErrorCode * err) { 368 if (rawContext == NULL) { 369 return; 370 } 371 const EncoderCallbackContext* context = reinterpret_cast<const EncoderCallbackContext*>(rawContext); 372 *err = U_ZERO_ERROR; 373 ucnv_cbFromUWriteBytes(fromArgs, context->replacementBytes, context->replacementByteCount, 0, err); 374 } 375 376 static UConverterFromUCallback getFromUCallback(int32_t mode) { 377 switch(mode) { 378 case NativeConverter_IGNORE: return UCNV_FROM_U_CALLBACK_SKIP; 379 case NativeConverter_REPLACE: return encoderReplaceCallback; 380 case NativeConverter_REPORT: return UCNV_FROM_U_CALLBACK_STOP; 381 } 382 abort(); 383 } 384 385 static void NativeConverter_setCallbackEncode(JNIEnv* env, jclass, jlong address, 386 jint onMalformedInput, jint onUnmappableInput, jbyteArray javaReplacement) { 387 UConverter* cnv = toUConverter(address); 388 if (cnv == NULL) { 389 maybeThrowIcuException(env, "toUConverter", U_ILLEGAL_ARGUMENT_ERROR); 390 return; 391 } 392 393 UConverterFromUCallback oldCallback = NULL; 394 const void* oldCallbackContext = NULL; 395 ucnv_getFromUCallBack(cnv, &oldCallback, const_cast<const void**>(&oldCallbackContext)); 396 397 EncoderCallbackContext* callbackContext = const_cast<EncoderCallbackContext*>( 398 reinterpret_cast<const EncoderCallbackContext*>(oldCallbackContext)); 399 if (callbackContext == NULL) { 400 callbackContext = new EncoderCallbackContext; 401 } 402 403 callbackContext->onMalformedInput = getFromUCallback(onMalformedInput); 404 callbackContext->onUnmappableInput = getFromUCallback(onUnmappableInput); 405 406 ScopedByteArrayRO replacementBytes(env, javaReplacement); 407 if (replacementBytes.get() == NULL) { 408 maybeThrowIcuException(env, "replacementBytes", U_ILLEGAL_ARGUMENT_ERROR); 409 return; 410 } 411 memcpy(callbackContext->replacementBytes, replacementBytes.get(), replacementBytes.size()); 412 callbackContext->replacementByteCount = replacementBytes.size(); 413 414 UErrorCode errorCode = U_ZERO_ERROR; 415 ucnv_setFromUCallBack(cnv, CHARSET_ENCODER_CALLBACK, callbackContext, NULL, NULL, &errorCode); 416 maybeThrowIcuException(env, "ucnv_setFromUCallBack", errorCode); 417 } 418 419 static void decoderIgnoreCallback(const void*, UConverterToUnicodeArgs*, const char*, int32_t, UConverterCallbackReason, UErrorCode* err) { 420 // The icu4c UCNV_FROM_U_CALLBACK_SKIP callback requires that the context is NULL, which is 421 // never true for us. 422 *err = U_ZERO_ERROR; 423 } 424 425 static void decoderReplaceCallback(const void* rawContext, 426 UConverterToUnicodeArgs* toArgs, const char*, int32_t, UConverterCallbackReason, 427 UErrorCode* err) { 428 if (!rawContext) { 429 return; 430 } 431 const DecoderCallbackContext* context = reinterpret_cast<const DecoderCallbackContext*>(rawContext); 432 *err = U_ZERO_ERROR; 433 ucnv_cbToUWriteUChars(toArgs,context->replacementChars, context->replacementCharCount, 0, err); 434 } 435 436 static UConverterToUCallback getToUCallback(int32_t mode) { 437 switch (mode) { 438 case NativeConverter_IGNORE: return decoderIgnoreCallback; 439 case NativeConverter_REPLACE: return decoderReplaceCallback; 440 case NativeConverter_REPORT: return UCNV_TO_U_CALLBACK_STOP; 441 } 442 abort(); 443 } 444 445 static void CHARSET_DECODER_CALLBACK(const void* rawContext, UConverterToUnicodeArgs* args, 446 const char* codeUnits, int32_t length, 447 UConverterCallbackReason reason, UErrorCode* status) { 448 if (!rawContext) { 449 return; 450 } 451 const DecoderCallbackContext* ctx = reinterpret_cast<const DecoderCallbackContext*>(rawContext); 452 switch(reason) { 453 case UCNV_UNASSIGNED: 454 ctx->onUnmappableInput(ctx, args, codeUnits, length, reason, status); 455 return; 456 case UCNV_ILLEGAL: 457 case UCNV_IRREGULAR: 458 ctx->onMalformedInput(ctx, args, codeUnits, length, reason, status); 459 return; 460 case UCNV_CLOSE: 461 delete ctx; 462 return; 463 default: 464 *status = U_ILLEGAL_ARGUMENT_ERROR; 465 return; 466 } 467 } 468 469 static void NativeConverter_setCallbackDecode(JNIEnv* env, jclass, jlong address, 470 jint onMalformedInput, jint onUnmappableInput, jstring javaReplacement) { 471 UConverter* cnv = toUConverter(address); 472 if (cnv == NULL) { 473 maybeThrowIcuException(env, "toConverter", U_ILLEGAL_ARGUMENT_ERROR); 474 return; 475 } 476 477 UConverterToUCallback oldCallback; 478 const void* oldCallbackContext; 479 ucnv_getToUCallBack(cnv, &oldCallback, &oldCallbackContext); 480 481 DecoderCallbackContext* callbackContext = const_cast<DecoderCallbackContext*>( 482 reinterpret_cast<const DecoderCallbackContext*>(oldCallbackContext)); 483 if (callbackContext == NULL) { 484 callbackContext = new DecoderCallbackContext; 485 } 486 487 callbackContext->onMalformedInput = getToUCallback(onMalformedInput); 488 callbackContext->onUnmappableInput = getToUCallback(onUnmappableInput); 489 490 ScopedStringChars replacement(env, javaReplacement); 491 if (replacement.get() == NULL) { 492 maybeThrowIcuException(env, "replacement", U_ILLEGAL_ARGUMENT_ERROR); 493 return; 494 } 495 u_strncpy(callbackContext->replacementChars, replacement.get(), replacement.size()); 496 callbackContext->replacementCharCount = replacement.size(); 497 498 UErrorCode errorCode = U_ZERO_ERROR; 499 ucnv_setToUCallBack(cnv, CHARSET_DECODER_CALLBACK, callbackContext, NULL, NULL, &errorCode); 500 maybeThrowIcuException(env, "ucnv_setToUCallBack", errorCode); 501 } 502 503 static jfloat NativeConverter_getAveCharsPerByte(JNIEnv* env, jclass, jlong handle) { 504 return (1 / (jfloat) NativeConverter_getMaxBytesPerChar(env, NULL, handle)); 505 } 506 507 static jbyteArray NativeConverter_getSubstitutionBytes(JNIEnv* env, jclass, jlong address) { 508 UConverter* cnv = toUConverter(address); 509 if (cnv == NULL) { 510 return NULL; 511 } 512 UErrorCode status = U_ZERO_ERROR; 513 char replacementBytes[MAX_REPLACEMENT_LENGTH]; 514 int8_t len = sizeof(replacementBytes); 515 ucnv_getSubstChars(cnv, replacementBytes, &len, &status); 516 if (!U_SUCCESS(status)) { 517 return env->NewByteArray(0); 518 } 519 jbyteArray result = env->NewByteArray(len); 520 if (result == NULL) { 521 return NULL; 522 } 523 env->SetByteArrayRegion(result, 0, len, reinterpret_cast<jbyte*>(replacementBytes)); 524 return result; 525 } 526 527 static jboolean NativeConverter_contains(JNIEnv* env, jclass, jstring name1, jstring name2) { 528 ScopedUtfChars name1Chars(env, name1); 529 if (name1Chars.c_str() == NULL) { 530 return JNI_FALSE; 531 } 532 ScopedUtfChars name2Chars(env, name2); 533 if (name2Chars.c_str() == NULL) { 534 return JNI_FALSE; 535 } 536 537 UErrorCode errorCode = U_ZERO_ERROR; 538 LocalUConverterPointer converter1(ucnv_open(name1Chars.c_str(), &errorCode)); 539 UnicodeSet set1; 540 ucnv_getUnicodeSet(&*converter1, set1.toUSet(), UCNV_ROUNDTRIP_SET, &errorCode); 541 542 LocalUConverterPointer converter2(ucnv_open(name2Chars.c_str(), &errorCode)); 543 UnicodeSet set2; 544 ucnv_getUnicodeSet(&*converter2, set2.toUSet(), UCNV_ROUNDTRIP_SET, &errorCode); 545 546 return U_SUCCESS(errorCode) && set1.containsAll(set2); 547 } 548 549 static jobject NativeConverter_charsetForName(JNIEnv* env, jclass, jstring charsetName) { 550 ScopedUtfChars charsetNameChars(env, charsetName); 551 if (charsetNameChars.c_str() == NULL) { 552 return NULL; 553 } 554 555 // Get ICU's canonical name for this charset. 556 const char* icuCanonicalName = getICUCanonicalName(charsetNameChars.c_str()); 557 if (icuCanonicalName == NULL) { 558 return NULL; 559 } 560 561 // Get Java's canonical name for this charset. 562 jstring javaCanonicalName = getJavaCanonicalName(env, icuCanonicalName); 563 if (env->ExceptionCheck()) { 564 return NULL; 565 } 566 567 // Check that this charset is supported. 568 { 569 // ICU doesn't offer any "isSupported", so we just open and immediately close. 570 UErrorCode error = U_ZERO_ERROR; 571 LocalUConverterPointer cnv(ucnv_open(icuCanonicalName, &error)); 572 if (!U_SUCCESS(error)) { 573 return NULL; 574 } 575 } 576 577 // Get the aliases for this charset. 578 std::vector<std::string> aliases; 579 if (!collectStandardNames(env, icuCanonicalName, "IANA", aliases)) { 580 return NULL; 581 } 582 if (!collectStandardNames(env, icuCanonicalName, "MIME", aliases)) { 583 return NULL; 584 } 585 if (!collectStandardNames(env, icuCanonicalName, "JAVA", aliases)) { 586 return NULL; 587 } 588 if (!collectStandardNames(env, icuCanonicalName, "WINDOWS", aliases)) { 589 return NULL; 590 } 591 jobjectArray javaAliases = toStringArray(env, aliases); 592 if (env->ExceptionCheck()) { 593 return NULL; 594 } 595 596 // Construct the CharsetICU object. 597 static jmethodID charsetConstructor = env->GetMethodID(JniConstants::charsetICUClass, "<init>", 598 "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V"); 599 if (env->ExceptionCheck()) { 600 return NULL; 601 } 602 return env->NewObject(JniConstants::charsetICUClass, charsetConstructor, 603 javaCanonicalName, env->NewStringUTF(icuCanonicalName), javaAliases); 604 } 605 606 static JNINativeMethod gMethods[] = { 607 NATIVE_METHOD(NativeConverter, charsetForName, "(Ljava/lang/String;)Ljava/nio/charset/Charset;"), 608 NATIVE_METHOD(NativeConverter, closeConverter, "(J)V"), 609 NATIVE_METHOD(NativeConverter, contains, "(Ljava/lang/String;Ljava/lang/String;)Z"), 610 NATIVE_METHOD(NativeConverter, decode, "(J[BI[CI[IZ)I"), 611 NATIVE_METHOD(NativeConverter, encode, "(J[CI[BI[IZ)I"), 612 NATIVE_METHOD(NativeConverter, getAvailableCharsetNames, "()[Ljava/lang/String;"), 613 NATIVE_METHOD(NativeConverter, getAveBytesPerChar, "(J)F"), 614 NATIVE_METHOD(NativeConverter, getAveCharsPerByte, "(J)F"), 615 NATIVE_METHOD(NativeConverter, getMaxBytesPerChar, "(J)I"), 616 NATIVE_METHOD(NativeConverter, getMinBytesPerChar, "(J)I"), 617 NATIVE_METHOD(NativeConverter, getSubstitutionBytes, "(J)[B"), 618 NATIVE_METHOD(NativeConverter, openConverter, "(Ljava/lang/String;)J"), 619 NATIVE_METHOD(NativeConverter, resetByteToChar, "(J)V"), 620 NATIVE_METHOD(NativeConverter, resetCharToByte, "(J)V"), 621 NATIVE_METHOD(NativeConverter, setCallbackDecode, "(JIILjava/lang/String;)V"), 622 NATIVE_METHOD(NativeConverter, setCallbackEncode, "(JII[B)V"), 623 }; 624 void register_libcore_icu_NativeConverter(JNIEnv* env) { 625 jniRegisterNativeMethods(env, "libcore/icu/NativeConverter", gMethods, NELEM(gMethods)); 626 } 627