packet.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 dataContents initial contents of the packet's data; the packet's data will remain uninitialized if dataContents 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. return packet;
  44. }
  45. /** Destroys the packet and deallocates its data.
  46. @param packet packet to be destroyed
  47. */
  48. void
  49. enet_packet_destroy (ENetPacket * packet)
  50. {
  51. if (packet == NULL)
  52. return;
  53. if (packet -> freeCallback != NULL)
  54. (* packet -> freeCallback) (packet);
  55. if (! (packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE) &&
  56. packet -> data != NULL)
  57. enet_free (packet -> data);
  58. enet_free (packet);
  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. if (dataLength <= packet -> dataLength || (packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE))
  71. {
  72. packet -> dataLength = dataLength;
  73. return 0;
  74. }
  75. newData = (enet_uint8 *) enet_malloc (dataLength);
  76. if (newData == NULL)
  77. return -1;
  78. memcpy (newData, packet -> data, packet -> dataLength);
  79. enet_free (packet -> data);
  80. packet -> data = newData;
  81. packet -> dataLength = dataLength;
  82. return 0;
  83. }
  84. static int initializedCRC32 = 0;
  85. static enet_uint32 crcTable [256];
  86. static enet_uint32
  87. reflect_crc (int val, int bits)
  88. {
  89. int result = 0, bit;
  90. for (bit = 0; bit < bits; bit ++)
  91. {
  92. if(val & 1) result |= 1 << (bits - 1 - bit);
  93. val >>= 1;
  94. }
  95. return result;
  96. }
  97. static void
  98. initialize_crc32 ()
  99. {
  100. int byte;
  101. for (byte = 0; byte < 256; ++ byte)
  102. {
  103. enet_uint32 crc = reflect_crc (byte, 8) << 24;
  104. int offset;
  105. for(offset = 0; offset < 8; ++ offset)
  106. {
  107. if (crc & 0x80000000)
  108. crc = (crc << 1) ^ 0x04c11db7;
  109. else
  110. crc <<= 1;
  111. }
  112. crcTable [byte] = reflect_crc (crc, 32);
  113. }
  114. initializedCRC32 = 1;
  115. }
  116. enet_uint32
  117. enet_crc32 (const ENetBuffer * buffers, size_t bufferCount)
  118. {
  119. enet_uint32 crc = 0xFFFFFFFF;
  120. if (! initializedCRC32) initialize_crc32 ();
  121. while (bufferCount -- > 0)
  122. {
  123. const enet_uint8 * data = (const enet_uint8 *) buffers -> data,
  124. * dataEnd = & data [buffers -> dataLength];
  125. while (data < dataEnd)
  126. {
  127. crc = (crc >> 8) ^ crcTable [(crc & 0xFF) ^ *data++];
  128. }
  129. ++ buffers;
  130. }
  131. return ENET_HOST_TO_NET_32 (~ crc);
  132. }
  133. void
  134. enet_crc32_inc (enet_uint32 *crc, enet_uint8 byte)
  135. {
  136. if (! initializedCRC32) initialize_crc32 ();
  137. *crc = (*crc >> 8) ^ crcTable [(*crc & 0xFF) ^ byte];
  138. }
  139. /** @} */