Example #1
0
        public unsafe static byte[] GetStructureBytes(ValueType structure)
        {
            Type t = structure.GetType();

            if (t.IsPrimitive || t.IsLayoutSequential)
            {
                return(ExtractOperation.ValueStructureToBytes(structure));
            }

            byte[] b          = null;
            var    _structure = structure;

            if (structure is DateTime)
            {
                b          = new byte[8];
                _structure = ((DateTime)structure).ToBinary();
            }
            else if (structure is Enum)
            {
                b          = new byte[4];
                _structure = Convert.ToInt32((Enum)structure);
            }
            else
            {
                b = new byte[Marshal.SizeOf(_structure)];
            }


            fixed(byte *pb = b)
            Marshal.StructureToPtr(_structure, new IntPtr(pb), false);

            return(b);
        }
Example #2
0
        [Fact] public unsafe void Extractor_StructModel_Test()
        {
            StructModel[] structure = new StructModel[] { new StructModel(83948930), new StructModel(45453), new StructModel(5435332) };
            structure[0].Alias = "FirstAlias";
            structure[0].Name  = "FirstName";
            structure[1].Alias = "SecondAlia";
            structure[1].Name  = "SecondName";
            structure[2].Alias = "ThirdAlias";
            structure[2].Name  = "ThirdName";

            StructModels structures = new StructModels(structure);

            int size = Marshal.SizeOf(structure[0]);

            byte *pserial = ExtractOperation.ValueStructureToPointer(structure[0]);

            StructModel structure2 = new StructModel();
            ValueType   o          = structure2;

            ExtractOperation.PointerToValueStructure(pserial, o, 0);

            structure2 = (StructModel)o;

            structure2.Alias = "FirstChange";
        }
Example #3
0
        public unsafe static void StructureToPointer(object structure, IntPtr binary)
        {
            if (structure is ValueType)
            {
                Type t = structure.GetType();
                if (t.IsPrimitive || t.IsLayoutSequential)
                {
                    Marshal.StructureToPtr(structure, binary, false);
                    return;
                }

                if (structure is DateTime)
                {
                    structure = ((DateTime)structure).ToBinary();
                    Marshal.StructureToPtr(structure, binary, false);
                    return;
                }

                if (t.IsLayoutSequential)
                {
                    ExtractOperation.ValueStructureToPointer(structure, (byte *)binary, 0);
                    return;
                }
            }

            Marshal.StructureToPtr(structure, binary, false);
        }
Example #4
0
        public unsafe static Byte[] GetBytes(this Object objvalue)
        {
            Type t = objvalue.GetType();

            if (objvalue is ValueType)
            {
                if (t.IsPrimitive || t.IsLayoutSequential)
                {
                    return(ExtractOperation.ValueStructureToBytes(objvalue));
                }
                if (objvalue is DateTime)
                {
                    return(((DateTime)objvalue).ToBinary().GetBytes());
                }
                if (objvalue is Enum)
                {
                    return(Convert.ToInt32(objvalue).GetBytes());
                }
                return(objvalue.GetSequentialBytes());
            }
            if (t.IsLayoutSequential)
            {
                return(objvalue.GetSequentialBytes());
            }
            if (objvalue is IUnique)
            {
                return(((IUnique)objvalue).GetBytes());
            }
            if (objvalue is String || objvalue is IFormattable)
            {
                return(objvalue.ToString().GetBytes());
            }
            return(new byte[0]);
        }
Example #5
0
 public unsafe static object BytesToStructure(byte[] binary, object structure, long offset)
 {
     if (structure is ValueType)
     {
         return(ExtractOperation.BytesToValueStructure(binary, structure, 0));
     }
     else
     {
         fixed(byte *b = &binary[offset])
         return(PointerToStructure(new IntPtr(b), structure));
     }
 }
Example #6
0
        public unsafe static byte[] GetStructureBytes(object structure)
        {
            byte[] b          = null;
            object _structure = structure;

            if (_structure is string)
            {
                int l = ((string)_structure).Length;
                b = new byte[l];

                fixed(char *c = (string)_structure)
                fixed(byte *pb = b)
                CopyBlock(pb, (byte *)c, l);
            }

            if (structure is ValueType)
            {
                Type t = structure.GetType();
                if (t.IsPrimitive || t.IsLayoutSequential)
                {
                    return(ExtractOperation.ValueStructureToBytes(structure));
                }

                if (structure is DateTime)
                {
                    b          = new byte[8];
                    _structure = ((DateTime)_structure).ToBinary();
                }
                else if (structure is Enum)
                {
                    b         = new byte[4];
                    structure = Convert.ToInt32((Enum)structure);
                }
                else
                {
                    b = new byte[Marshal.SizeOf(_structure)];
                }
            }
            else
            {
                b = new byte[Marshal.SizeOf(_structure)];


                fixed(byte *pb = b)
                Marshal.StructureToPtr(_structure, new IntPtr(pb), false);

                return(b);
        }
Example #7
0
        public unsafe static byte *GetStructurePointer(object structure)
        {
            int size = 0;

            if (structure is ValueType)
            {
                Type t = structure.GetType();
                if (t.IsPrimitive || t.IsLayoutSequential)
                {
                    return(ExtractOperation.ValueStructureToPointer(structure));
                }

                if (structure is DateTime)
                {
                    size      = 8;
                    structure = ((DateTime)structure).ToBinary();
                }
                else if (structure is ISerialNumber)
                {
                    size = 24;
                }
                else if (structure is IUnique)
                {
                    size = 8;
                }
                else if (structure is Enum)
                {
                    size      = 4;
                    structure = Convert.ToInt32((Enum)structure);
                }
                else
                {
                    size = Marshal.SizeOf(structure);
                }
            }
            else
            {
                size = Marshal.SizeOf(structure);
            }

            IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(structure));

            Marshal.StructureToPtr(structure, p, false);

            return((byte *)p);
        }
Example #8
0
 public unsafe static object PointerToStructure(IntPtr binary, object structure)
 {
     if (structure is ValueType)
     {
         Type t = structure.GetType();
         if (t.IsPrimitive || t.IsLayoutSequential)
         {
             structure = ExtractOperation.PointerToValueStructure((byte *)binary, structure, 0);
         }
         else
         {
             structure = PointerToStructure(binary, structure.GetType(), 0);
         }
     }
     else
     {
         Marshal.PtrToStructure(binary, structure);
     }
     return(structure);
 }
Example #9
0
 public static unsafe void CopyBlock(byte *dest, byte *src, uint count)
 {
     ExtractOperation.CopyBlock(dest, 0, src, 0, count);
 }
Example #10
0
 public static unsafe void CopyBlock(byte *dest, ulong destOffset, byte *src, ulong srcOffset, ulong count)
 {
     ExtractOperation.CopyBlock(dest, destOffset, src, srcOffset, count);
 }
Example #11
0
 public static unsafe void CopyBlock(void *dest, void *src, ulong count)
 {
     ExtractOperation.CopyBlock((byte *)dest, 0, (byte *)src, 0, count);
 }
Example #12
0
 public static unsafe void Cpblk(IntPtr dest, IntPtr src, ulong destOffset, ulong count)
 {
     ExtractOperation.CopyBlock((byte *)(dest.ToPointer()), destOffset, (byte *)(src.ToPointer()), 0, count);
 }
Example #13
0
 public static unsafe void CopyBlock(void *dest, void *src, uint destOffset, uint count)
 {
     ExtractOperation.CopyBlock((byte *)dest, destOffset, (byte *)src, 0, count);
 }
Example #14
0
 public static unsafe void Cpblk(byte *dest, byte *src, uint destOffset, uint count)
 {
     ExtractOperation.CopyBlock(dest, destOffset, src, 0, count);
 }
Example #15
0
 public static unsafe void CopyBlock(IntPtr dest, long destOffset, IntPtr src, long srcOffset, long count)
 {
     ExtractOperation.CopyBlock((byte *)(dest.ToPointer()), (ulong)destOffset, (byte *)(src.ToPointer()), (ulong)srcOffset, (ulong)count);
 }
Example #16
0
 public static unsafe void CopyBlock(IntPtr dest, uint destOffset, IntPtr src, uint srcOffset, uint count)
 {
     ExtractOperation.CopyBlock((byte *)(dest.ToPointer()), destOffset, (byte *)(src.ToPointer()), srcOffset, count);
 }
Example #17
0
 public static unsafe void CopyBlock(void *dest, ulong destOffset, void *src, ulong srcOffset, ulong count)
 {
     ExtractOperation.CopyBlock((byte *)dest, destOffset, (byte *)src, srcOffset, count);
 }
Example #18
0
 public unsafe static ValueType PointerToStructure(IntPtr binary, ValueType structure)
 {
     return(ExtractOperation.PointerToValueStructure((byte *)binary, structure, 0));
 }
Example #19
0
 public static unsafe void CopyBlock(byte *dest, byte *src, long count)
 {
     ExtractOperation.CopyBlock(dest, 0, src, 0, (ulong)count);
 }
Example #20
0
 public unsafe static ValueType BytesToStructure(byte[] binary, ValueType structure, long offset)
 {
     return(ExtractOperation.BytesToValueStructure(binary, structure, 0));
 }
Example #21
0
 public static unsafe void CopyBlock(byte[] dest, uint destOffset, byte[] src, uint srcOffset, uint count)
 {
     ExtractOperation.CopyBlock(dest, destOffset, src, srcOffset, count);
 }
Example #22
0
 public static unsafe void CopyBlock(void *dest, void *src, long destOffset, long count)
 {
     ExtractOperation.CopyBlock((byte *)dest, (ulong)destOffset, (byte *)src, 0, (ulong)count);
 }
Example #23
0
 public static unsafe void CopyBlock(byte[] dest, byte[] src, ulong count)
 {
     ExtractOperation.CopyBlock(dest, 0, src, 0, count);
 }
Example #24
0
 public static unsafe void CopyBlock(byte[] dest, byte[] src, int destOffset, int count)
 {
     ExtractOperation.CopyBlock(dest, (uint)destOffset, src, 0, (uint)count);
 }
Example #25
0
 public static unsafe void CopyBlock(IntPtr dest, IntPtr src, int destOffset, int count)
 {
     ExtractOperation.CopyBlock((byte *)src.ToPointer(), (uint)destOffset, (byte *)dest.ToPointer(), 0, (uint)count);
 }
Example #26
0
 public static unsafe void CopyBlock(byte[] dest, long destOffset, byte[] src, long srcOffset, long count)
 {
     ExtractOperation.CopyBlock(dest, (ulong)destOffset, src, (ulong)srcOffset, (ulong)count);
 }