Duff Device on Wikipedia

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DuffDeviceFun
{
    class Program
    {
        static void DuffDevice(byte[] src, byte[] dest, int count)
        {

            unsafe
            {
                fixed (byte* fromp = src, top = dest)
                {
                    byte* from = fromp;
                    byte* to = top;
                    int n = (count + 7) / 8;
                    switch (count % 8)
                    {
                        case 0: goto LBL0;
                        case 7: goto LBL7;
                        case 6: goto LBL6;
                        case 5: goto LBL5;
                        case 4: goto LBL4;
                        case 3: goto LBL3;
                        case 2: goto LBL2;
                        case 1: goto LBL1;
                    }
                LBL0: *to++ = *from++;
                LBL7: *to++ = *from++;
                LBL6: *to++ = *from++;
                LBL5: *to++ = *from++;
                LBL4: *to++ = *from++;
                LBL3: *to++ = *from++;
                LBL2: *to++ = *from++;
                LBL1: *to++ = *from++;
                    if (--n > 0)
                        goto LBL0;
                }
            }
        }
        static void Main(string[] args)
        {
            string msg = "Demo Message!";
            var len = msg.Length * sizeof(char);
            byte[] from = new byte[len];
            Buffer.BlockCopy(msg.ToCharArray(), 0, from, 0, len);
            byte[] to = new byte[from.Length * sizeof(char)];
            DuffDevice(from, to, len);
            char[] res = new char[to.Length / sizeof(char)];
            Buffer.BlockCopy(to, 0, res, 0, to.Length );
            Console.WriteLine(res);
            Console.ReadKey();
        }
    }
}