view libcommon/hexread.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 f7145c77b7fb
children 94d87d05f6c5
line wrap: on
line source

/*
 * This module contains the function for reading hex files,
 * to be used in the implementation of manual write commands.
 */

#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>

read_hex_data_file(filename, databuf)
	char *filename;
	u_char *databuf;
{
	FILE *inf;
	unsigned count;
	int c, c2;

	inf = fopen(filename, "r");
	if (!inf) {
		perror(filename);
		return(-1);
	}
	for (count = 0; ; count++) {
		do
			c = getc(inf);
		while (isspace(c));
		if (c < 0)
			break;
		if (!isxdigit(c)) {
inv_input:		fprintf(stderr, "%s: invalid hex file input\n",
				filename);
			fclose(inf);
			return(-1);
		}
		c2 = getc(inf);
		if (!isxdigit(c2))
			goto inv_input;
		if (count >= 255) {
			fprintf(stderr, "%s: hex input data is too long\n",
				filename);
			fclose(inf);
			return(-1);
		}
		databuf[count] = (decode_hex_digit(c) << 4) |
				 decode_hex_digit(c2);
	}
	fclose(inf);
	if (!count) {
		fprintf(stderr, "%s: no hex data input found\n", filename);
		return(-1);
	}
	return(count);
}