# HG changeset patch # User Mychaela Falconia # Date 1613098457 0 # Node ID b8d27c72747a8a0f7068e20d60f18e91af84c0d2 # Parent a6ca422323b972687180d739e177226271ec045d GSM7 encoding function factored out of pb-update-imm diff -r a6ca422323b9 -r b8d27c72747a libcommon/Makefile --- a/libcommon/Makefile Fri Feb 12 01:41:21 2021 +0000 +++ b/libcommon/Makefile Fri Feb 12 02:54:17 2021 +0000 @@ -1,9 +1,9 @@ CC= gcc CFLAGS= -O2 -I/usr/include/PCSC OBJS= alpha_decode.o alpha_valid.o apdu.o atr.o cardconnect.o chkblank.o \ - dumpdirfunc.o exit.o gsm7_encode_table.o hexdump.o hexread.o hexstr.o \ - names.o number_decode.o number_encode.o pbdumpfunc.o pinentry.o \ - revnibbles.o + dumpdirfunc.o exit.o gsm7_encode.o gsm7_encode_table.o hexdump.o \ + hexread.o hexstr.o names.o number_decode.o number_encode.o pbdumpfunc.o\ + pinentry.o revnibbles.o LIB= libcommon.a all: ${LIB} diff -r a6ca422323b9 -r b8d27c72747a libcommon/gsm7_encode.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libcommon/gsm7_encode.c Fri Feb 12 02:54:17 2021 +0000 @@ -0,0 +1,68 @@ +/* + * This module implements functions for parsing quoted string + * arguments intended for GSM7 string encoding, and actually + * encoding them into GSM7 binary strings. + */ + +#include +#include +#include + +extern u_char gsm7_encode_table[256]; + +qstring_arg_to_gsm7(arg, record, maxlen) + char *arg; + u_char *record; + unsigned maxlen; +{ + unsigned acclen, nadd; + char *cp; + int c; + + cp = arg; + for (acclen = 0; *cp; ) { + c = *cp++; + if (c == '\\') { + if (*cp == '\0') { + fprintf(stderr, + "error: dangling backslash escape\n"); + return(-1); + } + c = *cp++; + switch (c) { + case 'n': + c = '\n'; + break; + case 'r': + c = '\r'; + break; + case '"': + case '\\': + break; + default: + fprintf(stderr, + "error: non-understood backslash escape\n"); + return(-1); + } + } + c = gsm7_encode_table[c]; + if (c == 0xFF) { + fprintf(stderr, + "error: character in alpha tag string cannot be encoded in GSM7\n"); + return(-1); + } + if (c & 0x80) + nadd = 2; + else + nadd = 1; + if (acclen + nadd > maxlen) { + fprintf(stderr, + "error: alpha tag string is longer than SIM limit\n"); + return(-1); + } + if (c & 0x80) + record[acclen++] = 0x1B; + record[acclen++] = c & 0x7F; + } + return(acclen); +} diff -r a6ca422323b9 -r b8d27c72747a simtool/pbupd_imm.c --- a/simtool/pbupd_imm.c Fri Feb 12 01:41:21 2021 +0000 +++ b/simtool/pbupd_imm.c Fri Feb 12 02:54:17 2021 +0000 @@ -9,66 +9,6 @@ #include #include "curfile.h" -extern u_char gsm7_encode_table[256]; - -static -decode_alphatag_arg(arg, record, maxlen) - char *arg; - u_char *record; - unsigned maxlen; -{ - unsigned acclen, nadd; - char *cp; - int c; - - cp = arg; - for (acclen = 0; *cp; ) { - c = *cp++; - if (c == '\\') { - if (*cp == '\0') { - fprintf(stderr, - "error: dangling backslash escape\n"); - return(-1); - } - c = *cp++; - switch (c) { - case 'n': - c = '\n'; - break; - case 'r': - c = '\r'; - break; - case '"': - case '\\': - break; - default: - fprintf(stderr, - "error: non-understood backslash escape\n"); - return(-1); - } - } - c = gsm7_encode_table[c]; - if (c == 0xFF) { - fprintf(stderr, - "error: character in alpha tag string cannot be encoded in GSM7\n"); - return(-1); - } - if (c & 0x80) - nadd = 2; - else - nadd = 1; - if (acclen + nadd > maxlen) { - fprintf(stderr, - "error: alpha tag string is longer than SIM limit\n"); - return(-1); - } - if (c & 0x80) - record[acclen++] = 0x1B; - record[acclen++] = c & 0x7F; - } - return(0); -} - cmd_pb_update_imm(argc, argv) char **argv; { @@ -90,7 +30,7 @@ if (rc < 0) return(rc); if (argv[4]) { - rc = decode_alphatag_arg(argv[4], record, + rc = qstring_arg_to_gsm7(argv[4], record, curfile_record_len - 14); if (rc < 0) return(rc);