<commit_before>﻿using System;
using System.Net;

namespace Snifter
{
    // ReSharper disable once InconsistentNaming
    public class IPPacket
    {
        public int Version { get; }
        public int HeaderLength { get; }
        public int Protocol { get; }
        public IPAddress SourceAddress { get; }
        public IPAddress DestAddress { get; }
        public ushort SourcePort { get; }
        public ushort DestPort { get; }

        public IPPacket(byte[] data)
        {
            var versionAndLength = data[0];
            this.Version = versionAndLength >> 4;

            // Only parse IPv4 packets for now
            if (this.Version != 4)
                return;

            this.HeaderLength = (versionAndLength & 0x0F) << 2;

            this.Protocol = Convert.ToInt32(data[9]);
            this.SourceAddress = new IPAddress(BitConverter.ToUInt32(data, 12));
            this.DestAddress = new IPAddress(BitConverter.ToUInt32(data, 16));

            if (Enum.IsDefined(typeof(ProtocolsWithPort), this.Protocol))
            {
                this.SourcePort = BitConverter.ToUInt16(data, this.HeaderLength);
                this.DestPort = BitConverter.ToUInt16(data, this.HeaderLength + 2);
            }
        }
    }

    /// <summary>
    /// Protocols that have a port abstraction
    /// </summary>
    internal enum ProtocolsWithPort
    {
        TCP = 6,
        UDP = 17,
        SCTP = 132
    }
}<commit_msg>A workaround on endianness issue<commit_after>﻿using System;
using System.Net;

namespace Snifter
{
    // ReSharper disable once InconsistentNaming
    public class IPPacket
    {
        public int Version { get; }
        public int HeaderLength { get; }
        public int Protocol { get; }
        public IPAddress SourceAddress { get; }
        public IPAddress DestAddress { get; }
        public ushort SourcePort { get; }
        public ushort DestPort { get; }

        public IPPacket(byte[] data)
        {
            var versionAndLength = data[0];
            this.Version = versionAndLength >> 4;

            // Only parse IPv4 packets for now
            if (this.Version != 4)
                return;

            this.HeaderLength = (versionAndLength & 0x0F) << 2;

            this.Protocol = Convert.ToInt32(data[9]);
            this.SourceAddress = new IPAddress(BitConverter.ToUInt32(data, 12));
            this.DestAddress = new IPAddress(BitConverter.ToUInt32(data, 16));

            if (Enum.IsDefined(typeof(ProtocolsWithPort), this.Protocol))
            {
                // Ensure big-endian
                this.SourcePort = (ushort)((data[this.HeaderLength] << 8) | data[this.HeaderLength + 1]);
                this.DestPort = (ushort)((data[this.HeaderLength + 2] << 8) | data[this.HeaderLength + 3]);
            }
        }
    }

    /// <summary>
    /// Protocols that have a port abstraction
    /// </summary>
    internal enum ProtocolsWithPort
    {
        TCP = 6,
        UDP = 17,
        SCTP = 132
    }
}
{'fraction_non_alphanumeric': 0.10126162018592297, 'fraction_numerical': 0.019256308100929615, 'mean_word_length': 1.8613485280151947, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1405479', 'n_tokens_mistral': 895, 'n_tokens_neox': 832, 'n_words': 212}