public virtual byte[] ToBytes()
        {
            ByteArrayBuilder ab = new ByteArrayBuilder();

            ab.Append((byte)this.EscapeCode);
            ab.Append((byte)this.CmdCode);
            if (this.Subject != null)
            {
                ab.Append((byte)this.Subject.Value);
            }

            {
                var bodyBytes = BodyToBytes();
                if (bodyBytes != null)
                {
                    ab.Append(bodyBytes);
                }
            }

            if (this.GotClosingSE == true)
            {
                ab.Append((byte)CommandCode.IAC);
                ab.Append((byte)CommandCode.SE);
            }
            return(ab.ToByteArray());
        }
Exemple #2
0
        public static byte[] BuildReadScreenResponse(ScreenContent ScreenContent)
        {
            var ra = new ByteArrayBuilder();

            // data stream header.
            {
                var buf = DataStreamHeader.Build(50, TerminalOpcode.ReadScreen, 0, 0);
                ra.Append(buf);
            }

            // screen regeneration buffer. each byte on the screen.
            {
                var buf = ScreenContent.GetRegenerationBuffer();
                ra.Append(buf);
            }

            // update length of response data stream.
            {
                var wk = new ByteArrayBuilder();
                wk.AppendBigEndianShort((short)ra.Length);
                ra.CopyToBuffer(wk.ToByteArray(), 0);
            }

            // IAC EOR
            {
                ra.Append(EOR_Command.Build());
            }

            return(ra.ToByteArray());
        }
        public override byte[] ToBytes()
        {
            if (this.RawBytes != null)
            {
                return(this.RawBytes);
            }
            else
            {
                var ba = new ByteArrayBuilder();

                if (this.AttrByte != null)
                {
                    ba.Append(this.AttrByte.Value);
                }

                if (this.TextBytes != null)
                {
                    ba.Append(this.TextBytes);
                }
                else if (this.DisplayText != null)
                {
                    ba.Append(this.DisplayText.ToEbcdicBytes());
                }
                return(ba.ToByteArray());
            }
        }
        /// <summary>
        /// search for all the Find byte[] patterns and replace with Replace byte[]
        /// contents.
        /// </summary>
        /// <param name="Input"></param>
        /// <param name="Find"></param>
        /// <param name="Replace"></param>
        /// <returns></returns>
        public static byte[] ReplaceAll(this byte[] Input, byte[] Find, byte[] Replace)
        {
            int bx              = 0;
            int inputLength     = Input.Length;
            ByteArrayBuilder ba = new ByteArrayBuilder(inputLength + 100);

            // loop until all bytes of the Input array
            while (bx < inputLength)
            {
                var fx = Input.IndexOf(bx, Find);
                if (fx >= 0)
                {
                    var lx = fx - bx; // length of bytes before the found pattern.
                    ba.Append(Input, bx, lx);

                    // copy the Replace pattern to the result array.
                    ba.Append(Replace);

                    // advance in the input array.
                    bx = fx + Find.Length;
                }

                // find pattern not found. Copy remaining bytes to result array.
                else
                {
                    int remLx = inputLength - bx;
                    ba.Append(Input, bx, remLx);
                    bx += remLx;
                }
            }

            // return the
            return(ba.ToByteArray());
        }
        /// <summary>
        /// convert the sequence of pairs of hex characters on one or more input lines
        /// to an array of bytes. On each text line, convert the hex character pairs until
        /// text which is not a hex char pair is found.
        /// </summary>
        /// <param name="Lines"></param>
        /// <returns></returns>
        public static byte[] HexTextLinesToBytes(this IEnumerable <string> Lines)
        {
            var ba = new ByteArrayBuilder();

            foreach (var line in Lines)
            {
                // make sure the line is padded on right with at least 1 blank.
                var curLine = line.Trim() + "    ";

                // process 3 char chunks on  the current line.
                int ix = 0;
                while (true)
                {
                    var chunk = curLine.Substring(ix, 3);
                    if (chunk == "   ")
                    {
                        break;
                    }

                    var rv     = chunk.HexTextToByte();
                    var errmsg = rv.Item2;
                    if (errmsg != null)
                    {
                        break;
                    }
                    ba.Append(rv.Item1);

                    ix += 3;
                }
            }

            return(ba.ToByteArray());
        }
Exemple #6
0
        public static byte[] ToByteArray(this MacroOpBase cmd)
        {
            var builder = new ByteArrayBuilder(false);

            cmd.Serialize(builder);
            return(builder.ToByteArray());
        }
Exemple #7
0
        public virtual byte[] ToBytes( )
        {
            var ba = new ByteArrayBuilder();

            ba.Append(0x04);
            ba.Append((byte)this.CmdCode);
            return(ba.ToByteArray());
        }
Exemple #8
0
        public override byte[] ToBytes()
        {
            var ba  = new ByteArrayBuilder();
            var buf = base.ToBytes();

            ba.Append(buf);
            ba.Append(this.RequestByte);
            return(ba.ToByteArray());
        }
        public override byte[] ToBytes()
        {
            var ba  = new ByteArrayBuilder();
            var buf = base.ToBytes();

            ba.Append(buf);
            ba.Append(this.ControlChars);
            return(ba.ToByteArray());
        }
Exemple #10
0
        public static Tuple <int, byte[], TelnetLogList> ProcessTelnetCommand(
            TelnetCommand TelnetCmd, NegotiateSettings NegotiateSettings,
            bool doLog = false)
        {
            TelnetLogList logList = null;

            if (doLog == true)
            {
                logList = new TelnetLogList();
            }
            int cx          = 0;
            var writeStream = new ByteArrayBuilder();

            // write to the run log.
            if (logList != null)
            {
                logList.AddItem(Direction.Read, TelnetCmd.ToString());
            }

            if (TelnetCmd.CmdCode == Enums.CommandCode.DO)
            {
                var replyStmt = TelnetCmd.BuildReply(NegotiateSettings);
                writeStream.Append(replyStmt.ToBytes());
            }

            else if (TelnetCmd is NewEnvironCommand)
            {
                var envStmt = TelnetCmd as NewEnvironCommand;
                cx = ProcessNewEnvironStatement(envStmt, writeStream, NegotiateSettings);
            }

            else if (TelnetCmd is TerminalTypeCommand)
            {
                var ttStmt = TelnetCmd as TerminalTypeCommand;
                cx = ProcessTerminalTypeStatement(ttStmt, writeStream, NegotiateSettings);
            }

            else if ((TelnetCmd is EndOfRecordStatement) || (TelnetCmd is TransmitBinaryCommand))
            {
                if (TelnetCmd.CmdCode == Enums.CommandCode.DO)
                {
                    var replyStmt = TelnetCmd.BuildReply(NegotiateSettings);
                    writeStream.Append(replyStmt.ToBytes());

                    if ((replyStmt.CmdCode == Enums.CommandCode.DO) &&
                        (replyStmt.Subject.IsEqual(TelnetSubject.TRANSMIT_BINARY)))
                    {
                    }
                }
                else if (TelnetCmd.CmdCode == Enums.CommandCode.WILL)
                {
                    var replyStmt = TelnetCmd.BuildDoReply();
                    writeStream.Append(replyStmt.ToBytes());
                }
            }
            return(new Tuple <int, byte[], TelnetLogList>(cx, writeStream.ToByteArray(), logList));
        }
Exemple #11
0
        /// <summary>
        /// build a byte array containing a SBA order.
        /// </summary>
        /// <param name="RowNum"></param>
        /// <param name="ColNum"></param>
        /// <returns></returns>
        public static byte[] Build(OneRowCol RowCol)
        {
            var ba = new ByteArrayBuilder();

            ba.Append((byte)WtdOrder.SetBufferAddress);
            ba.Append(RowCol.RowNum);
            ba.Append(RowCol.ColNum);

            return(ba.ToByteArray());
        }
        public override byte[] ToBytes()
        {
            var ba = new ByteArrayBuilder();

            ba.Append((byte)this.OrderCode.Value);
            ba.Append(this.RowAddress);
            ba.Append(this.ColumnAddress);
            ba.Append(this.RepeatTextByte);
            return(ba.ToByteArray());
        }
        public static byte[] Build(OneRowCol RowCol, AidKey AidKey)
        {
            var ba = new ByteArrayBuilder();

            ba.Append((byte)RowCol.RowNum);
            ba.Append((byte)RowCol.ColNum);
            ba.Append((byte)AidKey);

            return(ba.ToByteArray());
        }
Exemple #14
0
        /// <summary>
        /// build a byte array containing an InsertCursor order.
        /// </summary>
        /// <param name="RowNum"></param>
        /// <param name="ColNum"></param>
        /// <returns></returns>
        public static byte[] Build(OneRowCol RowCol)
        {
            var ba = new ByteArrayBuilder();

            ba.Append((byte)WtdOrder.InsertCursor);
            ba.Append(RowCol.RowNum);
            ba.Append(RowCol.ColNum);

            return(ba.ToByteArray());
        }
        public override byte[] ToBytes()
        {
            var ba = new ByteArrayBuilder();

            ba.Append((byte)this.OrderCode.Value);
            ba.AppendBigEndianShort(this.MajorLength);
            ba.Append(this.ClassByte);
            ba.Append(this.TypeByte);

            return(ba.ToByteArray());
        }
        private static T RunThroughSerialize <T>(T cmd) where T : SerializableCommandBase
        {
            var builder = new ByteArrayBuilder(false);

            cmd.Serialize(builder);

            var parsedData = new ParsedByteArray(builder.ToByteArray(), false);
            T   resCmd     = (T)Activator.CreateInstance(typeof(T));

            resCmd.Deserialize(parsedData);
            return(resCmd);
        }
        /// <summary>
        /// Toes the byte array.
        /// </summary>
        /// <returns>a <c>byte[]</c> representing this instance</returns>
        public byte[] ToByteArray()
        {
            ByteArrayToken lengthToken;

            var arrayBuilder =
                new ByteArrayBuilder().AddToken(1, out lengthToken).AddByte((byte)this.ConnectionType).AddByte(
                    (byte)this.NetIpLayer).AddByte(0x00);

            arrayBuilder.ReplaceToken(lengthToken, arrayBuilder.Length);

            return(arrayBuilder.ToByteArray());
        }
 public virtual byte[] ToBytes()
 {
     if (this.OrderBytes != null)
     {
         return(this.OrderBytes);
     }
     else
     {
         var ba = new ByteArrayBuilder();
         ba.Append((byte)this.OrderCode.Value);
         return(ba.ToByteArray());
     }
 }
        /// <summary>
        /// build the entire 5250 query response stream. Starting from data stream header
        /// and concluding with IAC EOR.
        /// </summary>
        /// <returns></returns>
        public static byte[] BuildQuery5250Response()
        {
            var ra = new ByteArrayBuilder();

            // data stream header.
            {
                var buf = DataStreamHeader.Build(50, DataStreamOpcode.Noop, 0, 0);
                ra.Append(buf);
            }

            // response header.  send the special AID code - 0x88.
            {
                var buf = ResponseHeader.Build(0, 0, AidKey.Query5250Reply);
                ra.Append(buf);
            }

            // build the 5250 query response.
            {
                byte byteZero = 0x00;
                ra.AppendBigEndianShort(58);                            // LL. total length of structured field.
                ra.Append(new byte[] { 0xd9, 0x70, 0x80 });             // 5250 query response
                ra.Append(new byte[] { 0x06, 0x00 });                   // control unit code.
                ra.Append(new byte[] { 0x01, 0x03, 0x00 });             // code release level.
                ra.Append(byteZero.Repeat(16));                         // 16 bytes of null
                ra.Append(0x01);                                        // 01 - display station
                ra.Append("3180002".ToEbcdicBytes());                   // machine type and model.
                ra.Append(0x02);                                        // keyboard id
                ra.Append(0x00);                                        // extended keyboard id
                ra.Append(0x00);                                        // reserve
                ra.Append(new byte[] { 0x00, 0x61, 0x50, 0x00 });       // device serial number.
                ra.AppendBigEndianShort(256);                           // max number input fields.
                ra.Append(0x00);                                        // control unit customization
                ra.Append(new byte[] { 0x00, 0x00 });                   // reserved
                ra.Append(new byte[] { 0x18, 0x11, 0x00, 0x00, 0x00 }); //
                ra.Append(byteZero.Repeat(7));                          // 7 bytes of null.
            }

            // update length of response data stream.
            {
                var wk = new ByteArrayBuilder();
                wk.AppendBigEndianShort((short)ra.Length);
                ra.CopyToBuffer(wk.ToByteArray(), 0);
            }

            // IAC EOR
            {
                ra.Append(EOR_Command.Build());
            }

            return(ra.ToByteArray());
        }
        public static byte[] Build(byte ControlChar1, byte ControlChar2, byte[] OrderBytes)
        {
            var ba = new ByteArrayBuilder();

            {
                var buf = WorkstationCommandBase.ToBytes(WorkstationCode.WTD);
                ba.Append(buf);
            }
            ba.Append(ControlChar1);
            ba.Append(ControlChar2);
            ba.Append(OrderBytes);

            return(ba.ToByteArray());
        }
        public static byte[] BuildSaveScreenResponse(ScreenContent ScreenContent)
        {
            var ra = new ByteArrayBuilder();

            // data stream header.
            {
                var buf = DataStreamHeader.Build(50, TerminalOpcode.SaveScreen, 0, 0);
                ra.Append(buf);
            }

            // restore screen workstation command.
            {
                var cmd = new RestoreScreenCommand();
                ra.Append(cmd.ToBytes());
            }

            // clear unit command.
            if (ScreenContent.ScreenDim.GetIsWideScreen( ) == true)
            {
                var cmd = new ClearUnitAlternateCommand(0x00);
                ra.Append(cmd.ToBytes());
            }
            else
            {
                var cmd = new ClearUnitCommand();
                ra.Append(cmd.ToBytes());
            }

            // WTD command.
            {
                var ordersByteStream = ScreenContent.BuildOrderStream( );
                var buf = WriteToDisplayCommand.Build(0x00, 0x18, ordersByteStream);
                ra.Append(buf);
            }

            // update length of response data stream.
            {
                var wk = new ByteArrayBuilder();
                wk.AppendBigEndianShort((short)ra.Length);
                ra.CopyToBuffer(wk.ToByteArray(), 0);
            }

            // IAC EOR
            {
                ra.Append(EOR_Command.Build());
            }

            return(ra.ToByteArray());
        }
Exemple #22
0
 public override byte[] ToBytes()
 {
     if (this.OrderBytes != null)
     {
         return(this.OrderBytes);
     }
     else
     {
         var ba = new ByteArrayBuilder();
         ba.Append((byte)this.OrderCode.Value);
         ba.Append(this.RowNum);
         ba.Append(this.ColNum);
         return(ba.ToByteArray());
     }
 }
        public override byte[] ToBytes()
        {
            var ba = new ByteArrayBuilder();

            ba.Append((byte)this.OrderCode.Value);
            ba.Append(this.FFW_Bytes);
            if (this.FCW_Bytes != null)
            {
                ba.Append(this.FCW_Bytes);
            }
            ba.Append(this.AttrByte);
            ba.AppendBigEndianShort(this.LL_Length);

            return(ba.ToByteArray());
        }
Exemple #24
0
        public byte[] ToByteArray()
        {
            ByteArrayToken lengthToken;

            var arrayBuilder =
                new ByteArrayBuilder()
                .AddToken(1, out lengthToken)
                .AddByte((byte)this.HostProtocolCode)
                .Add(this.IpAddress)
                .AddInt(this.Port);

            arrayBuilder.ReplaceToken(lengthToken, arrayBuilder.Length);

            return(arrayBuilder.ToByteArray());
        }
        protected override byte[] BodyToBytes()
        {
            ByteArrayBuilder ab = new ByteArrayBuilder();

            if (this.SubOption != null)
            {
                ab.Append(this.SubOption.Value.ToByte());
            }

            if (this.TerminalNameBytes != null)
            {
                ab.Append(this.TerminalNameBytes);
            }

            return(ab.ToByteArray());
        }
        /// <summary>
        /// build a data stream header. Return byte array containing the header bytes.
        /// </summary>
        /// <param name="Length"></param>
        /// <param name="Opcode"></param>
        /// <param name="Flag1"></param>
        /// <param name="Flag2"></param>
        /// <returns></returns>
        public static byte[] Build(
            int Length, TerminalOpcode Opcode,
            byte Flag1 = 0x00, byte Flag2 = 0x00)
        {
            var ba = new ByteArrayBuilder();

            ba.AppendBigEndianShort((short)Length);
            ba.Append(0x12);
            ba.Append(0xa0);
            ba.Append(new byte[] { 0x00, 0x00 });
            ba.Append(0x04);
            ba.Append(new byte[] { Flag1, Flag2 });
            ba.Append((byte)Opcode);

            return(ba.ToByteArray());
        }
        public override byte[] ToBytes()
        {
            var ba = new ByteArrayBuilder();

            {
                var buf = base.ToBytes();
                ba.Append(buf);
            }
            ba.Append(this.ControlChars);
            foreach (var order in this.OrderList)
            {
                var buf = order.ToBytes();
                ba.Append(buf);
            }
            return(ba.ToByteArray());
        }
Exemple #28
0
        protected override byte[] BodyToBytes()
        {
            ByteArrayBuilder ab = new ByteArrayBuilder();

            if (this.SubOption != null)
            {
                ab.Append(this.SubOption.Value.ToByte());
            }

            foreach (var optn in this.OptionList)
            {
                ab.Append(optn.ToBytes());
            }

            return(ab.ToByteArray());
        }
Exemple #29
0
        public byte[] ToxBytes()
        {
            ByteArrayBuilder ab = new ByteArrayBuilder();
            var buf             = base.ToBytes();

            ab.Append(buf);
            if (this.SubOption != null)
            {
                ab.Append(this.SubOption.Value.ToByte());
            }

            foreach (var optn in this.OptionList)
            {
                ab.Append(optn.ToBytes());
            }

            return(ab.ToByteArray());
        }
        /// <summary>
        /// no longer used. called WorkstationCommandListExt.ProcessAndPaint, which
        /// itself is not used.
        /// </summary>
        /// <param name="VisualItems"></param>
        /// <param name="Caret"></param>
        /// <returns></returns>
        public static byte[] BuildSaveScreenResponse(
            ScreenVisualItems VisualItems, CanvasPositionCursor Caret)
        {
            var ra = new ByteArrayBuilder();

            // data stream header.
            {
                var buf = DataStreamHeader.Build(50, TerminalOpcode.SaveScreen, 0, 0);
                ra.Append(buf);
            }

            // restore screen workstation command.
            {
                var cmd = new RestoreScreenCommand();
                ra.Append(cmd.ToBytes());
            }

            // clear unit command.
            {
                var cmd = new ClearUnitCommand();
                ra.Append(cmd.ToBytes());
            }

            // WTD command.
            {
                var ordersByteStream = VisualItems.BuildOrderStream(Caret);
                var buf = WriteToDisplayCommand.Build(0x00, 0x18, ordersByteStream);
                ra.Append(buf);
            }

            // update length of response data stream.
            {
                var wk = new ByteArrayBuilder();
                wk.AppendBigEndianShort((short)ra.Length);
                ra.CopyToBuffer(wk.ToByteArray(), 0);
            }

            // IAC EOR
            {
                ra.Append(EOR_Command.Build());
            }

            return(ra.ToByteArray());
        }