comparison 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
comparison
equal deleted inserted replaced
9:c9ef9e91dd8e 10:ddd767f6e15b
1 /*
2 * This module implements the write-sst command for admin-level
3 * programming of SIM cards.
4 */
5
6 #include <sys/types.h>
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <strings.h>
12 #include "curfile.h"
13 #include "file_id.h"
14
15 extern FILE *open_script_input_file();
16
17 cmd_write_sst(argc, argv)
18 char **argv;
19 {
20 u_char sst[255];
21 FILE *inf;
22 int lineno, rc;
23 char linebuf[1024], *cp, *np;
24 unsigned num, code, max_serv;
25
26 rc = select_op(DF_GSM);
27 if (rc < 0)
28 return(rc);
29 rc = select_op(EF_SST);
30 if (rc < 0)
31 return(rc);
32 rc = parse_ef_select_response();
33 if (rc < 0)
34 return(rc);
35 if (curfile_structure != 0x00) {
36 fprintf(stderr, "error: EF_SST is not transparent\n");
37 return(-1);
38 }
39 if (curfile_total_size < 2) {
40 fprintf(stderr,
41 "error: EF_SST is shorter than spec minimum of 2 bytes\n");
42 return(-1);
43 }
44 if (curfile_total_size > 255) {
45 fprintf(stderr,
46 "error: EF_SST is longer than our 255 byte limit\n");
47 return(-1);
48 }
49 memset(sst, 0, curfile_total_size);
50 max_serv = curfile_total_size * 4;
51 inf = open_script_input_file(argv[1]);
52 if (!inf) {
53 perror(argv[1]);
54 return(-1);
55 }
56 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
57 if (!index(linebuf, '\n')) {
58 fprintf(stderr,
59 "%s line %d: too long or missing newline\n",
60 argv[1], lineno);
61 fclose(inf);
62 return(-1);
63 }
64 for (cp = linebuf; ; ) {
65 while (isspace(*cp))
66 cp++;
67 if (*cp == '\0' || *cp == '#')
68 break;
69 if (!isdigit(*cp)) {
70 inv_syntax: fprintf(stderr, "%s line %d: invalid syntax\n",
71 argv[1], lineno);
72 fclose(inf);
73 return(-1);
74 }
75 num = strtoul(cp, 0, 10);
76 while (isdigit(*cp))
77 cp++;
78 if (*cp == '^') {
79 cp++;
80 code = 1;
81 } else
82 code = 3;
83 if (*cp && !isspace(*cp))
84 goto inv_syntax;
85 if (num < 1 || num > max_serv) {
86 fprintf(stderr,
87 "%s line %d: service number is out of range\n",
88 argv[1], lineno);
89 fclose(inf);
90 return(-1);
91 }
92 num--;
93 sst[num >> 2] |= code << ((num & 3) << 1);
94 }
95 }
96 fclose(inf);
97 return update_bin_op(0, sst, curfile_total_size);
98 }