Exemplo n.º 1
0
        /// <summary>
        /// Authenticates the specified document.
        /// </summary>
        /// <param name="decoder">The decoder.</param>
        public static bool Authenticate(DecoderBase decoder)
        {
            // if authentication is required
            if (decoder.IsAuthenticationRequired)
            {
                // get the document filename
                string filename = null;
                if (decoder.SourceStream != null && decoder.SourceStream is FileStream)
                {
                    filename = ((FileStream)decoder.SourceStream).Name;
                }

                while (true)
                {
                    // create the document password dialog
                    using (DocumentPasswordForm enterPasswordDialog = new DocumentPasswordForm())
                    {
                        enterPasswordDialog.Filename = filename;

                        // show the document password dialog
                        DialogResult showDialogResult = enterPasswordDialog.ShowDialog();

                        // if "OK" button is clicked
                        if (showDialogResult == DialogResult.OK)
                        {
                            DocumentAuthenticationRequest authenticationRequest = new DocumentAuthenticationRequest(
                                enterPasswordDialog.authenticateTypeComboBox.Text,
                                enterPasswordDialog.Password);

                            // uuthenticate the decoder
                            DocumentAuthorizationResult result = decoder.Authenticate(authenticationRequest);
                            // if user is NOT authorized
                            if (!result.IsAuthorized)
                            {
                                MessageBox.Show(
                                    string.Format("The {0} password is incorrect.", enterPasswordDialog.authenticateTypeComboBox.Text),
                                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                            // if user is authorized
                            else
                            {
                                if (enterPasswordDialog.authenticateTypeComboBox.Text != result.UserName)
                                {
                                    MessageBox.Show(
                                        string.Format("Authorized as: {0}", result.UserName),
                                        "Authorization Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                break;
                            }
                        }
                        // if "Cancel" button is clicked
                        else
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        public FileReaderItem Next()
        {
TryAgain:
            m_nextPacket = m_stream.Read();
            if ((object)m_nextPacket == null)
            {
                return(FileReaderItem.EndOfStream);
            }

            if (m_nextPacket.CommandName == "DataStreamNormal")
            {
                if (m_decoderNormal == null)
                {
                    m_decoderNormal = new NormalDecoder();
                }
                m_decoderNormal.Load(((CommandDataStreamNormal)m_nextPacket));
                m_currentDecoder = m_decoderNormal;
                return(FileReaderItem.DataPoint);
            }
            else if (m_nextPacket.CommandName == "ProducerMetadata")
            {
                m_metadata = (SttpProducerMetadata)m_nextPacket;
                foreach (var item in m_metadata.DataPoints)
                {
                    m_metadataLookup[item.DataPointID] = item;
                }
                return(FileReaderItem.ProducerMetadata);
            }
            goto TryAgain;
        }
        /// <summary>
        /// Returns a value indication whether image collection contains single document.
        /// </summary>
        /// <param name="images">Image collection.</param>
        /// <returns><b>True</b> if image collection contains single document; otherwise <b>false</b>.</returns>
        private bool ContainsSingleDocument(ImageCollection images)
        {
            if (images.Count == 0)
            {
                return(false);
            }
            if (images.Count == 1)
            {
                return(true);
            }
            DecoderBase decoder = images[0].SourceInfo.Decoder;

            for (int i = 1; i < images.Count; i++)
            {
                VintasoftImage image = images[i];
                if (decoder != image.SourceInfo.Decoder)
                {
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 4
0
        public StreamEngine(Socket fd, Options options, String endpoint)
        {
            m_handle = fd;
            //      inbuf = null;
            m_insize = 0;
            m_inputError = false;
            //        outbuf = null;
            m_outsize = 0;
            m_handshaking = true;
            m_session = null;
            m_options = options;
            m_plugged = false;
            m_endpoint = endpoint;
            m_socket = null;
            m_encoder = null;
            m_decoder = null;

            //  Put the socket into non-blocking mode.
            Utils.UnblockSocket(m_handle);

            //  Set the socket buffer limits for the underlying socket.
            if (m_options.SendBuffer != 0)
            {
                m_handle.SendBufferSize = m_options.SendBuffer;
            }
            if (m_options.ReceiveBuffer != 0)
            {
                m_handle.ReceiveBufferSize = m_options.ReceiveBuffer;
            }
        }
Exemplo n.º 5
0
        private bool Handshake()
        {
            Debug.Assert(m_handshaking);

            //  Receive the greeting.
            while (m_greetingBytesRead < GreetingSize)
            {
                ByteArraySegment greetingSegment = new ByteArraySegment(m_greeting, m_greetingBytesRead);

                int n = Read(greetingSegment, GreetingSize - m_greetingBytesRead);
                if (n == -1)
                {
                    Error();
                    return false;
                }

                if (n == 0)
                    return false;

                m_greetingBytesRead += n;

                //  We have received at least one byte from the peer.
                //  If the first byte is not 0xff, we know that the
                //  peer is using unversioned protocol.
                if (m_greeting[0] != 0xff)
                    break;

                if (m_greetingBytesRead < 10)
                    continue;

                //  Inspect the right-most bit of the 10th byte (which coincides
                //  with the 'flags' field if a regular message was sent).
                //  Zero indicates this is a header of identity message
                //  (i.e. the peer is using the unversioned protocol).
                if ((m_greeting[9] & 0x01) == 0)
                    break;

                //  The peer is using versioned protocol.
                //  Send the rest of the greeting, if necessary.
                if (!(((byte[]) m_outpos) == ((byte[]) m_greetingOutputBuffer) &&
                      m_outpos.Offset + m_outsize == GreetingSize))
                {
                    if (m_outsize == 0)
                        m_ioObject.SetPollout(m_handle);

                    m_outpos[m_outsize++] = 1; // Protocol version
                    m_outpos[m_outsize++] = (byte) m_options.SocketType;
                }
            }

            //  Is the peer using the unversioned protocol?
            //  If so, we send and receive rests of identity
            //  messages.
            if (m_greeting[0] != 0xff || (m_greeting[9] & 0x01) == 0)
            {
                m_encoder = new Encoder(Config.OutBatchSize);
                m_encoder.SetMsgSource(m_session);

                m_decoder = new Decoder(Config.InBatchSize, m_options.Maxmsgsize);

                m_decoder.SetMsgSink(m_session);

                //  We have already sent the message header.
                //  Since there is no way to tell the encoder to
                //  skip the message header, we simply throw that
                //  header data away.
                int headerSize = m_options.IdentitySize + 1 >= 255 ? 10 : 2;
                byte[] tmp = new byte[10];
                ByteArraySegment bufferp = new ByteArraySegment(tmp);

                int bufferSize = headerSize;

                m_encoder.GetData(ref bufferp, ref bufferSize);

                Debug.Assert(bufferSize == headerSize);

                //  Make sure the decoder sees the data we have already received.
                m_inpos = new ByteArraySegment(m_greeting);
                m_insize = m_greetingBytesRead;

                //  To allow for interoperability with peers that do not forward
                //  their subscriptions, we inject a phony subsription
                //  message into the incomming message stream. To put this
                //  message right after the identity message, we temporarily
                //  divert the message stream from session to ourselves.
                if (m_options.SocketType == ZmqSocketType.Pub || m_options.SocketType == ZmqSocketType.Xpub)
                    m_decoder.SetMsgSink(this);
            }
            else if (m_greeting[VersionPos] == 0)
            {
                //  ZMTP/1.0 framing.
                m_encoder = new Encoder(Config.OutBatchSize);
                m_encoder.SetMsgSource(m_session);

                m_decoder = new Decoder(Config.InBatchSize, m_options.Maxmsgsize);
                m_decoder.SetMsgSink(m_session);
            }
            else
            {
                //  v1 framing protocol.
                m_encoder = new V1Encoder(Config.OutBatchSize, m_session);

                m_decoder = new V1Decoder(Config.InBatchSize, m_options.Maxmsgsize, m_session);
            }
            // Start polling for output if necessary.
            if (m_outsize == 0)
                m_ioObject.SetPollout(m_handle);

            //  Handshaking was successful.
            //  Switch into the normal message flow.
            m_handshaking = false;

            return true;
        }