2014-06-30 15:39:45 +00: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 11:12:37 +00: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 20:29:42 +00:00
|
|
|
|
|
|
|
#include "nsScriptableInputStream.h"
|
2000-06-03 09:46:12 +00:00
|
|
|
#include "nsMemory.h"
|
2010-08-27 19:42:51 +00:00
|
|
|
#include "nsString.h"
|
1999-12-04 20:29:42 +00:00
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsScriptableInputStream, nsIScriptableInputStream)
|
1999-12-04 20:29:42 +00:00
|
|
|
|
2011-08-09 16:28:00 +00:00
|
|
|
// nsIScriptableInputStream methods
|
1999-12-04 20:29:42 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-14 20:15:46 +00:00
|
|
|
nsScriptableInputStream::Close()
|
|
|
|
{
|
|
|
|
if (!mInputStream) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
return mInputStream->Close();
|
1999-12-04 20:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 20:15:46 +00:00
|
|
|
nsScriptableInputStream::Init(nsIInputStream* aInputStream)
|
|
|
|
{
|
|
|
|
if (!aInputStream) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
mInputStream = aInputStream;
|
|
|
|
return NS_OK;
|
2007-07-25 16:53:37 +00:00
|
|
|
}
|
|
|
|
|
2008-02-01 08:51:29 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-14 20:15:46 +00:00
|
|
|
nsScriptableInputStream::Available(uint64_t* aResult)
|
|
|
|
{
|
|
|
|
if (!mInputStream) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
return mInputStream->Available(aResult);
|
2008-02-01 08:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 20:15:46 +00:00
|
|
|
nsScriptableInputStream::Read(uint32_t aCount, char** aResult)
|
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
uint64_t count64 = 0;
|
2014-05-14 20:15:46 +00:00
|
|
|
char* buffer = nullptr;
|
2007-07-25 16:53:37 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
if (!mInputStream) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2008-02-01 08:51:29 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
rv = mInputStream->Available(&count64);
|
2014-05-14 20:15:46 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-07-25 16:53:37 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
// bug716556 - Ensure count+1 doesn't overflow
|
2014-06-26 05:19:21 +00:00
|
|
|
uint32_t count = XPCOM_MIN((uint32_t)XPCOM_MIN<uint64_t>(count64, aCount), UINT32_MAX - 1);
|
2014-05-14 20:15:46 +00:00
|
|
|
buffer = (char*)moz_malloc(count + 1); // make room for '\0'
|
|
|
|
if (!buffer) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2008-02-01 08:51:29 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
rv = ReadHelper(buffer, count);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nsMemory::Free(buffer);
|
|
|
|
return rv;
|
|
|
|
}
|
2008-02-01 08:51:29 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
buffer[count] = '\0';
|
2014-05-14 20:15:46 +00:00
|
|
|
*aResult = buffer;
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_OK;
|
2008-02-01 08:51:29 +00:00
|
|
|
}
|
|
|
|
|
2010-08-27 19:42:51 +00:00
|
|
|
NS_IMETHODIMP
|
2014-05-14 20:15:46 +00:00
|
|
|
nsScriptableInputStream::ReadBytes(uint32_t aCount, nsACString& aResult)
|
|
|
|
{
|
2014-05-05 17:30:43 +00:00
|
|
|
if (!mInputStream) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
aResult.SetLength(aCount);
|
|
|
|
if (aResult.Length() != aCount) {
|
2014-05-05 17:30:43 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
char* ptr = aResult.BeginWriting();
|
2014-05-05 17:30:43 +00:00
|
|
|
nsresult rv = ReadHelper(ptr, aCount);
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-14 20:15:46 +00:00
|
|
|
aResult.Truncate();
|
2014-05-05 17:30:43 +00:00
|
|
|
}
|
|
|
|
return rv;
|
2014-03-27 15:20:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsScriptableInputStream::ReadHelper(char* aBuffer, uint32_t aCount)
|
|
|
|
{
|
2014-05-05 17:30:43 +00: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 19:42:51 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
totalBytesRead += bytesRead;
|
|
|
|
if (totalBytesRead == aCount) {
|
|
|
|
break;
|
2010-08-27 19:42:51 +00:00
|
|
|
}
|
2014-05-05 17:30:43 +00:00
|
|
|
|
|
|
|
// If we have read zero bytes, we have hit EOF.
|
|
|
|
if (bytesRead == 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2010-08-27 19:42:51 +00:00
|
|
|
}
|
|
|
|
|
2010-06-10 18:11:11 +00:00
|
|
|
nsresult
|
2014-05-14 20:15:46 +00:00
|
|
|
nsScriptableInputStream::Create(nsISupports* aOuter, REFNSIID aIID,
|
|
|
|
void** aResult)
|
|
|
|
{
|
|
|
|
if (aOuter) {
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
}
|
2008-02-01 08:51:29 +00:00
|
|
|
|
2014-05-14 20:15:46 +00:00
|
|
|
nsScriptableInputStream* sis = new nsScriptableInputStream();
|
|
|
|
if (!sis) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2008-02-01 08:51:29 +00:00
|
|
|
|
2014-05-05 17:30:43 +00:00
|
|
|
NS_ADDREF(sis);
|
|
|
|
nsresult rv = sis->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(sis);
|
|
|
|
return rv;
|
1999-12-04 20:29:42 +00:00
|
|
|
}
|