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.0 KiB

  1. // rtfparser.h
  2. // Define parser class
  3. #ifndef _RTFPARSER_H_
  4. #define _RTFPARSER_H_
  5. // Error Code
  6. #define ecOK 0 // Everything's fine!
  7. #define ecStackUnderflow 1 // Unmatched '}'
  8. #define ecStackOverflow 2 // Too many '{' -- memory exhausted
  9. #define ecUnmatchedBrace 3 // RTF ended during an open group.
  10. #define ecInvalidHex 4 // invalid hex character found in data
  11. #define ecBadTable 5 // RTF table (sym or prop) invalid
  12. #define ecAssertion 6 // Assertion failure
  13. #define ecEndOfFile 7 // End of file reached while reading RTF
  14. #define ecOutOfMemory 8 // Memery allocate failed...
  15. #define ecBufTooSmall 9 // Write buffer too small
  16. // Rtf Destination State
  17. typedef enum { rdsNorm, rdsSkip } RDS;
  18. // Rtf Internal State
  19. typedef enum { risNorm, risBin, risHex } RIS;
  20. // special process
  21. typedef enum { ipfnBin, ipfnHex, ipfnSkipDest } IPFN;
  22. typedef enum { idestPict, idestSkip } IDEST;
  23. // keyword type
  24. typedef enum { kwdChar, kwdDest, kwdProp, kwdSpec } KWD;
  25. // save buffer status
  26. typedef enum { bsDefault, bsText, bsHex } BSTATUS;
  27. // keyword table
  28. typedef struct tagSymbol
  29. {
  30. char *szKeyword; // RTF keyword
  31. KWD kwd; // base action to take
  32. int idx; // index into property table if kwd == kwdProp
  33. // index into destination table if kwd == kwdDest
  34. // character to print if kwd == kwdChar
  35. } SYM;
  36. // save stack
  37. typedef struct tagSave // property save structure
  38. {
  39. struct tagSave *pNext; // next save
  40. RDS rds;
  41. RIS ris;
  42. } SAVE;
  43. typedef struct tagKeyword
  44. {
  45. WORD wStatus;
  46. char szKeyword[30];
  47. char szParameter[20];
  48. } SKeyword;
  49. // tagKeyword status
  50. enum { KW_ENABLE = 0x0001, // enable searching
  51. KW_PARAM = 0x0002, // found keyword, if have parameter
  52. KW_FOUND = 0x0004 // if found keyword
  53. };
  54. // parser class def
  55. class CRtfParser
  56. {
  57. public:
  58. // ctor
  59. CRtfParser(BYTE* pchInput, UINT cchInput,
  60. BYTE* pchOutput, UINT cchOutput);
  61. // dtor
  62. ~CRtfParser() {};
  63. // Check signature
  64. BOOL fRTFFile();
  65. // Get RTF version
  66. int GetVersion(PDWORD pdwMajor);
  67. // Get codepage
  68. int GetCodepage(PDWORD pdwCodepage);
  69. // start
  70. int Do();
  71. // return result buffer size
  72. int GetResult(PDWORD pdwSize) {
  73. *pdwSize = m_uOutPos;
  74. return ecOK;
  75. }
  76. private:
  77. // clear internal status
  78. void Reset(void);
  79. // PushRtfState
  80. // Save relevant info on a linked list of SAVE structures.
  81. int PushRtfState(void);
  82. // PopRtfState
  83. int PopRtfState(void);
  84. // ReleaseRtfState
  85. int ReleaseRtfState(void);
  86. // ParseChar
  87. // Route the character to the appropriate destination stream.
  88. int ParseChar(BYTE ch, BSTATUS bsStatus);
  89. // ParseRtfKeyword
  90. // get a control word (and its associated value) and
  91. // call TranslateKeyword to dispatch the control.
  92. int ParseRtfKeyword();
  93. // TranslateKeyword.
  94. int TranslateKeyword(char *szKeyword, char* szParameter);
  95. // ParseSpecialKeyword
  96. // Evaluate an RTF control that needs special processing.
  97. int ParseSpecialKeyword(IPFN ipfn, char* szParameter);
  98. // ChangeDest
  99. // Change to the destination specified by idest.
  100. // There's usually more to do here than this...
  101. int ChangeDest(IDEST idest);
  102. // Buffer funcs
  103. // GetByte
  104. // Get one char from input buffer
  105. int GetByte(BYTE* pch);
  106. // unGetByte
  107. // adjust the cursor, return one char
  108. int unGetByte(BYTE ch);
  109. // SaveByte
  110. // Save one char to output buffer
  111. int SaveByte(BYTE ch);
  112. // SetStatus
  113. // set the buffer status, if buffer status changed then start convert
  114. int SetStatus(BSTATUS bsStatus);
  115. // Hex2Char
  116. // convert hex string to char string
  117. int Hex2Char(BYTE* pchSrc, UINT cchSrc, BYTE* pchDes, UINT cchDes, UINT* pcchLen);
  118. // Char2Hex
  119. // convert char string to hex string
  120. int Char2Hex(BYTE* pchSrc, UINT cchSrc, BYTE* pchDes, UINT cchDes, UINT* pcchLen);
  121. // GetUnicodeDestination
  122. // convert unicode string to unicode destination in RTF
  123. int GetUnicodeDestination(BYTE* pchUniDes, LPWSTR pwchStr, UINT wchLen, UINT* pcchLen);
  124. // WideCharToKeyword
  125. // map one wide char to \u keyword
  126. int WideCharToKeyword(WCHAR wch, BYTE* pch, UINT* pcchLen);
  127. private:
  128. //
  129. BOOL m_fInit;
  130. // member for parser
  131. INT m_cGroup; // count of '{' and '}' pair
  132. UINT m_cbBin; // length of data block if \BIN
  133. RIS m_ris; // internal status
  134. RDS m_rds; // destination status
  135. BOOL m_fSkipDestIfUnk; // indicate how to process "\*"
  136. SAVE* m_psave; // status stack
  137. // member for IO buffer
  138. BYTE* m_pchInput; // input buffer
  139. UINT m_cchInput;
  140. UINT m_uCursor; // current position when read buffer
  141. BYTE* m_pchOutput; // output buffer
  142. UINT m_cchOutput; // output buffer size
  143. UINT m_uOutPos; // current position of write buffer
  144. BSTATUS m_bsStatus; // buffer status to control conversion
  145. UINT m_uConvStart; // start point of buffer when convert
  146. UINT m_cchConvLen; // length of buffer to convert
  147. // member when get specific keyword
  148. SKeyword m_sKeyword;
  149. };
  150. #endif // _RTFPARSER_H_