1 /**
2  @file  packet.c
3  @brief ENet packet management functions
4 */
5 #include <string.h>
6 #define ENET_BUILDING_LIB 1
7 #include "enet/enet.h"
8 
9 /** @defgroup Packet ENet packet functions
10     @{
11 */
12 
13 /** Creates a packet that may be sent to a peer.
14     @param dataContents initial contents of the packet's data; the packet's data will remain uninitialized if dataContents is NULL.
15     @param dataLength   size of the data allocated for this packet
16     @param flags        flags for this packet as described for the ENetPacket structure.
17     @returns the packet on success, NULL on failure
18 */
19 ENetPacket *
enet_packet_create(const void * data,size_t dataLength,enet_uint32 flags)20 enet_packet_create(const void *data, size_t dataLength, enet_uint32 flags)
21 {
22 	ENetPacket *packet = (ENetPacket *)enet_malloc(sizeof(ENetPacket));
23 	if (packet == NULL)
24 		return NULL;
25 
26 	if (flags & ENET_PACKET_FLAG_NO_ALLOCATE)
27 		packet->data = (enet_uint8 *)data;
28 	else if (dataLength <= 0)
29 		packet->data = NULL;
30 	else
31 	{
32 		packet->data = (enet_uint8 *)enet_malloc(dataLength);
33 		if (packet->data == NULL)
34 		{
35 			enet_free(packet);
36 			return NULL;
37 		}
38 
39 		if (data != NULL)
40 			memcpy(packet->data, data, dataLength);
41 	}
42 
43 	packet->referenceCount = 0;
44 	packet->flags = flags;
45 	packet->dataLength = dataLength;
46 	packet->freeCallback = NULL;
47 	packet->userData = NULL;
48 
49 	return packet;
50 }
51 
52 /** Destroys the packet and deallocates its data.
53     @param packet packet to be destroyed
54 */
enet_packet_destroy(ENetPacket * packet)55 void enet_packet_destroy(ENetPacket *packet)
56 {
57 	if (packet == NULL)
58 		return;
59 
60 	if (packet->freeCallback != NULL)
61 		(*packet->freeCallback)(packet);
62 	if (!(packet->flags & ENET_PACKET_FLAG_NO_ALLOCATE) &&
63 		packet->data != NULL)
64 		enet_free(packet->data);
65 	enet_free(packet);
66 }
67 
68 /** Attempts to resize the data in the packet to length specified in the
69     dataLength parameter
70     @param packet packet to resize
71     @param dataLength new size for the packet data
72     @returns 0 on success, < 0 on failure
73 */
enet_packet_resize(ENetPacket * packet,size_t dataLength)74 int enet_packet_resize(ENetPacket *packet, size_t dataLength)
75 {
76 	enet_uint8 *newData;
77 
78 	if (dataLength <= packet->dataLength || (packet->flags & ENET_PACKET_FLAG_NO_ALLOCATE))
79 	{
80 		packet->dataLength = dataLength;
81 
82 		return 0;
83 	}
84 
85 	newData = (enet_uint8 *)enet_malloc(dataLength);
86 	if (newData == NULL)
87 		return -1;
88 
89 	memcpy(newData, packet->data, packet->dataLength);
90 	enet_free(packet->data);
91 
92 	packet->data = newData;
93 	packet->dataLength = dataLength;
94 
95 	return 0;
96 }
97 
98 static int initializedCRC32 = 0;
99 static enet_uint32 crcTable[256];
100 
101 static enet_uint32
reflect_crc(int val,int bits)102 reflect_crc(int val, int bits)
103 {
104 	int result = 0, bit;
105 
106 	for (bit = 0; bit < bits; bit++)
107 	{
108 		if (val & 1) result |= 1 << (bits - 1 - bit);
109 		val >>= 1;
110 	}
111 
112 	return result;
113 }
114 
115 static void
initialize_crc32(void)116 initialize_crc32(void)
117 {
118 	int byte;
119 
120 	for (byte = 0; byte < 256; ++byte)
121 	{
122 		enet_uint32 crc = reflect_crc(byte, 8) << 24;
123 		int offset;
124 
125 		for (offset = 0; offset < 8; ++offset)
126 		{
127 			if (crc & 0x80000000)
128 				crc = (crc << 1) ^ 0x04c11db7;
129 			else
130 				crc <<= 1;
131 		}
132 
133 		crcTable[byte] = reflect_crc(crc, 32);
134 	}
135 
136 	initializedCRC32 = 1;
137 }
138 
139 enet_uint32
enet_crc32(const ENetBuffer * buffers,size_t bufferCount)140 enet_crc32(const ENetBuffer *buffers, size_t bufferCount)
141 {
142 	enet_uint32 crc = 0xFFFFFFFF;
143 
144 	if (!initializedCRC32) initialize_crc32();
145 
146 	while (bufferCount-- > 0)
147 	{
148 		const enet_uint8 *data = (const enet_uint8 *)buffers->data,
149 						 *dataEnd = &data[buffers->dataLength];
150 
151 		while (data < dataEnd)
152 		{
153 			crc = (crc >> 8) ^ crcTable[(crc & 0xFF) ^ *data++];
154 		}
155 
156 		++buffers;
157 	}
158 
159 	return ENET_HOST_TO_NET_32(~crc);
160 }
161 
162 /** @} */
163