michael@0: //* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsILocalFile.h" michael@0: #include "nsCRT.h" michael@0: #include "nsIFile.h" michael@0: #include "nsISupportsImpl.h" michael@0: #include "nsCheckSummedOutputStream.h" michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // nsCheckSummedOutputStream michael@0: michael@0: NS_IMPL_ISUPPORTS_INHERITED(nsCheckSummedOutputStream, michael@0: nsSafeFileOutputStream, michael@0: nsISafeOutputStream, michael@0: nsIOutputStream, michael@0: nsIFileOutputStream) michael@0: michael@0: NS_IMETHODIMP michael@0: nsCheckSummedOutputStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm, michael@0: int32_t behaviorFlags) michael@0: { michael@0: nsresult rv; michael@0: mHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = mHash->Init(nsICryptoHash::MD5); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return nsSafeFileOutputStream::Init(file, ioFlags, perm, behaviorFlags); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCheckSummedOutputStream::Finish() michael@0: { michael@0: nsresult rv = mHash->Finish(false, mCheckSum); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: uint32_t written; michael@0: rv = nsSafeFileOutputStream::Write(reinterpret_cast(mCheckSum.BeginReading()), michael@0: mCheckSum.Length(), &written); michael@0: NS_ASSERTION(written == mCheckSum.Length(), "Error writing stream checksum"); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return nsSafeFileOutputStream::Finish(); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsCheckSummedOutputStream::Write(const char *buf, uint32_t count, uint32_t *result) michael@0: { michael@0: nsresult rv = mHash->Update(reinterpret_cast(buf), count); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return nsSafeFileOutputStream::Write(buf, count, result); michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////////