Hybrids packet builder

02/16/2011 00:58 Arco.#1
Alright so hybrids packet builder;
It has 5 functions, each doing these:

Writing a UInt16
Writing a UInt32
Writing a UInt64
Writing a String
Writing a String with Length

Now I made this function, and I'm wanting it to be universal, used to do all of the above. Would this work?

Code:
 public static void WriteObject(Object Arg, Byte[] Buffer, UInt16 Offset, Boolean stringLength = false)
        {
            #region String function
            if (Arg.GetType() == typeof(String))
            {
                #region Write string to array of data
                if (!stringLength)
                {
                    ushort i = 0;
                    while (i < Convert.ToString(Arg).Length)
                    {
                        Buffer[(ushort)(i + Offset)] = (byte)Convert.ToString(Arg)[i];
                        i = (ushort)(i + 1);
                    }
                }
                #endregion

                #region Write string with length
                else
                {
                    Buffer[Offset] = (byte)Convert.ToString(Arg).Length;
                    Offset++;
                    ushort x = 0;
                    while (x < Convert.ToString(Arg).Length)
                    {
                        Buffer[(ushort)(x + Offset)] = (byte)Convert.ToString(Arg)[x];
                        x = (ushort)(x + 1);
                    }
                }
                #endregion
            } 
            #endregion

            #region Writing 2 bytes of data
            if (Arg.GetType() == typeof(UInt16))
            {
                Buffer[Offset] = (byte)(Arg);
                Buffer[Offset + 1] = (byte)(Convert.ToUInt16(Arg) >> 8);
            }
            if (Arg.GetType() == typeof(Int16))
            {
                Buffer[Offset] = (byte)(Arg);
                Buffer[Offset + 1] = (byte)(Convert.ToUInt16(Arg) >> 8);
            }
            #endregion

            #region Writing 4 bytes of data
            if (Arg.GetType() == typeof(Int32))
            {
                Buffer[Offset] = (byte)(Arg);
                Buffer[Offset + 1] = (byte)(Convert.ToInt32(Arg) >> 8);
                Buffer[Offset + 2] = (byte)(Convert.ToInt32(Arg) >> 16);
                Buffer[Offset + 3] = (byte)(Convert.ToInt32(Arg) >> 24);
            }
            if (Arg.GetType() == typeof(UInt32))
            {
                Buffer[Offset] = (byte)(Arg);
                Buffer[Offset + 1] = (byte)(Convert.ToUInt32(Arg) >> 8);
                Buffer[Offset + 2] = (byte)(Convert.ToUInt32(Arg) >> 16);
                Buffer[Offset + 3] = (byte)(Convert.ToUInt32(Arg) >> 24);
            } 
            #endregion

            #region Writing 8 bytes of data
            if (Arg.GetType() == typeof(Int64))
            {
                Buffer[Offset] = (byte)(Arg);
                Buffer[Offset + 1] = (byte)(Convert.ToInt64(Arg) >> 8);
                Buffer[Offset + 2] = (byte)(Convert.ToInt64(Arg) >> 16);
                Buffer[Offset + 3] = (byte)(Convert.ToInt64(Arg) >> 24);
                Buffer[Offset + 4] = (byte)(Convert.ToInt64(Arg) >> 32);
                Buffer[Offset + 5] = (byte)(Convert.ToInt64(Arg) >> 40);
                Buffer[Offset + 6] = (byte)(Convert.ToInt64(Arg) >> 48);
                Buffer[Offset + 7] = (byte)(Convert.ToInt64(Arg) >> 56);
            }
            if (Arg.GetType() == typeof(UInt64))
            {
                Buffer[Offset] = (byte)(Arg);
                Buffer[Offset + 1] = (byte)(Convert.ToUInt64(Arg) >> 8);
                Buffer[Offset + 2] = (byte)(Convert.ToUInt64(Arg) >> 16);
                Buffer[Offset + 3] = (byte)(Convert.ToUInt64(Arg) >> 24);
                Buffer[Offset + 4] = (byte)(Convert.ToUInt64(Arg) >> 32);
                Buffer[Offset + 5] = (byte)(Convert.ToUInt64(Arg) >> 40);
                Buffer[Offset + 6] = (byte)(Convert.ToUInt64(Arg) >> 48);
                Buffer[Offset + 7] = (byte)(Convert.ToUInt64(Arg) >> 56);
            } 
            #endregion
        }
02/16/2011 01:03 .Beatz#2
Looks damn sexy!
02/16/2011 01:03 Ian*#3
hi arco
02/16/2011 01:25 Korvacs#4
It would, but using pointers you could do that in like 1/8th of the code...
02/16/2011 01:37 Arco.#5
Quote:
Originally Posted by Korvacs View Post
It would, but using pointers you could do that in like 1/8th of the code...
I've NEVER used pointers, so mind elaborating on that please :D
02/16/2011 02:26 .Beatz#6
Quote:
Originally Posted by Yup Arco View Post
I've NEVER used pointers, so mind elaborating on that please :D
Don't know if this helps

I used google ;)

[Only registered and activated users can see links. Click Here To Register...]
02/16/2011 02:50 .Guru#7
use c++ you rat
02/16/2011 04:39 CptSky#8
Quote:
Originally Posted by Yup Arco View Post
Alright so hybrids packet builder;
It has 5 functions, each doing these:

Writing a UInt16
Writing a UInt32
Writing a UInt64
Writing a String
Writing a String with Length

Now I made this function, and I'm wanting it to be universal, used to do all of the above. Would this work?

Can be optimized... Here a rapid version. It should works, but I can't test.

Code:
        public static void WriteObject(Object Arg, Byte[] Buffer, UInt16 Offset, Boolean stringLength)
        {
            Type Type = Arg.GetType();
            Int32 Length = 0;

            if (Type == typeof(String))
                Length = (Arg as String).Length;
            else if (Type == typeof(Byte[]))
                Length = (Arg as Byte[]).Length;
            else if (Type == typeof(SByte) || Type == typeof(Byte))
                Length = 1;
            else if (Type == typeof(Int16) || Type == typeof(UInt16))
                Length = 2;
            else if (Type == typeof(Int32) || Type == typeof(UInt32))
                Length = 4;
            else if (Type == typeof(Int64) || Type == typeof(UInt64))
                Length = 8;

            if (Length < 1)
                return;

            Int32 Pos = Offset;
            if (Type == typeof(String) && stringLength)
            {
                Buffer[Pos] = (Byte)Length;
                Pos++;
            }

                if (Type == typeof(String))
                {
                    String Value = (Arg as String);
                    for (Int32 i = 0; i < Length; i++)
                    {
                        Buffer[Pos] = (Byte)Value[i];
                        Pos++;
                    }
                }
                else if (Type == typeof(Byte[]))
                {
                    Byte[] Value = (Arg as Byte[]);
                    for (Int32 i = 0; i < Length; i++)
                    {
                        Buffer[Pos] = (Byte)Value[i];
                        Pos++;
                    }
                }
                else
                {
                    UInt64 Value = Convert.ToUInt64(Arg);
                    for (Int32 i = 0; i < Length; i++)
                    {
                        Buffer[Pos] = (Byte)(Value >> (8 * i));
                        Pos++;
                    }
                }
        }
02/16/2011 05:12 _tao4229_#9
Code:
public struct PasswordSeedPacket
{
    public ushort Size;
    public ushort Type;
    public uint Seed;
    public PasswordSeedPacket(uint s)
    {
        Seed = s;
        Size = 8;
        Type = 0x423;
    }
}

// ...
{
    // ...
    PasswordSeedPacket seed = new PasswordSeedPacket(rand(..));
    socket.send(&seed, seed.Size [,...]);
}
02/16/2011 11:28 -impulse-#10
Quote:
Originally Posted by Korvacs View Post
It would, but using pointers you could do that in like 1/8th of the code...
Code:
        public static unsafe void WriteObject(Object Arg, Byte[] Buffer, UInt16 Offset, Boolean stringLength = false)
        {
            fixed (byte* buffer = Buffer)
            {
                #region String function
                if (Arg is String)
                {
                    #region Write string to array of data
                    if (!stringLength)
                    {
                        ushort i = 0;
                        string Argument = Convert.ToString(Arg);
                        while (i < Argument.Length)
                        {
                            *((byte*)(buffer + Offset + i)) = (byte)Argument[i];
                            i++;
                        }
                    }
                    #endregion

                    #region Write string with length
                    else
                    {
                        string Argument = Convert.ToString(Arg);
                        *((byte*)(buffer + Offset)) = (byte)Argument.Length;
                        ushort i = 1;
                        while (i < Argument.Length)
                        {
                            *((byte*)(buffer + Offset + i)) = (byte)Argument[i];
                            i++;
                        }
                    }
                    #endregion
                }
                #endregion

                #region Writing 1 byte of data
                if (Arg is Byte)
                    *((Byte*)(buffer + Offset)) = Arg;
                if (Arg is SByte)
                    *((SByte*)(buffer + Offset)) = Arg;
                #endregion

                #region Writing 2 bytes of data
                if (Arg is Int16)
                    *((Int16*)(buffer + Offset)) = Arg;
                if (Arg is UInt16)
                    *((UInt16*)(buffer + Offset)) = Arg;
                #endregion

                #region Writing 4 bytes of data
                if (Arg is Int32)
                    *((Int32*)(buffer + Offset)) = Arg;
                if (Arg is UInt32)
                    *((UInt32*)(buffer + Offset)) = Arg;
                #endregion

                #region Writing 8 bytes of data
                if (Arg is Int64)
                    *((Int64*)(buffer + Offset)) = Arg;
                if (Arg is UInt64)
                    *((UInt64*)(buffer + Offset)) = Arg;
                #endregion
            }
        }
02/16/2011 11:34 Korvacs#11
Put some returns in there, otherwise all of the cpu time you save by using pointers is wasted by performing all of those if statements. *nods*
02/16/2011 17:22 tanelipe#12
I suppose this would work also.

PHP Code:
static void Write<T>(byte[] Bufferint OffsetT Valuebool IncludeLength true)
        {
            if (
typeof(T) == typeof(string))
            {
                
string StringRepresentation Convert.ToString(Value);
                
byte Size = (byte)StringRepresentation.Length;
                if (
IncludeLength)
                {
                    
Buffer[Offset++] = Size;
                }
                for (
byte i 0Sizei++)
                {
                    
Buffer[Offset i] = (byte)StringRepresentation[i];
                }
            }
            else
            {
                
int Size Marshal.SizeOf(Value);
                
IntPtr Pointer Marshal.AllocHGlobal(Size);
                try
                {
                    
Marshal.StructureToPtr(ValuePointerfalse);
                    
Marshal.Copy(PointerBufferOffsetSize);
                }
                
finally
                
{
                    
Marshal.FreeHGlobal(Pointer);
                }
            }
        }