error.c 1.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "common.h"
#include "error.h"

11 12 13 14
#ifdef GIT_WINHTTP
# include <winhttp.h>
#endif

15
#ifndef WC_ERR_INVALID_CHARS
16
#define WC_ERR_INVALID_CHARS	0x80
17
#endif
18

19 20 21
char *git_win32_get_error_message(DWORD error_code)
{
	LPWSTR lpMsgBuf = NULL;
22 23 24 25 26
	HMODULE hModule = NULL;
	char *utf8_msg = NULL;
	int utf8_size;
	DWORD dwFlags =
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS;
27 28 29 30

	if (!error_code)
		return NULL;

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#ifdef GIT_WINHTTP
	/* Errors raised by WinHTTP are not in the system resource table */
	if (error_code >= WINHTTP_ERROR_BASE &&
		error_code <= WINHTTP_ERROR_LAST)
		hModule = GetModuleHandleW(L"winhttp");
#endif

	GIT_UNUSED(hModule);

	if (hModule)
		dwFlags |= FORMAT_MESSAGE_FROM_HMODULE;
	else
		dwFlags |= FORMAT_MESSAGE_FROM_SYSTEM;

	if (FormatMessageW(dwFlags, hModule, error_code,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPWSTR)&lpMsgBuf, 0, NULL)) {

		/* Invalid code point check supported on Vista+ only */
50
		if (git_has_win32_version(6, 0, 0))
51 52 53 54 55 56 57 58 59 60
			dwFlags = WC_ERR_INVALID_CHARS;
		else
			dwFlags = 0;

		utf8_size = WideCharToMultiByte(CP_UTF8, dwFlags,
			lpMsgBuf, -1, NULL, 0, NULL, NULL);

		if (!utf8_size) {
			assert(0);
			goto on_error;
61
		}
62 63 64 65 66 67 68 69 70 71

		utf8_msg = git__malloc(utf8_size);

		if (!utf8_msg)
			goto on_error;

		if (!WideCharToMultiByte(CP_UTF8, dwFlags,
			lpMsgBuf, -1, utf8_msg, utf8_size, NULL, NULL)) {
			git__free(utf8_msg);
			goto on_error;
72 73
		}

74
on_error:
75 76
		LocalFree(lpMsgBuf);
	}
77 78

	return utf8_msg;
79
}