Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/url-classifier/nsCheckSummedOutputStream.h
Line
Count
Source (jump to first uncovered line)
1
//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#ifndef nsCheckSummedOutputStream_h__
7
#define nsCheckSummedOutputStream_h__
8
9
#include "nsIFile.h"
10
#include "nsIOutputStream.h"
11
#include "nsICryptoHash.h"
12
#include "nsNetCID.h"
13
#include "nsString.h"
14
#include "nsToolkitCompsCID.h"
15
#include "../../../netwerk/base/nsBufferedStreams.h"
16
#include "prio.h"
17
18
class nsCheckSummedOutputStream : public nsBufferedOutputStream
19
{
20
public:
21
  NS_DECL_ISUPPORTS_INHERITED
22
23
  // Size of MD5 hash in bytes
24
  static const uint32_t CHECKSUM_SIZE = 16;
25
  static const uint32_t MAX_BUFFER_SIZE = 64 * 1024;
26
27
0
  nsCheckSummedOutputStream() {}
28
29
  NS_IMETHOD Finish() override;
30
  NS_IMETHOD Write(const char *buf, uint32_t count, uint32_t *result) override;
31
  NS_IMETHOD Init(nsIOutputStream* stream, uint32_t bufferSize) override;
32
33
protected:
34
0
  virtual ~nsCheckSummedOutputStream() { nsBufferedOutputStream::Close(); }
35
36
  nsCOMPtr<nsICryptoHash> mHash;
37
  nsCString mCheckSum;
38
};
39
40
// returns a file output stream which can be QI'ed to nsIFileOutputStream.
41
inline nsresult
42
NS_NewCheckSummedOutputStream(nsIOutputStream **result,
43
                              nsIFile         *file)
44
0
{
45
0
    nsCOMPtr<nsIOutputStream> localOutFile;
46
0
    nsresult rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(localOutFile), file,
47
0
                                                  PR_WRONLY | PR_TRUNCATE | PR_CREATE_FILE);
48
0
    NS_ENSURE_SUCCESS(rv, rv);
49
0
50
0
    nsCOMPtr<nsIBufferedOutputStream> out = new nsCheckSummedOutputStream();
51
0
    rv = out->Init(localOutFile, nsCheckSummedOutputStream::CHECKSUM_SIZE);
52
0
    if (NS_SUCCEEDED(rv)) {
53
0
      out.forget(result);
54
0
    }
55
0
    return rv;
56
0
}
57
58
#endif