private void ReadTwoStringFields(SqlEnvChange env, TdsParserStateObject stateObj)
 {
     env.newLength = stateObj.ReadByte();
     env.newValue = stateObj.ReadString(env.newLength);
     env.oldLength = stateObj.ReadByte();
     env.oldValue = stateObj.ReadString(env.oldLength);
     env.length = (3 + (env.newLength * 2)) + (env.oldLength * 2);
 }
 private void ProcessUDTMetaData(SqlMetaDataPriv metaData, TdsParserStateObject stateObj)
 {
     metaData.length = stateObj.ReadUInt16();
     int length = stateObj.ReadByte();
     if (length != 0)
     {
         metaData.udtDatabaseName = stateObj.ReadString(length);
     }
     length = stateObj.ReadByte();
     if (length != 0)
     {
         metaData.udtSchemaName = stateObj.ReadString(length);
     }
     length = stateObj.ReadByte();
     if (length != 0)
     {
         metaData.udtTypeName = stateObj.ReadString(length);
     }
     length = stateObj.ReadUInt16();
     if (length != 0)
     {
         metaData.udtAssemblyQualifiedName = stateObj.ReadString(length);
     }
 }
        private void ReadSqlStringValue(SqlBuffer value, byte type, int length, Encoding encoding, bool isPlp, TdsParserStateObject stateObj)
        {
            string strEmpty;
            byte num = type;
            if (num <= 0x63)
            {
                if (num > 0x27)
                {
                    switch (num)
                    {
                        case 0x2f:
                            goto Label_004C;

                        case 0x63:
                            goto Label_006B;
                    }
                    return;
                }
                if ((num != 0x23) && (num != 0x27))
                {
                    return;
                }
            }
            else if (num <= 0xaf)
            {
                if ((num != 0xa7) && (num != 0xaf))
                {
                    return;
                }
            }
            else
            {
                switch (num)
                {
                    case 0xe7:
                    case 0xef:
                        goto Label_006B;
                }
                return;
            }
        Label_004C:
            if (encoding == null)
            {
                encoding = this._defaultEncoding;
            }
            value.SetToString(stateObj.ReadStringWithEncoding(length, encoding, isPlp));
            return;
        Label_006B:
            strEmpty = null;
            if (isPlp)
            {
                char[] buff = null;
                length = this.ReadPlpUnicodeChars(ref buff, 0, length >> 1, stateObj);
                if (length > 0)
                {
                    strEmpty = new string(buff, 0, length);
                }
                else
                {
                    strEmpty = ADP.StrEmpty;
                }
            }
            else
            {
                strEmpty = stateObj.ReadString(length >> 1);
            }
            value.SetToString(strEmpty);
        }
 internal SqlReturnValue ProcessReturnValue(int length, TdsParserStateObject stateObj)
 {
     int tokenLength;
     uint num8;
     SqlReturnValue metaData = new SqlReturnValue {
         length = length
     };
     if (this._isYukon)
     {
         metaData.parmIndex = stateObj.ReadUInt16();
     }
     byte num2 = stateObj.ReadByte();
     if (num2 > 0)
     {
         metaData.parameter = stateObj.ReadString(num2);
     }
     stateObj.ReadByte();
     if (this.IsYukonOrNewer)
     {
         num8 = stateObj.ReadUInt32();
     }
     else
     {
         num8 = stateObj.ReadUInt16();
     }
     stateObj.ReadUInt16();
     byte tdsType = stateObj.ReadByte();
     if (tdsType == 0xf1)
     {
         tokenLength = 0xffff;
     }
     else if (this.IsVarTimeTds(tdsType))
     {
         tokenLength = 0;
     }
     else if (tdsType == 40)
     {
         tokenLength = 3;
     }
     else
     {
         tokenLength = this.GetTokenLength(tdsType, stateObj);
     }
     metaData.metaType = MetaType.GetSqlDataType(tdsType, num8, tokenLength);
     metaData.type = metaData.metaType.SqlDbType;
     if (this._isShiloh)
     {
         metaData.tdsType = metaData.metaType.NullableType;
         metaData.isNullable = true;
         if (tokenLength == 0xffff)
         {
             metaData.metaType = MetaType.GetMaxMetaTypeFromMetaType(metaData.metaType);
         }
     }
     else
     {
         if (metaData.metaType.NullableType == tdsType)
         {
             metaData.isNullable = true;
         }
         metaData.tdsType = tdsType;
     }
     if (metaData.type == SqlDbType.Decimal)
     {
         metaData.precision = stateObj.ReadByte();
         metaData.scale = stateObj.ReadByte();
     }
     if (metaData.metaType.IsVarTime)
     {
         metaData.scale = stateObj.ReadByte();
     }
     if (tdsType == 240)
     {
         this.ProcessUDTMetaData(metaData, stateObj);
     }
     if (metaData.type == SqlDbType.Xml)
     {
         if ((stateObj.ReadByte() & 1) != 0)
         {
             num2 = stateObj.ReadByte();
             if (num2 != 0)
             {
                 metaData.xmlSchemaCollectionDatabase = stateObj.ReadString(num2);
             }
             num2 = stateObj.ReadByte();
             if (num2 != 0)
             {
                 metaData.xmlSchemaCollectionOwningSchema = stateObj.ReadString(num2);
             }
             short num7 = stateObj.ReadInt16();
             if (num7 != 0)
             {
                 metaData.xmlSchemaCollectionName = stateObj.ReadString(num7);
             }
         }
     }
     else if (this._isShiloh && metaData.metaType.IsCharType)
     {
         metaData.collation = this.ProcessCollation(stateObj);
         int codePage = this.GetCodePage(metaData.collation, stateObj);
         if (codePage == this._defaultCodePage)
         {
             metaData.codePage = this._defaultCodePage;
             metaData.encoding = this._defaultEncoding;
         }
         else
         {
             metaData.codePage = codePage;
             metaData.encoding = Encoding.GetEncoding(metaData.codePage);
         }
     }
     bool isNull = false;
     ulong num5 = this.ProcessColumnHeader(metaData, stateObj, out isNull);
     int num4 = (num5 > 0x7fffffffL) ? 0x7fffffff : ((int) num5);
     if (metaData.metaType.IsPlp)
     {
         num4 = 0x7fffffff;
     }
     if (isNull)
     {
         this.GetNullSqlValue(metaData.value, metaData);
         return metaData;
     }
     this.ReadSqlValue(metaData.value, metaData, num4, stateObj);
     return metaData;
 }
        private void CommonProcessMetaData(TdsParserStateObject stateObj, _SqlMetaData col)
        {
            uint num5;
            int length = 0;
            if (this.IsYukonOrNewer)
            {
                num5 = stateObj.ReadUInt32();
            }
            else
            {
                num5 = stateObj.ReadUInt16();
            }
            byte num3 = stateObj.ReadByte();
            col.updatability = (byte) ((num3 & 11) >> 2);
            col.isNullable = 1 == (num3 & 1);
            col.isIdentity = 0x10 == (num3 & 0x10);
            stateObj.ReadByte();
            col.isColumnSet = 4 == (num3 & 4);
            byte tdsType = stateObj.ReadByte();
            if (tdsType == 0xf1)
            {
                col.length = 0xffff;
            }
            else if (this.IsVarTimeTds(tdsType))
            {
                col.length = 0;
            }
            else if (tdsType == 40)
            {
                col.length = 3;
            }
            else
            {
                col.length = this.GetTokenLength(tdsType, stateObj);
            }
            col.metaType = MetaType.GetSqlDataType(tdsType, num5, col.length);
            col.type = col.metaType.SqlDbType;
            if (this._isShiloh)
            {
                col.tdsType = col.isNullable ? col.metaType.NullableType : col.metaType.TDSType;
            }
            else
            {
                col.tdsType = tdsType;
            }
            if (this._isYukon)
            {
                if (240 == tdsType)
                {
                    this.ProcessUDTMetaData(col, stateObj);
                }
                if (col.length == 0xffff)
                {
                    col.metaType = MetaType.GetMaxMetaTypeFromMetaType(col.metaType);
                    col.length = 0x7fffffff;
                    if ((tdsType == 0xf1) && ((stateObj.ReadByte() & 1) != 0))
                    {
                        length = stateObj.ReadByte();
                        if (length != 0)
                        {
                            col.xmlSchemaCollectionDatabase = stateObj.ReadString(length);
                        }
                        length = stateObj.ReadByte();
                        if (length != 0)
                        {
                            col.xmlSchemaCollectionOwningSchema = stateObj.ReadString(length);
                        }
                        length = stateObj.ReadInt16();
                        if (length != 0)
                        {
                            col.xmlSchemaCollectionName = stateObj.ReadString(length);
                        }
                    }
                }
            }
            if (col.type == SqlDbType.Decimal)
            {
                col.precision = stateObj.ReadByte();
                col.scale = stateObj.ReadByte();
            }
            if (col.metaType.IsVarTime)
            {
                col.scale = stateObj.ReadByte();
                switch (col.metaType.SqlDbType)
                {
                    case SqlDbType.Time:
                        col.length = MetaType.GetTimeSizeFromScale(col.scale);
                        break;

                    case SqlDbType.DateTime2:
                        col.length = 3 + MetaType.GetTimeSizeFromScale(col.scale);
                        break;

                    case SqlDbType.DateTimeOffset:
                        col.length = 5 + MetaType.GetTimeSizeFromScale(col.scale);
                        break;
                }
            }
            if ((this._isShiloh && col.metaType.IsCharType) && (tdsType != 0xf1))
            {
                col.collation = this.ProcessCollation(stateObj);
                int codePage = this.GetCodePage(col.collation, stateObj);
                if (codePage == this._defaultCodePage)
                {
                    col.codePage = this._defaultCodePage;
                    col.encoding = this._defaultEncoding;
                }
                else
                {
                    col.codePage = codePage;
                    col.encoding = Encoding.GetEncoding(col.codePage);
                }
            }
            if (col.metaType.IsLong && !col.metaType.IsPlp)
            {
                if (this._isYukon)
                {
                    int num7 = 0xffff;
                    col.multiPartTableName = this.ProcessOneTable(stateObj, ref num7);
                }
                else
                {
                    length = stateObj.ReadUInt16();
                    string multipartName = stateObj.ReadString(length);
                    col.multiPartTableName = new MultiPartTableName(multipartName);
                }
            }
            length = stateObj.ReadByte();
            col.column = stateObj.ReadString(length);
            stateObj._receivedColMetaData = true;
        }
        private MultiPartTableName ProcessOneTable(TdsParserStateObject stateObj, ref int length)
        {
            ushort num;
            if (this._isShilohSP1)
            {
                MultiPartTableName name = new MultiPartTableName();
                byte num2 = stateObj.ReadByte();
                length--;
                switch (num2)
                {
                    case 4:
                        num = stateObj.ReadUInt16();
                        length -= 2;
                        name.ServerName = stateObj.ReadString(num);
                        num2 = (byte) (num2 - 1);
                        length -= num * 2;
                        break;

                    case 3:
                        num = stateObj.ReadUInt16();
                        length -= 2;
                        name.CatalogName = stateObj.ReadString(num);
                        length -= num * 2;
                        num2 = (byte) (num2 - 1);
                        break;

                    case 2:
                        num = stateObj.ReadUInt16();
                        length -= 2;
                        name.SchemaName = stateObj.ReadString(num);
                        length -= num * 2;
                        num2 = (byte) (num2 - 1);
                        break;

                    case 1:
                        num = stateObj.ReadUInt16();
                        length -= 2;
                        name.TableName = stateObj.ReadString(num);
                        length -= num * 2;
                        num2 = (byte) (num2 - 1);
                        break;
                }
                return name;
            }
            num = stateObj.ReadUInt16();
            length -= 2;
            string str = stateObj.ReadString(num);
            length -= num * 2;
            return new MultiPartTableName(MultipartIdentifier.ParseMultipartIdentifier(str, "[\"", "]\"", "SQL_TDSParserTableName", false));
        }
        private SqlLoginAck ProcessLoginAck(TdsParserStateObject stateObj)
        {
            SqlLoginAck ack = new SqlLoginAck();
            this.SkipBytes(1, stateObj);
            byte[] buff = new byte[4];
            stateObj.ReadByteArray(buff, 0, buff.Length);
            uint num3 = (uint) ((((((buff[0] << 8) | buff[1]) << 8) | buff[2]) << 8) | buff[3]);
            uint num6 = num3 & 0xff00ffff;
            uint num2 = (num3 >> 0x10) & 0xff;
            switch (num6)
            {
                case 0x72000002:
                    if (num2 != 9)
                    {
                        throw SQL.InvalidTDSVersion();
                    }
                    this._isYukon = true;
                    break;

                case 0x73000003:
                    if (num2 != 10)
                    {
                        throw SQL.InvalidTDSVersion();
                    }
                    this._isKatmai = true;
                    break;

                case 0x7000000:
                    switch (num2)
                    {
                        case 1:
                            this._isShiloh = true;
                            goto Label_00DF;
                    }
                    throw SQL.InvalidTDSVersion();

                case 0x71000001:
                    if (num2 != 0)
                    {
                        throw SQL.InvalidTDSVersion();
                    }
                    this._isShilohSP1 = true;
                    break;

                default:
                    throw SQL.InvalidTDSVersion();
            }
        Label_00DF:
            this._isYukon |= this._isKatmai;
            this._isShilohSP1 |= this._isYukon;
            this._isShiloh |= this._isShilohSP1;
            ack.isVersion8 = this._isShiloh;
            stateObj._outBytesUsed = stateObj._outputHeaderLen;
            byte length = stateObj.ReadByte();
            ack.programName = stateObj.ReadString(length);
            ack.majorVersion = stateObj.ReadByte();
            ack.minorVersion = stateObj.ReadByte();
            ack.buildNum = (short) ((stateObj.ReadByte() << 8) + stateObj.ReadByte());
            this._state = TdsParserState.OpenLoggedIn;
            if ((this._isYukon && this._fAsync) && this._fMARS)
            {
                this._resetConnectionEvent = new AutoResetEvent(true);
            }
            if (this._connHandler.ConnectionOptions.UserInstance && ADP.IsEmpty(this._connHandler.InstanceName))
            {
                this.Errors.Add(new SqlError(0, 0, 20, this.Server, SQLMessage.UserInstanceFailure(), "", 0));
                this.ThrowExceptionAndWarning();
            }
            return ack;
        }
 internal SqlError ProcessError(byte token, TdsParserStateObject stateObj)
 {
     int num2;
     int infoNumber = stateObj.ReadInt32();
     byte errorState = stateObj.ReadByte();
     byte errorClass = stateObj.ReadByte();
     int length = stateObj.ReadUInt16();
     string errorMessage = stateObj.ReadString(length);
     length = stateObj.ReadByte();
     if (length != 0)
     {
         stateObj.ReadString(length);
     }
     length = stateObj.ReadByte();
     string procedure = stateObj.ReadString(length);
     if (this._isYukon)
     {
         num2 = stateObj.ReadInt32();
     }
     else
     {
         num2 = stateObj.ReadUInt16();
         if ((this._state == TdsParserState.OpenNotLoggedIn) && (stateObj.PeekByte() == 0))
         {
             num2 = (num2 << 0x10) + stateObj.ReadUInt16();
         }
     }
     return new SqlError(infoNumber, errorState, errorClass, this._server, errorMessage, procedure, num2);
 }
        private SqlEnvChange[] ProcessEnvChange(int tokenLength, TdsParserStateObject stateObj)
        {
            int num4 = 0;
            int index = 0;
            SqlEnvChange[] changeArray = new SqlEnvChange[3];
            while (tokenLength > num4)
            {
                int num3;
                ushort num5;
                if (index >= changeArray.Length)
                {
                    SqlEnvChange[] changeArray2 = new SqlEnvChange[changeArray.Length + 3];
                    for (int i = 0; i < changeArray.Length; i++)
                    {
                        changeArray2[i] = changeArray[i];
                    }
                    changeArray = changeArray2;
                }
                SqlEnvChange env = new SqlEnvChange {
                    type = stateObj.ReadByte()
                };
                changeArray[index] = env;
                index++;
                switch (env.type)
                {
                    case 1:
                    case 2:
                        this.ReadTwoStringFields(env, stateObj);
                        goto Label_03E0;

                    case 3:
                        this.ReadTwoStringFields(env, stateObj);
                        if (!(env.newValue == "iso_1"))
                        {
                            break;
                        }
                        this._defaultCodePage = 0x4e4;
                        this._defaultEncoding = Encoding.GetEncoding(this._defaultCodePage);
                        goto Label_03E0;

                    case 4:
                    {
                        this.ReadTwoStringFields(env, stateObj);
                        int size = int.Parse(env.newValue, NumberStyles.Integer, CultureInfo.InvariantCulture);
                        if (this._physicalStateObj.SetPacketSize(size))
                        {
                            this._physicalStateObj._sniPacket.Dispose();
                            uint qInfo = (uint) size;
                            SNINativeMethodWrapper.SNISetInfo(this._physicalStateObj.Handle, SNINativeMethodWrapper.QTypes.SNI_QUERY_CONN_BUFSIZE, ref qInfo);
                            this._physicalStateObj._sniPacket = new SNIPacket(this._physicalStateObj.Handle);
                        }
                        goto Label_03E0;
                    }
                    case 5:
                        this.ReadTwoStringFields(env, stateObj);
                        this._defaultLCID = int.Parse(env.newValue, NumberStyles.Integer, CultureInfo.InvariantCulture);
                        goto Label_03E0;

                    case 6:
                        this.ReadTwoStringFields(env, stateObj);
                        goto Label_03E0;

                    case 7:
                        env.newLength = stateObj.ReadByte();
                        if (env.newLength == 5)
                        {
                            env.newCollation = this.ProcessCollation(stateObj);
                            this._defaultCollation = env.newCollation;
                            int codePage = this.GetCodePage(env.newCollation, stateObj);
                            if (codePage != this._defaultCodePage)
                            {
                                this._defaultCodePage = codePage;
                                this._defaultEncoding = Encoding.GetEncoding(this._defaultCodePage);
                            }
                            this._defaultLCID = env.newCollation.LCID;
                        }
                        env.oldLength = stateObj.ReadByte();
                        if (env.oldLength == 5)
                        {
                            env.oldCollation = this.ProcessCollation(stateObj);
                        }
                        env.length = (3 + env.newLength) + env.oldLength;
                        goto Label_03E0;

                    case 8:
                    case 9:
                    case 10:
                    case 11:
                    case 12:
                    case 0x11:
                        env.newLength = stateObj.ReadByte();
                        if (env.newLength <= 0)
                        {
                            goto Label_02B0;
                        }
                        env.newLongValue = stateObj.ReadInt64();
                        goto Label_02B8;

                    case 13:
                        this.ReadTwoStringFields(env, stateObj);
                        goto Label_03E0;

                    case 15:
                        env.newLength = stateObj.ReadInt32();
                        env.newBinValue = new byte[env.newLength];
                        stateObj.ReadByteArray(env.newBinValue, 0, env.newLength);
                        env.oldLength = stateObj.ReadByte();
                        env.length = 5 + env.newLength;
                        goto Label_03E0;

                    case 0x10:
                    case 0x12:
                        this.ReadTwoBinaryFields(env, stateObj);
                        goto Label_03E0;

                    case 0x13:
                        this.ReadTwoStringFields(env, stateObj);
                        goto Label_03E0;

                    case 20:
                    {
                        env.newLength = stateObj.ReadUInt16();
                        byte protocol = stateObj.ReadByte();
                        ushort port = stateObj.ReadUInt16();
                        ushort length = stateObj.ReadUInt16();
                        string servername = stateObj.ReadString(length);
                        env.newRoutingInfo = new RoutingInfo(protocol, port, servername);
                        num5 = stateObj.ReadUInt16();
                        num3 = 0;
                        goto Label_03C9;
                    }
                    default:
                        goto Label_03E0;
                }
                string s = env.newValue.Substring(2);
                this._defaultCodePage = int.Parse(s, NumberStyles.Integer, CultureInfo.InvariantCulture);
                this._defaultEncoding = Encoding.GetEncoding(this._defaultCodePage);
                goto Label_03E0;
            Label_02B0:
                env.newLongValue = 0L;
            Label_02B8:
                env.oldLength = stateObj.ReadByte();
                if (env.oldLength > 0)
                {
                    env.oldLongValue = stateObj.ReadInt64();
                }
                else
                {
                    env.oldLongValue = 0L;
                }
                env.length = (3 + env.newLength) + env.oldLength;
                goto Label_03E0;
            Label_03BC:
                stateObj.ReadByte();
                num3++;
            Label_03C9:
                if (num3 < num5)
                {
                    goto Label_03BC;
                }
                env.length = (env.newLength + num5) + 5;
            Label_03E0:
                num4 += env.length;
            }
            return changeArray;
        }
 private _SqlMetaDataSet ProcessColInfo(_SqlMetaDataSet columns, SqlDataReader reader, TdsParserStateObject stateObj)
 {
     for (int i = 0; i < columns.Length; i++)
     {
         _SqlMetaData data = columns[i];
         stateObj.ReadByte();
         data.tableNum = stateObj.ReadByte();
         byte num = stateObj.ReadByte();
         data.isDifferentName = 0x20 == (num & 0x20);
         data.isExpression = 4 == (num & 4);
         data.isKey = 8 == (num & 8);
         data.isHidden = 0x10 == (num & 0x10);
         if (data.isDifferentName)
         {
             byte length = stateObj.ReadByte();
             data.baseColumn = stateObj.ReadString(length);
         }
         if ((reader.TableNames != null) && (data.tableNum > 0))
         {
             data.multiPartTableName = reader.TableNames[data.tableNum - 1];
         }
         if (data.isExpression)
         {
             data.updatability = 0;
         }
     }
     return columns;
 }