コード例 #1
0
        public static ContinuationPacket ReadFrom(ArraySegment <byte> segment, int maxDataSize)
        {
            if (segment.Count < NoDataSize)
            {
                throw new InvalidPacketSizeException(segment,
                                                     $"Data is too small, expected {NoDataSize} bytes but provided only {segment.Count}");
            }

            var result = new ContinuationPacket();

            using (var stream = new MemoryStream(segment.Array, segment.Offset, segment.Count))
            {
                var reader = new EndianReader(stream, Endianness.BigEndian);
                result.ChannelIdentifier = reader.ReadUInt32();
                result.PaketSequence     = reader.ReadByte();

                CheckSequence(segment, result.PaketSequence);
            }

            var remainingBytes = Math.Min(maxDataSize, segment.Count - NoDataSize);

            result.Data = new ArraySegment <byte>(segment.Array, segment.Offset + NoDataSize, remainingBytes);

            return(result);
        }
コード例 #2
0
        static Tuple<InitializationPacket, List<ContinuationPacket>> MakeOutputPackets(
            int paketLength, FidoU2FHidMessage message)
        {
            var availableInInit = paketLength - InitializationPacket.NoDataSize;
            var availableInContinuation = paketLength - ContinuationPacket.NoDataSize;
            var data = message.Data;

            var init = new InitializationPacket
            {
                ChannelIdentifier = message.Channel,
                CommandIdentifier = (byte)message.Command,
                PayloadLength = (ushort)data.Count,
                Data = new ArraySegment<byte>(data.Array, data.Offset,
                    Math.Min(data.Count, availableInInit))
            };

            var sizeHandled = init.Data.Count;
            var continuations = new List<ContinuationPacket>();
            byte sequence = 0;
            while (sizeHandled < data.Count)
            {
                var continuation = new ContinuationPacket
                {
                    ChannelIdentifier = message.Channel,
                    PaketSequence = sequence,
                    Data = new ArraySegment<byte>(data.Array, data.Offset + sizeHandled,
                        Math.Min(data.Count - sizeHandled, availableInContinuation))
                };

                continuations.Add(continuation);

                sizeHandled += continuation.Data.Count;
                sequence += 1;
            }

            return Tuple.Create(init, continuations);
        }
コード例 #3
0
        public static ContinuationPacket ReadFrom(ArraySegment<byte> segment, int maxDataSize)
        {
            if (segment.Count < NoDataSize)
            {
                throw new InvalidPacketSizeException(segment,
                    $"Data is too small, expected {NoDataSize} bytes but provided only {segment.Count}");
            }

            var result = new ContinuationPacket();

            using (var stream = new MemoryStream(segment.Array, segment.Offset, segment.Count))
            {
                var reader = new EndianReader(stream, Endianness.BigEndian);
                result.ChannelIdentifier = reader.ReadUInt32();
                result.PaketSequence = reader.ReadByte();

                CheckSequence(segment, result.PaketSequence);
            }

            var remainingBytes = Math.Min(maxDataSize, segment.Count - NoDataSize);
            result.Data = new ArraySegment<byte>(segment.Array, segment.Offset + NoDataSize, remainingBytes);

            return result;
        }
コード例 #4
0
 static IHidOutputReport ToOutputReport(IHidDevice device, ContinuationPacket continuation)
 {
     var report = device.CreateOutputReport();
     continuation.WriteTo(report.Data);
     return report;
 }