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.

197 lines
6.6 KiB

  1. /*--------------------------------------------------------------------------
  2. ldapber.h
  3. CLdapBer Class:
  4. This class handles Basic Encode Rules (BER) processing for LDAP.
  5. The following BER restrictions are assumed.
  6. 1) Definite form of length encoding only.
  7. 2) Primitive forms only are used.
  8. Copyright (C) 1996 Microsoft Corporation
  9. All rights reserved.
  10. Authors:
  11. robertc Rob Carney
  12. History:
  13. 04-11-96 robertc Created.
  14. --------------------------------------------------------------------------*/
  15. #ifndef _LDAPBER_H
  16. #define _LDAPBER_H
  17. #if defined(DEBUG) && defined(INLINE)
  18. #undef THIS_FILE
  19. static char BASED_CODE_MODULE[] = "ldapber.h";
  20. #define THIS_FILE LDAPBER_H
  21. #endif
  22. // Identifier masks.
  23. #define BER_TAG_MASK 0x1f
  24. #define BER_FORM_MASK 0x20
  25. #define BER_CLASS_MASK 0xc0
  26. #define GetBerTag(x) (x & BER_TAG_MASK)
  27. #define GetBerForm(x) (x & BER_FORM_MASK)
  28. #define GetBerClass(x) (x & BER_CLASS_MASK)
  29. // id classes
  30. #define BER_FORM_CONSTRUCTED 0x20
  31. #define BER_CLASS_APPLICATION 0x40
  32. #define BER_CLASS_CONTEXT_SPECIFIC 0x80
  33. //
  34. // Standard BER types.
  35. #define BER_INVALID_TAG 0x00
  36. #define BER_BOOLEAN 0x01
  37. #define BER_INTEGER 0x02
  38. #define BER_BITSTRING 0x03
  39. #define BER_OCTETSTRING 0x04
  40. #define BER_NULL 0x05
  41. #define BER_ENUMERATED 0x0a
  42. #define BER_SEQUENCE 0x30
  43. #define BER_SET 0x31
  44. #define CB_DATA_GROW 1024
  45. #define MAX_BER_STACK 50 // max # of elements we can have in stack.
  46. #define MAX_ATTRIB_TYPE 40 // Max size of a AttributeType.
  47. // The SEQ_STACK entry is used to keep state info when building up a sequence.
  48. typedef struct
  49. {
  50. ULONG iPos; // Current position in the BER buffer where the
  51. // sequence length should go.
  52. ULONG cbLength; // # of bytes used for the length field.
  53. ULONG iParentSeqStart; // Starting position of the parent sequence.
  54. ULONG cbParentSeq; // # of bytes in the parent sequence.
  55. } SEQ_STACK;
  56. class CLdapBer;
  57. typedef CLdapBer LBER;
  58. class CLdapBer
  59. {
  60. // FRIENDS ------------------------------------------------
  61. private:
  62. // INTERFACES ---------------------------------------------
  63. // DECLARE OBJECT TYPE (SERIAL/DYNAMIC/DYNACREATE) --------
  64. public:
  65. // PUBLIC CONSTRUCTOR DESTRUCTOR --------------------------
  66. CLdapBer();
  67. ~CLdapBer();
  68. // PUBLIC ACCESSORS ---------------------------------------
  69. BYTE *PbData() { return m_pbData; }
  70. ULONG CbData() { return m_cbData; }
  71. ULONG CbSequence() { return m_cbSeq; }
  72. // PUBLIC FUNCTIONS ---------------------------------------
  73. //#ifndef CLIENT
  74. // VOID *operator new (size_t cSize) { return m_cpool.Alloc(); }
  75. // VOID operator delete (VOID *pInstance) { m_cpool.Free(pInstance); }
  76. //#endif
  77. void Reset();
  78. // Loads the BER class from a buffer.
  79. HRESULT HrLoadBer(BYTE *pbSrc, ULONG cbSrc, BOOL fLocalCopy=TRUE);
  80. // Function to make sure the input buffer has the full length field.
  81. static BOOL FCheckSequenceLength(BYTE *pbInput, ULONG cbInput, ULONG *pcbSeq, ULONG *piValuePos);
  82. // Read & Write sequence routines.
  83. HRESULT HrStartReadSequence(ULONG ulTag=BER_SEQUENCE);
  84. HRESULT HrEndReadSequence();
  85. HRESULT HrStartWriteSequence(ULONG ulTag=BER_SEQUENCE);
  86. HRESULT HrEndWriteSequence();
  87. BOOL FEndOfSequence()
  88. { if ((m_iCurrPos - m_iSeqStart) >= m_cbSeq) return TRUE;
  89. else return FALSE; }
  90. void GetCurrPos(ULONG *piCurrPos) { *piCurrPos = m_iCurrPos; }
  91. HRESULT FSetCurrPos(ULONG iCurrPos);
  92. HRESULT HrSkipValue();
  93. HRESULT HrSkipTag();
  94. HRESULT HrUnSkipTag();
  95. HRESULT HrPeekTag(ULONG *pulTag);
  96. HRESULT HrPeekLength(ULONG *pcb);
  97. HRESULT HrGetTag(ULONG *pulTag, ULONG ulTag=BER_INTEGER)
  98. { return HrGetValue((LONG *)pulTag, ulTag); }
  99. HRESULT HrGetValue(LONG *pi, ULONG ulTag=BER_INTEGER);
  100. HRESULT HrGetValue(TCHAR *szValue, ULONG cbValue, ULONG ulTag=BER_OCTETSTRING);
  101. HRESULT HrGetEnumValue(LONG *pi);
  102. HRESULT HrGetStringLength(int *pcbValue, ULONG ulTag = BER_OCTETSTRING);
  103. HRESULT HrGetBinaryValue(BYTE *pbBuf, ULONG cbBuf, ULONG ulTag = BER_OCTETSTRING);
  104. HRESULT HrAddValue(LONG i, ULONG ulTag=BER_INTEGER);
  105. HRESULT HrAddValue(const TCHAR *szValue, ULONG ulTag = BER_OCTETSTRING);
  106. HRESULT HrAddBinaryValue(BYTE *pbValue, ULONG cbValue, ULONG ulTag = BER_OCTETSTRING);
  107. // PUBLIC OVERRIDEABLES -----------------------------------
  108. // PUBLIC VARIABLES ---------------------------------------
  109. //#ifndef CLIENT
  110. // static CPool m_cpool;
  111. //#endif
  112. // PUBLIC DEBUG -------------------------------------------
  113. protected:
  114. // PROTECTED CONSTRUCTOR DESTRUCTOR -----------------------
  115. // PROTECTED ACCESSORS ------------------------------------
  116. // PROTECTED FUNCTIONS ------------------------------------
  117. // PROTECTED OVERRIDEABLES --------------------------------
  118. // PROTECTED VARIABLES ------------------------------------
  119. // PROTECTED DEBUG ----------------------------------------
  120. private:
  121. // PRIVATE ACCESSORS --------------------------------------
  122. // PRIVATE FUNCTIONS --------------------------------------
  123. HRESULT HrPushSeqStack(ULONG iPos, ULONG cbLength,
  124. ULONG iParentSeqStart, ULONG cbParentSeq);
  125. HRESULT HrPopSeqStack(ULONG *piPos, ULONG *pcbLength,
  126. ULONG *piParentSeqStart, ULONG *pcbParentSeq);
  127. static void GetCbLength(BYTE *pbData, ULONG *pcbLength);
  128. HRESULT HrGetLength(ULONG *pcb);
  129. static HRESULT HrGetLength(BYTE *pbData, ULONG *pcb, ULONG *piPos);
  130. void GetInt(BYTE *pbData, ULONG cbValue, LONG *plValue);
  131. void AddInt(BYTE *pbData, ULONG cbValue, LONG iValue);
  132. HRESULT HrSetLength(ULONG cb, ULONG cbLength=0xffffffff);
  133. // if fExact is true, cbNeeded is exactly the amount of data we need.
  134. HRESULT HrEnsureBuffer(ULONG cbNeeded, BOOL fExact = FALSE);
  135. // PRIVATE OVERRIDEABLES ----------------------------------
  136. // PRIVATE VARIABLES --------------------------------------
  137. ULONG m_iCurrPos; // Current position within the data buffer.
  138. ULONG m_cbData;
  139. BOOL m_fLocalCopy; // TRUE to alloc space for a local copy, FALSE keeps a reference.
  140. ULONG m_cbDataMax; // Current total size of buffer
  141. BYTE *m_pbData;
  142. ULONG m_iCurrSeqStack; // Curr position in the sequence stack.
  143. SEQ_STACK m_rgiSeqStack[MAX_BER_STACK]; // Stack used for keeping track of sequences.
  144. ULONG m_cbSeq; // # of bytes in the current sequence.
  145. ULONG m_iSeqStart; // Starting position of the current sequence.
  146. union {
  147. BOOL f;
  148. LONG l;
  149. BYTE *pb;
  150. } m_Value;
  151. // PRIVATE DEBUG ------------------------------------------
  152. // MESSAGE MAPS -------------------------------------------
  153. };
  154. #ifdef INLINE
  155. #endif // INLINE
  156. #endif