view libcommon/gsm7_decode.c @ 74:8562d8508cf2

grcard2-set-{adm,super}-hex commands implemented It appears that GrcardSIM2 cards allow arbitrary 64-bit keys for ADM and SUPER ADM, not necessarily consisting of ASCII digits like the specs require for standard PIN and PUK, and pySim-prog.py in fact sets the ADM key to 4444444444444444 in hex by default, which is not an ASCII digit string. If the cards allow such keys, we need to support them too.
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 16 Feb 2021 04:10:36 +0000
parents d4dc86195382
children
line wrap: on
line source

/*
 * This module contains functions for decoding GSM7 strings
 * that exist in various SIM files.
 */

#include <sys/types.h>
#include <stdio.h>

static char gsm7_decode_table[128] = {
	'@', 0,   '$',  0,   0,   0,    0,   0,
	0,   0,   '\n', 0,   0,   '\r', 0,   0,
	0,   '_', 0,    0,   0,   0,    0,   0,
	0,   0,   0,    0,   0,   0,    0,   0,
	' ', '!', '"',  '#', 0,   '%',  '&', 0x27,
	'(', ')', '*',  '+', ',', '-',  '.', '/',
	'0', '1', '2',  '3', '4', '5',  '6', '7',
	'8', '9', ':',  ';', '<', '=',  '>', '?',
	0,   'A', 'B',  'C', 'D', 'E',  'F', 'G',
	'H', 'I', 'J',  'K', 'L', 'M',  'N', 'O',
	'P', 'Q', 'R',  'S', 'T', 'U',  'V', 'W',
	'X', 'Y', 'Z',  0,   0,   0,    0,   0,
	0,   'a', 'b',  'c', 'd', 'e',  'f', 'g',
	'h', 'i', 'j',  'k', 'l', 'm',  'n', 'o',
	'p', 'q', 'r',  's', 't', 'u',  'v', 'w',
	'x', 'y', 'z',  0,   0,   0,    0,   0
};

static char gsm7ext_decode_table[128] = {
	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
	0,   0, 0, 0, '^', 0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
	0,   0, 0, 0, 0,   0, 0, 0, '{', '}', 0, 0, 0,   0,   0,   '\\',
	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, '[', '~', ']', 0,
	'|', 0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0,
	0,   0, 0, 0, 0,   0, 0, 0, 0,   0,   0, 0, 0,   0,   0,   0
};

void
print_gsm7_string_to_file(data, nbytes, outf)
	u_char *data;
	unsigned nbytes;
	FILE *outf;
{
	u_char *dp, *endp;
	int b, c;

	dp = data;
	endp = data + nbytes;
	putc('"', outf);
	while (dp < endp) {
		b = *dp++;
		if (b == 0x1B) {
			if (dp >= endp || *dp == 0x1B || *dp == '\n' ||
			    *dp == '\r') {
				putc('\\', outf);
				putc('e', outf);
				continue;
			}
			b = *dp++;
			c = gsm7ext_decode_table[b];
			if (!c) {
				fprintf(outf, "\\e\\%02X", b);
				continue;
			}
		} else {
			c = gsm7_decode_table[b];
			if (!c) {
				fprintf(outf, "\\%02X", b);
				continue;
			}
		}
		if (c == '\n') {
			putc('\\', outf);
			putc('n', outf);
			continue;
		}
		if (c == '\r') {
			putc('\\', outf);
			putc('r', outf);
			continue;
		}
		if (c == '"' || c == '\\')
			putc('\\', outf);
		putc(c, outf);
	}
	putc('"', outf);
}