aboutsummaryrefslogtreecommitdiffhomepage
path: root/packet.c
diff options
context:
space:
mode:
authoreihrul <eihrul>2006-07-06 21:22:41 +0000
committereihrul <eihrul>2006-07-06 21:22:41 +0000
commit1d6253cc3d63801cc4077ff4621e31cf53cc75f9 (patch)
tree7ec5c09bcc3f39afa21c72ca1a9efc3d8caaa656 /packet.c
parent1e2c45adf961f878b29b5df111153ee9a084c9d3 (diff)
downloadenet-1d6253cc3d63801cc4077ff4621e31cf53cc75f9.tar.gz
enet-1d6253cc3d63801cc4077ff4621e31cf53cc75f9.zip
*** empty log message ***
Diffstat (limited to 'packet.c')
-rw-r--r--packet.c65
1 files changed, 60 insertions, 5 deletions
diff --git a/packet.c b/packet.c
index 5226550..77b5583 100644
--- a/packet.c
+++ b/packet.c
@@ -21,10 +21,15 @@ enet_packet_create (const void * data, size_t dataLength, enet_uint32 flags)
{
ENetPacket * packet = (ENetPacket *) enet_malloc (sizeof (ENetPacket));
- packet -> data = (enet_uint8 *) enet_malloc (dataLength);
+ if(flags & ENET_PACKET_FLAG_NO_ALLOCATE)
+ packet -> data = (enet_uint8 *) data;
+ else
+ {
+ packet -> data = (enet_uint8 *) enet_malloc (dataLength);
- if (data != NULL)
- memcpy (packet -> data, data, dataLength);
+ if (data != NULL)
+ memcpy (packet -> data, data, dataLength);
+ };
packet -> referenceCount = 0;
packet -> flags = flags;
@@ -39,7 +44,8 @@ enet_packet_create (const void * data, size_t dataLength, enet_uint32 flags)
void
enet_packet_destroy (ENetPacket * packet)
{
- enet_free (packet -> data);
+ if((packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE) == 0)
+ enet_free (packet -> data);
enet_free (packet);
}
@@ -54,7 +60,7 @@ enet_packet_resize (ENetPacket * packet, size_t dataLength)
{
enet_uint8 * newData;
- if (dataLength <= packet -> dataLength)
+ if (dataLength <= packet -> dataLength || (packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE))
{
packet -> dataLength = dataLength;
@@ -71,4 +77,53 @@ enet_packet_resize (ENetPacket * packet, size_t dataLength)
return 0;
}
+static int initializedCRC32 = 0;
+static enet_uint32 crcTable [256];
+
+static void initialize_crc32 ()
+{
+ int byte;
+
+ for (byte = 0; byte < 256; ++ byte)
+ {
+ enet_uint32 crc = byte << 24;
+ int offset;
+
+ for(offset = 0; offset < 8; ++ offset)
+ {
+ if (crc & 0x80000000)
+ crc = (crc << 1) ^ 0x04c11db7;
+ else
+ crc <<= 1;
+ }
+
+ crcTable [byte] = crc;
+ }
+
+ initializedCRC32 = 1;
+}
+
+enet_uint32
+enet_crc32 (const ENetBuffer * buffers, size_t bufferCount)
+{
+ enet_uint32 crc = 0xFFFFFFFF;
+
+ if (! initializedCRC32) initialize_crc32 ();
+
+ while (bufferCount -- > 0)
+ {
+ const enet_uint8 * data = (const enet_uint8 *) buffers -> data,
+ * dataEnd = & data [buffers -> dataLength];
+
+ while (data < dataEnd)
+ {
+ crc = ((crc << 8) | * data ++) ^ crcTable [crc >> 24];
+ }
+
+ ++ buffers;
+ }
+
+ return ENET_HOST_TO_NET_32 (~ crc);
+}
+
/** @} */