NS_IMETHODIMP XULContentSinkImpl::ReportError(const PRUnichar* aErrorText, const PRUnichar* aSourceText, nsIScriptError *aError, bool *_retval) { NS_PRECONDITION(aError && aSourceText && aErrorText, "Check arguments!!!"); // The expat driver should report the error. *_retval = true; nsresult rv = NS_OK; // make sure to empty the context stack so that // <parsererror> could become the root element. mContextStack.Clear(); mState = eInProlog; // Clear any buffered-up text we have. It's enough to set the length to 0. // The buffer itself is allocated when we're created and deleted in our // destructor, so don't mess with it. mTextLength = 0; nsCOMPtr<nsIXULDocument> doc = do_QueryReferent(mDocument); if (doc && !doc->OnDocumentParserError()) { // The overlay was broken. Don't add a messy element to the master doc. return NS_OK; } const PRUnichar* noAtts[] = { 0, 0 }; NS_NAMED_LITERAL_STRING(errorNs, "http://www.mozilla.org/newlayout/xml/parsererror.xml"); nsAutoString parsererror(errorNs); parsererror.Append((PRUnichar)0xFFFF); parsererror.AppendLiteral("parsererror"); rv = HandleStartElement(parsererror.get(), noAtts, 0, -1, 0); NS_ENSURE_SUCCESS(rv,rv); rv = HandleCharacterData(aErrorText, NS_strlen(aErrorText)); NS_ENSURE_SUCCESS(rv,rv); nsAutoString sourcetext(errorNs); sourcetext.Append((PRUnichar)0xFFFF); sourcetext.AppendLiteral("sourcetext"); rv = HandleStartElement(sourcetext.get(), noAtts, 0, -1, 0); NS_ENSURE_SUCCESS(rv,rv); rv = HandleCharacterData(aSourceText, NS_strlen(aSourceText)); NS_ENSURE_SUCCESS(rv,rv); rv = HandleEndElement(sourcetext.get()); NS_ENSURE_SUCCESS(rv,rv); rv = HandleEndElement(parsererror.get()); NS_ENSURE_SUCCESS(rv,rv); return rv; }
PRUnichar* NS_strdup(const PRUnichar *aString) { uint32_t len = NS_strlen(aString); return NS_strndup(aString, len); }
/* void onStartLookup (in wstring searchString, in nsIAutoCompleteResults previousSearchResult, in nsIAutoCompleteListener listener); */ NS_IMETHODIMP nsLDAPAutoCompleteSession::OnStartLookup(const PRUnichar *searchString, nsIAutoCompleteResults *previousSearchResult, nsIAutoCompleteListener *listener) { NS_ENSURE_ARG_POINTER(listener); if (!mFormatter) { NS_WARNING("mFormatter should not be null for nsLDAPAutoCompleteSession::OnStartLookup"); return NS_ERROR_NOT_INITIALIZED; } nsresult rv; // Hold return values from XPCOM calls #ifdef PR_LOGGING // Initialize logging, if it hasn't been already. if (!sLDAPAutoCompleteLogModule) { sLDAPAutoCompleteLogModule = PR_NewLogModule("ldapautocomplete"); NS_ABORT_IF_FALSE(sLDAPAutoCompleteLogModule, "failed to initialize ldapautocomplete log module"); } #endif PR_LOG(sLDAPAutoCompleteLogModule, PR_LOG_DEBUG, ("nsLDAPAutoCompleteSession::OnStartLookup entered\n")); mListener = listener; // Ignore the empty string, strings with @ in them, and strings // that are too short. if (searchString[0] == 0 || nsDependentString(searchString).FindChar(PRUnichar('@'), 0) != -1 || nsDependentString(searchString).FindChar(PRUnichar(','), 0) != -1 || ( !IS_CJK_CHAR_FOR_LDAP(searchString[0]) ? mMinStringLength && NS_strlen(searchString) < mMinStringLength : mCjkMinStringLength && NS_strlen(searchString) < mCjkMinStringLength ) ) { FinishAutoCompleteLookup(nsIAutoCompleteStatus::ignored, 0, mState); return NS_OK; } else { mSearchString = searchString; // save it for later use } // Make sure this was called appropriately. if (mState == SEARCHING || mState == BINDING) { NS_ERROR("nsLDAPAutoCompleteSession::OnStartLookup(): called while " "search already in progress; no lookup started."); FinishAutoCompleteLookup(nsIAutoCompleteStatus::failureItems, NS_ERROR_FAILURE, mState); return NS_ERROR_FAILURE; } // See if this is a narrow search that we could potentially do locally. if (previousSearchResult) { // Get the string representing previous search results. nsString prevSearchStr; rv = previousSearchResult->GetSearchString( getter_Copies(prevSearchStr)); if ( NS_FAILED(rv) ) { NS_ERROR("nsLDAPAutoCompleteSession::OnStartLookup(): couldn't " "get search string from previousSearchResult"); FinishAutoCompleteLookup(nsIAutoCompleteStatus::failureItems, NS_ERROR_FAILURE, mState); return NS_ERROR_FAILURE; } // Does the string actually contain anything? if ( prevSearchStr.get() && prevSearchStr.get()[0]) { PR_LOG(sLDAPAutoCompleteLogModule, PR_LOG_DEBUG, ("nsLDAPAutoCompleteSession::OnStartLookup(): starting " "narrowing search\n")); // XXXdmose for performance, we should really do a local, // synchronous search against the existing dataset instead of // just kicking off a new LDAP search here. When implementing // this, need to be sure that only previous results which did not // hit the size limit and were successfully completed are used. // mState = SEARCHING; return DoTask(); } } // Init connection if necessary // switch (mState) { case UNBOUND: // Initialize the connection. // rv = InitConnection(); if (NS_FAILED(rv)) { // InitConnection() will have already called // FinishAutoCompleteLookup for us as necessary // return rv; } return NS_OK; case BOUND: PR_LOG(sLDAPAutoCompleteLogModule, PR_LOG_DEBUG, ("nsLDAPAutoComplete::OnStartLookup(): subsequent search " "starting")); // Kick off an LDAP search mState = SEARCHING; return DoTask(); case INITIALIZING: // We don't need to do anything here (for now at least), because // we can't really abandon the initialization. If we allowed the // initialization to be aborted, we could potentially lock the // UI thread again, since the DNS service might be stalled. // return NS_OK; case BINDING: case SEARCHING: // We should never get here NS_ERROR("nsLDAPAutoCompleteSession::OnStartLookup(): unexpected " "value of mStatus"); return NS_ERROR_UNEXPECTED; } return NS_ERROR_UNEXPECTED; /*NOTREACHED*/ }
//------------------------------------------------------------------------- // If aFormat is CF_DIBV5, aMIMEImageFormat must be a type for which we have // an image encoder (e.g. image/png). // For other values of aFormat, it is OK to pass null for aMIMEImageFormat. nsresult nsClipboard::GetNativeDataOffClipboard(IDataObject * aDataObject, UINT aIndex, UINT aFormat, const char * aMIMEImageFormat, void ** aData, uint32_t * aLen) { nsresult result = NS_ERROR_FAILURE; *aData = nullptr; *aLen = 0; if ( !aDataObject ) return result; UINT format = aFormat; HRESULT hres = S_FALSE; // XXX at the moment we only support global memory transfers // It is here where we will add support for native images // and IStream FORMATETC fe; STGMEDIUM stm; hres = FillSTGMedium(aDataObject, format, &fe, &stm, TYMED_HGLOBAL); // Currently this is only handling TYMED_HGLOBAL data // For Text, Dibs, Files, and generic data (like HTML) if (S_OK == hres) { static CLIPFORMAT fileDescriptorFlavorA = ::RegisterClipboardFormat( CFSTR_FILEDESCRIPTORA ); static CLIPFORMAT fileDescriptorFlavorW = ::RegisterClipboardFormat( CFSTR_FILEDESCRIPTORW ); static CLIPFORMAT fileFlavor = ::RegisterClipboardFormat( CFSTR_FILECONTENTS ); static CLIPFORMAT preferredDropEffect = ::RegisterClipboardFormat(CFSTR_PREFERREDDROPEFFECT); switch (stm.tymed) { case TYMED_HGLOBAL: { switch (fe.cfFormat) { case CF_TEXT: { // Get the data out of the global data handle. The size we return // should not include the null because the other platforms don't // use nulls, so just return the length we get back from strlen(), // since we know CF_TEXT is null terminated. Recall that GetGlobalData() // returns the size of the allocated buffer, not the size of the data // (on 98, these are not the same) so we can't use that. uint32_t allocLen = 0; if ( NS_SUCCEEDED(GetGlobalData(stm.hGlobal, aData, &allocLen)) ) { *aLen = strlen ( reinterpret_cast<char*>(*aData) ); result = NS_OK; } } break; case CF_UNICODETEXT: { // Get the data out of the global data handle. The size we return // should not include the null because the other platforms don't // use nulls, so just return the length we get back from strlen(), // since we know CF_UNICODETEXT is null terminated. Recall that GetGlobalData() // returns the size of the allocated buffer, not the size of the data // (on 98, these are not the same) so we can't use that. uint32_t allocLen = 0; if ( NS_SUCCEEDED(GetGlobalData(stm.hGlobal, aData, &allocLen)) ) { *aLen = NS_strlen(reinterpret_cast<PRUnichar*>(*aData)) * 2; result = NS_OK; } } break; case CF_DIBV5: if (aMIMEImageFormat) { uint32_t allocLen = 0; unsigned char * clipboardData; if (NS_SUCCEEDED(GetGlobalData(stm.hGlobal, (void **)&clipboardData, &allocLen))) { nsImageFromClipboard converter; nsIInputStream * inputStream; converter.GetEncodedImageStream(clipboardData, aMIMEImageFormat, &inputStream); // addrefs for us, don't release if ( inputStream ) { *aData = inputStream; *aLen = sizeof(nsIInputStream*); result = NS_OK; } } } break; case CF_HDROP : { // in the case of a file drop, multiple files are stashed within a // single data object. In order to match mozilla's D&D apis, we // just pull out the file at the requested index, pretending as // if there really are multiple drag items. HDROP dropFiles = (HDROP) GlobalLock(stm.hGlobal); UINT numFiles = ::DragQueryFileW(dropFiles, 0xFFFFFFFF, NULL, 0); NS_ASSERTION ( numFiles > 0, "File drop flavor, but no files...hmmmm" ); NS_ASSERTION ( aIndex < numFiles, "Asked for a file index out of range of list" ); if (numFiles > 0) { UINT fileNameLen = ::DragQueryFileW(dropFiles, aIndex, nullptr, 0); PRUnichar* buffer = reinterpret_cast<PRUnichar*>(nsMemory::Alloc((fileNameLen + 1) * sizeof(PRUnichar))); if ( buffer ) { ::DragQueryFileW(dropFiles, aIndex, buffer, fileNameLen + 1); *aData = buffer; *aLen = fileNameLen * sizeof(PRUnichar); result = NS_OK; } else result = NS_ERROR_OUT_OF_MEMORY; } GlobalUnlock (stm.hGlobal) ; } break; default: { if ( fe.cfFormat == fileDescriptorFlavorA || fe.cfFormat == fileDescriptorFlavorW || fe.cfFormat == fileFlavor ) { NS_WARNING ( "Mozilla doesn't yet understand how to read this type of file flavor" ); } else { // Get the data out of the global data handle. The size we return // should not include the null because the other platforms don't // use nulls, so just return the length we get back from strlen(), // since we know CF_UNICODETEXT is null terminated. Recall that GetGlobalData() // returns the size of the allocated buffer, not the size of the data // (on 98, these are not the same) so we can't use that. // // NOTE: we are assuming that anything that falls into this default case // is unicode. As we start to get more kinds of binary data, this // may become an incorrect assumption. Stay tuned. uint32_t allocLen = 0; if ( NS_SUCCEEDED(GetGlobalData(stm.hGlobal, aData, &allocLen)) ) { if ( fe.cfFormat == CF_HTML ) { // CF_HTML is actually UTF8, not unicode, so disregard the assumption // above. We have to check the header for the actual length, and we'll // do that in FindPlatformHTML(). For now, return the allocLen. This // case is mostly to ensure we don't try to call strlen on the buffer. *aLen = allocLen; } else if (fe.cfFormat == preferredDropEffect) { // As per the MSDN doc entitled: "Shell Clipboard Formats" // CFSTR_PREFERREDDROPEFFECT should return a DWORD // Reference: http://msdn.microsoft.com/en-us/library/bb776902(v=vs.85).aspx NS_ASSERTION(allocLen == sizeof(DWORD), "CFSTR_PREFERREDDROPEFFECT should return a DWORD"); *aLen = allocLen; } else { *aLen = NS_strlen(reinterpret_cast<PRUnichar*>(*aData)) * sizeof(PRUnichar); } result = NS_OK; } } } break; } // switch } break; case TYMED_GDI: { #ifdef DEBUG PR_LOG(gWin32ClipboardLog, PR_LOG_ALWAYS, ("*********************** TYMED_GDI\n")); #endif } break; default: break; } //switch ReleaseStgMedium(&stm); } return result; }
NS_IMETHODIMP nsSaveAsCharset::DoCharsetConversion(const PRUnichar *inString, char **outString) { NS_ENSURE_ARG_POINTER(outString); *outString = nullptr; nsresult rv; int32_t inStringLength = NS_strlen(inString); // original input string length int32_t bufferLength; // allocated buffer length int32_t srcLength = inStringLength; int32_t dstLength; int32_t pos1, pos2; nsresult saveResult = NS_OK; // to remember NS_ERROR_UENC_NOMAPPING // estimate and allocate the target buffer (reserve extra memory for fallback) rv = mEncoder->GetMaxLength(inString, inStringLength, &dstLength); if (NS_FAILED(rv)) return rv; bufferLength = dstLength + RESERVE_FALLBACK_BYTES; // extra bytes for fallback // + 1 is for the terminating NUL -- we don't add that to bufferLength so that // we can always write dstPtr[pos2] = '\0' even when the encoder filled the // buffer. char *dstPtr = (char *) PR_Malloc(bufferLength + 1); if (!dstPtr) { return NS_ERROR_OUT_OF_MEMORY; } for (pos1 = 0, pos2 = 0; pos1 < inStringLength;) { // convert from unicode dstLength = bufferLength - pos2; NS_ASSERTION(dstLength >= 0, "out of bounds write"); rv = mEncoder->Convert(&inString[pos1], &srcLength, &dstPtr[pos2], &dstLength); pos1 += srcLength ? srcLength : 1; pos2 += dstLength; dstPtr[pos2] = '\0'; // break: this is usually the case (no error) OR unrecoverable error if (NS_ERROR_UENC_NOMAPPING != rv) break; // remember this happened and reset the result saveResult = rv; rv = NS_OK; // finish encoder, give it a chance to write extra data like escape sequences dstLength = bufferLength - pos2; rv = mEncoder->Finish(&dstPtr[pos2], &dstLength); if (NS_SUCCEEDED(rv)) { pos2 += dstLength; dstPtr[pos2] = '\0'; } srcLength = inStringLength - pos1; // do the fallback if (!ATTR_NO_FALLBACK(mAttribute)) { uint32_t unMappedChar; if (NS_IS_HIGH_SURROGATE(inString[pos1-1]) && inStringLength > pos1 && NS_IS_LOW_SURROGATE(inString[pos1])) { unMappedChar = SURROGATE_TO_UCS4(inString[pos1-1], inString[pos1]); pos1++; } else { unMappedChar = inString[pos1-1]; } rv = mEncoder->GetMaxLength(inString+pos1, inStringLength-pos1, &dstLength); if (NS_FAILED(rv)) break; rv = HandleFallBack(unMappedChar, &dstPtr, &bufferLength, &pos2, dstLength); if (NS_FAILED(rv)) break; dstPtr[pos2] = '\0'; } } if (NS_SUCCEEDED(rv)) { // finish encoder, give it a chance to write extra data like escape sequences dstLength = bufferLength - pos2; rv = mEncoder->Finish(&dstPtr[pos2], &dstLength); if (NS_SUCCEEDED(rv)) { pos2 += dstLength; dstPtr[pos2] = '\0'; } } if (NS_FAILED(rv)) { PR_FREEIF(dstPtr); return rv; } *outString = dstPtr; // set the result string // set error code so that the caller can do own fall back if (NS_ERROR_UENC_NOMAPPING == saveResult) { rv = NS_ERROR_UENC_NOMAPPING; } return rv; }
NS_IMETHODIMP nsSaveAsCharset::DoCharsetConversion(const PRUnichar *inString, char **outString) { if(nullptr == outString ) return NS_ERROR_NULL_POINTER; NS_ASSERTION(outString, "invalid input"); *outString = NULL; nsresult rv; PRInt32 inStringLength = NS_strlen(inString); // original input string length PRInt32 bufferLength; // allocated buffer length PRInt32 srcLength = inStringLength; PRInt32 dstLength; char *dstPtr = NULL; PRInt32 pos1, pos2; nsresult saveResult = NS_OK; // to remember NS_ERROR_UENC_NOMAPPING // estimate and allocate the target buffer (reserve extra memory for fallback) rv = mEncoder->GetMaxLength(inString, inStringLength, &dstLength); if (NS_FAILED(rv)) return rv; bufferLength = dstLength + 512; // reserve 512 byte for fallback. dstPtr = (char *) PR_Malloc(bufferLength); if (NULL == dstPtr) return NS_ERROR_OUT_OF_MEMORY; for (pos1 = 0, pos2 = 0; pos1 < inStringLength;) { // convert from unicode dstLength = bufferLength - pos2; rv = mEncoder->Convert(&inString[pos1], &srcLength, &dstPtr[pos2], &dstLength); pos1 += srcLength ? srcLength : 1; pos2 += dstLength; dstPtr[pos2] = '\0'; // break: this is usually the case (no error) OR unrecoverable error if (NS_ERROR_UENC_NOMAPPING != rv) break; // remember this happened and reset the result saveResult = rv; rv = NS_OK; // finish encoder, give it a chance to write extra data like escape sequences dstLength = bufferLength - pos2; rv = mEncoder->Finish(&dstPtr[pos2], &dstLength); if (NS_SUCCEEDED(rv)) { pos2 += dstLength; dstPtr[pos2] = '\0'; } srcLength = inStringLength - pos1; // do the fallback if (!ATTR_NO_FALLBACK(mAttribute)) { PRUint32 unMappedChar; if (NS_IS_HIGH_SURROGATE(inString[pos1-1]) && inStringLength > pos1 && NS_IS_LOW_SURROGATE(inString[pos1])) { unMappedChar = SURROGATE_TO_UCS4(inString[pos1-1], inString[pos1]); pos1++; } else { unMappedChar = inString[pos1-1]; } rv = mEncoder->GetMaxLength(inString+pos1, inStringLength-pos1, &dstLength); if (NS_FAILED(rv)) break; rv = HandleFallBack(unMappedChar, &dstPtr, &bufferLength, &pos2, dstLength); if (NS_FAILED(rv)) break; dstPtr[pos2] = '\0'; } } if (NS_SUCCEEDED(rv)) { // finish encoder, give it a chance to write extra data like escape sequences dstLength = bufferLength - pos2; rv = mEncoder->Finish(&dstPtr[pos2], &dstLength); if (NS_SUCCEEDED(rv)) { pos2 += dstLength; dstPtr[pos2] = '\0'; } } if (NS_FAILED(rv)) { PR_FREEIF(dstPtr); return rv; } *outString = dstPtr; // set the result string // set error code so that the caller can do own fall back if (NS_ERROR_UENC_NOMAPPING == saveResult) { rv = NS_ERROR_UENC_NOMAPPING; } return rv; }
char16_t* NS_strdup(const char16_t* aString) { uint32_t len = NS_strlen(aString); return NS_strndup(aString, len); }
int32_t mozTXTToHTMLConv::CiteLevelTXT(const char16_t *line, uint32_t& logLineStart) { int32_t result = 0; int32_t lineLength = NS_strlen(line); bool moreCites = true; while (moreCites) { /* E.g. the following lines count as quote: > text //#ifdef QUOTE_RECOGNITION_AGGRESSIVE >text //#ifdef QUOTE_RECOGNITION_AGGRESSIVE > text ] text USER> text USER] text //#endif logLineStart is the position of "t" in this example */ uint32_t i = logLineStart; #ifdef QUOTE_RECOGNITION_AGGRESSIVE for (; int32_t(i) < lineLength && IsSpace(line[i]); i++) ; for (; int32_t(i) < lineLength && nsCRT::IsAsciiAlpha(line[i]) && nsCRT::IsUpper(line[i]) ; i++) ; if (int32_t(i) < lineLength && (line[i] == '>' || line[i] == ']')) #else if (int32_t(i) < lineLength && line[i] == '>') #endif { i++; if (int32_t(i) < lineLength && line[i] == ' ') i++; // sendmail/mbox // Placed here for performance increase const char16_t * indexString = &line[logLineStart]; // here, |logLineStart < lineLength| is always true uint32_t minlength = MinInt(6, NS_strlen(indexString)); if (Substring(indexString, indexString+minlength).Equals(Substring(NS_LITERAL_STRING(">From "), 0, minlength), nsCaseInsensitiveStringComparator())) //XXX RFC2646 moreCites = false; else { result++; logLineStart = i; } } else moreCites = false; } return result; }