protected override void EncodeTrans2Data() { int totalSize = 0; int fixedSize = 0; // The fixed size of each structure if (this.trans2Data.Data != null) { Type type = this.trans2Data.Data.GetType(); if (type == typeof(SMB_INFO_STANDARD_OF_TRANS2_FIND_FIRST2[])) { SMB_INFO_STANDARD_OF_TRANS2_FIND_FIRST2[] standardArray = ( SMB_INFO_STANDARD_OF_TRANS2_FIND_FIRST2[])this.trans2Data.Data; fixedSize = (this.smbHeader.Flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE ? 26 : 24; ushort resumeKeyLength = 4; if (isResumeKeyExisted) { totalSize = (resumeKeyLength + fixedSize) * standardArray.Length; } else { totalSize = fixedSize * standardArray.Length; } for (int i = 0; i < standardArray.Length; i++) { totalSize += standardArray[i].FileNameLength; } this.smbData.Trans2_Data = new byte[totalSize]; using (MemoryStream memoryStream = new MemoryStream(this.smbData.Trans2_Data)) { using (Channel channel = new Channel(null, memoryStream)) { channel.BeginWriteGroup(); for (int i = 0; i < standardArray.Length; i++) { if (isResumeKeyExisted) { channel.Write <uint>(standardArray[i].ResumeKey); } channel.Write <SmbDate>(standardArray[i].CreationDate); channel.Write <SmbTime>(standardArray[i].CreationTime); channel.Write <SmbDate>(standardArray[i].LastAccessDate); channel.Write <SmbTime>(standardArray[i].LastAccessTime); channel.Write <SmbDate>(standardArray[i].LastWriteDate); channel.Write <SmbTime>(standardArray[i].LastWriteTime); channel.Write <uint>(standardArray[i].DataSize); channel.Write <uint>(standardArray[i].AllocationSize); channel.Write <SmbFileAttributes>(standardArray[i].Attributes); channel.Write <byte>(standardArray[i].FileNameLength); if ((this.smbHeader.Flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE) { channel.Write <byte>(new byte()); } channel.WriteBytes(standardArray[i].FileName); if ((this.smbHeader.Flags2 & SmbFlags2.SMB_FLAGS2_UNICODE) == SmbFlags2.SMB_FLAGS2_UNICODE) { channel.Write <ushort>(new ushort()); } else { channel.Write <byte>(new byte()); } } channel.EndWriteGroup(); } } } else if (type == typeof(SMB_INFO_QUERY_EA_SIZE_OF_TRANS2_FIND_FIRST2[])) { SMB_INFO_QUERY_EA_SIZE_OF_TRANS2_FIND_FIRST2[] queryEaArray = ( SMB_INFO_QUERY_EA_SIZE_OF_TRANS2_FIND_FIRST2[])this.trans2Data.Data; fixedSize = 28; ushort resumeKeyLength = 4; if (isResumeKeyExisted) { totalSize = (resumeKeyLength + fixedSize) * queryEaArray.Length; } else { totalSize = fixedSize * queryEaArray.Length; } for (int i = 0; i < queryEaArray.Length; i++) { totalSize += queryEaArray[i].FileNameLength; } this.smbData.Trans2_Data = new byte[totalSize]; using (MemoryStream memoryStream = new MemoryStream(this.smbData.Trans2_Data)) { using (Channel channel = new Channel(null, memoryStream)) { channel.BeginWriteGroup(); for (int i = 0; i < queryEaArray.Length; i++) { if (isResumeKeyExisted) { channel.Write <uint>(queryEaArray[i].ResumeKey); } channel.Write <SmbDate>(queryEaArray[i].CreationDate); channel.Write <SmbTime>(queryEaArray[i].CreationTime); channel.Write <SmbDate>(queryEaArray[i].LastAccessDate); channel.Write <SmbTime>(queryEaArray[i].LastAccessTime); channel.Write <SmbDate>(queryEaArray[i].LastWriteDate); channel.Write <SmbTime>(queryEaArray[i].LastWriteTime); channel.Write <uint>(queryEaArray[i].DataSize); channel.Write <uint>(queryEaArray[i].AllocationSize); channel.Write <SmbFileAttributes>(queryEaArray[i].Attributes); channel.Write <uint>(queryEaArray[i].EaSize); channel.Write <byte>(queryEaArray[i].FileNameLength); channel.WriteBytes(queryEaArray[i].FileName); channel.Write <byte>(new byte()); } channel.EndWriteGroup(); } } } else if (type == typeof(SMB_INFO_QUERY_EAS_FROM_LIST_OF_TRANS2_FIND_FIRST2[])) { SMB_INFO_QUERY_EAS_FROM_LIST_OF_TRANS2_FIND_FIRST2[] queryEaFromListArray = ( SMB_INFO_QUERY_EAS_FROM_LIST_OF_TRANS2_FIND_FIRST2[])this.trans2Data.Data; fixedSize = 24; ushort resumeKeyLength = 4; if (isResumeKeyExisted) { totalSize = (resumeKeyLength + fixedSize) * queryEaFromListArray.Length; } else { totalSize = fixedSize * queryEaFromListArray.Length; } for (int i = 0; i < queryEaFromListArray.Length; i++) { totalSize += (int)(queryEaFromListArray[i].FileNameLength + queryEaFromListArray[i].EaSize); } this.smbData.Trans2_Data = new byte[totalSize]; using (MemoryStream memoryStream = new MemoryStream(this.smbData.Trans2_Data)) { using (Channel channel = new Channel(null, memoryStream)) { channel.BeginWriteGroup(); for (int i = 0; i < queryEaFromListArray.Length; i++) { if (isResumeKeyExisted) { channel.Write <uint>(queryEaFromListArray[i].ResumeKey); } channel.Write <SmbDate>(queryEaFromListArray[i].CreationDate); channel.Write <SmbTime>(queryEaFromListArray[i].CreationTime); channel.Write <SmbDate>(queryEaFromListArray[i].LastAccessDate); channel.Write <SmbTime>(queryEaFromListArray[i].LastAccessTime); channel.Write <SmbDate>(queryEaFromListArray[i].LastWriteDate); channel.Write <SmbTime>(queryEaFromListArray[i].LastWriteTime); channel.Write <uint>(queryEaFromListArray[i].DataSize); channel.Write <uint>(queryEaFromListArray[i].AllocationSize); channel.Write <SmbFileAttributes>(queryEaFromListArray[i].Attributes); channel.Write <uint>(queryEaFromListArray[i].EaSize); if (queryEaFromListArray[i].ExtendedAttributeList != null) { foreach (SMB_FEA smbEa in queryEaFromListArray[i].ExtendedAttributeList) { channel.Write <SMB_FEA>(smbEa); } } channel.Write <byte>(queryEaFromListArray[i].FileNameLength); channel.WriteBytes(queryEaFromListArray[i].FileName); channel.Write <byte>(new byte()); } channel.EndWriteGroup(); } } } else if (type == typeof(SMB_FIND_FILE_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2[])) { SMB_FIND_FILE_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2[] fileInfoArray = ( SMB_FIND_FILE_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2[])this.trans2Data.Data; fixedSize = 64; // Including the fixed length of this structure. totalSize = fixedSize * fileInfoArray.Length; int pad = 0; for (int i = 0; i < fileInfoArray.Length; i++) { totalSize += (int)(fileInfoArray[i].FileNameLength); pad = (int)(fileInfoArray[i].NextEntryOffset - fixedSize - fileInfoArray[i].FileNameLength); if (pad > 0) { totalSize += pad; } } this.smbData.Trans2_Data = new byte[totalSize]; using (MemoryStream memoryStream = new MemoryStream(this.smbData.Trans2_Data)) { using (Channel channel = new Channel(null, memoryStream)) { channel.BeginWriteGroup(); for (int i = 0; i < fileInfoArray.Length; i++) { fixedSize = 64; // Including the fixed length of this structure. channel.Write <SMB_FIND_FILE_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2>(fileInfoArray[i]); fixedSize += (int)fileInfoArray[i].FileNameLength; // Add the length of file name and pad. pad = (int)(fileInfoArray[i].NextEntryOffset - fixedSize); if (pad > 0) { channel.WriteBytes(new byte[pad]); } } channel.EndWriteGroup(); } } } else if (type == typeof(SMB_FIND_FILE_FULL_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2[])) { SMB_FIND_FILE_FULL_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2[] fullInfoArray = (SMB_FIND_FILE_FULL_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2[])this.trans2Data.Data; fixedSize = 68; // Including the fixed length of this structure. totalSize = fixedSize * fullInfoArray.Length; int pad = 0; for (int i = 0; i < fullInfoArray.Length; i++) { totalSize += (int)(fullInfoArray[i].FileNameLength + fullInfoArray[i].EaSize); pad = (int)(fullInfoArray[i].NextEntryOffset - fixedSize - fullInfoArray[i].FileNameLength); if (pad > 0) { totalSize += pad; } } this.smbData.Trans2_Data = new byte[totalSize]; using (MemoryStream memoryStream = new MemoryStream(this.smbData.Trans2_Data)) { using (Channel channel = new Channel(null, memoryStream)) { channel.BeginWriteGroup(); for (int i = 0; i < fullInfoArray.Length; i++) { fixedSize = 68; // Including the fixed length of this structure. channel.Write <SMB_FIND_FILE_FULL_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2>(fullInfoArray[i]); fixedSize += (int)fullInfoArray[i].FileNameLength; // Add the length of file name and pad. pad = (int)(fullInfoArray[i].NextEntryOffset - fixedSize); if (pad > 0) { channel.WriteBytes(new byte[pad]); } } channel.EndWriteGroup(); } } } else if (type == typeof(SMB_FIND_FILE_BOTH_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2[])) { SMB_FIND_FILE_BOTH_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2[] bothInfoArray = ( SMB_FIND_FILE_BOTH_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2[])this.trans2Data.Data; List <byte> buffer = new List <byte>(); for (int i = 0; i < bothInfoArray.Length; i++) { SMB_FIND_FILE_BOTH_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2 item = bothInfoArray[i]; // update the next entry offset. int len = CifsMessageUtils.GetSize <SMB_FIND_FILE_BOTH_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2>(item); item.NextEntryOffset = (uint)len; if (i + 1 < bothInfoArray.Length) { item.NextEntryOffset += (uint)CifsMessageUtils.CalculatePadLength(len, 8); } byte[] data = CifsMessageUtils.ToBytes <SMB_FIND_FILE_BOTH_DIRECTORY_INFO_OF_TRANS2_FIND_FIRST2>(item); buffer.AddRange(data); if (i + 1 < bothInfoArray.Length) { // 8 bytes align buffer.AddRange(new byte[CifsMessageUtils.CalculatePadLength(data.Length, 8)]); } } this.smbData.Trans2_Data = buffer.ToArray(); } else if (type == typeof(SMB_FIND_FILE_NAMES_INFO_OF_TRANS2_FIND_FIRST2[])) { SMB_FIND_FILE_NAMES_INFO_OF_TRANS2_FIND_FIRST2[] namesInfoArray = ( SMB_FIND_FILE_NAMES_INFO_OF_TRANS2_FIND_FIRST2[])this.trans2Data.Data; fixedSize = 12; // Including the fixed length of this structure. totalSize = fixedSize * namesInfoArray.Length; int pad = 0; for (int i = 0; i < namesInfoArray.Length; i++) { totalSize += (int)(namesInfoArray[i].FileNameLength); pad = (int)(namesInfoArray[i].NextEntryOffset - fixedSize - namesInfoArray[i].FileNameLength); if (pad > 0) { totalSize += pad; } } this.smbData.Trans2_Data = new byte[totalSize]; using (MemoryStream memoryStream = new MemoryStream(this.smbData.Trans2_Data)) { using (Channel channel = new Channel(null, memoryStream)) { channel.BeginWriteGroup(); for (int i = 0; i < namesInfoArray.Length; i++) { fixedSize = 12; // Including the fixed length of this structure. channel.Write <SMB_FIND_FILE_NAMES_INFO_OF_TRANS2_FIND_FIRST2>(namesInfoArray[i]); fixedSize += (int)namesInfoArray[i].FileNameLength; // Add the length of file name. pad = (int)(namesInfoArray[i].NextEntryOffset - fixedSize); if (pad > 0) { channel.WriteBytes(new byte[pad]); } } channel.EndWriteGroup(); } } } } else { this.smbData.Trans2_Data = new byte[0]; } }