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.

152 lines
4.5 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997 - 1998.
  5. //
  6. // File: cpid.cxx
  7. //
  8. // Contents: codepage functions
  9. //
  10. // History: 97-Jun-09 t-elainc Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #include <pch.cxx>
  14. #pragma hdrstop
  15. #include <gibralt.hxx>
  16. #include <codepage.hxx>
  17. #include <cphash.hxx>
  18. #include <cpid.hxx>
  19. //+---------------------------------------------------------------------------
  20. //
  21. // Function: GetBrowserCodepage - public
  22. //
  23. // Synposis: returns the ULONG value of the codepage determined by the
  24. // query string
  25. //
  26. // Arguments: CWebServer & webServer, LCID locale
  27. //
  28. // History: 97-Jun-09 t-elainc Created
  29. //
  30. //----------------------------------------------------------------------------
  31. ULONG GetBrowserCodepage( CWebServer & webServer, LCID locale )
  32. {
  33. ULONG cpval=0;
  34. //first get value of CICodepage specified in the query string
  35. CHAR acCPString[20];
  36. //if a codepage string is specified
  37. if ( GetCodepageValue( webServer, acCPString, sizeof(acCPString) ) )
  38. {
  39. WCHAR awcCPString[100];
  40. unsigned int ccString = strlen( acCPString ) + 1;
  41. unsigned int numconverted = MultiByteToWideChar(CP_ACP, 0, acCPString, ccString, awcCPString, 100);
  42. //make sure everything was converted properly. Otherwise throw an exception
  43. if (ccString != numconverted)
  44. {
  45. THROW ( CException () );
  46. }
  47. //Check to see if the string is one of the values in the code page hash table
  48. BOOL valid = CCodePageTable::Lookup(awcCPString, wcslen(awcCPString), cpval);
  49. //code page string is not in the hash table
  50. if (!valid)
  51. {
  52. CHAR* pctmp;
  53. cpval = strtoul(acCPString, &pctmp, 10);
  54. //if the codepage value is not a number
  55. if (!cpval)
  56. {
  57. LCID lcid = GetLCIDFromString(awcCPString);
  58. //if the codepage value is not a legitimate lcid
  59. if (InvalidLCID == lcid)
  60. {
  61. THROW ( CException (QUTIL_E_INVALID_CODEPAGE) );
  62. }
  63. //use the locale to determine the proper codepage
  64. cpval = LocaleToCodepage(lcid);
  65. }
  66. }
  67. }
  68. else //no codepage string specified, use locale to determine proper codepage
  69. {
  70. //ciGibDebugOut((DEB_ITRACE, "No codepage specified. Using default codepage by locale."));
  71. cpval = LocaleToCodepage(locale);
  72. }
  73. return cpval;
  74. }
  75. //+---------------------------------------------------------------------------
  76. //
  77. // Function: GetCodePageValue
  78. //
  79. // Synposis: Returns the string contained withing the Query String that
  80. // specifies the codepage. If no string is there, returns 0.
  81. //
  82. // Arguments: [webServer] -- Web server
  83. // [pcCPString] -- String containing CiCodepage returned here
  84. // [ccCPString] -- Size (in chars) of [pcCPString]
  85. //
  86. // Returns: TRUE if a codepage parameter was found and fits in buffer.
  87. //
  88. // History: 97-Jun-11 t-elainc Created
  89. //
  90. //----------------------------------------------------------------------------
  91. BOOL GetCodepageValue( CWebServer & webServer,
  92. char * pcCPString,
  93. unsigned ccCPString )
  94. {
  95. unsigned ccValue = 0xFFFFFFFF;
  96. char* ISAPI_CI_CODEPAGE_A = "CICODEPAGE";
  97. char const * pcStart = webServer.GetQueryString();
  98. while ( 0 != pcStart && 0 != pcStart[0] )
  99. {
  100. if ( 0 == _strnicmp( pcStart,
  101. ISAPI_CI_CODEPAGE_A,
  102. strlen(ISAPI_CI_CODEPAGE_A) ) &&
  103. '=' == pcStart[strlen(ISAPI_CI_CODEPAGE_A)] )
  104. break;
  105. pcStart = strchr( pcStart, '&' );
  106. if ( 0 != pcStart )
  107. pcStart++;
  108. }
  109. if ( 0 != pcStart && 0 != pcStart[0] )
  110. {
  111. pcStart += strlen(ISAPI_CI_CODEPAGE_A)+1; // sizeof includes null
  112. char* pcEnd = strchr(pcStart, '&');
  113. if (pcEnd)
  114. ccValue = CiPtrToUint( pcEnd - pcStart );
  115. else
  116. ccValue = strlen( pcStart );
  117. if ( ccValue < ccCPString )
  118. {
  119. strncpy( pcCPString, pcStart, ccValue );
  120. pcCPString[ccValue] = 0;
  121. }
  122. }
  123. return (ccValue < ccCPString);
  124. }