packet.c 3.8 KB

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