Code-style consistency improvement:
Apply clang-format-all.sh using the _clang-format file through all the cpp/.h files. make sure not to apply it to certain serialization structures, since some parser expects the * as part of the name, instead of type. This commit contains no other changes aside from adding and applying clang-format-all.sh
This commit is contained in:
@@ -17,54 +17,52 @@
|
||||
@returns the packet on success, NULL on failure
|
||||
*/
|
||||
ENetPacket *
|
||||
enet_packet_create (const void * data, size_t dataLength, enet_uint32 flags)
|
||||
enet_packet_create(const void *data, size_t dataLength, enet_uint32 flags)
|
||||
{
|
||||
ENetPacket * packet = (ENetPacket *) enet_malloc (sizeof (ENetPacket));
|
||||
if (packet == NULL)
|
||||
return NULL;
|
||||
ENetPacket *packet = (ENetPacket *)enet_malloc(sizeof(ENetPacket));
|
||||
if (packet == NULL)
|
||||
return NULL;
|
||||
|
||||
if (flags & ENET_PACKET_FLAG_NO_ALLOCATE)
|
||||
packet -> data = (enet_uint8 *) data;
|
||||
else
|
||||
if (dataLength <= 0)
|
||||
packet -> data = NULL;
|
||||
else
|
||||
{
|
||||
packet -> data = (enet_uint8 *) enet_malloc (dataLength);
|
||||
if (packet -> data == NULL)
|
||||
{
|
||||
enet_free (packet);
|
||||
return NULL;
|
||||
}
|
||||
if (flags & ENET_PACKET_FLAG_NO_ALLOCATE)
|
||||
packet->data = (enet_uint8 *)data;
|
||||
else if (dataLength <= 0)
|
||||
packet->data = NULL;
|
||||
else
|
||||
{
|
||||
packet->data = (enet_uint8 *)enet_malloc(dataLength);
|
||||
if (packet->data == NULL)
|
||||
{
|
||||
enet_free(packet);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (data != NULL)
|
||||
memcpy (packet -> data, data, dataLength);
|
||||
}
|
||||
if (data != NULL)
|
||||
memcpy(packet->data, data, dataLength);
|
||||
}
|
||||
|
||||
packet -> referenceCount = 0;
|
||||
packet -> flags = flags;
|
||||
packet -> dataLength = dataLength;
|
||||
packet -> freeCallback = NULL;
|
||||
packet -> userData = NULL;
|
||||
packet->referenceCount = 0;
|
||||
packet->flags = flags;
|
||||
packet->dataLength = dataLength;
|
||||
packet->freeCallback = NULL;
|
||||
packet->userData = NULL;
|
||||
|
||||
return packet;
|
||||
return packet;
|
||||
}
|
||||
|
||||
/** Destroys the packet and deallocates its data.
|
||||
@param packet packet to be destroyed
|
||||
*/
|
||||
void
|
||||
enet_packet_destroy (ENetPacket * packet)
|
||||
void enet_packet_destroy(ENetPacket *packet)
|
||||
{
|
||||
if (packet == NULL)
|
||||
return;
|
||||
if (packet == NULL)
|
||||
return;
|
||||
|
||||
if (packet -> freeCallback != NULL)
|
||||
(* packet -> freeCallback) (packet);
|
||||
if (! (packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE) &&
|
||||
packet -> data != NULL)
|
||||
enet_free (packet -> data);
|
||||
enet_free (packet);
|
||||
if (packet->freeCallback != NULL)
|
||||
(*packet->freeCallback)(packet);
|
||||
if (!(packet->flags & ENET_PACKET_FLAG_NO_ALLOCATE) &&
|
||||
packet->data != NULL)
|
||||
enet_free(packet->data);
|
||||
enet_free(packet);
|
||||
}
|
||||
|
||||
/** Attempts to resize the data in the packet to length specified in the
|
||||
@@ -73,93 +71,92 @@ enet_packet_destroy (ENetPacket * packet)
|
||||
@param dataLength new size for the packet data
|
||||
@returns 0 on success, < 0 on failure
|
||||
*/
|
||||
int
|
||||
enet_packet_resize (ENetPacket * packet, size_t dataLength)
|
||||
int enet_packet_resize(ENetPacket *packet, size_t dataLength)
|
||||
{
|
||||
enet_uint8 * newData;
|
||||
|
||||
if (dataLength <= packet -> dataLength || (packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE))
|
||||
{
|
||||
packet -> dataLength = dataLength;
|
||||
enet_uint8 *newData;
|
||||
|
||||
return 0;
|
||||
}
|
||||
if (dataLength <= packet->dataLength || (packet->flags & ENET_PACKET_FLAG_NO_ALLOCATE))
|
||||
{
|
||||
packet->dataLength = dataLength;
|
||||
|
||||
newData = (enet_uint8 *) enet_malloc (dataLength);
|
||||
if (newData == NULL)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy (newData, packet -> data, packet -> dataLength);
|
||||
enet_free (packet -> data);
|
||||
|
||||
packet -> data = newData;
|
||||
packet -> dataLength = dataLength;
|
||||
newData = (enet_uint8 *)enet_malloc(dataLength);
|
||||
if (newData == NULL)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
memcpy(newData, packet->data, packet->dataLength);
|
||||
enet_free(packet->data);
|
||||
|
||||
packet->data = newData;
|
||||
packet->dataLength = dataLength;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int initializedCRC32 = 0;
|
||||
static enet_uint32 crcTable [256];
|
||||
static enet_uint32 crcTable[256];
|
||||
|
||||
static enet_uint32
|
||||
reflect_crc (int val, int bits)
|
||||
static enet_uint32
|
||||
reflect_crc(int val, int bits)
|
||||
{
|
||||
int result = 0, bit;
|
||||
int result = 0, bit;
|
||||
|
||||
for (bit = 0; bit < bits; bit ++)
|
||||
{
|
||||
if(val & 1) result |= 1 << (bits - 1 - bit);
|
||||
val >>= 1;
|
||||
}
|
||||
for (bit = 0; bit < bits; bit++)
|
||||
{
|
||||
if (val & 1) result |= 1 << (bits - 1 - bit);
|
||||
val >>= 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
static void
|
||||
initialize_crc32 (void)
|
||||
static void
|
||||
initialize_crc32(void)
|
||||
{
|
||||
int byte;
|
||||
int byte;
|
||||
|
||||
for (byte = 0; byte < 256; ++ byte)
|
||||
{
|
||||
enet_uint32 crc = reflect_crc (byte, 8) << 24;
|
||||
int offset;
|
||||
for (byte = 0; byte < 256; ++byte)
|
||||
{
|
||||
enet_uint32 crc = reflect_crc(byte, 8) << 24;
|
||||
int offset;
|
||||
|
||||
for(offset = 0; offset < 8; ++ offset)
|
||||
{
|
||||
if (crc & 0x80000000)
|
||||
crc = (crc << 1) ^ 0x04c11db7;
|
||||
else
|
||||
crc <<= 1;
|
||||
}
|
||||
for (offset = 0; offset < 8; ++offset)
|
||||
{
|
||||
if (crc & 0x80000000)
|
||||
crc = (crc << 1) ^ 0x04c11db7;
|
||||
else
|
||||
crc <<= 1;
|
||||
}
|
||||
|
||||
crcTable [byte] = reflect_crc (crc, 32);
|
||||
}
|
||||
crcTable[byte] = reflect_crc(crc, 32);
|
||||
}
|
||||
|
||||
initializedCRC32 = 1;
|
||||
initializedCRC32 = 1;
|
||||
}
|
||||
|
||||
|
||||
enet_uint32
|
||||
enet_crc32 (const ENetBuffer * buffers, size_t bufferCount)
|
||||
enet_crc32(const ENetBuffer *buffers, size_t bufferCount)
|
||||
{
|
||||
enet_uint32 crc = 0xFFFFFFFF;
|
||||
|
||||
if (! initializedCRC32) initialize_crc32 ();
|
||||
enet_uint32 crc = 0xFFFFFFFF;
|
||||
|
||||
while (bufferCount -- > 0)
|
||||
{
|
||||
const enet_uint8 * data = (const enet_uint8 *) buffers -> data,
|
||||
* dataEnd = & data [buffers -> dataLength];
|
||||
if (!initializedCRC32) initialize_crc32();
|
||||
|
||||
while (data < dataEnd)
|
||||
{
|
||||
crc = (crc >> 8) ^ crcTable [(crc & 0xFF) ^ *data++];
|
||||
}
|
||||
while (bufferCount-- > 0)
|
||||
{
|
||||
const enet_uint8 *data = (const enet_uint8 *)buffers->data,
|
||||
*dataEnd = &data[buffers->dataLength];
|
||||
|
||||
++ buffers;
|
||||
}
|
||||
while (data < dataEnd)
|
||||
{
|
||||
crc = (crc >> 8) ^ crcTable[(crc & 0xFF) ^ *data++];
|
||||
}
|
||||
|
||||
return ENET_HOST_TO_NET_32 (~ crc);
|
||||
++buffers;
|
||||
}
|
||||
|
||||
return ENET_HOST_TO_NET_32(~crc);
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
Reference in New Issue
Block a user