errstr.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include "ParserPch.h"
  6. #include "errstr.h"
  7. // scaffolding - get a g_hInstance from scrbgase.cpp
  8. HANDLE g_hInstance;
  9. // Used as a prefix to generate the resource dll name.
  10. const char16 g_wszPrefix[] = _u("js");
  11. static BOOL FGetStringFromLibrary(HMODULE hlib, int istring, __out_ecount(cchMax) WCHAR * psz, int cchMax)
  12. {
  13. // NOTE - istring is expected to be HRESULT
  14. Assert(0 < cchMax);
  15. Assert(psz);
  16. HGLOBAL hgl = NULL;
  17. WCHAR * pchRes = NULL;
  18. HRSRC hrsrc;
  19. WCHAR * pchCur;
  20. int cch;
  21. int cstring;
  22. DWORD cbRes;
  23. int itable = ((WORD)istring >> 4) + 1;
  24. istring &= 0x0F;
  25. BOOL fRet = FALSE;
  26. #ifdef ENABLE_GLOBALIZATION
  27. psz[0] = '\0';
  28. if (NULL == hlib)
  29. goto LError;
  30. hrsrc = FindResourceEx((HMODULE)hlib, RT_STRING, MAKEINTRESOURCE(itable), 0);
  31. if (NULL == hrsrc)
  32. goto LError;
  33. hgl = LoadResource((HMODULE)hlib, hrsrc);
  34. if (NULL == hgl)
  35. goto LError;
  36. pchRes = (WCHAR *)LockResource(hgl);
  37. if (NULL == pchRes)
  38. goto LError;
  39. cbRes = SizeofResource((HMODULE)hlib, hrsrc);
  40. if (cbRes < sizeof(WORD))
  41. goto LError;
  42. pchCur = pchRes;
  43. for (cstring = istring; cstring-- > 0;)
  44. {
  45. if (cbRes - sizeof(WORD) < sizeof(WCHAR) * (pchCur - pchRes))
  46. goto LError;
  47. cch = (*(WORD *) pchCur) + 1;
  48. if (cch <= 0)
  49. goto LError;
  50. if (cbRes < sizeof(WCHAR) * cch)
  51. goto LError;
  52. if (cbRes - sizeof(WCHAR) * cch < sizeof(WCHAR) * (pchCur - pchRes))
  53. goto LError;
  54. pchCur += cch;
  55. }
  56. if (cbRes - sizeof(WORD) < sizeof(WCHAR) * (pchCur - pchRes))
  57. goto LError;
  58. cch = * (WORD *) pchCur;
  59. if (cch <= 0)
  60. goto LError;
  61. if (cbRes < sizeof(WCHAR) * (cch + 1))
  62. goto LError;
  63. if (cbRes - sizeof(WCHAR) * (cch + 1) < sizeof(WCHAR) * (pchCur - pchRes))
  64. goto LError;
  65. if (cch > cchMax - 1)
  66. cch = cchMax - 1;
  67. js_memcpy_s(psz, cchMax * sizeof(WCHAR), pchCur + 1, cch * sizeof(WCHAR));
  68. psz[cch] = '\0';
  69. fRet = TRUE;
  70. LError:
  71. #if !defined(_WIN32)
  72. //
  73. // Unlock/FreeResource non-essential on win32/64.
  74. //
  75. if (NULL != hgl)
  76. {
  77. if (NULL != pchRes)
  78. UnlockResource(hgl);
  79. FreeResource(hgl);
  80. }
  81. #endif // !defined(_WIN32)
  82. #endif // ENABLE_GLOBALIZATION
  83. return fRet;
  84. }
  85. BOOL FGetResourceString(int32 isz, __out_ecount(cchMax) OLECHAR *psz, int cchMax)
  86. {
  87. return FGetStringFromLibrary((HINSTANCE)g_hInstance, isz, psz, cchMax);
  88. }
  89. // Get a bstr version of the error string
  90. _NOINLINE // Don't inline. This function needs 2KB stack.
  91. BSTR BstrGetResourceString(int32 isz)
  92. {
  93. // NOTE - isz is expected to be HRESULT
  94. #ifdef _WIN32
  95. OLECHAR szT[1024];
  96. if (!FGetResourceString(isz, szT,
  97. sizeof(szT) / sizeof(szT[0]) - 1))
  98. {
  99. return NULL;
  100. }
  101. #else
  102. const char16* LoadResourceStr(UINT id);
  103. UINT id = (WORD)isz;
  104. const char16* szT = LoadResourceStr(id);
  105. if (!szT || !szT[0])
  106. {
  107. return NULL;
  108. }
  109. #endif
  110. return SysAllocString(szT);
  111. }