extern int main(int argc, const char *argv[]) { UErrorCode errorCode=U_ZERO_ERROR; // Note: Using a global variable for any object is not exactly thread-safe... // You can change this call to e.g. ucnv_open("UTF-8", &errorCode) if you pipe // the output to a file and look at it with a Unicode-capable editor. // This will currently affect only the printUString() function, see the code above. // printUnicodeString() could use this, too, by changing to an extract() overload // that takes a UConverter argument. cnv=ucnv_open(NULL, &errorCode); if(U_FAILURE(errorCode)) { fprintf(stderr, "error %s opening the default converter\n", u_errorName(errorCode)); return errorCode; } ucnv_setFromUCallBack(cnv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, NULL, NULL, &errorCode); if(U_FAILURE(errorCode)) { fprintf(stderr, "error %s setting the escape callback in the default converter\n", u_errorName(errorCode)); ucnv_close(cnv); return errorCode; } demo_utf_h_macros(); demo_C_Unicode_strings(); demoCaseMapInC(); demoCaseMapInCPlusPlus(); demoUnicodeStringStorage(); demoUnicodeStringInit(); ucnv_close(cnv); return 0; }
char * ppb_char_set_utf16_to_char_set(PP_Instance instance, const uint16_t *utf16, uint32_t utf16_len, const char *output_char_set, enum PP_CharSet_ConversionError on_error, uint32_t *output_length) { // each character could take up to 4 bytes in UTF-8; with additional zero-terminator byte const uint32_t output_buffer_length = (utf16_len + 1) * 4 + 1; char *output = ppb_memory_mem_alloc(output_buffer_length); if (!output) { trace_error("%s, can't allocate memory, %u bytes\n", __func__, output_buffer_length); goto err; } const char *charset = encoding_alias_get_canonical_name(output_char_set); const UChar subst = '?'; UErrorCode st = U_ZERO_ERROR; UConverter *u = ucnv_open(charset, &st); if (!U_SUCCESS(st)) { trace_error("%s, wrong charset %s\n", __func__, output_char_set); goto err; } switch (on_error) { default: case PP_CHARSET_CONVERSIONERROR_FAIL: st = U_ZERO_ERROR; ucnv_setFromUCallBack(u, UCNV_FROM_U_CALLBACK_STOP, NULL, NULL, NULL, &st); break; case PP_CHARSET_CONVERSIONERROR_SKIP: st = U_ZERO_ERROR; ucnv_setFromUCallBack(u, UCNV_FROM_U_CALLBACK_SKIP, NULL, NULL, NULL, &st); break; case PP_CHARSET_CONVERSIONERROR_SUBSTITUTE: st = U_ZERO_ERROR; ucnv_setFromUCallBack(u, UCNV_FROM_U_CALLBACK_SUBSTITUTE, NULL, NULL, NULL, &st); st = U_ZERO_ERROR; ucnv_setSubstString(u, &subst, 1, &st); break; } *output_length = ucnv_fromUChars(u, output, output_buffer_length, utf16, utf16_len, &st); if (st != U_BUFFER_OVERFLOW_ERROR && !U_SUCCESS(st)) goto err; ucnv_close(u); return output; err: *output_length = 0; ppb_memory_mem_free(output); if (u) ucnv_close(u); return NULL; }
static void _charset_close(lml_charset_t *lc) { if ( lc->from ) ucnv_close(lc->from); if ( lc->to ) ucnv_close(lc->to); }
void XeTeXFontMgr_FC::terminate() { if (macRomanConv != NULL) ucnv_close(macRomanConv); if (utf16beConv != NULL) ucnv_close(utf16beConv); if (utf8Conv != NULL) ucnv_close(utf8Conv); }
Variant c_UConverter::t___destruct() { if (m_src) { ucnv_close(m_src); } if (m_dest) { ucnv_close(m_dest); } return uninit_null(); }
status_t ICUCategoryData::_GetConverter(UConverter*& converterOut) { // we use different converters per thread to avoid concurrent accesses ICUThreadLocalStorageValue* tlsValue = NULL; status_t result = ICUThreadLocalStorageValue::GetInstanceForKey( fThreadLocalStorageKey, tlsValue); if (result != B_OK) return result; if (tlsValue->converter != NULL) { if (strcmp(tlsValue->charset, fGivenCharset) == 0) { converterOut = tlsValue->converter; return B_OK; } // charset no longer matches the converter, we need to dump it and // create a new one ucnv_close(tlsValue->converter); tlsValue->converter = NULL; } // create a new converter for the current charset UErrorCode icuStatus = U_ZERO_ERROR; UConverter* icuConverter = ucnv_open(fGivenCharset, &icuStatus); if (icuConverter == NULL) return B_NAME_NOT_FOUND; // setup the new converter to stop upon any errors icuStatus = U_ZERO_ERROR; ucnv_setToUCallBack(icuConverter, UCNV_TO_U_CALLBACK_STOP, NULL, NULL, NULL, &icuStatus); if (!U_SUCCESS(icuStatus)) { ucnv_close(icuConverter); return B_ERROR; } icuStatus = U_ZERO_ERROR; ucnv_setFromUCallBack(icuConverter, UCNV_FROM_U_CALLBACK_STOP, NULL, NULL, NULL, &icuStatus); if (!U_SUCCESS(icuStatus)) { ucnv_close(icuConverter); return B_ERROR; } tlsValue->converter = icuConverter; strlcpy(tlsValue->charset, fGivenCharset, sizeof(tlsValue->charset)); converterOut = icuConverter; return B_OK; }
U_CDECL_BEGIN static void U_CALLCONV _HZOpen(UConverter *cnv, UConverterLoadArgs *pArgs, UErrorCode *errorCode){ UConverter *gbConverter; if(pArgs->onlyTestIsLoadable) { ucnv_canCreateConverter("GBK", errorCode); /* errorCode carries result */ return; } gbConverter = ucnv_open("GBK", errorCode); if(U_FAILURE(*errorCode)) { return; } cnv->toUnicodeStatus = 0; cnv->fromUnicodeStatus= 0; cnv->mode=0; cnv->fromUChar32=0x0000; cnv->extraInfo = uprv_calloc(1, sizeof(UConverterDataHZ)); if(cnv->extraInfo != NULL){ ((UConverterDataHZ*)cnv->extraInfo)->gbConverter = gbConverter; } else { ucnv_close(gbConverter); *errorCode = U_MEMORY_ALLOCATION_ERROR; return; } }
//------------------------------------------ size_t CFS_Sqlite::WriteUTF8(const wchar* string, FSFILE *fp) { if (!string || !*string) return 0; // if not unicode #ifndef UNICODE return Write(string, 1, strlen(string), fp); #else unsigned int inputLen = wstrlen(string); unsigned char *target; target = new unsigned char[inputLen*4+1]; // make buffer long enough UErrorCode status = U_ZERO_ERROR; int32_t len; //// set up the converter UConverter *conv = ucnv_open("utf-8", &status); assertd(U_SUCCESS(status)!=0, "Failed to open utf-8 converter!"); //// convert len = ucnv_fromUChars(conv, (char*)target, inputLen*4, string, -1, &status); assertd(U_SUCCESS(status)!=0, "Failed to convert string to utf-8!"); //// write to file size_t wrlen = Write(target, 1, len, fp); //// close and clean delete[] target; ucnv_close(conv); return wrlen; #endif }
bool CSICU_charset_init(charset* cs, const ASCII* charSetName) { UErrorCode status = U_ZERO_ERROR; UConverter* conv = ucnv_open(charSetName, &status); if (U_SUCCESS(status)) { // charSetName comes from stack. Copy it. ASCII* p = new ASCII[strlen(charSetName) + 1]; cs->charset_name = p; strcpy(p, charSetName); cs->charset_version = CHARSET_VERSION_1; cs->charset_flags |= CHARSET_ASCII_BASED; cs->charset_min_bytes_per_char = ucnv_getMinCharSize(conv); cs->charset_max_bytes_per_char = ucnv_getMaxCharSize(conv); cs->charset_fn_destroy = charset_destroy; cs->charset_fn_well_formed = NULL; const UChar unicodeSpace = 32; BYTE* p2 = new BYTE[cs->charset_max_bytes_per_char]; cs->charset_space_character = p2; cs->charset_space_length = ucnv_fromUChars(conv, reinterpret_cast<char*>(p2), cs->charset_max_bytes_per_char, &unicodeSpace, 1, &status); fb_assert(U_SUCCESS(status)); ucnv_close(conv); CVICU_convert_init(cs); } return U_SUCCESS(status); }
mod_websocket_conv_t * mod_websocket_conv_init(const char *locale) { mod_websocket_conv_t *cnv; UErrorCode err = U_ZERO_ERROR; if (!locale) { return NULL; } cnv = (mod_websocket_conv_t *)malloc(sizeof(mod_websocket_conv_t)); if (!cnv) { return NULL; } if (strcasecmp(MOD_WEBSOCKET_UTF8_STR, locale) == 0) { cnv->cli = NULL; cnv->srv = NULL; } else { cnv->cli = ucnv_open(MOD_WEBSOCKET_UTF8_STR, &err); if (U_FAILURE(err)) { free(cnv); return NULL; } cnv->srv = ucnv_open(locale, &err); if (U_FAILURE(err)) { ucnv_close(cnv->cli); free(cnv); return NULL; } } return cnv; }
char *aescstrdup(const UChar* unichars,int32_t length){ char *newString,*targetLimit,*target; UConverterFromUCallback cb; const void *p; UErrorCode errorCode = U_ZERO_ERROR; #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY # if U_PLATFORM == U_PF_OS390 static const char convName[] = "ibm-1047"; # else static const char convName[] = "ibm-37"; # endif #else static const char convName[] = "US-ASCII"; #endif UConverter* conv = ucnv_open(convName, &errorCode); if(length==-1){ length = u_strlen( unichars); } newString = (char*)ctst_malloc ( sizeof(char) * 8 * (length +1)); target = newString; targetLimit = newString+sizeof(char) * 8 * (length +1); ucnv_setFromUCallBack(conv, UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_C, &cb, &p, &errorCode); ucnv_fromUnicode(conv,&target,targetLimit, &unichars, (UChar*)(unichars+length),NULL,TRUE,&errorCode); ucnv_close(conv); *target = '\0'; return newString; }
U_CAPI UConverter* U_EXPORT2 u_getDefaultConverter(UErrorCode *status) { UConverter *converter = NULL; if (gDefaultConverter != NULL) { umtx_lock(NULL); /* need to check to make sure it wasn't taken out from under us */ if (gDefaultConverter != NULL) { converter = gDefaultConverter; gDefaultConverter = NULL; } umtx_unlock(NULL); } /* if the cache was empty, create a converter */ if(converter == NULL) { converter = ucnv_open(NULL, status); if(U_FAILURE(*status)) { ucnv_close(converter); converter = NULL; } } return converter; }
QString QIcuCodec::convertToUnicode(const char *chars, int length, QTextCodec::ConverterState *state) const { UConverter *conv = getConverter(state); QString string(length + 2, Qt::Uninitialized); const char *end = chars + length; int convertedChars = 0; while (1) { UChar *uc = (UChar *)string.data(); UChar *ucEnd = uc + string.length(); uc += convertedChars; UErrorCode error = U_ZERO_ERROR; ucnv_toUnicode(conv, &uc, ucEnd, &chars, end, 0, false, &error); if (!U_SUCCESS(error) && error != U_BUFFER_OVERFLOW_ERROR) { qDebug() << "convertToUnicode failed:" << u_errorName(error); break; } convertedChars = uc - (UChar *)string.data(); if (chars >= end) break; string.resize(string.length()*2); } string.resize(convertedChars); if (!state) ucnv_close(conv); return string; }
Bool CodeSet_IsEncodingSupported(const char *name) // IN { UConverter *cv; UErrorCode uerr; /* * Fallback if necessary. */ if (dontUseIcu) { return CodeSetOld_IsEncodingSupported(name); } /* * Try to open the encoding. */ uerr = U_ZERO_ERROR; cv = ucnv_open(name, &uerr); if (cv) { ucnv_close(cv); return TRUE; } return FALSE; }
U_CAPI FileStream * U_EXPORT2 ucbuf_autodetect(const char* fileName, const char** cp,UConverter** conv, int32_t* signatureLength,UErrorCode* error){ FileStream* in=NULL; if(error==NULL || U_FAILURE(*error)){ return NULL; } if(conv==NULL || cp==NULL || fileName==NULL){ *error = U_ILLEGAL_ARGUMENT_ERROR; return NULL; } /* open the file */ in= T_FileStream_open(fileName,"rb"); if(in == NULL){ *error=U_FILE_ACCESS_ERROR; return NULL; } if(ucbuf_autodetect_fs(in,cp,conv,signatureLength,error)) { return in; } else { ucnv_close(*conv); *conv=NULL; T_FileStream_close(in); return NULL; } }
UErrorCode convsample_12() { printf("\n\n==============================================\n" "Sample 12: C: simple sjis -> unicode conversion\n"); // **************************** START SAMPLE ******************* char source[] = { 0x63, 0x61, 0x74, (char)0x94, 0x4C, (char)0x82, 0x6E, (char)0x82, 0x6A, 0x00 }; UChar target[100]; UErrorCode status = U_ZERO_ERROR; UConverter *conv; int32_t len; // set up the converter conv = ucnv_open("shift_jis", &status); assert(U_SUCCESS(status)); // convert to Unicode // Note: we can use strlen, we know it's an 8 bit null terminated codepage target[6] = 0xFDCA; len = ucnv_toUChars(conv, target, 100, source, strlen(source), &status); U_ASSERT(status); // close the converter ucnv_close(conv); // ***************************** END SAMPLE ******************** // Print it out printBytes("src", source, strlen(source) ); printf("\n"); printUChars("targ", target, len); return U_ZERO_ERROR; }
void mod_websocket_conv_final(mod_websocket_conv_t *cnv) { if (cnv) { if (cnv->cli) { ucnv_close(cnv->cli); cnv->cli = NULL; } if (cnv->srv) { ucnv_close(cnv->srv); cnv->srv = NULL; } free(cnv); cnv = NULL; } return; }
UErrorCode convsample_02() { printf("\n\n==============================================\n" "Sample 02: C: simple Unicode -> koi8-r conversion\n"); // **************************** START SAMPLE ******************* // "cat<cat>OK" UChar source[] = { 0x041C, 0x043E, 0x0441, 0x043A, 0x0432, 0x0430, 0x0021, 0x0000 }; char target[100]; UErrorCode status = U_ZERO_ERROR; UConverter *conv; int32_t len; // set up the converter conv = ucnv_open("koi8-r", &status); assert(U_SUCCESS(status)); // convert to koi8-r len = ucnv_fromUChars(conv, target, 100, source, -1, &status); assert(U_SUCCESS(status)); // close the converter ucnv_close(conv); // ***************************** END SAMPLE ******************** // Print it out printUChars("src", source); printf("\n"); printBytes("targ", target, len); return U_ZERO_ERROR; }
QByteArray QIcuCodec::convertFromUnicode(const QChar *unicode, int length, QTextCodec::ConverterState *state) const { UConverter *conv = getConverter(state); int requiredLength = UCNV_GET_MAX_BYTES_FOR_STRING(length, ucnv_getMaxCharSize(conv)); QByteArray string(requiredLength, Qt::Uninitialized); const UChar *uc = (const UChar *)unicode; const UChar *end = uc + length; int convertedChars = 0; while (1) { char *ch = (char *)string.data(); char *chEnd = ch + string.length(); ch += convertedChars; UErrorCode error = U_ZERO_ERROR; ucnv_fromUnicode(conv, &ch, chEnd, &uc, end, 0, false, &error); if (!U_SUCCESS(error)) qDebug() << "convertFromUnicode failed:" << u_errorName(error); convertedChars = ch - string.data(); if (uc >= end) break; string.resize(string.length()*2); } string.resize(convertedChars); if (!state) ucnv_close(conv); return string; }
static void xh_encoder_uconv_destroy(UConverter *uconv) { if (uconv != NULL) { ucnv_close(uconv); } }
static void mm_func_destroy(void *ptr) { mm_cipher_context_t *ctx = (mm_cipher_context_t *) ptr; if (--ctx->ref_count == 0) { ucnv_close(ctx->cnv); sqlite3_free(ctx); } }
void TextCodecICU::releaseICUConverter() const { if (m_converterICU) { if (cachedConverterICU) ucnv_close(cachedConverterICU); cachedConverterICU = m_converterICU; m_converterICU = 0; } }
int32_t CharsetMatch::getUChars(UChar *buf, int32_t cap, UErrorCode *status) const { UConverter *conv = ucnv_open(getName(), status); int32_t result = ucnv_toUChars(conv, buf, cap, (const char *) textIn->fRawInput, textIn->fRawLength, status); ucnv_close(conv); return result; }
void TextCodecICU::releaseICUConverter() const { if (m_converterICU) { UConverter*& cachedConverter = cachedConverterICU(); if (cachedConverter) ucnv_close(cachedConverter); cachedConverter = m_converterICU; m_converterICU = 0; } }
void StreamingTextDecoderICU::releaseICUConverter() { if (m_converterICU) { if (cachedConverterICU != 0) ucnv_close(cachedConverterICU); cachedConverterICU = m_converterICU; cachedConverterEncoding = m_encoding.encodingID(); m_converterICU = 0; } }
/*---------------------------------------------------------------------------------------------- The class destructor. Assumptions: <text> Exit conditions: <text> Parameters: None ----------------------------------------------------------------------------------------------*/ UnicodeConverter::~UnicodeConverter() { assert(!"necessary to explicitly delete sole instance"); if (m_converter != 0) ucnv_close(m_converter); // close the converter if (s_instance == this) s_instance = 0; }
static void U_CALLCONV _HZClose(UConverter *cnv){ if(cnv->extraInfo != NULL) { ucnv_close (((UConverterDataHZ *) (cnv->extraInfo))->gbConverter); if(!cnv->isExtraLocal) { uprv_free(cnv->extraInfo); } cnv->extraInfo = NULL; } }
librevenge::RVNGString libvisio::VSDMetaData::readCodePageString(librevenge::RVNGInputStream *input) { uint32_t size = readU32(input); if (size > getRemainingLength(input)) size = getRemainingLength(input); if (size == 0) return librevenge::RVNGString(); std::vector<unsigned char> characters; for (uint32_t i = 0; i < size; ++i) characters.push_back(readU8(input)); uint32_t codepage = getCodePage(); librevenge::RVNGString string; if (codepage == 65001) { // http://msdn.microsoft.com/en-us/library/windows/desktop/dd374130%28v=vs.85%29.aspx // says this is UTF-8. characters.push_back(0); string.append(reinterpret_cast<const char *>(characters.data())); } else { UErrorCode status = U_ZERO_ERROR; UConverter *conv = nullptr; switch (codepage) { case 1252: // http://msdn.microsoft.com/en-us/goglobal/bb964654 conv = ucnv_open("windows-1252", &status); break; } if (U_SUCCESS(status) && conv) { assert(!characters.empty()); const auto *src = (const char *)&characters[0]; const char *srcLimit = (const char *)src + characters.size(); while (src < srcLimit) { UChar32 ucs4Character = ucnv_getNextUChar(conv, &src, srcLimit, &status); if (U_SUCCESS(status) && U_IS_UNICODE_CHAR(ucs4Character)) appendUCS4(string, ucs4Character); } } if (conv) ucnv_close(conv); } return string; }
void constructEventSsid(char *eventstr) { char *pos = NULL; char *tmp = NULL; char ssid[BUF_SIZE] = {0}; char ssid_txt[BUF_SIZE] = {0}; char buf[BUF_SIZE] = {0}; bool isUTF8 = false, isCh = false; UConverterType conType = UCNV_UTF8; char dest[CONVERT_LINE_LEN] = {0}; UErrorCode err = U_ZERO_ERROR; UConverter* pConverter = ucnv_open(CHARSET_CN, &err); if (U_FAILURE(err)) { ALOGE("ucnv_open error"); return; } tmp = strstr(eventstr, " SSID"); if (strlen(tmp) > 6 ) { if(!strstr(tmp,"=")) sscanf(tmp + 7, "%[^\']", ssid); else sscanf(tmp + 6, "%s", ssid); if (DBG) ALOGD("%s, SSID = %s", __FUNCTION__, ssid); ssid_decode(buf,BUF_SIZE,ssid); isUTF8 = isUTF8String(buf,sizeof(buf)); isCh = false; for (pos = buf; '\0' != *pos; pos++) { if (0x80 == (*pos & 0x80)) { isCh = true; break; } } if (!isUTF8 && isCh) { ucnv_toAlgorithmic(conType, pConverter, dest, CONVERT_LINE_LEN, buf, strlen(buf), &err); if (U_FAILURE(err)) { ALOGE("ucnv_toUChars error"); goto EXIT; } ssid_encode(ssid_txt, BUF_SIZE, dest, strlen(dest)); if (!strstr(tmp,"=")) snprintf(eventstr + (strlen(eventstr) - strlen(tmp)), strlen(eventstr), " SSID \'%s\'", ssid_txt); else snprintf(eventstr + (strlen(eventstr) - strlen(tmp)), strlen(eventstr), " SSID=%s", ssid_txt); if (DBG) ALOGD("%s, ssid_txt = %s, eventsrt = %s", __FUNCTION__, ssid_txt, eventstr); } } EXIT: ucnv_close(pConverter); }
static UBool * getResultsManually(const char** encodings, int32_t num_encodings, const char *utf8, int32_t length, const USet* excludedCodePoints, const UConverterUnicodeSet whichSet) { UBool* resultsManually; int32_t i; resultsManually = (UBool*) uprv_malloc(gCountAvailable); uprv_memset(resultsManually, 0, gCountAvailable); for(i = 0 ; i < num_encodings ; i++) { UErrorCode status = U_ZERO_ERROR; /* get unicode set for that converter */ USet* set; UConverter* test_converter; UChar32 cp; int32_t encIndex, offset; set = uset_openEmpty(); test_converter = ucnv_open(encodings[i], &status); ucnv_getUnicodeSet(test_converter, set, whichSet, &status); if (excludedCodePoints != NULL) { uset_addAll(set, excludedCodePoints); } uset_freeze(set); offset = 0; cp = 0; encIndex = findIndex(encodings[i]); /* * The following is almost, but not entirely, the same as * resultsManually[encIndex] = * (UBool)(uset_spanUTF8(set, utf8, length, USET_SPAN_SIMPLE) == length); * They might be different if the set contains strings, * or if the utf8 string contains an illegal sequence. * * The UConverterSelector does not currently handle strings that can be * converted, and it treats an illegal sequence as convertible * while uset_spanUTF8() treats it like U+FFFD which may not be convertible. */ resultsManually[encIndex] = TRUE; while(offset<length) { U8_NEXT(utf8, offset, length, cp); if (cp >= 0 && !uset_contains(set, cp)) { resultsManually[encIndex] = FALSE; break; } } uset_close(set); ucnv_close(test_converter); } return resultsManually; }