private static byte[] GetMarker(long fields)
            {
                if (fields <= 15)
                {
                    return new[] { (byte)(0xA0 + fields) }
                }
                ;

                var output = new List <byte>();

                if (fields <= byte.MaxValue)
                {
                    output.Add(0xD8);
                }

                if (fields <= ushort.MaxValue)
                {
                    output.Add(0xD9);
                }

                if (fields <= uint.MaxValue)
                {
                    output.Add(0xDA);
                }

                if (fields > uint.MaxValue)
                {
                    throw new ArgumentOutOfRangeException(nameof(fields), "Too many fields defined!");
                }

                output.AddRange(PackStream.GetLength(fields));
                return(output.ToArray());
            }
Exemple #2
0
 public Node(byte[] bytes)
 {
     byte[] editedBytes;
     Id     = GetId(bytes, out editedBytes);
     Labels = GetLabels(editedBytes, out editedBytes);
     Data   = PackStream.Unpack <T>(editedBytes);
 }
            private static byte[] Marker(long size)
            {
                if (size <= 15)
                {
                    return new[] { (byte)(0x80 + size) }
                }
                ;

                var output = new List <byte>();

                if (size <= 255)
                {
                    output.Add(0xD0);
                }

                else if (size <= 65535)
                {
                    output.Add(0xD1);
                }

                else if (size <= 4294967295)
                {
                    output.Add(0xD2);
                }

                if (size > uint.MaxValue)
                {
                    throw new ArgumentOutOfRangeException(nameof(size), size, "Size given is too big.");
                }

                output.AddRange(PackStream.GetLength(size));
                return(output.ToArray());
            }
            private static IDictionary UnpackDictionary <TKey, TValue>(byte[] content)
            {
                int numberOfPairs;
                var markerLess = RemoveMarker(content, out numberOfPairs);

                var output = new Dictionary <TKey, TValue>();

                if (numberOfPairs == 0)
                {
                    return(output);
                }

                var packed = PackStream.GetPackedEntities(markerLess);

                for (var i = 0; i < packed.Length; i += 2)
                {
                    var  key   = PackStream.Unpack(packed[i]);
                    var  value = PackStream.Unpack(packed[i + 1]);
                    Type genericType;
                    if (Packers.List.IsEnumerable(typeof(TValue), out genericType))
                    {
                        var method        = typeof(Packers.List).GetTypeInfo().GetDeclaredMethod("Unpack");
                        var genericMethod = method.MakeGenericMethod(genericType);
                        var list          = genericMethod.Invoke(null, new object[] { packed[i + 1].Original }) as IEnumerable;
                        value = (TValue)list;
                    }

                    output.Add(key, value);
                }

                return(output);
            }
Exemple #5
0
            internal static byte[] GetMarker(long itemsInList)
            {
                if (itemsInList <= 15)
                {
                    return new[] { (byte)(0x90 + itemsInList) }
                }
                ;

                var output = new List <byte>();

                if (itemsInList <= 0xFF)
                {
                    output.Add(0xD4);
                }

                else if (itemsInList <= 0xFFFF)
                {
                    output.Add(0xD5);
                }

                else if (itemsInList <= uint.MaxValue)
                {
                    output.Add(0xD6);
                }

                if (itemsInList > uint.MaxValue)
                {
                    throw new ArgumentOutOfRangeException(nameof(itemsInList), itemsInList, "Too many items in the list!");
                }

                output.AddRange(PackStream.ConvertSizeToBytes(itemsInList));
                return(output.ToArray());
            }
Exemple #6
0
        public ICollection <byte[]> GetChunks(int chunkSize = 65535, bool includeZeroEnding = true)
        {
            if (chunkSize > 65535)
            {
                throw new ArgumentOutOfRangeException(nameof(chunkSize), chunkSize, "Chunk size too big!");
            }

            var fullMessage = new List <byte>();

            fullMessage.Add(GetStructSize());
            fullMessage.Add((byte)Signature);
            var packed = PackStream.Pack(Content);

            fullMessage.AddRange(packed);

            var header = GenerateChunkHeader(fullMessage.Count);

            for (int i = header.Length - 1; i >= 0; i--)
            {
                fullMessage.Insert(0, header[i]);
            }

            if (includeZeroEnding)
            {
                fullMessage.AddRange(ZeroEnding);
            }

            return(new List <byte[]> {
                fullMessage.ToArray()
            });
        }
Exemple #7
0
        private static IDictionary <string, IEnumerable <string> > GetMetaData(Struct s)
        {
            if (s.NumberOfFields == 0)
            {
                return(null);
            }

            //Get actual data
            var response = PackStream.Unpack <Dictionary <string, IEnumerable <string> > >(s.ContentWithoutStructAndSignature);

            return(response);
        }
Exemple #8
0
            public static byte[] Pack <T>(IEnumerable <T> content)
            {
                var output        = new List <byte>();
                var contentAsList = content.ToList();

                output.AddRange(GetMarker(contentAsList.Count));

                for (var i = 0; i < contentAsList.Count; i++)
                {
                    output.AddRange(PackStream.Pack(contentAsList[i]));
                }

                return(output.ToArray());
            }
Exemple #9
0
            public static IEnumerable <T> Unpack <T>(byte[] content)
            {
                if (!IsUnpackable(content))
                {
                    throw new ArgumentException("Not a list.", nameof(content));
                }

                var numberOfElements = GetNumberOfElements(content);
                var bytesToSkip      = GetSizeOfMarkerInBytes(numberOfElements);

                var unpacked = PackStream.Unpack(content.Skip(bytesToSkip).ToArray());

                return(unpacked.Cast <T>());
            }
Exemple #10
0
        public byte[] GetBytes()
        {
            var output = new List <byte>();

            output.AddRange(PackStream.Pack(Cypher));
            if (Parameters == null)
            {
                output.AddRange(new byte[] { 0xA0 });
            }
            else
            {
                throw new NotImplementedException();
            }

            return(output.ToArray());
        }
            private static byte[] PackDictionary(IDictionary content)
            {
                var arguments = content.GetType().GenericTypeArguments;
                var keyType   = arguments[0];
                var valueType = arguments[1];

                var bytes = new List <byte>();

                bytes.AddRange(GetMarker(content.Count));

                foreach (var item in content.Keys)
                {
                    var keyBytes = PackStream.Pack(Convert.ChangeType(item, keyType));
                    bytes.AddRange(keyBytes);
                    bytes.AddRange(PackStream.Pack(Convert.ChangeType(content[item], valueType)));
                }

                return(bytes.ToArray());
            }
            public static int GetExpectedSizeInBytes(byte[] content, bool includeMarkerSize = true)
            {
                var numberOfElements = GetExpectedNumberOfFields(content);
                var markerSize       = SizeOfMarkerInBytes(content);

                int length = 0;

                if (includeMarkerSize)
                {
                    length += markerSize;
                }

                var bytesWithoutMarker = content.Skip(markerSize).ToArray();

                for (int i = 0; i < numberOfElements; i++)
                {
                    var itemLength = PackStream.GetLengthOfFirstItem(bytesWithoutMarker);
                    bytesWithoutMarker = bytesWithoutMarker.Skip(itemLength).ToArray();
                    length            += itemLength;
                }

                return(length);
            }
Exemple #13
0
            public static int GetLengthInBytes(byte[] bytes, bool includeMarkerSize)
            {
                var numberOfElements = GetNumberOfElements(bytes);
                var markerSize       = GetSizeOfMarkerInBytes(numberOfElements);

                int length = 0;

                if (includeMarkerSize)
                {
                    length += markerSize;
                }

                var bytesWithoutMarker = bytes.Skip(markerSize).ToArray();

                for (int i = 0; i < numberOfElements; i++)
                {
                    var itemLength = PackStream.GetLengthOfFirstItem(bytesWithoutMarker);
                    bytesWithoutMarker = bytesWithoutMarker.Skip(itemLength).ToArray();
                    length            += itemLength;
                }

                return(length);
            }
Exemple #14
0
 public byte[] GetBytes()
 {
     return(PackStream.Pack(Content));
 }
Exemple #15
0
 private static byte[] GenerateChunkHeader(int length)
 {
     return(PackStream.ConvertSizeToBytes(length, 2));
 }