{"id":116,"date":"2020-07-27T04:36:56","date_gmt":"2020-07-27T04:36:56","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=116"},"modified":"2020-07-27T04:36:56","modified_gmt":"2020-07-27T04:36:56","slug":"miscbyte-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/07\/27\/miscbyte-cs\/","title":{"rendered":"MiscByte.cs"},"content":{"rendered":"\n<p>Collection of Miscellaneous Byte Helper Functions<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public static class MiscByte\n{\n public static byte[] Add(this byte[] left, byte[] right)\n        {\n            var l1 = left.Length;\n            var l2 = right.Length;\n\n            if (l1 > 0 &amp;&amp; l2 > 0)\n            {\n                var ret = new byte[l1 + l2];\n                Buffer.BlockCopy(left, 0, ret, 0, l1);\n                Buffer.BlockCopy(right, 0, ret, l1, l2);\n                return ret;\n            }\n\n            if (l1 > 0 &amp;&amp; l2 == 0)\n                return left;\n\n            if (l2 > 0 &amp;&amp; l1 == 0)\n                return right;\n\n            return new byte[0];\n        }\n\n        public static byte[] XOR(this byte[] left, byte[] right)\n        {\n            var l1 = left.Length;\n            var l2 = right.Length;\n\n            if (l1 != l2)\n                throw new Exception(\"Error: left and right arrays lengths must be equal.\");\n\n            var ret = new byte[l1];\n\n            for (var i = 0; i &lt; l1; ++i)\n                ret[i] = (byte)((left[i] ^ right[i]) &amp; 0xff);\n\n            return ret;\n        }\n\n        public static byte[] OR(this byte[] left, byte[] right)\n        {\n            var l1 = left.Length;\n            var l2 = right.Length;\n\n            if (l1 != l2)\n                throw new Exception(\"Error: left and right arrays lengths must be equal.\");\n\n            var ret = new byte[l1];\n\n            for (var i = 0; i &lt; l1; ++i)\n                ret[i] = (byte)((left[i] | right[i]) &amp; 0xff);\n\n            return ret;\n        }\n\n        public static byte[] AND(this byte[] left, byte[] right)\n        {\n            var l1 = left.Length;\n            var l2 = right.Length;\n\n            if (l1 != l2)\n                throw new Exception(\"Error: left and right arrays lengths must be equal.\");\n\n            var ret = new byte[l1];\n\n            for (var i = 0; i &lt; l1; ++i)\n                ret[i] = (byte)(left[i] &amp; right[i] &amp; 0xff);\n\n            return ret;\n        }\n\n        public static byte[] NOT(this byte[] left)\n        {\n            var l1 = left.Length;\n            var ret = new byte[l1];\n\n            for (var i = 0; i &lt; l1; ++i)\n                ret[i] = (byte)((ushort)~left[i] &amp; 0xff);\n\n            return ret;\n        }\n\n        [SecuritySafeCritical]\n        public static unsafe byte[] Clone(this byte[] a1)\n        {\n            if (a1 == null)\n                return null;\n\n            var a2 = new byte[a1.Length];\n\n            fixed (byte* p1 = a1, p2 = a2)\n            {\n                var Len = a1.Length;\n                byte* x1 = p1, x2 = p2;\n\n                while (Len > 7)\n                {\n                    *(long*)x2 = *(long*)x1;\n                    x1 += 8;\n                    x2 += 8;\n                    Len -= 8;\n                }\n\n                switch (Len % 8)\n                {\n                    case 0:\n                        break;\n                    case 7:\n                        *(int*)x2 = *(int*)x1;\n                        x1 += 4;\n                        x2 += 4;\n                        *(short*)x2 = *(short*)x1;\n                        x1 += 2;\n                        x2 += 2;\n                        *x2 = *x1;\n                        break;\n                    case 6:\n                        *(int*)x2 = *(int*)x1;\n                        x1 += 4;\n                        x2 += 4;\n                        *(short*)x2 = *(short*)x1;\n                        break;\n                    case 5:\n                        *(int*)x2 = *(int*)x1;\n                        x1 += 4;\n                        x2 += 4;\n                        *x2 = *x1;\n                        break;\n                    case 4:\n                        *(int*)x2 = *(int*)x1;\n                        break;\n                    case 3:\n                        *(short*)x2 = *(short*)x1;\n                        x1 += 2;\n                        x2 += 2;\n                        *x2 = *x1;\n                        break;\n                    case 2:\n                        *(short*)x2 = *(short*)x1;\n                        break;\n                    case 1:\n                        *x2 = *x1;\n                        break;\n                }\n\n                return a2;\n            }\n        }\n\n        [SecuritySafeCritical]\n        public static unsafe bool Copy(this byte[] a1, int aindex, byte[] a2, int bindex, int length)\n        {\n            if (a1 == null || a2 == null)\n                return false;\n\n            fixed (byte* p1 = a1, p2 = a2)\n            {\n                var Len = length;\n                byte* x1 = p1 + aindex, x2 = p2 + bindex;\n\n                while (Len > 7)\n                {\n                    *(long*)x2 = *(long*)x1;\n                    x1 += 8;\n                    x2 += 8;\n                    Len -= 8;\n                }\n\n                switch (Len % 8)\n                {\n                    case 0:\n                        break;\n                    case 7:\n                        *(int*)x2 = *(int*)x1;\n                        x1 += 4;\n                        x2 += 4;\n                        *(short*)x2 = *(short*)x1;\n                        x1 += 2;\n                        x2 += 2;\n                        *x2 = *x1;\n                        break;\n                    case 6:\n                        *(int*)x2 = *(int*)x1;\n                        x1 += 4;\n                        x2 += 4;\n                        *(short*)x2 = *(short*)x1;\n                        break;\n                    case 5:\n                        *(int*)x2 = *(int*)x1;\n                        x1 += 4;\n                        x2 += 4;\n                        *x2 = *x1;\n                        break;\n                    case 4:\n                        *(int*)x2 = *(int*)x1;\n                        break;\n                    case 3:\n                        *(short*)x2 = *(short*)x1;\n                        x1 += 2;\n                        x2 += 2;\n                        *x2 = *x1;\n                        break;\n                    case 2:\n                        *(short*)x2 = *(short*)x1;\n                        break;\n                    case 1:\n                        *x2 = *x1;\n                        break;\n                }\n\n                return true;\n            }\n        }\n        \n        [SecuritySafeCritical]\n        public static unsafe byte[] SubByte(this byte[] a1, int aindex, int length)\n        {\n            if (a1 == null)\n                return null;\n\n            if (aindex + length > a1.Length)\n                throw new Exception(\"Error: SubByte - index + length exceed source array length.\");\n\n            var a2 = new byte[length];\n\n            fixed (byte* p1 = a1, p2 = a2)\n            {\n                var Len = length;\n                byte* x1 = p1 + aindex, x2 = p2;\n\n                while (Len > 7)\n                {\n                    *(long*)x2 = *(long*)x1;\n                    x1 += 8;\n                    x2 += 8;\n                    Len -= 8;\n                }\n\n                switch (Len % 8)\n                {\n                    case 0:\n                        break;\n                    case 7:\n                        *(int*)x2 = *(int*)x1;\n                        x1 += 4;\n                        x2 += 4;\n                        *(short*)x2 = *(short*)x1;\n                        x1 += 2;\n                        x2 += 2;\n                        *x2 = *x1;\n                        break;\n                    case 6:\n                        *(int*)x2 = *(int*)x1;\n                        x1 += 4;\n                        x2 += 4;\n                        *(short*)x2 = *(short*)x1;\n                        break;\n                    case 5:\n                        *(int*)x2 = *(int*)x1;\n                        x1 += 4;\n                        x2 += 4;\n                        *x2 = *x1;\n                        break;\n                    case 4:\n                        *(int*)x2 = *(int*)x1;\n                        break;\n                    case 3:\n                        *(short*)x2 = *(short*)x1;\n                        x1 += 2;\n                        x2 += 2;\n                        *x2 = *x1;\n                        break;\n                    case 2:\n                        *(short*)x2 = *(short*)x1;\n                        break;\n                    case 1:\n                        *x2 = *x1;\n                        break;\n                }\n\n                return a2;\n            }\n        }\n\n       [SecuritySafeCritical]\n        public static byte[] CloneTo(this byte[] a1)\n        {\n            var a1l = a1.Length;\n            var copy = new byte[a1l];\n            a1.Copy(copy);\n            return copy;\n        }\n\n        [SecuritySafeCritical]\n        public static unsafe bool Copy(this byte[] a1, byte[] a2)\n        {\n            if (a1 == null || a2 == null || a1.Length != a2.Length)\n                return false;\n\n            fixed (byte* p1 = a1, p2 = a2)\n            {\n                var Len = a1.Length;\n                byte* x1 = p1, x2 = p2;\n\n                while (Len > 7)\n                {\n                    *(long*)x2 = *(long*)x1;\n                    x1 += 8;\n                    x2 += 8;\n                    Len -= 8;\n                }\n\n                switch (Len % 8)\n                {\n                    case 0:\n                        break;\n                    case 7:\n                        *(int*)x2 = *(int*)x1;\n                        x1 += 4;\n                        x2 += 4;\n                        *(short*)x2 = *(short*)x1;\n                        x1 += 2;\n                        x2 += 2;\n                        *x2 = *x1;\n                        break;\n                    case 6:\n                        *(int*)x2 = *(int*)x1;\n                        x1 += 4;\n                        x2 += 4;\n                        *(short*)x2 = *(short*)x1;\n                        break;\n                    case 5:\n                        *(int*)x2 = *(int*)x1;\n                        x1 += 4;\n                        x2 += 4;\n                        *x2 = *x1;\n                        break;\n                    case 4:\n                        *(int*)x2 = *(int*)x1;\n                        break;\n                    case 3:\n                        *(short*)x2 = *(short*)x1;\n                        x1 += 2;\n                        x2 += 2;\n                        *x2 = *x1;\n                        break;\n                    case 2:\n                        *(short*)x2 = *(short*)x1;\n                        break;\n                    case 1:\n                        *x2 = *x1;\n                        break;\n                }\n\n                return true;\n            }\n        }\n\n        public static bool Compare(this short[] a1, short[] a2)\n        {\n            var b1 = a1.GetBytes();\n            var b2 = a2.GetBytes();\n\n            return b1.Compare(b2);\n        }\n\n        public static bool Compare(this byte[] a1, short[] a2)\n        {\n            var b2 = a2.GetBytes();\n\n            return a1.Compare(b2);\n        }\n\n        public static bool Compare(this ushort[] a1, ushort[] a2)\n        {\n            var b1 = a1.GetBytes();\n            var b2 = a2.GetBytes();\n\n            return b1.Compare(b2);\n        }\n\n        public static bool Compare(this int[] a1, int[] a2)\n        {\n            var b1 = a1.GetBytes();\n            var b2 = a2.GetBytes();\n\n            return b1.Compare(b2);\n        }\n\n        public static bool Compare(this uint[] a1, uint[] a2)\n        {\n            var b1 = a1.GetBytes();\n            var b2 = a2.GetBytes();\n\n            return b1.Compare(b2);\n        }\n\n        public static bool Compare(this long[] a1, long[] a2)\n        {\n            var b1 = a1.GetBytes();\n            var b2 = a2.GetBytes();\n\n            return b1.Compare(b2);\n        }\n\n        public static bool Compare(this ulong[] a1, ulong[] a2)\n        {\n            var b1 = a1.GetBytes();\n            var b2 = a2.GetBytes();\n\n            return b1.Compare(b2);\n        }\n\n        public static bool Compare(this double[] a1, double[] a2)\n        {\n            var b1 = a1.GetBytes();\n            var b2 = a2.GetBytes();\n\n            return b1.Compare(b2);\n        }\n\n        public static bool Compare(this float[] a1, float[] a2)\n        {\n            var b1 = a1.GetBytes();\n            var b2 = a2.GetBytes();\n\n            return b1.Compare(b2);\n        }\n\n        [SecuritySafeCritical]\n        public static unsafe bool Compare(this byte[] a1, byte[] a2)\n        {\n            if (a1 == null &amp;&amp; a2 == null)\n                return true;\n\n            if (a1 == null || a2 == null || a1.Length != a2.Length)\n                return false;\n\n            fixed (byte* p1 = a1, p2 = a2)\n            {\n                var Len = a1.Length;\n                byte* x1 = p1, x2 = p2;\n\n                while (Len > 7)\n                {\n                    if (*(long*)x2 != *(long*)x1)\n                        return false;\n                    x1 += 8;\n                    x2 += 8;\n                    Len -= 8;\n                }\n\n                switch (Len % 8)\n                {\n                    case 0:\n                        break;\n                    case 7:\n                        if (*(int*)x2 != *(int*)x1)\n                            return false;\n                        x1 += 4;\n                        x2 += 4;\n                        if (*(short*)x2 != *(short*)x1)\n                            return false;\n                        x1 += 2;\n                        x2 += 2;\n                        if (*x2 != *x1)\n                            return false;\n                        break;\n                    case 6:\n                        if (*(int*)x2 != *(int*)x1)\n                            return false;\n                        x1 += 4;\n                        x2 += 4;\n                        if (*(short*)x2 != *(short*)x1)\n                            return false;\n                        break;\n                    case 5:\n                        if (*(int*)x2 != *(int*)x1)\n                            return false;\n                        x1 += 4;\n                        x2 += 4;\n                        if (*x2 != *x1)\n                            return false;\n                        break;\n                    case 4:\n                        if (*(int*)x2 != *(int*)x1)\n                            return false;\n                        break;\n                    case 3:\n                        if (*(short*)x2 != *(short*)x1)\n                            return false;\n                        x1 += 2;\n                        x2 += 2;\n                        if (*x2 != *x1)\n                            return false;\n                        break;\n                    case 2:\n                        if (*(short*)x2 != *(short*)x1)\n                            return false;\n                        break;\n                    case 1:\n                        if (*x2 != *x1)\n                            return false;\n                        break;\n                }\n\n                return true;\n            }\n        }\n\n        [SecuritySafeCritical]\n        public static unsafe bool Fill(this byte[] a1, byte b1)\n        {\n            if (a1 == null)\n                return false;\n\n            byte[] fbl = { b1, b1, b1, b1, b1, b1, b1, b1 };\n            byte[] fbi = { b1, b1, b1, b1 };\n            byte[] fbs = { b1, b1 };\n            byte[] fbb = { b1 };\n\n            fixed (byte* p1 = a1, p8 = fbl, p4 = fbi, p2 = fbs, p0 = fbb)\n            {\n                var Len = a1.Length;\n                var x1 = p1;\n\n                while (Len > 7)\n                {\n                    *(long*)x1 = *(long*)p8;\n                    x1 += 8;\n                    Len -= 8;\n                }\n\n                switch (Len % 8)\n                {\n                    case 0:\n                        break;\n                    case 7:\n                        *(int*)x1 = *(int*)p4;\n                        x1 += 4;\n                        *(short*)x1 = *(short*)p2;\n                        x1 += 2;\n                        *x1 = *p0;\n                        break;\n                    case 6:\n                        *(int*)x1 = *(int*)p4;\n                        x1 += 4;\n                        *(short*)x1 = *(short*)p2;\n                        break;\n                    case 5:\n                        *(int*)x1 = *(int*)p4;\n                        x1 += 4;\n                        *x1 = *p0;\n                        break;\n                    case 4:\n                        *(int*)x1 = *(int*)p4;\n                        break;\n                    case 3:\n                        *(short*)x1 = *(short*)p2;\n                        x1 += 2;\n                        *x1 = *p0;\n                        break;\n                    case 2:\n                        *(short*)x1 = *(short*)p2;\n                        break;\n                    case 1:\n                        *x1 = *p0;\n                        break;\n                }\n\n                return true;\n            }\n        }\n\n        [SecuritySafeCritical]\n        public static unsafe bool Fill(this int[] a1, int i1)\n        {\n            if (a1 == null)\n                return false;\n\n            int[] fbl = { i1, i1 };\n            int[] fbi = { i1 };\n\n            fixed (int* p1 = a1, p8 = fbl, p4 = fbi)\n            {\n                var Len = a1.Length;\n                var x1 = p1;\n\n                while (Len > 1)\n                {\n                    *(long*)x1 = *(long*)p8;\n                    x1 += 2;\n                    Len -= 2;\n                }\n\n                if (Len == 1)\n                    *x1 = *p4;\n\n                return true;\n            }\n        }\n\n        [SecuritySafeCritical]\n        public static unsafe bool Fill(this long[] a1, long i1)\n        {\n            if (a1 == null)\n                return false;\n\n            long[] fbi = { i1 };\n\n            fixed (long* p1 = a1, p = fbi)\n            {\n                var Len = a1.Length;\n                var x1 = p1;\n\n                while (Len > 1)\n                {\n                    *x1 = *p;\n                    x1 += 1;\n                    Len -= 1;\n                }\n\n                return true;\n            }\n        }\n\n        [SecuritySafeCritical]\n        public static unsafe bool Fill(this ulong[] a1, ulong i1)\n        {\n            if (a1 == null)\n                return false;\n\n            ulong[] fbi = { i1 };\n\n            fixed (ulong* p1 = a1, p = fbi)\n            {\n                var Len = a1.Length;\n                var x1 = p1;\n\n                while (Len > 1)\n                {\n                    *x1 = *p;\n                    x1 += 1;\n                    Len -= 1;\n                }\n\n                return true;\n            }\n        }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Collection of Miscellaneous Byte Helper Functions<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[34,52,28],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/116"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":117,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/116\/revisions\/117"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}