Bug 1861963 [wpt PR 42839] - Fix accessing update_properties for product, a=testonly
[gecko.git] / xpcom / io / nsScriptableInputStream.cpp
blob5f8b139c6fe9b6dde1f7d34609441fb195e23ab2
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsScriptableInputStream.h"
8 #include "nsString.h"
10 NS_IMPL_ISUPPORTS(nsScriptableInputStream, nsIScriptableInputStream)
12 // nsIScriptableInputStream methods
13 NS_IMETHODIMP
14 nsScriptableInputStream::Close() {
15 if (!mInputStream) {
16 return NS_ERROR_NOT_INITIALIZED;
18 return mInputStream->Close();
21 NS_IMETHODIMP
22 nsScriptableInputStream::Init(nsIInputStream* aInputStream) {
23 if (!aInputStream) {
24 return NS_ERROR_NULL_POINTER;
26 mInputStream = aInputStream;
27 return NS_OK;
30 NS_IMETHODIMP
31 nsScriptableInputStream::Available(uint64_t* aResult) {
32 if (!mInputStream) {
33 return NS_ERROR_NOT_INITIALIZED;
35 return mInputStream->Available(aResult);
38 NS_IMETHODIMP
39 nsScriptableInputStream::Read(uint32_t aCount, char** aResult) {
40 nsresult rv = NS_OK;
41 uint64_t count64 = 0;
42 char* buffer = nullptr;
44 if (!mInputStream) {
45 return NS_ERROR_NOT_INITIALIZED;
48 rv = mInputStream->Available(&count64);
49 if (NS_FAILED(rv)) {
50 return rv;
53 // bug716556 - Ensure count+1 doesn't overflow
54 uint32_t count =
55 XPCOM_MIN((uint32_t)XPCOM_MIN<uint64_t>(count64, aCount), UINT32_MAX - 1);
56 buffer = (char*)malloc(count + 1); // make room for '\0'
57 if (!buffer) {
58 return NS_ERROR_OUT_OF_MEMORY;
61 rv = ReadHelper(buffer, count);
62 if (NS_FAILED(rv)) {
63 free(buffer);
64 return rv;
67 buffer[count] = '\0';
68 *aResult = buffer;
69 return NS_OK;
72 NS_IMETHODIMP
73 nsScriptableInputStream::ReadBytes(uint32_t aCount, nsACString& aResult) {
74 if (!mInputStream) {
75 return NS_ERROR_NOT_INITIALIZED;
78 if (!aResult.SetLength(aCount, mozilla::fallible)) {
79 return NS_ERROR_OUT_OF_MEMORY;
82 MOZ_ASSERT(aResult.Length() == aCount);
83 char* ptr = aResult.BeginWriting();
84 nsresult rv = ReadHelper(ptr, aCount);
85 if (NS_FAILED(rv)) {
86 aResult.Truncate();
88 return rv;
91 nsresult nsScriptableInputStream::ReadHelper(char* aBuffer, uint32_t aCount) {
92 uint32_t totalBytesRead = 0;
93 while (1) {
94 uint32_t bytesRead;
95 nsresult rv = mInputStream->Read(aBuffer + totalBytesRead,
96 aCount - totalBytesRead, &bytesRead);
97 if (NS_FAILED(rv)) {
98 return rv;
101 totalBytesRead += bytesRead;
102 if (totalBytesRead == aCount) {
103 break;
106 // If we have read zero bytes, we have hit EOF.
107 if (bytesRead == 0) {
108 return NS_ERROR_FAILURE;
111 return NS_OK;
114 nsresult nsScriptableInputStream::Create(REFNSIID aIID, void** aResult) {
115 RefPtr<nsScriptableInputStream> sis = new nsScriptableInputStream();
116 return sis->QueryInterface(aIID, aResult);