|
|
68f5d9 |
/***************************************************************/
|
|
|
68f5d9 |
/* perk: PE Resource Kit */
|
|
|
68f5d9 |
/* Copyright (C) 2015--2016 Z. Gilboa */
|
|
|
68f5d9 |
/* Released under GPLv2 and GPLv3; see COPYING.PERK. */
|
|
|
68f5d9 |
/***************************************************************/
|
|
|
68f5d9 |
|
|
|
68f5d9 |
#include <stdio.h>
|
|
|
68f5d9 |
#include <string.h>
|
|
|
68f5d9 |
#include <errno.h>
|
|
|
68f5d9 |
#include <unistd.h>
|
|
|
68f5d9 |
#include <perk/perk.h>
|
|
|
68f5d9 |
|
|
|
68f5d9 |
static const char aclr_reset[] = "\x1b[0m";
|
|
|
68f5d9 |
static const char aclr_bold[] = "\x1b[1m";
|
|
|
68f5d9 |
|
|
|
68f5d9 |
static const char aclr_green[] = "\x1b[32m";
|
|
|
68f5d9 |
static const char aclr_blue[] = "\x1b[34m";
|
|
|
68f5d9 |
static const char aclr_magenta[] = "\x1b[35m";
|
|
|
68f5d9 |
|
|
|
0d161c |
static const char const * const pe_error_strings[PERK_ERR_CAP] = {
|
|
|
0d161c |
[PERK_ERR_FLOW_ERROR] = "flow error: unexpected condition or other",
|
|
|
0d161c |
[PERK_ERR_FLEE_ERROR] = "flees and bugs and cats and mice",
|
|
|
0d161c |
[PERK_ERR_NULL_CONTEXT] = "null driver or unit context",
|
|
|
0d161c |
[PERK_ERR_NULL_IMAGE] = "null image base pointer",
|
|
|
0d161c |
[PERK_ERR_INVALID_CONTEXT] = "invalid driver or unit context",
|
|
|
0d161c |
[PERK_ERR_INVALID_IMAGE] = "invalid PE image",
|
|
|
0d161c |
[PERK_ERR_IMAGE_SIZE_ZERO] = "PE image size cannot be zero",
|
|
|
0d161c |
[PERK_ERR_IMAGE_MALFORMED] = "malformed PE image detected",
|
|
|
0d161c |
[PERK_ERR_BAD_DOS_HEADER] = "bad DOS header",
|
|
|
0d161c |
[PERK_ERR_BAD_COFF_HEADER] = "bad COFF header",
|
|
|
0d161c |
[PERK_ERR_BAD_IMAGE_TYPE] = "bad PE image type",
|
|
|
0d161c |
};
|
|
|
0d161c |
|
|
|
68f5d9 |
static const char * pe_output_error_header(const struct pe_error_info * erri)
|
|
|
68f5d9 |
{
|
|
|
f006c4 |
if (erri->eflags & PERK_ERROR_CHILD)
|
|
|
68f5d9 |
return "exec error upon";
|
|
|
68f5d9 |
|
|
|
f006c4 |
else if (erri->eflags & PERK_ERROR_TOP_LEVEL)
|
|
|
68f5d9 |
return "error logged in";
|
|
|
68f5d9 |
|
|
|
f006c4 |
else if (erri->eflags & PERK_ERROR_NESTED)
|
|
|
68f5d9 |
return "< returned to >";
|
|
|
68f5d9 |
|
|
|
68f5d9 |
else
|
|
|
68f5d9 |
return "distorted state";
|
|
|
68f5d9 |
}
|
|
|
68f5d9 |
|
|
|
68f5d9 |
static const char * pe_output_strerror(const struct pe_error_info * erri)
|
|
|
68f5d9 |
{
|
|
|
f006c4 |
if (erri->eflags & PERK_ERROR_CUSTOM)
|
|
|
f006c4 |
return ((erri->elibcode < 0) || (erri->elibcode >= PERK_ERR_CAP))
|
|
|
0d161c |
? "internal error: please report to the maintainer"
|
|
|
f006c4 |
: pe_error_strings[erri->elibcode];
|
|
|
68f5d9 |
|
|
|
f006c4 |
else if (erri->eflags & PERK_ERROR_NESTED)
|
|
|
68f5d9 |
return "";
|
|
|
68f5d9 |
|
|
|
f006c4 |
else if (erri->eflags & PERK_ERROR_CHILD)
|
|
|
68f5d9 |
return "(see child process error messages)";
|
|
|
68f5d9 |
|
|
|
f006c4 |
else if (erri->esyscode == ENOBUFS)
|
|
|
68f5d9 |
return "input error: string length exceeds buffer size.";
|
|
|
68f5d9 |
|
|
|
68f5d9 |
else
|
|
|
f006c4 |
return strerror(erri->esyscode);
|
|
|
68f5d9 |
}
|
|
|
68f5d9 |
|
|
|
68f5d9 |
static int pe_output_error_record_plain(
|
|
|
68f5d9 |
const struct pe_driver_ctx * dctx,
|
|
|
68f5d9 |
const struct pe_error_info * erri)
|
|
|
68f5d9 |
{
|
|
|
68f5d9 |
const char * errdesc = pe_output_strerror(erri);
|
|
|
68f5d9 |
|
|
|
68f5d9 |
if (fprintf(stderr,"%s: %s %s(), line %d%s%s.\n",
|
|
|
68f5d9 |
dctx->program,
|
|
|
68f5d9 |
pe_output_error_header(erri),
|
|
|
f006c4 |
erri->efunction,
|
|
|
f006c4 |
erri->eline,
|
|
|
68f5d9 |
strlen(errdesc) ? ": " : "",
|
|
|
68f5d9 |
errdesc) < 0)
|
|
|
68f5d9 |
return -1;
|
|
|
68f5d9 |
|
|
|
68f5d9 |
return fflush(stderr);
|
|
|
68f5d9 |
}
|
|
|
68f5d9 |
|
|
|
68f5d9 |
static int pe_output_error_record_annotated(
|
|
|
68f5d9 |
const struct pe_driver_ctx * dctx,
|
|
|
68f5d9 |
const struct pe_error_info * erri)
|
|
|
68f5d9 |
{
|
|
|
68f5d9 |
const char * errdesc = pe_output_strerror(erri);
|
|
|
68f5d9 |
|
|
|
68f5d9 |
if (fprintf(
|
|
|
68f5d9 |
stderr,
|
|
|
68f5d9 |
"%s%s%s:%s %s%s%s %s%s%s()%s, %s%sline %d%s%s%s%s%s.\n",
|
|
|
68f5d9 |
|
|
|
68f5d9 |
aclr_bold,aclr_magenta,
|
|
|
68f5d9 |
dctx->program,
|
|
|
68f5d9 |
aclr_reset,
|
|
|
68f5d9 |
|
|
|
68f5d9 |
aclr_bold,
|
|
|
68f5d9 |
pe_output_error_header(erri),
|
|
|
68f5d9 |
aclr_reset,
|
|
|
68f5d9 |
|
|
|
68f5d9 |
aclr_bold,aclr_blue,
|
|
|
f006c4 |
erri->efunction,
|
|
|
68f5d9 |
aclr_reset,
|
|
|
68f5d9 |
|
|
|
68f5d9 |
aclr_bold,aclr_green,
|
|
|
f006c4 |
erri->eline,
|
|
|
68f5d9 |
aclr_reset,
|
|
|
68f5d9 |
strlen(errdesc) ? ": " : "",
|
|
|
68f5d9 |
|
|
|
68f5d9 |
aclr_bold,
|
|
|
68f5d9 |
pe_output_strerror(erri),
|
|
|
68f5d9 |
aclr_reset) < 0)
|
|
|
68f5d9 |
return -1;
|
|
|
68f5d9 |
|
|
|
68f5d9 |
return fflush(stderr);
|
|
|
68f5d9 |
}
|
|
|
68f5d9 |
|
|
|
68f5d9 |
int pe_output_error_record(
|
|
|
68f5d9 |
const struct pe_driver_ctx * dctx,
|
|
|
68f5d9 |
const struct pe_error_info * erri)
|
|
|
68f5d9 |
{
|
|
|
68f5d9 |
if (dctx->cctx->drvflags & PERK_DRIVER_ANNOTATE_NEVER)
|
|
|
68f5d9 |
return pe_output_error_record_plain(dctx,erri);
|
|
|
68f5d9 |
|
|
|
68f5d9 |
else if (dctx->cctx->drvflags & PERK_DRIVER_ANNOTATE_ALWAYS)
|
|
|
68f5d9 |
return pe_output_error_record_annotated(dctx,erri);
|
|
|
68f5d9 |
|
|
|
68f5d9 |
else if (isatty(STDERR_FILENO))
|
|
|
68f5d9 |
return pe_output_error_record_annotated(dctx,erri);
|
|
|
68f5d9 |
|
|
|
68f5d9 |
else
|
|
|
68f5d9 |
return pe_output_error_record_plain(dctx,erri);
|
|
|
68f5d9 |
}
|
|
|
68f5d9 |
|
|
|
68f5d9 |
static int pe_output_error_vector_plain(const struct pe_driver_ctx * dctx)
|
|
|
68f5d9 |
{
|
|
|
68f5d9 |
struct pe_error_info ** perr;
|
|
|
68f5d9 |
|
|
|
68f5d9 |
for (perr=dctx->errv; *perr; perr++)
|
|
|
68f5d9 |
if (pe_output_error_record_plain(dctx,*perr))
|
|
|
68f5d9 |
return -1;
|
|
|
68f5d9 |
|
|
|
68f5d9 |
return 0;
|
|
|
68f5d9 |
}
|
|
|
68f5d9 |
|
|
|
68f5d9 |
static int pe_output_error_vector_annotated(const struct pe_driver_ctx * dctx)
|
|
|
68f5d9 |
{
|
|
|
68f5d9 |
struct pe_error_info ** perr;
|
|
|
68f5d9 |
|
|
|
68f5d9 |
for (perr=dctx->errv; *perr; perr++)
|
|
|
68f5d9 |
if (pe_output_error_record_annotated(dctx,*perr))
|
|
|
68f5d9 |
return -1;
|
|
|
68f5d9 |
|
|
|
68f5d9 |
return 0;
|
|
|
68f5d9 |
}
|
|
|
68f5d9 |
|
|
|
68f5d9 |
int pe_output_error_vector(const struct pe_driver_ctx * dctx)
|
|
|
68f5d9 |
{
|
|
|
68f5d9 |
if (dctx->cctx->drvflags & PERK_DRIVER_ANNOTATE_NEVER)
|
|
|
68f5d9 |
return pe_output_error_vector_plain(dctx);
|
|
|
68f5d9 |
|
|
|
68f5d9 |
else if (dctx->cctx->drvflags & PERK_DRIVER_ANNOTATE_ALWAYS)
|
|
|
68f5d9 |
return pe_output_error_vector_annotated(dctx);
|
|
|
68f5d9 |
|
|
|
68f5d9 |
else if (isatty(STDERR_FILENO))
|
|
|
68f5d9 |
return pe_output_error_vector_annotated(dctx);
|
|
|
68f5d9 |
|
|
|
68f5d9 |
else
|
|
|
68f5d9 |
return pe_output_error_vector_plain(dctx);
|
|
|
68f5d9 |
}
|