errstr.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 wchar_t g_wszPrefix[] = L"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. AssertArrMem(psz, cchMax);
  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. psz[0] = '\0';
  27. if (NULL == hlib)
  28. goto LError;
  29. hrsrc = FindResourceEx((HMODULE)hlib, RT_STRING, MAKEINTRESOURCE(itable), 0);
  30. if (NULL == hrsrc)
  31. goto LError;
  32. hgl = LoadResource((HMODULE)hlib, hrsrc);
  33. if (NULL == hgl)
  34. goto LError;
  35. pchRes = (WCHAR *)LockResource(hgl);
  36. if (NULL == pchRes)
  37. goto LError;
  38. cbRes = SizeofResource((HMODULE)hlib, hrsrc);
  39. if (cbRes < sizeof(WORD))
  40. goto LError;
  41. pchCur = pchRes;
  42. for (cstring = istring; cstring-- > 0;)
  43. {
  44. if (cbRes - sizeof(WORD) < sizeof(WCHAR) * (pchCur - pchRes))
  45. goto LError;
  46. cch = (*(WORD *) pchCur) + 1;
  47. if (cch <= 0)
  48. goto LError;
  49. if (cbRes < sizeof(WCHAR) * cch)
  50. goto LError;
  51. if (cbRes - sizeof(WCHAR) * cch < sizeof(WCHAR) * (pchCur - pchRes))
  52. goto LError;
  53. pchCur += cch;
  54. }
  55. if (cbRes - sizeof(WORD) < sizeof(WCHAR) * (pchCur - pchRes))
  56. goto LError;
  57. cch = * (WORD *) pchCur;
  58. if (cch <= 0)
  59. goto LError;
  60. if (cbRes < sizeof(WCHAR) * (cch + 1))
  61. goto LError;
  62. if (cbRes - sizeof(WCHAR) * (cch + 1) < sizeof(WCHAR) * (pchCur - pchRes))
  63. goto LError;
  64. if (cch > cchMax - 1)
  65. cch = cchMax - 1;
  66. js_memcpy_s(psz, cchMax * sizeof(WCHAR), pchCur + 1, cch * sizeof(WCHAR));
  67. psz[cch] = '\0';
  68. fRet = TRUE;
  69. LError:
  70. #if !_WIN32 && !_WIN64
  71. //
  72. // Unlock/FreeResource non-essential on win32/64.
  73. //
  74. if (NULL != hgl)
  75. {
  76. if (NULL != pchRes)
  77. UnlockResource(hgl);
  78. FreeResource(hgl);
  79. }
  80. #endif
  81. return fRet;
  82. }
  83. BOOL FGetResourceString(long isz, __out_ecount(cchMax) OLECHAR *psz, int cchMax)
  84. {
  85. return FGetStringFromLibrary((HINSTANCE)g_hInstance, isz, psz, cchMax);
  86. }
  87. // Get a bstr version of the error string
  88. __declspec(noinline) // Don't inline. This function needs 2KB stack.
  89. BSTR BstrGetResourceString(long isz)
  90. {
  91. // NOTE - isz is expected to be HRESULT
  92. OLECHAR szT[1024];
  93. if (!FGetResourceString(isz, szT,
  94. sizeof(szT) / sizeof(szT[0]) - 1))
  95. {
  96. return NULL;
  97. }
  98. return SysAllocString(szT);
  99. }