changeset 10:a76ec3e7da09

simtool/pbupdate.c: a little refactoring from fc-uicc-tool
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 12 Feb 2021 00:47:04 +0000
parents dc565e91069d
children 62cdfed70de7
files simtool/pbupdate.c
diffstat 1 files changed, 29 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/simtool/pbupdate.c	Fri Feb 12 00:28:06 2021 +0000
+++ b/simtool/pbupdate.c	Fri Feb 12 00:47:04 2021 +0000
@@ -1,5 +1,5 @@
 /*
- * This module implements the pb-update command.
+ * This module implements the pb-update family of commands.
  */
 
 #include <sys/types.h>
@@ -8,7 +8,6 @@
 #include <strings.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include "simresp.h"
 #include "curfile.h"
 
 static u_char gsm7_encode_table[256] = {
@@ -94,14 +93,14 @@
 }
 
 static char *
-decode_qstring_alpha(cp, record, filename_for_errs, lineno_for_errs)
+decode_qstring_alpha(cp, record, maxlen, filename_for_errs, lineno_for_errs)
 	char *cp, *filename_for_errs;
 	u_char *record;
+	unsigned maxlen;
 {
-	unsigned maxlen, acclen, nadd;
+	unsigned acclen, nadd;
 	int c;
 
-	maxlen = curfile_record_len - 14;
 	for (acclen = 0; ; ) {
 		if (*cp == '\0') {
 unterm_qstring:		fprintf(stderr,
@@ -158,13 +157,13 @@
 }
 
 static char *
-decode_hex_alpha(cp, record, filename_for_errs, lineno_for_errs)
+decode_hex_alpha(cp, record, maxlen, filename_for_errs, lineno_for_errs)
 	char *cp, *filename_for_errs;
 	u_char *record;
+	unsigned maxlen;
 {
-	unsigned maxlen, acclen;
+	unsigned acclen;
 
-	maxlen = curfile_record_len - 14;
 	for (acclen = 0; ; ) {
 		if (!isxdigit(cp[0]) || !isxdigit(cp[1]))
 			break;
@@ -182,8 +181,10 @@
 }
 
 static
-process_record(line, filename_for_errs, lineno_for_errs)
+process_record(line, pb_record_len, pb_record_count, filename_for_errs,
+		lineno_for_errs)
 	char *line, *filename_for_errs;
+	unsigned pb_record_len, pb_record_count;
 {
 	unsigned recno;
 	u_char record[255], *fixp;
@@ -193,7 +194,7 @@
 	int c;
 
 	recno = strtoul(line+1, 0, 10);
-	if (recno < 1 || recno > curfile_record_count) {
+	if (recno < 1 || recno > pb_record_count) {
 		fprintf(stderr, "%s line %d: record number is out of range\n",
 			filename_for_errs, lineno_for_errs);
 		return(-1);
@@ -208,8 +209,8 @@
 	}
 	while (isspace(*cp))
 		cp++;
-	memset(record, 0xFF, curfile_record_len);
-	fixp = record + curfile_record_len - 14;
+	memset(record, 0xFF, pb_record_len);
+	fixp = record + pb_record_len - 14;
 	if (digit_char_to_gsm(*cp) < 0)
 		goto inv_syntax;
 	for (ndigits = 0; ; ndigits++) {
@@ -256,16 +257,16 @@
 	}
 	if (*cp == '"') {
 		cp++;
-		cp = decode_qstring_alpha(cp, record, filename_for_errs,
-					  lineno_for_errs);
+		cp = decode_qstring_alpha(cp, record, pb_record_len - 14,
+					  filename_for_errs, lineno_for_errs);
 		if (!cp)
 			return(-1);
 	} else if (!strncasecmp(cp, "HEX", 3)) {
 		cp += 3;
 		while (isspace(*cp))
 			cp++;
-		cp = decode_hex_alpha(cp, record, filename_for_errs,
-				      lineno_for_errs);
+		cp = decode_hex_alpha(cp, record, pb_record_len - 14,
+				      filename_for_errs, lineno_for_errs);
 		if (!cp)
 			return(-1);
 	} else
@@ -274,7 +275,7 @@
 		cp++;
 	if (*cp)
 		goto inv_syntax;
-	return update_rec_op(recno, 0x04, record, curfile_record_len);
+	return update_rec_op(recno, 0x04, record, pb_record_len);
 }
 
 cmd_pb_update(argc, argv)
@@ -303,7 +304,8 @@
 		}
 		if (linebuf[0] != '#' || !isdigit(linebuf[1]))
 			continue;
-		rc = process_record(linebuf, argv[2], lineno);
+		rc = process_record(linebuf, curfile_record_len,
+				    curfile_record_count, argv[2], lineno);
 		if (rc < 0) {
 			fclose(inf);
 			return(rc);
@@ -362,15 +364,15 @@
 }
 
 static
-decode_alphatag_arg(arg, record)
+decode_alphatag_arg(arg, record, maxlen)
 	char *arg;
 	u_char *record;
+	unsigned maxlen;
 {
-	unsigned maxlen, acclen, nadd;
+	unsigned acclen, nadd;
 	char *cp;
 	int c;
 
-	maxlen = curfile_record_len - 14;
 	cp = arg;
 	for (acclen = 0; *cp; ) {
 		c = *cp++;
@@ -440,7 +442,8 @@
 	if (rc < 0)
 		return(rc);
 	if (argv[4]) {
-		rc = decode_alphatag_arg(argv[4], record);
+		rc = decode_alphatag_arg(argv[4], record,
+					 curfile_record_len - 14);
 		if (rc < 0)
 			return(rc);
 	}
@@ -448,13 +451,13 @@
 }
 
 static
-decode_alphatag_arg_hex(arg, record)
+decode_alphatag_arg_hex(arg, record, maxlen)
 	char *arg;
 	u_char *record;
+	unsigned maxlen;
 {
-	unsigned maxlen, acclen;
+	unsigned acclen;
 
-	maxlen = curfile_record_len - 14;
 	for (acclen = 0; ; acclen++) {
 		while (isspace(*arg))
 			arg++;
@@ -496,7 +499,7 @@
 	rc = decode_number_arg(argv[3], fixp);
 	if (rc < 0)
 		return(rc);
-	rc = decode_alphatag_arg_hex(argv[4], record);
+	rc = decode_alphatag_arg_hex(argv[4], record, curfile_record_len - 14);
 	if (rc < 0)
 		return(rc);
 	return update_rec_op(recno, 0x04, record, curfile_record_len);