Blame src/win32/error.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "error.h"
Packit Service 20376f
#include "utf-conv.h"
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WINHTTP
Packit Service 20376f
# include <winhttp.h>
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
char *git_win32_get_error_message(DWORD error_code)
Packit Service 20376f
{
Packit Service 20376f
	LPWSTR lpMsgBuf = NULL;
Packit Service 20376f
	HMODULE hModule = NULL;
Packit Service 20376f
	char *utf8_msg = NULL;
Packit Service 20376f
	DWORD dwFlags =
Packit Service 20376f
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS;
Packit Service 20376f
Packit Service 20376f
	if (!error_code)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WINHTTP
Packit Service 20376f
	/* Errors raised by WinHTTP are not in the system resource table */
Packit Service 20376f
	if (error_code >= WINHTTP_ERROR_BASE &&
Packit Service 20376f
		error_code <= WINHTTP_ERROR_LAST)
Packit Service 20376f
		hModule = GetModuleHandleW(L"winhttp");
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	GIT_UNUSED(hModule);
Packit Service 20376f
Packit Service 20376f
	if (hModule)
Packit Service 20376f
		dwFlags |= FORMAT_MESSAGE_FROM_HMODULE;
Packit Service 20376f
	else
Packit Service 20376f
		dwFlags |= FORMAT_MESSAGE_FROM_SYSTEM;
Packit Service 20376f
Packit Service 20376f
	if (FormatMessageW(dwFlags, hModule, error_code,
Packit Service 20376f
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
Packit Service 20376f
		(LPWSTR)&lpMsgBuf, 0, NULL)) {
Packit Service 20376f
		/* Convert the message to UTF-8. If this fails, we will
Packit Service 20376f
		 * return NULL, which is a condition expected by the caller */
Packit Service 20376f
		if (git__utf16_to_8_alloc(&utf8_msg, lpMsgBuf) < 0)
Packit Service 20376f
			utf8_msg = NULL;
Packit Service 20376f
Packit Service 20376f
		LocalFree(lpMsgBuf);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return utf8_msg;
Packit Service 20376f
}