Пример #1
0
        private void CheckForValidity(bool force = false)
        {
            if (_isComplete && !force)
            {
                return;
            }

            IsValid    = false;
            IsComplete = false;
            if (Chunks.Count <= 0)
            {
                return;
            }

            var tmp = Chunks.Aggregate <byte[], IEnumerable <byte> >(null, (current, b) => current == null ? b : current.Concat(b)).ToArray();

            if (tmp.Length < 12) // Can't fit the header in here
            {
                return;
            }

            // Check the header
            if (_header.Where((t, i) => tmp[i] != t).Any())
            {
                return;
            }

            // Another checking
            if (tmp[8] != 0x06)
            {
                return;
            }

            // Get the size and name
            var size = BitConverter.ToInt32(tmp.SubArray(4, 4).Reverse().ToArray(), 0);

            IsValid = true;

            // Get the name length
            const int nameOffset = 12;
            int       nameLength = tmp[9];

            if (tmp.Length > nameOffset + tmp[9])
            {
                Name        = Encoding.Unicode.GetString(tmp.SubArray(nameOffset, nameLength));
                _isComplete = tmp.Length >= size + nameOffset + nameLength;
                Debug.WriteLine(tmp.Length);
                if (_isComplete)
                {
                    Data = tmp.SubArray(nameOffset + nameLength, size - nameLength - 4).Concat(new byte[] { 0x00, 0x00 }).ToArray();
                }
            }
        }
Пример #2
0
        private void CheckForValidity(bool force = false)
        {
            if (_isComplete && !force)
            {
                return;
            }

            IsValid    = false;
            IsComplete = false;
            if (Chunks.Count <= 0)
            {
                return;
            }

            var tmp = Chunks.Aggregate <byte[], IEnumerable <byte> >(null, (current, b) => current == null ? b : current.Concat(b)).ToArray();

            // Check the header
            Type = PrimeUsbDataType.Unknown;
            foreach (var _header in _headers)
            {
                if (tmp.Length < _header.Value.Header.Length || _header.Value.Header.Where((t, i) => tmp[i] != t).Any())
                {
                    continue;
                }

                Type = _header.Value.Type; // Valid type
                break;
            }

            var size = 0;

            switch (Type)
            {
            case PrimeUsbDataType.Message:
                size = BitConverter.ToInt32(tmp.SubArray(4, 4).Reverse().ToArray(), 0);

                IsValid = true;

                if (tmp.Length > 8 + size)
                {
                    Data        = tmp.SubArray(8, size - 2);
                    _isComplete = true;
                }
                break;

            case PrimeUsbDataType.File:
                if (tmp.Length < 12)     // Can't fit the header for files in here
                {
                    return;
                }

                // Another checking
                if (tmp[8] != 0x06)
                {
                    return;
                }

                // Get the size and name
                size = BitConverter.ToInt32(tmp.SubArray(4, 4).Reverse().ToArray(), 0);

                IsValid = true;

                // Get the name length
                const int nameOffset = 12;
                int       nameLength = tmp[9];

                if (tmp.Length > nameOffset + tmp[9])
                {
                    Name        = Encoding.Unicode.GetString(tmp.SubArray(nameOffset, nameLength));
                    _isComplete = tmp.Length >= size + nameOffset + nameLength;

                    if (_isComplete)
                    {
                        Data = tmp.SubArray(nameOffset + nameLength, size - nameLength - 4)
                               .Concat(new byte[] { 0x00, 0x00 }).ToArray();
                    }
                }
                break;
            }
        }