Blame src/output/pe_output_import_libraries.c

e2e2c2
/***************************************************************/
e2e2c2
/*  perk: PE Resource Kit                                      */
ced38a
/*  Copyright (C) 2015--2017  Z. Gilboa                        */
e2e2c2
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
e2e2c2
/***************************************************************/
e2e2c2
9c5807
#include <stdint.h>
9c5807
#include <stdlib.h>
9c5807
#include <stdio.h>
9c5807
#include <string.h>
9c5807
#include <errno.h>
9c5807
9c5807
#include <perk/perk.h>
9c5807
#include <perk/perk_output.h>
251206
#include "perk_errinfo_impl.h"
9c5807
6025fb
static int pretty_header(const struct pe_common_ctx * cctx, FILE * fout)
e343a6
{
6025fb
	return (cctx->fmtflags & PERK_PRETTY_YAML)
6025fb
		? fputs("imports:\n",fout)
6025fb
		: 0;
e343a6
}
0d3dde
6025fb
static int pretty_implib_header(const struct pe_common_ctx * cctx, const char * name, FILE * fout)
e343a6
{
e343a6
	if ((cctx->fmtflags & PERK_PRETTY_YAML) && (cctx->fmtflags & PERK_OUTPUT_IMPORT_SYMS))
6025fb
		return fprintf(fout,"  %s:\n",name);
e343a6
	else if (cctx->fmtflags & PERK_PRETTY_YAML)
6025fb
		return fprintf(fout,"- %s:\n",name);
e343a6
	else if (cctx->fmtflags & PERK_OUTPUT_IMPORT_SYMS)
6025fb
		return fprintf(fout,"%s:\n",name);
e343a6
	else
6025fb
		return fprintf(fout,"%s\n",name);
e343a6
}
0d3dde
6025fb
static int pretty_implib_item(const struct pe_common_ctx * cctx, const char * name, FILE * fout)
0d3dde
{
0d3dde
	if (cctx->fmtflags & PERK_PRETTY_YAML)
6025fb
		return fprintf(fout,"  - %s\n",name);
0d3dde
	else
6025fb
		return fprintf(fout,"%s\n",name);
0d3dde
}
0d3dde
9c5807
int pe_output_import_libraries(
251206
	const struct pe_driver_ctx *	dctx,
9c5807
	const struct pe_image_meta *	m,
9c5807
	FILE *				fout)
9c5807
{
251206
	int				i;
251206
	unsigned			j;
251206
	const struct pe_common_ctx *	cctx = dctx->cctx;
9c5807
58ced9
	if (!m->m_stats.nimplibs)
9c5807
		return 0;
9c5807
f11c1f
	if (!fout)
f11c1f
		fout = stdout;
9c5807
6025fb
	if ((pretty_header(cctx,fout)) < 0)
f11c1f
		return PERK_FILE_ERROR(dctx);
0d3dde
58ced9
	for (i=0; i<m->m_stats.nimplibs; i++) {
58ced9
		if ((pretty_implib_header(cctx,m->m_idata[i].ih_name,fout)) < 0)
f11c1f
			return PERK_FILE_ERROR(dctx);
9c5807
9c5807
		if (cctx->fmtflags & PERK_OUTPUT_IMPORT_SYMS)
58ced9
			for (j=0; j<m->m_idata[i].ih_count; j++)
58ced9
				if (m->m_idata[i].ih_items[j].ii_name)
6025fb
					if ((pretty_implib_item(
6025fb
							cctx,
58ced9
							m->m_idata[i].ih_items[j].ii_name,
6025fb
							fout)) < 0)
f11c1f
						return PERK_FILE_ERROR(dctx);
9c5807
	}
9c5807
f11c1f
	return 0;
9c5807
}