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.

165 lines
6.2 KiB

  1. #include "uksPCH.h"
  2. extern "C" {
  3. #include <wdm.h>
  4. }
  5. #include <ks.h>
  6. #include "inc/KrmCommStructs.h"
  7. #include "SBuffer.h"
  8. //------------------------------------------------------------------------------
  9. SBuffer::SBuffer(BYTE* BufX, unsigned int Len){
  10. buf=BufX;
  11. getPos=0;
  12. putPos=0;
  13. len=Len;
  14. lasterror=KRM_OK;
  15. };
  16. //------------------------------------------------------------------------------
  17. SBuffer::~SBuffer(){
  18. buf=NULL;
  19. };
  20. //------------------------------------------------------------------------------
  21. void SBuffer::reset(){
  22. getPos=0;
  23. putPos=0;
  24. lasterror=KRM_OK;
  25. };
  26. //------------------------------------------------------------------------------
  27. DRM_STATUS SBuffer::append(BYTE* Data, DWORD datLen){
  28. unsigned int p;
  29. if (KRM_OK == getPutPosAndAdvance(&p,datLen)) {
  30. memcpy(buf+p, Data, datLen);
  31. }
  32. return lasterror;
  33. };
  34. //------------------------------------------------------------------------------
  35. void SBuffer::err(const char* Msg, DRM_STATUS err){
  36. lasterror = err;
  37. #ifdef DBG
  38. DbgPrint("DRMK:");DbgPrint((char*) Msg);DbgPrint("\n");
  39. #endif
  40. ASSERT(FALSE);
  41. };
  42. //------------------------------------------------------------------------------
  43. DRM_STATUS SBuffer::getGetPosAndAdvance(unsigned int *pos, unsigned int Len) {
  44. if (KRM_OK == lasterror) {
  45. if (Len > len-getPos) {
  46. err("pop overflow", KRM_BUFSIZE);
  47. }
  48. else {
  49. *pos=getPos;
  50. getPos+=Len;
  51. }
  52. }
  53. return lasterror;
  54. };
  55. //------------------------------------------------------------------------------
  56. DRM_STATUS SBuffer::getPutPosAndAdvance(unsigned int *pos, unsigned int Len) {
  57. if (KRM_OK == lasterror) {
  58. if (Len > len-putPos) {
  59. err("push overflow", KRM_BUFSIZE);
  60. }
  61. else {
  62. *pos=putPos;
  63. putPos+=Len;
  64. }
  65. }
  66. return lasterror;
  67. };
  68. //------------------------------------------------------------------------------
  69. #define INSERT(_TYPE, _OBJADDR) \
  70. if (KRM_OK == lasterror) { \
  71. unsigned int _size=sizeof(_TYPE); \
  72. if (_size > len-putPos) { \
  73. err("push overflow",KRM_BUFSIZE); \
  74. } \
  75. else { \
  76. memcpy(buf+putPos, _OBJADDR, _size); \
  77. putPos+=_size; \
  78. } \
  79. } \
  80. return *this;
  81. //------------------------------------------------------------------------------
  82. #define EXTRACT(_TYPE, _OBJADDR) \
  83. if (KRM_OK == lasterror) { \
  84. unsigned int _size = sizeof(_TYPE); \
  85. if(_size > len-getPos) { \
  86. err("pop overflow",KRM_BUFSIZE); \
  87. } \
  88. else { \
  89. memcpy(_OBJADDR, buf+getPos, _size); \
  90. getPos += _size; \
  91. } \
  92. } \
  93. return *this;
  94. //------------------------------------------------------------------------------
  95. SBuffer& SBuffer::operator << (const DWORD Val) {
  96. INSERT(DWORD, &Val);
  97. };
  98. //------------------------------------------------------------------------------
  99. SBuffer& SBuffer::operator << (const PVOID Ptr) {
  100. INSERT(DWORD, &Ptr);
  101. };
  102. //------------------------------------------------------------------------------
  103. SBuffer& SBuffer::operator << (PDRMRIGHTS R) {
  104. INSERT(DRMRIGHTS, R);
  105. };
  106. //------------------------------------------------------------------------------
  107. SBuffer& SBuffer::operator << (PSTREAMKEY S) {
  108. INSERT(STREAMKEY, S);
  109. };
  110. //------------------------------------------------------------------------------
  111. SBuffer& SBuffer::operator << (PCERT C) {
  112. INSERT(CERT, C);
  113. };
  114. //------------------------------------------------------------------------------
  115. SBuffer& SBuffer::operator << (PDRMDIGEST D) {
  116. INSERT(DRMDIGEST, D);
  117. };
  118. //------------------------------------------------------------------------------
  119. SBuffer& SBuffer::operator >> (DWORD& Val) {
  120. EXTRACT(DWORD, &Val);
  121. };
  122. //------------------------------------------------------------------------------
  123. SBuffer& SBuffer::operator >> (DRMRIGHTS* R) {
  124. EXTRACT(DRMRIGHTS, R);
  125. };
  126. //------------------------------------------------------------------------------
  127. SBuffer& SBuffer::operator >> (PSTREAMKEY S) {
  128. EXTRACT(STREAMKEY, S);
  129. };
  130. //------------------------------------------------------------------------------
  131. SBuffer& SBuffer::operator >> (PCERT C) {
  132. EXTRACT(CERT, C);
  133. };
  134. //------------------------------------------------------------------------------
  135. DRM_STATUS term(SBuffer& S) {
  136. if (KRM_OK == S.getLastError()) {
  137. S << 0xFFFFffff;
  138. }
  139. return S.getLastError();
  140. };
  141. //------------------------------------------------------------------------------
  142. DRM_STATUS checkTerm(SBuffer& S) {
  143. if (KRM_OK == S.getLastError()) {
  144. DWORD Val = 0;
  145. S >> Val;
  146. if (KRM_OK == S.getLastError()) {
  147. if (Val != 0xFFFFffff) {
  148. S.err("Bad terminator", KRM_BADTERMINATOR);
  149. }
  150. }
  151. ASSERT(Val==0xFFFFffff);
  152. }
  153. return S.getLastError();;
  154. };
  155. //------------------------------------------------------------------------------