view simtool/readef.c @ 10:ddd767f6e15b

fc-simtool ported over
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 07:11:25 +0000
parents
children 49b7e02787c1
line wrap: on
line source

/*
 * This module implements the readef command for dumping the complete
 * content of SIM files.
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "simresp.h"
#include "curfile.h"

static void
hexdump_with_offset(outf, extoff)
	FILE *outf;
	unsigned extoff;
{
	unsigned off, cc, n, c;

	for (off = 0; off < sim_resp_data_len; off += cc) {
		fprintf(outf, "%04X:", extoff + off);
		cc = 16;
		if (sim_resp_data_len - off < cc)
			cc = sim_resp_data_len - off;
		for (n = 0; n < 16; n++) {
			if (n == 0 || n == 8)
				putc(' ', outf);
			putc(' ', outf);
			if (n < cc)
				fprintf(outf, "%02X", sim_resp_data[off + n]);
			else {
				putc(' ', outf);
				putc(' ', outf);
			}
		}
		putc(' ', outf);
		putc(' ', outf);
		for (n = 0; n < cc; n++) {
			c = sim_resp_data[off + n];
			if (c < 0x20 || c > 0x7E)
				c = '.';
			putc(c, outf);
		}
		putc('\n', outf);
	}
}

static
readef_transparent(outf)
	FILE *outf;
{
	unsigned off, cc;
	int rc;

	for (off = 0; off < curfile_total_size; off += cc) {
		cc = curfile_total_size - off;
		if (cc > 256)
			cc = 256;
		rc = readbin_op(off, cc);
		if (rc < 0)
			return(rc);
		hexdump_with_offset(outf, off);
	}
	return(0);
}

static
readef_records(outf)
	FILE *outf;
{
	unsigned recno;
	int rc;

	for (recno = 1; recno <= curfile_record_count; recno++) {
		fprintf(outf, "Record #%u:\n", recno);
		rc = readrec_op(recno, 0x04, curfile_record_len);
		if (rc < 0)
			return(rc);
		display_sim_resp_in_hex(outf);
	}
	return(0);
}

cmd_readef(argc, argv, outf)
	char **argv;
	FILE *outf;
{
	int file_id, rc;
	unsigned readlen;

	if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
	    isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
		file_id = strtoul(argv[1], 0, 16);
	else
		file_id = find_symbolic_file_name(argv[1]);
	if (file_id < 0) {
		fprintf(stderr,
"error: file ID argument is not a hex value or a recognized symbolic name\n");
		return(-1);
	}
	rc = select_op(file_id);
	if (rc < 0)
		return(rc);
	rc = parse_ef_select_response();
	if (rc < 0)
		return(rc);
	show_access_conditions(outf, "UPDATE", sim_resp_data[8] & 0xF);
	show_access_conditions(outf, "READ & SEEK", sim_resp_data[8] >> 4);
	show_access_conditions(outf, "INCREASE", sim_resp_data[9] >> 4);
	show_access_conditions(outf, "INVALIDATE", sim_resp_data[10] & 0xF);
	show_access_conditions(outf, "REHABILITATE", sim_resp_data[10] >> 4);
	fprintf(outf, "File status: %02X\n", sim_resp_data[11]);
	switch (curfile_structure) {
	case 0x00:
		fprintf(outf, "Transparent EF of %u byte(s)\n",
			curfile_total_size);
		return readef_transparent(outf);
	case 0x01:
		fprintf(outf, "%u records of %u bytes (linear fixed)\n",
			curfile_record_count, curfile_record_len);
		return readef_records(outf);
	case 0x03:
		fprintf(outf, "%u records of %u bytes (cyclic)\n",
			curfile_record_count, curfile_record_len);
		return readef_records(outf);
	}
}