public X509Crl ReadCrl(Stream inStream) { X509Crl crl; if (inStream == null) { throw new ArgumentNullException("inStream"); } if (!inStream.CanRead) { throw new ArgumentException("inStream must be read-able", "inStream"); } if (this.currentCrlStream == null) { this.currentCrlStream = inStream; this.sCrlData = null; this.sCrlDataObjectCount = 0; } else if (this.currentCrlStream != inStream) { this.currentCrlStream = inStream; this.sCrlData = null; this.sCrlDataObjectCount = 0; } try { if (this.sCrlData != null) { if (this.sCrlDataObjectCount != this.sCrlData.Count) { return(this.GetCrl()); } this.sCrlData = null; this.sCrlDataObjectCount = 0; return(null); } PushbackStream stream = new PushbackStream(inStream); int b = stream.ReadByte(); if (b < 0) { return(null); } stream.Unread(b); if (b != 0x30) { return(this.ReadPemCrl(stream)); } Asn1InputStream dIn = !this.lazyAsn1 ? new Asn1InputStream(stream) : new LazyAsn1InputStream(stream); crl = this.ReadDerCrl(dIn); } catch (CrlException exception) { throw exception; } catch (Exception exception2) { throw new CrlException(exception2.ToString()); } return(crl); }
/// <summary> /// Checks that the supplied InputStream (which MUST support mark and reset, or be a PushbackInputStream) has a OOXML (zip) header at the start of it. /// If your InputStream does not support mark / reset, then wrap it in a PushBackInputStream, then be sure to always use that, and not the original! /// </summary> /// <param name="inp">An InputStream which supports either mark/reset, or is a PushbackInputStream</param> /// <returns></returns> public static bool HasOOXMLHeader(Stream inp) { // We want to peek at the first 4 bytes //inp.mark(4); byte[] header = new byte[4]; int bytesRead = IOUtils.ReadFully(inp, header); // Wind back those 4 bytes if (inp is PushbackStream) { PushbackStream pin = (PushbackStream)inp; pin.Position = pin.Position - 4; //pin.unread(header, 0, bytesRead); } else { inp.Position = 0; } // Did it match the ooxml zip signature? return( bytesRead == 4 && header[0] == POIFSConstants.OOXML_FILE_HEADER[0] && header[1] == POIFSConstants.OOXML_FILE_HEADER[1] && header[2] == POIFSConstants.OOXML_FILE_HEADER[2] && header[3] == POIFSConstants.OOXML_FILE_HEADER[3] ); }
/// <summary> /// Creates a new stream instance using the provided stream as a source. /// Will also read the first frame of the MP3 into the internal buffer. /// TODO: allow selecting stereo or mono in the constructor (note that this also requires "implementing" the stereo format). /// UPDATE: (Giperion) I hate that TODO above /// </summary> public MP3Stream(Stream sourceStream, int chunkSize, bool IsMono) { IsEOF = false; m_SourceStream = sourceStream; pushback = new PushbackStream(m_SourceStream, chunkSize); m_BitStream = new Bitstream(pushback); if (IsMono) { Decoder.Params ParamsCustom = new Decoder.Params(); ParamsCustom.OutputChannels = OutputChannels.LEFT; m_Decoder = new Decoder(ParamsCustom); m_Buffer = new Buffer16BitMono(); FormatRep = SoundFormat.Pcm16BitMono; m_ChannelCountRep = 1; } else { m_Buffer = new Buffer16BitStereo(); FormatRep = SoundFormat.Pcm16BitStereo; m_ChannelCountRep = 2; } m_Decoder.OutputBuffer = m_Buffer; // read the first frame. This will fill the initial buffer with data, and get our frequency! if (!ReadFrame()) { IsEOF = true; } }
public X509CertificatePair ReadCertPair(Stream inStream) { if (inStream == null) { throw new ArgumentNullException("inStream"); } if (!inStream.CanRead) { throw new ArgumentException("inStream must be read-able", "inStream"); } if (currentStream == null) { currentStream = inStream; } else if (currentStream != inStream) { currentStream = inStream; } try { PushbackStream pushbackStream = new PushbackStream(inStream); int num = pushbackStream.ReadByte(); if (num < 0) { return(null); } pushbackStream.Unread(num); return(ReadDerCrossCertificatePair(pushbackStream)); } catch (Exception ex) { throw new CertificateException(ex.ToString()); } }
public void TestDetectAsPOIFS() { Stream in1; // ooxml file is in1 = new PushbackStream( HSSFTestDataSamples.OpenSampleFileStream("SampleSS.xlsx") ); Assert.IsTrue(POIXMLDocument.HasOOXMLHeader(in1)); in1.Dispose(); // xls file isn't in1 = new PushbackStream( HSSFTestDataSamples.OpenSampleFileStream("SampleSS.xls") ); Assert.IsFalse(POIXMLDocument.HasOOXMLHeader(in1)); in1.Dispose(); // text file isn't in1 = new PushbackStream( HSSFTestDataSamples.OpenSampleFileStream("SampleSS.txt") ); Assert.IsFalse(POIXMLDocument.HasOOXMLHeader(in1)); in1.Dispose(); }
public X509Crl ReadCrl(Stream inStream) { //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_0020: Unknown result type (might be due to invalid IL or missing references) if (inStream == null) { throw new ArgumentNullException("inStream"); } if (!inStream.get_CanRead()) { throw new ArgumentException("inStream must be read-able", "inStream"); } if (currentCrlStream == null) { currentCrlStream = inStream; sCrlData = null; sCrlDataObjectCount = 0; } else if (currentCrlStream != inStream) { currentCrlStream = inStream; sCrlData = null; sCrlDataObjectCount = 0; } try { if (sCrlData != null) { if (sCrlDataObjectCount != sCrlData.Count) { return(GetCrl()); } sCrlData = null; sCrlDataObjectCount = 0; return(null); } PushbackStream pushbackStream = new PushbackStream(inStream); int num = ((Stream)pushbackStream).ReadByte(); if (num < 0) { return(null); } pushbackStream.Unread(num); if (num != 48) { return(ReadPemCrl((Stream)(object)pushbackStream)); } Asn1InputStream dIn = (lazyAsn1 ? new LazyAsn1InputStream((Stream)(object)pushbackStream) : new Asn1InputStream((Stream)(object)pushbackStream)); return(ReadDerCrl(dIn)); } catch (CrlException ex) { throw ex; } catch (global::System.Exception ex2) { throw new CrlException(ex2.ToString()); } }
public X509Certificate ReadCertificate(Stream inStream) { if (inStream == null) { throw new ArgumentNullException("inStream"); } if (inStream.CanRead) { if (currentStream == null) { currentStream = inStream; sData = null; sDataObjectCount = 0; } else if (currentStream != inStream) { currentStream = inStream; sData = null; sDataObjectCount = 0; } try { if (sData != null) { if (sDataObjectCount != sData.Count) { return(GetCertificate()); } sData = null; sDataObjectCount = 0; return(null); } PushbackStream pushbackStream = new PushbackStream(inStream); int num = pushbackStream.ReadByte(); if (num < 0) { return(null); } pushbackStream.Unread(num); if (num != 48) { return(ReadPemCertificate(pushbackStream)); } return(ReadDerCertificate(new Asn1InputStream(pushbackStream))); IL_00fe: X509Certificate result; return(result); } catch (Exception exception) { throw new CertificateException("Failed to read certificate", exception); IL_0110: X509Certificate result; return(result); } } throw new ArgumentException("inStream must be read-able", "inStream"); }
public X509Crl ReadCrl(Stream inStream) { if (inStream == null) { throw new ArgumentNullException("inStream"); } if (!inStream.CanRead) { throw new ArgumentException("inStream must be read-able", "inStream"); } if (currentCrlStream == null) { currentCrlStream = inStream; sCrlData = null; sCrlDataObjectCount = 0; } else if (currentCrlStream != inStream) { currentCrlStream = inStream; sCrlData = null; sCrlDataObjectCount = 0; } try { if (sCrlData != null) { if (sCrlDataObjectCount != sCrlData.Count) { return(GetCrl()); } sCrlData = null; sCrlDataObjectCount = 0; return(null); } PushbackStream pushbackStream = new PushbackStream(inStream); int num = pushbackStream.ReadByte(); if (num < 0) { return(null); } pushbackStream.Unread(num); if (num != 48) { return(ReadPemCrl(pushbackStream)); } Asn1InputStream dIn = lazyAsn1 ? new LazyAsn1InputStream(pushbackStream) : new Asn1InputStream(pushbackStream); return(ReadDerCrl(dIn)); } catch (CrlException ex) { throw ex; } catch (Exception ex2) { throw new CrlException(ex2.ToString()); } }
public X509Certificate ReadCertificate(Stream inStream) { X509Certificate certificate; if (inStream == null) { throw new ArgumentNullException("inStream"); } if (!inStream.CanRead) { throw new ArgumentException("inStream must be read-able", "inStream"); } if (this.currentStream == null) { this.currentStream = inStream; this.sData = null; this.sDataObjectCount = 0; } else if (this.currentStream != inStream) { this.currentStream = inStream; this.sData = null; this.sDataObjectCount = 0; } try { if (this.sData != null) { if (this.sDataObjectCount != this.sData.Count) { return(this.GetCertificate()); } this.sData = null; this.sDataObjectCount = 0; return(null); } PushbackStream stream = new PushbackStream(inStream); int b = stream.ReadByte(); if (b < 0) { return(null); } stream.Unread(b); if (b != 0x30) { return(this.ReadPemCertificate(stream)); } certificate = this.ReadDerCertificate(new Asn1InputStream(stream)); } catch (Exception exception) { throw new CertificateException("Failed to read certificate", exception); } return(certificate); }
/** * Generates a certificate object and initializes it with the data * read from the input stream inStream. */ public IX509AttributeCertificate ReadAttrCert( Stream inStream) { if (inStream == null) throw new ArgumentNullException("inStream"); if (!inStream.CanRead) throw new ArgumentException("inStream must be read-able", "inStream"); if (currentStream == null) { currentStream = inStream; sData = null; sDataObjectCount = 0; } else if (currentStream != inStream) // reset if input stream has changed { currentStream = inStream; sData = null; sDataObjectCount = 0; } try { if (sData != null) { if (sDataObjectCount != sData.Count) { return GetCertificate(); } sData = null; sDataObjectCount = 0; return null; } PushbackStream pis = new PushbackStream(inStream); int tag = pis.ReadByte(); if (tag < 0) return null; pis.Unread(tag); if (tag != 0x30) // assume ascii PEM encoded. { return ReadPemCertificate(pis); } return ReadDerCertificate(new Asn1InputStream(pis)); } catch (Exception e) { throw new CertificateException(e.ToString()); } }
public X509Certificate ReadCertificate(Stream inStream) { //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_0020: Unknown result type (might be due to invalid IL or missing references) if (inStream == null) { throw new ArgumentNullException("inStream"); } if (!inStream.get_CanRead()) { throw new ArgumentException("inStream must be read-able", "inStream"); } if (currentStream == null) { currentStream = inStream; sData = null; sDataObjectCount = 0; } else if (currentStream != inStream) { currentStream = inStream; sData = null; sDataObjectCount = 0; } try { if (sData != null) { if (sDataObjectCount != sData.Count) { return(GetCertificate()); } sData = null; sDataObjectCount = 0; return(null); } PushbackStream pushbackStream = new PushbackStream(inStream); int num = ((Stream)pushbackStream).ReadByte(); if (num < 0) { return(null); } pushbackStream.Unread(num); if (num != 48) { return(ReadPemCertificate((Stream)(object)pushbackStream)); } return(ReadDerCertificate(new Asn1InputStream((Stream)(object)pushbackStream))); } catch (global::System.Exception exception) { throw new CertificateException("Failed to read certificate", exception); } }
public IX509AttributeCertificate ReadAttrCert(Stream inStream) { if (inStream == null) { throw new ArgumentNullException("inStream"); } if (!inStream.CanRead) { throw new ArgumentException("inStream must be read-able", "inStream"); } if (currentStream == null) { currentStream = inStream; sData = null; sDataObjectCount = 0; } else if (currentStream != inStream) { currentStream = inStream; sData = null; sDataObjectCount = 0; } try { if (sData != null) { if (sDataObjectCount != sData.Count) { return(GetCertificate()); } sData = null; sDataObjectCount = 0; return(null); } PushbackStream pushbackStream = new PushbackStream(inStream); int num = pushbackStream.ReadByte(); if (num < 0) { return(null); } pushbackStream.Unread(num); if (num != 48) { return(ReadPemCertificate(pushbackStream)); } return(ReadDerCertificate(new Asn1InputStream(pushbackStream))); } catch (Exception ex) { throw new CertificateException(ex.ToString()); } }
/// <summary> /// Creates the appropriate HSSFWorkbook / XSSFWorkbook from /// the given InputStream. The Stream is wraped inside a PushbackInputStream. /// </summary> /// <param name="inputStream">Input Stream of .xls or .xlsx file</param> /// <param name="password"></param> /// <returns>IWorkbook depending on the input HSSFWorkbook or XSSFWorkbook is returned.</returns> // Your input stream MUST either support mark/reset, or // be wrapped as a {@link PushbackInputStream}! public static IWorkbook Create(Stream inputStream, bool bReadonly) { inputStream = new PushbackStream(inputStream); if (POIFSFileSystem.HasPOIFSHeader(inputStream)) { return(new HSSFWorkbook(inputStream)); } inputStream.Position = 0; if (DocumentFactoryHelper.HasOOXMLHeader(inputStream)) { return(new XSSFWorkbook(OPCPackage.Open(inputStream, bReadonly))); } throw new ArgumentException("Your stream was neither an OLE2 stream, nor an OOXML stream."); }
/// <summary> /// Creates a new stream instance using the provided stream as a source. /// Will also read the first frame of the MP3 into the internal buffer. /// TODO: allow selecting stereo or mono in the constructor (note that this also requires "implementing" the stereo format). /// </summary> public MP3Stream(Stream sourceStream, int chunkSize) { IsEOF = false; FormatRep = SoundFormat.Pcm16BitStereo; m_SourceStream = sourceStream; pushback = new PushbackStream(m_SourceStream, chunkSize); m_BitStream = new Bitstream(pushback); m_Buffer = new Buffer16BitStereo(); m_Decoder.OutputBuffer = m_Buffer; // read the first frame. This will fill the initial buffer with data, and get our frequency! if (!ReadFrame()) { IsEOF = true; } }
/// <summary> /// Creates the appropriate HSSFWorkbook / XSSFWorkbook from the given InputStream. The Stream is wraped inside a PushbackInputStream. /// </summary> /// <param name="inp"></param> /// <returns></returns> public static IWorkbook Create(Stream inp) { Stream s = new PushbackStream(inp); if (POIFSFileSystem.HasPOIFSHeader(s)) { return(new HSSFWorkbook(s)); } s.Position = 0; if (POIXMLDocument.HasOOXMLHeader(s)) { return(new XSSFWorkbook(OPCPackage.Open(s))); } throw new ArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream."); }
/// <summary> /// // TIMING DEBUG INFO /// </summary> /// <summary> /// private long endTime = 0; /// </summary> /// <summary> /// private Date endDate = null; /// </summary> /// <summary> /// private long endFree = 0; /// </summary> /// <summary> /// private DecimalFormat df = new DecimalFormat("#,##0"); /// </summary> /// <summary> /// private Date startDate = new Date(); /// </summary> /// <summary> /// private long startTime = System.CurrentTimeMillis(); /// </summary> /// <summary> /// private long startFree = Runtime.GetRuntime().FreeMemory(); /// </summary> /// <summary> /// Constructor /// </summary> /// <param name="rtfParser">The parser object this manager works with.</param> /// <param name="reader">the PushbackReader from the tokeniser.</param> public RtfCtrlWordMgr(RtfParser rtfParser, PushbackStream reader) { _rtfParser = rtfParser; // set the parser _reader = reader; // set the reader value _ctrlWordMap = new RtfCtrlWordMap(rtfParser); // // TIMING DEBUG INFO // endFree = Runtime.GetRuntime().FreeMemory(); // endTime = System.CurrentTimeMillis(); // endDate = new Date(); // Console.Out.WriteLine("RtfCtrlWordMgr start date: " + startDate.ToLocaleString()); // Console.Out.WriteLine("RtfCtrlWordMgr end date : " + endDate.ToLocaleString()); // Console.Out.WriteLine(" Elapsed time : " + Long.ToString(endTime - startTime) + " milliseconds."); // Console.Out.WriteLine("Begin Constructor RtfCtrlWordMgr , free mem is " + df.Format(startFree / 1024) + "k"); // Console.Out.WriteLine("End Constructor RtfCtrlWordMgr , free mem is " + df.Format(endFree / 1024) + "k"); // Console.Out.WriteLine("RtfCtrlWordMgr used approximately " + df.Format((startFree - endFree) / 1024) + "k"); }
public override InStream unread(int n) { try { // don't take the hit until we know we need to wrap // the raw input stream with a pushback stream if (!(inStream is PushbackStream)) { inStream = new PushbackStream(inStream, 128); } ((PushbackStream)inStream).Unread(n); return(this); } catch (IOException e) { throw IOErr.make(e).val; } }
/// <summary> /// Creates the appropriate HSSFWorkbook / XSSFWorkbook from /// the given InputStream. The Stream is wraped inside a PushbackInputStream. /// </summary> /// <param name="inputStream">Input Stream of .xls or .xlsx file</param> /// <returns>IWorkbook depending on the input HSSFWorkbook or XSSFWorkbook is returned.</returns> // Your input stream MUST either support mark/reset, or // be wrapped as a {@link PushbackInputStream}! public static IWorkbook Create(Stream inputStream) { // If Clearly doesn't do mark/reset, wrap up //if (!inp.MarkSupported()) //{ // inp = new PushbackInputStream(inp, 8); //} inputStream = new PushbackStream(inputStream); if (POIFSFileSystem.HasPOIFSHeader(inputStream)) { return(new HSSFWorkbook(inputStream)); } inputStream.Position = 0; if (POIXMLDocument.HasOOXMLHeader(inputStream)) { return(new XSSFWorkbook(OPCPackage.Open(inputStream))); } throw new ArgumentException("Your stream was neither an OLE2 stream, nor an OOXML stream."); }
/** * Takens an InputStream, verifies that it's not RTF, builds a * POIFSFileSystem from it, and returns that. */ public static POIFSFileSystem VerifyAndBuildPOIFS(Stream istream) { // Open a PushbackInputStream, so we can peek at the first few bytes PushbackStream pis = new PushbackStream(istream); byte[] first6 = new byte[6]; pis.Read(first6, 0, 6); // Does it start with {\rtf ? If so, it's really RTF if (first6[0] == '{' && first6[1] == '\\' && first6[2] == 'r' && first6[3] == 't' && first6[4] == 'f') { throw new ArgumentException("The document is really a RTF file"); } // OK, so it's not RTF // Open a POIFSFileSystem on the (pushed back) stream pis.Unread(6); return(new POIFSFileSystem(istream)); }
public X509CertificatePair ReadCertPair( Stream inStream) { if (inStream == null) { throw new ArgumentNullException("inStream"); } if (!inStream.CanRead) { throw new ArgumentException("inStream must be read-able", "inStream"); } if (currentStream == null) { currentStream = inStream; } else if (currentStream != inStream) // reset if input stream has changed { currentStream = inStream; } try { PushbackStream pis = new PushbackStream(inStream); int tag = pis.ReadByte(); if (tag < 0) { return(null); } pis.Unread(tag); return(ReadDerCrossCertificatePair(pis)); } catch (Exception e) { throw new CertificateException(e.ToString()); } }
public X509CertificatePair ReadCertPair(Stream inStream) { //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_0020: Unknown result type (might be due to invalid IL or missing references) if (inStream == null) { throw new ArgumentNullException("inStream"); } if (!inStream.get_CanRead()) { throw new ArgumentException("inStream must be read-able", "inStream"); } if (currentStream == null) { currentStream = inStream; } else if (currentStream != inStream) { currentStream = inStream; } try { PushbackStream pushbackStream = new PushbackStream(inStream); int num = ((Stream)pushbackStream).ReadByte(); if (num < 0) { return(null); } pushbackStream.Unread(num); return(ReadDerCrossCertificatePair((Stream)(object)pushbackStream)); } catch (global::System.Exception ex) { throw new CertificateException(ex.ToString()); } }
/// <summary> /// Creates the appropriate HSSFWorkbook / XSSFWorkbook from /// the given InputStream. The Stream is wraped inside a PushbackInputStream. /// </summary> /// <param name="inputStream">Input Stream of .xls or .xlsx file</param> /// <param name="password"></param> /// <returns>IWorkbook depending on the input HSSFWorkbook or XSSFWorkbook is returned.</returns> // Your input stream MUST either support mark/reset, or // be wrapped as a {@link PushbackInputStream}! public static IWorkbook Create(Stream inputStream, string password) { // If Clearly doesn't do mark/reset, wrap up //if (!inp.MarkSupported()) //{ // inp = new PushbackInputStream(inp, 8); //} inputStream = new PushbackStream(inputStream); // Ensure that there is at least some data there //byte[] header8 = IOUtils.PeekFirst8Bytes(inputStream); if (POIFSFileSystem.HasPOIFSHeader(inputStream)) { //NPOIFSFileSystem fs = new NPOIFSFileSystem(inputStream); //return Create(fs, password); return(new HSSFWorkbook(inputStream)); } inputStream.Position = 0; if (DocumentFactoryHelper.HasOOXMLHeader(inputStream)) { return(new XSSFWorkbook(OPCPackage.Open(inputStream))); } throw new InvalidFormatException("Your stream was neither an OLE2 stream, nor an OOXML stream."); }
/** * Parses a keyword and it's parameter if one exists * @param reader * This is a pushback reader for file input. * @return * Returns an error code or errOK if no error. * @throws IOException * Catch any file read problem. */ private int ParseCtrlWord(PushbackStream reader) { int nextChar = 0; int result = errOK; if((nextChar = reader.ReadByte()) == -1) { return errEndOfFile; } this.byteCount++; StringBuilder parsedCtrlWord = new StringBuilder(); StringBuilder parsedParam= new StringBuilder(); RtfCtrlWordData ctrlWordParam = new RtfCtrlWordData(); if (!Char.IsLetterOrDigit((char)nextChar)) { parsedCtrlWord.Append((char)nextChar); ctrlWordParam.ctrlWord = parsedCtrlWord.ToString(); result = this.HandleCtrlWord(ctrlWordParam); lastCtrlWordParam = ctrlWordParam; return result; } // for ( ; Character.IsLetter(nextChar[0]); reader.Read(nextChar) ) { // parsedCtrlWord.Append(nextChar[0]); // } do { parsedCtrlWord.Append((char)nextChar); //TODO: catch EOF nextChar = reader.ReadByte(); this.byteCount++; } while (Char.IsLetter((char)nextChar)); ctrlWordParam.ctrlWord = parsedCtrlWord.ToString(); if ((char)nextChar == '-') { ctrlWordParam.isNeg = true; if((nextChar = reader.ReadByte()) == -1) { return errEndOfFile; } this.byteCount++; } if (Char.IsDigit((char)nextChar)) { ctrlWordParam.hasParam = true; // for ( ; Character.IsDigit(nextChar[0]); reader.Read(nextChar) ) { // parsedParam.Append(nextChar[0]); // } do { parsedParam.Append((char)nextChar); //TODO: catch EOF nextChar = reader.ReadByte(); this.byteCount++; } while (Char.IsDigit((char)nextChar)); ctrlWordParam.param = parsedParam.ToString(); } // push this character back into the stream if ((char)nextChar != ' ') { // || this.IsImport() ) { reader.Unread(nextChar); } if (debugParser) { // // debug: insrsid6254399 // if (ctrlWordParam.ctrlWord.Equals("proptype") && ctrlWordParam.param.Equals("30")) { // System.out.Print("Debug value found\n"); // } // if (ctrlWordParam.ctrlWord.Equals("panose") ) { // System.out.Print("Debug value found\n"); // } } result = this.HandleCtrlWord(ctrlWordParam); lastCtrlWordParam = ctrlWordParam; return result; }
/** * Generates a certificate revocation list (CRL) object and initializes * it with the data read from the input stream inStream. */ public X509Crl ReadCrl( Stream inStream) { if (inStream == null) { throw new ArgumentNullException("inStream"); } if (!inStream.CanRead) { throw new ArgumentException("inStream must be read-able", "inStream"); } if (currentCrlStream == null) { currentCrlStream = inStream; sCrlData = null; sCrlDataObjectCount = 0; } else if (currentCrlStream != inStream) // reset if input stream has changed { currentCrlStream = inStream; sCrlData = null; sCrlDataObjectCount = 0; } try { if (sCrlData != null) { if (sCrlDataObjectCount != sCrlData.Count) { return(GetCrl()); } sCrlData = null; sCrlDataObjectCount = 0; return(null); } PushbackStream pis = new PushbackStream(inStream); int tag = pis.ReadByte(); if (tag < 0) { return(null); } pis.Unread(tag); if (tag != 0x30) // assume ascii PEM encoded. { return(ReadPemCrl(pis)); } Asn1InputStream asn1 = lazyAsn1 ? new LazyAsn1InputStream(pis) : new Asn1InputStream(pis); return(ReadDerCrl(asn1)); } catch (CrlException e) { throw e; } catch (Exception e) { throw new CrlException(e.ToString()); } }
public Org.BouncyCastle.X509.X509Certificate ReadCertificate( Stream inStream) { if (inStream == null) { throw new ArgumentNullException("inStream"); } if (!inStream.CanRead) { throw new ArgumentException("inStream must be read-able", "inStream"); } if (currentStream == null) { currentStream = inStream; sData = null; sDataObjectCount = 0; } else if (currentStream != inStream) // reset if input stream has changed { currentStream = inStream; sData = null; sDataObjectCount = 0; } try { if (sData != null) { if (sDataObjectCount != sData.Count) { return(GetCertificate()); } sData = null; sDataObjectCount = 0; return(null); } var pis = new PushbackStream(inStream); int tag = pis.ReadByte(); if (tag < 0) { return(null); } pis.Unread(tag); if (tag != 0x30) // assume ascii PEM encoded. { return(ReadPemCertificate(pis)); } return(ReadDerCertificate(new Asn1InputStream(pis))); } catch (Exception e) { throw new CertificateException("Failed to read certificate", e); } }
public X509Certificate ReadCertificate(Stream inStream) { if (inStream == null) { throw new ArgumentNullException("inStream"); } if (!inStream.CanRead) { throw new ArgumentException("inStream must be read-able", "inStream"); } if (this.currentStream == null) { this.currentStream = inStream; this.sData = null; this.sDataObjectCount = 0; } else if (this.currentStream != inStream) { this.currentStream = inStream; this.sData = null; this.sDataObjectCount = 0; } X509Certificate result; try { if (this.sData != null) { if (this.sDataObjectCount != this.sData.Count) { result = this.GetCertificate(); } else { this.sData = null; this.sDataObjectCount = 0; result = null; } } else { PushbackStream pushbackStream = new PushbackStream(inStream); int num = pushbackStream.ReadByte(); if (num < 0) { result = null; } else { pushbackStream.Unread(num); if (num != 48) { result = this.ReadPemCertificate(pushbackStream); } else { result = this.ReadDerCertificate(new Asn1InputStream(pushbackStream)); } } } } catch (Exception exception) { throw new CertificateException("Failed to read certificate", exception); } return(result); }
/** * This will parse the stream and create a cmap object. * * @param input The CMAP stream to parse. * @return The parsed stream as a java object. * * @throws IOException If there is an error parsing the stream. */ public CMap Parse(Stream input) { PushbackStream cmapStream = new PushbackStream(input); CMap result = new CMap(); Object token = null; while ((token = ParseNextToken(cmapStream)) != null) { if (token is Operator) { Operator op = (Operator)token; if (op.op.Equals(BEGIN_CODESPACE_RANGE)) { while (true) { Object nx = ParseNextToken(cmapStream); if (nx is Operator && ((Operator)nx).op.Equals("endcodespacerange")) { break; } byte[] startRange = (byte[])nx; byte[] endRange = (byte[])ParseNextToken(cmapStream); CodespaceRange range = new CodespaceRange(); range.SetStart(startRange); range.SetEnd(endRange); result.AddCodespaceRange(range); } } else if (op.op.Equals(BEGIN_BASE_FONT_CHAR)) { while (true) { Object nx = ParseNextToken(cmapStream); if (nx is Operator && ((Operator)nx).op.Equals("endbfchar")) { break; } byte[] inputCode = (byte[])nx; Object nextToken = ParseNextToken(cmapStream); if (nextToken is byte[]) { byte[] bytes = (byte[])nextToken; String value = CreateStringFromBytes(bytes); result.AddMapping(inputCode, value); } else if (nextToken is LiteralName) { result.AddMapping(inputCode, ((LiteralName)nextToken).name); } else { throw new IOException(MessageLocalization.GetComposedMessage("error.parsing.cmap.beginbfchar.expected.cosstring.or.cosname.and.not.1", nextToken)); } } } else if (op.op.Equals(BEGIN_BASE_FONT_RANGE)) { while (true) { Object nx = ParseNextToken(cmapStream); if (nx is Operator && ((Operator)nx).op.Equals("endbfrange")) { break; } byte[] startCode = (byte[])nx; byte[] endCode = (byte[])ParseNextToken(cmapStream); Object nextToken = ParseNextToken(cmapStream); IList <byte[]> array = null; byte[] tokenBytes = null; if (nextToken is IList <byte[]> ) { array = (IList <byte[]>)nextToken; tokenBytes = array[0]; } else { tokenBytes = (byte[])nextToken; } String value = null; int arrayIndex = 0; bool done = false; while (!done) { if (Compare(startCode, endCode) >= 0) { done = true; } value = CreateStringFromBytes(tokenBytes); result.AddMapping(startCode, value); Increment(startCode); if (array == null) { Increment(tokenBytes); } else { arrayIndex++; if (arrayIndex < array.Count) { tokenBytes = array[arrayIndex]; } } } } } } } return(result); }
private Object ParseNextToken(PushbackStream pis) { Object retval = null; int nextByte = pis.ReadByte(); //skip whitespace while (nextByte == 0x09 || nextByte == 0x20 || nextByte == 0x0D || nextByte == 0x0A) { nextByte = pis.ReadByte(); } switch (nextByte) { case '%': { //header operations, for now return the entire line //may need to smarter in the future StringBuilder buffer = new StringBuilder(); buffer.Append((char)nextByte); ReadUntilEndOfLine(pis, buffer); retval = buffer.ToString(); break; } case '(': { StringBuilder buffer = new StringBuilder(); int stringByte = pis.ReadByte(); while (stringByte != -1 && stringByte != ')') { buffer.Append((char)stringByte); stringByte = pis.ReadByte(); } retval = buffer.ToString(); break; } case '>': { int secondCloseBrace = pis.ReadByte(); if (secondCloseBrace == '>') { retval = MARK_END_OF_DICTIONARY; } else { throw new IOException(MessageLocalization.GetComposedMessage("error.expected.the.end.of.a.dictionary")); } break; } case ']': { retval = MARK_END_OF_ARRAY; break; } case '[': { IList <byte[]> list = new List <byte[]>(); Object nextToken = ParseNextToken(pis); while (!MARK_END_OF_ARRAY.Equals(nextToken)) { list.Add((byte[])nextToken); nextToken = ParseNextToken(pis); } retval = list; break; } case '<': { int theNextByte = pis.ReadByte(); if (theNextByte == '<') { IDictionary <String, Object> result = new Dictionary <String, Object>(); //we are reading a dictionary Object key = ParseNextToken(pis); while (key is LiteralName && !MARK_END_OF_DICTIONARY.Equals(key)) { Object value = ParseNextToken(pis); result[((LiteralName)key).name] = value; key = ParseNextToken(pis); } retval = result; } else { //won't read more than 512 bytes int multiplyer = 16; int bufferIndex = -1; while (theNextByte != -1 && theNextByte != '>') { int intValue = 0; if (theNextByte >= '0' && theNextByte <= '9') { intValue = theNextByte - '0'; } else if (theNextByte >= 'A' && theNextByte <= 'F') { intValue = 10 + theNextByte - 'A'; } else if (theNextByte >= 'a' && theNextByte <= 'f') { intValue = 10 + theNextByte - 'a'; } else if (theNextByte == 0x20 || theNextByte == 0x09) { // skipping whitespaces - from pdf's generated by Mac osx theNextByte = pis.ReadByte(); continue; } else { throw new IOException(MessageLocalization.GetComposedMessage("error.expected.hex.character.and.not.char.thenextbyte.1", theNextByte)); } intValue *= multiplyer; if (multiplyer == 16) { bufferIndex++; tokenParserByteBuffer[bufferIndex] = 0; multiplyer = 1; } else { multiplyer = 16; } tokenParserByteBuffer[bufferIndex] += (byte)intValue; theNextByte = pis.ReadByte(); } byte[] finalResult = new byte[bufferIndex + 1]; System.Array.Copy(tokenParserByteBuffer, 0, finalResult, 0, bufferIndex + 1); retval = finalResult; } break; } case '/': { StringBuilder buffer = new StringBuilder(); int stringByte = pis.ReadByte(); while (!IsWhitespaceOrEOF(stringByte)) { buffer.Append((char)stringByte); stringByte = pis.ReadByte(); } retval = new LiteralName(buffer.ToString()); break; } case -1: { //EOF return null; break; } case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { StringBuilder buffer = new StringBuilder(); buffer.Append((char)nextByte); nextByte = pis.ReadByte(); while (!IsWhitespaceOrEOF(nextByte) && (Char.IsDigit((char)nextByte) || nextByte == '.')) { buffer.Append((char)nextByte); nextByte = pis.ReadByte(); } pis.Unread(nextByte); String value = buffer.ToString(); if (value.IndexOf('.') >= 0) { retval = double.Parse(value, CultureInfo.InvariantCulture); } else { retval = int.Parse(value, CultureInfo.InvariantCulture); } break; } default: { StringBuilder buffer = new StringBuilder(); buffer.Append((char)nextByte); nextByte = pis.ReadByte(); while (!IsWhitespaceOrEOF(nextByte)) { buffer.Append((char)nextByte); nextByte = pis.ReadByte(); } retval = new Operator(buffer.ToString()); break; } } return(retval); }
public X509Crl ReadCrl(Stream inStream) { if (inStream == null) { throw new ArgumentNullException("inStream"); } if (!inStream.CanRead) { throw new ArgumentException("inStream must be read-able", "inStream"); } if (this.currentCrlStream == null) { this.currentCrlStream = inStream; this.sCrlData = null; this.sCrlDataObjectCount = 0; } else if (this.currentCrlStream != inStream) { this.currentCrlStream = inStream; this.sCrlData = null; this.sCrlDataObjectCount = 0; } X509Crl result; try { if (this.sCrlData != null) { if (this.sCrlDataObjectCount != this.sCrlData.Count) { result = this.GetCrl(); } else { this.sCrlData = null; this.sCrlDataObjectCount = 0; result = null; } } else { PushbackStream pushbackStream = new PushbackStream(inStream); int num = pushbackStream.ReadByte(); if (num < 0) { result = null; } else { pushbackStream.Unread(num); if (num != 48) { result = this.ReadPemCrl(pushbackStream); } else { Asn1InputStream dIn = this.lazyAsn1 ? new LazyAsn1InputStream(pushbackStream) : new Asn1InputStream(pushbackStream); result = this.ReadDerCrl(dIn); } } } } catch (CrlException ex) { throw ex; } catch (Exception ex2) { throw new CrlException(ex2.ToString()); } return(result); }
/// <summary> /// Creates the appropriate HSSFWorkbook / XSSFWorkbook from /// the given File, which must exist and be readable. /// </summary> /// <param name="file"></param> /// <param name="password"></param> /// <param name="readOnly"></param> /// <returns></returns> /// <remarks> /// Note that for Workbooks opened this way, it is not possible /// to explicitly close the underlying File resource. /// </remarks> public static IWorkbook Create(Stream inputStream, string password, bool readOnly) { inputStream = new PushbackStream(inputStream); try { if (POIFSFileSystem.HasPOIFSHeader(inputStream)) { if (DocumentFactoryHelper.GetPasswordProtected(inputStream) == DocumentFactoryHelper.OfficeProtectType.ProtectedOOXML) { inputStream.Position = 0; POIFSFileSystem fs = new POIFSFileSystem(inputStream); var decriptedStream = DocumentFactoryHelper.GetDecryptedStream(fs, password); return(new XSSFWorkbook(decriptedStream)); } inputStream.Position = 0; NPOIFSFileSystem nfs = new NPOIFSFileSystem(inputStream); try { return(Create(nfs, password)); } finally { // ensure that the file-handle is closed again if (nfs != null) { nfs.Close(); } } } inputStream.Position = 0; if (DocumentFactoryHelper.HasOOXMLHeader(inputStream)) { inputStream.Position = 0; OPCPackage pkg = OPCPackage.Open(inputStream, readOnly); try { return(new XSSFWorkbook(pkg)); } catch (IOException ioe) { // ensure that file handles are closed (use revert() to not re-write the file) pkg.Revert(); //pkg.close(); // rethrow exception throw ioe; } catch (Exception ioe) { // ensure that file handles are closed (use revert() to not re-write the file) pkg.Revert(); //pkg.close(); // rethrow exception throw ioe; } } } finally { if (inputStream != null) { inputStream.Dispose(); } } throw new InvalidFormatException("Your stream was neither an OLE2 stream, nor an OOXML stream."); }
/** * Initialize the parser object values. * * @param type Type of conversion or import * @param rtfDoc The <code>RtfDocument</code> * @param readerIn The input stream * @param doc The iText <code>Document</code> */ private void Init(int type, RtfDocument rtfDoc, Stream readerIn, Legacy.Text.Document doc, IElement elem) { Init_stats(); // initialize reader to a PushbackReader this.pbReader = Init_Reader(readerIn); this.conversionType = type; this.rtfDoc = rtfDoc; this.document = doc; this.elem = elem; this.currentState = new RtfParserState(); this.stackState = new Stack(); this.SetParserState(PARSER_STARTSTOP); this.importMgr = new RtfImportMgr(this.rtfDoc, this.document); // get destination Mgr this.destinationMgr = RtfDestinationMgr.GetInstance(this); // set the parser RtfDestinationMgr.SetParser(this); // DEBUG INFO for timing and memory usage of RtfCtrlWordMgr object // create multiple new RtfCtrlWordMgr objects to check timing and memory usage // System.Gc(); // long endTime = 0; // Date endDate = null; // long endFree = 0; // DecimalFormat df = new DecimalFormat("#,##0"); // Date startDate = new Date(); // long startTime = System.CurrentTimeMillis(); // long startFree = Runtime.GetRuntime().FreeMemory(); // System.out.Println("1:"); this.rtfKeywordMgr = new RtfCtrlWordMgr(this, this.pbReader);/////////DO NOT COMMENT OUT THIS LINE /////////// foreach (object listener in listeners) { if (listener is IRtfCtrlWordListener) { this.rtfKeywordMgr.AddRtfCtrlWordListener((IRtfCtrlWordListener)listener); } } // endFree = Runtime.GetRuntime().FreeMemory(); // endTime = System.CurrentTimeMillis(); // endDate = new Date(); // System.out.Println("RtfCtrlWordMgr start date: " + startDate.ToLocaleString()); // System.out.Println("RtfCtrlWordMgr end date : " + endDate.ToLocaleString()); // System.out.Println(" Elapsed time : " + Long.ToString(endTime - startTime) + " milliseconds."); // System.out.Println("Begin Constructor RtfCtrlWordMgr , free mem is " + df.Format(startFree / 1024) + "k"); // System.out.Println("End Constructor RtfCtrlWordMgr , free mem is " + df.Format(endFree / 1024) + "k"); // System.out.Println("RtfCtrlWordMgr used approximately " + df.Format((startFree - endFree) / 1024) + "k"); // // System.Gc(); // System.out.Println("2:"); // startDate = new Date(); // startTime = System.CurrentTimeMillis(); // startFree = Runtime.GetRuntime().FreeMemory(); // RtfCtrlWordMgr rtfKeywordMgr2 = new RtfCtrlWordMgr(this, this.pbReader); // endFree = Runtime.GetRuntime().FreeMemory(); // endTime = System.CurrentTimeMillis(); // endDate = new Date(); // System.out.Println("RtfCtrlWordMgr start date: " + startDate.ToLocaleString()); // System.out.Println("RtfCtrlWordMgr end date : " + endDate.ToLocaleString()); // System.out.Println(" Elapsed time : " + Long.ToString(endTime - startTime) + " milliseconds."); // System.out.Println("Begin Constructor RtfCtrlWordMgr , free mem is " + df.Format(startFree / 1024) + "k"); // System.out.Println("End Constructor RtfCtrlWordMgr , free mem is " + df.Format(endFree / 1024) + "k"); // System.out.Println("RtfCtrlWordMgr used approximately " + df.Format((startFree - endFree) / 1024) + "k"); // // System.Gc(); // System.out.Println("3:"); // startDate = new Date(); // startTime = System.CurrentTimeMillis(); // startFree = Runtime.GetRuntime().FreeMemory(); // RtfCtrlWordMgr rtfKeywordMgr3 = new RtfCtrlWordMgr(this, this.pbReader); // endFree = Runtime.GetRuntime().FreeMemory(); // endTime = System.CurrentTimeMillis(); // endDate = new Date(); // System.out.Println("RtfCtrlWordMgr start date: " + startDate.ToLocaleString()); // System.out.Println("RtfCtrlWordMgr end date : " + endDate.ToLocaleString()); // System.out.Println(" Elapsed time : " + Long.ToString(endTime - startTime) + " milliseconds."); // System.out.Println("Begin Constructor RtfCtrlWordMgr , free mem is " + df.Format(startFree / 1024) + "k"); // System.out.Println("End Constructor RtfCtrlWordMgr , free mem is " + df.Format(endFree / 1024) + "k"); // System.out.Println("RtfCtrlWordMgr used approximately " + df.Format((startFree - endFree) / 1024) + "k"); // // System.Gc(); // System.out.Println("4:"); // startDate = new Date(); // startTime = System.CurrentTimeMillis(); // startFree = Runtime.GetRuntime().FreeMemory(); // RtfCtrlWordMgr rtfKeywordMgr4 = new RtfCtrlWordMgr(this, this.pbReader); // endFree = Runtime.GetRuntime().FreeMemory(); // endTime = System.CurrentTimeMillis(); // endDate = new Date(); // System.out.Println("RtfCtrlWordMgr start date: " + startDate.ToLocaleString()); // System.out.Println("RtfCtrlWordMgr end date : " + endDate.ToLocaleString()); // System.out.Println(" Elapsed time : " + Long.ToString(endTime - startTime) + " milliseconds."); // System.out.Println("Begin Constructor RtfCtrlWordMgr , free mem is " + df.Format(startFree / 1024) + "k"); // System.out.Println("End Constructor RtfCtrlWordMgr , free mem is " + df.Format(endFree / 1024) + "k"); // System.out.Println("RtfCtrlWordMgr used approximately " + df.Format((startFree - endFree) / 1024) + "k"); // // System.Gc(); // System.out.Println("5:"); // startDate = new Date(); // startTime = System.CurrentTimeMillis(); // startFree = Runtime.GetRuntime().FreeMemory(); // RtfCtrlWordMgr rtfKeywordMgr5 = new RtfCtrlWordMgr(this, this.pbReader); // endFree = Runtime.GetRuntime().FreeMemory(); // endTime = System.CurrentTimeMillis(); // endDate = new Date(); // System.out.Println("RtfCtrlWordMgr start date: " + startDate.ToLocaleString()); // System.out.Println("RtfCtrlWordMgr end date : " + endDate.ToLocaleString()); // System.out.Println(" Elapsed time : " + Long.ToString(endTime - startTime) + " milliseconds."); // System.out.Println("Begin Constructor RtfCtrlWordMgr , free mem is " + df.Format(startFree / 1024) + "k"); // System.out.Println("End Constructor RtfCtrlWordMgr , free mem is " + df.Format(endFree / 1024) + "k"); // System.out.Println("RtfCtrlWordMgr used approximately " + df.Format((startFree - endFree) / 1024) + "k"); // System.Gc(); // System.out.Println("At ed:"); // startDate = new Date(); // startTime = System.CurrentTimeMillis(); // startFree = Runtime.GetRuntime().FreeMemory(); // //RtfCtrlWordMgr rtfKeywordMgr6 = new RtfCtrlWordMgr(this, this.pbReader); // endFree = Runtime.GetRuntime().FreeMemory(); // endTime = System.CurrentTimeMillis(); // endDate = new Date(); // System.out.Println("RtfCtrlWordMgr start date: " + startDate.ToLocaleString()); // System.out.Println("RtfCtrlWordMgr end date : " + endDate.ToLocaleString()); // System.out.Println(" Elapsed time : " + Long.ToString(endTime - startTime) + " milliseconds."); // System.out.Println("Begin Constructor RtfCtrlWordMgr , free mem is " + df.Format(startFree / 1024) + "k"); // System.out.Println("End Constructor RtfCtrlWordMgr , free mem is " + df.Format(endFree / 1024) + "k"); // System.out.Println("RtfCtrlWordMgr used approximately " + df.Format((startFree - endFree) / 1024) + "k"); }