Source code of Windows XP (NT5)
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.

224 lines
3.8 KiB

  1. /*
  2. * CSS support functions
  3. * Copyright (C) 2000 Microsoft Corporation
  4. */
  5. #include "precomp.h"
  6. BOOL FIsCssWhitespaceW(WCHAR wch)
  7. {
  8. return((wch == L' ') || (wch == L'\x9') || (wch == L'\xA') || (wch == L'\xC') || (wch == L'\xD'));
  9. }
  10. BOOL FIsCssWhitespaceA(char ch)
  11. {
  12. return(FIsCssWhitespaceW((WCHAR) (BYTE) ch));
  13. }
  14. BOOL FIsCssA(LPCSTR rgch, UINT cch)
  15. {
  16. if (memcmp(rgch, "@charset", 8) != 0)
  17. {
  18. // Not XML
  19. return(FALSE);
  20. }
  21. return(TRUE);
  22. UNREFERENCED_PARAMETER( cch );
  23. }
  24. BOOL FIsCssW(LPCWSTR rgwch, UINT cch)
  25. {
  26. if (memcmp(rgwch, L"@charset", 8 * sizeof(WCHAR)) != 0)
  27. {
  28. // Not XML
  29. return(FALSE);
  30. }
  31. return(TRUE);
  32. UNREFERENCED_PARAMETER( cch );
  33. }
  34. BOOL FDetectCssEncodingA(LPCSTR rgch, UINT cch, UINT *pcp)
  35. {
  36. const char *pchMax;
  37. const char *pch;
  38. char chQuote;
  39. const char *pchCharset;
  40. // Check for file begining with @charset
  41. if (cch < 13)
  42. {
  43. // File is too small
  44. return(FALSE);
  45. }
  46. if (!FIsCssA(rgch, cch))
  47. {
  48. // Not XML
  49. return(FALSE);
  50. }
  51. // Don't scan more than 4K looking for encoding even if it is valid XML
  52. cch = __min(cch, 4096);
  53. pchMax = rgch + cch;
  54. pch = rgch + 8;
  55. while ((pch < pchMax) && FIsCssWhitespaceA(*pch))
  56. {
  57. pch++;
  58. }
  59. if ((pch == pchMax) || ((*pch != '\'') && (*pch != '"')))
  60. {
  61. // No @charset specification
  62. return(FALSE);
  63. }
  64. chQuote = *pch++;
  65. pchCharset = pch;
  66. while ((pch < pchMax) && (*pch != chQuote))
  67. {
  68. pch++;
  69. }
  70. if (pch == pchMax)
  71. {
  72. // No @charset specification
  73. return(FALSE);
  74. }
  75. // We have an CSS encoding declaration from pchCharset to (pch - 1)
  76. if (pch == pchCharset)
  77. {
  78. // No @charset specification
  79. return(FALSE);
  80. }
  81. // To be strict a CSS charset declaration should have optional whitespace then a semicolon here
  82. if (!FLookupCodepageNameA((LPCSTR) pchCharset, (UINT) (pch - pchCharset), pcp))
  83. {
  84. // Encoding is not recognized
  85. return(FALSE);
  86. }
  87. if ((*pcp == CP_UTF16) || (*pcp == CP_UTF16BE))
  88. {
  89. // These are bogus since we know the file is MBCS
  90. return(FALSE);
  91. }
  92. return(FValidateCodepage(hwndNP, *pcp));
  93. }
  94. BOOL FDetectCssEncodingW(LPCWSTR rgch, UINT cch, UINT *pcp)
  95. {
  96. const WCHAR *pchMax;
  97. const WCHAR *pch;
  98. WCHAR chQuote;
  99. const WCHAR *pchCharset;
  100. // Check for file begining with @charset
  101. if (cch < 13)
  102. {
  103. // File is too small
  104. return(FALSE);
  105. }
  106. if (!FIsCssW(rgch, cch))
  107. {
  108. // No @charset specification
  109. return(FALSE);
  110. }
  111. // Don't scan more than 4K looking for encoding even if it is valid XML
  112. cch = __min(cch, 4096);
  113. pchMax = rgch + cch;
  114. pch = rgch + 8;
  115. while ((pch < pchMax) && FIsCssWhitespaceW(*pch))
  116. {
  117. pch++;
  118. }
  119. if ((pch == pchMax) || ((*pch != L'\'') && (*pch != L'"')))
  120. {
  121. // No @charset specification
  122. return(FALSE);
  123. }
  124. chQuote = *pch++;
  125. pchCharset = pch;
  126. while ((pch < pchMax) && (*pch != chQuote))
  127. {
  128. pch++;
  129. }
  130. if (pch == pchMax)
  131. {
  132. // No @charset specification
  133. return(FALSE);
  134. }
  135. // We have an CSS encoding declaration from pchCharset to (pch - 1)
  136. if (pch == pchCharset)
  137. {
  138. // No @charset specification
  139. return(FALSE);
  140. }
  141. // To be strict a CSS charset declaration should have optional whitespace then a semicolon here
  142. if (!FLookupCodepageNameW(pchCharset, (UINT) (pch - pchCharset), pcp))
  143. {
  144. // Encoding is not recognized
  145. return(FALSE);
  146. }
  147. #if 0
  148. if ((*pcp == CP_UTF16) || (*pcp == CP_UTF16BE))
  149. {
  150. // These are bogus since we know the file is MBCS
  151. return(FALSE);
  152. }
  153. #endif
  154. return(FValidateCodepage(hwndNP, *pcp));
  155. }