예제 #1
0
파일: PacketSlice.cs 프로젝트: wyrover/ospy
 public PacketSlice(PacketSlice srcSlice)
 {
     Packet = srcSlice.Packet;
     Offset = srcSlice.Offset;
     Length = srcSlice.Length;
     Tag    = srcSlice.Tag;
 }
예제 #2
0
        public override long Seek(long offset, SeekOrigin origin)
        {
            long end = length - 1;

            if (end < 0)
            {
                end = 0;
            }

            long dest;

            switch (origin)
            {
            case SeekOrigin.Begin:
                dest = offset;
                break;

            case SeekOrigin.Current:
                dest = position + offset;
                break;

            case SeekOrigin.End:
                dest = end + offset;
                break;

            default:
                dest = 0;
                break;
            }

            position = dest;
            curSlice = null;

            return(dest);
        }
예제 #3
0
        private void UpdateCurPacketSlice()
        {
            if (position < 0 || position >= length)
            {
                return;
            }

            int i = 0, offset = 0;

            foreach (IPPacket packet in packets)
            {
                int pktLen = packet.Bytes.Length;

                if (position < offset + pktLen)
                {
                    int pktOffset = (int)position - offset;

                    curSlice = new PacketSlice(packet, pktOffset, (int)pktLen - pktOffset, i);
                    return;
                }

                i++;
                offset += pktLen;
            }
        }
예제 #4
0
 public PacketSlice(PacketSlice srcSlice)
 {
     Packet = srcSlice.Packet;
     Offset = srcSlice.Offset;
     Length = srcSlice.Length;
     Tag = srcSlice.Tag;
 }
예제 #5
0
        public void PopState()
        {
            KeyValuePair <long, PacketSlice> state = prevStates[prevStates.Count - 1];

            prevStates.RemoveAt(prevStates.Count - 1);
            position = state.Key;
            curSlice = state.Value;
        }
예제 #6
0
파일: PacketSlice.cs 프로젝트: wyrover/ospy
        public int CompareTo(Object obj)
        {
            PacketSlice otherSlice = obj as PacketSlice;

            int result = Packet.Index.CompareTo(otherSlice.Packet.Index);

            if (result != 0)
            {
                return(result);
            }

            return(Offset.CompareTo(otherSlice.Offset));
        }
예제 #7
0
        public override long Seek(long offset, SeekOrigin origin)
        {
            long end = length - 1;
            if (end < 0)
                end = 0;

            long dest;
            switch (origin)
            {
                case SeekOrigin.Begin:
                    dest = offset;
                    break;
                case SeekOrigin.Current:
                    dest = position + offset;
                    break;
                case SeekOrigin.End:
                    dest = end + offset;
                    break;
                default:
                    dest = 0;
                    break;
            }

            position = dest;
            curSlice = null;

            return dest;
        }
예제 #8
0
 public void ResetState()
 {
     position = 0;
     curSlice = null;
 }
예제 #9
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (curSlice == null)
            {
                UpdateCurPacketSlice();
                if (curSlice == null)
                    return 0;
            }

            lastReadSlices.Clear();

            long maxCount = length - position;
            if (maxCount <= 0)
                return 0;

            long totalBytesToRead = (count > maxCount) ? maxCount : count;
            long totalBytesLeft = totalBytesToRead;

            long dstOffset = offset;

            while (totalBytesLeft > 0)
            {
                long n = totalBytesLeft;
                if (n > curSlice.Length)
                    n = curSlice.Length;

                lastReadSlices.Add(new PacketSlice(curSlice.Packet, curSlice.Offset, (int)n));

                Array.Copy(curSlice.Packet.Bytes, curSlice.Offset, buffer, dstOffset, n);

                curSlice.Offset += (int)n;
                curSlice.Length -= (int)n;

                if (curSlice.Length == 0)
                {
                    int index = curSlice.Tag + 1;
                    if (index < packets.Count)
                    {
                        curSlice.Packet = packets[index];
                        curSlice.Offset = 0;
                        curSlice.Length = packets[index].Bytes.Length;
                        curSlice.Tag = index;
                    }
                    else
                    {
                        curSlice = null;
                    }
                }

                position += n;

                dstOffset += n;
                totalBytesLeft -= n;
            }

            return (int)totalBytesToRead;
        }
예제 #10
0
 public void PopState()
 {
     KeyValuePair<long, PacketSlice> state = prevStates[prevStates.Count - 1];
     prevStates.RemoveAt(prevStates.Count - 1);
     position = state.Key;
     curSlice = state.Value;
 }
예제 #11
0
파일: MainForm.cs 프로젝트: SayHalou/ospy
        private void ScrollPacketIntoView(PacketSlice slice)
        {
            PacketPosition pPos = (PacketPosition) slice.Packet.Tag;

            if (!pPos.Row.Displayed)
            {
                dataGridView.FirstDisplayedScrollingRowIndex = pPos.Row.Index;
            }

            int lineStart, lineEnd;
            if (dumpDisplayMode == DisplayMode.HEX)
            {
                lineStart = pPos.LineStart + (slice.Offset / 16);
                int bytesRemaining = slice.Length - (16 - (slice.Offset % 16));
                if (bytesRemaining < 0)
                    bytesRemaining = 0;
                lineEnd = lineStart + (bytesRemaining / 16);
            }
            else
            {
                int i = pPos.GetLineOffsetIndexFromPacketOffset(slice.Offset);
                if (i < 0)
                    return;

                lineStart = pPos.LineStart + i;
                lineEnd = lineStart + 1; // FIXME
            }

            System.Drawing.Graphics gfx = CreateGraphics();
            int linePixelHeight = (int) Math.Round((gfx.DpiY / 70.0f) * richTextBox.Font.SizeInPoints);

            int displayedLineFirst = StaticUtils.GetScrollPos(richTextBox.Handle, StaticUtils.SB_VERT) / linePixelHeight;
            int displayedLineLast = displayedLineFirst + (richTextBox.Height / linePixelHeight);
            if (lineStart < displayedLineFirst || lineEnd > displayedLineLast)
            {
                richTextBox.Select(richTextBox.GetFirstCharIndexFromLine(lineStart), 0);
                richTextBox.ScrollToCaret();
            }
        }
예제 #12
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (curSlice == null)
            {
                UpdateCurPacketSlice();
                if (curSlice == null)
                {
                    return(0);
                }
            }

            lastReadSlices.Clear();

            long maxCount = length - position;

            if (maxCount <= 0)
            {
                return(0);
            }

            long totalBytesToRead = (count > maxCount) ? maxCount : count;
            long totalBytesLeft   = totalBytesToRead;

            long dstOffset = offset;

            while (totalBytesLeft > 0)
            {
                long n = totalBytesLeft;
                if (n > curSlice.Length)
                {
                    n = curSlice.Length;
                }

                lastReadSlices.Add(new PacketSlice(curSlice.Packet, curSlice.Offset, (int)n));

                Array.Copy(curSlice.Packet.Bytes, curSlice.Offset, buffer, dstOffset, n);

                curSlice.Offset += (int)n;
                curSlice.Length -= (int)n;

                if (curSlice.Length == 0)
                {
                    int index = curSlice.Tag + 1;
                    if (index < packets.Count)
                    {
                        curSlice.Packet = packets[index];
                        curSlice.Offset = 0;
                        curSlice.Length = packets[index].Bytes.Length;
                        curSlice.Tag    = index;
                    }
                    else
                    {
                        curSlice = null;
                    }
                }

                position += n;

                dstOffset      += n;
                totalBytesLeft -= n;
            }

            return((int)totalBytesToRead);
        }
예제 #13
0
파일: MainForm.cs 프로젝트: SayHalou/ospy
        private void HighlightSlices(PacketSlice[] slices, ref Color bgColor)
        {
            foreach (PacketSlice slice in slices)
            {
                int offset = slice.Offset;
                int length = slice.Length;
                int viewLineNo, basePos;

                if (slice.Packet.Tag == null)
                    return;

                PacketPosition viewPos = (PacketPosition)slice.Packet.Tag;

                // FIXME: this should be cleaned up

                if (dumpDisplayMode == DisplayMode.ASCII)
                {
                    LineOffset[] offsets = viewPos.LineOffsets;

                    int i = viewPos.GetLineOffsetIndexFromPacketOffset(offset);
                    if (i < 0)
                        return;

                    viewLineNo = viewPos.LineStart + 1 + i;
                    for (; i < offsets.Length && length > 0; i++)
                    {
                        LineOffset loff = offsets[i];

                        int lineOffset = offset - loff.Start;
                        if (lineOffset < 0)
                            lineOffset = 0;

                        int lineLeft = loff.Length - lineOffset;
                        if (lineLeft > length)
                            lineLeft = length;

                        basePos = richTextBox.GetFirstCharIndexFromLine(viewLineNo) + 3;

                        richTextBox.Select(basePos + lineOffset, lineLeft);
                        richTextBox.SelectionBackColor = bgColor;

                        viewLineNo++;
                        offset += lineLeft;
                        length -= lineLeft;
                    }
                }
                else if (dumpDisplayMode == DisplayMode.HEX)
                {
                    viewLineNo = viewPos.LineStart + 1 + (offset / 16);

                    while (length > 0)
                    {
                        int lineOffset = offset % 16;
                        int lineLeft = 16 - lineOffset;
                        if (lineLeft > length)
                            lineLeft = length;

                        basePos = richTextBox.GetFirstCharIndexFromLine(viewLineNo);

                        int rPos = basePos + 9 + (lineOffset * 3);
                        richTextBox.Select(rPos, (lineLeft * 3) - 1);
                        richTextBox.SelectionBackColor = bgColor;

                        rPos = basePos + 9 + (16 * 3) + 1 + lineOffset;
                        richTextBox.Select(rPos, lineLeft);
                        richTextBox.SelectionBackColor = bgColor;

                        viewLineNo++;
                        offset += lineLeft;
                        length -= lineLeft;
                    }
                }
            }
        }
예제 #14
0
파일: MainForm.cs 프로젝트: SayHalou/ospy
        private void UpdateDumpHighlighting(PacketSlice[] slices)
        {
            slices = OptimizeSlices(slices);

            if (prevSelectedSlices != null)
            {
                Color cl = richTextBox.BackColor;
                HighlightSlices(prevSelectedSlices, ref cl);
            }

            prevSelectedSlices = slices;

            if (slices == null)
                return;

            Color bgColor = Color.FromArgb(126, 162, 88);

            HighlightSlices(slices, ref bgColor);
        }
예제 #15
0
        private void UpdateCurPacketSlice()
        {
            if (position < 0 || position >= length)
                return;

            int i = 0, offset = 0;
            foreach (IPPacket packet in packets)
            {
                int pktLen = packet.Bytes.Length;

                if (position < offset + pktLen)
                {
                    int pktOffset = (int)position - offset;

                    curSlice = new PacketSlice(packet, pktOffset, (int)pktLen - pktOffset, i);
                    return;
                }

                i++;
                offset += pktLen;
            }
        }
예제 #16
0
 public void ResetState()
 {
     position = 0;
     curSlice = null;
 }
예제 #17
0
파일: MainForm.cs 프로젝트: SayHalou/ospy
        private PacketSlice[] OptimizeSlices(PacketSlice[] slices)
        {
            if (slices == null)
                return null;

            List<PacketSlice> optimizedSlices = new List<PacketSlice>(slices.Length);

            PacketSlice newSlice = null;

            foreach (PacketSlice slice in slices)
            {
                if (newSlice == null)
                {
                    newSlice = new PacketSlice(slice.Packet, slice.Offset, slice.Length);
                }
                else
                {
                    if (slice.Packet == newSlice.Packet &&
                        slice.Offset == newSlice.Offset + newSlice.Length)
                    {
                        newSlice.Length += slice.Length;
                    }
                    else
                    {
                        optimizedSlices.Add(newSlice);
                        newSlice = new PacketSlice(slice.Packet, slice.Offset, slice.Length);
                    }
                }
            }

            if (newSlice != null)
            {
                optimizedSlices.Add(newSlice);
            }

            return optimizedSlices.ToArray();
        }