Пример #1
0
        private static int EncodeString(string value, Span <byte> headerBlock, bool huffmanEncode)
        {
            byte[] data = Encoding.ASCII.GetBytes(value);
            byte   prefix;

            if (!huffmanEncode)
            {
                prefix = 0;
            }
            else
            {
                int len = HuffmanEncoder.GetEncodedLength(data);

                byte[] huffmanData = new byte[len];
                HuffmanEncoder.Encode(data, huffmanData);

                data   = huffmanData;
                prefix = 0x80;
            }

            int bytesGenerated = 0;

            bytesGenerated += EncodeInteger(data.Length, prefix, 0x80, headerBlock);

            data.AsSpan().CopyTo(headerBlock.Slice(bytesGenerated));
            bytesGenerated += data.Length;

            return(bytesGenerated);
        }
Пример #2
0
        public static int EncodeHeader(Span <byte> buffer, string name, string value, QPackFlags flags = QPackFlags.None)
        {
            byte[] data = Encoding.ASCII.GetBytes(name);
            byte   prefix;

            if (!flags.HasFlag(QPackFlags.HuffmanEncodeName))
            {
                prefix = 0b0010_0000;
            }
            else
            {
                int len = HuffmanEncoder.GetEncodedLength(data);

                byte[] huffmanData = new byte[len];
                HuffmanEncoder.Encode(data, huffmanData);

                data   = huffmanData;
                prefix = 0b0010_1000;
            }

            if (flags.HasFlag(QPackFlags.NeverIndexed))
            {
                prefix |= 0b0001_0000;
            }

            int bytesGenerated = 0;

            // write name string header.
            bytesGenerated += EncodeInteger(buffer, data.Length, prefix, 0b1111_1000);

            // write name string.
            data.AsSpan().CopyTo(buffer.Slice(bytesGenerated));
            bytesGenerated += data.Length;

            // write value string.
            bytesGenerated += EncodeString(buffer.Slice(bytesGenerated), value, flags.HasFlag(QPackFlags.HuffmanEncodeValue));

            return(bytesGenerated);
        }