view uicc/hlread.c @ 158:65a2a96386cd

fc-uicc-tool: hex string parsing with min and max length, matching fc-simtool
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 07 Feb 2021 04:00:27 +0000
parents 69628bcfec17
children
line wrap: on
line source

/*
 * This module implements some high-level or user-friendly read commands.
 */

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

encode_hex_digit(d)
	unsigned d;
{
	if (d <= 9)
		return(d + '0');
	else
		return(d - 10 + 'A');
}

decode_reversed_nibbles(bytes, nbytes, dest)
	u_char *bytes;
	unsigned nbytes;
	char *dest;
{
	u_char *sp;
	char *dp;
	unsigned n, c;

	sp = bytes;
	dp = dest;
	for (n = 0; n < nbytes; n++) {
		c = *sp & 0xF;
		*dp++ = encode_hex_digit(c);
		c = *sp >> 4;
		*dp++ = encode_hex_digit(c);
		sp++;
	}
}

cmd_iccid()
{
	int rc;
	unsigned len;
	char buf[21];

	rc = select_op(FILEID_MF);
	if (rc < 0)
		return(rc);
	rc = select_op(EF_ICCID);
	if (rc < 0)
		return(rc);
	rc = select_resp_get_transparent(&len);
	if (rc < 0)
		return(rc);
	if (len != 10) {
		fprintf(stderr, "error: expected transparent EF of 10 bytes\n");
		return(-1);
	}
	rc = readbin_op(0, 10);
	if (rc < 0)
		return(rc);
	decode_reversed_nibbles(sim_resp_data, 10, buf);
	buf[20] = '\0';
	printf("%s\n", buf);
	return(0);
}