netwerk/streamconv/converters/nsBinHexDecoder.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/streamconv/converters/nsBinHexDecoder.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,120 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +// A decoder for Mac Bin Hex 4.0.
    1.10 +
    1.11 +// This decoder is currently only intended to be used on NON-Mac platforms. It isn't hooked up to
    1.12 +// code which would actually save the file to disk. As a result, we can't leverage the resource fork.
    1.13 +// This makes this decoder most unhelpful for the Mac. Our assumption is that if you save a bin hex file
    1.14 +// on the mac and try to open it, stuffit or some other tool is already going to be on the Mac which knows how
    1.15 +// to handle bin hex. On windows and unix, that's not the case. We need client code to strip out the data fork.
    1.16 +// So this decoder currently just strips out the data fork.
    1.17 +
    1.18 +// Note: it's possible that we can eventually turn this decoder into both a decoder and into a file stream (much
    1.19 +// like the apple double decoder) so on the Mac, if we are saving to disk, we can invoke the decoder as a file stream
    1.20 +// and it will process the resource fork and do the right magic.
    1.21 +
    1.22 +#ifndef nsBinHexDecoder_h__
    1.23 +#define nsBinHexDecoder_h__
    1.24 +
    1.25 +#include "nsIStreamConverter.h"
    1.26 +#include "nsIChannel.h"
    1.27 +#include "nsIOutputStream.h"
    1.28 +#include "nsIInputStream.h"
    1.29 +
    1.30 +#include "nsCOMPtr.h"
    1.31 +#include "nsString.h"
    1.32 +
    1.33 +#define NS_BINHEXDECODER_CID                         \
    1.34 +{ /* 301DEA42-6850-4cda-8945-81F7DBC2186B */         \
    1.35 +  0x301dea42, 0x6850, 0x4cda,                        \
    1.36 +  { 0x89, 0x45, 0x81, 0xf7, 0xdb, 0xc2, 0x18, 0x6b } \
    1.37 +}
    1.38 +
    1.39 +typedef struct _binhex_header
    1.40 +{
    1.41 +  uint32_t type, creator;
    1.42 +  uint16_t flags;
    1.43 +  int32_t dlen, rlen;
    1.44 +} binhex_header;
    1.45 +
    1.46 +typedef union
    1.47 +{
    1.48 +  unsigned char c[4];
    1.49 +  uint32_t      val;
    1.50 +} longbuf;
    1.51 +
    1.52 +#define BINHEX_STATE_START    0
    1.53 +#define BINHEX_STATE_FNAME    1
    1.54 +#define BINHEX_STATE_HEADER   2
    1.55 +#define BINHEX_STATE_HCRC     3
    1.56 +#define BINHEX_STATE_DFORK    4
    1.57 +#define BINHEX_STATE_DCRC     5
    1.58 +#define BINHEX_STATE_RFORK    6
    1.59 +#define BINHEX_STATE_RCRC     7
    1.60 +#define BINHEX_STATE_FINISH   8
    1.61 +#define BINHEX_STATE_DONE     9
    1.62 +/* #define BINHEX_STATE_ERROR  10 */
    1.63 +
    1.64 +class nsBinHexDecoder : public nsIStreamConverter
    1.65 +{
    1.66 +public:
    1.67 +  // nsISupports methods
    1.68 +  NS_DECL_ISUPPORTS
    1.69 +
    1.70 +  // nsIStreamConverter methods
    1.71 +  NS_DECL_NSISTREAMCONVERTER
    1.72 +
    1.73 +  // nsIStreamListener methods
    1.74 +  NS_DECL_NSISTREAMLISTENER
    1.75 +
    1.76 +  // nsIRequestObserver methods
    1.77 +  NS_DECL_NSIREQUESTOBSERVER
    1.78 +
    1.79 +  nsBinHexDecoder();
    1.80 +
    1.81 +protected:
    1.82 +  virtual ~nsBinHexDecoder();
    1.83 +
    1.84 +  int16_t  GetNextChar(uint32_t numBytesInBuffer);
    1.85 +  nsresult ProcessNextChunk(nsIRequest * aRequest, nsISupports * aContext, uint32_t numBytesInBuffer);
    1.86 +  nsresult ProcessNextState(nsIRequest * aRequest, nsISupports * aContext);
    1.87 +  nsresult DetectContentType(nsIRequest * aRequest, const nsAFlatCString &aFilename);
    1.88 +
    1.89 +protected:
    1.90 +  nsCOMPtr<nsIStreamListener> mNextListener;
    1.91 +
    1.92 +  // the input and output streams form a pipe...they need to be passed around together..
    1.93 +  nsCOMPtr<nsIOutputStream>     mOutputStream;     // output stream
    1.94 +  nsCOMPtr<nsIInputStream>      mInputStream;
    1.95 +
    1.96 +  int16_t   mState;      /* current state */
    1.97 +  uint16_t  mCRC;        /* cumulative CRC */
    1.98 +  uint16_t  mFileCRC;    /* CRC value from file */
    1.99 +  longbuf   mOctetBuf;   /* buffer for decoded 6-bit values     */
   1.100 +  int16_t   mOctetin;    /* current input position in octetbuf */
   1.101 +  int16_t   mDonePos;    /* ending position in octetbuf */
   1.102 +  int16_t   mInCRC;      /* flag set when reading a CRC */
   1.103 +
   1.104 +  // Bin Hex Header Information
   1.105 +  binhex_header mHeader;
   1.106 +  nsCString mName;       /* fsspec for the output file */
   1.107 +
   1.108 +  // unfortunately we are going to need 2 8K buffers here. One for the data we are currently digesting. Another
   1.109 +  // for the outgoing decoded data. I tried getting them to share a buffer but things didn't work out so nicely.
   1.110 +  char * mDataBuffer; // temporary holding pen for the incoming data.
   1.111 +  char * mOutgoingBuffer; // temporary holding pen for the incoming data.
   1.112 +  uint32_t mPosInDataBuffer;
   1.113 +
   1.114 +  unsigned char mRlebuf;  /* buffer for last run length encoding value */
   1.115 +
   1.116 +  uint32_t mCount;        /* generic counter */
   1.117 +  int16_t mMarker;        /* flag indicating maker */
   1.118 +
   1.119 +  int32_t mPosInbuff;     /* the index of the inbuff.  */
   1.120 +  int32_t mPosOutputBuff; /* the position of the out buff.    */
   1.121 +};
   1.122 +
   1.123 +#endif /* nsBinHexDecoder_h__ */

mercurial