1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
// Copyright © 2004,2010, Oracle and/or its affiliates. All rights reserved.
//
// MySQL Connector/NET is licensed under the terms of the GPLv2
// <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
// MySQL Connectors. There are special exceptions to the terms and
// conditions of the GPLv2 as it is applied to this software, see the
// FLOSS License Exception
// <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
using System;
using System.Diagnostics;
using System.Text;
using System.IO;
using MySql.Data.Common;
using MySql.Data.MySqlClient.Properties;
namespace MySql.Data.MySqlClient
{
class MySqlPacket
{
private byte[] tempBuffer = new byte[256];
private Encoding encoding;
private MemoryStream buffer = new MemoryStream(5);
private DBVersion version;
private MySqlPacket()
{
Clear();
}
public MySqlPacket(Encoding enc) : this()
{
Encoding = enc;
}
public MySqlPacket(MemoryStream stream)
: this()
{
buffer = stream;
}
#region Properties
public Encoding Encoding
{
get { return encoding; }
set
{
Debug.Assert(value != null);
encoding = value;
}
}
public bool HasMoreData
{
get { return buffer.Position < buffer.Length; }
}
public int Position
{
get { return (int)buffer.Position; }
set { buffer.Position = (long)value; }
}
public int Length
{
get { return (int)buffer.Length; }
set { buffer.SetLength(value); }
}
public bool IsLastPacket
{
get
{
byte[] bits = buffer.GetBuffer();
return bits[0] == 0xfe && Length <= 5;
}
}
public byte[] Buffer
{
get { return buffer.GetBuffer(); }
}
public DBVersion Version
{
get { return version; }
set { version = value; }
}
#endregion
public void Clear()
{
Position = 4;
}
#region Byte methods
public byte ReadByte()
{
return (byte)buffer.ReadByte();
}
public int Read(byte[] byteBuffer, int offset, int count)
{
return buffer.Read(byteBuffer, offset, count);
}
public void WriteByte(byte b)
{
buffer.WriteByte(b);
}
public void Write(byte[] bytesToWrite)
{
Write(bytesToWrite, 0, bytesToWrite.Length);
}
public void Write(byte[] bytesToWrite, int offset, int countToWrite)
{
buffer.Write(bytesToWrite, offset, countToWrite);
}
public int ReadNBytes()
{
byte c = ReadByte();
if (c < 1 || c > 4)
throw new MySqlException(Resources.IncorrectTransmission);
return ReadInteger(c);
}
#endregion
#region Integer methods
public long ReadFieldLength()
{
byte c = ReadByte();
switch (c)
{
case 251: return -1;
case 252: return ReadInteger(2);
case 253: return ReadInteger(3);
case 254: return ReadLong(8);
default: return c;
}
}
public ulong ReadBitValue(int numbytes)
{
ulong value = 0;
int pos = (int)buffer.Position;
byte[] bits = buffer.GetBuffer();
int shift = 0;
for (int i = 0; i < numbytes; i++)
{
value <<= shift;
value |= bits[pos++];
shift = 8;
}
buffer.Position += numbytes;
return value;
}
public long ReadLong(int numbytes)
{
Debug.Assert((buffer.Position + numbytes) <= buffer.Length);
byte[] bytes = buffer.GetBuffer();
int pos = (int)buffer.Position;
buffer.Position += numbytes;
switch (numbytes)
{
case 2: return BitConverter.ToUInt16(bytes, pos);
case 4: return BitConverter.ToUInt32(bytes, pos);
case 8: return BitConverter.ToInt64(bytes, pos);
}
throw new NotSupportedException("Only byte lengths of 2, 4, or 8 are supported");
}
public ulong ReadULong(int numbytes)
{
Debug.Assert((buffer.Position + numbytes) <= buffer.Length);
byte[] bytes = buffer.GetBuffer();
int pos = (int)buffer.Position;
buffer.Position += numbytes;
switch (numbytes)
{
case 2: return BitConverter.ToUInt16(bytes, pos);
case 4: return BitConverter.ToUInt32(bytes, pos);
case 8: return BitConverter.ToUInt64(bytes, pos);
}
throw new NotSupportedException("Only byte lengths of 2, 4, or 8 are supported");
}
public int Read3ByteInt()
{
int value = 0;
int pos = (int)buffer.Position;
byte[] bits = buffer.GetBuffer();
int shift = 0;
for (int i = 0; i < 3; i++)
{
value |= (int)(bits[pos++] << shift);
shift += 8;
}
buffer.Position += 3;
return value;
}
public int ReadInteger(int numbytes)
{
if (numbytes == 3)
return Read3ByteInt();
Debug.Assert(numbytes <= 4);
return (int)ReadLong(numbytes);
}
/// <summary>
/// WriteInteger
/// </summary>
/// <param name="v"></param>
/// <param name="numbytes"></param>
public void WriteInteger(long v, int numbytes)
{
long val = v;
Debug.Assert(numbytes > 0 && numbytes < 9);
for (int x = 0; x < numbytes; x++)
{
tempBuffer[x] = (byte)(val & 0xff);
val >>= 8;
}
Write(tempBuffer, 0, numbytes);
}
public int ReadPackedInteger()
{
byte c = ReadByte();
switch (c)
{
case 251: return -1;
case 252: return ReadInteger(2);
case 253: return ReadInteger(3);
case 254: return ReadInteger(4);
default: return c;
}
}
public void WriteLength(long length)
{
if (length < 251)
WriteByte((byte)length);
else if (length < 65536L)
{
WriteByte(252);
WriteInteger(length, 2);
}
else if (length < 16777216L)
{
WriteByte(253);
WriteInteger(length, 3);
}
else
{
WriteByte(254);
WriteInteger(length, 4);
}
}
#endregion
#region String methods
public void WriteLenString(string s)
{
byte[] bytes = encoding.GetBytes(s);
WriteLength(bytes.Length);
Write(bytes, 0, bytes.Length);
}
public void WriteStringNoNull(string v)
{
byte[] bytes = encoding.GetBytes(v);
Write(bytes, 0, bytes.Length);
}
public void WriteString(string v)
{
WriteStringNoNull(v);
WriteByte(0);
}
public string ReadLenString()
{
long len = ReadPackedInteger();
return ReadString(len);
}
public string ReadAsciiString(long length)
{
if (length == 0)
return String.Empty;
// byte[] buf = new byte[length];
Read(tempBuffer, 0, (int)length);
return ASCIIEncoding.ASCII.GetString(tempBuffer, 0, (int)length);
//return encoding.GetString(tempBuffer, 0, (int)length); //buf.Length);
}
public string ReadString(long length)
{
if (length == 0)
return String.Empty;
if (tempBuffer == null || length > tempBuffer.Length)
tempBuffer = new byte[length];
Read(tempBuffer, 0, (int)length);
return encoding.GetString(tempBuffer, 0, (int)length);
}
public string ReadString(Encoding enc)
{
byte[] bits = buffer.GetBuffer();
int end = (int)buffer.Position;
while (end < (int)buffer.Length &&
bits[end] != 0 && (int)bits[end] != -1)
end++;
string s = enc.GetString(bits,
(int)buffer.Position, end - (int)buffer.Position);
buffer.Position = end + 1;
return s;
}
public string ReadString()
{
return ReadString(this.encoding);
}
#endregion
}
}
|