view simtool/readcmd.c @ 91:226b231d00f3

fc-simtool: readrec command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 24 Jan 2021 18:46:11 +0000
parents 53e2c00566af
children 0ead9444a698
line wrap: on
line source

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <pcsclite.h>
#include <winscard.h>
#include "globals.h"

cmd_readbin(argc, argv)
	char **argv;
{
	unsigned offset, len;
	u_char cmd[5];
	int rc;

	offset = strtoul(argv[1], 0, 0);
	if (offset > 0xFFFF) {
		fprintf(stderr, "error: offset argument is out of range\n");
		return(-1);
	}
	len = strtoul(argv[2], 0, 0);
	if (len < 1 || len > 256) {
		fprintf(stderr, "error: length argument is out of range\n");
		return(-1);
	}
	/* READ BINARY command APDU */
	cmd[0] = 0xA0;
	cmd[1] = 0xB0;
	cmd[2] = offset >> 8;
	cmd[3] = offset;
	cmd[4] = len;
	rc = apdu_exchange(cmd, 5);
	if (rc < 0)
		return(rc);
	if (sim_resp_sw != 0x9000) {
		fprintf(stderr, "bad SW response to READ BINARY: %04X\n",
			sim_resp_sw);
		return(-1);
	}
	if (sim_resp_data_len != len) {
		fprintf(stderr,
			"error: READ BINARY returned %u bytes, expected %u\n",
			sim_resp_data_len, len);
		return(-1);
	}
	display_sim_resp_in_hex();
	return(0);
}

cmd_readrec(argc, argv)
	char **argv;
{
	unsigned recno, len;
	u_char cmd[5];
	int rc;

	recno = strtoul(argv[1], 0, 0);
	if (recno < 1 || recno > 255) {
		fprintf(stderr,
			"error: record number argument is out of range\n");
		return(-1);
	}
	if (argv[2]) {
		len = strtoul(argv[2], 0, 0);
		if (len < 1 || len > 255) {
			fprintf(stderr,
				"error: length argument is out of range\n");
			return(-1);
		}
	} else {
		if (!curfile_record_len) {
			fprintf(stderr,
			"error: no current file record length is available\n");
			return(-1);
		}
		len = curfile_record_len;
	}
	/* READ RECORD command APDU */
	cmd[0] = 0xA0;
	cmd[1] = 0xB2;
	cmd[2] = recno;
	cmd[3] = 0x04;
	cmd[4] = len;
	rc = apdu_exchange(cmd, 5);
	if (rc < 0)
		return(rc);
	if (sim_resp_sw != 0x9000) {
		fprintf(stderr, "bad SW response to READ RECORD: %04X\n",
			sim_resp_sw);
		return(-1);
	}
	if (sim_resp_data_len != len) {
		fprintf(stderr,
			"error: READ RECORD returned %u bytes, expected %u\n",
			sim_resp_data_len, len);
		return(-1);
	}
	display_sim_resp_in_hex();
	return(0);
}