123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Text;
- using Utilities;
- namespace SMBLibrary.RPC
- {
-
-
-
-
- public class NDRWriter
- {
- private MemoryStream m_stream = new MemoryStream();
- private int m_depth;
- private List<INDRStructure> m_deferredStructures = new List<INDRStructure>();
- private Dictionary<uint, INDRStructure> m_referentToInstance = new Dictionary<uint, INDRStructure>();
- private uint m_nextReferentID = 0x00020000;
- public void BeginStructure()
- {
- m_depth++;
- }
-
-
-
- private void AddDeferredStructure(INDRStructure structure)
- {
- m_deferredStructures.Add(structure);
- }
- public void EndStructure()
- {
- m_depth--;
-
-
-
-
-
-
- if (m_depth == 0)
- {
-
-
- List<INDRStructure> deferredStructures = new List<INDRStructure>(m_deferredStructures);
- m_deferredStructures.Clear();
-
- foreach (INDRStructure deferredStructure in deferredStructures)
- {
- deferredStructure.Write(this);
- }
- }
- }
- public void WriteUnicodeString(string value)
- {
- NDRUnicodeString unicodeString = new NDRUnicodeString(value);
- unicodeString.Write(this);
- }
- public void WriteStructure(INDRStructure structure)
- {
- structure.Write(this);
- }
- public void WriteTopLevelUnicodeStringPointer(string value)
- {
- if (value == null)
- {
- WriteUInt32(0);
- return;
- }
-
- uint referentID = GetNextReferentID();
- WriteUInt32(referentID);
- NDRUnicodeString unicodeString = new NDRUnicodeString(value);
- unicodeString.Write(this);
- m_referentToInstance.Add(referentID, unicodeString);
- }
-
- public void WriteEmbeddedStructureFullPointer(INDRStructure structure)
- {
- if (structure == null)
- {
- WriteUInt32(0);
- return;
- }
- else
- {
-
- uint referentID = GetNextReferentID();
- WriteUInt32(referentID);
- AddDeferredStructure(structure);
- m_referentToInstance.Add(referentID, structure);
- }
- }
-
- public void WriteUInt16(ushort value)
- {
- uint padding = (uint)(2 - (m_stream.Position % 2)) % 2;
- m_stream.Position += padding;
- LittleEndianWriter.WriteUInt16(m_stream, value);
- }
-
- public void WriteUInt32(uint value)
- {
- uint padding = (uint)(4 - (m_stream.Position % 4)) % 4;
- m_stream.Position += padding;
- LittleEndianWriter.WriteUInt32(m_stream, value);
- }
- public byte[] GetBytes()
- {
- byte[] buffer = new byte[m_stream.Length];
- m_stream.Seek(0, SeekOrigin.Begin);
- m_stream.Read(buffer, 0, buffer.Length);
- return buffer;
- }
- private uint GetNextReferentID()
- {
- uint result = m_nextReferentID;
- m_nextReferentID++;
- return result;
- }
- }
- }
|