Esempio n. 1
0
        public unsafe static Byte[] GetBytes(this IList objvalue)
        {
            int  l      = objvalue.Count;
            long offset = 0;

            byte[] b = new byte[512];
            fixed(byte *pb = &b[0])
            {
                for (int i = 0; i < l; i++)
                {
                    long   s = 0;
                    object o = objvalue[i];
                    if (o is string)
                    {
                        s = ((string)o).Length * sizeof(char);
                        if ((s + offset) > b.Length)
                            Array.Resize(ref b, (int)(s + offset));

                        fixed(char *c = (string)o)
                        Extractor.Cpblk(pb, (uint)offset, (byte *)c, 0, (uint)s);
                    }
                    else
                    {
                        s = o.GetSize();
                        Extractor.StructureToPointer(o, pb + offset);
                    }
                    offset += s;
                }
            }

            return(b);
        }
Esempio n. 2
0
        /// <summary>
        /// The GetBytes.
        /// </summary>
        /// <param name="objvalue">The objvalue<see cref="IList"/>.</param>
        /// <param name="buffer">The buffer<see cref="byte*"/>.</param>
        /// <param name="length">The length<see cref="int"/>.</param>
        /// <param name="forKeys">The forKeys<see cref="bool"/>.</param>
        /// <returns>The <see cref="int"/>.</returns>
        public unsafe static int GetBytes(this IList objvalue, ref byte *buffer, int length, bool forKeys = false)
        {
            int offset   = 0;
            int charsize = sizeof(char);
            //  int count = objvalue.Count;
            int s;

            foreach (var o in objvalue)
            {
                s = 0;
                // object o = objvalue[i];
                if (o is string)
                {
                    s = ((string)o).Length * charsize;
                    int fs = (s + offset);
                    if (fs > length)
                    {
                        byte *_buffer = stackalloc byte[fs];
                        Extractor.CopyBlock(_buffer, buffer, offset);
                        buffer = _buffer;
                        length = fs;
                    }

                    fixed(char *c = (string)o)
                    Extractor.CopyBlock(buffer, (byte *)c, offset, s);
                }
                else
                {
                    if (forKeys && o is IUnique)
                    {
                        s = 8;
                        *((ulong *)(buffer + offset)) = ((IUnique)o).UniqueKey;
                    }
                    else
                    {
                        s = o.GetSize();
                        Extractor.StructureToPointer(o, buffer + offset);
                    }
                }
                offset += s;
            }

            return(offset);
        }
Esempio n. 3
0
 public unsafe static void StructureTo(this object structure, IntPtr binary)
 {
     Extractor.StructureToPointer(structure, binary);
 }
Esempio n. 4
0
        /// <summary>
        /// The GetBytes.
        /// </summary>
        /// <param name="obj">The obj<see cref="IList"/>.</param>
        /// <param name="forKeys">The forKeys<see cref="bool"/>.</param>
        /// <returns>The <see cref="Byte[]"/>.</returns>
        public unsafe static Byte[] GetBytes(this IList obj, bool forKeys = false)
        {
            int   length = 256, offset = 0, postoffset = 0, count = obj.Count, charsize = sizeof(char), s = 0;
            byte *buffer   = stackalloc byte[length];
            bool  toResize = false;

            for (int i = 0; i < count; i++)
            {
                object o = obj[i];
                if (o is string)
                {
                    string str = ((string)o);
                    s          = str.Length * charsize;
                    postoffset = (s + offset);

                    if (postoffset > length)
                    {
                        toResize = true;
                    }
                    else
                        fixed(char *c = str)
                        Extractor.CopyBlock(buffer, (byte *)c, offset, s);
                }
                else
                {
                    if (o is IUnique)
                    {
                        s          = 8;
                        postoffset = (s + offset);

                        if (postoffset > length)
                        {
                            toResize = true;
                        }
                        else
                        {
                            *((ulong *)(buffer + offset)) = ((IUnique)o).UniqueKey;
                        }
                    }
                    else
                    {
                        s          = o.GetSize();
                        postoffset = (s + offset);

                        if (postoffset > length)
                        {
                            toResize = true;
                        }
                        else
                        {
                            Extractor.StructureToPointer(o, new IntPtr(buffer + offset));
                        }
                    }
                }

                if (toResize)
                {
                    i--;
                    toResize = false;
                    byte *_buffer = stackalloc byte[postoffset];
                    Extractor.CopyBlock(_buffer, buffer, offset);
                    buffer = _buffer;
                    length = postoffset;
                }
                else
                {
                    offset = postoffset;
                }
            }

            byte[] result = new byte[offset];
            fixed(byte *result_p = result)
            {
                Extractor.CopyBlock(result_p, buffer, offset);
            }

            return(result);
        }