2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-12-04 23:29:42 +03:00
|
|
|
|
|
|
|
#include "nsScriptableInputStream.h"
|
2000-06-03 13:46:12 +04:00
|
|
|
#include "nsMemory.h"
|
2010-08-27 23:42:51 +04:00
|
|
|
#include "nsString.h"
|
1999-12-04 23:29:42 +03:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsScriptableInputStream, nsIScriptableInputStream)
|
1999-12-04 23:29:42 +03:00
|
|
|
|
2011-08-09 20:28:00 +04:00
|
|
|
// nsIScriptableInputStream methods
|
1999-12-04 23:29:42 +03:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsScriptableInputStream::Close() {
|
|
|
|
if (!mInputStream) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
return mInputStream->Close();
|
1999-12-04 23:29:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsScriptableInputStream::Init(nsIInputStream* aInputStream) {
|
|
|
|
if (!aInputStream) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
mInputStream = aInputStream;
|
|
|
|
return NS_OK;
|
2007-07-25 20:53:37 +04:00
|
|
|
}
|
|
|
|
|
2008-02-01 11:51:29 +03:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsScriptableInputStream::Available(uint64_t* aResult) {
|
|
|
|
if (!mInputStream) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
return mInputStream->Available(aResult);
|
2008-02-01 11:51:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsScriptableInputStream::Read(uint32_t aCount, char** aResult) {
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
uint64_t count64 = 0;
|
2014-05-15 00:15:46 +04:00
|
|
|
char* buffer = nullptr;
|
2007-07-25 20:53:37 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!mInputStream) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2008-02-01 11:51:29 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = mInputStream->Available(&count64);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-07-25 20:53:37 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// bug716556 - Ensure count+1 doesn't overflow
|
2014-08-25 23:17:15 +04:00
|
|
|
uint32_t count =
|
|
|
|
XPCOM_MIN((uint32_t)XPCOM_MIN<uint64_t>(count64, aCount), UINT32_MAX - 1);
|
2015-02-19 07:51:06 +03:00
|
|
|
buffer = (char*)malloc(count + 1); // make room for '\0'
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!buffer) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2008-02-01 11:51:29 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = ReadHelper(buffer, count);
|
|
|
|
if (NS_FAILED(rv)) {
|
2015-03-27 03:01:12 +03:00
|
|
|
free(buffer);
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2008-02-01 11:51:29 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
buffer[count] = '\0';
|
2014-05-15 00:15:46 +04:00
|
|
|
*aResult = buffer;
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2008-02-01 11:51:29 +03:00
|
|
|
}
|
|
|
|
|
2010-08-27 23:42:51 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsScriptableInputStream::ReadBytes(uint32_t aCount, nsACString& aResult) {
|
2014-05-05 21:30:43 +04:00
|
|
|
if (!mInputStream) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2016-03-21 23:49:18 +03:00
|
|
|
if (!aResult.SetLength(aCount, fallible)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2016-03-21 23:49:18 +03:00
|
|
|
MOZ_ASSERT(aResult.Length() == aCount);
|
2014-05-15 00:15:46 +04:00
|
|
|
char* ptr = aResult.BeginWriting();
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult rv = ReadHelper(ptr, aCount);
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-15 00:15:46 +04:00
|
|
|
aResult.Truncate();
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2014-03-27 19:20:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsScriptableInputStream::ReadHelper(char* aBuffer, uint32_t aCount) {
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t totalBytesRead = 0;
|
|
|
|
while (1) {
|
|
|
|
uint32_t bytesRead;
|
|
|
|
nsresult rv = mInputStream->Read(aBuffer + totalBytesRead,
|
|
|
|
aCount - totalBytesRead, &bytesRead);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2010-08-27 23:42:51 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
totalBytesRead += bytesRead;
|
|
|
|
if (totalBytesRead == aCount) {
|
|
|
|
break;
|
2010-08-27 23:42:51 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// If we have read zero bytes, we have hit EOF.
|
|
|
|
if (bytesRead == 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2010-08-27 23:42:51 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult nsScriptableInputStream::Create(nsISupports* aOuter, REFNSIID aIID,
|
|
|
|
void** aResult) {
|
|
|
|
if (aOuter) {
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
}
|
2008-02-01 11:51:29 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsScriptableInputStream> sis = new nsScriptableInputStream();
|
2015-07-01 19:27:43 +03:00
|
|
|
return sis->QueryInterface(aIID, aResult);
|
1999-12-04 23:29:42 +03:00
|
|
|
}
|