/// <summary> /// Checks the given bytes to make sure the header is valid /// </summary> /// <param name="bytes"></param> /// <param name="flag"></param> /// <returns></returns> private bool CheckHeader(byte[] bytes, HeaderTestFlag flag) { bool hasBadGroupHeaderContent = false; bool hasBadCoalescedHeaderContent = false; if (bytes.Length < HEADER_LEN) { //missing the header if ((flag & HeaderTestFlag.ThrowOnFailedCheck) != 0) { throw new ArgumentException("Given byte array is too short to be a serialized SOG/CSOG"); } else { return(false); } } if ((flag & HeaderTestFlag.CheckValidGroup) != 0 || (flag & HeaderTestFlag.CheckValidEither) != 0) { //test group header if (!Enumerable.SequenceEqual(GROUP_HEADER, bytes.Take(HEADER_LEN))) { hasBadGroupHeaderContent = true; } } if ((flag & HeaderTestFlag.CheckValidCoalesced) != 0 || (flag & HeaderTestFlag.CheckValidEither) != 0) { //test coalesced header if (!Enumerable.SequenceEqual(COALESCED_HEADER, bytes.Take(HEADER_LEN))) { hasBadCoalescedHeaderContent = true; } } if ((flag & HeaderTestFlag.CheckValidGroup) != 0) { if (hasBadGroupHeaderContent) { if ((flag & HeaderTestFlag.ThrowOnFailedCheck) != 0) { throw new ArgumentException("Given bytes do not contain the correct header to be a serialized SOG"); } else { return(false); } } } if ((flag & HeaderTestFlag.CheckValidCoalesced) != 0) { if (hasBadCoalescedHeaderContent) { if ((flag & HeaderTestFlag.ThrowOnFailedCheck) != 0) { throw new ArgumentException("Given bytes do not contain the correct header to be a serialized CSOG"); } else { return(false); } } } if ((flag & HeaderTestFlag.CheckValidEither) != 0) { if (hasBadGroupHeaderContent && hasBadCoalescedHeaderContent) { if ((flag & HeaderTestFlag.ThrowOnFailedCheck) != 0) { throw new ArgumentException("Given bytes do not contain the correct header to be a serialized SOG/CSOG"); } else { return(false); } } } return(true); }
/// <summary> /// Checks the given bytes to make sure the header is valid /// </summary> /// <param name="bytes"></param> /// <param name="flag"></param> /// <returns></returns> private bool CheckHeader(byte[] bytes, HeaderTestFlag flag) { bool hasBadGroupHeaderContent = false; bool hasBadCoalescedHeaderContent = false; if (bytes.Length < HEADER_LEN) { //missing the header if ((flag & HeaderTestFlag.ThrowOnFailedCheck) != 0) { throw new ArgumentException("Given byte array is too short to be a serialized SOG/CSOG"); } else { return false; } } if ((flag & HeaderTestFlag.CheckValidGroup) != 0 || (flag & HeaderTestFlag.CheckValidEither) != 0) { //test group header if (!Enumerable.SequenceEqual(GROUP_HEADER, bytes.Take(HEADER_LEN))) { hasBadGroupHeaderContent = true; } } if ((flag & HeaderTestFlag.CheckValidCoalesced) != 0 || (flag & HeaderTestFlag.CheckValidEither) != 0) { //test coalesced header if (!Enumerable.SequenceEqual(COALESCED_HEADER, bytes.Take(HEADER_LEN))) { hasBadCoalescedHeaderContent = true; } } if ((flag & HeaderTestFlag.CheckValidGroup) != 0) { if (hasBadGroupHeaderContent) { if ((flag & HeaderTestFlag.ThrowOnFailedCheck) != 0) { throw new ArgumentException("Given bytes do not contain the correct header to be a serialized SOG"); } else { return false; } } } if ((flag & HeaderTestFlag.CheckValidCoalesced) != 0) { if (hasBadCoalescedHeaderContent) { if ((flag & HeaderTestFlag.ThrowOnFailedCheck) != 0) { throw new ArgumentException("Given bytes do not contain the correct header to be a serialized CSOG"); } else { return false; } } } if ((flag & HeaderTestFlag.CheckValidEither) != 0) { if (hasBadGroupHeaderContent && hasBadCoalescedHeaderContent) { if ((flag & HeaderTestFlag.ThrowOnFailedCheck) != 0) { throw new ArgumentException("Given bytes do not contain the correct header to be a serialized SOG/CSOG"); } else { return false; } } } return true; }