summaryrefslogtreecommitdiffhomepage
path: root/src/output/pe_output_import_libraries.c
blob: 54b7b48c00384524a5832e969a35fff45419fe12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/***************************************************************/
/*  perk: PE Resource Kit                                      */
/*  Copyright (C) 2015--2016  Z. Gilboa                        */
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
/***************************************************************/

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <perk/perk.h>
#include <perk/perk_output.h>
#include "perk_errinfo_impl.h"

static int pretty_header(const struct pe_common_ctx * cctx, FILE * fout)
{
	return (cctx->fmtflags & PERK_PRETTY_YAML)
		? fputs("imports:\n",fout)
		: 0;
}

static int pretty_implib_header(const struct pe_common_ctx * cctx, const char * name, FILE * fout)
{
	if ((cctx->fmtflags & PERK_PRETTY_YAML) && (cctx->fmtflags & PERK_OUTPUT_IMPORT_SYMS))
		return fprintf(fout,"  %s:\n",name);
	else if (cctx->fmtflags & PERK_PRETTY_YAML)
		return fprintf(fout,"- %s:\n",name);
	else if (cctx->fmtflags & PERK_OUTPUT_IMPORT_SYMS)
		return fprintf(fout,"%s:\n",name);
	else
		return fprintf(fout,"%s\n",name);
}

static int pretty_implib_item(const struct pe_common_ctx * cctx, const char * name, FILE * fout)
{
	if (cctx->fmtflags & PERK_PRETTY_YAML)
		return fprintf(fout,"  - %s\n",name);
	else
		return fprintf(fout,"%s\n",name);
}

int pe_output_import_libraries(
	const struct pe_driver_ctx *	dctx,
	const struct pe_image_meta *	m,
	FILE *				fout)
{
	int				i;
	unsigned			j;
	const struct pe_common_ctx *	cctx = dctx->cctx;

	if (!m->summary.nimplibs)
		return 0;

	if (!fout)
		fout = stdout;

	if ((pretty_header(cctx,fout)) < 0)
		return PERK_FILE_ERROR(dctx);

	for (i=0; i<m->summary.nimplibs; i++) {
		if ((pretty_implib_header(cctx,m->idata[i].name,fout)) < 0)
			return PERK_FILE_ERROR(dctx);

		if (cctx->fmtflags & PERK_OUTPUT_IMPORT_SYMS)
			for (j=0; j<m->idata[i].count; j++)
				if (m->idata[i].items[j].name)
					if ((pretty_implib_item(
							cctx,
							m->idata[i].items[j].name,
							fout)) < 0)
						return PERK_FILE_ERROR(dctx);
	}

	return 0;
}