view simtool/sstprog.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 write-sst command for admin-level
 * programming of SIM cards.
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "curfile.h"
#include "file_id.h"

extern FILE *open_script_input_file();

cmd_write_sst(argc, argv)
	char **argv;
{
	u_char sst[255];
	FILE *inf;
	int lineno, rc;
	char linebuf[1024], *cp, *np;
	unsigned num, code, max_serv;

	rc = select_op(DF_GSM);
	if (rc < 0)
		return(rc);
	rc = select_op(EF_SST);
	if (rc < 0)
		return(rc);
	rc = parse_ef_select_response();
	if (rc < 0)
		return(rc);
	if (curfile_structure != 0x00) {
		fprintf(stderr, "error: EF_SST is not transparent\n");
		return(-1);
	}
	if (curfile_total_size < 2) {
		fprintf(stderr,
		"error: EF_SST is shorter than spec minimum of 2 bytes\n");
		return(-1);
	}
	if (curfile_total_size > 255) {
		fprintf(stderr,
			"error: EF_SST is longer than our 255 byte limit\n");
		return(-1);
	}
	memset(sst, 0, curfile_total_size);
	max_serv = curfile_total_size * 4;
	inf = open_script_input_file(argv[1]);
	if (!inf) {
		perror(argv[1]);
		return(-1);
	}
	for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
		if (!index(linebuf, '\n')) {
			fprintf(stderr,
				"%s line %d: too long or missing newline\n",
				argv[1], lineno);
			fclose(inf);
			return(-1);
		}
		for (cp = linebuf; ; ) {
			while (isspace(*cp))
				cp++;
			if (*cp == '\0' || *cp == '#')
				break;
			if (!isdigit(*cp)) {
inv_syntax:			fprintf(stderr, "%s line %d: invalid syntax\n",
					argv[1], lineno);
				fclose(inf);
				return(-1);
			}
			num = strtoul(cp, 0, 10);
			while (isdigit(*cp))
				cp++;
			if (*cp == '^') {
				cp++;
				code = 1;
			} else
				code = 3;
			if (*cp && !isspace(*cp))
				goto inv_syntax;
			if (num < 1 || num > max_serv) {
				fprintf(stderr,
				"%s line %d: service number is out of range\n",
					argv[1], lineno);
				fclose(inf);
				return(-1);
			}
			num--;
			sst[num >> 2] |= code << ((num & 3) << 1);
		}
	}
	fclose(inf);
	return update_bin_op(0, sst, curfile_total_size);
}