Leaked source code of windows server 2003
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.

140 lines
3.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: carefulreader.hxx
  7. //
  8. // Contents: Simple utility class for reading data out of buffers,
  9. // properly aligned, and always making sure there is enough
  10. // data.
  11. //
  12. // History: Dec-14-00 JohnDoty Created
  13. //
  14. //--------------------------------------------------------------------------
  15. #pragma once
  16. class CarefulBufferReader
  17. {
  18. public:
  19. CarefulBufferReader(unsigned char *buffer, ULONG_PTR cb)
  20. {
  21. pBuffer = buffer;
  22. cbBuffer = cb;
  23. }
  24. unsigned short ReadUSHORT()
  25. {
  26. Align(2);
  27. return ReadUSHORTNA();
  28. }
  29. unsigned short ReadUSHORTNA()
  30. {
  31. CheckSize(sizeof(unsigned short));
  32. cbBuffer -= sizeof(unsigned short);
  33. return *(PUSHORT_LV_CAST pBuffer)++;
  34. }
  35. long ReadLONG()
  36. {
  37. Align(4);
  38. return ReadLONGNA();
  39. }
  40. long ReadLONGNA()
  41. {
  42. CheckSize(sizeof(long));
  43. cbBuffer -= sizeof(long);
  44. return *(PLONG_LV_CAST pBuffer)++;
  45. }
  46. unsigned long ReadULONG()
  47. {
  48. Align(4);
  49. return ReadULONGNA();
  50. }
  51. unsigned long ReadULONGNA()
  52. {
  53. CheckSize(sizeof(unsigned long));
  54. cbBuffer -= sizeof(unsigned long);
  55. return *(PULONG_LV_CAST pBuffer)++;
  56. }
  57. hyper ReadHYPER()
  58. {
  59. Align(8);
  60. return ReadHYPERNA();
  61. }
  62. hyper ReadHYPERNA()
  63. {
  64. CheckSize(sizeof(hyper));
  65. cbBuffer -= sizeof(hyper);
  66. return *(PHYPER_LV_CAST pBuffer)++;
  67. }
  68. void Read(void *pvBuffer, int cbRead, int iAlignment)
  69. {
  70. Align(iAlignment);
  71. CheckSize(cbRead);
  72. cbBuffer -= cbRead;
  73. memcpy(pvBuffer, pBuffer, cbRead);
  74. pBuffer += cbRead;
  75. }
  76. void Align( int size )
  77. {
  78. unsigned char *temp = pBuffer;
  79. ALIGN( pBuffer, (size-1) );
  80. unsigned long cbFixup = (unsigned long)(pBuffer - temp);
  81. CheckSize(cbFixup);
  82. cbBuffer -= cbFixup;
  83. }
  84. void CheckSize ( ULONG_PTR size )
  85. {
  86. if ( cbBuffer < size )
  87. RAISE_RPC_EXCEPTION( RPC_X_BAD_STUB_DATA );
  88. }
  89. void Advance ( ULONG_PTR size )
  90. {
  91. CheckSize(size);
  92. cbBuffer -= size;
  93. pBuffer += size;
  94. }
  95. void AdvanceTo (unsigned char *mark)
  96. {
  97. // Assert you can only move forward!
  98. Win4Assert((ULONG_PTR)mark > (ULONG_PTR)pBuffer);
  99. int size = (int)(mark - pBuffer);
  100. CheckSize(size);
  101. cbBuffer -= size;
  102. pBuffer = mark;
  103. }
  104. unsigned char *GetBuffer() { return pBuffer; };
  105. ULONG_PTR BytesRemaining() { return cbBuffer; };
  106. private:
  107. unsigned char *pBuffer;
  108. ULONG_PTR cbBuffer;
  109. };