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.

138 lines
3.3 KiB

  1. // =================================================================================
  2. // Internet Character Set Conversion: Input from HZ-GB-2312
  3. // =================================================================================
  4. #include "pch.hxx"
  5. #include "HzGbIn.h"
  6. #include "FEChrCnv.h"
  7. int HZGB_to_GB2312 (CONV_CONTEXT *pcontext, UCHAR *pHZGB, int HZGB_len, UCHAR *pGB2312, int GB2312_len)
  8. {
  9. long lConvertedSize;
  10. if (!GB2312_len) {
  11. // Wanted the converted size
  12. if (!pcontext->pIncc0)
  13. pcontext->pIncc0 = new CInccHzGbIn;
  14. if (FAILED(((CInccHzGbIn*)pcontext->pIncc0)->GetStringSizeA(pHZGB, HZGB_len, &lConvertedSize)))
  15. return -1;
  16. } else {
  17. if (!pcontext->pIncc)
  18. pcontext->pIncc = new CInccHzGbIn;
  19. if (FAILED(((CInccHzGbIn*)pcontext->pIncc)->ConvertStringA(pHZGB, HZGB_len, pGB2312, GB2312_len, &lConvertedSize)))
  20. return -1;
  21. }
  22. if (!pHZGB) {
  23. // Let's clean up our context here.
  24. if (pcontext->pIncc0) {
  25. delete pcontext->pIncc0;
  26. pcontext->pIncc0 = NULL;
  27. }
  28. if (pcontext->pIncc) {
  29. delete pcontext->pIncc;
  30. pcontext->pIncc = NULL;
  31. }
  32. return 0;
  33. }
  34. return (int)lConvertedSize;
  35. }
  36. CInccHzGbIn::CInccHzGbIn()
  37. {
  38. pfnNextProc = ConvMain;
  39. fGBMode = FALSE;
  40. }
  41. HRESULT CInccHzGbIn::ConvertByte(BYTE by)
  42. {
  43. return (this->*pfnNextProc)(FALSE, by, lNextParam);
  44. }
  45. HRESULT CInccHzGbIn::CleanUp()
  46. {
  47. return (this->*pfnNextProc)(TRUE, 0, lNextParam);
  48. }
  49. HRESULT CInccHzGbIn::ConvMain(BOOL fCleanUp, BYTE by, long lParam)
  50. {
  51. HRESULT hr = S_OK;
  52. if (!fCleanUp) {
  53. if (!fGBMode) {
  54. if (by == '~') {
  55. pfnNextProc = ConvTilde;
  56. } else {
  57. hr = Output(by);
  58. }
  59. } else {
  60. if (by >= 0x20 && by <= 0x7e) {
  61. pfnNextProc = ConvDoubleByte;
  62. lNextParam = (long)by;
  63. } else {
  64. hr = Output(by);
  65. }
  66. }
  67. }
  68. return hr;
  69. }
  70. HRESULT CInccHzGbIn::ConvTilde(BOOL fCleanUp, BYTE by, long lParam)
  71. {
  72. pfnNextProc = ConvMain;
  73. if (!fCleanUp) {
  74. switch (by) {
  75. case '~':
  76. return Output('~');
  77. case '{':
  78. fGBMode = TRUE;
  79. return ResultFromScode(S_OK);
  80. case '\n':
  81. // Just eat it
  82. return ResultFromScode(S_OK);
  83. default:
  84. (void)Output('~');
  85. return ConvertByte(by);
  86. }
  87. } else {
  88. return Output('~');
  89. }
  90. }
  91. HRESULT CInccHzGbIn::ConvDoubleByte(BOOL fCleanUp, BYTE byTrail, long lParam)
  92. {
  93. pfnNextProc = ConvMain;
  94. if (!fCleanUp) {
  95. if ((BYTE)lParam >= 0x21 && (BYTE)lParam <= 0x77 && byTrail >= 0x21 && byTrail <= 0x7e) { // Check if GB char
  96. (void)Output((BYTE)lParam | 0x80);
  97. return Output(byTrail | 0x80);
  98. } else if ((BYTE)lParam == '~' && byTrail == '}') { // 0x7e7d
  99. fGBMode = FALSE;
  100. return ResultFromScode(S_OK);
  101. } else if ((BYTE)lParam >= 0x78 && (BYTE)lParam <= 0x7d && byTrail >= 0x21 && byTrail <= 0x7e) { // Check if non standard extended code
  102. (void)Output(0xa1); // Output blank box symbol
  103. return Output(0xf5);
  104. } else if ((BYTE)lParam == '~') {
  105. (void)Output('~'); // Output blank box symbol
  106. return Output(byTrail);
  107. } else if ((BYTE)lParam == ' ') {
  108. return Output(byTrail);
  109. } else if (byTrail == ' ') {
  110. (void)Output(0xa1); // Output space symbol
  111. return Output(0xa1);
  112. } else {
  113. (void)Output((BYTE)lParam);
  114. return Output(byTrail);
  115. }
  116. } else {
  117. return Output((BYTE)lParam);
  118. }
  119. }