Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | #include "StreamFunctions.h" |
michael@0 | 7 | #include "nsDeflateConverter.h" |
michael@0 | 8 | #include "nsIStringStream.h" |
michael@0 | 9 | #include "nsIInputStreamPump.h" |
michael@0 | 10 | #include "nsComponentManagerUtils.h" |
michael@0 | 11 | #include "nsMemory.h" |
michael@0 | 12 | #include "nsAutoPtr.h" |
michael@0 | 13 | #include "plstr.h" |
michael@0 | 14 | |
michael@0 | 15 | #define ZLIB_TYPE "deflate" |
michael@0 | 16 | #define GZIP_TYPE "gzip" |
michael@0 | 17 | #define X_GZIP_TYPE "x-gzip" |
michael@0 | 18 | |
michael@0 | 19 | /** |
michael@0 | 20 | * nsDeflateConverter is a stream converter applies the deflate compression |
michael@0 | 21 | * method to the data. |
michael@0 | 22 | */ |
michael@0 | 23 | NS_IMPL_ISUPPORTS(nsDeflateConverter, nsIStreamConverter, |
michael@0 | 24 | nsIStreamListener, |
michael@0 | 25 | nsIRequestObserver) |
michael@0 | 26 | |
michael@0 | 27 | nsresult nsDeflateConverter::Init() |
michael@0 | 28 | { |
michael@0 | 29 | int zerr; |
michael@0 | 30 | |
michael@0 | 31 | mOffset = 0; |
michael@0 | 32 | |
michael@0 | 33 | mZstream.zalloc = Z_NULL; |
michael@0 | 34 | mZstream.zfree = Z_NULL; |
michael@0 | 35 | mZstream.opaque = Z_NULL; |
michael@0 | 36 | |
michael@0 | 37 | int32_t window = MAX_WBITS; |
michael@0 | 38 | switch (mWrapMode) { |
michael@0 | 39 | case WRAP_NONE: |
michael@0 | 40 | window = -window; |
michael@0 | 41 | break; |
michael@0 | 42 | case WRAP_GZIP: |
michael@0 | 43 | window += 16; |
michael@0 | 44 | break; |
michael@0 | 45 | default: |
michael@0 | 46 | break; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | zerr = deflateInit2(&mZstream, mLevel, Z_DEFLATED, window, 8, |
michael@0 | 50 | Z_DEFAULT_STRATEGY); |
michael@0 | 51 | if (zerr != Z_OK) return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 52 | |
michael@0 | 53 | mZstream.next_out = mWriteBuffer; |
michael@0 | 54 | mZstream.avail_out = sizeof(mWriteBuffer); |
michael@0 | 55 | |
michael@0 | 56 | // mark the input buffer as empty. |
michael@0 | 57 | mZstream.avail_in = 0; |
michael@0 | 58 | mZstream.next_in = Z_NULL; |
michael@0 | 59 | |
michael@0 | 60 | return NS_OK; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | /* nsIInputStream convert (in nsIInputStream aFromStream, in string aFromType |
michael@0 | 64 | * in string aToType, in nsISupports aCtxt); */ |
michael@0 | 65 | NS_IMETHODIMP nsDeflateConverter::Convert(nsIInputStream *aFromStream, |
michael@0 | 66 | const char *aFromType, |
michael@0 | 67 | const char *aToType, |
michael@0 | 68 | nsISupports *aCtxt, |
michael@0 | 69 | nsIInputStream **_retval) |
michael@0 | 70 | { |
michael@0 | 71 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | /* void asyncConvertData (in string aFromType, in string aToType, |
michael@0 | 75 | * in nsIStreamListener aListener, |
michael@0 | 76 | * in nsISupports aCtxt); */ |
michael@0 | 77 | NS_IMETHODIMP nsDeflateConverter::AsyncConvertData(const char *aFromType, |
michael@0 | 78 | const char *aToType, |
michael@0 | 79 | nsIStreamListener *aListener, |
michael@0 | 80 | nsISupports *aCtxt) |
michael@0 | 81 | { |
michael@0 | 82 | if (mListener) |
michael@0 | 83 | return NS_ERROR_ALREADY_INITIALIZED; |
michael@0 | 84 | |
michael@0 | 85 | NS_ENSURE_ARG_POINTER(aListener); |
michael@0 | 86 | |
michael@0 | 87 | if (!PL_strncasecmp(aToType, ZLIB_TYPE, sizeof(ZLIB_TYPE)-1)) |
michael@0 | 88 | mWrapMode = WRAP_ZLIB; |
michael@0 | 89 | else if (!PL_strcasecmp(aToType, GZIP_TYPE) || |
michael@0 | 90 | !PL_strcasecmp(aToType, X_GZIP_TYPE)) |
michael@0 | 91 | mWrapMode = WRAP_GZIP; |
michael@0 | 92 | else |
michael@0 | 93 | mWrapMode = WRAP_NONE; |
michael@0 | 94 | |
michael@0 | 95 | nsresult rv = Init(); |
michael@0 | 96 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 97 | |
michael@0 | 98 | mListener = aListener; |
michael@0 | 99 | mContext = aCtxt; |
michael@0 | 100 | return rv; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | /* void onDataAvailable (in nsIRequest aRequest, in nsISupports aContext, |
michael@0 | 104 | * in nsIInputStream aInputStream, |
michael@0 | 105 | * in unsigned long long aOffset, |
michael@0 | 106 | * in unsigned long aCount); */ |
michael@0 | 107 | NS_IMETHODIMP nsDeflateConverter::OnDataAvailable(nsIRequest *aRequest, |
michael@0 | 108 | nsISupports *aContext, |
michael@0 | 109 | nsIInputStream *aInputStream, |
michael@0 | 110 | uint64_t aOffset, |
michael@0 | 111 | uint32_t aCount) |
michael@0 | 112 | { |
michael@0 | 113 | if (!mListener) |
michael@0 | 114 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 115 | |
michael@0 | 116 | nsAutoArrayPtr<char> buffer(new char[aCount]); |
michael@0 | 117 | NS_ENSURE_TRUE(buffer, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 118 | |
michael@0 | 119 | nsresult rv = ZW_ReadData(aInputStream, buffer.get(), aCount); |
michael@0 | 120 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 121 | |
michael@0 | 122 | // make sure we aren't reading too much |
michael@0 | 123 | mZstream.avail_in = aCount; |
michael@0 | 124 | mZstream.next_in = (unsigned char*)buffer.get(); |
michael@0 | 125 | |
michael@0 | 126 | int zerr = Z_OK; |
michael@0 | 127 | // deflate loop |
michael@0 | 128 | while (mZstream.avail_in > 0 && zerr == Z_OK) { |
michael@0 | 129 | zerr = deflate(&mZstream, Z_NO_FLUSH); |
michael@0 | 130 | |
michael@0 | 131 | while (mZstream.avail_out == 0) { |
michael@0 | 132 | // buffer is full, push the data out to the listener |
michael@0 | 133 | rv = PushAvailableData(aRequest, aContext); |
michael@0 | 134 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 135 | zerr = deflate(&mZstream, Z_NO_FLUSH); |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | return NS_OK; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | /* void onStartRequest (in nsIRequest aRequest, in nsISupports aContext); */ |
michael@0 | 143 | NS_IMETHODIMP nsDeflateConverter::OnStartRequest(nsIRequest *aRequest, |
michael@0 | 144 | nsISupports *aContext) |
michael@0 | 145 | { |
michael@0 | 146 | if (!mListener) |
michael@0 | 147 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 148 | |
michael@0 | 149 | return mListener->OnStartRequest(aRequest, mContext); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | /* void onStopRequest (in nsIRequest aRequest, in nsISupports aContext, |
michael@0 | 153 | * in nsresult aStatusCode); */ |
michael@0 | 154 | NS_IMETHODIMP nsDeflateConverter::OnStopRequest(nsIRequest *aRequest, |
michael@0 | 155 | nsISupports *aContext, |
michael@0 | 156 | nsresult aStatusCode) |
michael@0 | 157 | { |
michael@0 | 158 | if (!mListener) |
michael@0 | 159 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 160 | |
michael@0 | 161 | nsresult rv; |
michael@0 | 162 | |
michael@0 | 163 | int zerr; |
michael@0 | 164 | do { |
michael@0 | 165 | zerr = deflate(&mZstream, Z_FINISH); |
michael@0 | 166 | rv = PushAvailableData(aRequest, aContext); |
michael@0 | 167 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 168 | } while (zerr == Z_OK); |
michael@0 | 169 | |
michael@0 | 170 | deflateEnd(&mZstream); |
michael@0 | 171 | |
michael@0 | 172 | return mListener->OnStopRequest(aRequest, mContext, aStatusCode); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | nsresult nsDeflateConverter::PushAvailableData(nsIRequest *aRequest, |
michael@0 | 176 | nsISupports *aContext) |
michael@0 | 177 | { |
michael@0 | 178 | uint32_t bytesToWrite = sizeof(mWriteBuffer) - mZstream.avail_out; |
michael@0 | 179 | // We don't need to do anything if there isn't any data |
michael@0 | 180 | if (bytesToWrite == 0) |
michael@0 | 181 | return NS_OK; |
michael@0 | 182 | |
michael@0 | 183 | nsresult rv; |
michael@0 | 184 | nsCOMPtr<nsIStringInputStream> stream = |
michael@0 | 185 | do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv); |
michael@0 | 186 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 187 | |
michael@0 | 188 | stream->ShareData((char*)mWriteBuffer, bytesToWrite); |
michael@0 | 189 | rv = mListener->OnDataAvailable(aRequest, mContext, stream, mOffset, |
michael@0 | 190 | bytesToWrite); |
michael@0 | 191 | |
michael@0 | 192 | // now set the state for 'deflate' |
michael@0 | 193 | mZstream.next_out = mWriteBuffer; |
michael@0 | 194 | mZstream.avail_out = sizeof(mWriteBuffer); |
michael@0 | 195 | |
michael@0 | 196 | mOffset += bytesToWrite; |
michael@0 | 197 | return rv; |
michael@0 | 198 | } |