UriHelper.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. namespace Js
  7. {
  8. class UriHelper
  9. {
  10. public:
  11. // Flags used to mark properties of various characters for URI handling.
  12. enum
  13. {
  14. URINone = 0x0,
  15. URIReserved = 0x1,
  16. URIUnescaped = 0x2
  17. };
  18. static Var EncodeCoreURI(ScriptContext* scriptContext, Arguments& args, unsigned char flags);
  19. static Var Decode(__in_ecount(len) const wchar_t* psz, ulong len, unsigned char reservedFlags, ScriptContext* scriptContext);
  20. static Var DecodeCoreURI(ScriptContext* scriptContext, Arguments& args, unsigned char reservedFlags);
  21. static unsigned char s_uriProps[128];
  22. #if DBG
  23. // Validate the array of character properties for URI handling. Each entry is a
  24. // bitwise OR of the flags defined above.
  25. static void ValidateURIProps(void)
  26. {
  27. static bool fChecked = false;
  28. if (fChecked)
  29. return;
  30. for( int c = 0; c < 128; c++ )
  31. {
  32. if (isalnum(c))
  33. Assert(s_uriProps[c] & URIUnescaped);
  34. switch(c)
  35. {
  36. case '-':
  37. case '_':
  38. case '.':
  39. case '!':
  40. case '~':
  41. case '*':
  42. case '\'':
  43. case '(':
  44. case ')':
  45. Assert(s_uriProps[c] & URIUnescaped);
  46. break;
  47. case ';':
  48. case '/':
  49. case '?':
  50. case ':':
  51. case '@':
  52. case '&':
  53. case '=':
  54. case '+':
  55. case '$':
  56. case ',':
  57. case '#':
  58. Assert(s_uriProps[c] & URIReserved);
  59. break;
  60. default:
  61. if (!isalnum(c))
  62. Assert(0 == s_uriProps[c]);
  63. }
  64. }
  65. fChecked = true;
  66. }
  67. #else
  68. static inline void ValidateURIProps(void) {}
  69. #endif
  70. static inline BOOL InURISet( wchar_t c, unsigned char flags )
  71. {
  72. //static unsigned char *uriProps = GetURIProps();
  73. ValidateURIProps();
  74. return c <= 0x7f && (s_uriProps[c] & flags);
  75. }
  76. static const ulong MaxUTF8Len = 4;
  77. static ulong ToUTF8( ulong uVal, BYTE bUTF8[MaxUTF8Len]);
  78. static unsigned long FromUTF8( BYTE bUTF8[MaxUTF8Len], ulong uLen );
  79. static Var Encode(__in_ecount(len) const wchar_t* psz, ulong len, unsigned char unescapedFlags, ScriptContext* scriptContext );
  80. private:
  81. static bool DecodeByteFromHex(const wchar_t digit1, const wchar_t digit2, unsigned char &value);
  82. };
  83. }