Example #1
0
        public static byte[] PackEnumerable(string description, IEnumerable args)
        {
            DataConverter.PackContext packContext = new DataConverter.PackContext();
            packContext.conv        = DataConverter.CopyConv;
            packContext.description = description;
            IEnumerator enumerator = args.GetEnumerator();
            bool        flag       = enumerator.MoveNext();

            packContext.i = 0;
            while (packContext.i < description.Length)
            {
                object oarg;
                if (flag)
                {
                    oarg = enumerator.Current;
                }
                else
                {
                    if (packContext.repeat != 0)
                    {
                        break;
                    }
                    oarg = null;
                }
                int i = packContext.i;
                if (DataConverter.PackOne(packContext, oarg))
                {
                    flag = enumerator.MoveNext();
                    if (packContext.repeat > 0)
                    {
                        if (--packContext.repeat > 0)
                        {
                            packContext.i = i;
                        }
                        else
                        {
                            packContext.i++;
                        }
                    }
                    else
                    {
                        packContext.i++;
                    }
                }
                else
                {
                    packContext.i++;
                }
            }
            return(packContext.Get());
        }
Example #2
0
        public static byte[] Pack(string description, params object[] args)
        {
            int num = 0;

            DataConverter.PackContext packContext = new DataConverter.PackContext();
            packContext.conv        = DataConverter.CopyConv;
            packContext.description = description;
            packContext.i           = 0;
            while (packContext.i < description.Length)
            {
                object oarg;
                if (num < args.Length)
                {
                    oarg = args[num];
                }
                else
                {
                    if (packContext.repeat != 0)
                    {
                        break;
                    }
                    oarg = null;
                }
                int i = packContext.i;
                if (DataConverter.PackOne(packContext, oarg))
                {
                    num++;
                    if (packContext.repeat > 0)
                    {
                        if (--packContext.repeat > 0)
                        {
                            packContext.i = i;
                        }
                        else
                        {
                            packContext.i++;
                        }
                    }
                    else
                    {
                        packContext.i++;
                    }
                }
                else
                {
                    packContext.i++;
                }
            }
            return(packContext.Get());
        }
Example #3
0
        private static bool PackOne(DataConverter.PackContext b, object oarg)
        {
            char c = b.description[b.i];

            switch (c)
            {
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                b.repeat = (int)((short)b.description[b.i] - 48);
                return(false);

            default:
                switch (c)
                {
                case '[':
                {
                    int num = -1;
                    int i;
                    for (i = b.i + 1; i < b.description.Length; i++)
                    {
                        if (b.description[i] == ']')
                        {
                            break;
                        }
                        int num2 = (int)((short)b.description[i] - 48);
                        if (num2 >= 0 && num2 <= 9)
                        {
                            if (num == -1)
                            {
                                num = num2;
                            }
                            else
                            {
                                num = num * 10 + num2;
                            }
                        }
                    }
                    if (num == -1)
                    {
                        throw new ArgumentException("invalid size specification");
                    }
                    b.i      = i;
                    b.repeat = num;
                    return(false);
                }

                default:
                {
                    switch (c)
                    {
                    case '!':
                        b.align = -1;
                        return(false);

                    default:
                        switch (c)
                        {
                        case 'I':
                            b.Add(b.conv.GetBytes(Convert.ToUInt32(oarg)));
                            return(true);

                        default:
                            switch (c)
                            {
                            case 'x':
                                b.Add(new byte[1]);
                                return(false);

                            default:
                                if (c == '*')
                                {
                                    b.repeat = int.MaxValue;
                                    return(false);
                                }
                                if (c == 'S')
                                {
                                    b.Add(b.conv.GetBytes(Convert.ToUInt16(oarg)));
                                    return(true);
                                }
                                if (c != 's')
                                {
                                    throw new ArgumentException(string.Format("invalid format specified `{0}'", b.description[b.i]));
                                }
                                b.Add(b.conv.GetBytes(Convert.ToInt16(oarg)));
                                return(true);

                            case 'z':
                                break;
                            }
                            break;

                        case 'L':
                            b.Add(b.conv.GetBytes(Convert.ToUInt64(oarg)));
                            return(true);
                        }
                        break;

                    case '$':
                        break;

                    case '%':
                        b.conv = DataConverter.Native;
                        return(false);
                    }
                    bool flag = b.description[b.i] == 'z';
                    b.i++;
                    if (b.i >= b.description.Length)
                    {
                        throw new ArgumentException("$ description needs a type specified", "description");
                    }
                    char     c2 = b.description[b.i];
                    char     c3 = c2;
                    int      num2;
                    Encoding encoding;
                    switch (c3)
                    {
                    case '3':
                        encoding = Encoding.GetEncoding(12000);
                        num2     = 4;
                        break;

                    case '4':
                        encoding = Encoding.GetEncoding(12001);
                        num2     = 4;
                        break;

                    default:
                        if (c3 != 'b')
                        {
                            throw new ArgumentException("Invalid format for $ specifier", "description");
                        }
                        encoding = Encoding.BigEndianUnicode;
                        num2     = 2;
                        break;

                    case '6':
                        encoding = Encoding.Unicode;
                        num2     = 2;
                        break;

                    case '7':
                        encoding = Encoding.UTF7;
                        num2     = 1;
                        break;

                    case '8':
                        encoding = Encoding.UTF8;
                        num2     = 1;
                        break;
                    }
                    if (b.align == -1)
                    {
                        b.align = 4;
                    }
                    b.Add(encoding.GetBytes(Convert.ToString(oarg)));
                    if (flag)
                    {
                        b.Add(new byte[num2]);
                    }
                    break;
                }

                case '^':
                    b.conv = DataConverter.BigEndian;
                    return(false);

                case '_':
                    b.conv = DataConverter.LittleEndian;
                    return(false);

                case 'b':
                    b.Add(new byte[]
                    {
                        Convert.ToByte(oarg)
                    });
                    break;

                case 'c':
                    b.Add(new byte[]
                    {
                        (byte)Convert.ToSByte(oarg)
                    });
                    break;

                case 'd':
                    b.Add(b.conv.GetBytes(Convert.ToDouble(oarg)));
                    break;

                case 'f':
                    b.Add(b.conv.GetBytes(Convert.ToSingle(oarg)));
                    break;

                case 'i':
                    b.Add(b.conv.GetBytes(Convert.ToInt32(oarg)));
                    break;

                case 'l':
                    b.Add(b.conv.GetBytes(Convert.ToInt64(oarg)));
                    break;
                }
                break;

            case 'C':
                b.Add(new byte[]
                {
                    Convert.ToByte(oarg)
                });
                break;
            }
            return(true);
        }