2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-20 09:32:28 -07:00
|
|
|
#include "nsIOService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsBinHexDecoder.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIStreamConverterService.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsIPipe.h"
|
|
|
|
#include "nsMimeTypes.h"
|
|
|
|
#include "netCore.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "prnetdb.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
|
|
|
|
#include "nsIMIMEService.h"
|
|
|
|
#include "nsMimeTypes.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
// sadly I couldn't find char defintions for CR LF elsehwere in the code (they are defined as strings in nsCRT.h)
|
|
|
|
#define CR '\015'
|
|
|
|
#define LF '\012'
|
|
|
|
|
|
|
|
nsBinHexDecoder::nsBinHexDecoder() :
|
2009-08-05 11:41:45 -07:00
|
|
|
mState(0), mCRC(0), mFileCRC(0), mOctetin(26),
|
|
|
|
mDonePos(3), mInCRC(0), mCount(0), mMarker(0), mPosInbuff(0),
|
2007-03-22 10:30:00 -07:00
|
|
|
mPosOutputBuff(0)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
mDataBuffer = nullptr;
|
|
|
|
mOutgoingBuffer = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-05 11:41:45 -07:00
|
|
|
mOctetBuf.val = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
mHeader.type = 0;
|
|
|
|
mHeader.creator = 0;
|
|
|
|
mHeader.flags = 0;
|
|
|
|
mHeader.dlen = 0;
|
|
|
|
mHeader.rlen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsBinHexDecoder::~nsBinHexDecoder()
|
|
|
|
{
|
|
|
|
if (mDataBuffer)
|
|
|
|
nsMemory::Free(mDataBuffer);
|
|
|
|
if (mOutgoingBuffer)
|
|
|
|
nsMemory::Free(mOutgoingBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsBinHexDecoder)
|
|
|
|
NS_IMPL_RELEASE(nsBinHexDecoder)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsBinHexDecoder)
|
2009-08-05 11:41:45 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamConverter)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
|
|
|
|
// The binhex 4.0 decoder table....
|
|
|
|
|
2010-04-20 01:44:59 -07:00
|
|
|
static signed char binhex_decode[256] =
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-08-05 11:41:45 -07:00
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, -1, -1,
|
|
|
|
13, 14, 15, 16, 17, 18, 19, -1, 20, 21, -1, -1, -1, -1, -1, -1,
|
|
|
|
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, -1,
|
|
|
|
37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, -1, -1, -1, -1,
|
|
|
|
48, 49, 50, 51, 52, 53, 54, -1, 55, 56, 57, 58, 59, 60, -1, -1,
|
|
|
|
61, 62, 63, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#define BHEXVAL(c) (binhex_decode[(unsigned char) c])
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// nsIStreamConverter methods...
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinHexDecoder::Convert(nsIInputStream *aFromStream,
|
|
|
|
const char *aFromType,
|
|
|
|
const char *aToType,
|
2009-08-05 11:41:45 -07:00
|
|
|
nsISupports *aCtxt,
|
|
|
|
nsIInputStream **aResultStream)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-08-05 11:41:45 -07:00
|
|
|
nsBinHexDecoder::AsyncConvertData(const char *aFromType,
|
2007-03-22 10:30:00 -07:00
|
|
|
const char *aToType,
|
2009-08-05 11:41:45 -07:00
|
|
|
nsIStreamListener *aListener,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsISupports *aCtxt)
|
|
|
|
{
|
2009-08-05 11:41:45 -07:00
|
|
|
NS_ASSERTION(aListener && aFromType && aToType,
|
2007-03-22 10:30:00 -07:00
|
|
|
"null pointer passed into bin hex converter");
|
|
|
|
|
|
|
|
// hook up our final listener. this guy gets the various On*() calls we want to throw
|
|
|
|
// at him.
|
|
|
|
//
|
|
|
|
mNextListener = aListener;
|
|
|
|
return (aListener) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// nsIStreamListener methods...
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
NS_IMETHODIMP
|
2009-08-05 11:41:45 -07:00
|
|
|
nsBinHexDecoder::OnDataAvailable(nsIRequest* request,
|
|
|
|
nsISupports *aCtxt,
|
|
|
|
nsIInputStream *aStream,
|
2012-09-05 19:41:02 -07:00
|
|
|
uint64_t aSourceOffset,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (mOutputStream && mDataBuffer && aCount > 0)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numBytesRead = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (aCount > 0) // while we still have bytes to copy...
|
|
|
|
{
|
2013-01-15 04:22:03 -08:00
|
|
|
aStream->Read(mDataBuffer, std::min(aCount, nsIOService::gDefaultSegmentSize - 1), &numBytesRead);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aCount >= numBytesRead)
|
|
|
|
aCount -= numBytesRead; // subtract off the number of bytes we just read
|
|
|
|
else
|
|
|
|
aCount = 0;
|
|
|
|
|
|
|
|
// Process this new chunk of bin hex data...
|
|
|
|
ProcessNextChunk(request, aCtxt, numBytesRead);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsBinHexDecoder::ProcessNextState(nsIRequest * aRequest, nsISupports * aContext)
|
|
|
|
{
|
2009-08-05 11:41:45 -07:00
|
|
|
nsresult status = NS_OK;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t tmpcrc, cval;
|
2009-08-05 11:41:45 -07:00
|
|
|
unsigned char ctmp, c = mRlebuf;
|
|
|
|
|
|
|
|
/* do CRC */
|
|
|
|
ctmp = mInCRC ? c : 0;
|
|
|
|
cval = mCRC & 0xf000;
|
2012-08-22 08:56:38 -07:00
|
|
|
tmpcrc = ((uint16_t) (mCRC << 4) | (ctmp >> 4)) ^ (cval | (cval >> 7) | (cval >> 12));
|
2009-08-05 11:41:45 -07:00
|
|
|
cval = tmpcrc & 0xf000;
|
2012-08-22 08:56:38 -07:00
|
|
|
mCRC = ((uint16_t) (tmpcrc << 4) | (ctmp & 0x0f)) ^ (cval | (cval >> 7) | (cval >> 12));
|
2009-08-05 11:41:45 -07:00
|
|
|
|
|
|
|
/* handle state */
|
|
|
|
switch (mState)
|
|
|
|
{
|
|
|
|
case BINHEX_STATE_START:
|
|
|
|
mState = BINHEX_STATE_FNAME;
|
2009-08-05 11:42:52 -07:00
|
|
|
mCount = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// c & 63 returns the length of mName. So if we need the length, that's how
|
2009-08-05 11:42:52 -07:00
|
|
|
// you can figure it out....
|
|
|
|
mName.SetLength(c & 63);
|
2010-02-02 13:56:45 -08:00
|
|
|
if (mName.Length() != (c & 63)) {
|
2009-08-05 11:42:52 -07:00
|
|
|
/* XXX ProcessNextState/ProcessNextChunk aren't rv checked */
|
|
|
|
mState = BINHEX_STATE_DONE;
|
|
|
|
}
|
2009-08-05 11:41:45 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BINHEX_STATE_FNAME:
|
2009-08-05 11:42:52 -07:00
|
|
|
mName.BeginWriting()[mCount] = c;
|
2009-08-05 11:41:45 -07:00
|
|
|
|
2009-08-05 11:42:52 -07:00
|
|
|
if (++mCount > mName.Length())
|
2009-08-05 11:41:45 -07:00
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
// okay we've figured out the file name....set the content type on the channel
|
|
|
|
// based on the file name AND issue our delayed on start request....
|
2009-08-05 11:41:45 -07:00
|
|
|
|
2009-08-05 11:42:52 -07:00
|
|
|
DetectContentType(aRequest, mName);
|
2007-03-22 10:30:00 -07:00
|
|
|
// now propagate the on start request
|
|
|
|
mNextListener->OnStartRequest(aRequest, aContext);
|
|
|
|
|
2009-08-05 11:41:45 -07:00
|
|
|
mState = BINHEX_STATE_HEADER;
|
|
|
|
mCount = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BINHEX_STATE_HEADER:
|
|
|
|
((char *) &mHeader)[mCount] = c;
|
|
|
|
if (++mCount == 18)
|
|
|
|
{
|
|
|
|
if (sizeof(binhex_header) != 18) /* fix an alignment problem in some OSes */
|
|
|
|
{
|
|
|
|
char *p = (char *)&mHeader;
|
|
|
|
p += 19;
|
|
|
|
for (c = 0; c < 8; c++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-08-05 11:41:45 -07:00
|
|
|
*p = *(p-2);
|
|
|
|
--p;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-08-05 11:41:45 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mState = BINHEX_STATE_HCRC;
|
2009-08-05 11:41:45 -07:00
|
|
|
mInCRC = 1;
|
|
|
|
mCount = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BINHEX_STATE_DFORK:
|
|
|
|
case BINHEX_STATE_RFORK:
|
|
|
|
mOutgoingBuffer[mPosOutputBuff++] = c;
|
|
|
|
if (--mCount == 0)
|
|
|
|
{
|
|
|
|
/* only output data fork in the non-mac system. */
|
|
|
|
if (mState == BINHEX_STATE_DFORK)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numBytesWritten = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
mOutputStream->Write(mOutgoingBuffer, mPosOutputBuff, &numBytesWritten);
|
2012-08-22 08:56:38 -07:00
|
|
|
if (int32_t(numBytesWritten) != mPosOutputBuff)
|
2007-03-22 10:30:00 -07:00
|
|
|
status = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// now propagate the data we just wrote
|
|
|
|
mNextListener->OnDataAvailable(aRequest, aContext, mInputStream, 0, numBytesWritten);
|
2009-08-05 11:41:45 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
status = NS_OK; /* do nothing for resource fork. */
|
|
|
|
|
|
|
|
mPosOutputBuff = 0;
|
|
|
|
|
|
|
|
if (status != NS_OK)
|
|
|
|
mState = BINHEX_STATE_DONE;
|
|
|
|
else
|
|
|
|
++mState;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mInCRC = 1;
|
2009-08-05 11:41:45 -07:00
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
else if (mPosOutputBuff >= (int32_t) nsIOService::gDefaultSegmentSize)
|
2009-08-05 11:41:45 -07:00
|
|
|
{
|
|
|
|
if (mState == BINHEX_STATE_DFORK)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numBytesWritten = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
mOutputStream->Write(mOutgoingBuffer, mPosOutputBuff, &numBytesWritten);
|
2012-08-22 08:56:38 -07:00
|
|
|
if (int32_t(numBytesWritten) != mPosOutputBuff)
|
2007-03-22 10:30:00 -07:00
|
|
|
status = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
mNextListener->OnDataAvailable(aRequest, aContext, mInputStream, 0, numBytesWritten);
|
|
|
|
mPosOutputBuff = 0;
|
2009-08-05 11:41:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BINHEX_STATE_HCRC:
|
|
|
|
case BINHEX_STATE_DCRC:
|
|
|
|
case BINHEX_STATE_RCRC:
|
|
|
|
if (!mCount++)
|
|
|
|
mFileCRC = (unsigned short) c << 8;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((mFileCRC | c) != mCRC)
|
|
|
|
{
|
|
|
|
mState = BINHEX_STATE_DONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* passed the CRC check!!!*/
|
|
|
|
mCRC = 0;
|
|
|
|
if (++mState == BINHEX_STATE_FINISH)
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
// when we reach the finished state...fire an on stop request on the event listener...
|
|
|
|
mNextListener->OnStopRequest(aRequest, aContext, NS_OK);
|
|
|
|
mNextListener = 0;
|
2009-08-05 11:41:45 -07:00
|
|
|
|
|
|
|
/* now We are done with everything. */
|
|
|
|
++mState;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == BINHEX_STATE_DFORK)
|
|
|
|
mCount = PR_ntohl(mHeader.dlen);
|
|
|
|
else
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
// we aren't processing the resurce Fork. uncomment this line if we make this converter
|
|
|
|
// smart enough to do this in the future.
|
2009-08-05 11:41:45 -07:00
|
|
|
// mCount = PR_ntohl(mHeader.rlen); /* it should in host byte order */
|
|
|
|
mCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCount) {
|
|
|
|
mInCRC = 0;
|
|
|
|
} else {
|
|
|
|
/* nothing inside, so skip to the next state. */
|
|
|
|
++mState;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult nsBinHexDecoder::ProcessNextChunk(nsIRequest * aRequest, nsISupports * aContext, uint32_t numBytesInBuffer)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool foundStart;
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t octetpos, c = 0;
|
|
|
|
uint32_t val;
|
2009-08-05 11:41:45 -07:00
|
|
|
mPosInDataBuffer = 0; // use member variable.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(numBytesInBuffer > 0, NS_ERROR_FAILURE);
|
2009-08-05 11:41:45 -07:00
|
|
|
|
|
|
|
// if it is the first time, seek to the right start place.
|
|
|
|
if (mState == BINHEX_STATE_START)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
foundStart = false;
|
2009-08-05 11:41:45 -07:00
|
|
|
// go through the line, until we get a ':'
|
|
|
|
while (mPosInDataBuffer < numBytesInBuffer)
|
|
|
|
{
|
|
|
|
c = mDataBuffer[mPosInDataBuffer++];
|
|
|
|
while (c == CR || c == LF)
|
|
|
|
{
|
|
|
|
if (mPosInDataBuffer >= numBytesInBuffer)
|
|
|
|
break;
|
|
|
|
|
|
|
|
c = mDataBuffer[mPosInDataBuffer++];
|
|
|
|
if (c == ':')
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
foundStart = true;
|
2009-08-05 11:41:45 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (foundStart) break; /* we got the start point. */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPosInDataBuffer >= numBytesInBuffer)
|
|
|
|
return NS_OK; /* we meet buff end before we get the start point, wait till next fills. */
|
|
|
|
|
|
|
|
if (c != ':')
|
|
|
|
return NS_ERROR_FAILURE; /* can't find the start character. */
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mState != BINHEX_STATE_DONE)
|
|
|
|
{
|
|
|
|
/* fill in octetbuf */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (mPosInDataBuffer >= numBytesInBuffer)
|
|
|
|
return NS_OK; /* end of buff, go on for the nxet calls. */
|
|
|
|
|
|
|
|
c = GetNextChar(numBytesInBuffer);
|
|
|
|
if (c == 0) return NS_OK;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if ((val = BHEXVAL(c)) == uint32_t(-1))
|
2009-08-05 11:41:45 -07:00
|
|
|
{
|
|
|
|
/* we incount an invalid character. */
|
|
|
|
if (c)
|
|
|
|
{
|
|
|
|
/* rolling back. */
|
|
|
|
--mDonePos;
|
|
|
|
if (mOctetin >= 14)
|
|
|
|
--mDonePos;
|
|
|
|
if (mOctetin >= 20)
|
|
|
|
--mDonePos;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mOctetBuf.val |= val << mOctetin;
|
|
|
|
}
|
|
|
|
while ((mOctetin -= 6) > 2);
|
|
|
|
|
|
|
|
/* handle decoded characters -- run length encoding (rle) detection */
|
|
|
|
|
|
|
|
// We put decoded chars into mOctetBuf.val in order from high to low (via
|
|
|
|
// bitshifting, above). But we want to byte-address them, so we want the
|
|
|
|
// first byte to correspond to the high byte. In other words, we want
|
|
|
|
// these bytes to be in network order.
|
|
|
|
mOctetBuf.val = PR_htonl(mOctetBuf.val);
|
|
|
|
|
|
|
|
for (octetpos = 0; octetpos < mDonePos; ++octetpos)
|
|
|
|
{
|
|
|
|
c = mOctetBuf.c[octetpos];
|
|
|
|
|
|
|
|
if (c == 0x90 && !mMarker++)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (mMarker)
|
|
|
|
{
|
|
|
|
if (c == 0)
|
|
|
|
{
|
|
|
|
mRlebuf = 0x90;
|
|
|
|
ProcessNextState(aRequest, aContext);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* we are in the run length mode */
|
|
|
|
while (--c > 0)
|
|
|
|
ProcessNextState(aRequest, aContext);
|
|
|
|
}
|
|
|
|
mMarker = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mRlebuf = (unsigned char) c;
|
|
|
|
ProcessNextState(aRequest, aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState >= BINHEX_STATE_DONE)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prepare for next 3 characters. */
|
|
|
|
if (mDonePos < 3 && mState < BINHEX_STATE_DONE)
|
|
|
|
mState = BINHEX_STATE_DONE;
|
|
|
|
|
|
|
|
mOctetin = 26;
|
|
|
|
mOctetBuf.val = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t nsBinHexDecoder::GetNextChar(uint32_t numBytesInBuffer)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-08-05 11:41:45 -07:00
|
|
|
char c = 0;
|
|
|
|
|
|
|
|
while (mPosInDataBuffer < numBytesInBuffer)
|
|
|
|
{
|
|
|
|
c = mDataBuffer[mPosInDataBuffer++];
|
|
|
|
if (c != LF && c != CR)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (c == LF || c == CR) ? 0 : (int) c;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
// nsIRequestObserver methods...
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-08-05 11:41:45 -07:00
|
|
|
nsBinHexDecoder::OnStartRequest(nsIRequest* request, nsISupports *aCtxt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(mNextListener, NS_ERROR_FAILURE);
|
|
|
|
|
2010-04-20 09:32:28 -07:00
|
|
|
mDataBuffer = (char *) nsMemory::Alloc((sizeof(char) * nsIOService::gDefaultSegmentSize));
|
|
|
|
mOutgoingBuffer = (char *) nsMemory::Alloc((sizeof(char) * nsIOService::gDefaultSegmentSize));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mDataBuffer || !mOutgoingBuffer) return NS_ERROR_FAILURE; // out of memory;
|
|
|
|
|
|
|
|
// now we want to create a pipe which we'll use to write our converted data...
|
|
|
|
rv = NS_NewPipe(getter_AddRefs(mInputStream), getter_AddRefs(mOutputStream),
|
2010-04-20 09:32:28 -07:00
|
|
|
nsIOService::gDefaultSegmentSize,
|
|
|
|
nsIOService::gDefaultSegmentSize,
|
2011-10-17 07:59:28 -07:00
|
|
|
true, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// don't propagate the on start request to mNextListener until we have determined the content type.
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given the fileName we discovered inside the bin hex decoding, figure out the
|
|
|
|
// content type and set it on the channel associated with the request. If the
|
|
|
|
// filename tells us nothing useful, just report an unknown type and let the
|
|
|
|
// unknown decoder handle things.
|
2009-08-05 11:42:52 -07:00
|
|
|
nsresult nsBinHexDecoder::DetectContentType(nsIRequest* aRequest,
|
|
|
|
const nsAFlatCString &aFilename)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-08-05 11:42:52 -07:00
|
|
|
if (aFilename.IsEmpty()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Nothing to do here.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-08-05 11:41:45 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIMIMEService> mimeService(do_GetService("@mozilla.org/mime;1", &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString contentType;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-05 11:42:52 -07:00
|
|
|
// extract the extension from aFilename and look it up.
|
|
|
|
const char * fileExt = strrchr(aFilename.get(), '.');
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fileExt) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mimeService->GetTypeFromExtension(nsDependentCString(fileExt), contentType);
|
|
|
|
|
|
|
|
// Only set the type if it's not empty and, to prevent recursive loops, not the binhex type
|
|
|
|
if (!contentType.IsEmpty() && !contentType.Equals(APPLICATION_BINHEX)) {
|
|
|
|
channel->SetContentType(contentType);
|
|
|
|
} else {
|
|
|
|
channel->SetContentType(NS_LITERAL_CSTRING(UNKNOWN_CONTENT_TYPE));
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBinHexDecoder::OnStopRequest(nsIRequest* request, nsISupports *aCtxt,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (!mNextListener) return NS_ERROR_FAILURE;
|
|
|
|
// don't do anything here...we'll fire our own on stop request when we are done
|
|
|
|
// processing the data....
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|