view simtool/pnndump.c @ 10:ddd767f6e15b

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

/*
 * This module implements the pnn-dump command, providing a
 * user-accessible way to identify MVNO SIMs.
 */

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

select_ef_pnn()
{
	int rc;

	rc = select_op(DF_GSM);
	if (rc < 0)
		return(rc);
	rc = select_op(EF_PNN);
	if (rc < 0)
		return(rc);
	rc = parse_ef_select_response();
	if (rc < 0)
		return(rc);
	if (curfile_structure != 0x01) {
		fprintf(stderr, "error: EF_PNN is not linear fixed\n");
		return(-1);
	}
	if (curfile_record_len < 3) {
		fprintf(stderr,
"error: EF_PNN record length is less than the spec minimum of 3 bytes\n");
		return(-1);
	}
	return(0);
}

static void
dump_record(recno, outf)
	unsigned recno;
	FILE *outf;
{
	u_char *dp, *endp;
	char *name_kw;
	unsigned ielen, code_byte, nsept;
	u_char gsm7_buf[288];

	fprintf(outf, "#%u:", recno);
	dp = sim_resp_data;
	endp = sim_resp_data + sim_resp_data_len;
	while (dp < endp) {
		if (*dp == 0xFF)
			break;
		switch (*dp++) {
		case 0x43:
			name_kw = "Ln";
			break;
		case 0x45:
			name_kw = "Sn";
			break;
		default:
			fprintf(outf, " unknown-IEI\n");
			return;
		}
		if (dp >= endp) {
			fprintf(outf, " truncated-IE\n");
			return;
		}
		ielen = *dp++;
		if (ielen < 1 || ielen > (endp - dp)) {
			fprintf(outf, " bad-length\n");
			return;
		}
		code_byte = *dp++;
		ielen--;
		fprintf(outf, " %s=0x%02X", name_kw, code_byte);
		if (!ielen)
			continue;
		putc(',', outf);
		if ((code_byte & 0x70) == 0) {
			nsept = ielen * 8 / 7;
			gsm7_unpack(dp, gsm7_buf, nsept);
			dp += ielen;
			print_gsm7_string_to_file(gsm7_buf, nsept, outf);
		} else {
			for (; ielen; ielen--)
				fprintf(outf, "%02X", *dp++);
		}
	}
	for (; dp < endp; dp++) {
		if (*dp != 0xFF) {
			fprintf(outf, " bad-padding\n");
			return;
		}
	}
	putc('\n', outf);
}

cmd_pnn_dump(argc, argv, outf)
	char **argv;
	FILE *outf;
{
	int rc;
	unsigned recno;

	rc = select_ef_pnn();
	if (rc < 0)
		return(rc);
	for (recno = 1; recno <= curfile_record_count; recno++) {
		rc = readrec_op(recno, 0x04, curfile_record_len);
		if (rc < 0)
			return(rc);
		if (check_simresp_all_blank())
			continue;
		dump_record(recno, outf);
	}
	return(0);
}