Team Fortress 2 Source Code as on 22/4/2020
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.

90 lines
2.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Low level byte swapping routines.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include "byteswap.h"
  8. //-----------------------------------------------------------------------------
  9. // Copy a single field from the input buffer to the output buffer, swapping the bytes if necessary
  10. //-----------------------------------------------------------------------------
  11. void CByteswap::SwapFieldToTargetEndian( void* pOutputBuffer, void *pData, typedescription_t *pField )
  12. {
  13. switch ( pField->fieldType )
  14. {
  15. case FIELD_CHARACTER:
  16. SwapBufferToTargetEndian<char>( (char*)pOutputBuffer, (char*)pData, pField->fieldSize );
  17. break;
  18. case FIELD_BOOLEAN:
  19. SwapBufferToTargetEndian<bool>( (bool*)pOutputBuffer, (bool*)pData, pField->fieldSize );
  20. break;
  21. case FIELD_SHORT:
  22. SwapBufferToTargetEndian<short>( (short*)pOutputBuffer, (short*)pData, pField->fieldSize );
  23. break;
  24. case FIELD_FLOAT:
  25. SwapBufferToTargetEndian<uint>( (uint*)pOutputBuffer, (uint*)pData, pField->fieldSize );
  26. break;
  27. case FIELD_INTEGER:
  28. SwapBufferToTargetEndian<int>( (int*)pOutputBuffer, (int*)pData, pField->fieldSize );
  29. break;
  30. case FIELD_VECTOR:
  31. SwapBufferToTargetEndian<uint>( (uint*)pOutputBuffer, (uint*)pData, pField->fieldSize * 3 );
  32. break;
  33. case FIELD_VECTOR2D:
  34. SwapBufferToTargetEndian<uint>( (uint*)pOutputBuffer, (uint*)pData, pField->fieldSize * 2 );
  35. break;
  36. case FIELD_QUATERNION:
  37. SwapBufferToTargetEndian<uint>( (uint*)pOutputBuffer, (uint*)pData, pField->fieldSize * 4 );
  38. break;
  39. case FIELD_EMBEDDED:
  40. {
  41. typedescription_t *pEmbed = pField->td->dataDesc;
  42. for ( int i = 0; i < pField->fieldSize; ++i )
  43. {
  44. SwapFieldsToTargetEndian( (byte*)pOutputBuffer + pEmbed->fieldOffset[ TD_OFFSET_NORMAL ],
  45. (byte*)pData + pEmbed->fieldOffset[ TD_OFFSET_NORMAL ],
  46. pField->td );
  47. pOutputBuffer = (byte*)pOutputBuffer + pField->fieldSizeInBytes;
  48. pData = (byte*)pData + pField->fieldSizeInBytes;
  49. }
  50. }
  51. break;
  52. default:
  53. assert(0);
  54. }
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Write a block of fields. Works a bit like the saverestore code.
  58. //-----------------------------------------------------------------------------
  59. void CByteswap::SwapFieldsToTargetEndian( void *pOutputBuffer, void *pBaseData, datamap_t *pDataMap )
  60. {
  61. // deal with base class first
  62. if ( pDataMap->baseMap )
  63. {
  64. SwapFieldsToTargetEndian( pOutputBuffer, pBaseData, pDataMap->baseMap );
  65. }
  66. typedescription_t *pFields = pDataMap->dataDesc;
  67. int fieldCount = pDataMap->dataNumFields;
  68. for ( int i = 0; i < fieldCount; ++i )
  69. {
  70. typedescription_t *pField = &pFields[i];
  71. SwapFieldToTargetEndian( (BYTE*)pOutputBuffer + pField->fieldOffset[ TD_OFFSET_NORMAL ],
  72. (BYTE*)pBaseData + pField->fieldOffset[ TD_OFFSET_NORMAL ],
  73. pField );
  74. }
  75. }