/// get page from network bool getPage(const char* url, int method, std::string& response) { // Todo : add proxy and authentication support response.clear(); string strurl = url; boost::regex regspace(" "); strurl = boost::regex_replace(strurl, regspace, "%20"); HttpWrap *phttpwrap = new HttpWrap(); response += phttpwrap->Get(strurl.c_str()); delete phttpwrap; phttpwrap = NULL; if (response.length() < 200) { return false; } else { boost::regex regRn("\\r\\n"); response = boost::regex_replace(response, regRn, "\\n"); boost::regex reg1("<meta(.*?)charset=(.*?)>",boost::regex::icase); boost::smatch what; std::string::const_iterator start = response.begin(); std::string::const_iterator end1 = response.end(); if( boost::regex_search(start, end1, what, reg1) ) { string strcharset(what[2].first,what[2].second); if((int)strcharset.length() == 0) { if(!IsUTF8(response.c_str(),response.length())) { string strtmp = ""; iconv_string("gbk","utf-8", response.c_str(), response.length(),strtmp,1); response = strtmp; } } else { if ((int)strcharset.find("UTF") >= 0 || (int)strcharset.find("utf") >= 0) { ; } else { string strtmp = ""; iconv_string("gbk","utf-8", response.c_str(), response.length(),strtmp,1); response = strtmp; } } } else if(!IsUTF8(response.c_str(),response.length())/*pSpiderconf->m_config.ruleCollections[pSpiderconf->m_config.plans[m_plan_id].collectionRuleId].charset != "utf-8"*/) { string strtmp = ""; iconv_string("gbk","utf-8", response.c_str(), response.length(),strtmp,1); response = strtmp; } } return true; }
NS_IMETHODIMP nsUTF8ConverterService::ConvertStringToUTF8(const nsACString &aString, const char *aCharset, bool aSkipCheck, bool aAllowSubstitution, uint8_t aOptionalArgc, nsACString &aUTF8String) { bool allowSubstitution = (aOptionalArgc == 1) ? aAllowSubstitution : true; // return if ASCII only or valid UTF-8 providing that the ASCII/UTF-8 // check is requested. It may not be asked for if a caller suspects // that the input is in non-ASCII 7bit charset (ISO-2022-xx, HZ) or // it's in a charset other than UTF-8 that can be mistaken for UTF-8. if (!aSkipCheck && (IsASCII(aString) || IsUTF8(aString))) { aUTF8String = aString; return NS_OK; } aUTF8String.Truncate(); nsresult rv = ToUTF8(aString, aCharset, allowSubstitution, aUTF8String); // additional protection for cases where check is skipped and the input // is actually in UTF-8 as opposed to aCharset. (i.e. caller's hunch // was wrong.) We don't check ASCIIness assuming there's no charset // incompatible with ASCII (we don't support EBCDIC). if (aSkipCheck && NS_FAILED(rv) && IsUTF8(aString)) { aUTF8String = aString; return NS_OK; } return rv; }
void CWrapper_encoding_isutf8(char **characters, int *numres) { char* s = *characters; int l; l = strlen(s); *numres = IsUTF8(s,l); }
NS_IMETHODIMP nsUTF8ConverterService::ConvertURISpecToUTF8(const nsACString &aSpec, const char *aCharset, nsACString &aUTF8Spec) { // assume UTF-8 if the spec contains unescaped non-ASCII characters. // No valid spec in Mozilla would break this assumption. if (!IsASCII(aSpec)) { aUTF8Spec = aSpec; return NS_OK; } aUTF8Spec.Truncate(); nsAutoCString unescapedSpec; // NS_UnescapeURL does not fill up unescapedSpec unless there's at least // one character to unescape. bool written = NS_UnescapeURL(PromiseFlatCString(aSpec).get(), aSpec.Length(), esc_OnlyNonASCII, unescapedSpec); if (!written) { aUTF8Spec = aSpec; return NS_OK; } // return if ASCII only or escaped UTF-8 if (IsASCII(unescapedSpec) || IsUTF8(unescapedSpec)) { aUTF8Spec = unescapedSpec; return NS_OK; } return ToUTF8(unescapedSpec, aCharset, true, aUTF8Spec); }
BOOL StrictUTF8(char *s) { char *invalid_point = NULL; if (!IsUTF8(s, NULL, &invalid_point) && invalid_point) { *invalid_point = 0; return TRUE; } return FALSE; }
bool CMainWindow::LoadFile(LPCTSTR filename) { SendEditor(SCI_SETREADONLY, FALSE); SendEditor(SCI_CLEARALL); SendEditor(EM_EMPTYUNDOBUFFER); SendEditor(SCI_SETSAVEPOINT); SendEditor(SCI_CANCEL); SendEditor(SCI_SETUNDOCOLLECTION, 0); FILE *fp = NULL; _tfopen_s(&fp, filename, _T("rb")); if (fp) { //SetTitle(); char data[4096]; int lenFile = fread(data, 1, sizeof(data), fp); bool bUTF8 = IsUTF8(data, lenFile); while (lenFile > 0) { SendEditor(SCI_ADDTEXT, lenFile, reinterpret_cast<LPARAM>(static_cast<char *>(data))); lenFile = fread(data, 1, sizeof(data), fp); } fclose(fp); SendEditor(SCI_SETCODEPAGE, bUTF8 ? SC_CP_UTF8 : GetACP()); } else { return false; } SendEditor(SCI_SETUNDOCOLLECTION, 1); ::SetFocus(m_hWndEdit); SendEditor(EM_EMPTYUNDOBUFFER); SendEditor(SCI_SETSAVEPOINT); SendEditor(SCI_GOTOPOS, 0); SendEditor(SCI_SETREADONLY, TRUE); SendEditor(SCI_CLEARDOCUMENTSTYLE, 0, 0); SendEditor(SCI_SETSTYLEBITS, 5, 0); //SetAStyle(SCE_DIFF_DEFAULT, RGB(0, 0, 0)); SetAStyle(SCE_DIFF_COMMAND, RGB(0x0A, 0x24, 0x36)); SetAStyle(SCE_DIFF_POSITION, RGB(0xFF, 0, 0)); SetAStyle(SCE_DIFF_HEADER, RGB(0x80, 0, 0), RGB(0xFF, 0xFF, 0x80)); SetAStyle(SCE_DIFF_COMMENT, RGB(0, 0x80, 0)); SendEditor(SCI_STYLESETBOLD, SCE_DIFF_COMMENT, TRUE); SetAStyle(SCE_DIFF_DELETED, ::GetSysColor(COLOR_WINDOWTEXT), RGB(0xFF, 0x80, 0x80)); SetAStyle(SCE_DIFF_ADDED, ::GetSysColor(COLOR_WINDOWTEXT), RGB(0x80, 0xFF, 0x80)); SendEditor(SCI_SETLEXER, SCLEX_DIFF); SendEditor(SCI_SETKEYWORDS, 0, (LPARAM)"revision"); SendEditor(SCI_COLOURISE, 0, -1); ::ShowWindow(m_hWndEdit, SW_SHOW); return true; }
static inline void MaybeFromLocaleRep (char **str) { char *const orig_str = *str; if ((orig_str != NULL) && !IsUTF8 (orig_str)) { *str = FromLocaleDup (orig_str); free (orig_str); } }
static bool IsSDPString (const char *str) { if (strchr (str, '\r') != NULL) return false; if (strchr (str, '\n') != NULL) return false; if (!IsUTF8 (str)) return false; return true; }
// XXX : aTryLocaleCharset is not yet effective. nsresult nsMIMEHeaderParamImpl::DoGetParameter(const nsACString& aHeaderVal, const char *aParamName, ParamDecoding aDecoding, const nsACString& aFallbackCharset, bool aTryLocaleCharset, char **aLang, nsAString& aResult) { aResult.Truncate(); nsresult rv; // get parameter (decode RFC 2231/5987 when applicable, as specified by // aDecoding (5987 being a subset of 2231) and return charset.) nsXPIDLCString med; nsXPIDLCString charset; rv = DoParameterInternal(PromiseFlatCString(aHeaderVal).get(), aParamName, aDecoding, getter_Copies(charset), aLang, getter_Copies(med)); if (NS_FAILED(rv)) return rv; // convert to UTF-8 after charset conversion and RFC 2047 decoding // if necessary. nsCAutoString str1; rv = DecodeParameter(med, charset.get(), nullptr, false, str1); NS_ENSURE_SUCCESS(rv, rv); if (!aFallbackCharset.IsEmpty()) { nsCAutoString str2; nsCOMPtr<nsIUTF8ConverterService> cvtUTF8(do_GetService(NS_UTF8CONVERTERSERVICE_CONTRACTID)); if (cvtUTF8 && NS_SUCCEEDED(cvtUTF8->ConvertStringToUTF8(str1, PromiseFlatCString(aFallbackCharset).get(), false, true, 1, str2))) { CopyUTF8toUTF16(str2, aResult); return NS_OK; } } if (IsUTF8(str1)) { CopyUTF8toUTF16(str1, aResult); return NS_OK; } if (aTryLocaleCharset && !NS_IsNativeUTF8()) return NS_CopyNativeToUnicode(str1, aResult); CopyASCIItoUTF16(str1, aResult); return NS_OK; }
nsresult nsTextToSubURI::convertURItoUnicode(const nsAFlatCString &aCharset, const nsAFlatCString &aURI, bool aIRI, nsAString &_retval) { nsresult rv = NS_OK; // check for 7bit encoding the data may not be ASCII after we decode bool isStatefulCharset = statefulCharset(aCharset.get()); if (!isStatefulCharset && IsASCII(aURI)) { CopyASCIItoUTF16(aURI, _retval); return rv; } if (!isStatefulCharset && aIRI) { if (IsUTF8(aURI)) { CopyUTF8toUTF16(aURI, _retval); return rv; } } // empty charset could indicate UTF-8, but aURI turns out not to be UTF-8. NS_ENSURE_FALSE(aCharset.IsEmpty(), NS_ERROR_INVALID_ARG); nsCOMPtr<nsICharsetConverterManager> charsetConverterManager; charsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder; rv = charsetConverterManager->GetUnicodeDecoder(aCharset.get(), getter_AddRefs(unicodeDecoder)); NS_ENSURE_SUCCESS(rv, rv); unicodeDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal); int32_t srcLen = aURI.Length(); int32_t dstLen; rv = unicodeDecoder->GetMaxLength(aURI.get(), srcLen, &dstLen); NS_ENSURE_SUCCESS(rv, rv); char16_t *ustr = (char16_t *) NS_Alloc(dstLen * sizeof(char16_t)); NS_ENSURE_TRUE(ustr, NS_ERROR_OUT_OF_MEMORY); rv = unicodeDecoder->Convert(aURI.get(), &srcLen, ustr, &dstLen); if (NS_SUCCEEDED(rv)) _retval.Assign(ustr, dstLen); NS_Free(ustr); return rv; }
nsresult net_GetFileFromURLSpec(const nsACString &aURL, nsIFile **result) { // NOTE: See also the implementation in nsURLHelperOSX.cpp, // which is based on this. nsresult rv; nsCOMPtr<nsILocalFile> localFile; rv = NS_NewNativeLocalFile(EmptyCString(), PR_TRUE, getter_AddRefs(localFile)); if (NS_FAILED(rv)) return rv; nsCAutoString directory, fileBaseName, fileExtension, path; rv = net_ParseFileURL(aURL, directory, fileBaseName, fileExtension); if (NS_FAILED(rv)) return rv; if (!directory.IsEmpty()) NS_EscapeURL(directory, esc_Directory|esc_AlwaysCopy, path); if (!fileBaseName.IsEmpty()) NS_EscapeURL(fileBaseName, esc_FileBaseName|esc_AlwaysCopy, path); if (!fileExtension.IsEmpty()) { path += '.'; NS_EscapeURL(fileExtension, esc_FileExtension|esc_AlwaysCopy, path); } NS_UnescapeURL(path); if (path.Length() != strlen(path.get())) return NS_ERROR_FILE_INVALID_PATH; if (IsUTF8(path)) { // speed up the start-up where UTF-8 is the native charset // (e.g. on recent Linux distributions) if (NS_IsNativeUTF8()) rv = localFile->InitWithNativePath(path); else rv = localFile->InitWithPath(NS_ConvertUTF8toUTF16(path)); // XXX In rare cases, a valid UTF-8 string can be valid as a native // encoding (e.g. 0xC5 0x83 is valid both as UTF-8 and Windows-125x). // However, the chance is very low that a meaningful word in a legacy // encoding is valid as UTF-8. } else // if path is not in UTF-8, assume it is encoded in the native charset rv = localFile->InitWithNativePath(path); if (NS_FAILED(rv)) return rv; NS_ADDREF(*result = localFile); return NS_OK; }
// XXX : aTryLocaleCharset is not yet effective. NS_IMETHODIMP nsMIMEHeaderParamImpl::GetParameter(const nsACString& aHeaderVal, const char *aParamName, const nsACString& aFallbackCharset, PRBool aTryLocaleCharset, char **aLang, nsAString& aResult) { aResult.Truncate(); nsresult rv; // get parameter (decode RFC 2231 if it's RFC 2231-encoded and // return charset.) nsXPIDLCString med; nsXPIDLCString charset; rv = GetParameterInternal(PromiseFlatCString(aHeaderVal).get(), aParamName, getter_Copies(charset), aLang, getter_Copies(med)); if (NS_FAILED(rv)) return rv; // convert to UTF-8 after charset conversion and RFC 2047 decoding // if necessary. nsCAutoString str1; rv = DecodeParameter(med, charset.get(), nsnull, PR_FALSE, str1); NS_ENSURE_SUCCESS(rv, rv); if (!aFallbackCharset.IsEmpty()) { nsCAutoString str2; nsCOMPtr<nsIUTF8ConverterService> cvtUTF8(do_GetService(NS_UTF8CONVERTERSERVICE_CONTRACTID)); if (cvtUTF8 && NS_SUCCEEDED(cvtUTF8->ConvertStringToUTF8(str1, PromiseFlatCString(aFallbackCharset).get(), PR_FALSE, str2))) { CopyUTF8toUTF16(str2, aResult); return NS_OK; } } if (IsUTF8(str1)) { CopyUTF8toUTF16(str1, aResult); return NS_OK; } if (aTryLocaleCharset && !NS_IsNativeUTF8()) return NS_CopyNativeToUnicode(str1, aResult); CopyASCIItoUTF16(str1, aResult); return NS_OK; }
static void test (const char *in, const char *out) { bool isutf8 = !strcmp (in, out); char *str = strdup (in); if (str == NULL) abort (); if (isutf8) printf ("\"%s\" should be accepted...\n", in); else printf ("\"%s\" should be rewritten as \"%s\"...\n", in, out); if ((IsUTF8 (in) != NULL) != isutf8) { printf (" ERROR: IsUTF8 (%s) failed\n", in); exit (1); } if ((EnsureUTF8 (str) != NULL) != isutf8) { printf (" ERROR: EnsureUTF8 (%s) failed\n", in); exit (2); } if (strcmp (str, out)) { printf (" ERROR: got \"%s\"\n", str); exit (3); } if ((EnsureUTF8 (str) == NULL) || IsUTF8 (str) == NULL) { printf (" ERROR: EnsureUTF8 (%s) is not UTF-8\n", in); exit (4); } free (str); }
/* ACString ConvertUTF8toACE (in AUTF8String input); */ NS_IMETHODIMP nsIDNService::ConvertUTF8toACE(const nsACString & input, nsACString & ace) { // protect against bogus input NS_ENSURE_TRUE(IsUTF8(input), NS_ERROR_UNEXPECTED); nsresult rv; NS_ConvertUTF8toUCS2 ustr(input); // map ideographic period to ASCII period etc. normalizeFullStops(ustr); PRUint32 len, offset; len = 0; offset = 0; nsCAutoString encodedBuf; nsAString::const_iterator start, end; ustr.BeginReading(start); ustr.EndReading(end); ace.Truncate(); // encode nodes if non ASCII while (start != end) { len++; if (*start++ == (PRUnichar)'.') { rv = stringPrepAndACE(Substring(ustr, offset, len - 1), encodedBuf); NS_ENSURE_SUCCESS(rv, rv); ace.Append(encodedBuf); ace.Append('.'); offset += len; len = 0; } } // add extra node for multilingual test bed if (mMultilingualTestBed) ace.AppendLiteral("mltbd."); // encode the last node if non ASCII if (len) { rv = stringPrepAndACE(Substring(ustr, offset, len), encodedBuf); NS_ENSURE_SUCCESS(rv, rv); ace.Append(encodedBuf); } return NS_OK; }
BOOL Cfg::IniStrToW(char *str, WCHAR *wstr) { int len = (int)strlen(str) + 1; if (needIniConvert && *str == '|') { // old style hexstr2bin(str + 1, (BYTE *)wstr, len, &len); } else { if (needIniConvert && !IsUTF8(str)) { AtoW(str, wstr, len); } else { U8toW(str, wstr, len); } } return TRUE; }
bool CMainWindow::LoadFile(HANDLE hFile) { InitEditor(); char data[4096] = { 0 }; DWORD dwRead = 0; BOOL bRet = ReadFile(hFile, data, sizeof(data), &dwRead, NULL); bool bUTF8 = IsUTF8(data, dwRead); while ((dwRead > 0) && (bRet)) { SendEditor(SCI_ADDTEXT, dwRead, reinterpret_cast<LPARAM>(static_cast<char *>(data))); bRet = ReadFile(hFile, data, sizeof(data), &dwRead, NULL); } SetupWindow(bUTF8); return true; }
NS_IMETHODIMP nsIDNService::Normalize(const nsACString & input, nsACString & output) { // protect against bogus input NS_ENSURE_TRUE(IsUTF8(input), NS_ERROR_UNEXPECTED); NS_ConvertUTF8toUTF16 inUTF16(input); normalizeFullStops(inUTF16); nsAutoString outUTF16; nsresult rv = stringPrep(inUTF16, outUTF16); if (NS_FAILED(rv)) return rv; CopyUTF16toUTF8(outUTF16, output); if (!isOnlySafeChars(outUTF16, mIDNBlacklist)) return ConvertUTF8toACE(output, output); return NS_OK; }
/* AUTF8String normalize(in AUTF8String input); */ NS_IMETHODIMP nsIDNService::Normalize(const nsACString & input, nsACString & output) { // protect against bogus input NS_ENSURE_TRUE(IsUTF8(input), NS_ERROR_UNEXPECTED); NS_ConvertUTF8toUTF16 inUTF16(input); normalizeFullStops(inUTF16); // pass the domain name to stringprep label by label nsAutoString outUTF16, outLabel; PRUint32 len = 0, offset = 0; nsresult rv; nsAString::const_iterator start, end; inUTF16.BeginReading(start); inUTF16.EndReading(end); while (start != end) { len++; if (*start++ == PRUnichar('.')) { rv = stringPrep(Substring(inUTF16, offset, len - 1), outLabel); NS_ENSURE_SUCCESS(rv, rv); outUTF16.Append(outLabel); outUTF16.Append(PRUnichar('.')); offset += len; len = 0; } } if (len) { rv = stringPrep(Substring(inUTF16, offset, len), outLabel); NS_ENSURE_SUCCESS(rv, rv); outUTF16.Append(outLabel); } CopyUTF16toUTF8(outUTF16, output); if (!isOnlySafeChars(outUTF16, mIDNBlacklist)) return ConvertUTF8toACE(output, output); return NS_OK; }
__declspec(dllexport) BYTE* CompileFile(char* Filename, DWORD* CompiledSize){ BYTE* Buffer=NULL; DWORD BufferSize=0; BYTE* ret; if(callbacks.Dll_LoadFile){ Buffer = callbacks.Dll_LoadFile(ExtData, Filename, &BufferSize); } if(Buffer==NULL){ if(callbacks.Dll_AddError) callbacks.Dll_AddError(ExtData, 0, "Error opening script."); if(CompiledSize) *CompiledSize = 0; return NULL; } else{ if(IsUTF8(Buffer, BufferSize)) ret = CompileBuffer(Buffer+3, Filename, BufferSize-3, CompiledSize); else ret = CompileBuffer(Buffer, Filename, BufferSize, CompiledSize); if(callbacks.Dll_CloseFile) callbacks.Dll_CloseFile(ExtData, Buffer); return ret; } }
NS_IMETHODIMP nsMIMEHeaderParamImpl::DecodeRFC2047Header(const char* aHeaderVal, const char* aDefaultCharset, PRBool aOverrideCharset, PRBool aEatContinuations, nsACString& aResult) { aResult.Truncate(); if (!aHeaderVal) return NS_ERROR_INVALID_ARG; if (!*aHeaderVal) return NS_OK; // If aHeaderVal is RFC 2047 encoded or is not a UTF-8 string but // aDefaultCharset is specified, decodes RFC 2047 encoding and converts // to UTF-8. Otherwise, just strips away CRLF. if (PL_strstr(aHeaderVal, "=?") || aDefaultCharset && (!IsUTF8(nsDependentCString(aHeaderVal)) || Is7bitNonAsciiString(aHeaderVal, PL_strlen(aHeaderVal)))) { DecodeRFC2047Str(aHeaderVal, aDefaultCharset, aOverrideCharset, aResult); } else if (aEatContinuations && (PL_strchr(aHeaderVal, '\n') || PL_strchr(aHeaderVal, '\r'))) { aResult = aHeaderVal; } else { aEatContinuations = PR_FALSE; aResult = aHeaderVal; } if (aEatContinuations) { nsCAutoString temp(aResult); temp.ReplaceSubstring("\n\t", " "); temp.ReplaceSubstring("\r\t", " "); temp.StripChars("\r\n"); aResult = temp; } return NS_OK; }
NS_IMETHODIMP nsTextToSubURI::UnEscapeNonAsciiURI(const nsACString & aCharset, const nsACString & aURIFragment, nsAString &_retval) { nsAutoCString unescapedSpec; NS_UnescapeURL(PromiseFlatCString(aURIFragment), esc_AlwaysCopy | esc_OnlyNonASCII, unescapedSpec); // leave the URI as it is if it's not UTF-8 and aCharset is not a ASCII // superset since converting "http:" with such an encoding is always a bad // idea. if (!IsUTF8(unescapedSpec) && (aCharset.LowerCaseEqualsLiteral("utf-16") || aCharset.LowerCaseEqualsLiteral("utf-16be") || aCharset.LowerCaseEqualsLiteral("utf-16le") || aCharset.LowerCaseEqualsLiteral("utf-7") || aCharset.LowerCaseEqualsLiteral("x-imap4-modified-utf7"))){ CopyASCIItoUTF16(aURIFragment, _retval); return NS_OK; } return convertURItoUnicode(PromiseFlatCString(aCharset), unescapedSpec, true, _retval); }
bool OggCodecState::AddVorbisComment(MetadataTags* aTags, const char* aComment, uint32_t aLength) { const char* div = (const char*)memchr(aComment, '=', aLength); if (!div) { LOG(PR_LOG_DEBUG, ("Skipping comment: no separator")); return false; } nsCString key = nsCString(aComment, div-aComment); if (!IsValidVorbisTagName(key)) { LOG(PR_LOG_DEBUG, ("Skipping comment: invalid tag name")); return false; } uint32_t valueLength = aLength - (div-aComment); nsCString value = nsCString(div + 1, valueLength); if (!IsUTF8(value)) { LOG(PR_LOG_DEBUG, ("Skipping comment: invalid UTF-8 in value")); return false; } aTags->Put(key, value); return true; }
bool CMainWindow::LoadFile(LPCTSTR filename) { InitEditor(); FILE *fp = NULL; _tfopen_s(&fp, filename, L"rb"); if (!fp) return false; //SetTitle(); char data[4096] = { 0 }; size_t lenFile = fread(data, 1, sizeof(data), fp); bool bUTF8 = IsUTF8(data, lenFile); while (lenFile > 0) { SendEditor(SCI_ADDTEXT, lenFile, reinterpret_cast<LPARAM>(static_cast<char *>(data))); lenFile = fread(data, 1, sizeof(data), fp); } fclose(fp); SetupWindow(bUTF8); m_filename = filename; return true; }
NS_IMETHODIMP nsSubscribableServer::SetState(const nsACString &aPath, PRBool aState, PRBool *aStateChanged) { nsresult rv = NS_OK; NS_ASSERTION(!aPath.IsEmpty() && aStateChanged, "no path or stateChanged"); if (aPath.IsEmpty() || !aStateChanged) return NS_ERROR_NULL_POINTER; NS_ASSERTION(IsUTF8(aPath), "aPath is not in UTF-8"); *aStateChanged = PR_FALSE; SubscribeTreeNode *node = nsnull; rv = FindAndCreateNode(aPath, &node); NS_ENSURE_SUCCESS(rv,rv); NS_ASSERTION(node,"didn't find the node"); if (!node) return NS_ERROR_FAILURE; NS_ASSERTION(node->isSubscribable, "fix this"); if (!node->isSubscribable) { return NS_OK; } if (node->isSubscribed == aState) { return NS_OK; } else { node->isSubscribed = aState; *aStateChanged = PR_TRUE; rv = NotifyChange(node, kNC_Subscribed, node->isSubscribed); NS_ENSURE_SUCCESS(rv,rv); } return rv; }
char * xml_ansitoutf8(char* wText) { char * utf8RetStr = NULL; int tmpLen = 0; if(!wText) return utf8RetStr; if(!IsUTF8(wText)) { utf8RetStr = ANSIToUTF8(wText); tmpLen = !utf8RetStr ? 0 : strlen(utf8RetStr); if(tmpLen == 1) { if(utf8RetStr) free(utf8RetStr); utf8RetStr = UnicodeToUTF8((wchar_t *)wText); } } else { tmpLen = strlen(wText)+1; utf8RetStr = (char *)malloc(tmpLen); memcpy(utf8RetStr, wText, tmpLen); } return utf8RetStr; }
static int ReadDir(stream_t *demux, input_item_node_t *subitems) { char *line; while ((line = vlc_stream_ReadLine(demux->s)) != NULL) { if (!IsUTF8(line)) goto skip; if (!strcmp(line, "[Reference]")) goto skip; const char *key = line; char *value = strchr(line, '='); if (value == NULL) { msg_Warn(demux, "unexpected entry \"%s\"", line); goto skip; } *(value++) = '\0'; unsigned id; if (sscanf(key, "Ref%u", &id) != 1) { msg_Warn(demux, "unexpected entry key \"%s\"", key); goto skip; } if (!strncasecmp(value, "http://", 7)) memcpy(value, "mmsh", 4); /* Force MMSH access/demux */ input_item_t *item = input_item_New(value, value); input_item_node_AppendItem(subitems, item); input_item_Release(item); skip: free(line); } return VLC_SUCCESS; }
static char *CheckUnicode (const char *str) { return IsUTF8 (str) ? strdup (str): NULL; }
static char *GuessEncoding (const char *str) { return IsUTF8 (str) ? strdup (str) : FromLatin1 (str); }
static void AppendErrorTextMismatch(const nsString &host, nsIX509Cert* ix509, nsINSSComponent *component, bool wantsHtml, nsString &returnedMessage) { const char16_t *params[1]; nsresult rv; ScopedCERTCertificate nssCert(ix509->GetCert()); if (!nssCert) { // We are unable to extract the valid names, say "not valid for name". params[0] = host.get(); nsString formattedString; rv = component->PIPBundleFormatStringFromName("certErrorMismatch", params, 1, formattedString); if (NS_SUCCEEDED(rv)) { returnedMessage.Append(formattedString); returnedMessage.Append('\n'); } return; } nsString allNames; uint32_t nameCount = 0; bool useSAN = false; if (nssCert) useSAN = GetSubjectAltNames(nssCert.get(), component, allNames, nameCount); if (!useSAN) { char *certName = CERT_GetCommonName(&nssCert->subject); if (certName) { nsDependentCSubstring commonName(certName, strlen(certName)); if (IsUTF8(commonName)) { // Bug 1024781 // We should actually check that the common name is a valid dns name or // ip address and not any string value before adding it to the display // list. ++nameCount; allNames.Assign(NS_ConvertUTF8toUTF16(commonName)); } PORT_Free(certName); } } if (nameCount > 1) { nsString message; rv = component->GetPIPNSSBundleString("certErrorMismatchMultiple", message); if (NS_SUCCEEDED(rv)) { returnedMessage.Append(message); returnedMessage.AppendLiteral("\n "); returnedMessage.Append(allNames); returnedMessage.AppendLiteral(" \n"); } } else if (nameCount == 1) { const char16_t *params[1]; params[0] = allNames.get(); const char *stringID; if (wantsHtml) stringID = "certErrorMismatchSingle2"; else stringID = "certErrorMismatchSinglePlain"; nsString formattedString; rv = component->PIPBundleFormatStringFromName(stringID, params, 1, formattedString); if (NS_SUCCEEDED(rv)) { returnedMessage.Append(formattedString); returnedMessage.Append('\n'); } } else { // nameCount == 0 nsString message; nsresult rv = component->GetPIPNSSBundleString("certErrorMismatchNoNames", message); if (NS_SUCCEEDED(rv)) { returnedMessage.Append(message); returnedMessage.Append('\n'); } } }
// wrapper for ldap_get_values() // NS_IMETHODIMP nsLDAPMessage::GetValues(const char *aAttr, PRUint32 *aCount, PRUnichar ***aValues) { char **values; #if defined(DEBUG) // We only want this being logged for debug builds so as not to affect performance too much. PR_LOG(gLDAPLogModule, PR_LOG_DEBUG, ("nsLDAPMessage::GetValues(): called with aAttr = '%s'", aAttr)); #endif values = ldap_get_values(mConnectionHandle, mMsgHandle, aAttr); // bail out if there was a problem // if (!values) { PRInt32 lderrno = ldap_get_lderrno(mConnectionHandle, 0, 0); if ( lderrno == LDAP_DECODING_ERROR ) { // this may not be an error; it could just be that the // caller has asked for an attribute that doesn't exist. // PR_LOG(gLDAPLogModule, PR_LOG_WARNING, ("nsLDAPMessage::GetValues(): ldap_get_values returned " "LDAP_DECODING_ERROR")); return NS_ERROR_LDAP_DECODING_ERROR; } else if ( lderrno == LDAP_PARAM_ERROR ) { NS_ERROR("nsLDAPMessage::GetValues(): internal error: 1"); return NS_ERROR_UNEXPECTED; } else { NS_ERROR("nsLDAPMessage::GetValues(): internal error: 2"); return NS_ERROR_UNEXPECTED; } } // count the values // PRUint32 numVals = ldap_count_values(values); // create an array of the appropriate size // *aValues = static_cast<PRUnichar **>(nsMemory::Alloc(numVals * sizeof(PRUnichar *))); if (!*aValues) { ldap_value_free(values); return NS_ERROR_OUT_OF_MEMORY; } // clone the array (except for the trailing NULL entry) using the // shared allocator for XPCOM correctness // PRUint32 i; for ( i = 0 ; i < numVals ; i++ ) { nsDependentCString sValue(values[i]); if (IsUTF8(sValue)) (*aValues)[i] = ToNewUnicode(NS_ConvertUTF8toUTF16(sValue)); else (*aValues)[i] = ToNewUnicode(NS_ConvertASCIItoUTF16(sValue)); if ( ! (*aValues)[i] ) { NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(i, aValues); ldap_value_free(values); return NS_ERROR_OUT_OF_MEMORY; } } // now free our value array since we already cloned the values array // to the 'aValues' results array. ldap_value_free(values); *aCount = numVals; return NS_OK; }