/* radare - LGPL - Copyright 2009-2026 - pancake, nibble, dso */

#define R_LOG_ORIGIN "bin.java"

#include <r_bin.h>
#include "../../shlr/java/class.h"
#include "../../shlr/java/code.h"

static bool add_sdb_bin_obj(const char *key, RBinJavaObj *bin_obj) {
	char value[SDB_NUM_BUFSZ] = {0};
	char *addr = sdb_itoa ((ut64) (size_t) bin_obj, 16, value, sizeof (value));
	if (key && bin_obj && bin_obj->kv) {
		R_LOG_DEBUG ("Adding %s:%s to the bin_objs db", key, addr);
		sdb_set (bin_obj->kv, key, addr, 0);
		return true;
	}
	return false;
}

static void add_bin_obj_to_sdb(RBinJavaObj *bj) {
	R_RETURN_IF_FAIL (bj);
	char *jvcname = r_bin_java_build_obj_key (bj);
	add_sdb_bin_obj (jvcname, bj);
	bj->AllJavaBinObjs = bj->kv; // XXX that was a global.. so this must be inside bin->sdb namespace
	free (jvcname);
}

static Sdb *get_sdb(RBinFile *bf) {
	RBinJavaObj *bin = R_UNWRAP3 (bf, bo, bin_obj);
	return bin? bin->kv: NULL;
}

static bool load(RBinFile *bf, RBuffer *buf, ut64 loadaddr) {
	RBuffer *tbuf = r_ref (buf);
	RBinJavaObj *tbo = R_NEW0 (RBinJavaObj);
	tbo->classes_names_only = bf->rbin->options.classes_names_only;
	ut64 tmpsz = 0;
	const ut8 *tmp = r_buf_data (tbuf, &tmpsz);
	if (!tmp || !r_bin_java_new_bin (tbo, loadaddr, bf->sdb, tmp, tmpsz)) {
		r_bin_java_free (tbo);
		r_unref (tbuf);
		return false;
	}
	bf->bo->bin_obj = tbo;
	add_bin_obj_to_sdb (tbo);
	if (bf && bf->file) {
		tbo->file = strdup (bf->file);
	}
	r_unref (tbuf);
	return true;
}

static void destroy(RBinFile *bf) {
	r_bin_java_free ((RBinJavaObj *) bf->bo->bin_obj);
}

static RList *entries(RBinFile *bf) {
	return r_bin_java_get_entrypoints (bf->bo->bin_obj);
}

static RList *classes(RBinFile *bf) {
	return r_bin_java_get_classes ((RBinJavaObj *) bf->bo->bin_obj);
}

static bool symbols_vec(RBinFile *bf) {
	r_bin_java_load_symbols ((RBinJavaObj *) bf->bo->bin_obj, &bf->bo->symbols_vec);
	return true;
}

static RVecRBinString *strings(RBinFile *bf) {
	return r_bin_java_get_strings ((RBinJavaObj *) bf->bo->bin_obj);
}

static RBinInfo *info(RBinFile *bf) {
	RBinJavaObj *jo = bf->bo->bin_obj;
	RBinInfo *ret = R_NEW0 (RBinInfo);
	ret->lang = jo ? jo->lang : "java";
	ret->file = strdup (bf->file);
	ret->type = strdup ("JAVA CLASS");
	ret->bclass = r_bin_java_get_version (bf->bo->bin_obj);
	ret->has_va = 0;
	// ret->has_lit = true;
	ret->rclass = strdup ("class");
	ret->os = strdup ("any");
	ret->subsystem = strdup ("any");
	ret->machine = strdup ("jvm");
	ret->arch = strdup ("java");
	ret->bits = 32;
	ret->big_endian = 0;
	ret->dbg_info = 4 | 8; /* LineNums | Syms */
	return ret;
}

static bool check(RBinFile *bf, RBuffer *b) {
	if (r_buf_size (b) >= 10) {
		ut8 buf[4];
		r_buf_read_at (b, 0, buf, sizeof (buf));
		if (!memcmp (buf, "\xca\xfe\xba\xbe", 4)) {
			ut16 constant_pool_count = r_buf_read_be16_at (b, 8);
			ut16 version = r_buf_read_be16_at (b, 6);
			if (constant_pool_count > 0 && version > 0 && version < 1024) {
				return true;
			}
		}
	}
	return false;
}

static int retdemangle(const char *str) {
	return R_BIN_LANG_JAVA;
}

static RBinAddr *binsym(RBinFile *bf, int sym) {
	return r_bin_java_get_entrypoint (bf->bo->bin_obj, sym);
}

static R_UNOWNED RList *lines(RBinFile *bf) {
	return NULL;
#if 0
	char *file = bf->file? strdup (bf->file): strdup ("");
	RList *list = r_list_newf (free);
	// XXX the owner of this list should be the plugin, so we are leaking here
	file = r_str_replace (file, ".class", ".java", 0);
	/*
	   int i;
	   RBinJavaObj *b = bf->bo->bin_obj;
	   for (i = 0; i < b->lines.count; i++) {
	        RBinDwarfRow *row = R_NEW0 (RBinDwarfRow);
	        r_bin_dwarf_line_new (row, b->lines.addr[i], file, b->lines.line[i]);
	        r_list_append (list, row);
	   }*/
	free (file);
	return list;
#endif
}

static bool imports_vec(RBinFile *bf) {
	r_bin_java_load_imports (bf->bo->bin_obj, &bf->bo->imports_vec);
	return true;
}

static RList *libs(RBinFile *bf) {
	return r_bin_java_get_lib_names (bf->bo->bin_obj);
}

static bool sections_vec(RBinFile *bf) {
	RVecRBinSection_clear (&bf->bo->sections_vec);
	return r_bin_java_load_sections (bf->bo->bin_obj, &bf->bo->sections_vec);
}

static const char *get_cc(RBinFile *bf, ut64 vaddr) {
	R_RETURN_VAL_IF_FAIL (bf && bf->rbin, NULL);
	RBinSymbol *m = r_bin_get_symbol_at (bf->rbin, vaddr);
	if (!m || !m->arg_prefix) {
		return NULL;
	}
	const bool instance = !(m->attr & R_BIN_ATTR_STATIC);
	RStrBuf *sb = r_strbuf_new ("dyncc:");
	if (!sb) {
		return NULL;
	}
	if (m->cc_arg_count > 0) {
		r_strbuf_appendf (sb, "%s%u+%u", m->arg_prefix, m->arg_first, m->cc_arg_count);
	}
	r_strbuf_append (sb, ":");
	if (m->ret_count > 0) {
		r_strbuf_appendf (sb, "r0+%u", m->ret_count);
	}
	if (instance && m->cc_arg_count > 0) {
		r_strbuf_append (sb, "!T0");
	}
	char *s = r_strbuf_drain (sb);
	const char *ret = r_str_constpool_get (&bf->rbin->constpool, s);
	free (s);
	return ret;
}

RBinPlugin r_bin_plugin_java = {
	.meta = {
		.name = "java",
		.author = "pancake",
		.desc = "Java Cafebabe Class",
		.license = "LGPL-3.0-only",
	},
	.get_sdb = &get_sdb, // XXX we should remove this imho
	.load = &load,
	.destroy = &destroy,
	.check = &check,
	.binsym = binsym,
	.entries = &entries,
	.sections_vec = &sections_vec,
	.symbols_vec = symbols_vec,
	.imports_vec = &imports_vec,
	.strings = &strings,
	.info = &info,
	.libs = libs,
	.lines = &lines,
	.classes = classes,
	.demangle_type = retdemangle,
	.get_cc = &get_cc,
	.minstrlen = 3,
};

#ifndef R2_PLUGIN_INCORE
R_API RLibStruct radare_plugin = {
	.type = R_LIB_TYPE_BIN,
	.data = &r_bin_plugin_java,
	.version = R2_VERSION
};
#endif
