/**
 * XML Security Library (http://www.aleksey.com/xmlsec).
 *
 * This is free software; see the Copyright file in the source distribution for precise wording.
 *
 * Copyright (C) 2003-2026 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved.
 * Copyright (C) 2003 Cordys R&D BV, All rights reserved.
 */
/**
 * @brief Internal global header for MSCrypto used during compilation.
 */
#ifndef __XMLSEC_GLOBALS_H__
#define __XMLSEC_GLOBALS_H__

/**
 * Use autoconf defines if present.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#define IN_XMLSEC_CRYPTO
#define XMLSEC_PRIVATE

#include <windows.h>
#include <wincrypt.h>

#include <xmlsec/xmlsec.h>

/* Include common error helper macros. */
#include "../errors_helpers.h"
#include "../cast_helpers.h"

#define XMLSEC_MSCRYPTO_ERROR_MSG_BUFFER_SIZE       4096

void xmlSecMSCryptoGetErrorMessage      (DWORD dwError,
                                         xmlChar * out,
                                         int outLen);


/**
 * @brief Macro. Reports MSCrypto crypto errors.
 * @details Macro. The XMLSec library macro for reporting MSCrypto crypro errors.
 * @param errorFunction the failed function name.
 * @param errorObject the error specific error object (e.g. transform, key data, etc).
 */
#define xmlSecMSCryptoError(errorFunction, errorObject)            \
    {                                                              \
        DWORD dwLastError = GetLastError();                        \
        xmlChar errBuf[XMLSEC_MSCRYPTO_ERROR_MSG_BUFFER_SIZE];     \
        xmlSecMSCryptoGetErrorMessage(dwLastError, errBuf, sizeof(errBuf)); \
        xmlSecError(XMLSEC_ERRORS_HERE,                            \
                    (const char*)(errorObject),                    \
                    (errorFunction),                               \
                    XMLSEC_ERRORS_R_CRYPTO_FAILED,                 \
                    "MSCrypto error: %lu (0x%08lx): %s", \
                    (dwLastError),                                 \
                    (dwLastError),                                 \
                    errBuf                                         \
        );                                                         \
    }

/**
 * @brief Macro. Reports MSCrypto crypto errors.
 * @details Macro. The XMLSec library macro for reporting MSCrypto crypro errors.
 * @param errorFunction the failed function name.
 * @param errorObject the error specific error object (e.g. transform, key data, etc).
 * @param msg the extra message.
 * @param param the extra message param.
 */
#define xmlSecMSCryptoError2(errorFunction, errorObject, msg, param) \
    {                                                             \
        DWORD dwLastError = GetLastError();                       \
        xmlChar errBuf[XMLSEC_MSCRYPTO_ERROR_MSG_BUFFER_SIZE];    \
        xmlSecMSCryptoGetErrorMessage(dwLastError, errBuf, sizeof(errBuf)); \
        xmlSecError(XMLSEC_ERRORS_HERE,                           \
                    (const char*)(errorObject),                   \
                    (errorFunction),                              \
                    XMLSEC_ERRORS_R_CRYPTO_FAILED,                \
                    msg "; MSCrypto error: %lu (0x%08lx): %s",    \
                    (param),                                      \
                    (dwLastError),                                \
                    (dwLastError),                                \
                    errBuf                                        \
        );                                                        \
    }

/**
 * @brief Macro. Reports MSCrypto crypto errors.
 * @details Macro. The XMLSec library macro for reporting MSCrypto crypro errors.
 * @param errorFunction the failed function name.
 * @param errorObject the error specific error object (e.g. transform, key data, etc).
 * @param msg the extra message.
 * @param param1 the extra message param1.
 * @param param2 the extra message param2.
 */
#define xmlSecMSCryptoError3(errorFunction, errorObject, msg, param1, param2) \
    {                                                             \
        DWORD dwLastError = GetLastError();                       \
        xmlChar errBuf[XMLSEC_MSCRYPTO_ERROR_MSG_BUFFER_SIZE];    \
        xmlSecMSCryptoGetErrorMessage(dwLastError, errBuf, sizeof(errBuf)); \
        xmlSecError(XMLSEC_ERRORS_HERE,                           \
                    (const char*)(errorObject),                   \
                    (errorFunction),                              \
                    XMLSEC_ERRORS_R_CRYPTO_FAILED,                \
                    msg "; MSCrypto error: %lu (0x%08lx): %s",    \
                    (param1),                                     \
                    (param2),                                     \
                    (dwLastError),                                \
                    (dwLastError),                                \
                    errBuf                                        \
        );                                                        \
    }

#endif /* ! __XMLSEC_GLOBALS_H__ */
