static bool GetStringValue(HKEY aBaseKey, const nsAString& aStrSubKey, const nsAString& aValueName, nsAString& aOutput) { const nsString& flatSubKey = PromiseFlatString(aStrSubKey); const nsString& flatValueName = PromiseFlatString(aValueName); LPCWSTR valueName = aValueName.IsEmpty() ? nullptr : flatValueName.get(); DWORD type = 0; DWORD numBytes = 0; LONG result = RegGetValue(aBaseKey, flatSubKey.get(), valueName, RRF_RT_ANY, &type, nullptr, &numBytes); if (result != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) { return false; } int numChars = (numBytes + 1) / sizeof(wchar_t); aOutput.SetLength(numChars); DWORD acceptFlag = type == REG_SZ ? RRF_RT_REG_SZ : RRF_RT_REG_EXPAND_SZ; result = RegGetValue(aBaseKey, flatSubKey.get(), valueName, acceptFlag, nullptr, aOutput.BeginWriting(), &numBytes); if (result == ERROR_SUCCESS) { // Truncate null terminator aOutput.SetLength(((numBytes + 1) / sizeof(wchar_t)) - 1); } return result == ERROR_SUCCESS; }
nsresult nsDOMFileReader::ConvertStream(const char *aFileData, PRUint32 aDataLen, const char *aCharset, nsAString &aResult) { nsresult rv; nsCOMPtr<nsICharsetConverterManager> charsetConverter = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder; rv = charsetConverter->GetUnicodeDecoder(aCharset, getter_AddRefs(unicodeDecoder)); NS_ENSURE_SUCCESS(rv, rv); PRInt32 destLength; rv = unicodeDecoder->GetMaxLength(aFileData, aDataLen, &destLength); NS_ENSURE_SUCCESS(rv, rv); aResult.SetLength(destLength); //Make sure we have enough space for the conversion destLength = aResult.Length(); PRInt32 srcLength = aDataLen; rv = unicodeDecoder->Convert(aFileData, &srcLength, aResult.BeginWriting(), &destLength); aResult.SetLength(destLength); //Trim down to the correct size return rv; }
nsresult NS_CopyNativeToUnicode(const nsACString& aInput, nsAString& aOutput) { uint32_t inputLen = aInput.Length(); nsACString::const_iterator iter; aInput.BeginReading(iter); const char* buf = iter.get(); // determine length of result uint32_t resultLen = 0; int n = ::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, nullptr, 0); if (n > 0) { resultLen += n; } // allocate sufficient space if (!aOutput.SetLength(resultLen, fallible)) { return NS_ERROR_OUT_OF_MEMORY; } if (resultLen > 0) { nsAString::iterator out_iter; aOutput.BeginWriting(out_iter); char16_t* result = out_iter.get(); ::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, wwc(result), resultLen); } return NS_OK; }
static void RemoveControlCharactersFrom(nsAString& aStr, TextRangeArray* aRanges) { size_t firstControlCharOffset = FindFirstControlCharacter(aStr); if (firstControlCharOffset == (size_t)-1) { return; } nsAutoString copy(aStr); const char16_t* sourceBegin = copy.BeginReading(); const char16_t* sourceEnd = copy.EndReading(); char16_t* dest = aStr.BeginWriting(); if (NS_WARN_IF(!dest)) { return; } char16_t* curDest = dest + firstControlCharOffset; size_t i = firstControlCharOffset; for (const char16_t* source = sourceBegin + firstControlCharOffset; source < sourceEnd; ++source) { if (*source == '\t' || *source == '\n' || !IsControlChar(*source)) { *curDest = *source; ++curDest; ++i; } else if (aRanges) { aRanges->RemoveCharacter(i); } } aStr.SetLength(curDest - dest); }
nsresult nsDOMFileReader::ConvertStream(const char *aFileData, uint32_t aDataLen, const char *aCharset, nsAString &aResult) { nsresult rv; nsCOMPtr<nsICharsetConverterManager> charsetConverter = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder; rv = charsetConverter->GetUnicodeDecoder(aCharset, getter_AddRefs(unicodeDecoder)); NS_ENSURE_SUCCESS(rv, rv); int32_t destLength; rv = unicodeDecoder->GetMaxLength(aFileData, aDataLen, &destLength); NS_ENSURE_SUCCESS(rv, rv); if (!aResult.SetLength(destLength, fallible_t())) return NS_ERROR_OUT_OF_MEMORY; int32_t srcLength = aDataLen; rv = unicodeDecoder->Convert(aFileData, &srcLength, aResult.BeginWriting(), &destLength); aResult.SetLength(destLength); //Trim down to the correct size return rv; }
nsresult NS_CopyNativeToUnicode(const nsACString &input, nsAString &output) { PRUint32 inputLen = input.Length(); nsACString::const_iterator iter; input.BeginReading(iter); const char *buf = iter.get(); // determine length of result PRUint32 resultLen = 0; int n = ::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, NULL, 0); if (n > 0) resultLen += n; // allocate sufficient space if (!EnsureStringLength(output, resultLen)) return NS_ERROR_OUT_OF_MEMORY; if (resultLen > 0) { nsAString::iterator out_iter; output.BeginWriting(out_iter); PRUnichar *result = out_iter.get(); ::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, result, resultLen); } return NS_OK; }
static bool ReadCOMRegDefaultString(const nsString& aRegPath, nsAString& aOutBuf) { aOutBuf.Truncate(); nsAutoString fullyQualifiedRegPath; fullyQualifiedRegPath.AppendLiteral(u"SOFTWARE\\Classes\\"); fullyQualifiedRegPath.Append(aRegPath); // Get the required size and type of the registry value. // We expect either REG_SZ or REG_EXPAND_SZ. DWORD type; DWORD bufLen = 0; LONG result = ::RegGetValue(HKEY_LOCAL_MACHINE, fullyQualifiedRegPath.get(), nullptr, RRF_RT_ANY, &type, nullptr, &bufLen); if (result != ERROR_SUCCESS || (type != REG_SZ && type != REG_EXPAND_SZ)) { return false; } // Now obtain the value DWORD flags = type == REG_SZ ? RRF_RT_REG_SZ : RRF_RT_REG_EXPAND_SZ; aOutBuf.SetLength((bufLen + 1) / sizeof(char16_t)); result = ::RegGetValue(HKEY_LOCAL_MACHINE, fullyQualifiedRegPath.get(), nullptr, flags, nullptr, aOutBuf.BeginWriting(), &bufLen); if (result != ERROR_SUCCESS) { aOutBuf.Truncate(); return false; } // Truncate terminator aOutBuf.Truncate((bufLen + 1) / sizeof(char16_t) - 1); return true; }
void TX_ToLowerCase(nsAString& aString) { nsAString::iterator fromBegin, fromEnd; ConvertToLowerCase converter; copy_string(aString.BeginWriting(fromBegin), aString.EndWriting(fromEnd), converter); }
void TX_ToLowerCase(const nsAString& aSource, nsAString& aDest) { nsAString::const_iterator fromBegin, fromEnd; nsAString::iterator toBegin; if (!EnsureStringLength(aDest, aSource.Length())) return; // XXX no way to signal out-of-memory CopyToLowerCase converter(aDest.BeginWriting(toBegin)); copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter); }
void ToUpperCase(const nsAString& aSource, nsAString& aDest) { const char16_t *in = aSource.BeginReading(); uint32_t len = aSource.Length(); aDest.SetLength(len); char16_t *out = aDest.BeginWriting(); ToUpperCase(in, out, len); }
void CopyUnicodeTo(const nsAString::const_iterator& aSrcStart, const nsAString::const_iterator& aSrcEnd, nsAString& aDest) { aDest.SetLength(Distance(aSrcStart, aSrcEnd)); nsAString::char_iterator dest = aDest.BeginWriting(); nsAString::const_iterator fromBegin(aSrcStart); copy_string(fromBegin, aSrcEnd, dest); }
void CopyUnicodeTo( const nsAString::const_iterator& aSrcStart, const nsAString::const_iterator& aSrcEnd, nsAString& aDest ) { nsAString::iterator writer; aDest.SetLength(Distance(aSrcStart, aSrcEnd)); aDest.BeginWriting(writer); nsAString::const_iterator fromBegin(aSrcStart); copy_string(fromBegin, aSrcEnd, writer); }
void AppendUnicodeTo(const nsAString::const_iterator& aSrcStart, const nsAString::const_iterator& aSrcEnd, nsAString& aDest) { uint32_t oldLength = aDest.Length(); aDest.SetLength(oldLength + Distance(aSrcStart, aSrcEnd)); nsAString::char_iterator dest = aDest.BeginWriting() + oldLength; nsAString::const_iterator fromBegin(aSrcStart); copy_string(fromBegin, aSrcEnd, dest); }
/** * Provides a fallback for getting the path to APPDATA or LOCALAPPDATA by * querying the registry when the call to SHGetSpecialFolderLocation or * SHGetPathFromIDListW is unable to provide these paths (Bug 513958). */ static nsresult GetRegWindowsAppDataFolder(bool aLocal, nsAString& _retval) { HKEY key; NS_NAMED_LITERAL_STRING(keyName, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"); DWORD res = ::RegOpenKeyExW(HKEY_CURRENT_USER, keyName.get(), 0, KEY_READ, &key); if (res != ERROR_SUCCESS) { _retval.SetLength(0); return NS_ERROR_NOT_AVAILABLE; } DWORD type, size; res = RegQueryValueExW(key, (aLocal ? L"Local AppData" : L"AppData"), nullptr, &type, nullptr, &size); // The call to RegQueryValueExW must succeed, the type must be REG_SZ, the // buffer size must not equal 0, and the buffer size be a multiple of 2. if (res != ERROR_SUCCESS || type != REG_SZ || size == 0 || size % 2 != 0) { ::RegCloseKey(key); _retval.SetLength(0); return NS_ERROR_NOT_AVAILABLE; } // |size| may or may not include room for the terminating null character DWORD resultLen = size / 2; _retval.SetLength(resultLen); nsAString::iterator begin; _retval.BeginWriting(begin); if (begin.size_forward() != resultLen) { ::RegCloseKey(key); _retval.SetLength(0); return NS_ERROR_NOT_AVAILABLE; } res = RegQueryValueExW(key, (aLocal ? L"Local AppData" : L"AppData"), nullptr, nullptr, (LPBYTE) begin.get(), &size); ::RegCloseKey(key); if (res != ERROR_SUCCESS) { _retval.SetLength(0); return NS_ERROR_NOT_AVAILABLE; } if (!_retval.CharAt(resultLen - 1)) { // It was already null terminated. _retval.Truncate(resultLen - 1); } return NS_OK; }
void AppendUnicodeTo( const nsAString::const_iterator& aSrcStart, const nsAString::const_iterator& aSrcEnd, nsAString& aDest ) { nsAString::iterator writer; uint32_t oldLength = aDest.Length(); aDest.SetLength(oldLength + Distance(aSrcStart, aSrcEnd)); aDest.BeginWriting(writer).advance(oldLength); nsAString::const_iterator fromBegin(aSrcStart); copy_string(fromBegin, aSrcEnd, writer); }
void GUIDToString(REFGUID aGuid, nsAString& aOutString) { // This buffer length is long enough to hold a GUID string that is formatted // to include curly braces and dashes. const int kBufLenWithNul = 39; aOutString.SetLength(kBufLenWithNul); int result = StringFromGUID2(aGuid, wwc(aOutString.BeginWriting()), kBufLenWithNul); MOZ_ASSERT(result); if (result) { // Truncate the terminator aOutString.SetLength(result - 1); } }
NS_StringGetMutableData(nsAString& aStr, uint32_t aDataLength, char16_t** aData) { if (aDataLength != UINT32_MAX) { aStr.SetLength(aDataLength); if (aStr.Length() != aDataLength) { *aData = nullptr; return 0; } } *aData = aStr.BeginWriting(); return aStr.Length(); }
void AppendUnicodeTo( const nsAString::const_iterator& aSrcStart, const nsAString::const_iterator& aSrcEnd, nsAString& aDest ) { nsAString::iterator writer; PRUint32 oldLength = aDest.Length(); if(!SetLengthForWriting(aDest, oldLength + Distance(aSrcStart, aSrcEnd))) return; aDest.BeginWriting(writer).advance(oldLength); nsAString::const_iterator fromBegin(aSrcStart); copy_string(fromBegin, aSrcEnd, writer); }
NS_StringGetMutableData(nsAString &aStr, PRUint32 aDataLength, PRUnichar **aData) { if (aDataLength != PR_UINT32_MAX) { aStr.SetLength(aDataLength); if (aStr.Length() != aDataLength) { *aData = nsnull; return 0; } } nsAString::iterator begin; aStr.BeginWriting(begin); *aData = begin.get(); return begin.size_forward(); }
void AppendASCIItoUTF16( const nsACString& aSource, nsAString& aDest ) { uint32_t old_dest_length = aDest.Length(); aDest.SetLength(old_dest_length + aSource.Length()); nsACString::const_iterator fromBegin, fromEnd; nsAString::iterator dest; aDest.BeginWriting(dest); dest.advance(old_dest_length); // right now, this won't work on multi-fragment destinations LossyConvertEncoding8to16 converter(dest.get()); copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter); }
NS_IMETHODIMP nsBinaryInputStream::ReadString(nsAString& aString) { nsresult rv; uint32_t length, bytesRead; rv = Read32(&length); if (NS_FAILED(rv)) { return rv; } if (length == 0) { aString.Truncate(); return NS_OK; } // pre-allocate output buffer, and get direct access to buffer... if (!aString.SetLength(length, mozilla::fallible)) { return NS_ERROR_OUT_OF_MEMORY; } nsAString::iterator start; aString.BeginWriting(start); WriteStringClosure closure; closure.mWriteCursor = start.get(); closure.mHasCarryoverByte = false; rv = ReadSegments(WriteSegmentToString, &closure, length * sizeof(char16_t), &bytesRead); if (NS_FAILED(rv)) { return rv; } NS_ASSERTION(!closure.mHasCarryoverByte, "some strange stream corruption!"); if (bytesRead != length * sizeof(char16_t)) { return NS_ERROR_FAILURE; } return NS_OK; }
NS_IMETHODIMP nsWindowsRegKey::ReadStringValue(const nsAString &name, nsAString &result) { NS_ENSURE_TRUE(mKey, NS_ERROR_NOT_INITIALIZED); DWORD type, size; const nsString &flatName = PromiseFlatString(name); LONG rv = RegQueryValueExW(mKey, flatName.get(), 0, &type, NULL, &size); if (rv != ERROR_SUCCESS) return NS_ERROR_FAILURE; // This must be a string type in order to fetch the value as a string. // We're being a bit forgiving here by allowing types other than REG_SZ. NS_ENSURE_STATE(type == REG_SZ || type == REG_EXPAND_SZ || type == REG_MULTI_SZ); // The buffer size must be a multiple of 2. NS_ENSURE_STATE(size % 2 == 0); if (size == 0) { result.Truncate(); return NS_OK; } // |size| includes room for the terminating null character DWORD resultLen = size / 2 - 1; result.SetLength(resultLen); nsAString::iterator begin; result.BeginWriting(begin); if (begin.size_forward() != resultLen) return NS_ERROR_OUT_OF_MEMORY; rv = RegQueryValueExW(mKey, flatName.get(), 0, NULL, (LPBYTE) begin.get(), &size); return (rv == ERROR_SUCCESS) ? NS_OK : NS_ERROR_FAILURE; }
bool DriveToNtPath(const wchar_t aDriveLetter, nsAString& aNtPath) { const wchar_t drvTpl[] = {aDriveLetter, L':', L'\0'}; aNtPath.SetLength(MAX_PATH); DWORD pathLen; while (true) { pathLen = QueryDosDeviceW(drvTpl, aNtPath.BeginWriting(), aNtPath.Length()); if (pathLen || GetLastError() != ERROR_INSUFFICIENT_BUFFER) { break; } aNtPath.SetLength(aNtPath.Length() * 2); } if (!pathLen) { return false; } // aNtPath contains embedded NULLs, so we need to figure out the real length // via wcslen. aNtPath.SetLength(wcslen(aNtPath.BeginReading())); return true; }
bool AppendUTF8toUTF16( const nsACString& aSource, nsAString& aDest, const mozilla::fallible_t& ) { nsACString::const_iterator source_start, source_end; CalculateUTF8Length calculator; copy_string(aSource.BeginReading(source_start), aSource.EndReading(source_end), calculator); uint32_t count = calculator.Length(); // Avoid making the string mutable if we're appending an empty string if (count) { uint32_t old_dest_length = aDest.Length(); // Grow the buffer if we need to. if (!aDest.SetLength(old_dest_length + count, mozilla::fallible_t())) { return false; } // All ready? Time to convert ConvertUTF8toUTF16 converter(aDest.BeginWriting() + old_dest_length); copy_string(aSource.BeginReading(source_start), aSource.EndReading(source_end), converter); NS_ASSERTION(converter.ErrorEncountered() || converter.Length() == count, "CalculateUTF8Length produced the wrong length"); if (converter.ErrorEncountered()) { NS_ERROR("Input wasn't UTF8 or incorrect length was calculated"); aDest.SetLength(old_dest_length); } } return true; }
nsresult NS_CopyNativeToUnicode(const nsACString &input, nsAString &output) { uint32_t inputLen = input.Length(); nsACString::const_iterator iter; input.BeginReading(iter); const char *inputStr = iter.get(); // determine length of result uint32_t resultLen = inputLen; if (!output.SetLength(resultLen, fallible_t())) return NS_ERROR_OUT_OF_MEMORY; nsAString::iterator out_iter; output.BeginWriting(out_iter); UniChar *result = (UniChar*)out_iter.get(); size_t cSubs = 0; size_t resultLeft = resultLen; if (!UnicodeConverter) NS_StartupNativeCharsetUtils(); int unirc = ::UniUconvToUcs(UnicodeConverter, (void**)&inputStr, &inputLen, &result, &resultLeft, &cSubs); NS_ASSERTION(unirc != UCONV_E2BIG, "Path too big"); if (unirc != ULS_SUCCESS) { output.Truncate(); return NS_ERROR_FAILURE; } // Need to update string length to reflect how many bytes were actually // written. output.Truncate(resultLen - resultLeft); return NS_OK; }
void URLSearchParams::ConvertString(const nsACString& aInput, nsAString& aOutput) { aOutput.Truncate(); if (!mDecoder) { mDecoder = EncodingUtils::DecoderForEncoding("UTF-8"); if (!mDecoder) { MOZ_ASSERT(mDecoder, "Failed to create a decoder."); return; } } int32_t inputLength = aInput.Length(); int32_t outputLength = 0; nsresult rv = mDecoder->GetMaxLength(aInput.BeginReading(), inputLength, &outputLength); if (NS_WARN_IF(NS_FAILED(rv))) { return; } if (!aOutput.SetLength(outputLength, fallible)) { return; } int32_t newOutputLength = outputLength; rv = mDecoder->Convert(aInput.BeginReading(), &inputLength, aOutput.BeginWriting(), &newOutputLength); if (NS_FAILED(rv)) { aOutput.Truncate(); return; } if (newOutputLength < outputLength) { aOutput.Truncate(newOutputLength); } }
nsresult NS_CopyNativeToUnicode(const nsACString& aInput, nsAString& aOutput) { aOutput.Truncate(); uint32_t inputLen = aInput.Length(); nsACString::const_iterator iter; aInput.BeginReading(iter); // // OPTIMIZATION: preallocate space for largest possible result; convert // directly into the result buffer to avoid intermediate buffer copy. // // this will generally result in a larger allocation, but that seems // better than an extra buffer copy. // if (!aOutput.SetLength(inputLen, fallible)) { return NS_ERROR_OUT_OF_MEMORY; } nsAString::iterator out_iter; aOutput.BeginWriting(out_iter); char16_t* result = out_iter.get(); uint32_t resultLeft = inputLen; const char* buf = iter.get(); uint32_t bufLeft = inputLen; nsNativeCharsetConverter conv; nsresult rv = conv.NativeToUnicode(&buf, &bufLeft, &result, &resultLeft); if (NS_SUCCEEDED(rv)) { NS_ASSERTION(bufLeft == 0, "did not consume entire input buffer"); aOutput.SetLength(inputLen - resultLeft); } return rv; }
bool AppendASCIItoUTF16(const nsACString& aSource, nsAString& aDest, const mozilla::fallible_t&) { uint32_t old_dest_length = aDest.Length(); if (!aDest.SetLength(old_dest_length + aSource.Length(), mozilla::fallible_t())) { return false; } nsACString::const_iterator fromBegin, fromEnd; nsAString::iterator dest; aDest.BeginWriting(dest); dest.advance(old_dest_length); // right now, this won't work on multi-fragment destinations LossyConvertEncoding8to16 converter(dest.get()); copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter); return true; }
NS_IMETHODIMP nsBinaryInputStream::ReadString(nsAString& aString) { nsresult rv; PRUint32 length, bytesRead; rv = Read32(&length); if (NS_FAILED(rv)) return rv; if (length == 0) { aString.Truncate(); return NS_OK; } // pre-allocate output buffer, and get direct access to buffer... if (!EnsureStringLength(aString, length)) return NS_ERROR_OUT_OF_MEMORY; nsAString::iterator start; aString.BeginWriting(start); WriteStringClosure closure; closure.mWriteCursor = start.get(); closure.mHasCarryoverByte = PR_FALSE; rv = ReadSegments(WriteSegmentToString, &closure, length*sizeof(PRUnichar), &bytesRead); if (NS_FAILED(rv)) return rv; NS_ASSERTION(!closure.mHasCarryoverByte, "some strange stream corruption!"); if (bytesRead != length*sizeof(PRUnichar)) return NS_ERROR_FAILURE; return NS_OK; }
static bool GetCurrentWorkingDirectory(nsAString& workingDirectory) { #if !defined(XP_WIN) && !defined(XP_UNIX) //XXX: your platform should really implement this return false; #elif XP_WIN DWORD requiredLength = GetCurrentDirectoryW(0, nullptr); workingDirectory.SetLength(requiredLength); GetCurrentDirectoryW(workingDirectory.Length(), (LPWSTR)workingDirectory.BeginWriting()); // we got a trailing null there workingDirectory.SetLength(requiredLength); workingDirectory.Replace(workingDirectory.Length() - 1, 1, L'\\'); #elif defined(XP_UNIX) nsAutoCString cwd; // 1024 is just a guess at a sane starting value size_t bufsize = 1024; char* result = nullptr; while (result == nullptr) { cwd.SetLength(bufsize); result = getcwd(cwd.BeginWriting(), cwd.Length()); if (!result) { if (errno != ERANGE) return false; // need to make the buffer bigger bufsize *= 2; } } // size back down to the actual string length cwd.SetLength(strlen(result) + 1); cwd.Replace(cwd.Length() - 1, 1, '/'); workingDirectory = NS_ConvertUTF8toUTF16(cwd); #endif return true; }