view libutil/plmnlist.c @ 53:fbedb67d234f

serial: fix parity for inverse coding convention Important note: it is my (Mother Mychaela's) understanding that SIM cards with inverse coding convention are extremely rare, and I have never seen such a card. Therefore, our support for the inverse coding convention will likely remain forever untested.
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 21 Mar 2021 20:46:09 +0000
parents 34bbb0585cab
children
line wrap: on
line source

/*
 * This module implements a function for reading PLMN lists from files.
 */

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

extern FILE *open_script_input_file();

read_plmn_list_from_file(filename, buf, ef_len)
	char *filename;
	u_char *buf;
	unsigned ef_len;
{
	FILE *inf;
	int lineno, rc;
	char linebuf[1024], *cp, *np;
	u_char *dp, *endp;

	inf = open_script_input_file(filename);
	if (!inf) {
		perror(filename);
		return(-1);
	}
	dp = buf;
	endp = buf + ef_len;
	for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
		if (!index(linebuf, '\n')) {
			fprintf(stderr,
				"%s line %d: too long or missing newline\n",
				filename, lineno);
			fclose(inf);
			return(-1);
		}
		for (cp = linebuf; ; ) {
			while (isspace(*cp))
				cp++;
			if (*cp == '\0' || *cp == '#')
				break;
			for (np = cp; *cp && !isspace(*cp); cp++)
				;
			if (*cp)
				*cp++ = '\0';
			if (dp >= endp) {
				fprintf(stderr,
			"%s line %d: number of PLMN codes exceeds EF size\n",
					filename, lineno);
				fclose(inf);
				return(-1);
			}
			rc = encode_plmn_3bytes(np, dp);
			if (rc < 0) {
				fprintf(stderr, "%s line %d: invalid MCC-MNC\n",
					filename, lineno);
				fclose(inf);
				return(-1);
			}
			dp += 3;
		}
	}
	fclose(inf);
	while (dp < endp)
		*dp++ = 0xFF;
	return(0);
}