view libutil/plmncodes.c @ 8:34bbb0585cab

libutil: import from previous fc-pcsc-tools version
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 05:42:37 +0000
parents
children
line wrap: on
line source

/*
 * This module implements some functions for working with MCC-MNC PLMN codes.
 */

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

decode_plmn_3bytes(bin, asc, space_pad)
	u_char *bin;
	char *asc;
{
	asc[0] = encode_hex_digit(bin[0] & 0xF);
	asc[1] = encode_hex_digit(bin[0] >> 4);
	asc[2] = encode_hex_digit(bin[1] & 0xF);
	asc[3] = '-';
	asc[4] = encode_hex_digit(bin[2] & 0xF);
	asc[5] = encode_hex_digit(bin[2] >> 4);
	asc[6] = encode_hex_digit(bin[1] >> 4);
	asc[7] = '\0';
	if (asc[6] == 'F') {
		if (space_pad)
			asc[6] = ' ';
		else
			asc[6] = '\0';
	}
}

encode_plmn_3bytes(asc, bin)
	char *asc;
	u_char *bin;
{
	u_char mcc[3], mnc[3];

	if (!isxdigit(asc[0]) || !isxdigit(asc[1]) || !isxdigit(asc[2]))
		return(-1);
	mcc[0] = decode_hex_digit(asc[0]);
	mcc[1] = decode_hex_digit(asc[1]);
	mcc[2] = decode_hex_digit(asc[2]);
	asc += 3;
	if (*asc == '-')
		asc++;
	if (!isxdigit(asc[0]) || !isxdigit(asc[1]))
		return(-1);
	mnc[0] = decode_hex_digit(asc[0]);
	mnc[1] = decode_hex_digit(asc[1]);
	asc += 2;
	if (*asc == '\0')
		mnc[2] = 0xF;
	else if (isxdigit(asc[0]) && asc[1] == '\0')
		mnc[2] = decode_hex_digit(*asc);
	else
		return(-1);
	bin[0] = (mcc[1] << 4) | mcc[0];
	bin[1] = (mnc[2] << 4) | mcc[2];
	bin[2] = (mnc[1] << 4) | mnc[0];
	return(0);
}