#include <Security/cssmerrno.h>
#include <Security/SecBase.h>
#include <Security/Authorization.h>
#include <Security/globalizer.h>
#include <stdio.h>
#include <map>
namespace Security {
struct ErrorMap : public map<CSSM_RETURN, const char *> {
ErrorMap();
};
static ModuleNexus<ErrorMap> errorMap;
struct Mapping {
CSSM_RETURN error;
const char *name;
};
#include "errorcodes.gen" // include generated error tables
ErrorMap::ErrorMap()
{
for (unsigned n = 0; n < sizeof(errorList) / sizeof(errorList[0]); n++)
(*this)[errorList[n].error] = errorList[n].name;
}
extern "C"
void cssmPerror(const char *how, CSSM_RETURN error)
{
if (how)
fprintf(stderr, "%s: %s\n", how, cssmErrorString(error).c_str());
else
fprintf(stderr, "%s\n", cssmErrorString(error).c_str());
}
string cssmErrorString(CSSM_RETURN error)
{
if (error == CSSM_OK) {
return "[ok]";
} else if (error >= errSecErrnoBase && error <= errSecErrnoLimit) {
return string("UNIX[") + strerror(error - errSecErrnoBase) + "]";
} else if (error > 0 &&
int(error) < int(sizeof(convErrorList) / sizeof(convErrorList[0])) &&
convErrorList[error]) {
return string("COMMON[") + convErrorList[error] + "]";
} else {
ErrorMap::const_iterator it = errorMap().find(error);
if (it == errorMap().end()) {
char msg[80];
snprintf(msg, sizeof(msg), "[UNKNOWN:0x%lx=%ld]", error, error);
return msg;
} else {
return it->second;
}
}
}
string cssmErrorString(const CssmCommonError &error)
{ return cssmErrorString(error.cssmError()); }
}