toolkit/components/url-classifier/nsCheckSummedOutputStream.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/url-classifier/nsCheckSummedOutputStream.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,53 @@
     1.4 +//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsCheckSummedOutputStream_h__
    1.10 +#define nsCheckSummedOutputStream_h__
    1.11 +
    1.12 +#include "nsILocalFile.h"
    1.13 +#include "nsIFile.h"
    1.14 +#include "nsIOutputStream.h"
    1.15 +#include "nsICryptoHash.h"
    1.16 +#include "nsNetCID.h"
    1.17 +#include "nsString.h"
    1.18 +#include "../../../netwerk/base/src/nsFileStreams.h"
    1.19 +#include "nsToolkitCompsCID.h"
    1.20 +
    1.21 +class nsCheckSummedOutputStream : public nsSafeFileOutputStream
    1.22 +{
    1.23 +public:
    1.24 +  NS_DECL_ISUPPORTS_INHERITED
    1.25 +
    1.26 +  // Size of MD5 hash in bytes
    1.27 +  static const uint32_t CHECKSUM_SIZE = 16;
    1.28 +
    1.29 +  nsCheckSummedOutputStream() {}
    1.30 +  virtual ~nsCheckSummedOutputStream() { nsSafeFileOutputStream::Close(); }
    1.31 +
    1.32 +  NS_IMETHOD Finish();
    1.33 +  NS_IMETHOD Write(const char *buf, uint32_t count, uint32_t *result);
    1.34 +  NS_IMETHOD Init(nsIFile* file, int32_t ioFlags, int32_t perm, int32_t behaviorFlags);
    1.35 +
    1.36 +protected:
    1.37 +  nsCOMPtr<nsICryptoHash> mHash;
    1.38 +  nsAutoCString mCheckSum;
    1.39 +};
    1.40 +
    1.41 +// returns a file output stream which can be QI'ed to nsIFileOutputStream.
    1.42 +inline nsresult
    1.43 +NS_NewCheckSummedOutputStream(nsIOutputStream **result,
    1.44 +                              nsIFile         *file,
    1.45 +                              int32_t         ioFlags       = -1,
    1.46 +                              int32_t         perm          = -1,
    1.47 +                              int32_t         behaviorFlags =  0)
    1.48 +{
    1.49 +    nsCOMPtr<nsIFileOutputStream> out = new nsCheckSummedOutputStream();
    1.50 +    nsresult rv = out->Init(file, ioFlags, perm, behaviorFlags);
    1.51 +    if (NS_SUCCEEDED(rv))
    1.52 +      NS_ADDREF(*result = out);  // cannot use nsCOMPtr::swap
    1.53 +    return rv;
    1.54 +}
    1.55 +
    1.56 +#endif

mercurial