1 : //* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Url Classifier code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * the Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Gian-Carlo Pascutto <gpascutto@mozilla.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef nsCheckSummedOutputStream_h__
40 : #define nsCheckSummedOutputStream_h__
41 :
42 : #include "nsILocalFile.h"
43 : #include "nsIFile.h"
44 : #include "nsIOutputStream.h"
45 : #include "nsICryptoHash.h"
46 : #include "nsNetCID.h"
47 : #include "nsString.h"
48 : #include "../../../netwerk/base/src/nsFileStreams.h"
49 : #include "nsToolkitCompsCID.h"
50 :
51 : class nsCheckSummedOutputStream : public nsSafeFileOutputStream
52 : {
53 : public:
54 : NS_DECL_ISUPPORTS_INHERITED
55 :
56 : // Size of MD5 hash in bytes
57 : static const PRUint32 CHECKSUM_SIZE = 16;
58 :
59 97 : nsCheckSummedOutputStream() {}
60 388 : virtual ~nsCheckSummedOutputStream() { nsSafeFileOutputStream::Close(); }
61 :
62 : NS_IMETHOD Finish();
63 : NS_IMETHOD Write(const char *buf, PRUint32 count, PRUint32 *result);
64 : NS_IMETHOD Init(nsIFile* file, PRInt32 ioFlags, PRInt32 perm, PRInt32 behaviorFlags);
65 :
66 : protected:
67 : nsCOMPtr<nsICryptoHash> mHash;
68 : nsCAutoString mCheckSum;
69 : };
70 :
71 : // returns a file output stream which can be QI'ed to nsIFileOutputStream.
72 : inline nsresult
73 97 : NS_NewCheckSummedOutputStream(nsIOutputStream **result,
74 : nsIFile *file,
75 : PRInt32 ioFlags = -1,
76 : PRInt32 perm = -1,
77 : PRInt32 behaviorFlags = 0)
78 : {
79 194 : nsCOMPtr<nsIFileOutputStream> out = new nsCheckSummedOutputStream();
80 97 : nsresult rv = out->Init(file, ioFlags, perm, behaviorFlags);
81 97 : if (NS_SUCCEEDED(rv))
82 97 : NS_ADDREF(*result = out); // cannot use nsCOMPtr::swap
83 97 : return rv;
84 : }
85 :
86 : #endif
|