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
|
|
|
|
|
|
|
// A decoder for Mac Bin Hex 4.0.
|
|
|
|
|
2009-08-05 11:41:45 -07:00
|
|
|
// This decoder is currently only intended to be used on NON-Mac platforms. It isn't hooked up to
|
2007-03-22 10:30:00 -07:00
|
|
|
// 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
|
2009-08-05 11:41:45 -07:00
|
|
|
// 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.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// 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
|
2009-08-05 11:41:45 -07:00
|
|
|
// and it will process the resource fork and do the right magic.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef nsBinHexDecoder_h__
|
|
|
|
#define nsBinHexDecoder_h__
|
|
|
|
|
|
|
|
#include "nsIStreamConverter.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
2009-08-05 11:41:45 -07:00
|
|
|
#define NS_BINHEXDECODER_CID \
|
2007-03-22 10:30:00 -07:00
|
|
|
{ /* 301DEA42-6850-4cda-8945-81F7DBC2186B */ \
|
2009-08-05 11:41:45 -07:00
|
|
|
0x301dea42, 0x6850, 0x4cda, \
|
|
|
|
{ 0x89, 0x45, 0x81, 0xf7, 0xdb, 0xc2, 0x18, 0x6b } \
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct _binhex_header
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t type, creator;
|
|
|
|
uint16_t flags;
|
|
|
|
int32_t dlen, rlen;
|
2007-03-22 10:30:00 -07:00
|
|
|
} binhex_header;
|
|
|
|
|
2009-08-05 11:41:45 -07:00
|
|
|
typedef union
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-08-05 11:41:45 -07:00
|
|
|
unsigned char c[4];
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t val;
|
2007-03-22 10:30:00 -07:00
|
|
|
} longbuf;
|
|
|
|
|
2009-08-05 11:41:45 -07:00
|
|
|
#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 */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class nsBinHexDecoder : public nsIStreamConverter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// nsISupports methods
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIStreamConverter methods
|
|
|
|
NS_DECL_NSISTREAMCONVERTER
|
|
|
|
|
|
|
|
// nsIStreamListener methods
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
|
|
|
|
// nsIRequestObserver methods
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
|
|
|
|
nsBinHexDecoder();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~nsBinHexDecoder();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t GetNextChar(uint32_t numBytesInBuffer);
|
|
|
|
nsresult ProcessNextChunk(nsIRequest * aRequest, nsISupports * aContext, uint32_t numBytesInBuffer);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult ProcessNextState(nsIRequest * aRequest, nsISupports * aContext);
|
2009-08-05 11:42:52 -07:00
|
|
|
nsresult DetectContentType(nsIRequest * aRequest, const nsAFlatCString &aFilename);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
protected:
|
|
|
|
nsCOMPtr<nsIStreamListener> mNextListener;
|
|
|
|
|
|
|
|
// the input and output streams form a pipe...they need to be passed around together..
|
|
|
|
nsCOMPtr<nsIOutputStream> mOutputStream; // output stream
|
2009-08-05 11:41:45 -07:00
|
|
|
nsCOMPtr<nsIInputStream> mInputStream;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t mState; /* current state */
|
|
|
|
uint16_t mCRC; /* cumulative CRC */
|
|
|
|
uint16_t mFileCRC; /* CRC value from file */
|
2009-08-05 11:41:45 -07:00
|
|
|
longbuf mOctetBuf; /* buffer for decoded 6-bit values */
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t mOctetin; /* current input position in octetbuf */
|
|
|
|
int16_t mDonePos; /* ending position in octetbuf */
|
|
|
|
int16_t mInCRC; /* flag set when reading a CRC */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Bin Hex Header Information
|
|
|
|
binhex_header mHeader;
|
2009-08-05 11:42:52 -07:00
|
|
|
nsCString mName; /* fsspec for the output file */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
char * mDataBuffer; // temporary holding pen for the incoming data.
|
|
|
|
char * mOutgoingBuffer; // temporary holding pen for the incoming data.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mPosInDataBuffer;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-05 11:41:45 -07:00
|
|
|
unsigned char mRlebuf; /* buffer for last run length encoding value */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mCount; /* generic counter */
|
|
|
|
int16_t mMarker; /* flag indicating maker */
|
2009-08-05 11:41:45 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mPosInbuff; /* the index of the inbuff. */
|
|
|
|
int32_t mPosOutputBuff; /* the position of the out buff. */
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* nsBinHexDecoder_h__ */
|