зеркало из https://github.com/mozilla/gecko-dev.git
Bug 508229 Clean up whitespace in nsBinHexDecoder
r=biesi
This commit is contained in:
Родитель
ff45688575
Коммит
4cd5ae33cc
|
@ -52,7 +52,7 @@
|
|||
#include "nsMimeTypes.h"
|
||||
|
||||
|
||||
#define DATA_BUFFER_SIZE (4096*2)
|
||||
#define DATA_BUFFER_SIZE (4096*2)
|
||||
|
||||
#define NS_STREAM_CONVERTER_SEGMENT_SIZE (4*1024)
|
||||
#define NS_STREAM_CONVERTER_BUFFER_SIZE (32*1024)
|
||||
|
@ -62,14 +62,14 @@
|
|||
#define LF '\012'
|
||||
|
||||
nsBinHexDecoder::nsBinHexDecoder() :
|
||||
mState(0), mCRC(0), mFileCRC(0), mOctetin(26),
|
||||
mDonePos(3), mInCRC(0), mCount(0), mMarker(0), mPosInbuff(0),
|
||||
mState(0), mCRC(0), mFileCRC(0), mOctetin(26),
|
||||
mDonePos(3), mInCRC(0), mCount(0), mMarker(0), mPosInbuff(0),
|
||||
mPosOutputBuff(0)
|
||||
{
|
||||
mDataBuffer = nsnull;
|
||||
mOutgoingBuffer = nsnull;
|
||||
|
||||
mOctetBuf.val = 0;
|
||||
mOctetBuf.val = 0;
|
||||
mHeader.type = 0;
|
||||
mHeader.creator = 0;
|
||||
mHeader.flags = 0;
|
||||
|
@ -89,33 +89,33 @@ NS_IMPL_ADDREF(nsBinHexDecoder)
|
|||
NS_IMPL_RELEASE(nsBinHexDecoder)
|
||||
|
||||
NS_INTERFACE_MAP_BEGIN(nsBinHexDecoder)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIStreamConverter)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
||||
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIStreamConverter)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
||||
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
||||
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
||||
NS_INTERFACE_MAP_END
|
||||
|
||||
|
||||
// The binhex 4.0 decoder table....
|
||||
|
||||
static char binhex_decode[256] =
|
||||
static char binhex_decode[256] =
|
||||
{
|
||||
-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,
|
||||
-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,
|
||||
};
|
||||
|
||||
#define BHEXVAL(c) (binhex_decode[(unsigned char) c])
|
||||
|
@ -128,19 +128,19 @@ NS_IMETHODIMP
|
|||
nsBinHexDecoder::Convert(nsIInputStream *aFromStream,
|
||||
const char *aFromType,
|
||||
const char *aToType,
|
||||
nsISupports *aCtxt,
|
||||
nsIInputStream **aResultStream)
|
||||
nsISupports *aCtxt,
|
||||
nsIInputStream **aResultStream)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsBinHexDecoder::AsyncConvertData(const char *aFromType,
|
||||
nsBinHexDecoder::AsyncConvertData(const char *aFromType,
|
||||
const char *aToType,
|
||||
nsIStreamListener *aListener,
|
||||
nsIStreamListener *aListener,
|
||||
nsISupports *aCtxt)
|
||||
{
|
||||
NS_ASSERTION(aListener && aFromType && aToType,
|
||||
NS_ASSERTION(aListener && aFromType && aToType,
|
||||
"null pointer passed into bin hex converter");
|
||||
|
||||
// hook up our final listener. this guy gets the various On*() calls we want to throw
|
||||
|
@ -154,17 +154,17 @@ nsBinHexDecoder::AsyncConvertData(const char *aFromType,
|
|||
// nsIStreamListener methods...
|
||||
//////////////////////////////////////////////////////
|
||||
NS_IMETHODIMP
|
||||
nsBinHexDecoder::OnDataAvailable(nsIRequest* request,
|
||||
nsISupports *aCtxt,
|
||||
nsIInputStream *aStream,
|
||||
PRUint32 aSourceOffset,
|
||||
PRUint32 aCount)
|
||||
nsBinHexDecoder::OnDataAvailable(nsIRequest* request,
|
||||
nsISupports *aCtxt,
|
||||
nsIInputStream *aStream,
|
||||
PRUint32 aSourceOffset,
|
||||
PRUint32 aCount)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
if (mOutputStream && mDataBuffer && aCount > 0)
|
||||
{
|
||||
PRUint32 numBytesRead = 0;
|
||||
PRUint32 numBytesRead = 0;
|
||||
while (aCount > 0) // while we still have bytes to copy...
|
||||
{
|
||||
aStream->Read(mDataBuffer, PR_MIN(aCount, DATA_BUFFER_SIZE - 1), &numBytesRead);
|
||||
|
@ -183,285 +183,290 @@ nsBinHexDecoder::OnDataAvailable(nsIRequest* request,
|
|||
|
||||
nsresult nsBinHexDecoder::ProcessNextState(nsIRequest * aRequest, nsISupports * aContext)
|
||||
{
|
||||
nsresult status = NS_OK;
|
||||
PRUint16 tmpcrc, cval;
|
||||
unsigned char ctmp, c = mRlebuf;
|
||||
|
||||
/* do CRC */
|
||||
ctmp = mInCRC ? c : 0;
|
||||
cval = mCRC & 0xf000;
|
||||
tmpcrc = ((PRUint16) (mCRC << 4) | (ctmp >> 4)) ^ (cval | (cval >> 7) | (cval >> 12));
|
||||
cval = tmpcrc & 0xf000;
|
||||
mCRC = ((PRUint16) (tmpcrc << 4) | (ctmp & 0x0f)) ^ (cval | (cval >> 7) | (cval >> 12));
|
||||
nsresult status = NS_OK;
|
||||
PRUint16 tmpcrc, cval;
|
||||
unsigned char ctmp, c = mRlebuf;
|
||||
|
||||
/* handle state */
|
||||
switch (mState)
|
||||
{
|
||||
case BINHEX_STATE_START:
|
||||
mState = BINHEX_STATE_FNAME;
|
||||
mCount = 1;
|
||||
/* do CRC */
|
||||
ctmp = mInCRC ? c : 0;
|
||||
cval = mCRC & 0xf000;
|
||||
tmpcrc = ((PRUint16) (mCRC << 4) | (ctmp >> 4)) ^ (cval | (cval >> 7) | (cval >> 12));
|
||||
cval = tmpcrc & 0xf000;
|
||||
mCRC = ((PRUint16) (tmpcrc << 4) | (ctmp & 0x0f)) ^ (cval | (cval >> 7) | (cval >> 12));
|
||||
|
||||
/* handle state */
|
||||
switch (mState)
|
||||
{
|
||||
case BINHEX_STATE_START:
|
||||
mState = BINHEX_STATE_FNAME;
|
||||
mCount = 1;
|
||||
|
||||
// c & 63 returns the length of mName. So if we need the length, that's how
|
||||
// you can figure it out...for now we are storing it in the first byte of mName
|
||||
*(mName) = (c & 63);
|
||||
break;
|
||||
|
||||
case BINHEX_STATE_FNAME:
|
||||
mName[mCount] = c;
|
||||
|
||||
if (mCount++ > *(mName)) // the first byte of mName is the length...
|
||||
{
|
||||
*(mName) = (c & 63);
|
||||
break;
|
||||
|
||||
case BINHEX_STATE_FNAME:
|
||||
mName[mCount] = c;
|
||||
|
||||
if (mCount++ > *(mName)) // the first byte of mName is the length...
|
||||
{
|
||||
// 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....
|
||||
// be sure to skip the first byte of mName which is the size of the file name
|
||||
|
||||
|
||||
SetContentType(aRequest, &mName[1]);
|
||||
// now propagate the on start request
|
||||
mNextListener->OnStartRequest(aRequest, aContext);
|
||||
|
||||
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++)
|
||||
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++)
|
||||
{
|
||||
*p = *(p-2); p--;
|
||||
*p = *(p-2);
|
||||
--p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mState = BINHEX_STATE_HCRC;
|
||||
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)
|
||||
{
|
||||
PRUint32 numBytesWritten = 0;
|
||||
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)
|
||||
{
|
||||
PRUint32 numBytesWritten = 0;
|
||||
mOutputStream->Write(mOutgoingBuffer, mPosOutputBuff, &numBytesWritten);
|
||||
if (PRInt32(numBytesWritten) != mPosOutputBuff)
|
||||
status = NS_ERROR_FAILURE;
|
||||
|
||||
// now propagate the data we just wrote
|
||||
mNextListener->OnDataAvailable(aRequest, aContext, mInputStream, 0, numBytesWritten);
|
||||
}
|
||||
else
|
||||
status = NS_OK; /* do nothing for resource fork. */
|
||||
}
|
||||
else
|
||||
status = NS_OK; /* do nothing for resource fork. */
|
||||
|
||||
mPosOutputBuff = 0;
|
||||
|
||||
if (status != NS_OK)
|
||||
mState = BINHEX_STATE_DONE;
|
||||
else
|
||||
++mState;
|
||||
|
||||
mPosOutputBuff = 0;
|
||||
|
||||
if (status != NS_OK)
|
||||
mState = BINHEX_STATE_DONE;
|
||||
else
|
||||
mState ++;
|
||||
|
||||
mInCRC = 1;
|
||||
}
|
||||
else if (mPosOutputBuff >= DATA_BUFFER_SIZE)
|
||||
{
|
||||
if (mState == BINHEX_STATE_DFORK)
|
||||
{
|
||||
PRUint32 numBytesWritten = 0;
|
||||
}
|
||||
else if (mPosOutputBuff >= DATA_BUFFER_SIZE)
|
||||
{
|
||||
if (mState == BINHEX_STATE_DFORK)
|
||||
{
|
||||
PRUint32 numBytesWritten = 0;
|
||||
mOutputStream->Write(mOutgoingBuffer, mPosOutputBuff, &numBytesWritten);
|
||||
if (PRInt32(numBytesWritten) != mPosOutputBuff)
|
||||
status = NS_ERROR_FAILURE;
|
||||
|
||||
mNextListener->OnDataAvailable(aRequest, aContext, mInputStream, 0, numBytesWritten);
|
||||
mPosOutputBuff = 0;
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
// when we reach the finished state...fire an on stop request on the event listener...
|
||||
mNextListener->OnStopRequest(aRequest, aContext, NS_OK);
|
||||
mNextListener = 0;
|
||||
|
||||
/* now We are done with everything. */
|
||||
mState++;
|
||||
break;
|
||||
}
|
||||
|
||||
if (mState == BINHEX_STATE_DFORK)
|
||||
mCount = PR_ntohl(mHeader.dlen);
|
||||
else
|
||||
{
|
||||
|
||||
/* now We are done with everything. */
|
||||
++mState;
|
||||
break;
|
||||
}
|
||||
|
||||
if (mState == BINHEX_STATE_DFORK)
|
||||
mCount = PR_ntohl(mHeader.dlen);
|
||||
else
|
||||
{
|
||||
// we aren't processing the resurce Fork. uncomment this line if we make this converter
|
||||
// smart enough to do this in the future.
|
||||
// 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;
|
||||
// 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;
|
||||
}
|
||||
|
||||
nsresult nsBinHexDecoder::ProcessNextChunk(nsIRequest * aRequest, nsISupports * aContext, PRUint32 numBytesInBuffer)
|
||||
{
|
||||
PRBool foundStart;
|
||||
PRInt16 octetpos, c = 0;
|
||||
PRUint32 val;
|
||||
mPosInDataBuffer = 0; // use member variable.
|
||||
PRBool foundStart;
|
||||
PRInt16 octetpos, c = 0;
|
||||
PRUint32 val;
|
||||
mPosInDataBuffer = 0; // use member variable.
|
||||
|
||||
NS_ENSURE_TRUE(numBytesInBuffer > 0, NS_ERROR_FAILURE);
|
||||
|
||||
// if it is the first time, seek to the right start place.
|
||||
if (mState == BINHEX_STATE_START)
|
||||
{
|
||||
foundStart = PR_FALSE;
|
||||
// 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 == ':')
|
||||
{
|
||||
foundStart = PR_TRUE;
|
||||
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;
|
||||
|
||||
if ((val = BHEXVAL(c)) == PRUint32(-1))
|
||||
{
|
||||
/* 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);
|
||||
// if it is the first time, seek to the right start place.
|
||||
if (mState == BINHEX_STATE_START)
|
||||
{
|
||||
foundStart = PR_FALSE;
|
||||
// go through the line, until we get a ':'
|
||||
while (mPosInDataBuffer < numBytesInBuffer)
|
||||
{
|
||||
c = mDataBuffer[mPosInDataBuffer++];
|
||||
while (c == CR || c == LF)
|
||||
{
|
||||
if (mPosInDataBuffer >= numBytesInBuffer)
|
||||
break;
|
||||
|
||||
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
|
||||
{
|
||||
while (--c > 0) /* we are in the run lenght mode */
|
||||
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;
|
||||
}
|
||||
c = mDataBuffer[mPosInDataBuffer++];
|
||||
if (c == ':')
|
||||
{
|
||||
foundStart = PR_TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundStart) break; /* we got the start point. */
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
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;
|
||||
|
||||
if ((val = BHEXVAL(c)) == PRUint32(-1))
|
||||
{
|
||||
/* 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;
|
||||
}
|
||||
|
||||
PRInt16 nsBinHexDecoder::GetNextChar(PRUint32 numBytesInBuffer)
|
||||
{
|
||||
char c = 0;
|
||||
|
||||
while (mPosInDataBuffer < numBytesInBuffer)
|
||||
{
|
||||
c = mDataBuffer[mPosInDataBuffer++];
|
||||
if (c != LF && c != CR)
|
||||
break;
|
||||
}
|
||||
return (c == LF || c == CR) ? 0 : (int) c;
|
||||
char c = 0;
|
||||
|
||||
while (mPosInDataBuffer < numBytesInBuffer)
|
||||
{
|
||||
c = mDataBuffer[mPosInDataBuffer++];
|
||||
if (c != LF && c != CR)
|
||||
break;
|
||||
}
|
||||
return (c == LF || c == CR) ? 0 : (int) c;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
|
@ -469,7 +474,7 @@ PRInt16 nsBinHexDecoder::GetNextChar(PRUint32 numBytesInBuffer)
|
|||
//////////////////////////////////////////////////////
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsBinHexDecoder::OnStartRequest(nsIRequest* request, nsISupports *aCtxt)
|
||||
nsBinHexDecoder::OnStartRequest(nsIRequest* request, nsISupports *aCtxt)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
|
@ -504,7 +509,7 @@ nsresult nsBinHexDecoder::SetContentType(nsIRequest* aRequest,
|
|||
nsresult rv;
|
||||
nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest, &rv));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
|
||||
nsCOMPtr<nsIMIMEService> mimeService(do_GetService("@mozilla.org/mime;1", &rv));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
|
|
|
@ -38,16 +38,16 @@
|
|||
|
||||
// A decoder for Mac Bin Hex 4.0.
|
||||
|
||||
// This decoder is currently only intended to be used on NON-Mac platforms. It isn't hooked up to
|
||||
// This decoder is currently only intended to be used on NON-Mac platforms. It isn't hooked up to
|
||||
// code which would actually save the file to disk. As a result, we can't leverage the resource fork.
|
||||
// This makes this decoder most unhelpful for the Mac. Our assumption is that if you save a bin hex file
|
||||
// on the mac and try to open it, stuffit or some other tool is already going to be on the Mac which knows how
|
||||
// to handle bin hex. On windows and unix, that's not the case. We need client code to strip out the data fork.
|
||||
// So this decoder currently just strips out the data fork.
|
||||
// to handle bin hex. On windows and unix, that's not the case. We need client code to strip out the data fork.
|
||||
// So this decoder currently just strips out the data fork.
|
||||
|
||||
// Note: it's possible that we can eventually turn this decoder into both a decoder and into a file stream (much
|
||||
// like the apple double decoder) so on the Mac, if we are saving to disk, we can invoke the decoder as a file stream
|
||||
// and it will process the resource fork and do the right magic.
|
||||
// and it will process the resource fork and do the right magic.
|
||||
|
||||
#ifndef nsBinHexDecoder_h__
|
||||
#define nsBinHexDecoder_h__
|
||||
|
@ -60,36 +60,36 @@
|
|||
#include "nsCOMPtr.h"
|
||||
#include "nsString.h"
|
||||
|
||||
#define NS_BINHEXDECODER_CID \
|
||||
#define NS_BINHEXDECODER_CID \
|
||||
{ /* 301DEA42-6850-4cda-8945-81F7DBC2186B */ \
|
||||
0x301dea42, 0x6850, 0x4cda, \
|
||||
{ 0x89, 0x45, 0x81, 0xf7, 0xdb, 0xc2, 0x18, 0x6b } \
|
||||
0x301dea42, 0x6850, 0x4cda, \
|
||||
{ 0x89, 0x45, 0x81, 0xf7, 0xdb, 0xc2, 0x18, 0x6b } \
|
||||
}
|
||||
|
||||
typedef struct _binhex_header
|
||||
{
|
||||
PRUint32 type, creator;
|
||||
PRUint16 flags;
|
||||
PRInt32 dlen, rlen;
|
||||
PRUint32 type, creator;
|
||||
PRUint16 flags;
|
||||
PRInt32 dlen, rlen;
|
||||
} binhex_header;
|
||||
|
||||
typedef union
|
||||
typedef union
|
||||
{
|
||||
unsigned char c[4];
|
||||
PRUint32 val;
|
||||
unsigned char c[4];
|
||||
PRUint32 val;
|
||||
} longbuf;
|
||||
|
||||
#define BINHEX_STATE_START 0
|
||||
#define BINHEX_STATE_FNAME 1
|
||||
#define BINHEX_STATE_HEADER 2
|
||||
#define BINHEX_STATE_HCRC 3
|
||||
#define BINHEX_STATE_DFORK 4
|
||||
#define BINHEX_STATE_DCRC 5
|
||||
#define BINHEX_STATE_RFORK 6
|
||||
#define BINHEX_STATE_RCRC 7
|
||||
#define BINHEX_STATE_FINISH 8
|
||||
#define BINHEX_STATE_DONE 9
|
||||
/* #define BINHEX_STATE_ERROR 10 */
|
||||
#define BINHEX_STATE_START 0
|
||||
#define BINHEX_STATE_FNAME 1
|
||||
#define BINHEX_STATE_HEADER 2
|
||||
#define BINHEX_STATE_HCRC 3
|
||||
#define BINHEX_STATE_DFORK 4
|
||||
#define BINHEX_STATE_DCRC 5
|
||||
#define BINHEX_STATE_RFORK 6
|
||||
#define BINHEX_STATE_RCRC 7
|
||||
#define BINHEX_STATE_FINISH 8
|
||||
#define BINHEX_STATE_DONE 9
|
||||
/* #define BINHEX_STATE_ERROR 10 */
|
||||
|
||||
class nsBinHexDecoder : public nsIStreamConverter
|
||||
{
|
||||
|
@ -121,19 +121,19 @@ protected:
|
|||
|
||||
// the input and output streams form a pipe...they need to be passed around together..
|
||||
nsCOMPtr<nsIOutputStream> mOutputStream; // output stream
|
||||
nsCOMPtr<nsIInputStream> mInputStream;
|
||||
nsCOMPtr<nsIInputStream> mInputStream;
|
||||
|
||||
PRInt16 mState; /* current state */
|
||||
PRUint16 mCRC; /* cumulative CRC */
|
||||
PRUint16 mFileCRC; /* CRC value from file */
|
||||
longbuf mOctetBuf; /* buffer for decoded 6-bit values */
|
||||
PRInt16 mOctetin; /* current input position in octetbuf */
|
||||
PRInt16 mDonePos; /* ending position in octetbuf */
|
||||
PRInt16 mInCRC; /* flag set when reading a CRC */
|
||||
PRInt16 mState; /* current state */
|
||||
PRUint16 mCRC; /* cumulative CRC */
|
||||
PRUint16 mFileCRC; /* CRC value from file */
|
||||
longbuf mOctetBuf; /* buffer for decoded 6-bit values */
|
||||
PRInt16 mOctetin; /* current input position in octetbuf */
|
||||
PRInt16 mDonePos; /* ending position in octetbuf */
|
||||
PRInt16 mInCRC; /* flag set when reading a CRC */
|
||||
|
||||
// Bin Hex Header Information
|
||||
binhex_header mHeader;
|
||||
char mName[64]; /* fsspec for the output file */
|
||||
char mName[64]; /* fsspec for the output file */
|
||||
|
||||
// unfortunately we are going to need 2 8K buffers here. One for the data we are currently digesting. Another
|
||||
// for the outgoing decoded data. I tried getting them to share a buffer but things didn't work out so nicely.
|
||||
|
@ -141,13 +141,13 @@ protected:
|
|||
char * mOutgoingBuffer; // temporary holding pen for the incoming data.
|
||||
PRUint32 mPosInDataBuffer;
|
||||
|
||||
unsigned char mRlebuf; /* buffer for last run length encoding value */
|
||||
unsigned char mRlebuf; /* buffer for last run length encoding value */
|
||||
|
||||
PRInt32 mCount; /* generic counter */
|
||||
PRInt16 mMarker; /* flag indicating maker */
|
||||
|
||||
PRInt32 mPosInbuff; /* the index of the inbuff. */
|
||||
PRInt32 mPosOutputBuff; /* the position of the out buff. */
|
||||
PRInt32 mCount; /* generic counter */
|
||||
PRInt16 mMarker; /* flag indicating maker */
|
||||
|
||||
PRInt32 mPosInbuff; /* the index of the inbuff. */
|
||||
PRInt32 mPosOutputBuff; /* the position of the out buff. */
|
||||
};
|
||||
|
||||
#endif /* nsBinHexDecoder_h__ */
|
||||
|
|
Загрузка…
Ссылка в новой задаче