Blame src/output/pe_output_import_libraries.c

e2e2c2
/***************************************************************/
e2e2c2
/*  perk: PE Resource Kit                                      */
e2e2c2
/*  Copyright (C) 2015--2016  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>
9c5807
#include "perk_output_impl.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
	FILE *				ftmp;
251206
	int				i;
251206
	unsigned			j;
251206
	const struct pe_common_ctx *	cctx = dctx->cctx;
9c5807
b5c6e6
	if (!m->summary.nimplibs)
9c5807
		return 0;
9c5807
9c5807
	if (!(fout = pe_output_prolog(cctx,fout,&ftmp)))
251206
		return PERK_SYSTEM_ERROR(dctx);
9c5807
6025fb
	if ((pretty_header(cctx,fout)) < 0)
251206
		return pe_output_epilog(
251206
			PERK_FILE_ERROR(dctx),
251206
			ftmp);
0d3dde
b5c6e6
	for (i=0; i<m->summary.nimplibs; i++) {
6025fb
		if ((pretty_implib_header(cctx,m->idata[i].name,fout)) < 0)
251206
			return pe_output_epilog(
251206
				PERK_FILE_ERROR(dctx),
251206
				ftmp);
9c5807
9c5807
		if (cctx->fmtflags & PERK_OUTPUT_IMPORT_SYMS)
9c5807
			for (j=0; j<m->idata[i].count; j++)
9c5807
				if (m->idata[i].items[j].name)
6025fb
					if ((pretty_implib_item(
6025fb
							cctx,
6025fb
							m->idata[i].items[j].name,
6025fb
							fout)) < 0)
251206
						return pe_output_epilog(
251206
							PERK_FILE_ERROR(dctx),
251206
							ftmp);
9c5807
	}
9c5807
9c5807
	return pe_output_epilog(0,ftmp);
9c5807
}