Example #1
0
        private MemoryStream modifyDataRow(DataRowMessage dataRow, RowDescriptionMessage rowDescription)
        {
            Func <string, string> maskingFunction;
            var ms = new MemoryStream();

            ms.Write(EndianHelpers.ToBigE(dataRow.NumFields));

            int i = 0;

            foreach (var f in dataRow.DataRowFields)
            {
                uint   tableOid    = rowDescription.FieldDescriptions[i].ObjectId;
                string columnName  = rowDescription.FieldDescriptions[i].FieldName;
                uint   dataTypeOid = rowDescription.FieldDescriptions[i].DataTypeObjectId;

                //If the column vale is null or we are preserving keyed column values and the column is either a primary or foreign key.
                if (f.ColumnValueLength == -1 || (_state._maskingModel.PreserveKeys && _state.isColumnKeyed(tableOid, columnName)))
                {
                    ms.Write(EndianHelpers.ToBigE(f.ColumnValueLength));
                    ms.Write(f.ColumnValue);
                }
                else
                {
                    maskingFunction = getMaskingFunction(tableOid, dataTypeOid, columnName);
                    string rowValue  = System.Text.Encoding.UTF8.GetString(f.ColumnValue);
                    byte[] newBytes  = System.Text.Encoding.UTF8.GetBytes(maskingFunction(rowValue));
                    byte[] newLength = EndianHelpers.ToBigE(newBytes.Length);
                    ms.Write(newLength);
                    ms.Write(newBytes);
                }
                i++;
            }
            return(ms);
        }
        private byte[] handleDataRow(byte[] buffer, int messageLength)
        {
            var dataRow = new DataRowMessage();

            int    offset    = 5; //1 byte for message type and 4 bytes for the int32 that states the length.
            ushort numFields = EndianHelpers.SwapEndianness(BitConverter.ToUInt16(buffer, 5));

            dataRow.NumFields = numFields;
            if (numFields != 0)
            {
                dataRow.DataRowFields = new List <DataRowField>(numFields);
            }
            offset += 2;
            for (int i = 0; i < numFields; i++)
            {
                var dataRowField      = new DataRowField();
                int columnValueLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset));
                dataRowField.ColumnValueLength = columnValueLength;
                offset += 4;
                if (columnValueLength != -1)
                {
                    dataRowField.ColumnValue = new byte[columnValueLength];
                    Buffer.BlockCopy(buffer, offset, dataRowField.ColumnValue, 0, columnValueLength);
                    var colValue = Encoding.ASCII.GetString(buffer, offset, columnValueLength);
                    offset += columnValueLength;
                }
                dataRow.DataRowFields.Add(dataRowField);
            }
            var modifiedResultStream = _modifyDataRow(dataRow, currentRowDescription);

            modifiedResultStream.Seek(0, SeekOrigin.Begin);
            var message = new byte[modifiedResultStream.Length + 5];

            message[0] = buffer[0];

            var newLength = EndianHelpers.ToBigE((int)modifiedResultStream.Length + 4);

            message[1] = newLength[0];
            message[2] = newLength[1];
            message[3] = newLength[2];
            message[4] = newLength[3];
            modifiedResultStream.Read(message, 5, (int)modifiedResultStream.Length);
            return(message);
        }