Пример #1
0
        private byte[] GetNumericBytes(DbField field)
        {
            decimal value   = field.DbValue.GetDecimal();
            object  numeric = TypeEncoder.EncodeDecimal(value, field.NumericScale, field.DataType);

            switch (field.SqlType)
            {
            case IscCodes.SQL_SHORT:
                return(BitConverter.GetBytes((short)numeric));

            case IscCodes.SQL_LONG:
                return(BitConverter.GetBytes((int)numeric));

            case IscCodes.SQL_INT64:
            case IscCodes.SQL_QUAD:
                return(BitConverter.GetBytes((long)numeric));

            case IscCodes.SQL_DOUBLE:
                return(BitConverter.GetBytes(field.DbValue.GetDouble()));

            default:
                return(null);
            }
        }
Пример #2
0
        private byte[] EncodeSlice(ArrayDesc desc, Array sourceArray, int length)
        {
            var writer  = new BinaryWriter(new MemoryStream());
            var charset = _db.Charset;
            var dbType  = DbDataType.Array;
            var subType = (Descriptor.Scale < 0) ? 2 : 0;
            var type    = 0;

            type   = TypeHelper.GetSqlTypeFromBlrType(Descriptor.DataType);
            dbType = TypeHelper.GetDbDataTypeFromBlrType(Descriptor.DataType, subType, Descriptor.Scale);

            foreach (var source in sourceArray)
            {
                switch (dbType)
                {
                case DbDataType.Char:
                {
                    var value  = source != null ? (string)source : string.Empty;
                    var buffer = charset.GetBytes(value);

                    writer.Write(buffer);

                    if (desc.Length > buffer.Length)
                    {
                        for (var j = buffer.Length; j < desc.Length; j++)
                        {
                            writer.Write((byte)32);
                        }
                    }
                }
                break;

                case DbDataType.VarChar:
                {
                    var value = source != null ? (string)source : string.Empty;

                    var buffer = charset.GetBytes(value);
                    writer.Write(buffer);

                    if (desc.Length > buffer.Length)
                    {
                        for (var j = buffer.Length; j < desc.Length; j++)
                        {
                            writer.Write((byte)0);
                        }
                    }
                    writer.Write((short)0);
                }
                break;

                case DbDataType.SmallInt:
                    writer.Write((short)source);
                    break;

                case DbDataType.Integer:
                    writer.Write((int)source);
                    break;

                case DbDataType.BigInt:
                    writer.Write((long)source);
                    break;

                case DbDataType.Float:
                    writer.Write((float)source);
                    break;

                case DbDataType.Double:
                    writer.Write((double)source);
                    break;

                case DbDataType.Numeric:
                case DbDataType.Decimal:
                {
                    var numeric = TypeEncoder.EncodeDecimal((decimal)source, desc.Scale, type);

                    switch (type)
                    {
                    case IscCodes.SQL_SHORT:
                        writer.Write((short)numeric);
                        break;

                    case IscCodes.SQL_LONG:
                        writer.Write((int)numeric);
                        break;

                    case IscCodes.SQL_QUAD:
                    case IscCodes.SQL_INT64:
                        writer.Write((long)numeric);
                        break;
                    }
                }
                break;

                case DbDataType.Date:
                    writer.Write(TypeEncoder.EncodeDate(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat)));
                    break;

                case DbDataType.Time:
                    writer.Write(TypeEncoder.EncodeTime((TimeSpan)source));
                    break;

                case DbDataType.TimeStamp:
                    var dt = Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat);
                    writer.Write(TypeEncoder.EncodeDate(dt));
                    writer.Write(TypeEncoder.EncodeTime(TypeHelper.DateTimeToTimeSpan(dt)));
                    break;

                default:
                    throw TypeHelper.InvalidDataType((int)dbType);
                }
            }

            return(((MemoryStream)writer.BaseStream).ToArray());
        }
 public void WriteTime(TimeSpan value)
 {
     Write(TypeEncoder.EncodeTime(value));
 }
 public void WriteDate(DateTime value)
 {
     Write(TypeEncoder.EncodeDate(Convert.ToDateTime(value)));
 }
 public Task Write(long value, AsyncWrappingCommonArgs async)
 {
     return(_dataProvider.Write(TypeEncoder.EncodeInt64(value), 0, 8, async));
 }
Пример #6
0
 public ValueTask WriteAsync(Guid value, CancellationToken cancellationToken = default)
 {
     return(WriteOpaqueAsync(TypeEncoder.EncodeGuid(value), cancellationToken));
 }
Пример #7
0
 public ValueTask WriteAsync(bool value, CancellationToken cancellationToken = default)
 {
     return(WriteOpaqueAsync(TypeEncoder.EncodeBoolean(value), cancellationToken));
 }
Пример #8
0
 public ValueTask WriteAsync(long value, CancellationToken cancellationToken = default)
 {
     return(_dataProvider.WriteAsync(TypeEncoder.EncodeInt64(value), 0, 8, cancellationToken));
 }
    public byte[] UserIdentificationData()
    {
        using (var result = new MemoryStream(1024))
        {
            var userString = Environment.GetEnvironmentVariable("USERNAME") ?? Environment.GetEnvironmentVariable("USER") ?? string.Empty;
            var user       = Encoding.UTF8.GetBytes(userString);
            result.WriteByte(IscCodes.CNCT_user);
            result.WriteByte((byte)user.Length);
            result.Write(user, 0, user.Length);

            var host = Encoding.UTF8.GetBytes(Dns.GetHostName());
            result.WriteByte(IscCodes.CNCT_host);
            result.WriteByte((byte)host.Length);
            result.Write(host, 0, host.Length);

            result.WriteByte(IscCodes.CNCT_user_verification);
            result.WriteByte(0);

            if (!string.IsNullOrEmpty(User))
            {
                var login = Encoding.UTF8.GetBytes(User);
                result.WriteByte(IscCodes.CNCT_login);
                result.WriteByte((byte)login.Length);
                result.Write(login, 0, login.Length);

                var pluginNameBytes = Encoding.ASCII.GetBytes(_srp256.Name);
                result.WriteByte(IscCodes.CNCT_plugin_name);
                result.WriteByte((byte)pluginNameBytes.Length);
                result.Write(pluginNameBytes, 0, pluginNameBytes.Length);
                var specificData = Encoding.ASCII.GetBytes(_srp256.PublicKeyHex);
                WriteMultiPartHelper(result, IscCodes.CNCT_specific_data, specificData);

                var plugins      = string.Join(",", new[] { _srp256.Name, _srp.Name });
                var pluginsBytes = Encoding.ASCII.GetBytes(plugins);
                result.WriteByte(IscCodes.CNCT_plugin_list);
                result.WriteByte((byte)pluginsBytes.Length);
                result.Write(pluginsBytes, 0, pluginsBytes.Length);

                result.WriteByte(IscCodes.CNCT_client_crypt);
                result.WriteByte(4);
                result.Write(TypeEncoder.EncodeInt32(WireCryptOptionValue(WireCrypt)), 0, 4);
            }
            else
            {
                var pluginNameBytes = Encoding.ASCII.GetBytes(_sspi.Name);
                result.WriteByte(IscCodes.CNCT_plugin_name);
                result.WriteByte((byte)pluginNameBytes.Length);
                result.Write(pluginNameBytes, 0, pluginNameBytes.Length);
                var specificData = _sspi.InitializeClientSecurity();
                WriteMultiPartHelper(result, IscCodes.CNCT_specific_data, specificData);

                result.WriteByte(IscCodes.CNCT_plugin_list);
                result.WriteByte((byte)pluginNameBytes.Length);
                result.Write(pluginNameBytes, 0, pluginNameBytes.Length);

                result.WriteByte(IscCodes.CNCT_client_crypt);
                result.WriteByte(4);
                result.Write(TypeEncoder.EncodeInt32(IscCodes.WIRE_CRYPT_DISABLED), 0, 4);
            }

            return(result.ToArray());
        }
    }
Пример #10
0
        private byte[] GetBytes(DbField field)
        {
            if (field.DbValue.IsDBNull())
            {
                int length = field.Length;

                if (field.SqlType == IscCodes.SQL_VARYING)
                {
                    // Add two bytes more for store	value length
                    length += 2;
                }

                return(new byte[length]);
            }

            switch (field.DbDataType)
            {
            case DbDataType.Char:
            {
                string svalue = field.DbValue.GetString();

                if ((field.Length % field.Charset.BytesPerCharacter) == 0 &&
                    svalue.Length > field.CharCount)
                {
                    throw new IscException(335544321);
                }

                byte[] buffer = new     byte[field.Length];
                for (int i = 0; i < buffer.Length; i++)
                {
                    buffer[i] = 32;
                }

                byte[] bytes = field.Charset.GetBytes(svalue);

                Buffer.BlockCopy(bytes, 0, buffer, 0, bytes.Length);

                return(buffer);
            }

            case DbDataType.VarChar:
            {
                string svalue = field.Value.ToString();

                if ((field.Length % field.Charset.BytesPerCharacter) == 0 &&
                    svalue.Length > field.CharCount)
                {
                    throw new IscException(335544321);
                }

                byte[] sbuffer = field.Charset.GetBytes(svalue);

                byte[] buffer = new     byte[field.Length + 2];

                // Copy	length
                Buffer.BlockCopy(
                    BitConverter.GetBytes((short)sbuffer.Length),
                    0, buffer, 0, 2);

                // Copy	string value
                Buffer.BlockCopy(sbuffer, 0, buffer, 2, sbuffer.Length);

                return(buffer);
            }

            case DbDataType.Numeric:
            case DbDataType.Decimal:
                return(this.GetNumericBytes(field));

            case DbDataType.SmallInt:
                return(BitConverter.GetBytes(field.DbValue.GetInt16()));

            case DbDataType.Integer:
                return(BitConverter.GetBytes(field.DbValue.GetInt32()));

            case DbDataType.Array:
            case DbDataType.Binary:
            case DbDataType.Text:
            case DbDataType.BigInt:
                return(BitConverter.GetBytes(field.DbValue.GetInt64()));

            case DbDataType.Float:
                return(BitConverter.GetBytes(field.DbValue.GetFloat()));

            case DbDataType.Double:
                return(BitConverter.GetBytes(field.DbValue.GetDouble()));

            case DbDataType.Date:
                return(BitConverter.GetBytes(
                           TypeEncoder.EncodeDate(field.DbValue.GetDateTime())));

            case DbDataType.Time:
                return(BitConverter.GetBytes(
                           TypeEncoder.EncodeTime(field.DbValue.GetDateTime())));

            case DbDataType.TimeStamp:
                byte[] date = BitConverter.GetBytes(
                    TypeEncoder.EncodeDate(field.DbValue.GetDateTime()));

                byte[] time = BitConverter.GetBytes(
                    TypeEncoder.EncodeTime(field.DbValue.GetDateTime()));

                byte[] result = new     byte[8];

                Buffer.BlockCopy(date, 0, result, 0, date.Length);
                Buffer.BlockCopy(time, 0, result, 4, time.Length);

                return(result);

            case DbDataType.Guid:
                return(field.DbValue.GetGuid().ToByteArray());

            default:
                throw new NotSupportedException("Unknown data type");
            }
        }
        private byte[] UserIdentificationData(out SrpClient srp, out SspiHelper sspi)
        {
            srp  = null;
            sspi = null;

            using (var result = new MemoryStream())
            {
                var userString = Environment.GetEnvironmentVariable("USERNAME") ?? Environment.GetEnvironmentVariable("USER") ?? string.Empty;
                var user       = Encoding.UTF8.GetBytes(userString);
                result.WriteByte(IscCodes.CNCT_user);
                result.WriteByte((byte)user.Length);
                result.Write(user, 0, user.Length);

                var host = Encoding.UTF8.GetBytes(Dns.GetHostName());
                result.WriteByte(IscCodes.CNCT_host);
                result.WriteByte((byte)host.Length);
                result.Write(host, 0, host.Length);

                result.WriteByte(IscCodes.CNCT_user_verification);
                result.WriteByte(0);

                if (!string.IsNullOrEmpty(_userID))
                {
                    srp = new SrpClient();

                    var login = Encoding.UTF8.GetBytes(_userID);
                    result.WriteByte(IscCodes.CNCT_login);
                    result.WriteByte((byte)login.Length);
                    result.Write(login, 0, login.Length);

                    var pluginName = Encoding.ASCII.GetBytes(SrpClient.PluginName);
                    result.WriteByte(IscCodes.CNCT_plugin_name);
                    result.WriteByte((byte)pluginName.Length);
                    result.Write(pluginName, 0, pluginName.Length);
                    result.WriteByte(IscCodes.CNCT_plugin_list);
                    result.WriteByte((byte)pluginName.Length);
                    result.Write(pluginName, 0, pluginName.Length);

                    var specificData = Encoding.ASCII.GetBytes(srp.PublicKeyHex);
                    WriteMultiPartHelper(result, IscCodes.CNCT_specific_data, specificData);

                    result.WriteByte(IscCodes.CNCT_client_crypt);
                    result.WriteByte(4);
                    result.Write(TypeEncoder.EncodeInt32(WireCryptOptionValue(_wireCrypt)), 0, 4);
                }
                else
                {
                    sspi = new SspiHelper();

                    var pluginName = Encoding.ASCII.GetBytes(SspiHelper.PluginName);
                    result.WriteByte(IscCodes.CNCT_plugin_name);
                    result.WriteByte((byte)pluginName.Length);
                    result.Write(pluginName, 0, pluginName.Length);
                    result.WriteByte(IscCodes.CNCT_plugin_list);
                    result.WriteByte((byte)pluginName.Length);
                    result.Write(pluginName, 0, pluginName.Length);

                    var specificData = sspi.InitializeClientSecurity();
                    WriteMultiPartHelper(result, IscCodes.CNCT_specific_data, specificData);

                    result.WriteByte(IscCodes.CNCT_client_crypt);
                    result.WriteByte(4);
                    result.Write(TypeEncoder.EncodeInt32(IscCodes.WIRE_CRYPT_DISABLED), 0, 4);
                }

                return(result.ToArray());
            }
        }
Пример #12
0
 public void Write(long value)
 {
     _stream.Write(TypeEncoder.EncodeInt64(value), 0, 8);
 }
Пример #13
0
 public void Write(int value)
 {
     _stream.Write(TypeEncoder.EncodeInt32(value), 0, 4);
 }
Пример #14
0
 public void Write(int value)
 {
     _dataProvider.Write(TypeEncoder.EncodeInt32(value), 0, 4);
 }
 public Task Write(long value, AsyncWrappingCommonArgs async)
 {
     return(async.AsyncSyncCall(_stream.WriteAsync, _stream.Write, TypeEncoder.EncodeInt64(value), 0, 8));
 }
Пример #16
0
 public void Write(long value)
 {
     _dataProvider.WriteAsync(TypeEncoder.EncodeInt64(value), 0, 8);
 }
 public Task Write(bool value, AsyncWrappingCommonArgs async)
 {
     return(WriteOpaque(TypeEncoder.EncodeBoolean(value), async));
 }
Пример #18
0
 public void Write(bool value)
 {
     WriteOpaque(TypeEncoder.EncodeBoolean(value));
 }
 public Task Write(Guid value, AsyncWrappingCommonArgs async)
 {
     return(WriteOpaque(TypeEncoder.EncodeGuid(value), async));
 }
Пример #20
0
 public void Write(Guid value)
 {
     WriteOpaque(TypeEncoder.EncodeGuid(value));
 }
Пример #21
0
 public void WriteDate(DateTime value) => Write(TypeEncoder.EncodeDate(value));
Пример #22
0
        private byte[] EncodeSlice(ArrayDesc desc, Array sourceArray, int length)
        {
            BinaryWriter writer  = new BinaryWriter(new MemoryStream());
            Charset      charset = this.db.Charset;
            DbDataType   dbType  = DbDataType.Array;
            int          subType = (this.Descriptor.Scale < 0) ? 2 : 0;
            int          type    = 0;

            // Infer data types
            type   = TypeHelper.GetFbType(this.Descriptor.DataType);
            dbType = TypeHelper.GetDbDataType(this.Descriptor.DataType, subType, this.Descriptor.Scale);

            foreach (object source in sourceArray)
            {
                switch (dbType)
                {
                case DbDataType.Char:
                {
                    string value  = source != null ? (string)source : string.Empty;
                    byte[] buffer = charset.GetBytes(value);

                    writer.Write(buffer);

                    if (desc.Length > buffer.Length)
                    {
                        for (int j = buffer.Length; j < desc.Length; j++)
                        {
                            writer.Write((byte)32);
                        }
                    }
                }
                break;

                case DbDataType.VarChar:
                {
                    string value = source != null ? (string)source : string.Empty;

                    byte[] buffer = charset.GetBytes(value);
                    writer.Write(buffer);

                    if (desc.Length > buffer.Length)
                    {
                        for (int j = buffer.Length; j < desc.Length; j++)
                        {
                            writer.Write((byte)0);
                        }
                    }
                    writer.Write((short)0);
                }
                break;

                case DbDataType.SmallInt:
                    writer.Write((short)source);
                    break;

                case DbDataType.Integer:
                    writer.Write((int)source);
                    break;

                case DbDataType.BigInt:
                    writer.Write((long)source);
                    break;

                case DbDataType.Float:
                    writer.Write((float)source);
                    break;

                case DbDataType.Double:
                    writer.Write((double)source);
                    break;

                case DbDataType.Numeric:
                case DbDataType.Decimal:
                {
                    object numeric = TypeEncoder.EncodeDecimal((decimal)source, desc.Scale, type);

                    switch (type)
                    {
                    case IscCodes.SQL_SHORT:
                        writer.Write((short)numeric);
                        break;

                    case IscCodes.SQL_LONG:
                        writer.Write((int)numeric);
                        break;

                    case IscCodes.SQL_QUAD:
                    case IscCodes.SQL_INT64:
                        writer.Write((long)numeric);
                        break;
                    }
                }
                break;

                case DbDataType.Date:
                    writer.Write(TypeEncoder.EncodeDate(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat)));
                    break;

                case DbDataType.Time:
                    writer.Write(TypeEncoder.EncodeTime((TimeSpan)source));
                    break;

                case DbDataType.TimeStamp:
                    var dt = Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat);
                    writer.Write(TypeEncoder.EncodeDate(dt));
                    writer.Write(TypeEncoder.EncodeTime(TypeHelper.DateTimeToTimeSpan(dt)));
                    break;

                default:
                    throw new NotSupportedException("Unknown data type");
                }
            }

            return(((MemoryStream)writer.BaseStream).ToArray());
        }
 public Task Write(int value, AsyncWrappingCommonArgs async)
 {
     return(_dataProvider.Write(TypeEncoder.EncodeInt32(value), 0, 4, async));
 }