Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.0 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. // Unique ID generation
  8. //=============================================================================//
  9. #include "tier0/platform.h"
  10. #ifdef IS_WINDOWS_PC
  11. #include <windows.h> // uuidcreate
  12. #else
  13. #include "checksum_crc.h"
  14. #endif
  15. #include "tier1/uniqueid.h"
  16. #include "tier1/utlbuffer.h"
  17. // NOTE: This has to be the last file included!
  18. #include "tier0/memdbgon.h"
  19. //-----------------------------------------------------------------------------
  20. // Creates a new unique id
  21. //-----------------------------------------------------------------------------
  22. void CreateUniqueId( UniqueId_t *pDest )
  23. {
  24. #ifdef IS_WINDOWS_PC
  25. Assert( sizeof( UUID ) == sizeof( *pDest ) );
  26. UuidCreate( (UUID *)pDest );
  27. #else
  28. // X360/linux TBD: Need a real UUID Implementation
  29. Q_memset( pDest, 0, sizeof( UniqueId_t ) );
  30. #endif
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Creates a new unique id from a string representation of one
  34. //-----------------------------------------------------------------------------
  35. bool UniqueIdFromString( UniqueId_t *pDest, const char *pBuf, int nMaxLen )
  36. {
  37. if ( nMaxLen == 0 )
  38. {
  39. nMaxLen = Q_strlen( pBuf );
  40. }
  41. char *pTemp = (char*)stackalloc( nMaxLen + 1 );
  42. V_strncpy( pTemp, pBuf, nMaxLen + 1 );
  43. --nMaxLen;
  44. while( (nMaxLen >= 0) && V_isspace( pTemp[nMaxLen] ) )
  45. {
  46. --nMaxLen;
  47. }
  48. pTemp[ nMaxLen + 1 ] = 0;
  49. while( *pTemp && V_isspace( *pTemp ) )
  50. {
  51. ++pTemp;
  52. }
  53. #ifdef IS_WINDOWS_PC
  54. Assert( sizeof( UUID ) == sizeof( *pDest ) );
  55. if ( RPC_S_OK != UuidFromString( (unsigned char *)pTemp, (UUID *)pDest ) )
  56. {
  57. InvalidateUniqueId( pDest );
  58. return false;
  59. }
  60. #else
  61. // X360TBD: Need a real UUID Implementation
  62. // For now, use crc to generate a unique ID from the UUID string.
  63. Q_memset( pDest, 0, sizeof( UniqueId_t ) );
  64. if ( nMaxLen > 0 )
  65. {
  66. CRC32_t crc;
  67. CRC32_Init( &crc );
  68. CRC32_ProcessBuffer( &crc, pBuf, nMaxLen );
  69. CRC32_Final( &crc );
  70. Q_memcpy( pDest, &crc, sizeof( CRC32_t ) );
  71. }
  72. #endif
  73. return true;
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Sets an object ID to be an invalid state
  77. //-----------------------------------------------------------------------------
  78. void InvalidateUniqueId( UniqueId_t *pDest )
  79. {
  80. Assert( pDest );
  81. memset( pDest, 0, sizeof( UniqueId_t ) );
  82. }
  83. bool IsUniqueIdValid( const UniqueId_t &id )
  84. {
  85. UniqueId_t invalidId;
  86. memset( &invalidId, 0, sizeof( UniqueId_t ) );
  87. return !IsUniqueIdEqual( invalidId, id );
  88. }
  89. bool IsUniqueIdEqual( const UniqueId_t &id1, const UniqueId_t &id2 )
  90. {
  91. return memcmp( &id1, &id2, sizeof( UniqueId_t ) ) == 0;
  92. }
  93. void UniqueIdToString( const UniqueId_t &id, char *pBuf, int nMaxLen )
  94. {
  95. pBuf[ 0 ] = 0;
  96. // X360TBD: Need a real UUID Implementation
  97. #ifdef IS_WINDOWS_PC
  98. UUID *self = ( UUID * )&id;
  99. unsigned char *outstring = NULL;
  100. UuidToString( self, &outstring );
  101. if ( outstring && *outstring )
  102. {
  103. Q_strncpy( pBuf, (const char *)outstring, nMaxLen );
  104. RpcStringFree( &outstring );
  105. }
  106. #endif
  107. }
  108. void CopyUniqueId( const UniqueId_t &src, UniqueId_t *pDest )
  109. {
  110. memcpy( pDest, &src, sizeof( UniqueId_t ) );
  111. }
  112. bool Serialize( CUtlBuffer &buf, const UniqueId_t &src )
  113. {
  114. // X360TBD: Need a real UUID Implementation
  115. #ifdef IS_WINDOWS_PC
  116. if ( buf.IsText() )
  117. {
  118. UUID *pId = ( UUID * )&src;
  119. unsigned char *outstring = NULL;
  120. UuidToString( pId, &outstring );
  121. if ( outstring && *outstring )
  122. {
  123. buf.PutString( (const char *)outstring );
  124. RpcStringFree( &outstring );
  125. }
  126. else
  127. {
  128. buf.PutChar( '\0' );
  129. }
  130. }
  131. else
  132. {
  133. buf.Put( &src, sizeof(UniqueId_t) );
  134. }
  135. return buf.IsValid();
  136. #else
  137. return false;
  138. #endif
  139. }
  140. bool Unserialize( CUtlBuffer &buf, UniqueId_t &dest )
  141. {
  142. if ( buf.IsText() )
  143. {
  144. int nTextLen = buf.PeekStringLength();
  145. char *pBuf = (char*)stackalloc( nTextLen );
  146. buf.GetString( pBuf, nTextLen );
  147. UniqueIdFromString( &dest, pBuf, nTextLen );
  148. }
  149. else
  150. {
  151. buf.Get( &dest, sizeof(UniqueId_t) );
  152. }
  153. return buf.IsValid();
  154. }