michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // A decoder for Mac Bin Hex 4.0. michael@0: michael@0: // This decoder is currently only intended to be used on NON-Mac platforms. It isn't hooked up to michael@0: // code which would actually save the file to disk. As a result, we can't leverage the resource fork. michael@0: // This makes this decoder most unhelpful for the Mac. Our assumption is that if you save a bin hex file michael@0: // on the mac and try to open it, stuffit or some other tool is already going to be on the Mac which knows how michael@0: // to handle bin hex. On windows and unix, that's not the case. We need client code to strip out the data fork. michael@0: // So this decoder currently just strips out the data fork. michael@0: michael@0: // Note: it's possible that we can eventually turn this decoder into both a decoder and into a file stream (much michael@0: // like the apple double decoder) so on the Mac, if we are saving to disk, we can invoke the decoder as a file stream michael@0: // and it will process the resource fork and do the right magic. michael@0: michael@0: #ifndef nsBinHexDecoder_h__ michael@0: #define nsBinHexDecoder_h__ michael@0: michael@0: #include "nsIStreamConverter.h" michael@0: #include "nsIChannel.h" michael@0: #include "nsIOutputStream.h" michael@0: #include "nsIInputStream.h" michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsString.h" michael@0: michael@0: #define NS_BINHEXDECODER_CID \ michael@0: { /* 301DEA42-6850-4cda-8945-81F7DBC2186B */ \ michael@0: 0x301dea42, 0x6850, 0x4cda, \ michael@0: { 0x89, 0x45, 0x81, 0xf7, 0xdb, 0xc2, 0x18, 0x6b } \ michael@0: } michael@0: michael@0: typedef struct _binhex_header michael@0: { michael@0: uint32_t type, creator; michael@0: uint16_t flags; michael@0: int32_t dlen, rlen; michael@0: } binhex_header; michael@0: michael@0: typedef union michael@0: { michael@0: unsigned char c[4]; michael@0: uint32_t val; michael@0: } longbuf; michael@0: michael@0: #define BINHEX_STATE_START 0 michael@0: #define BINHEX_STATE_FNAME 1 michael@0: #define BINHEX_STATE_HEADER 2 michael@0: #define BINHEX_STATE_HCRC 3 michael@0: #define BINHEX_STATE_DFORK 4 michael@0: #define BINHEX_STATE_DCRC 5 michael@0: #define BINHEX_STATE_RFORK 6 michael@0: #define BINHEX_STATE_RCRC 7 michael@0: #define BINHEX_STATE_FINISH 8 michael@0: #define BINHEX_STATE_DONE 9 michael@0: /* #define BINHEX_STATE_ERROR 10 */ michael@0: michael@0: class nsBinHexDecoder : public nsIStreamConverter michael@0: { michael@0: public: michael@0: // nsISupports methods michael@0: NS_DECL_ISUPPORTS michael@0: michael@0: // nsIStreamConverter methods michael@0: NS_DECL_NSISTREAMCONVERTER michael@0: michael@0: // nsIStreamListener methods michael@0: NS_DECL_NSISTREAMLISTENER michael@0: michael@0: // nsIRequestObserver methods michael@0: NS_DECL_NSIREQUESTOBSERVER michael@0: michael@0: nsBinHexDecoder(); michael@0: michael@0: protected: michael@0: virtual ~nsBinHexDecoder(); michael@0: michael@0: int16_t GetNextChar(uint32_t numBytesInBuffer); michael@0: nsresult ProcessNextChunk(nsIRequest * aRequest, nsISupports * aContext, uint32_t numBytesInBuffer); michael@0: nsresult ProcessNextState(nsIRequest * aRequest, nsISupports * aContext); michael@0: nsresult DetectContentType(nsIRequest * aRequest, const nsAFlatCString &aFilename); michael@0: michael@0: protected: michael@0: nsCOMPtr mNextListener; michael@0: michael@0: // the input and output streams form a pipe...they need to be passed around together.. michael@0: nsCOMPtr mOutputStream; // output stream michael@0: nsCOMPtr mInputStream; michael@0: michael@0: int16_t mState; /* current state */ michael@0: uint16_t mCRC; /* cumulative CRC */ michael@0: uint16_t mFileCRC; /* CRC value from file */ michael@0: longbuf mOctetBuf; /* buffer for decoded 6-bit values */ michael@0: int16_t mOctetin; /* current input position in octetbuf */ michael@0: int16_t mDonePos; /* ending position in octetbuf */ michael@0: int16_t mInCRC; /* flag set when reading a CRC */ michael@0: michael@0: // Bin Hex Header Information michael@0: binhex_header mHeader; michael@0: nsCString mName; /* fsspec for the output file */ michael@0: michael@0: // unfortunately we are going to need 2 8K buffers here. One for the data we are currently digesting. Another michael@0: // for the outgoing decoded data. I tried getting them to share a buffer but things didn't work out so nicely. michael@0: char * mDataBuffer; // temporary holding pen for the incoming data. michael@0: char * mOutgoingBuffer; // temporary holding pen for the incoming data. michael@0: uint32_t mPosInDataBuffer; michael@0: michael@0: unsigned char mRlebuf; /* buffer for last run length encoding value */ michael@0: michael@0: uint32_t mCount; /* generic counter */ michael@0: int16_t mMarker; /* flag indicating maker */ michael@0: michael@0: int32_t mPosInbuff; /* the index of the inbuff. */ michael@0: int32_t mPosOutputBuff; /* the position of the out buff. */ michael@0: }; michael@0: michael@0: #endif /* nsBinHexDecoder_h__ */