예제 #1
0
        [Fact] public unsafe void Extractor_CopyBlock_ByteArray_UInt_Test()
        {
            Random r = new Random();

            r.NextBytes(source);
            dest.Initialize();

            Extractor.CopyBlock(dest, 0, source, 0, source.Length);
            bool equal = dest.BlockEqual(source);

            Assert.True(equal);
        }
예제 #2
0
        /// <summary>
        /// The GetBytes.
        /// </summary>
        /// <param name="objvalue">The objvalue<see cref="ulong[]"/>.</param>
        /// <returns>The <see cref="Byte[]"/>.</returns>
        public unsafe static Byte[] GetBytes(this ulong[] objvalue)
        {
            int l = objvalue.Length * 8;

            byte[] b = new byte[l];

            fixed(byte *bp = b)
            fixed(ulong *lp = objvalue)
            Extractor.CopyBlock(bp, (byte *)lp, l);

            return(b);
        }
예제 #3
0
        [Fact] public unsafe void Extractor_CopyBlock_Pointer_UInt_Test()
        {
            Random r = new Random();

            r.NextBytes(source);
            dest.Initialize();

            fixed(byte *psrc = source, pdst = dest)
            {
                Extractor.CopyBlock(pdst, 0, psrc, 0, source.Length);
                bool equal = dest.BlockEqual(source);

                Assert.True(equal);
            }
        }
예제 #4
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);
        }
예제 #5
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);
        }