Source code of Windows XP (NT5)
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.

158 lines
2.9 KiB

  1. #define __BUFFER_C__
  2. #include <windows.h>
  3. #include <stdlib.h>
  4. #include <wtypes.h>
  5. #include "hidsdi.h"
  6. #include "list.h"
  7. #include "hidtest.h"
  8. #include "buffer.h"
  9. /*
  10. // Make sure when we include debug.h we are using the debug routines no matter
  11. // what since for this buffer allocation stuff we will use the built in memory
  12. // allocation routines
  13. */
  14. #ifndef DEBUG
  15. #define DEBUG
  16. #include "debug.h"
  17. #undef DEBUG
  18. #else
  19. #include "debug.h"
  20. #endif
  21. /*****************************************************************************
  22. /* External function definitions
  23. /*****************************************************************************/
  24. PVOID
  25. AllocateTestBuffer(
  26. IN ULONG BufferSize
  27. )
  28. {
  29. PVOID newBuffer;
  30. BOOL wasTrapping;
  31. wasTrapping = GET_TRAP_STATE();
  32. TRAP_OFF();
  33. newBuffer = ALLOC(BufferSize);
  34. if (wasTrapping)
  35. TRAP_ON();
  36. return (newBuffer);
  37. }
  38. VOID
  39. FillTestBuffer(
  40. IN PVOID Buffer,
  41. IN BYTE FillValue,
  42. IN ULONG NumBytes
  43. )
  44. {
  45. FillMemory(Buffer, NumBytes, FillValue);
  46. return;
  47. }
  48. VOID
  49. FreeTestBuffer(
  50. IN PVOID Buffer
  51. )
  52. {
  53. BOOL wasTrapping;
  54. wasTrapping = GET_TRAP_STATE();
  55. TRAP_OFF();
  56. FREE(Buffer);
  57. if (wasTrapping)
  58. TRAP_ON();
  59. return;
  60. }
  61. BOOL
  62. ValidateTestBuffer(
  63. IN PVOID Buffer
  64. )
  65. {
  66. BOOL wasTrapping;
  67. BOOL isValidMemory;
  68. wasTrapping = GET_TRAP_STATE();
  69. TRAP_OFF();
  70. isValidMemory = VALIDATEMEM(Buffer);
  71. if (wasTrapping)
  72. TRAP_ON();
  73. return (isValidMemory);
  74. }
  75. VOID
  76. CompareTestBuffers(
  77. IN PUCHAR Buffer1,
  78. IN PUCHAR Buffer2,
  79. IN ULONG BufferLength,
  80. OUT PULONG BytesChanged,
  81. OUT PULONG BitsChanged
  82. )
  83. {
  84. ULONG bytesChanged;
  85. ULONG bitsChanged;
  86. BYTE xorResult;
  87. bytesChanged = 0;
  88. bitsChanged = 0;
  89. while (BufferLength--) {
  90. xorResult = (*Buffer1) ^ (*Buffer2);
  91. if (xorResult) {
  92. bytesChanged++;
  93. /*
  94. // Algorithm to count the number of bits that changed...
  95. // Don't know the details of this...Found it somewhere
  96. // and it works...So I'm using it.
  97. */
  98. // Count pairs of bits
  99. xorResult = ( xorResult & 0x55 ) + ( ( xorResult >> 1 ) & 0x55 );
  100. // Count nybbles
  101. xorResult = ( xorResult & 0x33 ) + ( ( xorResult >> 2 ) & 0x33 );
  102. // etc (some optimizations now that the count can be represented
  103. // in fewer bits than the original number)
  104. xorResult = ( xorResult + ( xorResult >> 4 ) ) & 0x0F;
  105. bitsChanged += (xorResult & 0x3F);
  106. }
  107. Buffer1++;
  108. Buffer2++;
  109. }
  110. if (NULL != BytesChanged) {
  111. *BytesChanged = bytesChanged;
  112. }
  113. if (NULL != BitsChanged) {
  114. *BitsChanged = bitsChanged;
  115. }
  116. return;
  117. }