view libutil/plmnlist.c @ 25:9c9f6adbaedb

sim-iccid-mkrange utility added
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 15 Mar 2021 09:07:20 +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);
}