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.

66 lines
1.6 KiB

  1. //===== Copyright c 1996-2008, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: Simple encoder/decoder
  4. //
  5. //===========================================================================//
  6. #ifndef COMMON_VALVE_FONT
  7. #define COMMON_VALVE_FONT
  8. #include "simplecodec.h"
  9. #include <time.h>
  10. namespace ValveFont
  11. {
  12. //
  13. // Encodes the font buffer, buffer size is increased automatically
  14. //
  15. inline void EncodeFont( CUtlBuffer &buffer )
  16. {
  17. srand( (unsigned) time( NULL ) );
  18. int numBytes = buffer.TellPut();
  19. int numSultBytes = 0x10 + ( rand() % 0x10 );
  20. char const *szTag = "VFONT1";
  21. int numTagBytes = strlen( szTag );
  22. for ( int k = 0; k < numSultBytes; ++ k )
  23. buffer.PutUnsignedChar( k );
  24. for ( int k = 0; k < numTagBytes; ++ k )
  25. buffer.PutUnsignedChar( szTag[ k ] );
  26. SimpleCodec::EncodeBuffer( ( unsigned char * ) buffer.Base(), numBytes, numSultBytes );
  27. }
  28. //
  29. // Decodes font buffer, returns true if successful, buffer's put pointer is positioned
  30. // at the end of the font data.
  31. // Returns false on failure.
  32. //
  33. inline bool DecodeFont( CUtlBuffer &buffer )
  34. {
  35. int numTotalBytes = buffer.TellPut();
  36. char const *szTag = "VFONT1";
  37. int numTagBytes = strlen( szTag );
  38. if ( numTotalBytes <= numTagBytes )
  39. return false;
  40. if ( memcmp( (( unsigned char * ) buffer.Base() ) + numTotalBytes - numTagBytes,
  41. szTag, numTagBytes ) )
  42. return false;
  43. numTotalBytes -= numTagBytes;
  44. SimpleCodec::DecodeBuffer( ( unsigned char * ) buffer.Base(), numTotalBytes );
  45. buffer.SeekPut( CUtlBuffer::SEEK_HEAD, numTotalBytes );
  46. return true;
  47. }
  48. }; // namespace ValveFont
  49. #endif // COMMON_VALVE_FONT