[13] | 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 * |
---|
| 20 | enet_packet_create (const void * data, size_t dataLength, enet_uint32 flags) |
---|
| 21 | { |
---|
| 22 | ENetPacket * packet = (ENetPacket *) enet_malloc (sizeof (ENetPacket)); |
---|
| 23 | |
---|
| 24 | if (flags & ENET_PACKET_FLAG_NO_ALLOCATE) |
---|
| 25 | packet -> data = (enet_uint8 *) data; |
---|
| 26 | else |
---|
| 27 | { |
---|
| 28 | packet -> data = (enet_uint8 *) enet_malloc (dataLength); |
---|
| 29 | if (packet -> data == NULL) |
---|
| 30 | { |
---|
| 31 | enet_free (packet); |
---|
| 32 | return NULL; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | if (data != NULL) |
---|
| 36 | memcpy (packet -> data, data, dataLength); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | packet -> referenceCount = 0; |
---|
| 40 | packet -> flags = flags; |
---|
| 41 | packet -> dataLength = dataLength; |
---|
| 42 | packet -> freeCallback = NULL; |
---|
| 43 | |
---|
| 44 | return packet; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /** Destroys the packet and deallocates its data. |
---|
| 48 | @param packet packet to be destroyed |
---|
| 49 | */ |
---|
| 50 | void |
---|
| 51 | enet_packet_destroy (ENetPacket * packet) |
---|
| 52 | { |
---|
| 53 | if (packet -> freeCallback != NULL) |
---|
| 54 | (* packet -> freeCallback) (packet); |
---|
| 55 | if (! (packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE)) |
---|
| 56 | enet_free (packet -> data); |
---|
| 57 | enet_free (packet); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | /** Attempts to resize the data in the packet to length specified in the |
---|
| 61 | dataLength parameter |
---|
| 62 | @param packet packet to resize |
---|
| 63 | @param dataLength new size for the packet data |
---|
| 64 | @returns 0 on success, < 0 on failure |
---|
| 65 | */ |
---|
| 66 | int |
---|
| 67 | enet_packet_resize (ENetPacket * packet, size_t dataLength) |
---|
| 68 | { |
---|
| 69 | enet_uint8 * newData; |
---|
| 70 | |
---|
| 71 | if (dataLength <= packet -> dataLength || (packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE)) |
---|
| 72 | { |
---|
| 73 | packet -> dataLength = dataLength; |
---|
| 74 | |
---|
| 75 | return 0; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | newData = (enet_uint8 *) enet_malloc (dataLength); |
---|
| 79 | if (newData == NULL) |
---|
| 80 | return -1; |
---|
| 81 | |
---|
| 82 | memcpy (newData, packet -> data, packet -> dataLength); |
---|
| 83 | enet_free (packet -> data); |
---|
| 84 | |
---|
| 85 | packet -> data = newData; |
---|
| 86 | packet -> dataLength = dataLength; |
---|
| 87 | |
---|
| 88 | return 0; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | static int initializedCRC32 = 0; |
---|
| 92 | static enet_uint32 crcTable [256]; |
---|
| 93 | |
---|
| 94 | static void initialize_crc32 () |
---|
| 95 | { |
---|
| 96 | int byte; |
---|
| 97 | |
---|
| 98 | for (byte = 0; byte < 256; ++ byte) |
---|
| 99 | { |
---|
| 100 | enet_uint32 crc = byte << 24; |
---|
| 101 | int offset; |
---|
| 102 | |
---|
| 103 | for(offset = 0; offset < 8; ++ offset) |
---|
| 104 | { |
---|
| 105 | if (crc & 0x80000000) |
---|
| 106 | crc = (crc << 1) ^ 0x04c11db7; |
---|
| 107 | else |
---|
| 108 | crc <<= 1; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | crcTable [byte] = crc; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | initializedCRC32 = 1; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | enet_uint32 |
---|
| 118 | enet_crc32 (const ENetBuffer * buffers, size_t bufferCount) |
---|
| 119 | { |
---|
| 120 | enet_uint32 crc = 0xFFFFFFFF; |
---|
| 121 | |
---|
| 122 | if (! initializedCRC32) initialize_crc32 (); |
---|
| 123 | |
---|
| 124 | while (bufferCount -- > 0) |
---|
| 125 | { |
---|
| 126 | const enet_uint8 * data = (const enet_uint8 *) buffers -> data, |
---|
| 127 | * dataEnd = & data [buffers -> dataLength]; |
---|
| 128 | |
---|
| 129 | while (data < dataEnd) |
---|
| 130 | { |
---|
| 131 | crc = ((crc << 8) | * data ++) ^ crcTable [crc >> 24]; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | ++ buffers; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | return ENET_HOST_TO_NET_32 (~ crc); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /** @} */ |
---|