Line data Source code
1 : /* nsJARInputStream.h
2 : *
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/. */
6 :
7 : #ifndef nsJARINPUTSTREAM_h__
8 : #define nsJARINPUTSTREAM_h__
9 :
10 : #include "nsIInputStream.h"
11 : #include "nsJAR.h"
12 : #include "nsTArray.h"
13 : #include "mozilla/Attributes.h"
14 :
15 : #ifdef MOZ_JAR_BROTLI
16 : struct BrotliStateStruct;
17 : #endif
18 :
19 : /*-------------------------------------------------------------------------
20 : * Class nsJARInputStream declaration. This class defines the type of the
21 : * object returned by calls to nsJAR::GetInputStream(filename) for the
22 : * purpose of reading a file item out of a JAR file.
23 : *------------------------------------------------------------------------*/
24 : class nsJARInputStream final : public nsIInputStream
25 : {
26 : public:
27 0 : nsJARInputStream()
28 0 : : mOutSize(0)
29 : , mInCrc(0)
30 : , mOutCrc(0)
31 : #ifdef MOZ_JAR_BROTLI
32 : , mBrotliState(nullptr)
33 : #endif
34 : , mNameLen(0)
35 : , mCurPos(0)
36 : , mArrPos(0)
37 0 : , mMode(MODE_NOTINITED)
38 : {
39 0 : memset(&mZs, 0, sizeof(z_stream));
40 0 : }
41 :
42 : NS_DECL_THREADSAFE_ISUPPORTS
43 : NS_DECL_NSIINPUTSTREAM
44 :
45 : // takes ownership of |fd|, even on failure
46 : nsresult InitFile(nsJAR *aJar, nsZipItem *item);
47 :
48 : nsresult InitDirectory(nsJAR *aJar,
49 : const nsACString& aJarDirSpec,
50 : const char* aDir);
51 :
52 : private:
53 0 : ~nsJARInputStream() { Close(); }
54 :
55 : RefPtr<nsZipHandle> mFd; // handle for reading
56 : uint32_t mOutSize; // inflated size
57 : uint32_t mInCrc; // CRC as provided by the zipentry
58 : uint32_t mOutCrc; // CRC as calculated by me
59 : z_stream mZs; // zip data structure
60 : #ifdef MOZ_JAR_BROTLI
61 : BrotliStateStruct* mBrotliState; // Brotli decoder state
62 : #endif
63 :
64 : /* For directory reading */
65 : RefPtr<nsJAR> mJar; // string reference to zipreader
66 : uint32_t mNameLen; // length of dirname
67 : nsCString mBuffer; // storage for generated text of stream
68 : uint32_t mCurPos; // Current position in buffer
69 : uint32_t mArrPos; // current position within mArray
70 : nsTArray<nsCString> mArray; // array of names in (zip) directory
71 :
72 : typedef enum {
73 : MODE_NOTINITED,
74 : MODE_CLOSED,
75 : MODE_DIRECTORY,
76 : MODE_INFLATE,
77 : #ifdef MOZ_JAR_BROTLI
78 : MODE_BROTLI,
79 : #endif
80 : MODE_COPY
81 : } JISMode;
82 :
83 : JISMode mMode; // Modus of the stream
84 :
85 : nsresult ContinueInflate(char* aBuf, uint32_t aCount, uint32_t* aBytesRead);
86 : nsresult ReadDirectory(char* aBuf, uint32_t aCount, uint32_t* aBytesRead);
87 : uint32_t CopyDataToBuffer(char* &aBuffer, uint32_t &aCount);
88 : };
89 :
90 : #endif /* nsJARINPUTSTREAM_h__ */
91 :
|