clamav/clambc/bcrun.c

171 lines
4.3 KiB
C
Raw Normal View History

2009-06-26 20:23:52 +03:00
/*
* ClamAV bytecode handler tool.
*
* Copyright (C) 2009 Sourcefire, Inc.
*
* Authors: Török Edvin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#include "cltypes.h"
2009-06-26 20:23:52 +03:00
#include "bytecode.h"
#include "clamav.h"
2009-06-30 18:15:12 +03:00
#include "shared/optparser.h"
2009-06-30 19:27:26 +03:00
#include "shared/misc.h"
2009-06-26 20:23:52 +03:00
#include <stdlib.h>
2009-06-30 18:15:12 +03:00
static void help(void)
{
printf("\n");
2009-06-30 20:14:21 +03:00
printf(" Clam AntiVirus: Bytecode Testing Tool %s\n",
get_version());
2009-06-30 18:15:12 +03:00
printf(" By The ClamAV Team: http://www.clamav.net/team\n");
printf(" (C) 2009 Sourcefire, Inc.\n\n");
2009-07-07 23:36:36 +03:00
printf("clambc <file> [function] [param1 ...]\n\n");
2009-06-30 18:15:12 +03:00
printf(" --help -h Show help\n");
printf(" --version -V Show version\n");
printf(" file file to test\n");
printf("\n");
return;
}
2009-06-26 20:23:52 +03:00
int main(int argc, char *argv[])
{
FILE *f;
struct cli_bc *bc;
struct cli_bc_ctx *ctx;
2009-08-27 18:12:39 +03:00
int rc, dbgargc;
2009-06-30 18:15:12 +03:00
struct optstruct *opts;
2009-07-07 23:36:36 +03:00
unsigned funcid=0, i;
struct cli_all_bc bcs;
2009-06-30 18:15:12 +03:00
opts = optparse(NULL, argc, argv, 1, OPT_CLAMBC, 0, NULL);
if (!opts) {
fprintf(stderr, "ERROR: Can't parse command line options\n");
2009-06-26 20:23:52 +03:00
exit(1);
}
2009-09-04 12:09:17 +03:00
if(optget(opts, "version")->enabled) {
printf("Clam AntiVirus Bytecode Testing Tool %s\n", get_version());
2009-06-30 18:15:12 +03:00
optfree(opts);
exit(0);
}
2009-09-04 12:09:17 +03:00
if(optget(opts, "help")->enabled || !opts->filename) {
2009-06-30 18:15:12 +03:00
optfree(opts);
2009-09-04 12:09:17 +03:00
help();
2009-06-30 18:15:12 +03:00
exit(0);
}
2009-07-07 23:36:36 +03:00
f = fopen(opts->filename[0], "r");
2009-06-26 20:23:52 +03:00
if (!f) {
fprintf(stderr, "Unable to load %s\n", argv[1]);
2009-06-30 19:40:26 +03:00
optfree(opts);
2009-06-26 20:23:52 +03:00
exit(2);
}
bc = malloc(sizeof(*bc));
if (!bc) {
fprintf(stderr, "Out of memory\n");
2009-06-30 19:40:26 +03:00
optfree(opts);
2009-06-26 20:23:52 +03:00
exit(3);
}
cl_debug();
rc = cl_init(CL_INIT_DEFAULT);
if (rc != CL_SUCCESS) {
fprintf(stderr,"Unable to init libclamav: %s\n", cl_strerror(rc));
optfree(opts);
exit(4);
}
2009-08-27 18:12:39 +03:00
dbgargc=1;
while (opts->filename[dbgargc]) dbgargc++;
if (dbgargc > 1)
cli_bytecode_debug(dbgargc, opts->filename);
2009-09-04 12:09:17 +03:00
if (optget(opts, "force-interpreter")->enabled) {
bcs.engine = NULL;
} else {
rc = cli_bytecode_init(&bcs);
if (rc != CL_SUCCESS) {
fprintf(stderr,"Unable to init bytecode engine: %s\n", cl_strerror(rc));
optfree(opts);
exit(4);
}
}
bcs.all_bcs = bc;
bcs.count = 1;
2009-06-26 20:23:52 +03:00
rc = cli_bytecode_load(bc, f, NULL);
if (rc != CL_SUCCESS) {
fprintf(stderr,"Unable to load bytecode: %s\n", cl_strerror(rc));
2009-06-30 19:40:26 +03:00
optfree(opts);
2009-06-26 20:23:52 +03:00
exit(4);
}
2009-06-30 19:58:08 +03:00
rc = cli_bytecode_prepare(&bcs);
if (rc != CL_SUCCESS) {
fprintf(stderr,"Unable to prepare bytecode: %s\n", cl_strerror(rc));
optfree(opts);
exit(4);
}
2009-06-26 20:23:52 +03:00
fclose(f);
printf("Bytecode loaded\n");
ctx = cli_bytecode_context_alloc();
2009-06-26 20:23:52 +03:00
if (!ctx) {
fprintf(stderr,"Out of memory\n");
exit(3);
}
2009-07-07 23:36:36 +03:00
if (opts->filename[1]) {
funcid = atoi(opts->filename[1]);
}
cli_bytecode_context_setfuncid(ctx, bc, funcid);
printf("Running bytecode function :%u\n", funcid);
if (opts->filename[1]) {
i=2;
while (opts->filename[i]) {
rc = cli_bytecode_context_setparam_int(ctx, i-2, atoi(opts->filename[i]));
if (rc != CL_SUCCESS) {
fprintf(stderr,"Unable to set param %u: %s\n", i-2, cl_strerror(rc));
}
i++;
}
}
2009-08-27 20:41:29 +03:00
rc = cli_bytecode_run(&bcs, bc, ctx);
2009-07-07 23:36:36 +03:00
if (rc != CL_SUCCESS) {
fprintf(stderr,"Unable to run bytecode: %s\n", cl_strerror(rc));
} else {
2009-07-08 12:45:06 +03:00
uint64_t v;
2009-07-07 23:36:36 +03:00
printf("Bytecode run finished\n");
2009-07-08 12:45:06 +03:00
v = cli_bytecode_context_getresult_int(ctx);
printf("Bytecode returned: 0x%llx\n", (long long)v);
2009-07-07 23:36:36 +03:00
}
cli_bytecode_context_destroy(ctx);
2009-06-26 20:23:52 +03:00
cli_bytecode_destroy(bc);
cli_bytecode_done(&bcs);
2009-06-26 20:23:52 +03:00
free(bc);
2009-06-30 19:40:26 +03:00
optfree(opts);
2009-07-08 12:45:06 +03:00
printf("Exiting\n");
2009-06-26 20:23:52 +03:00
return 0;
}