1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999
20 * the Initial Developer. All Rights Reserved.
23 * Scott MacGregor <mscott@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 // A decoder for Mac Bin Hex 4.0.
41 // This decoder is currently only intended to be used on NON-Mac platforms. It isn't hooked up to
42 // code which would actually save the file to disk. As a result, we can't leverage the resource fork.
43 // This makes this decoder most unhelpful for the Mac. Our assumption is that if you save a bin hex file
44 // on the mac and try to open it, stuffit or some other tool is already going to be on the Mac which knows how
45 // to handle bin hex. On windows and unix, that's not the case. We need client code to strip out the data fork.
46 // So this decoder currently just strips out the data fork.
48 // Note: it's possible that we can eventually turn this decoder into both a decoder and into a file stream (much
49 // like the apple double decoder) so on the Mac, if we are saving to disk, we can invoke the decoder as a file stream
50 // and it will process the resource fork and do the right magic.
52 #ifndef nsBinHexDecoder_h__
53 #define nsBinHexDecoder_h__
55 #include "nsIStreamConverter.h"
56 #include "nsIChannel.h"
57 #include "nsIOutputStream.h"
58 #include "nsIInputStream.h"
63 #define NS_BINHEXDECODER_CID \
64 { /* 301DEA42-6850-4cda-8945-81F7DBC2186B */ \
65 0x301dea42, 0x6850, 0x4cda, \
66 { 0x89, 0x45, 0x81, 0xf7, 0xdb, 0xc2, 0x18, 0x6b } \
69 typedef struct _binhex_header
71 PRUint32 type
, creator
;
82 #define BINHEX_STATE_START 0
83 #define BINHEX_STATE_FNAME 1
84 #define BINHEX_STATE_HEADER 2
85 #define BINHEX_STATE_HCRC 3
86 #define BINHEX_STATE_DFORK 4
87 #define BINHEX_STATE_DCRC 5
88 #define BINHEX_STATE_RFORK 6
89 #define BINHEX_STATE_RCRC 7
90 #define BINHEX_STATE_FINISH 8
91 #define BINHEX_STATE_DONE 9
92 /* #define BINHEX_STATE_ERROR 10 */
94 class nsBinHexDecoder
: public nsIStreamConverter
97 // nsISupports methods
100 // nsIStreamConverter methods
101 NS_DECL_NSISTREAMCONVERTER
103 // nsIStreamListener methods
104 NS_DECL_NSISTREAMLISTENER
106 // nsIRequestObserver methods
107 NS_DECL_NSIREQUESTOBSERVER
112 virtual ~nsBinHexDecoder();
114 PRInt16
GetNextChar(PRUint32 numBytesInBuffer
);
115 nsresult
ProcessNextChunk(nsIRequest
* aRequest
, nsISupports
* aContext
, PRUint32 numBytesInBuffer
);
116 nsresult
ProcessNextState(nsIRequest
* aRequest
, nsISupports
* aContext
);
117 nsresult
SetContentType(nsIRequest
* aRequest
, const char * fileName
);
120 nsCOMPtr
<nsIStreamListener
> mNextListener
;
122 // the input and output streams form a pipe...they need to be passed around together..
123 nsCOMPtr
<nsIOutputStream
> mOutputStream
; // output stream
124 nsCOMPtr
<nsIInputStream
> mInputStream
;
126 PRInt16 mState
; /* current state */
127 PRUint16 mCRC
; /* cumulative CRC */
128 PRUint16 mFileCRC
; /* CRC value from file */
129 longbuf mOctetBuf
; /* buffer for decoded 6-bit values */
130 PRInt16 mOctetin
; /* current input position in octetbuf */
131 PRInt16 mDonePos
; /* ending position in octetbuf */
132 PRInt16 mInCRC
; /* flag set when reading a CRC */
134 // Bin Hex Header Information
135 binhex_header mHeader
;
136 char mName
[64]; /* fsspec for the output file */
138 // unfortunately we are going to need 2 8K buffers here. One for the data we are currently digesting. Another
139 // for the outgoing decoded data. I tried getting them to share a buffer but things didn't work out so nicely.
140 char * mDataBuffer
; // temporary holding pen for the incoming data.
141 char * mOutgoingBuffer
; // temporary holding pen for the incoming data.
142 PRUint32 mPosInDataBuffer
;
144 unsigned char mRlebuf
; /* buffer for last run length encoding value */
146 PRInt32 mCount
; /* generic counter */
147 PRInt16 mMarker
; /* flag indicating maker */
149 PRInt32 mPosInbuff
; /* the index of the inbuff. */
150 PRInt32 mPosOutputBuff
; /* the position of the out buff. */
153 #endif /* nsBinHexDecoder_h__ */