Skip to content

Merge | Cleanup Pre 2005 Support in TdsParser #3206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ internal sealed partial class TdsParser

// Version variables

private bool _is2005 = false; // set to true if speaking to 2005 or later

private bool _is2008 = false;

private bool _is2012 = false;
Expand Down Expand Up @@ -931,7 +929,6 @@ private PreLoginHandshakeStatus ConsumePreLoginHandshake(
// Assign default values
marsCapable = _fMARS;
fedAuthRequired = false;
bool is2005OrLater = false;
Debug.Assert(_physicalStateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
TdsOperationStatus result = _physicalStateObj.TryReadNetworkPacket();
if (result != TdsOperationStatus.Done)
Expand Down Expand Up @@ -990,13 +987,6 @@ private PreLoginHandshakeStatus ConsumePreLoginHandshake(
byte minorVersion = payload[payloadOffset + 1];
int level = (payload[payloadOffset + 2] << 8) |
payload[payloadOffset + 3];

is2005OrLater = majorVersion >= 9;
if (!is2005OrLater)
{
marsCapable = false; // If pre-2005, MARS not supported.
}

break;

case (int)PreLoginOptions.ENCRYPT:
Expand Down Expand Up @@ -1154,7 +1144,7 @@ private PreLoginHandshakeStatus ConsumePreLoginHandshake(
bool shouldValidateServerCert = (_encryptionOption == EncryptionOptions.ON && !trustServerCert) ||
(_connHandler._accessTokenInBytes != null && !trustServerCert);
uint info = (shouldValidateServerCert ? TdsEnums.SNI_SSL_VALIDATE_CERTIFICATE : 0)
| (is2005OrLater ? TdsEnums.SNI_SSL_USE_SCHANNEL_CACHE : 0);
| TdsEnums.SNI_SSL_USE_SCHANNEL_CACHE;

EnableSsl(info, encrypt, integratedSecurity, serverCert);
}
Expand Down Expand Up @@ -2747,30 +2737,7 @@ private TdsOperationStatus TryProcessEnvChange(int tokenLength, TdsParserStateOb
}
break;

case TdsEnums.ENV_CHARSET:
// we copied this behavior directly from luxor - see charset envchange
// section from sqlctokn.c
result = TryReadTwoStringFields(env, stateObj);
if (result != TdsOperationStatus.Done)
{
return result;
}
if (env._newValue == TdsEnums.DEFAULT_ENGLISH_CODE_PAGE_STRING)
{
_defaultCodePage = TdsEnums.DEFAULT_ENGLISH_CODE_PAGE_VALUE;
_defaultEncoding = System.Text.Encoding.GetEncoding(_defaultCodePage);
}
else
{
Debug.Assert(env._newValue.Length > TdsEnums.CHARSET_CODE_PAGE_OFFSET, "TdsParser.ProcessEnvChange(): charset value received with length <=10");

string stringCodePage = env._newValue.Substring(TdsEnums.CHARSET_CODE_PAGE_OFFSET);

_defaultCodePage = int.Parse(stringCodePage, NumberStyles.Integer, CultureInfo.InvariantCulture);
_defaultEncoding = System.Text.Encoding.GetEncoding(_defaultCodePage);
}

break;
// TdsEnums.ENV_CHARSET (3) is only supported in TDS <= 7 which is no longer supported

case TdsEnums.ENV_PACKETSIZE:
// take care of packet size right here
Expand Down Expand Up @@ -2802,24 +2769,9 @@ private TdsOperationStatus TryProcessEnvChange(int tokenLength, TdsParserStateOb

break;

case TdsEnums.ENV_LOCALEID:
// UNDONE: this LCID may be incorrect for OEM code pages on 7.0
// need a way to get lcid from code page
result = TryReadTwoStringFields(env, stateObj);
if (result != TdsOperationStatus.Done)
{
return result;
}
_defaultLCID = int.Parse(env._newValue, NumberStyles.Integer, CultureInfo.InvariantCulture);
break;
// TdsEnums.ENV_LOCALE (5) is only supported in TDS <= 7 which is no longer supported

case TdsEnums.ENV_COMPFLAGS:
result = TryReadTwoStringFields(env, stateObj);
if (result != TdsOperationStatus.Done)
{
return result;
}
break;
// TdsEnums.ENV_COMPFLAGS (6) is only supported in TDS <= 7 which is no longer supported

case TdsEnums.ENV_COLLATION:
Debug.Assert(env._newLength == 5 || env._newLength == 0, "Improper length in new collation!");
Expand Down Expand Up @@ -3829,7 +3781,6 @@ private TdsOperationStatus TryProcessLoginAck(TdsParserStateObject stateObj, out
{
throw SQL.InvalidTDSVersion();
}
_is2005 = true;
break;
case TdsEnums.SQL2008_MAJOR << 24 | TdsEnums.SQL2008_MINOR:
if (increment != TdsEnums.SQL2008_INCREMENT)
Expand Down Expand Up @@ -3857,7 +3808,6 @@ private TdsOperationStatus TryProcessLoginAck(TdsParserStateObject stateObj, out
}
_is2012 |= _is2022;
_is2008 |= _is2012;
_is2005 |= _is2008;

stateObj._outBytesUsed = stateObj._outputHeaderLen;
byte len;
Expand Down Expand Up @@ -5178,7 +5128,7 @@ private TdsOperationStatus TryProcessTypeInfo(TdsParserStateObject stateObj, Sql
}

// read the collation for 7.x servers
if (col.metaType.IsCharType && (tdsType != TdsEnums.SQLXMLTYPE) && ((tdsType != TdsEnums.SQLJSON)))
if (col.metaType.IsCharType && (tdsType != TdsEnums.SQLXMLTYPE) && (tdsType != TdsEnums.SQLJSON))
{
result = TryProcessCollation(stateObj, out col.collation);
if (result != TdsOperationStatus.Done)
Expand Down Expand Up @@ -9431,8 +9381,7 @@ internal Task TdsExecuteRPC(SqlCommand cmd, IList<_SqlRPC> rpcArray, int timeout
continue;
}

if ((!_is2005 && !mt.Is80Supported) ||
(!_is2008 && !mt.Is90Supported))
if (!_is2008 && !mt.Is90Supported)
{
throw ADP.VersionDoesNotSupportDataType(mt.TypeName);
}
Expand Down Expand Up @@ -9797,8 +9746,6 @@ private Task TDSExecuteRPCAddParameter(TdsParserStateObject stateObj, SqlParamet
}
else if (mt.SqlDbType == SqlDbType.Udt)
{
Debug.Assert(_is2005, "Invalid DataType UDT for non-2005 or later server!");

int maxSupportedSize = Is2008OrNewer ? int.MaxValue : short.MaxValue;
byte[] udtVal = null;
SqlServer.Server.Format format = SqlServer.Server.Format.Native;
Expand Down Expand Up @@ -9891,7 +9838,7 @@ private Task TDSExecuteRPCAddParameter(TdsParserStateObject stateObj, SqlParamet
else if ((!mt.IsVarTime) && (mt.SqlDbType != SqlDbType.Date))
{ // Time, Date, DateTime2, DateTimeoffset do not have the size written out
maxsize = (size > actualSize) ? size : actualSize;
if (maxsize == 0 && _is2005)
if (maxsize == 0)
{
// 2005 doesn't like 0 as MaxSize. Change it to 2 for unicode types (SQL9 - 682322)
if (mt.IsNCharType)
Expand Down Expand Up @@ -11224,8 +11171,6 @@ private int GetNotificationHeaderSize(SqlNotificationRequest notificationRequest
// Write query notificaiton header data, not including the notificaiton header length
private void WriteQueryNotificationHeaderData(SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj)
{
Debug.Assert(_is2005, "WriteQueryNotificationHeaderData called on a non-2005 server");

// We may need to update the notification header length if the header is changed in the future

Debug.Assert(notificationRequest != null, "notificationRequest is null");
Expand Down Expand Up @@ -13284,16 +13229,15 @@ private TdsOperationStatus TryProcessUDTMetaData(SqlMetaDataPriv metaData, TdsPa
+ " _connHandler = {14}\n\t"
+ " _fMARS = {15}\n\t"
+ " _sessionPool = {16}\n\t"
+ " _is2005 = {17}\n\t"
+ " _sniSpnBuffer = {18}\n\t"
+ " _errors = {19}\n\t"
+ " _warnings = {20}\n\t"
+ " _attentionErrors = {21}\n\t"
+ " _attentionWarnings = {22}\n\t"
+ " _statistics = {23}\n\t"
+ " _statisticsIsInTransaction = {24}\n\t"
+ " _fPreserveTransaction = {25}"
+ " _fParallel = {26}"
+ " _sniSpnBuffer = {17}\n\t"
+ " _errors = {18}\n\t"
+ " _warnings = {19}\n\t"
+ " _attentionErrors = {20}\n\t"
+ " _attentionWarnings = {21}\n\t"
+ " _statistics = {22}\n\t"
+ " _statisticsIsInTransaction = {23}\n\t"
+ " _fPreserveTransaction = {24}"
+ " _fParallel = {25}"
;
internal string TraceString()
{
Expand All @@ -13316,7 +13260,6 @@ internal string TraceString()
_connHandler == null ? "(null)" : _connHandler.ObjectID.ToString((IFormatProvider)null),
_fMARS ? bool.TrueString : bool.FalseString,
_sessionPool == null ? "(null)" : _sessionPool.TraceString(),
_is2005 ? bool.TrueString : bool.FalseString,
_serverSpn == null ? "(null)" : _serverSpn.Length.ToString((IFormatProvider)null),
_physicalStateObj != null ? "(null)" : _physicalStateObj.ErrorCount.ToString((IFormatProvider)null),
_physicalStateObj != null ? "(null)" : _physicalStateObj.WarningCount.ToString((IFormatProvider)null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,14 +713,8 @@ override protected internal bool IsNonPoolableTransactionRoot
return IsTransactionRoot && (!Is2008OrNewer || Pool == null);
}
}

override internal bool Is2000
{
get
{
return _loginAck.isVersion8;
}
}

override internal bool Is2000 => true;

override internal bool Is2005OrNewer
{
Expand Down
Loading
Loading