|
|
68f5d9 |
/***************************************************************/
|
|
|
68f5d9 |
/* perk: PE Resource Kit */
|
|
|
ced38a |
/* Copyright (C) 2015--2017 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 |
|
|
|
798729 |
static const char aclr_red[] = "\x1b[31m";
|
|
|
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 |
|
|
|
4f36c2 |
static const char * 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 |
|
|
|
7ac294 |
static const char * pe_output_unit_header(const struct pe_error_info * erri)
|
|
|
7ac294 |
{
|
|
|
7ac294 |
if (!(erri->eflags & PERK_ERROR_CUSTOM))
|
|
|
7ac294 |
return "while opening";
|
|
|
7ac294 |
|
|
|
7ac294 |
else if (erri->elibcode == PERK_ERR_IMAGE_SIZE_ZERO)
|
|
|
7ac294 |
return "while mapping";
|
|
|
7ac294 |
|
|
|
7ac294 |
else
|
|
|
7ac294 |
return "while parsing";
|
|
|
7ac294 |
}
|
|
|
7ac294 |
|
|
|
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 |
{
|
|
|
798729 |
const char * epath;
|
|
|
68f5d9 |
const char * errdesc = pe_output_strerror(erri);
|
|
|
68f5d9 |
|
|
|
798729 |
epath = erri->euctx
|
|
|
798729 |
? *erri->euctx->path
|
|
|
798729 |
: erri->eunit;
|
|
|
798729 |
|
|
|
798729 |
if (epath && !(erri->eflags & PERK_ERROR_NESTED))
|
|
|
7ac294 |
if (fprintf(stderr,"%s: [%s] '%s':\n",
|
|
|
798729 |
dctx->program,
|
|
|
7ac294 |
pe_output_unit_header(erri),
|
|
|
798729 |
epath) < 0)
|
|
|
798729 |
return -1;
|
|
|
798729 |
|
|
|
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 |
{
|
|
|
798729 |
const char * epath;
|
|
|
68f5d9 |
const char * errdesc = pe_output_strerror(erri);
|
|
|
68f5d9 |
|
|
|
798729 |
epath = erri->euctx
|
|
|
798729 |
? *erri->euctx->path
|
|
|
798729 |
: erri->eunit;
|
|
|
798729 |
|
|
|
798729 |
if (epath && !(erri->eflags & PERK_ERROR_NESTED))
|
|
|
798729 |
if (fprintf(
|
|
|
798729 |
stderr,
|
|
|
7ac294 |
"%s%s%s:%s %s[%s]%s %s%s'%s'%s:\n",
|
|
|
798729 |
|
|
|
798729 |
aclr_bold,aclr_magenta,
|
|
|
798729 |
dctx->program,
|
|
|
798729 |
aclr_reset,
|
|
|
798729 |
|
|
|
798729 |
aclr_bold,
|
|
|
7ac294 |
pe_output_unit_header(erri),
|
|
|
798729 |
aclr_reset,
|
|
|
798729 |
|
|
|
798729 |
aclr_bold,aclr_red,
|
|
|
798729 |
epath,
|
|
|
798729 |
aclr_reset) < 0)
|
|
|
798729 |
return -1;
|
|
|
798729 |
|
|
|
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 |
}
|