FreeCalypso > hg > fc-pcsc-tools
diff simtool/hlread.c @ 1:2071b28cd0c7
simtool: first refactored version
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 11 Feb 2021 23:04:28 +0000 |
parents | |
children | 4360a7906f34 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/simtool/hlread.c Thu Feb 11 23:04:28 2021 +0000 @@ -0,0 +1,125 @@ +/* + * This module implements some high-level or user-friendly read commands. + */ + +#include <sys/types.h> +#include <stdio.h> +#include <stdlib.h> +#include "simresp.h" +#include "curfile.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; + char buf[21]; + + rc = select_op(FILEID_MF); + if (rc < 0) + return(rc); + rc = select_op(EF_ICCID); + if (rc < 0) + return(rc); + rc = parse_ef_select_response(); + if (rc < 0) + return(rc); + if (curfile_structure != 0x00 || curfile_total_size != 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); +} + +cmd_imsi() +{ + int rc; + char buf[17]; + + rc = select_op(DF_GSM); + if (rc < 0) + return(rc); + rc = select_op(EF_IMSI); + if (rc < 0) + return(rc); + rc = parse_ef_select_response(); + if (rc < 0) + return(rc); + if (curfile_structure != 0x00 || curfile_total_size != 9) { + fprintf(stderr, "error: expected transparent EF of 9 bytes\n"); + return(-1); + } + rc = readbin_op(0, 9); + if (rc < 0) + return(rc); + decode_reversed_nibbles(sim_resp_data + 1, 8, buf); + buf[16] = '\0'; + printf("%s parity=%c len=%u\n", buf + 1, buf[0], sim_resp_data[0]); + return(0); +} + +cmd_spn() +{ + int rc; + unsigned textlen; + + rc = select_op(DF_GSM); + if (rc < 0) + return(rc); + rc = select_op(EF_SPN); + if (rc < 0) + return(rc); + rc = parse_ef_select_response(); + if (rc < 0) + return(rc); + if (curfile_structure != 0x00 || curfile_total_size != 17) { + fprintf(stderr, "error: expected transparent EF of 17 bytes\n"); + return(-1); + } + rc = readbin_op(0, 17); + if (rc < 0) + return(rc); + printf("Display condition: %02X\n", sim_resp_data[0]); + printf("SPN: "); + rc = validate_alpha_field(sim_resp_data + 1, 16, &textlen); + if (rc >= 0) + print_alpha_field(sim_resp_data, textlen, stdout); + else + printf("malformed alpha field"); + putchar('\n'); + return(0); +}