1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-06 02:11:06 +09:00

BIGENDIAN fixes for 16 bit values

This commit is contained in:
lidgren
2012-05-26 08:12:55 +00:00
parent ae70354832
commit 35a0831a83
4 changed files with 91 additions and 15 deletions

View File

@@ -1,4 +1,6 @@
/* Copyright (c) 2010 Michael Lidgren
//#define UNSAFE
//#define BIGENDIAN
/* Copyright (c) 2010 Michael Lidgren
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without
@@ -169,6 +171,49 @@ namespace Lidgren.Network
return;
}
[CLSCompliant(false)]
#if UNSAFE
public static unsafe ushort ReadUInt16(byte[] fromBuffer, int numberOfBits, int readBitOffset)
{
Debug.Assert(((numberOfBits > 0) && (numberOfBits <= 16)), "ReadUInt16() can only read between 1 and 16 bits");
if (numberOfBits == 16 && ((readBitOffset % 8) == 0))
{
fixed (byte* ptr = &(fromBuffer[readBitOffset / 8]))
{
return *(((ushort*)ptr));
}
}
#else
public static ushort ReadUInt16(byte[] fromBuffer, int numberOfBits, int readBitOffset)
{
Debug.Assert(((numberOfBits > 0) && (numberOfBits <= 16)), "ReadUInt16() can only read between 1 and 16 bits");
#endif
ushort returnValue;
if (numberOfBits <= 8)
{
returnValue = ReadByte(fromBuffer, numberOfBits, readBitOffset);
return returnValue;
}
returnValue = ReadByte(fromBuffer, 8, readBitOffset);
numberOfBits -= 8;
readBitOffset += 8;
if (numberOfBits <= 8)
{
returnValue |= (ushort)(ReadByte(fromBuffer, numberOfBits, readBitOffset) << 8);
}
#if BIGENDIAN
// reorder bytes
uint retVal = returnValue;
retVal = ((retVal & 0x0000ff00) >> 8) | ((retVal & 0x000000ff) << 8);
return (ushort)retVal;
#else
return returnValue;
#endif
}
/// <summary>
/// Reads the specified number of bits into an UInt32
/// </summary>
@@ -226,18 +271,47 @@ namespace Lidgren.Network
#if BIGENDIAN
// reorder bytes
return
((a & 0xff000000) >> 24) |
((a & 0x00ff0000) >> 8) |
((a & 0x0000ff00) << 8) |
((a & 0x000000ff) << 24);
#endif
((returnValue & 0xff000000) >> 24) |
((returnValue & 0x00ff0000) >> 8) |
((returnValue & 0x0000ff00) << 8) |
((returnValue & 0x000000ff) << 24);
#else
return returnValue;
#endif
}
//[CLSCompliant(false)]
//public static ulong ReadUInt64(byte[] fromBuffer, int numberOfBits, int readBitOffset)
[CLSCompliant(false)]
public static int WriteUInt16(ushort source, int numberOfBits, byte[] destination, int destinationBitOffset)
{
#if BIGENDIAN
// reorder bytes
uint intSource = source;
intSource = ((intSource & 0x0000ff00) >> 8) | ((intSource & 0x000000ff) << 8);
source = (ushort)intSource;
#endif
int returnValue = destinationBitOffset + numberOfBits;
if (numberOfBits <= 8)
{
NetBitWriter.WriteByte((byte)source, numberOfBits, destination, destinationBitOffset);
return returnValue;
}
NetBitWriter.WriteByte((byte)source, 8, destination, destinationBitOffset);
destinationBitOffset += 8;
numberOfBits -= 8;
if (numberOfBits <= 8)
{
NetBitWriter.WriteByte((byte)(source >> 8), numberOfBits, destination, destinationBitOffset);
return returnValue;
}
return returnValue;
}
/// <summary>
/// Writes the specified number of bits into a byte array
/// </summary>

View File

@@ -109,7 +109,7 @@ namespace Lidgren.Network
public Int16 PeekInt16()
{
NetException.Assert(m_bitLength - m_readPosition >= 16, c_readOverflowError);
uint retval = NetBitWriter.ReadUInt32(m_data, 16, m_readPosition);
uint retval = NetBitWriter.ReadUInt16(m_data, 16, m_readPosition);
return (short)retval;
}
@@ -120,7 +120,7 @@ namespace Lidgren.Network
public UInt16 PeekUInt16()
{
NetException.Assert(m_bitLength - m_readPosition >= 16, c_readOverflowError);
uint retval = NetBitWriter.ReadUInt32(m_data, 16, m_readPosition);
uint retval = NetBitWriter.ReadUInt16(m_data, 16, m_readPosition);
return (ushort)retval;
}

View File

@@ -146,7 +146,7 @@ namespace Lidgren.Network
public Int16 ReadInt16()
{
NetException.Assert(m_bitLength - m_readPosition >= 16, c_readOverflowError);
uint retval = NetBitWriter.ReadUInt32(m_data, 16, m_readPosition);
uint retval = NetBitWriter.ReadUInt16(m_data, 16, m_readPosition);
m_readPosition += 16;
return (short)retval;
}
@@ -158,7 +158,7 @@ namespace Lidgren.Network
public UInt16 ReadUInt16()
{
NetException.Assert(m_bitLength - m_readPosition >= 16, c_readOverflowError);
uint retval = NetBitWriter.ReadUInt32(m_data, 16, m_readPosition);
uint retval = NetBitWriter.ReadUInt16(m_data, 16, m_readPosition);
m_readPosition += 16;
return (ushort)retval;
}

View File

@@ -1,4 +1,6 @@
/* Copyright (c) 2010 Michael Lidgren
//#define UNSAFE
//#define BIGENDIAN
/* Copyright (c) 2010 Michael Lidgren
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without
@@ -146,7 +148,7 @@ namespace Lidgren.Network
public void Write(UInt16 source)
{
EnsureBufferSize(m_bitLength + 16);
NetBitWriter.WriteUInt32((uint)source, 16, m_data, m_bitLength);
NetBitWriter.WriteUInt16(source, 16, m_data, m_bitLength);
m_bitLength += 16;
}
@@ -158,7 +160,7 @@ namespace Lidgren.Network
{
NetException.Assert((numberOfBits > 0 && numberOfBits <= 16), "Write(ushort, numberOfBits) can only write between 1 and 16 bits");
EnsureBufferSize(m_bitLength + numberOfBits);
NetBitWriter.WriteUInt32((uint)source, numberOfBits, m_data, m_bitLength);
NetBitWriter.WriteUInt16(source, numberOfBits, m_data, m_bitLength);
m_bitLength += numberOfBits;
}
@@ -168,7 +170,7 @@ namespace Lidgren.Network
public void Write(Int16 source)
{
EnsureBufferSize(m_bitLength + 16);
NetBitWriter.WriteUInt32((uint)source, 16, m_data, m_bitLength);
NetBitWriter.WriteUInt16((ushort)source, 16, m_data, m_bitLength);
m_bitLength += 16;
}